debonair
debonair

Reputation: 2593

major and minor numbers in device drivers

I read some material on major and minor numbers and have doubts in it. What I understood:

  1. driver has one major number associated with it due to register_chrdev_region().
  2. mknod /dev/hello1 -c 123 32 will create device file with major number 123 and when application opens /dev/hello1 it searches driver with major number same as /dev/hello1.
  3. multiple device file can be associated with one driver. and none of the two files in /dev will share same pair of major and minor number.
  4. now some modern operating systems allows drivers with same major numbers. Now in this case how mapping will work?

Upvotes: 3

Views: 3071

Answers (2)

wallyk
wallyk

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

rurtle
rurtle

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

Related Questions