尽管linux 2.6 后,驱动的框架变化很多,但是字符设备创建的基础并没有变,只是需要我们自己实现的机会变少了。
因而,在学习驱动时最先学到的东西,反而淡忘了。
这里做下温习。
struct cdev 字符设备的基础结构 - 在系统中标识次字符设备的。实际使用中,往往需要在此之上做些扩展。 比如
struct xxx_dev {
struct cdev cdev;
int value;
}
这样在初始化的时候
我们先申请字符设备号 -- register_chrdev_region, or allo_chrdev_region.
然后 分配 struct xxx_dev 结构体的内存, 并对扩展变量和 cdev做初始化。 使用cdev_init 等,文件操作符指针也是在这里赋值给cdev的成员变量.ops
最后,使用cdev_add,添加次设备。
描述通用字符设备的数据结构 struct cdev定义如下:
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};