major device number minor device number

Linux access to character devices is via the device name within the filesystem. Those are called special files, device files, or simply the nodes of the filesystem tree, and they are usually located in the /dev directory. Device files for character device drivers can be identified by the first column "c" in the output of the ls -l command. Block devices also appear under /dev, but they are identified by the character "b".


Typically, the major number identifies the driver for the device. For example: /dev/tty0 is managed by driver 4. Modern Linux kernels allow multiple drivers to share major device numbers, but most devices are still organized according to the " one major device number, one driver " principle; the minor device number is used by the kernel to correctly determine what a device file refers to device of. Depending on how the driver is written, we can obtain a direct pointer to the kernel device through the minor device number, or we can use the device number as an index into the device's local array. Either way, the kernel itself doesn't really care about any other information about the minor number, other than knowing that the minor number is used to point to the device implemented by the driver.

Internal representation of device number:

The dev_t type is defined in <linux/types.h> and is used to store device numbers - including major and minor device numbers. To get the major number of dev_t , use:

MAJOR(dev_t dev)
MINOR(dev_t dev)

Conversely, if you need to convert major and minor numbers to dev_t type, use:

MKDEV(int major, int minor);

Allocate and release device numbers

int register_chrdev_region(dev_t first, unsigned int count, char *name)    //Static application

first: To assign the starting value of the device number range, the minor device number of first is often set to 0, but is not required for the function.

count: is the number of consecutive device numbers requested (note that if count is very large, the requested range may overlap with the next major device number, but as long as the requested range of numbers is available , it will not cause any problems

name: indicates the name of the device

int alloc_chrdev_region(dev_t dev, unsigned int firstminor, unsigned int count, char *name)    //Dynamic application

dev is an output-only parameter that will hold the first number of the allocated range upon successful completion of the call. firstminor should be the first requested minor device number to use, which is usually 0. , the count and name parameters are the same as the register_chrdev_region function.

The release of the device number requires the use of the following functions:

void unregister_chrdev_region(dev_t first,unsigned int count)
Usually, we call the unregister_chrdev_region function in the module's cleanup function

The main purpose of the minor device number:

1. Distinguish the actual device controlled by the device driver;

2. Distinguish equipment for different purposes (misc series equipment)

3. Distinguish the partition of the block device (partition)





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325767460&siteId=291194637