mrigendra
mrigendra

Reputation: 1576

I am unable to use echo and cat in this simple device driver module.

    static ssize_t device_read (struct file* filp, char *bufStoreData, size_t bufCount, loff_t* curOffset)
    {
      printk(KERN_INFO"reading from the device");
      ret = copy_to_user(bufStoreData,virtual_device.data,bufCount);
      return ret;
    }

    static ssize_t device_write(struct file *filp,const char* bufSourceData,size_t bufCount, loff_t* curOffset)
    {
      printk(KERN_INFO"writing to device");
      ret=copy_from_user(virtual_device.data,bufSourceData,bufCount);
      return ret;
    }

I was using echo and cat command to do the user write and read but i was not reading the data properly. Maybe i am not returning right values.Is that so?

Upvotes: 0

Views: 757

Answers (1)

Federico
Federico

Reputation: 3902

device_read() and device_write() return value is the number of read/written bytes. copy_to_user() and copy_from_user() return 0 if all bytes were copied, otherwise the numer of bytes not copied.

Probably your operation succeed and you are returning 0, which means "0 byte copied".

You must return bufCount on success and a negative error code on fail.

ret=copy_from_user(virtual_device.data,bufSourceData,bufCount);
if (ret)
    return ret;

return bufCount;

Upvotes: 1

Related Questions