Skip to content
Advertisement

How to invoke scull_open() in user-space, where scull_open is defined in scull kernel driver

I am newbie to Linux device driver programming. I am just trying to use the scull driver which is explained in Linux Device Drivers.

Would like to invoke the scull_open() from user-space for write/read or close operations.

Currently I’m able to insert the scull module successfully using insmod and got the major number too. I also got the dev node /dev/scull0 using mknod /dev/scull0 c 251 0.

Next i used the below statement to open my newly created scull device

file_d = scull_open("/dev/scull0", 0);

But I got the following error:

undefined reference to `scull_open'

I used gcc for compilation.

Do I need to link any library or header files to make use of scull driver?
Please explain how should i open scull driver.

Advertisement

Answer

Do I need to link any library or header files to make use of scull driver?

No you do NOT need to use any additional libraries/headers to make this work.


In the example scull driver from LDD3 book, scull_open() is the handler function that is implemented to handle the open() call from user-space on the /dev/scull0 device.

Please go ahead and update your userspace app to

file_d = open("/dev/scull0", 0);

When running the updated app, if the module is insmod-ed and /dev/scull0 exists, executing the above line will result in scull_open() being called within your Linux kernel driver module immediately.

So how does open() end up calling scull_open() ?

Remember scull_fops() within your scull driver code?

Defined as…

struct file_operations scull_fops = {
    .owner =    THIS_MODULE,
    .llseek =   scull_llseek,
    .read =     scull_read,
    .write =    scull_write,
    .unlocked_ioctl =    scull_ioctl,
    .open =     scull_open,
    .release =  scull_release,
};

and used as…

cdev_init(&dev->cdev, &scull_fops); 

The above steps essentially associate the various functions listed within scull_fops with the scull device (eg. /dev/scull0).

Specifically, scull_open() is associated with open(). In other words,

  • scull_open()
  • is registered as the handler to open()
  • within the scull driver.
  1. In user-space, the call to open() is seen by the Linux kernel.
  2. The kernel checks for the driver responsible for creating the /dev/scull0 device.
  3. Next the kernel checks to see which is the function handler registered to handle open within the scull driver and calls it; in this case scull_open().

1. On a side-note, it is always a good idea to use the relevant macros/enums to clarify the context/intent. For example, use O_RDONLY instead of 0 as the 2nd parameter to the call to open().

2. Also FYI, since you are following the LDD3 book, here is a list of unconfirmed / confirmed mistakes in the book.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement