Linux master and secondary device numbers

Source: Internet
Author: User
Linux device management is closely integrated with the file system. Various devices are stored in the/dev directory as files, which are called device files. ApplicationProgramYou can open, close, and read/write these device files to complete operations on the device, just like operating a common data file. To manage these devices, the system numbers the devices. Each device number is divided into the primary device number and secondary device number.The primary device number is used to distinguish different types of devices, while the secondary device number is used to distinguish multiple devices of the same type.For common devices, Linux has a conventional number, for example, the master device Number of the hard disk is 3.

Linux provides a unified operation function interface for all device files by using the data structure struct file_operations. This data structure includes the pointers of many operation functions, such as open (), close (), read (), and write (). However, due to the many types of peripherals, the operation methods are different. The members in the struct file_operations struct are a series of interface functions, such as the read/write function for read/write and IOCTL for control. To open a file, call the open operation in file_operations. Different types of files have different file_operations member functions, such as common disk data files. The interface functions perform disk data block read/write operations. For various device files, then, I/O functions in the respective drivers are called to perform operations on specific devices. In this way, the application does not have to consider whether the operation is a backup or a common file.CompositionProcessing, with clear and unified I/O interfaces. Therefore, file_operations is an I/O interface at the file level.

Transferred from:Http://hi.baidu.com/wudaovip/blog/item/479b451e95c475f81ad57621.html

 

 

 

 

The device number is allocated and registered in the driver module. That is to say, the driver module has this device number.(My understanding), and the device files under the/dev directory are created based on this device number. Therefore,When accessing the device files in the/dev directory, the driver module knows that it is time to provide services (kernel notifications of course ).
In the Linux kernel, the main device ID identifies the driver corresponding to the device and tells the Linux kernel which driver is used to serve the device (that is, the device file under/Dev; the device number is used to identify a specific and unique device.
In the kernel, the variables of the dev_t type (actually a 32-bit unsigned integer) are used to save the Primary and Secondary device numbers of the device. The 12-bit high represents the primary device number, A device number with a minimum of 20 bits indicates the next device number.
There are two ways for a device to obtain the Primary and Secondary device numbers: one is to manually specify a 32-digit number and associate it with the device (that is, register with a function ); the other is to call the system function to dynamically allocate a primary/secondary device number to the device.

To manually specify a primary/secondary device number, use the following function:
IntRegister_chrdev_region (Dev_t first,
Unsigned int-count,
Char * name)
First is the device number that we manually specify, count is the number of consecutive device numbers requested, and name is the name of the device associated with the range of the device numbers, it will appear in/proc/devices and sysfs.
For example, if first is 0x3ffff0 and count is 0x5, this function registers device numbers for five devices, namely 0x3ffffff0, 0x3ffff1, 0x3ffff2, 0x3ff3, and 0x3ffff4, among them, 0x3 (12-bit high) is the master device number that these five devices share (that is, these five devices use the same driver ). 0xffff0, 0xffff1, 0xffff2, 0xffff3, and 0xffff4 are the sub-device numbers of these five devices. Note that if the Count value is too large, the requested device number range may overlap with the next primary device number. For example, if first is 0x3ffff0 and count is 0x11, first + Count = 0x400001, that is, the number of the primary device allocated to the last two devices is no longer 0x3, it's 0x4! One disadvantage of using this method to register a device number is that if the driver module is widely used by others, therefore, it cannot be ensured that the registered device number is a device number not assigned to others in Linux.

To dynamically allocate device numbers, use the following functions:
Int Alloc_chrdev_region (Dev_t * Dev,
Unsigned int-Firstminor,
Unsigned int- Count,
Char * name)
This function must be passed to the specified device number firstminor (usually 0), the number of devices to be allocated, and the device name, after this function is called, the device numbers automatically allocated are stored in Dev.Dynamic Allocation of device numbers can avoid the disadvantages of manually specifying a device number, but it also has its own disadvantages, that is, it is impossible to create a device node under/dev in advance,Because the Dynamic Allocation of device numbers cannot always be consistent when the driver module is loaded every time (in fact, if no other module is loaded when the same driver module is loaded twice, the automatically assigned device numbers are the same, because the kernel allocation device numbers are not random, but the book says some kernel developers will handle them randomly in the near future ), however, this disadvantage can be avoided, because after the driver module is loaded, we can read the/proc/devices file to obtain the primary device number allocated to the device by the Linux kernel.

Three macros related to the primary and secondary device numbers:
Major (dev_t Dev): obtains the master device number based on the device number Dev;
Minor (dev_t Dev): obtains the next device number based on the device number Dev;
Mkdev (INT major, int minor): Creates a device number based on the master device number major and sub-device number minor.

 

 

Another solution:

Linux device management is closely integrated with the file system and associates devices with files. In this way, system calls can directly operate devices using the same method as operating files. All devices are stored in the/dev directory as files, which are called device files. Applications can open, close, and read/write these device files to complete operations on the device, just like operating a common data file. To manage these devices, the system numbers the devices. Each device number is divided into the primary device number and secondary device number. The primary device number is used to distinguish different types of devices, while the secondary device number is used to distinguish multiple devices of the same type. For common devices, Linux has a conventional number, for example, the master device Number of the hard disk is 3.

Transferred from:Http://fanyihui1986.blog.163.com/blog/static/784485920091132043905/

From: http://hi.baidu.com/vv1133/blog/item/08d271941e1f3213d31b70bd.html
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.