Johannes 《Linux内核模块与设备驱动开发:编写Linux驱动程序》 (9)manual_cdev

视频教程:Lecture 9 - 手动创建字符设备_哔哩哔哩_bilibili

代码:anGitHub - Johannes4Linux/Linux_Driver_Tutorial: My new Tutorial how to get started with Linux Kernel Modules and Linux Drivers. Smtarted in 2024 · GitHub

复制代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/cdev.h>

static dev_t dev_nr;
static struct cdev my_cdev;

static ssize_t my_read(struct file *f, char __user *u, size_t l, loff_t *o)
{
	pr_info("hello_cdev - Read is called\n");
	return 0;
}


static struct file_operations fops = {
	.read = my_read
};

static int __init my_init(void)
{
	int status;
#ifdef STATIC_DEVNR
	dev_nr = STATIC_DEVNR;
	status = register_chrdev_region(dev_nr, MINORMASK + 1, "hello_cdev");
#else
	status = alloc_chrdev_region(&dev_nr, 0, MINORMASK + 1, "hello_cdev");
#endif
	if (status) {
		pr_err("hello_cdev - Error reserving the region of device numbers\n");
		return status;
	}

	cdev_init(&my_cdev, &fops);
	my_cdev.owner = THIS_MODULE;

	status = cdev_add(&my_cdev, dev_nr, MINORMASK + 1);
	if (status) {
		pr_err("hello_cdev - Error adding cdev\n");
		goto free_devnr;
	}

	pr_info("hello_cdev - Registered a character device for Major %d starting with Minor %d\n", MAJOR(dev_nr), MINOR(dev_nr));
	return 0;

free_devnr:
	unregister_chrdev_region(dev_nr, MINORMASK + 1);
	return status;
}

static void __exit my_exit(void)
{
	cdev_del(&my_cdev);
	unregister_chrdev_region(dev_nr, MINORMASK + 1);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Johannes 4Linux");
MODULE_DESCRIPTION("A sample driver for manually registering a character device");

添加了头文件<linux/cdev.h>,

程序中调用的宏 :STATIC_DEVNR ,MINORMASK

手动注册字符设备:

status = register_chrdev_region(dev_nr, MINORMASK + 1, "hello_cdev");

register_chrdev_region 需要配置主/副设备号,这里用的 STATIC_DEVNR ,MINORMASK+1 来注册字符设备 hello_cdev

动态注册字符设备:

status = alloc_chrdev_region(&dev_nr, 0, MINORMASK + 1, "hello_cdev");

不需要手动配置,自动配置字符设备 hello_cdev

创建并添加字符设备:

cdev_init(&my_cdev, &fops);

&my_cdev 指向全局变量里这个指针,fops就将字符设备和读写操作结合起来

全局变量my_cdev 的所有者是这个模块,这样就将hello_cdev 接上了

my_cdev.owner = THIS_MODULE;

这样系统内核就添加字符设备,在 /proc/dev/中可以看到字符设备 hello_cdev,主设备号是dev_nr

status = cdev_add(&my_cdev, dev_nr, MINORMASK + 1);

不过这节系统内核添加hello_cdev这个设备驱动之后还是要mknod 创建字符设备 mknod /dev/hello0 c 236 0

相关推荐
不灭的程序员阿澄1 小时前
在飞牛 NAS 上用 Docker 运行 DeepSeek Harness
运维·docker·容器
Elastic 中国社区官方博客2 小时前
让大模型思考,让小模型执行:在 Elastic Workflows 中拆分 LLM 成本
大数据·运维·数据库·人工智能·elasticsearch·ai
艾伦_耶格宇2 小时前
【AI】-4 OpenCode Go 接入 Obsidian 完整指南
运维·开发语言·人工智能·agent·opencode
SXkehuirongsheng2 小时前
APP开发定制和模板开发哪个更实用?
大数据·运维·人工智能
fb_123453 小时前
Linux三剑客超全精讲(grep+sed+awk)零基础入门|正则+实战面试题
linux·运维·服务器
HiDev_4 小时前
【非标自动化】2、认识元器件(节流阀)
运维·自动化
火车叼位4 小时前
Bash 实现 IDE 式补全的组件与配置
linux·运维
晓晓_za8986684 小时前
Geo 优化服务 CI/CD 流水线搭建:源码自动构建、测试与灰度发布
运维·服务器·tcp/ip·spring·缓存·ci/cd
潘正翔4 小时前
k8s高级_调度器Deployment
linux·运维·云原生·容器·kubernetes·jenkins·devops
Tangyuewei5 小时前
388 个 PR:AI 自主运维实测
运维·人工智能