Linux驱动学习(二)--字符设备
设备分类
- 字符设备
- 块设备
- 网络设备
内核结构图:
字符设备号
字符设备号是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);