為linux核心添加新驅動,並添加到menuconfig菜單【實踐簡記】

來源:互聯網
上載者:User

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.