字符设备驱动开发
步骤:
kernel阶段:
1.申请设备号
静态申请:指定主设备号和次设备号
int register_chrdev_region(dev_t from, unsigned count, const char *name)
参数:dev_t from------->设备号
unsigned count—>设备数
const char *name–>设备名
返回值:成功返回0,失败返回其他值
动态申请:自动申请未被占用的设备号
alloc_chrdev_region()
2.初始化设备cdev
cdev_init(struct cdev *cdev, const struct file_operations *fops)
参数:struct cdev *cdev------>存放设备基本信息
const struct file_operations *fops----->对设备的操作
3.添加设备
cdev_add(struct cdev *p, dev_t dev, unsigned count)
4.创建节点
1命令行输入:mknod /dev/mychr c 500 0
| | | |
名称 字符型设备 主设备号 次设备号
2自动创建:
a)class_create(owner, name)
参数:owner—>一般用THIS_MODULE
name---->逻辑类名 随便取
返回值:struct class
b)device *device_create(struct class *class, struct device *parent,
dev_t devt, void *drvdata, const char *fmt)
参数:struct class *class
device *parent—>NULL
dev_t devt-------->设备号
void *drvdata----->NULL
const char *fmt---->节点设备名
字符设备驱动开发
最新推荐文章于 2024-07-28 14:05:57 发布