linux驱动之cdev
代码在fs/char_dev.c
linux下每一个具体的字符设备都会对应一个cdev,cdev->ops就是设备的操作接口。
上层应用程序调用open()系统调用打开/dev/xxx设备节点时,最终会调用cdev->ops->open()。
struct cdev {
struct kobject kobj;
struct module *owner;
const struct file_operations *ops;
struct list_head list;
dev_t dev;
unsigned int count;
};
//驱动操作接口
struct file_operations {
struct module *owner;
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
int (*open) (struct inode *, struct file *);
...
};
初始化cdev,需自行为cdev分配内存,使用kmalloc()
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
memset(cdev, 0, sizeof *cdev);
INIT_LIST_HEAD(&cdev->list);
//kobject用于嵌入一些数据结构中方便linux管理
kobject_init(&cdev->kobj, &ktype_cdev_default);
cdev->ops = fops;
}
注册cdev
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
int error;
//记录设备号,次设备号个数
p->dev = dev;
p->count = count;
error = kobj_map(cdev_map, dev, count, NULL,
exact_match, exact_lock, p);
if (error)
return error;
kobject_get(p->kobj.parent);
return 0;
}
用哈希表保存cdev结构体
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;
};
static struct kobj_map *cdev_map;
int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
struct module *module, kobj_probe_t *probe,
int (*lock)(dev_t, void *), void *data)
{
//占用主设备号个数,一般是n = 1
unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1;
unsigned index = MAJOR(dev); //获取次设备号
unsigned i;
struct probe *p;
if (n > 255)
n = 255;
//申请内存, n * sizeof(struct probe)
p = kmalloc_array(n, sizeof(struct probe), GFP_KERNEL);
if (p == NULL)
return -ENOMEM;
//n == 1
for (i = 0; i < n; i++, p++) {
p->owner = module; // == NULL
//获取cdev->kobj
p->get = probe; // == exact_match
//用于给cdev->kobj增加引用计数,当引用计数为0,可以删除
p->lock = lock; // == exact_lock
//记录设备号,以后可以通过设备号找到cdev结构体
p->dev = dev;
p->range = range;
//记录cdev
p->data = data; // == cdev
}
mutex_lock(domain->lock);
//在哈希表domain->probes[]中,保存p,有哈希冲突就挂在链表上
for (i = 0, p -= n; i < n; i++, p++, index++) {
struct probe **s = &domain->probes[index % 255];
while (*s && (*s)->range < range)
s = &(*s)->next;
p->next = *s;
*s = p;
}
mutex_unlock(domain->lock);
return 0;
}
static struct kobject *exact_match(dev_t dev, int *part, void *data)
{
struct cdev *p = data;
return &p->kobj;
}
static int exact_lock(dev_t dev, void *data)
{
struct cdev *p = data;
return cdev_get(p) ? 0 : -1;
}
cdev_map是指针在系统启动时就初始化完成
void __init chrdev_init(void)
{
cdev_map = kobj_map_init(base_probe, &chrdevs_lock);
}
struct kobj_map *kobj_map_init(kobj_probe_t *base_probe, struct mutex *lock)
{
struct kobj_map *p = kmalloc(sizeof(struct kobj_map), GFP_KERNEL);
struct probe *base = kzalloc(sizeof(*base), GFP_KERNEL);
int i;
...
base->dev = 1;
base->range = ~0;
base->get = base_probe;
//用base填充p->probes[],初始化
for (i = 0; i < 255; i++)
p->probes[i] = base;
//互斥所
p->lock = lock;
return p;
}