Original URL: http://blog.csdn.net/nexttake/article/details/8181008
Just read the "LINUX device Driver" written by O ' REILLY. The author has repeatedly emphasized that the kernel tree must be established when writing drivers. The so-called kernel tree, my understanding and online information said the same is a logical form of kernel source code.
Check the kernel version used by your OS first
[Email protected]:~$ uname-r
2.6.22-14-generic/* This is the result of my display */
If the system is installed, the source code is installed automatically. In the/USR/SRC directory there is a corresponding version of the directory to use. For example (I am under my own)
[Email protected]:/usr/src$ ls
Linux-headers-2.6.22-14
Linux-headers-2.6.22-14-generic
linux-source-2.6.22/* This is the extracted source directory */
LINUX-SOURCE-2.6.22.TAR.BZ2/* This is my next source package */
[Email protected]:/usr/src$
If there is no source code. (There is no general Ubuntu.)
Check out the source package that you can download (remember not to use this command with Superuser otherwise ...). Will be prompted without this command)
[Email protected]:/usr/src$ apt-cache Search Linux-source
Linux-source-linux kernel source with Ubuntu patches
Xen-source-2.6.16-linux kernel source for version 2.6.17 with Ubuntu patches
Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches
[Email protected]:/usr/src$
I chose Linux-source-2.6.22-linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install the
[Email protected]:/usr/src$sudo apt-get Install linux-source-2.6.22 (equivalent to download this version of the kernel source code)
After the download is complete, under/USR/SRC, the file name is: linux-source-2.6.22.tar.bz2, is a compressed package, decompression can get the entire kernel source code:
Can be uninstalled using Apt-get purgelinux-source-2.6.22.
Note that you have switched to Super User mode
[Email Protected]:/usr/src#sudo tar jxvf linux-source-2.6.20.tar.bz2
Unzip into a new directory/usr/src/linux-source-2.6.22, all the source code is in this directory.
--------------------------------------------------------------------------------------------------------------- ------------------------------------
Enter this directory
to start configuring the kernel select the fastest original configuration (default) mode (I am)
[email protected]:/usr/src/linux-source-2.6.22# make Oldconfig or make Defconfig
of course you can also use your preferred configuration such as menuconfig, Xconfig (must have GTK environment bar). It doesn't have to be trimmed anyway, so it's OK to configure it in that way.
When you're done, start make. It's a long time to be here for 11 hours. (Make sure the space is enough for me to compile and use 1.8G) I partition time to give/directory 30G space, I do not encounter this problem. It was my friend who met.
[email protected]:/usr/src/linux-source-2.6.22$ make
[email protected]:/usr/src/ Linux-source-2.6.22$ make Bzimage
Of course, the first make can also be done without execution, and directly makes Bzimage. After execution, you can see that a new file has been generated under the current directory: Vmlinux, whose property is-rwxr-xr-x.
Then:
[email protected]:/usr/src/linux-source-2.6.22#make modules /* Compile module */
[Email protected]:/usr/src/linux-source-2.6.22#make modules_install /* Installation Module */
After execution finishes, a new directory/lib/modules/2.6.22-14-generic/
is generated under/lib/modules. The build directory under this path is used for subsequent compilation of the module file. This completes the kernel compilation. You can restart the system.
This kernel tree is built it is not very difficult .....
--------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------
Write one of the simplest and most useless drivers.
I created 2 text files under the/home/shana/linux_q/directory hello.c Makefile
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);
I will not explain the procedure ...
Makefile file
[HTML]View Plaincopy
- Obj-m : = hello.o
- Kerneldir : =/lib/modules/2.6.38-8-generic/build/
- PWD : = $ (shell pwd)
- modules:$ (make)-C $ (kerneldir) m=$ (PWD) modules
- modules_install:$ (make)-C $ (kerneldir) m=$ (PWD) Modules_install
In fact, if you compile with this command, you can makefile a line
[HTML]View Plaincopy
- Obj-m : = hello.o
[Email protected]:~/linux_ driver Development $make-c/lib/modules/2.6.38-8-generic/build/m=/home/yzy/mydriver
[Email protected]:~/linux_ driver Development $ ls-l
Total Dosage 124
-rw-r--r--1 Shana Shana 303 2008-03-16 10:43 hello.c
-rw-r--r--1 Shana Shana 4array03array 2008-03-16 12:11 Hello.ko
-rw-r--r--1 Shana Shana 687 2008-03-16 12:11 HELLO.MOD.C
-rw-r--r--1 Shana Shana 25840 2008-03-16 12:11 HELLO.MOD.O
-rw-r--r--1 Shana Shana 24360 2008-03-16 12:11 hello.o
-rw-r--r--1 Shana Shana 8344 2008-03-16 0array:17 linux_qudong_qu.txt
-rw-r--r--1 Shana Shana 266 2008-03-16 12:0array Makefile
-rw-r--r--1 Shana Shana 0 2008-03-16 12:11 module.symvers
[Email protected]:~/linux_ Driver Development $
Then load the module (super User)
[Email protected]:/home/shana/linux_ Driver Development # Insmod./hello.ko
According to the example in the book will show Hello at the terminal, world but nothing appears after running (reason puzzled)
[Email protected]:/home/shana/linux_ Driver Development # Insmod./hello.ko or sudo-s switch to root user first
[Email protected]:/home/shana/linux_ Driver Development #
View load Modules
[Email protected]:/home/shana/linux_ Driver Development # Lsmod
Module Size used by
Hello 2560 0
Already loaded on the ~ ~
Deleting a module
[Email protected]:/home/shana/linux_ Driver Development # Rmmod Hello
[Email protected]:/home/shana/linux_ Driver Development #
--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------
What about the output of that program? The book says that if it does not appear in the terminal it will be written into the syslog file
[Email protected]:~/linux_ driver Development $ DMESG
[2266.586863] Hello, world\n
[2389.281426] Goodbye, cruel world\n
At this point all the work is done.
"Go" the first Linux kernel driver