Linux驱动学习(二)--字符设备
- 软件开发
- 2025-08-30 21:27:02

设备分类 字符设备块设备网络设备
内核结构图:
字符设备号字符设备号是32位的无符号整型值
高12位:主设备号低20位:次设备号 查看设备号 cat /proc/devices 设备号构造直接使用宏MKDEV
#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi)) 设备号注册/注销注册设备号函数:
设备号注销函数
实验 结果 代码/* *chr_dev.c *Original Author: luoyunheng, 2025-02-19 * * Linux驱动之字符设备的设备号 */ #include <linux/init.h> #include <linux/module.h> #include <linux/kdev_t.h> #include <linux/fs.h> static int major = 222; static int minor = 0; static dev_t devno; static int hello_init(void) { int result; printk("hello_init\n"); devno = MKDEV(major, minor); result = register_chrdev_region(devno, 1, "loh"); if (result < 0 ) { printk("register dev number failed\n"); return result; } return 0; } static void hello_exit(void) { printk("hello_exit\n"); unregister_chrdev_region(devno, 1);\ return; } module_init(hello_init); module_exit(hello_exit);
Linux驱动学习(二)--字符设备由讯客互联软件开发栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“Linux驱动学习(二)--字符设备”