1、申请设备号
#include <linux/fs.h> int register_chrdev_region(dev_t first, unsigned int count, char *name); 静态分配字符设备号,从fist开始的count个,name为设备名称(name会出现在/proc/devices和sysfs中),成功返回0,失败返回一个负的错误码 int alloc_chrdev_region(dev_t *dev, unsigned int firstminor, unsigned int count, char *name); 动态分配字符设备号,主设备号动态分配,次设备号从firstminor开始的count个,name为设备名称(动态分配的主设备号可以在/proc/devices中获取) void unregister_chrdev_region(dev_t first, unsigned int count); 注销字符设备号,从first开始的count个
2、初始化字符设备
#include <linux/cdev.h> 动态创建 struct cdev *my_cdev = cdev_alloc(); my_cdev->ops = &my_fops; 静态创建 void cdev_init(struct cdev *cdev, struct file_operations *fops);
3、添加和删除字符设备
#include <linux/cdev.h> 添加字符设备 int cdev_add(struct cdev *dev, dev_t num, unsigned int count); 删除字符设备 void cdev_del(struct cdev *dev);
4、file_operations