The difference between Linux device drivers and user programs

The difference between Linux device drivers and user programs


The development of Linux drivers is very different from the development of applications, and these differences lead to the essential difference between writing Linux device drivers and writing applications.

user mode and kernel mode

  • The Linux operating system is divided into user mode and kernel mode. The kernel mode completes the interaction with the hardware, such as reading and writing memory, reading data from the hard disk to the memory, and so on. The driver interacts with the hardware at the bottom layer, because it works in the kernel mode. User mode can be understood as upper-level applications, which can be Java applications, Qt applications, Python applications, etc. The reason why the Linux operating system is divided into two states is that even if the application program in the user mode is abnormal, it will not cause the operating system to crash, and all this is due to the strong protection ability of the kernel mode on the operating system.
  • On the other hand, the reason why the Linux operating system is divided into two states is mainly to provide applications with a unified abstraction of computer hardware. The application program working in the user mode can completely ignore the underlying hardware operations, and these operations are completed by the kernel mode program. Most of these kernel state programs are device drivers. The application can operate the hardware device well without understanding how the hardware works, and at the same time will not put the hardware device into an illegal state.
  • It is worth noting that user mode and kernel mode can be converted to each other. Whenever an application executes a system call or is suspended by a hardware interrupt, the Linux operating system will switch from the user mode to the kernel mode; when the system call is completed or the interrupt processing is completed, the operating system will return from the kernel mode to the user mode and continue to execute application.

module mechanism

  • A module is code that can be added to the kernel at runtime. This is a good feature of Linux. This feature can make the kernel easy to expand or shrink. Expanding the kernel can increase the functionality of the kernel, and shrinking the kernel can reduce the size of the kernel. The Linux kernel supports a variety of modules, and the driver is the most important one. Each module is composed of compiled object code. Use the insmod command to add the module to the running kernel, and use the rmmod command to remove an unused module from the kernel. delete.
  • Loading a module when the kernel starts is called static loading, and loading when the kernel is already running is called dynamic loading. Modules can extend any functionality expected by the kernel, but are typically used to implement device drivers.

Buses, Devices and Drivers

  • If you want to master Linux driver development, you must have a deep understanding of the Linux bus device driver framework. The reason why such a framework is formed is mainly for code reusability, because the relationship between drivers and devices is one-to-many. Just like the major device number and the minor device number, the major device number indicates the driver, and the minor device number indicates the specific device.   
    In addition, in order to improve the portability of the driver, Linux strips the resources used by the driver (such as GPIO and interrupts, etc.) to the device for management. That is, the device contains its own device attributes, and also includes the resources used by it to connect to the SOC. The driver focuses on the process and method of operation.
  • The role of the bus is to manage devices and drivers at the software level. For a device to make the system aware of its own existence, the device needs to register itself with the bus. Similarly, for a driver to make the system aware of its presence, it also needs to register itself with the bus. Devices and buses must be clear about which bus they are when they are initialized. Therefore, in order to achieve operational consistency, Linux invented a virtual bus called the Platform bus.
  • Multiple devices and multiple drivers are registered on the same bus, so how does the device find the most suitable driver for itself, or how does the driver find the device it supports? This is also the responsibility of the bus, which is like a matchmaker, responsible for connecting devices and drivers. The device will propose its own conditions to the driver to the bus (the simplest and most accurate is to specify the name of the other party), and the driver will also inform the bus of the conditions of the device it can support (usually the model ID, etc., the simplest It can also be the name of the device).
  • When the device is registered, the bus will traverse the drivers registered on it, find the driver that is most suitable for this device, and then fill it in the structure members of the device; when the driver is registered, the bus will also traverse the devices registered on it , find the supported devices (there can be more than one, the relationship between the driver and the device is 1:N), and fill the device into the driver's support list. We call the behavior of the bus as a match. After pulling the line, the matchmaker doesn't care about the interaction between the device and the driver.

Guess you like

Origin blog.csdn.net/qq_45902301/article/details/125503947