Linux 中字符设备的注册

 

Linux中字符设备的注册过程是比较简单的。咱们一般能够调用misc_register()函数来注册一个字符设备。Misc设备是一种字符设备,经过该设备能够将fops请求转发给注册的misc设备,从而实现字符设备的功能。用户调用该接口注册Misc字符设备时,能够动态分配设备Minor号,当获取Minor号以后调用class_simple_device_add()或者device_create()函数完成字符设备的建立。Misc字符设备注册函数以下所示:

 

  
  
           
  
  
  1. int misc_register(struct miscdevice * misc)  
  2. {  
  3.     struct miscdevice *c;  
  4.     dev_t dev;  
  5.     int err = 0;  
  6.  
  7.     INIT_LIST_HEAD(&misc->list);  
  8.  
  9.     mutex_lock(&misc_mtx);      //获取misc设备信号量  
  10.     list_for_each_entry(c, &misc_list, list) {  //检查设备是否已经存在  
  11.         if (c->minor == misc->minor) {  
  12.             mutex_unlock(&misc_mtx);  
  13.             return -EBUSY;          //若是设备存在,直接返回  
  14.         }  
  15.     }  
  16.  
  17.     if (misc->minor == MISC_DYNAMIC_MINOR) {    //动态分配分配minor号  
  18.         int i = DYNAMIC_MINORS;  
  19.         while (--i >= 0)  
  20.             if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)  
  21.                 break;  
  22.         if (i<0) {  
  23.             mutex_unlock(&misc_mtx);  
  24.             return -EBUSY;  
  25.         }  
  26.         misc->minor = i;  
  27.     }  
  28.  
  29.     if (misc->minor < DYNAMIC_MINORS)  
  30.         misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);  
  31.     dev = MKDEV(MISC_MAJOR, misc->minor);  
  32.  
  33.     misc->this_device = device_create(misc_class, misc->parent, dev,  
  34.                       "%s", misc->name);        //建立字符设备(Misc设备)  
  35.     if (IS_ERR(misc->this_device)) {  
  36.         err = PTR_ERR(misc->this_device);  
  37.         goto out;  
  38.     }  
  39.  
  40.     /*  
  41.      * Add it to the front, so that later devices can "override"  
  42.      * earlier defaults  
  43.      */  
  44.     list_add(&misc->list, &misc_list);  //将设备保存至misc设备链中,设备访问时须要操做该链表  
  45.  out:  
  46.     mutex_unlock(&misc_mtx);  
  47.     return err;  
须要注意的是2.6.12内核中建立系统设备时须要调用simple_device的接口class_simple_device_add()。在2.6.23中须要调用device_create()函数完成设备注册。在3.2内核中,simple_device的接口已经不存在了,因此必须调用device_create函数,另外,3.2内核不支持具备相同minor号的字符设备,在2.6.x内核中是支持的。
 
系统是如何完成fops函数调用的呢?回答这个问题须要分析Misc设备打开过程。在打开Misc设备驱动的时候,Misc设备驱动会根据访问设备的Minor号重定向fops函数集,该程序说明以下:

 

  
  
           
  
  
  1. static int misc_open(struct inode * inode, struct file * file)  
  2. {  
  3.     int minor = iminor(inode);  
  4.     struct miscdevice *c;  
  5.     int err = -ENODEV;  
  6.     const struct file_operations *old_fops, *new_fops = NULL;  
  7.  
  8.     mutex_lock(&misc_mtx);  
  9.  
  10.     list_for_each_entry(c, &misc_list, list) {  //经过minor号来匹配对应的fops函数集  
  11.         if (c->minor == minor) {  
  12.             new_fops = fops_get(c->fops);  
  13.             break;  
  14.         }  
  15.     }  
  16.  
  17.     if (!new_fops) {  
  18.         mutex_unlock(&misc_mtx);  
  19.         request_module("char-major-%d-%d", MISC_MAJOR, minor);  
  20.         mutex_lock(&misc_mtx);  
  21.  
  22.         list_for_each_entry(c, &misc_list, list) {  
  23.             if (c->minor == minor) {  
  24.                 new_fops = fops_get(c->fops);  
  25.                 break;  
  26.             }  
  27.         }  
  28.         if (!new_fops)  
  29.             goto fail;  
  30.     }  
  31.  
  32.     err = 0;  
  33.     old_fops = file->f_op;  
  34.     file->f_op = new_fops;  //重定向fops函数  
  35.     if (file->f_op->open) {     //打开设备  
  36.         err=file->f_op->open(inode,file);  
  37.         if (err) {  
  38.             fops_put(file->f_op);  
  39.             file->f_op = fops_get(old_fops);  
  40.         }  
  41.     }  
  42.     fops_put(old_fops);  
  43. fail:  
  44.     mutex_unlock(&misc_mtx);  
  45.     return err;  
不少时候咱们不想建立Misc字符设备,想要本身建立一个字符设备类,而后再建立该设备类的字符设备,那么整个建立过程能够描述以下:
1,调用register_chrdev_region函数注册设备号区间
2,调用cdev_alloc函数分配一个字符设备
3,调用cdev_add函数添加内核字符设备
4,调用device_create函数通知udev建立设备节点,而且注册到sysfs中。
 
register_chrdev_region函数用来分配设备号。在linux系统中维护了一个char_device的Hash表,每一个Major占用一个Hash项。经过register_chrdev_region函数就是向全局的char_device Hash表注册设备号。该函数说明以下:

 

  
  
           
  
  
  1. int register_chrdev_region(dev_t from, unsigned count, const char *name)  
  2. {  
  3.     struct char_device_struct *cd;  
  4.     dev_t to = from + count;  
  5.     dev_t n, next;  
  6.  
  7.     for (n = from; n < ton = next) {  
  8.         next = MKDEV(MAJOR(n)+1, 0);    //一个Major设备类最多能够容纳1M个Minor设备  
  9.         if (next > to)  
  10.             next = to;  //取最小设备号  
  11.         cd = __register_chrdev_region(MAJOR(n), MINOR(n),  
  12.                    next - n, name); //注册设备号区间  
  13.         if (IS_ERR(cd))  
  14.             goto fail;  
  15.     }  
  16.     return 0;  
  17. fail:  
  18.     to = n;  
  19.     for (n = from; n < ton = next) {  
  20.         next = MKDEV(MAJOR(n)+1, 0);  
  21.         kfree(__unregister_chrdev_region(MAJOR(n), MINOR(n), next - n));  
  22.     }  
  23.     return PTR_ERR(cd);  
当设备号区间分配完成以后,经过cdev_alloc()函数分配一个内核字符设备,而后经过cdev_add函数将该字符设备注册到内核cdev_map->probes[]数组中,至此,内核的字符设备建立完毕。可是,此时,应用层还没法看到字符设备节点,所以,能够调用device_create函数通知udev去建立设备节点,而且将设备添加到sysfs系统树中。至此,应用层能够经过设备节点访问字符设备了。
 
字符设备是Linux中最简单的一种设备,其系统注册过程也相对比较简单。
相关文章
相关标签/搜索