1、编写驱动程序框架
#include <linux/module.h>
#include <linux/init.h>
static int __init hello_init(void)
{
printk("Hello, I'm ready!\n");
return 0;
}
static void __exit hello_exit(void)
{
printk("I'll be leaving, bye!\n");
}
module_init(hello_init);
module_exit(hello_exit);
MODULE_LICENSE("GPL");
2、编写makefile
# Makefile2.6
ifneq ($(KERNELRELEASE),)
#kbuild syntax. dependency relationshsip of files and target modules are listed here.
obj-m := beepdrv.o
else
PWD := $(shell pwd)
KVER = 2.6.27.8
KDIR := /home/chenxibing/lpc3250/linux-2.6.27.8
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions
endif
注意:
1、all 和clean下的命令要用tab建隔开。
若是不编译进内核最简单的驱动模块框架就完事了。若要编译进内核还需要进行如下修改。
3、编写Kconfig
例如我想把模块放到/derives/char目录下,则在char目录下简历hello文件夹,然后将上面的.c放进去。然后在写一个Kconfig内容如下:
config HELLO
tristate "hello world"
help
hello
很简单,具体Kconfig语法百度一下就行
第一行:是配置宏,在对应的Makefile中要用到。
第二行:是三态开关 后面的字符串是在menuconfig中会看到的内容
第三行:是help要下一行写帮助信息。
然后在写一个makefile内容如下:
obj-$(CONFIG_HELLO)+=hello.o
就很简单,意思是Kconfig中的内容选中则会出现obj-M obj-y obj-n 三中状态。
4。修改上级目录中的Kconfig
修改好本文件的内容后,为了能关联到整个内核中要修改上级文件。本例中打开char目录下的Kconfig如下:
#
# Character device configuration
#
menu "Character devices"
source "drivers/char/hello/Kconf"
config VT
bool "Virtual terminal" if EMBEDDED
depends on !S390
。。。。。。。。。。。。。。。。
不要在意后面的内容,主要是在意source "drivers/char/hello/Kconf"要添加对路径。这步配置完成就能看到hello模块的选项了。
5、修改上级目录的Makefile
为了能让模块真正编译进内核需要修改char 目录下的Makefile,如下
增加
obj-y += hello/
一定要添加“/”要不然报错
arm-fsl-linux-gnueabi-ld: drivers/char/hello: No such file: File format not recognized
上面就是驱动模块的框架和注意事项