</>EmbeddedInterview

What are major and minor numbers?

EasyLinux Kernel & Device Drivers

Short answer: Every device node has a device number (dev_t) made of a major number, which identifies the driver, and a minor number, which the driver uses to identify the device instance. ls -l /dev/ttyS0 shows 4, 64 (major 4, minor 64). Use MAJOR(), MINOR() and MKDEV() to work with them, and allocate numbers dynamically with alloc_chrdev_region rather than hard-coding a major.

Details

  • dev_t is 32 bits: 12 bits major and 20 bits minor.
  • Character and block devices have separate number spaces; /proc/devices lists registered majors.
  • The /dev node is just a pointer (type + major + minor); mknod /dev/x c 240 0 creates one manually, but devtmpfs and udev normally create them automatically when device_create() is called.
  • In open(), a driver can find the instance with iminor(inode) or, better, container_of(inode->i_cdev, struct mydev, cdev).
  • Well-known fixed majors exist (1 = mem, 4 = tty, 8 = SCSI disks), documented in Documentation/admin-guide/devices.txt; new drivers should not take fixed ones.
  • Misc devices all share major 10 with different minors.