為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就被編譯成模塊了。


http://hnwwff.blog.163.com/blog/static/3216909220116734646732/?latestBlog

arrow
arrow
    全站熱搜

    東勢厝滴yang 發表在 痞客邦 留言(0) 人氣()