概要:通过qemu 启动kernel 加载第一个驱动。
平台:ubuntu 20.04
kernel 版本:linux-4.0
前期准备:linux-4.0 内核下载 linux qume开发环境搭建
开始驱动模块编写
1. 驱动程序
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
static int __init helloworld_init(void)
{
printk(KERN_INFO"pomepy Hello World\n");
return 0;
}
static void __exit helloworld_exit(void)
{
printk(KERN_INFO"pomepy Godbye Hello World\n");
}
module_init(helloworld_init);
module_exit(helloworld_exit);
MODULE_LICENSE("GPL");
命名为 Helloworld.c ;并在kernel/drivers/下新建rivotek文件夹,将Helloworld.c 保存在该文件夹下。
2. Makefile 和 Kconfig 编写
Kconfig
config HELLOWORLD
tristate "Hello Word driver support"
default m
help
This is a driver module test,just for pratice.
Makefile
#
# Makefile for the Rivotek device drivers
#
obj-$(CONFIG_HELLOWORLD) += Helloworld.o
通过以上两个配置,可以确定,当 make xxx_defconfig 时, 一定会将Helloworld驱动编译成为Helloworld.ko文件。
3. dirvers的Makefile和Kconfig
Kconfig
diff --git a/drivers/Kconfig b/drivers/Kconfig
index c0cc96ba..aa659097 100644
--- a/drivers/Kconfig
+++ b/drivers/Kconfig
@@ -182,4 +182,6 @@ source "drivers/thunderbolt/Kconfig"
source "drivers/android/Kconfig"
+source "drivers/rivotek/Kconfig"
+
endmenu
Makefile
diff --git a/drivers/Makefile b/drivers/Makefile
index 527a6da8..e178dba6 100644
--- a/drivers/Makefile
+++ b/drivers/Makefile
@@ -165,3 +165,6 @@ obj-$(CONFIG_RAS) += ras/
obj-$(CONFIG_THUNDERBOLT) += thunderbolt/
obj-$(CONFIG_CORESIGHT) += coresight/
obj-$(CONFIG_ANDROID) += android/
+
+
+obj-y += rivotek/
通过以上两个文件的配置,确保内核在配置和编译的时候可以囊括到rivotek文件夹下面的Kconfig 和 Makefile文件。
此时,通过make menuconfig 查看,如下图:
和Kconfig中设置相符合。
4.验证
清理并重新编译内核后,安装、卸载该模块。
执行 “make distclean && make [xxx_defconfig] && make -j* ” 命令
make distclean && make vexpress_qemu_defconfig && make -j10
编译有如下log
查看drivers/rivotek/目录
$ ls drivers/rivotek/
built-in.o Helloworld.c Helloworld.ko Helloworld.mod.c Helloworld.mod.o Helloworld.o Kconfig Makefile modules.builtin modules.order
启动qemu验证
打印和程序相符合。