Device Classification and Their Associations in Linux System
Linux Each device on the system has its own set of fixed rules for operation, and there are numerous types and models of devices. It is not practical or meaningful to write the fixed rules for each device into the kernel.
Each device has its corresponding driver to ensure it can operate properly on the system. From a small LED light to a large network card, all devices require relevant drivers A driver can be seen as a layer of software between the application program and the actual device.
An application program controls a device by first sending a signal to the driver. The driver then receives the signal and commands the device to perform the corresponding action. If the application program wants to obtain the data collected by the device, it also needs to be done through the driver.
Classification of device in Linux
Linux systems divide devices into three basic types, and each module is typically implemented as one of these classes.
01 Character device
A character device is a device that can be accessed as a stream of bytes. This feature is implemented by a character device driver. Character device drivers typically implement at least open, close, read, and write system calls.
The character device is a sequential access, and the corresponding information can be read only after the device responds. It cannot be accessed randomly, and each character device has a device number, which consists of a major device number and a minor device number Common character devices such as serial ports and IICs.
The corresponding files for character devices are in the /dev directory, and each file corresponds to one piece of hardware. In the /dev directory of linux system, use ls -l to view the details, the first letter "c" is the identification of the character device file.
02 Block device
Like character devices, block devices are accessed through the file system node in the/dev directory. File systems can be accommodated on block devices. Common block devices such as disk, emmc flash, NAND flash, SD card, etc. Each block device likewise has r--+'/h vortex【5 slices【a device number, which consists of a primary device number and a secondary device number. Use the command ls -l to view the details in the /dev directory on a linux system. The first letter "b" identifies the block device file. /dev Each block device file under dev corresponds to a partition of the disk.
03 Network equipment
Any network-related transaction goes through a network interface, a device that can exchange data with other hosts. Usually, the interface is a hardware device, but it can also be a purely software device, for example, there is a lo in ifconfig to view information about the network interface, which is the network loopback (loopback) interface.
The way to access the network interfaces is to assign them a unique name, such as eth0, eth1, lo, and so on. However, no corresponding node exists in the file system for this name. Network interfaces do not have a device number like character and block devices, only a unique name such as eth0, eth1, etc., and this name does not need to correspond to a device file node.
The kernel uses a set of functions related to packet transfers to communicate with network device drivers, which differ from the read() and write() methods of character and block devices.
Various character devices and block devices all reflect the linux "everything is a file" design idea; network devices are the only devices that do not reflect this idea.
Device node, device driver and device association
There are many devices mounted on the whole system. When we access a device node, how does the system know which device driver to use and which device to access? This is achieved through the device number.
When creating a device node, you need to specify the major device number and the minor device number. For device nodes, the name is not important, the device number is the most important, which actually specifies the corresponding driver and the corresponding device.
Primary device numbers are used to distinguish between different types of devices, while secondary device numbers are used to distinguish between multiple devices of the same type. For commonly used devices, Linux has agreed-upon numbers, such asHard Disk the master device number of 3.
Use the command ls -l plus name in the /dev directory to view the corresponding primary and secondary device numbers.
When a driver is initialized, it registers its driver and corresponding master device number with the system so that when an application accesses a device node, the system knows which driver it is accessing. You can drive the master device number of a system device through the /proc/devices file.
Minor equipment number
When the driver runs, it creates a device object for each device it finds that it can drive and assigns it a minor-device number to distinguish between different devices. So that when an application accesses a device node, the driver knows which device it is accessing based on the minor number.