Linux driver development environment Configuration

Source: Internet
Author: User

Link: http://hi.baidu.com/igtdqoihjhbacsq/item/9b937ad7c69d4311d78ed091

I have tried hzxing and run it on my 64-bit 10.04 ubuntu. This method works. Thank you, hzxing!

The following is the body of the article

**************************************** ****

Hzxing1010 Space
Linux driver development environment Configuration

This article is based on multiple posts on the Internet, but it is not original. The only thing that is gratifying is the implementation on the local machine. Because it failed several times after all. It is also quite difficult for me to learn many simple problems in driver programming. Don't laugh. Finally, I hope this article will provide some help to those who have just learned to drive and haven't started yet.
I just read O 'Reilly's "Linux Device Driver. 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.
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)
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 */
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)
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
I chose linux-source-2.6.22-Linux kernel source for version 2.6.22 with Ubuntu patches this ~
Then install
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 both get the source code of the entire kernel:
Note that the superuser mode has been switched.
Jxvf linux-source-2.6.20.tar.bz2
Decompress the package and generate a new directory/usr/src/linux-source-2.6.22 where all source code is stored.
Enter this directory
Start to configure the kernel and select the fastest original configuration (default) method (I do)
# 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.
Make
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:
Modules/* Compilation module */
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.20/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.
_ 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.
_ 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'
_ Driver development $

_ 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
_ Driver development $

Then load the module (Super User)
_ 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)
_ Driver development # insmod./Hello. Ko
_ Driver development #
View the load Module
_ Driver development # lsmod
Module size used
Hello 2560 0
Already loaded ~~
Delete A Module
_ Driver development # rmmod hello
_ 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.
_ 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
_ Driver development $
Now all the work has been completed. Is it useful to you?

* ***************** This is the end of the article **************** *******

My supplement:

1. Make oldconfig has a lot of options to be strong. If you don't want to always choose, use yes "" | make oldconfig to finish the process at one time without interrupting.

2. The intermediate commands are make modules and make modules_install.

3. a variable in makefile needs to be changed, which is the following line:

Kerneldir: =/lib/modules/2.6.20/build

Change 2.6.20 to the file path on your machine. If you are not sure about the path, you can use

Ls-LHT/lib/modules/

Check the latest modified folder in this path. For example, if my folder is 2.6.32.60 + drm33.26, correct the folder accordingly.

Related Article

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.