Detailed analysis of the composition, allocation, and release of device numbers based on Linux

Devices are divided into three categories: character devices, block devices, and network devices. The kernel provides a corresponding driver model architecture for each type of device, including basic kernel facilities and file system interfaces.

Character device: a device-driven classification of a word in Linux in characters during transmission

Details of the composition, distribution, and release of Linux-based device numbers

As you can see from the above figure, the Linux system divides different devices into three categories: character devices, block devices, and network devices. The kernel provides a corresponding driver model architecture for each type of device, including basic kernel facilities and file system interfaces.

Character device: in the process of transmitting, in bytes, one byte and one byte of reading and writing, can not read and write data randomly, because such devices read and write speed is relatively slow (so no caching mechanism is provided in its kernel facilities) Common character devices include keyboards, mouse printers, and the like.

Block device: A device that can read data from any location. Reading and writing to such devices is read and written in units of blocks. It uses the buffer area to temporarily store data, and waits for the condition to mature, and then writes the data to the device or reads it from the device to the buffer at one time. Compare U disk, disk, SD card, etc.

Network devices: Network devices are different from character devices and block devices, and they are message-oriented. At the same time, there is no device node in the /dev directory. In the application layer, the user uses the network device through the socket function of the API. Such as network cards.

Composition of equipment number

● Primary device number and minor device number

Regarding the device number, let us first understand the following figure.

Details of the composition, distribution, and release of Linux-based device numbers

As can be seen from the above figure, c represents a character device, and d code is a block device.

For the red area, 1 is the major device number and 11 is the minor device number.

For the green area, 7 is the primary device number and 0-7 is the secondary device number.

The major device number is used to identify the device driver for the device, and the minor device number is used by the driver to identify several similar devices it manages.

●Representation of device number

In Linux systems, the device number is represented by dev_t. This is a 32-bit unsigned integer.

In the kernel, the lower 20 bits of dev_t are used to indicate the minor device number, and the upper 12 bits are used to indicate the major device number. With the evolution of the Linux system, the distribution of the above primary and secondary device numbers may change in the future, so device driver developers should avoid directly using the bit width occupied by the primary and secondary device numbers to obtain the primary device number or time. Device No. In order to ensure that the number of bits in the primary and secondary device numbers changes afterwards, the driver can still work normally. The kernel provides the following macros to operate the device number.

Details of the composition, distribution, and release of Linux-based device numbers

The MAJOR macro is used to extract the major device number from a device number of type dev_t. The MINOR macro always extracts the device number from a device number of type dev_t. MKDEV combines the major device number ma and the minor device number mi into a device number of the dev_t type.

Assume that the number of bits in the primary and secondary device numbers has changed since the kernel version. MINORBITS is changed to 18 bits. As long as the driver uses the MAJOR and MINOR macros to manipulate the device number, there is no need to modify the driver code or in the new kernel. use.

Assignment of equipment number

In the kernel source code, the allocation of device numbers mainly has two functions:

● Statically assigned device number

Details of the composition, distribution, and release of Linux-based device numbers

This function is used to know the master device number. The first parameter form represents a device number, and the second parameter count represents the number of secondary devices, that is, the number of similar devices managed by the current driver. The third parameter name indicates the name of the device or driver. Successfully returns 0, and failure returns a negative number.

For example, in the kernel code, use register_chrdev_region to request an example of a device number:

The above code applies for the primary device number to be 13, with a total of 1024 secondary devices, and the device name is input.

● Dynamically assign device number

Details of the composition, distribution, and release of Linux-based device numbers

This function has a system dynamic allocation device number, which allows the system to assign a device number without the knowledge of the main device number. The first parameter dev indicates the output parameter, which is the device number. The second parameter baseminor indicates the first minor device number, the third parameter count indicates the number of minor device numbers, and the fourth parameter name is the device. Or the name of the driver.

An example of using alloc_chrdev_region in the kernel:

Details of the composition, distribution, and release of Linux-based device numbers

The above example code is to apply for the device number through the alloc_chrdev_region function. The number of devices is 16, and the name of the device is rtc.

Device number release

The device number needs to be released when the driver is not in use, because the device number is also a resource of the system, and the unused use needs to release resources in time. It is already available for other devices.

Details of the composition, distribution, and release of Linux-based device numbers

Use this function to use the device number of the application. The first parameter from indicates the device number to be released, and the second parameter count indicates the number to be released.

The kernel uses an example of unregister_chrdev_region:

As you can see from the above code, if you call unregister_chrdev_region, the device with the major device number 13 will be released from the system.

Household Electrical Appliances

gree , https://www.greegroups.com