LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Kernel module "read()" return value (https://www.linuxquestions.org/questions/programming-9/kernel-module-read-return-value-800477/)

shahzebihsan 04-06-2010 10:09 PM

Kernel module "read()" return value
 
Hi,

I wrote a character device driver for an Embedded Linux build (Kernel Revision 2.6.17.1) running on ARM9. In the module's "read()" function, I'm returning the number of bytes that were copied into the user buffer. I saw some module code samples online and in most cases the "read()" function is returning the number of bytes read.

The very strange behavior I saw in my case was, if I use a variable to return the value from the module's "read()" function, the user application reading from the module always saw "0".

For instance:

Code:

static int zlg7289_read(struct file *file, char *buf, size_t count, loff_t *ppos)
{
  int bytes = 0;
 
  // Read bytes, copy to user space using "put_user()"
  ...
  ...
  ...
 
  // Added this for debugging
  if(bytes) printk("Bytes: %d\n", bytes);

  return (bytes);
}

If I use the code given above, the user application always got back "0". In case I returned a hard-coded value, it worked fine, i.e.:

Code:

  .
  .
  .
  // Any number here works fine
  return (1);

Any explanation for this behavior?

Thanks!
- Shahzeb

Aquarius_Girl 04-15-2010 02:32 AM

Does your following code print the correct value ?
Code:

// Added this for debugging
  if(bytes) printk("Bytes: %d\n", bytes);

Also show how and where you are handling the variable which holds the return value of zlg7289_read !


All times are GMT -5. The time now is 01:15 PM.