) Linux driver development environment Configuration

Source: Internet
Author: User

I just read the Linux Device Driver written by o'reilly.Program. The author has repeatedly stressed that the kernel tree must be established when writing drivers. The so-called kernel tree is a logical form of kernel source code. How can we establish it? For this reason, the Internet was "overwhelming" and the result was "a fiasco".
For this reason, after four hours a day (including the time for eating and sleeping, of course), a simple hello wrold is not implemented. (P22 pages in the book are the simplest and most useless driving event columns)
However, it is hard to be honest. Today, I finally figured out what was going on. Let me take it easy.

First, check the kernel version used by your OS.

Shana @ Shana :~ $ Uname-R
2.6.22-14-generic/* this is the result of my display */

If the source code is automatically installed during system installation. There is a corresponding version directory under the/usr/src directory. Example)

Shana @ Shana:/usr/src $ ls
Linux-headers-2.6.22-14
Linux-headers-2.6.22-14-generic
Linux-source-2.6.22/* This is the decompressed source directory */
Linux-source-2.6.22.tar.bz2/* this is my source package */
Shana @ Shana:/usr/src $

If no source code is available. (Not UBUNTU)
Check the downloadable source code package (remember not to use this command by a Super User, otherwise ...... This command is not displayed)

Shana @ Shana:/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
Shana @ Shana:/usr/src $

I chose linux-source-2.6.22-Linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install

Shana @ Shana:/usr/src $ sudo apt-Get install linux-source-2.6.22

After the download is complete, under/usr/src, the file name is: linux-source-2.6.22.tar.bz2, is a compressed package, extract can get the entire KernelSource code:

Note that the superuser mode has been switched.

Root @ Shana:/usr/src # tar jxvf linux-source-2.6.20.tar.bz2

Decompress the package and generate a new directory/usr/src/linux-source-2.6.22 for all the sourcesCodeAll in this directory.

Enter this directory

Start to configure the kernel and select the fastest original configuration (default) method (I do)

Root @ Shana:/usr/src/linux-source-2.6.22 # Make oldconfig

Of course, you can also use your favorite configuration methods such as menuconfig and xconfig (you must have a GTK environment ). You don't need to crop anything, so you can configure it in any way.

After that, start to make it. Generally, it takes an hour. (Ensure that the space is sufficient. I used 1.8 GB after compilation.) I gave the/directory 30 GB space for the partition. I did not encounter this problem. My friend met me.

Shana @ Shana:/usr/src/linux-source-2.6.22 $ make

Shana @ Shana:/usr/src/linux-source-2.6.22 $ make bzimage

Of course, the first make statement can also be executed without making bzimage directly. After the execution is complete, a new file named vmlinux is generated in the current directory. Its attribute is-rwxr-XR-X.

Then:

Root @ Shana:/usr/src/linux-source-2.6.22 # Make modules/* Compilation module */

Root @ Shana:/usr/src/linux-source-2.6.22 # Make modules_install/* installation module */

After the execution, a new directory/lib/modules/2.6.22-14-generic/will be generated under/lib/modules/
. The build directory under this path will be used for subsequent compilation of module files. So far, the kernel compilation is complete. You can restart the system.

So far, it is not very difficult to establish the kernel tree .....

Write the simplest and most useless driver.
I created two text files hello. c makefile in the/home/Shana/linux_q/directory.

// 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 program ......

Makefile

OBJ-M: = Hello. o
Kerneldir: =/lib/modules/2.6.22-14-generic/build
PWD: = $ (shell PWD)

Modules:
$ (Make)-C $ (kerneldir) M = $ (PWD) Modules

Modules_install:
$ (Make)-C $ (kerneldir) M = $ (PWD) modules_install

If all of the above are complete, this error will occur during make.

Shana @ Shana :~ /Linux _ driver development $ make
Make: There is nothing to do for 'modules '.

The reason is very simple. You must have copied it from me ~~~ The makefile format is incorrect ~
The solution is to move $ (make)-C $ (kerneldir) M = $ (PWD) modules_install to the beginning of the line, and then press the tab key to automatically align
At this time, the variables inside are all green ~ Then go to make.

Shana @ Shana :~ /Linux _ driver development $ make
Make-C/lib/modules/2.6.22-14-generic/build M =/home/Shana/Linux _ driver development modules
Make [1]: Entering directory '/usr/src/linux-headers-2.6.22-14-generic'
CC [m]/home/Shana/Linux _ driver development/Hello. o
Building modules, stage 2.
Modpost 1 modules
CC/home/Shana/Linux _ driver development/Hello. Mod. o
LD [m]/home/Shana/Linux _ driver development/Hello. Ko
Make [1]: Leaving directory '/usr/src/linux-headers-2.6.22-14-generic'
Shana @ Shana :~ /Linux _ driver development $

Shana @ Shana :~ /Linux _ driver development $ LS-l
Total usage 124
-RW-r -- 1 Shana 303 2008-03-16 hello. c
-RW-r -- 1 Shana 49039 hello. Ko
-RW-r -- 1 Shana 687 hello. Mod. c
-RW-r -- 1 Shana 25840 hello. Mod. o
-RW-r -- 1 Shana 24360 hello. o
-RW-r -- 1 Shana 8344 2008-03-16 linux_qudong_qu.txt
-RW-r -- 1 Shana 266 makefile
-RW-r -- 1 Shana 0 2008-03-16 12:11 module. symvers
Shana @ Shana :~ /Linux _ driver development $

Then load the module (Super User)

Root @ Shana:/home/Shana/Linux _ driver development # insmod./Hello. Ko

According to the example in the book, Hello, world will be displayed on the terminal, but nothing will appear after the operation (the reason is puzzled)

Root @ Shana:/home/Shana/Linux _ driver development # insmod./Hello. Ko
Root @ Shana:/home/Shana/Linux _ driver development #

View the load Module

Root @ Shana:/home/Shana/Linux _ driver development # lsmod
Module size used
Hello 2560 0

Already loaded ~~

Delete A Module

Root @ Shana:/home/Shana/Linux _ driver development # rmmod hello
Root @ Shana:/home/Shana/Linux _ driver development #

What is the output of the program? It indicates that if the terminal does not exist, it will be written into the syslog file.

Shana @ Shana :~ /Linux _ driver development $ CAT/var/log/syslog | grep world
Mar 16 12:14:53 Shana kernel: [5937.529297] Hello, world
Mar 16 12:16:05 Shana kernel: [6009.439036] Goodbye, cruel world
Shana @ Shana :~ /Linux _ driver development $

Now all the work has been completed. Is it useful to you?

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/ruixj/archive/2010/06/12/5667551.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.