一个简单的驱动程序———模块框架
1,基本的模块的框架
1>头文件
#include <linux/init.h>
#include <linux/module.h>
2>模块加载函数和模块卸载函数
static int __init hello_init(void)
{
printk("---------%s------------\n",__FUNCTION__);
return 0;
}
static void __exit hello_exit(void)
{
printk("---------%s------------\n",__FUNCTION__);
}
3>模块声明和认证
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL"); //认证
2,编译驱动模块
编写:Makefile
KERNEL_DIR = /home/lpf/1803/s5pv210/kernel/linux-3.0.8 #指定内核源码路径
CUR_DIR = $(shell pwd)
all:
#使make进入内核源码目录,并将当前目录下的源码作为内核的模块一起编译
make -C $(KERNEL_DIR) M=$(CUR_DIR) modules
clean:
#删除上面生成的文件
make -C $(KERNEL_DIR) M=$(CUR_DIR) clean
install:
cp *.ko /opt/rootfs/drv_module
obj-m = hello_drv.o //指定将哪个.c文件编译为驱动模块
---------------------------------------------------------
make
#使make进入内核源码目录,并将当前目录下的源码作为内核的模块一起编译
make -C /home/lpf/1803/s5pv210/kernel/linux-3.0.8 M=/home/lpf/1803/s5pv210/driver/1day_code modules
make[1]: Entering directory `/home/lpf/1803/s5pv210/kernel/linux-3.0.8'
CC [M] /home/lpf/1803/s5pv210/driver/1day_code/hello_drv.o
Building modules, stage 2.
MODPOST 1 modules
CC /home/lpf/1803/s5pv210/driver/1day_code/hello_drv.mod.o
LD [M] /home/lpf/1803/s5pv210/driver/1day_code/hello_drv.ko
make[1]: Leaving directory `/home/lpf/1803/s5pv210/kernel/linux-3.0.8'
ls
hello_drv.c hello_drv.mod.c hello_drv.o modules.order
hello_drv.ko hello_drv.mod.o Makefile Module.symvers
make install
cp *.ko /opt/rootfs/drv_module
3,模块加载和卸载
[root@farsight /drv_module]# insmod hello_drv.ko //加载模块
---------hello_init------------
[root@farsight /drv_module]# lsmod //查看已加载的模块
hello_drv 747 0 - Live 0x7f000000
[root@farsight /drv_module]# rmmod hello_drv //卸载模块
---------hello_exit------------
[root@farsight /drv_module]#
代码如下:
hello_drv.c文件代码
static int __init hello_init(void)
{
printk(“———%s————\n”,FUNCTION);
return 0;
}
static void __exit hello_exit(void)
{
printk(“———%s————\n”,FUNCTION);
}
//声明
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE(“GPL”); //认证
Makefile文件代码
KERNEL_DIR = /home/lpf/1803/s5pv210/kernel/linux-3.0.8 #指定内核源码路径
CUR_DIR = $(shell pwd)
all:
#使make进入内核源码目录,并将当前目录下的源码作为内核的模块一起编译
make -C
(CUR_DIR) modules
clean:
#删除上面生成的文件
make -C
(CUR_DIR) clean
install:
cp *.ko /opt/rootfs/drv_module
obj-m = hello_drv.o