Linux primary and secondary device number introduction

  
 1. The function of the main device number and the minor device number
In the Linux kernel, the main device number identifies the driver corresponding to the device, telling the Linux kernel which driver to use for the device (that is, the device file under /dev) Service; the secondary device number is used to identify a specific and unique device.
For example, in the terminal of linux, enter the command:
**@ubuntu:/dev$ ls –l crw-rw—- 1 root root 4, 0 2010-05-25 06:50 tty0 crw— —- 1 root root 4, 1 2010-05-25 06:51 tty1
There will be a lot of file lists, here for example string device files [string device beginning with c, of course block device is represented as b 】, these files can be called nodes of the file system book, all located in the /dev directory. In addition, the numbers in the above 2 lines, 4 [purple red], 0, 1 [blue] respectively indicate the major device number and the minor device number of the device. A primary device number and a secondary device number form a unique identifier for the device. Although the current Linux kernel allows multiple drivers to share the major device number, most devices are still organized according to the principle that a master device number corresponds to a driver. The minor device number is used to point to the device implemented by the driver, and the kernel itself does not substantially care about any other information about the minor device number.
View the system has been assigned the main device:
#cat /proc/devices
2, the internal representation of the device number
device type is dev_t type (2.4 kernel is kdev_t), in Defined in <linux/coda.h>.
typedef unsigned long dev_t ;
where dev_t is a 32-bit number, 12 bits represent the major device number, and 20 bits represent the minor device number.
The 2.6 kernel extends the major device number from 8 bits to 12 bits, while the minor device number is extended from 8 bits to 20 bits.
The method of obtaining the major device number and the minor device number is as follows:
MAJOR(dev_t dev): Get the major device number according to the device number dev; MINOR(dev_t dev): Get the minor device number according to the device number dev; > The above macros are defined in <linux/kdev_t.h> as follows:
#define MAJOR(dev) ((unsigned int) ((dev) >>MINORBITS)) #define MINOR(dev) (( Unsigned int) ((dev) & MINORMASK))
Build the device number (type converted to dev_t) according to the major device number major and the minor device number minor, you can use
MKDEV(int major, int minor);
The above macros are defined in <linux/kdev_t.h> as follows:
#define MKDEV(ma,mi) (((ma) << MINORBITS)
Copyright © Windows knowledge All Rights Reserved