The development of Linux drivers is quite different from the development of applications. These differences lead to the essential difference between writing Linux device drivers and writing applications.
First, user mode and kernel mode
The Linux operating system is divided into user mode and kernel mode. The kernel state completes the interaction with the hardware, such as reading and writing memory, reading data from the hard disk into the memory, and the like. The driver interacts with the hardware at the bottom, so it works in kernel mode. User mode can be understood as an upper application, which can be a Java application, a Qt application, a Python application, or the like. The reason why the Linux operating system is divided into two states is that even if the user-mode application is abnormal, it will not cause the operating system to crash, and this is all due to the strong protection of the kernel state to the operating system.
On the other hand, the reason why the Linux operating system is divided into two states is mainly to provide a unified computer hardware abstraction for the application. Applications that work in user mode can completely ignore the underlying hardware operations, which are done by kernel-mode programs. Most of these kernel-mode programs are device drivers. The application can operate the hardware device well without knowing how the hardware works, without causing the hardware device to enter an illegal state. It is worth noting that the user mode and the kernel state can be converted to each other. Whenever an application executes a system call or is suspended by a hardware interrupt, the Linux operating system switches from user mode to kernel mode; when the system call is completed or the interrupt processing is completed, the operating system returns from kernel mode to user mode and continues execution. application.
Second, the module mechanism
A module is code that can be added to the kernel at runtime. This is a nice feature of Linux. This feature makes it easy for the kernel to expand or shrink. Expanding the kernel can increase the functionality of the kernel. Reducing the kernel can reduce the size of the kernel. The Linux kernel supports a variety of modules. The driver is the most important one. Each module consists of compiled object code. Use the insmod command to add the module to the running kernel. Use the rmmod command to use an unused module from the kernel. delete. A module is called a static load when the kernel is started, and a load is called a dynamic load when the kernel is already running. Modules can augment any functionality that the kernel expects, but are often used to implement device drivers. The most basic framework code for a module is as follows:
Third, bus, equipment, drive
To harness 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 the reusability of the code, because the relationship between the driver and the device is one-to-many. Just like the primary and secondary device numbers, the primary device number represents the driver and the secondary device number represents the specific device. In addition, in order to improve the portability of the driver, Linux strips the resources (such as GPIO and interrupts) used by the driver to the device for management. That is, it contains its own device attributes in the device, and also includes the resources it uses to connect to the SOC. The driver focuses on the processes and methods of operation. The role of the bus is to manage devices and drivers at the software layer. To make the system aware of its existence, the device needs to register itself with the bus; similarly, the driver needs to register itself with the bus to make the system aware of its existence. The device and bus must be initialized to know which bus they are. 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. How does the device find the best driver for itself, or how does the driver find the device it supports? This is also the responsibility of the bus, the bus is like a matchmaker, responsible for the match between the device and the driver. The device will present its own conditions to the bus (the simplest and most accurate is to specify the other party's name), and the driver will also inform the bus of the conditions of the device that it can support (generally 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 driver registered on it, find the driver that is most suitable for the device, and then fill in the structure member of the device; when the driver registers, the bus will also traverse the registration on it. Device, find the devices it supports (can be multiple, 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 line match. After the line is taken, the interaction between the device and the driver can be ignored.
Pod System,Prefilled Cartomizer,Vape Atomizer,Vape Atomizers
Shenzhen Xcool Vapor Technology Co.,Ltd , http://www.xcoolvapor.com