Because of the existence of Linux device drivers in the form of INCORE modules, it is necessary to compile any type of device drivers to master this chapter. Compiling a driver into a kernel module in the development of a specific device driver also has a strong engineering significance, because if you directly compile the driver under development into the kernel, if the driver code is constantly modified during the development process, you need to constantly compile the kernel and restart the kernel. However, if the driver code is compiled as a module, you only need rmmod and insmod, which greatly improves the development efficiency. The following describes how to add, compile, and allow Linux modules.
The LINUX module consists of six parts:
1. Module Loading Function (required)
When the kernel module is loaded using the insmod or modprobe command, the module's loading function is automatically executed by the kernel to complete the initialization of this module.
2. unmount function of the module (required)
When you run the rmmod command to uninstall a module, the unmount function of the module is automatically executed by the kernel to complete the function opposite to the function loaded by the module.
3. Module license statement
The module license declares the permission to describe the kernel module. If the license is not declared, the module will receive a warning of kernel contamination when it is loaded.
4. Module parameters (optional)
A module parameter is the value that can be passed to a module when it is loaded. It corresponds to the global variable in the module.
5. Module export symbol (optional)
The kernel module can export symbols (symbol, corresponding to functions or variables), so that other modules can use the variables or functions in this module.
6. Module author and other information statement (optional)
After understanding the composition of the LINUX module, let's compile a simple LINUX kernel module hello. c.
Create a new folder named testmod under the drivers directory of the kernel source code tree directory, and then enter this folder to create our. c source file, hello. c
After the source code is compiled, add the MAKFILE compilation file. First, modify the MAKEFILE in the drvier directory and add the testmode directory to this MAKEFILE.
The MAKEFILE file modified in the drvier directory is as follows:
Add a makefile under the testmode directory. The file content is:
After completing the above preparations, we start to compile the module File and use the following compilation command:
Make-C/usr/src/linux-2.6.35.13 M =/usr/src/linux-2.6.35.13/Drivers/testmod modules
After entering the preceding command, we will get the module File, hello. Ko
After the module File is generated, run the insmod hello. Ko command to print Hello world enter on the screen.
Run the rmmod hello, Ko command to print Hello World exit on the screen.