Device-to-drive relationships and device numbers, device files

Source: Internet
Author: User

Linux Equipment Classification
The devices under Linux are usually divided into three categories, character devices, block devices, and network devices.

Character device

A character device is a byte throttle device, access to the device can only be accessed sequentially in bytes, not random access, the character device does not request a buffer, all access requests are executed sequentially. Most of the devices under Linux are character devices. An application accesses a character device through a character device node. Device nodes are typically created in the/ dev directory by the mknod command, and the following example shows the device node for the serial device. The first flag of a character device file is the preceding "C" flag.

CRW-RW----  1 root  root 4, Feb 23:34/dev/ttys0
crw-r-----  1 root& nbsp Root 4, 10:26/dev/ttys1
CRW-RW----  1 root  root 4, jul  5  2000/dev/ttys2
CRW-RW ----  1 root  root 4, jul  5  2000/dev/ttys3

Character devices are devices that can only be read sequentially one byte at a time, but in fact some advanced character devices can now read a piece of data at a specified location. A character device is a data flow-oriented device with a device number for each character device, and a device number that consists of a master device number and a secondary device number. At the same time, Linux uses the same management files to manage character devices, so each character device has a corresponding device file in the /dev/ directory, the device node, which contains the type of device, the master / Secondary device number and access control of the device, the system through the device files to the character device operation, each character device file has its own common files with the same file Operation function Group structure (struct file_operations). Character device drivers typically require at least the implementation of the open,release,read , and write four operations in the file manipulation function group. The common character device has mouse, keyboard, serial port, console and so on.

Block devices

Storage devices generally belong to the block device, the block device has a request buffer, and support random access without having to access the data in sequence, such as you can first access the data behind, and then access the previous data, which is not possible for the character device. Disk devices under Linux are block devices, and although there is a block device node under Linux , the application typically accesses the block device through the file system and its cache , rather than reading and writing data on the block device directly through the device node. The first flag of a block device file is the preceding "B" flag.

root# Ls-l/dev/hda[1-3]
BRW-RW----1 root root 3, 1 Jul 5 2000/dev/hda1
BRW-RW----1 root root 3, 2 Jul 5 2000/dev/hda2
BRW-RW----1 root root 3, 3 Jul 5 2000/dev/hda3

Block devices are devices that can read data of any length from any location on the device. Each block device also has a device number, which consists of the main device number and the secondary device number. At the same time, Linux also uses the same management files to manage block devices, each block device in the /dev/ directory has a corresponding device files, namely the device node, they contain the type of device, Master / Secondary device number and access control of the device, the system through the device files to the block device operation, each block device file has its own common files with the same file Operation function Group structure (struct file_operations). However, the block device needs to be implemented much more than the method of operation of the character device, but also much more difficult. A block device can be used as a normal bare device to hold arbitrary data, or the block device can be formatted as a file system type, and then read the data on the block device in the format of the file system type, but either way, the last access to the data on the device must be implemented by invoking the operation method of the device itself. The difference is that the former directly calls the action method of the block device, while the latter indirectly calls the action method of the block device. Common block devices have various hard disks,flash disks,RAM disks, and so on.

Network equipment

Network devices differ from character devices and block devices, which are message-oriented rather than stream-oriented, and do not support random access or request buffers. In Linux A network device can also be called a network interface, such as eth0, the application through the Socket rather than the device node to access network equipment, there is no network device node in the system.

The network interface is used to exchange data with other devices, it can be a hardware device, or it can be a pure software device, such as the loopback interface is a pure software device. The network interface is driven by the network subsystem in the kernel and is responsible for sending and receiving packets, but it does not need to know how each transaction maps to the actual transmitted packets, and many network connections (especially those using the TCP protocol) are stream-oriented, but the network devices are designed around the transmission and reception of the packets. The network driver does not need to know the information about each connection, it only needs to process the packet. The network interface does not have the same device number as the character device and the block device, only a unique name, such as eth0,eth1 , etc., and the name does not need to correspond to the device file node. The kernel uses a set of functions related to packet transport to communicate with network device drivers, which are different from the read () and write () methods of character devices and block devices.

Association of device nodes, device drivers, and devices
When we access a device node, is the system aware of which device is being used to drive and which device to access? This is done by the device number. When we create a device node, we need to specify the main device number and the secondary device number. For the device node, the name is not important, the device number is the most important, it actually specifies the corresponding driver and the corresponding device.

Linux Device Management is tightly integrated with the file system, and various devices are stored as files in the/ dev directory, called device files. Applications can open, close, and read and write these device files, completing the operation of the device as if it were a normal data file. In order to manage these devices, the system has numbered the devices, and each device number is divided into the main device number and the secondary device number. The main device number is used to distinguish between different kinds of devices, while the secondary device number is used to differentiate multiple devices of the same type. For common devices,Linux has a conventional number, such as the main device number of the hard disk is 3.

LinuxProvides a unified operation function interface for all device files by using data structuresstruct File_operations。 This data structure includes pointers to many operation functions, such asOpen ()、Close ()、Read ()AndWrite ()And so on, but due to the variety of peripherals, operating methods are different.Struct file_operationsA member of a struct is a series of interface functions, such as those used for reading/Wrote theRead/writeFunctions and controls for theIOCTLsuch as To open a file is to call this filefile_operationsIn theOpenOperation. Different types of files have differentfile_operationsmember functions, such as normal disk data files, interface functions to complete disk data block read and write operations, and for various device files, the final call to the respective driverI/Ofunction for the operation of the specific device. In this way, the application does not have to consider the operation of the device or ordinary files, can be treated as a file, with a very clear unifiedI/OInterface. Sofile_operationsis file-levelI/OInterface.

Main device number

When the driver is initialized, it registers its driver and the corresponding master device number into the system so that when the application accesses the device node, the system knows the driver it accesses. You can use the /proc/devices file to drive the system device's main device number.

Secondary device number

When the driver traverses the device, each time it discovers a device it can drive, it creates a device object and assigns it a secondary device number to differentiate between different devices. This way, when the application accesses the device node, the driver can know what device it says to be accessing according to the secondary device number.

Each character device and block device (network interface without a device number) in the system has a device number, the traditionalUNIXand earlier versionsLinuxThe device number in is -bit, primary and secondary equipment numbers are8bit, low8Bit for secondary device number, high8is the primary device number, so the system supports up to65536Character Devices and65536Block device, which has not been able to meet the needs of a variety of new equipment in the current emerging, soLinux2.6The device number has been extended and a device number is +Bit, the main device number is Abit, the secondary device number is -bit, but this +Bit device number is encoded in both old and new, the old device number format is: highest ABit main device number, minimum -Bit is the secondary device number, and the new device number format is:Bit[19:8]is the main device number,Bit[31:20]Is the secondary device number of the high A-bitbit[7:0]This is the low of the secondary device number8-bit If you know the main device number of a deviceMajorand secondary device numberMinor, then useMKDEV (Major,minor)Generates a device number that is the device's old format, using theNew_encode_dev (MKDEV (major,minor))The device number for the new format is generated.LinuxThe main device numbers for the various devices supported are defined in theInclude/linux/major.hFile, while the main device number and the secondary device number are already in the official registrationDocumentation/devices.txtCan be found in the file.

The conversion operation function of the old-fashioned device number, the old format device number and the new format device number are as follows:

New_encode_dev (dev_t dev) function

Convert The old format device number Dev to a new -format device number.

New_decode_dev (u32 dev) function

Converts The new format device number to the old -format device number.

Old_encode_dev (dev_t dev) function

Converts the old format device number to an old- fashioned device number.

dev_t Old_decode_dev (U16 val) function

Converts the old-fashioned device number to the old format device number.

The device nodes in Linux are created through the "mknod" command. A device node is actually a file, which is called a device file inLinux . It is necessary to note that in Linux , all device access is through the file, General data file program ordinary files, device nodes called device files. In the Linux kernel, network devices are also operated through files, called network device files, which are accessed through the socket interface in user space. The socket number is the network device file descriptor.

Example:mknod/dev/mydevice C 254 0

(c for sub-devices,254 main device number,0 is the secondary device number )

Open,close and other operations /dev/ device files, the kernel according to the file's main device number to find the corresponding device driver

The main device number can be divided into dynamic and static applications.

Device files

Linux uses the same management as the file to manage devices, so for every character device or block device in the system, you must create a device file for it, which is the device node that is placed in the /dev/ directory. It contains the device type (block device or character device), device number (main device number and secondary device number), device access control properties, and so on. The device files can be generated by hand with the mknod command or by the udev user tool software after the system is booted according to the actual information of each device in the /sys directory, using the latter way to dynamically assign the device number to each device. Instead of assigning a fixed device number, if there are not many devices in the system, and the device type is common, it is convenient to create a device file by hand, creating a device file with the assigned number for a common device. If the system is large and there are too many devices in the system, it is best to dynamically assign the device number, which is automatically created by Udev based on the actual information of the device after the system is booted.

Device-to-drive relationships and device numbers, device files

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: 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.