驱动的第一个模块

概要:通过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验证

打印和程序相符合。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值