Search code examples
clinux-kernellinux-device-driverdevice-driver

The call-back function (scull_read) for read system call of a custum character device being called infinity times while reading using $cat


I have written a character device driver to toggle two GPIO pins. For reading the device, callback function is scull_read(). The normal operations of the device open, read, write and close is functioning normally, If I am performing everything from the C program, but while performing the above operations from terminal using echo and cat, the callback functions for read and write (scull_read() and scull_write()) are being called for infinity times. At this stage, the kill signal ^c to stop the process is not working for scull_write().

A test code is already available with name: strong text

Can you please help me with this.

Here is a github link: https://github.com/guruprasad-92/Device-Driver.git


Solution

  • cat (and presumably echo) will call your read function in a loop and terminate once your read function returns 0, indicating that the end of the file has been reached. You can check the source here.

    It is up to you to determine when the "end of file" has been reached. Typically, this is done by updating the file offset each time your read function is called. (This is the parameter you call f_pos.)

    So, you need to do something like *f_pos += count in scull_read. Then, when a program calls scull_read again on the same file, you can check *f_pos to determine whether or not there is still data to be read. If there is no data left to read, scull_read should return 0.

    Perhaps you will find this useful.