Source: Ji yiqin blog Author: Ji yiqin
I first came into contact with Linux driver development and bought a "Linux Device Driver". The first thing is to build a development environment !!!
There is a hello World column on it:
// Hello. c
# Include <Linux/init. h>
# Include <Linux/module. h>
Module_license ("dual BSD/GPL ");
Static int hello_init (void ){
Printk (kern_alert "Hello, world/N ");
Return 0;
}
Static void hello_exit (void ){
Printk (kern_alert "Goodbye, cruel world/N ");
}
Module_init (hello_init );
Module_exit (hello_exit );
Let's see how to make it run:
The driver is different from the user program. It runs as a module connected to the kernel module and runs in the kernel space.
To run a self-constructed module, You need to configure the kernel tree for your system and connect the target module and the kernel tree to run it! However, the Linux we installed does not have the source code (at least mine), let alone the compiled kernel tree ~
(1) download the Linux source code:
First, check the Linux kernel source code package that can be downloaded.
Root @ jiq-desktop :~ # Apt-cache search Linux-Source
Then select the source code package to download:
Root @ jiq-desktop :~ # Apt-Get install linux-source-2.6.22
After the download is complete, under/usr/src, the file name is: linux-source-2.6.32.tar.bz2, is a compressed package, and then use
Command to obtain the source code of the Linux kernel:
Root @ jiq-desktop:/usr/src # tar jxvf linux-source-2.6.32.tar.bz2
(2) and then in the Linux kernel source directory/usr/src/linux-source-2.6.32 directory with the old way to configure the Linux kernel:
Root @ jiq-desktop:/usr/src/linux-source-2.6.32 # Make oldconfig
(3) then execute:
Root @ jiq-desktop:/usr/src/linux-source-2.6.32 # Make bzimage
This process takes about an hour... After execution, a file vmlinux. O is generated under the current directory.
(4) then compile the module:
Root @ jiq-desktop:/usr/src/linux-source-2.6.32 # Make modules
This process is about an hour...
(5) Then you can install the module:
Root @ jiq-desktop:/usr/src/linux-source-2.6.32 # Make modules_install
This process takes about two minutes:
After the above steps are completed, a folder linux-2.6.32-24-generic will be generated under the/lib/modules directory, you have almost become, because you have constructed the kernel tree !!!
Create two files in the project folder as follows:
Hello. C (needless to say)
Makefile (it needs to be able to locate the kernel tree and then link the target module ):
Pay attention to the color of various variables ~ If you are interested, you can study this file by yourself. the only part that is related to hello. C is
OBJ-M: = Hello. O! Row 3 shows the location of the kernel tree build directory.
Run the following command under the current directory:
Root @ jiq-desktop:/usr/jiq/driverproject # Make
Generate the following files
Then use the insmod command to insert the module into the kernel tree:
Root @ jiq-desktop:/usr/jiq/driverproject # insmod./Hello. Ko
The module loads the init () method that triggers hello. C and outputs Hello world. If not, the output is put into/var/log/syslog. You can see your results when you open them!
Run the following command to uninstall the target module:
Root @ jiq-desktop:/usr/jiq/driverproject # rmmod./Hello. Ko
Now you have taken the first step in Linux driver development ~