skywang12345

导航

 

Linux PC端驱动程序实例

1新建驱动程序

新建程序文件hello.c

(1)hello.c的路径如下:

helloworld/hello.c

(2)hello.c的内容如下:

 1 #include <linux/init.h>
 2 #include <linux/module.h>
 3 
 4 static int __init hello_init(void)
 5 {
 6     printk("%s\n", __FUNCTION__);
 7     return 0;
 8 }
 9 
10 static void __exit hello_exit(void)
11 {
12     printk("%s\n", __FUNCTION__);
13 }
14 
15 module_init(hello_init);
16 module_exit(hello_exit);
View Code

 

2添加配置文件

新建配置文件Makefile

(1)Makefile的路径如下:

Helloworld/Makefile

(2)Makefile的内容如下:

1 obj-m := hello.o  #这个是要中间文件
2 Kernel_path=/usr/src/linux-headers-$(shell uname -r)  #内核存在的路径
3 all:
4     make -C $(Kernel_path) M=$(PWD) modules
5 clean:
6     make -C $(Kernel_path) M=$(PWD) clean
View Code

 

3编译

进入helloworld目录,并执行make命令,生成hello.ko模块文件

 

4加载/卸载模块

4.1加载模块

(1)执行以下命令加载模块

# insmod hello.ko

(2)用dmesg查看信息

# hello_init

 

4.2卸载模块

(1)执行以下命令卸载模块

# rmmod hello.ko

(2)用dmesg查看信息

# hello_exit

 

 

posted on 2013-05-14 22:04  如果天空不死  阅读(423)  评论(0编辑  收藏  举报