字符设备驱动

来源:互联网 发布:武汉大学网络自助服务 编辑:程序博客网 时间:2024/06/06 09:35

2.4内核

static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)static inline void unregister_chrdev(unsigned int major, const char *name)

2.6内核

#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))#define MINOR(dev) ((unsigned int) ((dev) & MINORMASK))#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi)) //将主设备号和次设备号转换成dev_t类型int register_chrdev_region(dev_t from, unsigned count, const char *name) //注册设备号int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count, const char *name)void unregister_chrdev_region(dev_t from, unsigned count)struct cdev *cdev_alloc(void)void cdev_init(struct cdev *cdev, const struct file_operations *fops) //初始化int cdev_add(struct cdev *p, dev_t dev, unsigned count) //添加到内核void cdev_del(struct cdev *p) //删除

手动创建设备节点

# cat /proc/devices //查看主设备号# mknod /dev/设备名 c|b(c是字符设备,b是块设备) 主设备号 次设备号 //创建# rm /dev/char //删除

借助class创建设备节点
从linux内核2.6的某个版本之后,devfs不复存在,udev成为devfs的替代

#define class_create(owner, name)void class_destroy(struct class *cls)struct device *device_create(struct class *class, struct device *parent, dev_t devt, void *drvdata, const char *fmt, ...)void device_destroy(struct class *class, dev_t devt)

创建多个设备

struct cdev *cdev;cdev = kzalloc(sizeof(struct cdev) * COUNT, GFP_KERNEL);for(i = 0; i < COUNT; i++){    cdev_init(&cdev[i], &fops);    cdev_add(&cdev[i], dev+i, 1);}for(i = 0; i < COUNT; i++){    cdev_del(&cdev[i]);}
1 0
原创粉丝点击