In this chapter, we mainly introduce a Linux driver, and give us an example of a Linux driver in a practical way.
Linux-driven work and access is one of the highlights of Linux, and has been widely praised by the industry. The Linux system maps each driver into a single file. These files are called device files or drive files and are saved in the/dev directory. This design concept makes interacting with Linux drivers as easy as interacting with ordinary files. Of course, it's easier than visiting Linuxapi. Since most Linux drivers have their corresponding device files, exchanging data with the Linux driver becomes the exchange of data with the device files.
To write a Linux driver: 1. Build the Linu x drive skeleton (load and unload Linu x drive). 2.: Register and unregister the device files. 3. Specify driver-related information. 4. Specify the callback function. 5. Write the business logic. 6. Write the makefile file. 7. Compile the Linux driver. 8. Install and uninstall Linux drivers.
This chapter also describes how to test Linux drivers using different methods. These methods are mainly differentiated by platform. For example, you can test Linux drivers directly using Ubuntu Linux. There are many drivers that can be installed directly on Android, Linux and other embedded systems as long as they are compiled with the corresponding Linux kernel. Of course, you can also test Linux drivers directly on the Android emulator, the Development Board, or even your phone. To develop Linux drivers faster, you can also consider the ECLIPSE integrated development environment described in section 6.5.
The way Linux drives work is interaction. For example, to send a print command to the Linux printer driver, you can open the device file directly using the C function open and send the Print command to the driver's device file using the C function ioctl. The most important thing to write a Linux driver is to write a callback function, otherwise data that interacts with the device file cannot be processed.
Build a Linux drive skeleton using up to two functions: Module_init,module_exit. The macro definition to be made.
Create and remove device files with Misc_register,misc_deregister.
The callback function is generated by itself, and a driver does not necessarily specify all the callback functions.
The specific business logic is related to the functionality of the drive, and there may be multiple functions, multiple files, or even multiple Linux driver modules.
The program can be compiled directly into the kernel, or it can be used as a separate module.
Using the output is PRINTK, there is a question why not printf, because the Linux system is divided into user space and kernel space, and printf can only run in user space. Furthermore, the device files that interact with each other.
Understand the open source file, GPL agreement, LGPL agreement, BSD protocol, MIT protocol, and Apache License2.0 protocol.
When writing and destroying device files, be aware that Misc_register can only set this device number.
Use Cat/proc/devices to get a display of what the main device and the main device number are in the current system
The File_operation.read and File_operation.write member variables allow you to specify a pointer to the callback function to be invoked by the read-write device, respectively.
The driver is written, the driver is compiled multiple times, and the driver reads and writes the directory, there are build.sh and related script files.
The Cat command does not revert 4 bytes to a value of type int
The goldfish kernel for the Android emulator does not allow the dynamic loading of the Linux driver module by default, so the CD ~/kernel/goldfish,make Menuconfig is required to select the correct entry submenu in the Configuration box, or the kernel may fail to load.
The sixth chapter-use examples to understand Linux driver development and experience