文件位于fs/chr_dev.c
重要的结构体
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
struct char_device_struct {
struct char_device_struct *next;
unsigned int major;
unsigned int baseminor;
int minorct;
char name[64];
struct cdev *cdev; /* will die */
} *chrdevs[CHRDEV_MAJOR_HASH_SIZE];
struct kobj_map {
struct probe {
struct probe *next;
dev_t dev;
unsigned long range;
struct module *owner;
kobj_probe_t *get;
int (*lock)(dev_t, void *);
void *data;
} *probes[255];
struct mutex *lock;
};
struct kobj_map
struct kobj_map里面strcut probe *probe[255]是一个哈希链表数组,如下:
struct kobj_map *cdev_map = kobj_map_init(…)
cdev_alloc\cdev_init -> kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev_add -> kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
cdev_del -> kobj_unmap(cdev_map, dev, count);
cdev_get -> try_module_get(struct module *);
、、、、 -> kobject_get(struct kobject *);
cdev_put -> kobject_put(struct kobject *);
、、、、 -> module_put(struct module *);
没搞明白这个计数到底有什么用处?
struct char_device_struct
如果分配了一个设备号,就会创建struct char_device_struct的对象,并将其添加到chrdevs数组中,,这样通过chrdevs数组,我们就知道分配了哪些设备号。
register_chrdev_region 分配指定的设备号 -> __register_chrdev_region
alloc_chrdev_region 动态分配设备号 -> __register_chrdev_region
以上这两个函数仅仅是注册设备号,如果要和cdev关联起来,还要调用cdev_add
register_chrdev()申请指定的设备号,并且将其注册到字符设备驱动模型中。
cdev与inode、file的关系
cdev->list下面挂了很多inode
inode->i_rdev是字符设备号
inode->i_cdev对应struct cdev,所以可以通过inode找到cdev
int chrdev_open(struct inode *inode, struct file *file)
{
struct cdev *p = inode->i_cdev;
filp->f_op = fops_get(p->ops);
filp->f_op->open(inode, file);
…
}
参考过以下两篇文章:
http://www.fx114.net/qa-35-95320.aspx
http://blog.chinaunix.net/uid-27097876-id-3352195.html