驱动开发-hello world

helloworld.c文件的内容, 最简单,无参数版本的,都需要Makefile进行构建
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>

//pro_info 是一个系统函数,类型print 这种,rust 实现的一个宏定义
//模块初始化的执行的函数
static int __init hellowolrd_init(void) {
    pr_info("Hello world!\n");
    return 0;
}
//模块退出执行的函数
static void __exit hellowolrd_exit(void) {
    pr_info("End of the world\n");
}
//标准流程,吃方面的要+肠+蛋
module_init(hellowolrd_init);
module_exit(hellowolrd_exit);
MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
MODULE_LICENSE("GPL");
下面的是一个有参数版本的,使用套路:
外面随意的定义参数, 然后, 使用内部的宏声明一下,这样就关联起来了。
module_param(变量名称, 变量类型, 变量权限)
MODULE_PARM_DESC 变量说明
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/moduleparam.h>

static char *mystr = "hello";
static int myint = 1;
static int myarr[3] = { 0, 1, 2 };

module_param(myint, int, S_IRUGO);
module_param(mystr, charp, S_IRUGO);
module_param_array(myarr, int, NULL, S_IWUSR | S_IRUSR);

MODULE_PARM_DESC(myint, "this is my int variable");
MODULE_PARM_DESC(mystr, "this is my char pointer variable");
MODULE_PARM_DESC(myarr, "this is my array of int");
MODULE_INFO(my_field_name, "What eeasy value");

static int __init hellowolrd_init(void)
{
	pr_info("Hello world with parameters!\n");
	pr_info("The *mystr* parameter: %s\n", mystr);
	pr_info("The *myint* parameter: %d\n", myint);
	pr_info("The *myarr* parameter: %d, %d, %d\n", myarr[0], myarr[1],
		myarr[2]);
	return 0;
}

static void __exit hellowolrd_exit(void)
{
	pr_info("End of the world\n");
}

module_init(hellowolrd_init);
module_exit(hellowolrd_exit);
MODULE_AUTHOR("John Madieu <john.madieu@gmail.com>");
MODULE_LICENSE("GPL");
Makefile内容(写的还不错):
obj-m 老演员,helloworld.o 就会默认找到helloworld.c进行编译
modules modules_install help clean 相当于参数,
make -C *** -M *** modules/modules_install 等参数之一
obj-m := helloworld-params.o helloworld.o

KERNELDIR ?= /lib/modules/$(shell uname -r)/build

all default: modules
install: modules_install

modules modules_install help clean:
	$(MAKE) -C $(KERNELDIR) M=$(shell pwd) $@

成果展示:

安装模块 :insmod helloworld-params.ko 

卸载模块:rmmod helloworld_params

列出模块:lsmod

查看信息:dmesg

点评一下:内核的固定套路就是定义很多宏,貌似也不能跳转,都切换到rust吧。  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值