为Android 内核添加新驱动,并提供menuconfig 选项
为Android 的Linux 内核2.6.25 添加驱动。
1.
在drives
目录下添加hello
目录,内含hello.c Kconfig Makefile
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);
Kconfig
内容:
config HELLO
tristate "Hello Driver added by Dong"
default n
help
test for adding driver to menuconfig.
MakeFile
内容:
obj-$(CONFIG_HELLO) += hello.o
2.
上面的Kconfig
文件再加上下面的两个配置,可使hello
项出现在配置菜单中。
在arch/arm/Kconfig menu "Device Drivers" endmenu
之间添加
source "drivers/hello/Kconfig"
在drivers/Kconfig menu "Device Drivers" endmenu
之间添加
source "drivers/hello/Kconfig
"
(不知为什么arch/arm/Kconfig
中竟然含有Drivers
里Kconfig
内容的一个复本,
实验证明只对drivers/Kconfig
中修改内容无效。)
3.
修改Drivers
目录下的Makefile
文件,添加如下行,
obj-$(CONFIG_HELLO) += hello/
当CONFIG_HELLO
为y
或m
时,使系统能找到hello
驱动的makefile
。
linux-2.6.25 目录下make menuconfig ,在Device Drivers 菜单下选中Hello Driver added by Dong 项比如M ,作为module 。然后保存配置,执行make 命令,就可以看到 CC [M] drivers/hello/hello.o 的log 了,hello 目录里生成了hello.o hello.ko 的等文件。
流程:
假如在make menuconfig
时配置Hello Driver added by Dong
为M
(即编为模块,而不是编进linux
内核)
则.config
中就会多一行
CONFIG_HELLO = m
如此一来,drivers/Makefile
中obj-$(CONFIG_HELLO) += hello/
就变成了
obj-m +=hello/
于是执行make
命令时,便会进入hello
目录里找makefile
,MakeFile
内容obj-$(CONFIG_HELLO) += hello.o
变成了obj-m +=hello.o
,所以hello.c
就被编译成模块了。