Search code examples
linuxlinux-kernelioctl

Kernel modules and major numbers


I have been reading up on drivers implemented as kernel modules and am confused about the CMD argument to the system call. It seems that the CMD argument to the system call encodes amongst other information, the major number of the device. Why is that? Is this information absolutely necessary?

Suppose I perform a write to my device as "echo 5 > /dev/mytestdevice". I do not specify the major number, hence I believe that the kernel already has a means to associate the device with its kernel module. If that is the case, why do I need to provide that information in an ioctl call to the device(as I pass the fd to the device as the first argument)?

Summarising my questions are :-

  1. When I do a "echo 5 > /dev/mytestdevice", how does the kernel find the driver corresponding to that device?
  2. Why is the major number passed as an argument to an ioctl call?

Solution

    1. When I do a "echo 5 > /dev/mytestdevice", how does the kernel find the driver corresponding to that device?

    Inode for device file /dev/mytestdevice is a special: instead of being associated with some file's content, it just contains major and minor numbers. When you tried to perform some operation with such file, the kernel firstly searches registered device with these identificators, and then redirect the operation for the device found.

    1. Why is the major number passed as an argument to an ioctl call?

    It is not a true: cmd argument for ioctl call doesn't contain major number.