Reputation: 2593
I read some material on major and minor numbers and have doubts in it. What I understood:
Upvotes: 3
Views: 3071
Reputation: 57784
When open()
is called and the file entry contains a major/minor pair, open finds the device driver which has a corresponding struct device
that contains the same major/minor pair. The major
number alone is not enough to open a device.
Modern drivers should have their major number dynamically allocated by the kernel by leaving the dev_num set to zero when calling alloc_chrdev_region (&dev_num, 0, <number of contiguous minors to reserve>, DEVICE_NAME)
If the call succeeds, MAJOR(dev_num)
is the dynamically allocated major device number (and MINOR(dev_num)
has the minor device number).
Upvotes: 1
Reputation: 411
When you have multiple drivers associated with the same major number, you can differentiate between them through different minor number ranges under individual drivers. You can use the minor number as an index to a local array to access individual devices.
Also, it is advisable to use alloc_chrdev_region() to get the major number from the kernel dynamically rather than hardcoding a number that's currently free through register_chrdev_region().
Hope this helps!
Upvotes: 1