Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Linux device driver Development

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about the example analysis of Linux device driver development, which may not be well understood by many people. in order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

Compile and run

Driver compilation uses kernel's Makefile file-- that is, the compilation system of the source tree. Therefore, the source code needs to be configured and compiled. Take the source code that comes with ubuntu as an example:

The compile command to compile the external module (.ko) is:

Make-CM=mak**e − Cpathtokernelsrc > M=PWD

That is, go to the kernel directory and use the kbuild system to compile the driver file. Obj-m tells the compiler that it needs to be compiled into a module (.ko). Foo.o indicates that the source file is foo.c or foo.S. If the driver module contains multiple files (such as foo_main.c, foo_common.c), write as follows:

Kbuild will compile all the files listed in $(foo-y) and merge to produce foo.ko

During compilation, the Makefile of the module will be read by kbuild many times, so it is recommended to use $(KERNELRELEASE) to distinguish the use stage of Makefile. The optimized Makefile is as follows:

The first time you run make, $(KERNELRELEASE) is empty, so the 'else' content of Makefile is read first, and then, execute *' make-C... .'*, during execution, the Makefile file will be read back. This time, the 'ifneq' condition is satisfied, take different paths twice, and the compiler system configures different variable parameters.

If you do not use the $(KERNELRELEASE) distinction, each compilation system will set all variables and rules, which may conflict with kernel's Makefile variables or rules. Therefore, it is recommended to configure driver-specific variables and rules when (KERNELRELEASE) is empty. Besides configuring drive**r-specific variables and rules when (KERNELRELEASE) is empty, kernel also provides some other methods in addition to using (KERNELRELEASE).

For more information about the kernel compilation system, please refer to "Documentation/kbuild/" under the kernel source code

The driver module runs related commands

Insmod foo.ko-load driver into kernel to run. Rmmod foo-- remove driver.lsmod from kernel-- view the modules currently running in kernel. Character equipment

The character device driver actually implements a file interface so that the device file can be accessed like an ordinary file, so that the application can access the driver and exchange data with the driver using the file IO API (open/write/read/close series functions) of the libc library. Therefore, its core is to implement the file system interface-file operation.

Program entry

One of the biggest differences between a macro kernel and a micro kernel is the running space of the driver. In a microkernel system, the driver, as an application, runs in user space, and its entry is the application's' main' function. Linux as a macro kernel system, its driver and kernel are integrated, running in the kernel space, its entry is' module_init','module_exit''is the corresponding exit function, they must appear in pairs.

Foo_init performs the most basic character device operation: using cdev_add to add a 'cdev' to the character device list (which is actually a map structure), so that the character device foo is entrusted to kernel for management, and when the application manipulates the corresponding device file, kernel can schedule to the foo driver.

Foo_exit must use cdev_del to remove devices from the list, otherwise, when kernel finds cdev from the list, it will return an "obsolete" pointer, and when you use it to callback the corresponding operation, a null pointer exception will occur, causing kernel to hang up. Remember! Foo_init and foo_exit must be used in pairs and do the opposite.

Bug instance:

Foo_exit does not execute the cdev_del function. Insmod foo.ko-OK. The application reads and writes device files-- OK. Rmmod foo-OK. Insmod foo.ko-OK. The application reads and writes device files-- core dump.

When rmmod foo', foo_exit is called, but the programmer forgot to execute the cdev_del function, causing the pointer of foo.cdev to become a null pointer without being deleted, which is still in the list of character devices. When the foo.ko is inserted for the second time, when reading and writing the device, Kernel is looking for the old foo.cdev null pointer, and when using it to invoke the corresponding file operation, a null pointer core dump error occurs.

File operation

Setup_dev: register the file manipulation function of the current device, and when the application manipulates the device file, it calls the corresponding driver function. Exchange data with user space, copy_from/to_user, and the return of 0 from these two functions indicates that the function was executed successfully.

Copy_from_user: the data written by the user is saved by copy driver data buf. Copy_to_user: copy drives the data buf to the buf where the user reads the data.

The interaction flow between application and character driver

Create device file-sudo mknod / dev/foodev c 50000 modify device file permissions-sudo chmod 766 / dev/foodev application uses the open function to open the device file. Kernel finds the list of character devices according to the file type (character device file) and finds the corresponding device driver module according to the device number (Major, Minor). Call the device-driven open function foo_open. The application calls the read/write function to read and write the device file. The driver calls foo_read/write and uses copy_from/to_user to exchange data.

common problem

Q: when reading and writing device files, the write or read function returns 0 and cannot read or write data?

A: this kind of device file read and write failure is likely to be a permission problem. Confirm the file read and write permissions, followed by whether the data meets the requirements of the driver.

Block equipment

Block device refers to storage device, and block device driver is storage driver such as HD,SSD. Linux uses the Block subsystem to manage them, and transforms the IO read and write requests in the application layer into Request, which is passed to the corresponding device driver. The driving process is relatively simple:

Register_blkdev → alloc_disk → handles request

Q: the relationship between the file system and the Block subsystem?

A: the Block subsystem mainly provides the lowest level of data reading and writing, that is, raw io, which is used by the file system for IO operations.

Register

Register block device (primary device number)

Register the device (MAJOR,MINOR)

Add disk

This disk will appear in the / dev directory, in this case / dev/frd0, users can format device files, partitions and other disk-related operations. Such as' mkfs.ext2 / dev/frd0', 'mount / dev/frd0 / mnt'.

Initialize request queue to process device request

Kernel provides some macros to help traverse the list of requests. The request processing strategy is the core and essence of the Block driver. Developers have to improve access efficiency and solve concurrency congestion according to the physical characteristics of the device.

* fr_queue_rq () * -'* * request queue'* * handler function, which is set when initializing the request queue, and Loop handles each request:

* fr_transfer () *-the physical device reads and writes data and transmits the underlying data according to the context content (context) of the request. Here is the lowest IO communication, and the driver reads and writes the data according to the interface protocol of the physical device.

Block device driver test

After executing the above command, the contents of frd_data_r and frd_data_w should be the same.

After reading the above, do you have any further understanding of the sample analysis of Linux device driver development? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report