Linux application layer to driver layer to hardware diagram (inline)

1 The overall working principle of the system

     1.1 Overall flow chart

        1.1.1  Here is an example of a function, such as the function pointers such as open/read/write provided by the kernel to our application layer. These functions are encapsulated by the file_operation (all interfaces used to manipulate files) structure in the kernel's fs.h, They are just a function pointer (used to hook the function entity in the driver), the entity is specifically used to operate the hardware, and it is written in the driver module according to its own needs.

        1.1.2   List the above key points

                (1) Application layer->API->device driver->hardware

                (2)API:open、read、write、close等

                (3) The real open, read, write, close and other function entities are provided in the driver source code


 1.2 Register the device driver (that is, what needs to be done in the KO module)

1.2.1 5 points to understand when registering device drivers

              (1) Why register the driver

        Explanation: After registration, the function pointer of the file_operation called by the app can be correctly connected to the function entity in the driver, and the corresponding hardware can be correctly operated.

              (2) Who is responsible for registration

     Explanation: This part of the job is the driver itself to register

               (3) Who to register with

     Explanation: Register with the kernel

               (4) Where does the registration function come from

     Explanation: It is a registration function provided by the kernel. The driver only needs to call this function to register.

               (5) What about before registration? What happens after registration? What is the result of registration?

     Explanation: For example, a character driver, after registration, the kernel will provide us with a device number, which is called to operate in the form of a file. Everything is the idea of ​​a file. This is a set of software defined by the kernel VFS virtual file system for the convenience of operation. .


1.2.2 Detailed explanation of register_chrdev function for registering device driver (#include <linux/fs.h>)

(1) Function, the driver registers its own file_operations with the kernel

(2)inline 

        Explanation 1: When inline is linked, it will destroy the function body and only take the content of this function and link it directly, which reduces the calling level of the function and reduces the time overhead. Generally, there is only one code in the function, mainly to ensure the encapsulation of the function (easy to read the code), such as a function defined by __init, we will encapsulate it again and remove __.

        Explanation: Originally it was just for efficiency. Using it, he also had another effect. The encapsulated function body can be placed in the h file, which is convenient for modularization. The function body is generally not placed in the h file, because the The variable definition and function declaration are different, and the error of repeatedly including the same function body will occur when linking.


1.2.3 How the kernel manages device drivers

(1) There is an array in the kernel to store the registered character device drivers

        Explanation: The kernel provides an array that defines 255 elements, all of which can only register 255 different types of device drivers (the same type can have minor device numbers, such as several LED drivers)

(2) register_chrdev internally stores the information of the driver we want to register (mainly) in the corresponding position in the array

(3) cat /proc/devices to view the character device drivers (and block device drivers) that have been registered in the kernel

The flow chart is as follows:




1.2.4 How the application layer calls the driver

     1. Creation of driver device file
        (1) What is device file
        (2) The key information of device file is: device number = major device number + minor device number, use ls -l to view the device file, you can get the device file The corresponding major and minor device numbers.

        (3) Use mknod to create a device file: mknod /dev/xxx c major device number and minor device number


1.2.5 How the driver operates the hardware

1. Or that hardware
        (1) The physical principle of the hardware remains unchanged
        (2) The hardware operation interface (register) remains unchanged
        (3) The hardware operation code remains unchanged
2. What is the difference?
        (1) The register addresses are different. The original is to use the physical address directly, and now it is necessary to use the virtual address corresponding to the physical address in the kernel virtual address space. The physical address of the register is determined when the CPU is designed and found from the datasheet.
        (2) The programming method is different. In bare metal, function pointers are used to directly operate register addresses, while in kernel, encapsulated io read and write functions are used to operate registers to achieve maximum portability.








        












Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325855546&siteId=291194637