Hello world for Linux driver

Source: Internet
Author: User
Reprinted from jjzhang166 and finally edited jjzhang166

Http://hi.baidu.com/ah%5F%5Ffu/blog/item/57b76201e27095de267fb5d4.html

Finally, I made up my mind to study the Linux kernel and drive development. I was not afraid of the shame of tens of millions of people. I was brave enough to write my learning process:

1. About Directories
/Lib/modules/2.6.9-42. elsmp/build/the directory where the kernel source code is located
Generally, run the following command to enter the Directory: CD/lib/modules/$ (uname-R)/build/
This directory actually points to:/usr/src/kernels/2.6.9-42. EL-smp-i686

2. Compile the makefile used by the driver.
In fact, when compiling the driver, a preset makefile is used, which is located:
/Lib/modules/$ (uname-R)/build/makefile
Note: m is capitalized.

3. source code of the Linux driver Hello World copied online:
// 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! \ N ");
}

Module_init (hello_init );
Module_exit (hello_exit );

4. Write a makefile to compile the driver: (version 1 is the simplest)
# The following line shows the content of the makefile. Note that M is capitalized.
OBJ-M: = Hello. o

Save hello. C and makefile in the same directory, and then execute:
Make-C/lib/modules/'uname-R'/build subdirs = $ PWD modules
In this way, the driver is compiled, and the result is the hello. Ko file.
Note: makefile must be written as makefile. If it is written as makefile, it cannot be compiled. (It takes more than n time to complete this step)

5. Write another makefile: (version 2: the most time-consuming)
# The content of makefile is as follows:
OBJ-M: = Hello. o
Kernel_dir: =/lib/modules/$ (shell uname-R)/build
PWD: = $ (shell PWD)
ALL:
Make-C $ (kernel_dir) subdirs = $ (PWD) Modules
Clean:
RM *. O *. Ko

Then execute: make the compilation is successful, and the command line does not add parameters, which is very easy.
Note: All: And clean: The following row. The front is a tab key.

6. Load the driver:
Run
Insmod./Hello. Ko
The screen does not respond. (Because I used a remote terminal to connect to Windows)
OK. Let's let the time go back and go back to loading the driver. open another window and execute:
Tail-F/var/log/message
Then execute the following in the original window:
Insmod./Hello. Ko
Haha, the long-awaited Hello world is displayed in the/var/log/message file!

7. view the driver:
Lsmod saw the hello driver in it.

8. Uninstall the driver:
Rmmod hello
Goodbye is displayed in/var/log/message.

 

The appendix is the compiled helloworldProgram

Hello. c

01 # Include <Linux/init. h>
02  
03 # Include <Linux/module. h>
04  
05  
06  
07 Static Int Hello_init (Void)
08  
09 {
10  
11 Printk (kern_info"Hello world enter \ n");
12  
13 Return 0;
14  
15 }
16  
17  
18  
19 Static Void Hello_exit (Void)
20  
21 {
22  
23 Printk (kern_info"Hello World exit \ n");
24  
25 }
26  
27  
28  
29 Module_init (hello_init );
30  
31 Module_exit (hello_exit );
32  
33  
34  
35 Module_author ("Barry song <21cnbao@gmail.com>");
36  
37 Module_license ("Dual BSD/GPL");
38  
39 Module_description ("A simple hello World module");
40  
41 Module_alias ("A simplest module");

Makefile

View sourceprint?
01 Kvers = $ (shell uname-R)
02  
03 # Kernel Modules
04 OBJ-M + = Hello. o
05  
06 # Specify flags for the module compilation.
07 # Extra_cflags =-g-O0
08  
09 Build: kernel_modules
10  
11 Kernel_modules:
12 Make-C/lib/modules/$ (kvers)/build M = $ (curdir) Modules
13  
14 Clean:
15 Make-C/lib/modules/$ (kvers)/build M = $ (curdir) clean

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.