To write a Linux driver:
1th Step: Build Linux driver skeleton (load and unload Linux drivers)
Any type of program has a basic structure, for example, the C language needs to have an entry function main. Linux drivers are no exception. The Linux kernel needs to load the driver first when it uses the driver. Some initialization work is required during the loading process, such as setting up a device file, allocating memory address space, and so on. When the Linux system exits, the Linux driver needs to be uninstalled, and the resources used by the Linux driver are freed during the uninstallation process, such as deleting the device files, freeing the memory address space, and so on. In the Linux driver, you need to provide two functions to handle the operation of the driver initialization and exit separately. The two functions are specified with Module_init and module_exit macros, respectively. Linux drivers generally need to specify these two functions, so the C program files that contain both functions and the two macros that specify the two functions can also be seen as Linux-driven skeletons.
2nd step: Registering and unregistering device files
Any Linux driver needs to have a device file. Otherwise the application will not be able to interact with the driver. The creation of a device file is generally done in the 1th step of the function that is written to handle the Linux initialization work. Deleting a device file is typically done in the 1th step of the function that handles the Linux exit. You can create and remove device files using the Misc_register and Misc_deregister functions, respectively.
3rd Step: Specify driver-related information
The driver is self-describing. For example, you can get the author name of the driver, the Open source protocol used, the alias, the driver description, and other information through the Modinfo command. This information needs to be specified in the driver source code. The Module_author, Module_license, Module_alias, Module_description, and other macros can be used to specify driver-related information.
4th step: Specify the callback function
Linux drivers contain a variety of actions, which can also be called events. For example, a write event is triggered when writing data to a device file, and the Linux system invokes the write callback function of the corresponding driver, which triggers a "read" event when the data is read from the device file, and the Linux system invokes the read callback function for the corresponding driver. A driver does not necessarily specify all of the callback functions. The callback function is registered through the relevant mechanism. For example, a callback function associated with a device file is registered through the Misc_register function.
5th step: Writing Business logic
This step is a core part of the Linux drive. There is no point in having a Linux driver with a skeleton and a callback function. Any full Linux driver will do some work related to its functionality, such as printer drivers sending print instructions to the printer. The COM driver interacts with data based on the rate of transfer. The specific business logic is related to the function of the driver. The business logic may have multiple functions, multiple files, or even multiple Linux driver modules. The specific implementation of the reader can be based on the actual situation.
6th step: Writing the Makefile file
The compilation rules for the Linux kernel source code are defined by the makefile file. Therefore, writing a new Linux driver must have a makefile file.
7th step: Compiling Linux drivers
Linux drivers can be compiled directly into the kernel or separately as modules.
8th step: Install and uninstall Linux drivers
If the Linux driver is compiled into the kernel, the driver will automatically load as soon as Linux uses the kernel. If the Linux driver exists separately in the module, you need to load the Linux driver module with the Insmod or Modprobe command, and use the Rmmod command to uninstall the Linux driver module.
Android Deep Exploration Sixth chapter experience