Linux device driver (1) -- the basis of driver development

Code learning materials come from:

Lecture 2 Basic experiment of character device driver development_哔哩哔哩_bilibili

Only for personal study/review, intrusion deleted

1. The interaction principle of application program and driver

The driver is to obtain peripherals, or sensor data, and control peripherals. Data is submitted to the application. Linux driver writing not only requires writing the driver, but also simply writing a test application. Drivers and applications under linux are completely separate.

User Space and Kernel Space

Every process has a virtual address space. For a 32-bit operating system, the virtual address space is 4G, and the allocation ratio between kernel space and user space is 1:3. The purpose of this is to allow each process to run without interfering with each other. The user space corresponds to the process, so whenever the process is switched, the user space will change accordingly. The kernel space is mapped by the kernel process, and each kernel space will have its own corresponding page table, and each user process has a different page table.

A more detailed description is as follows: Linux kernel space and user space_liefyuan's blog-CSDN blog

User space (user mode) and kernel space (kernel mode):

The Linux operating system kernel and drivers run in the kernel space, and applications run in the user space. One of the biggest benefits of being divided into kernel space and user space is security, which can prevent an application from obtaining greater permissions to change the operating system.

What if the application wants to access kernel resources (drivers)? There are three methods: system call, exception (interrupt), trap (soft interrupt).

The application does not directly call the system call, but indirectly calls the system call through the API function, such as POSIX, C library, etc. The most commonly used programming interface in unix-like operating systems is POSIX.

POSIX: POSIX is an acronym for Portable Operating System Interface. It is an IEEE 1003.1 standard that defines a language interface between application programs (as well as command-line shell and utility interfaces) and the UNIX operating system.

For example: the application uses the open function to open a device file

Every system call has a system call number

The system call is in the kernel space, and the application cannot directly access it, so it needs to fall into the kernel, and the method is soft interrupt. After falling into the kernel, the system call number must be specified (the figure below is from the network).

2. Character device driver development process

1) Everything in linux is a file, and the performance of the drive device is a file under /dev/, such as /dev/led. The application calls the open function to open the device, such as led. The application writes data through the write function, for example, writing 1 means open, and writing 0 means close. If you want to close the device, then it is the close function.

2) When writing the driver, it is also necessary to write the open, close, write and other functions corresponding to the driver. Character device driver file_operations structure.

Guess you like

Origin blog.csdn.net/qq_58550520/article/details/129151785