- int misc_register(struct miscdevice * misc)
- {
- struct miscdevice *c;
- dev_t dev;
- int err = 0;
- INIT_LIST_HEAD(&misc->list);
- mutex_lock(&misc_mtx); //获取misc设备信号量
- list_for_each_entry(c, &misc_list, list) { //检查设备是否已经存在
- if (c->minor == misc->minor) {
- mutex_unlock(&misc_mtx);
- return -EBUSY; //如果设备存在,直接返回
- }
- }
- if (misc->minor == MISC_DYNAMIC_MINOR) { //动态分配分配minor号
- int i = DYNAMIC_MINORS;
- while (--i >= 0)
- if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
- break;
- if (i<0) {
- mutex_unlock(&misc_mtx);
- return -EBUSY;
- }
- misc->minor = i;
- }
- if (misc->minor < DYNAMIC_MINORS)
- misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
- dev = MKDEV(MISC_MAJOR, misc->minor);
- misc->this_device = device_create(misc_class, misc->parent, dev,
- "%s", misc->name); //创建字符设备(Misc设备)
- if (IS_ERR(misc->this_device)) {
- err = PTR_ERR(misc->this_device);
- goto out;
- }
- /*
- * Add it to the front, so that later devices can "override"
- * earlier defaults
- */
- list_add(&misc->list, &misc_list); //将设备保存至misc设备链中,设备访问时需要操作该链表
- out:
- mutex_unlock(&misc_mtx);
- return err;
- }
- static int misc_open(struct inode * inode, struct file * file)
- {
- int minor = iminor(inode);
- struct miscdevice *c;
- int err = -ENODEV;
- const struct file_operations *old_fops, *new_fops = NULL;
- mutex_lock(&misc_mtx);
- list_for_each_entry(c, &misc_list, list) { //通过minor号来匹配对应的fops函数集
- if (c->minor == minor) {
- new_fops = fops_get(c->fops);
- break;
- }
- }
- if (!new_fops) {
- mutex_unlock(&misc_mtx);
- request_module("char-major-%d-%d", MISC_MAJOR, minor);
- mutex_lock(&misc_mtx);
- list_for_each_entry(c, &misc_list, list) {
- if (c->minor == minor) {
- new_fops = fops_get(c->fops);
- break;
- }
- }
- if (!new_fops)
- goto fail;
- }
- err = 0;
- old_fops = file->f_op;
- file->f_op = new_fops; //重定向fops函数
- if (file->f_op->open) { //打开设备
- err=file->f_op->open(inode,file);
- if (err) {
- fops_put(file->f_op);
- file->f_op = fops_get(old_fops);
- }
- }
- fops_put(old_fops);
- fail:
- mutex_unlock(&misc_mtx);
- return err;
- }
- int register_chrdev_region(dev_t from, unsigned count, const char *name)
- {
- struct char_device_struct *cd;
- dev_t to = from + count;
- dev_t n, next;
- for (n = from; n < to; n = next) {
- next = MKDEV(MAJOR(n)+1, 0); //一个Major设备类最多可以容纳1M个Minor设备
- if (next > to)
- next = to; //取最小设备号
- cd = __register_chrdev_region(MAJOR(n), MINOR(n),
- next - n, name); //注册设备号区间
- if (IS_ERR(cd))
- goto fail;
- }
- return 0;
- fail:
- to = n;
- for (n = from; n < to; n = next) {
- next = MKDEV(MAJOR(n)+1, 0);
- kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));
- }
- return PTR_ERR(cd);
- }