Linux Device Driver - what's wrong with my device_read()?
Posted
by Rob
on Stack Overflow
See other posts from Stack Overflow
or by Rob
Published on 2010-04-14T11:59:25Z
Indexed on
2010/04/14
12:03 UTC
Read the original article
Hit count: 615
My device /dev/my_inc is meant to take a positive integer N represented as an ascii string, and store it. Any read from /dev/my_inc will produce the ascii string representation of N + 1.
The problem is that when I cat /dev/my_inc, I only get the first byte of myinc_value output to my shell, even though I have bytes_read == 2 at the end of my loop.
/* Read from the device
*********************/
static ssize_t device_read(struct file *filp, char *buffer, size_t length, loff_t *offset)
{
char c;
int bytes_read = 0;
int value = myinc_value + 1;
printk(KERN_INFO "my_inc device_read() called with value %d and msg %s.\n", value, msg);
/* No bytes read if pointer to 0 */
if (*msg_ptr == 0)
return 0;
/* Put the incremented value in msg */
snprintf(msg, MAX_LENGTH, "%d", value);
/* Put bytes from msg into user space buffer */
while (length && *msg_ptr)
{
c = *(msg_ptr++);
printk(KERN_INFO "%s device_read() read %c.", DEV_NAME, c);
if(put_user(c, buffer++))
return -EFAULT;
length--;
bytes_read++;
}
printk("my_inc device_read() returning %d.\n", bytes_read);
/* Return bytes copied */
return bytes_read;
}
© Stack Overflow or respective owner