0

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?

2 Answers2

1

i'm going to assume you are only talking about the ioctl system call here. remember that there are many many more system calls out there and many of them do not operate on devices at all.

why do you think the cmd argument (the 3rd one) must have the major number encoded in it ? the set of ioctl commands for any specific driver tend to pick an arbitrary value and encode that in its bits. this is so it can be decoded easily by other tools. but that value is not (or is not required to be) the major number.

the open/valid fd is used to route file based syscalls like read and write and ioctl.

you might want to look at the ioctl Documentation like Documentation/ioctl/ioctl-number.txt and Documentation/ioctl/ioctl-decoding.txt.

Mike Frysinger
  • 2,827
  • 1
  • 21
  • 26
1
  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.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153