在linux内核里面新添加一个驱动模块

这篇文章完全参考:原文:https://blog.csdn.net/u012247418/article/details/83684126  

原文讲解的特别详细,希望大家都去看原文

发这篇博客的原因完全只是为了方便自己以后查阅。

1. 构建测试模块:hello

1.1 在linux-3.4/drivers/下新建目录hello

cd linux-3.4/drivers/

mkdir hello

1.2 在hello/下新建hello.c Makefile Kconfig三个文件

hello.c:

#include <linux/module.h> //所有模块都需要的头文件
#include <linux/init.h>   // init&exit相关宏
#include <linux/kernel.h>
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("baoli");
MODULE_DESCRIPTION("hello world module");
 
static int __init hello_init(void)
{
      printk(KERN_WARNING "hello world.\n");
      return 0;
}
static void __exit hello_exit(void)
{
      printk(KERN_WARNING "hello exit!\n");
}
 
module_init(hello_init);
module_exit(hello_exit);

Makefile:

obj-$(CONFIG_HELLO) += hello.o

Kconfig:

menu "HELLO TEST Driver "
comment "HELLO TEST Driver Config"
 
config HELLO
	tristate "hello module test"
	default m
	help
	This is the hello test driver 
 
endmenu

1.3 修改上一级目录的Kconfig和Makefile

进入linux-3.4/drivers/

1)编辑Makefile,在后面添加一行:

obj-$(CONFIG_HELLO) += hello/

2)编辑Kconfig,在后面添加一行:

source "drivers/hello/Kconfig"

注:某些内核版本需要同时在arch/arm/Kconfig中添加:source "drivers/hello/Kconfig"

2. make menuconfig配置

1)执行:make menuconfig ARCH=arm

2)选择并进入:Device Drivers选项

可以看到新增 HELLO TEST Driver选项

3)进入 HELLO TEST Driver选项

可以选择<m> <y> <n>,分别为编译成内核模块、编译进内核、不编译。

3. 编译

退出保存后

开始编译内核

1)如果选择编译成模块<m>

编译内核过程中,会有如下输出:

LD drivers/hello/built-in.o

CC [M] drivers/hello/hello.o

CC drivers/hello/hello.mod.o

LD [M] drivers/hello/hello.ko

2)如果选择编译进内核<y>

编译内核过程中,会有如下输出:

CC drivers/hello/hello.o

LD drivers/hello/built-in.o

编译完成后,drivers/hello/下新增hello.o和hello.ko,并且/linux-3.4/output/lib/modules/3.4.39/下也会有hello.ko。
 

猜你喜欢

转载自blog.csdn.net/a3121772305/article/details/86188126