0
    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?

mrigendra
  • 1,472
  • 3
  • 19
  • 33

1 Answers1

1

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;
Federico
  • 3,782
  • 32
  • 46
  • 1
    `copy_{to,from}_user}` do not return an error code; they return the number of bytes *not* copied (which is `0` on success). If the `copy_...` function returns a non-zero result, the function should probably return `-EINVAL`. – Keith Thompson Dec 23 '13 at 17:30
  • the thing is i am getting N INFINITE amount of outout on terminal...how to control it? – mrigendra Dec 24 '13 at 10:59
  • @KeithThompson Yeah, you are right :) Fast answer without think, I edit my answer – Federico Dec 24 '13 at 15:06
  • @mrigendra did you fix your module? Or it is a new issue? – Federico Dec 24 '13 at 15:08
  • its not a new issue i am having this problemm for 2 days now, and i think the problem is in returning, but i can't figure out how to manage it... – mrigendra Dec 24 '13 at 15:43
  • printing only stops when driver knows that it work is over, how did it knows that it has outputted all the bytes that is to be printed?if i return bfcount it will print that requested amount of bytes continuosly and if i return ret that is 0 so nothing will be printed. – mrigendra Dec 24 '13 at 17:05
  • You wrote a continuous read program, and it works great if it outputs forever: it is what you wrote. `cat` is a program that read until `EOF`, you wrote a driver that outputs data every time. So you are outputting data forever. – Federico Dec 27 '13 at 20:41