在Linux2.6内核以前注册字符设备的函数接口是register_chrdev,在2.6中其可继续使用。

register_chrdev大致作用:向内核注册cdev结构体,当在用户空间打开设备文件时内核可以根据设备号快速定位此设备文件的cdev->file_operations结构体,从而调用驱动底层的open,close,read,write,ioctl等函数,当我们在用户空间open字符设备文件时,首先调用def_chr_fops->chrdev_open()函数(所有字符设备都调用此函数),chrdev_open会调用kobj_lookup函数找到设备的cdev->kobject,从而得到设备的cdev,进而获得file_operations.要想通过kobj_lookup找到相应的cdev,必需调用register_chrdev()函数。向内核注册

 

现分析如下:

/**
 * register_chrdev() - Register a major number for character devices.
 * @major: major device number or 0 for dynamic allocation
 * @name: name of this range of devices
 * @fops: file operations associated with this devices
 *
 * If @major == 0 this functions will dynamically allocate a major and return
 * its number.
 *
 * If @major > 0 this function will attempt to reserve a device with the given
 * major number and will return zero on success.
 *
 * Returns a -ve errno on failure.
 *
 * The name of this device has nothing to do with the name of the device in
 * /dev. It only helps to keep track of the different owners of devices. If
 * your module name has only one type of devices it's ok to use e.g. the name
 * of the module here.
 *
 * This function registers a range of 256 minor numbers. The first minor number
 * is 0.
 */
int register_chrdev(unsigned int major, const char *name,
      const struct file_operations *fops)
{
 struct char_device_struct *cd;
 struct cdev *cdev;
 char *s;
 int err = -ENOMEM;

/* 内核中用char_device_struct结构体指针数组chrdevs(哈希表)来维护所有的设备号

__register_chrdev_region函数向内核注册major主设备号,其有256个次设备号

在以前内核中用8位来表示次设备号,2.6内核用20位来表示次设备号*/

 cd = __register_chrdev_region(major, 0, 256, name);
 if (IS_ERR(cd))
  return PTR_ERR(cd);

 cdev = cdev_alloc();//2.6内核新接口
 if (!cdev)
  goto out2;

 cdev->owner = fops->owner;
 cdev->ops = fops;//底层驱动接口
 kobject_set_name(&cdev->kobj, "%s", name);//linux内核驱动模型kobject.
 for (s = strchr(kobject_name(&cdev->kobj),'/'); s; s = strchr(s, '/'))
  *s = '!';

 err = cdev_add(cdev, MKDEV(cd->major, 0), 256);//向内核提交cdev.
 if (err)
  goto out;

 cd->cdev = cdev;

 return major ? 0 : cd->major;
out:
 kobject_put(&cdev->kobj);
out2:
 kfree(__unregister_chrdev_region(cd->major, 0, 256));
 return err;
}

下面分析:__register_chrdev_region

 

点击(此处)折叠或打开

  1. /*
  2. * Register a single major with a specified minor range.
  3. *
  4. * If major == 0 this functions will dynamically allocate a major and return
  5. * its number.
  6. *
  7. * If major > 0 this function will attempt to reserve the passed range of
  8. * minors and will return zero on success.
  9. *
  10. * Returns a -ve errno on failure.
  11. */
  12. static struct char_device_struct *
  13. __register_chrdev_region(unsigned int major, unsigned int baseminor,
  14. int minorct, const char *name)
  15. {
  16. struct char_device_struct *cd, **cp;
  17. int ret = 0;
  18. int i;
  19.  
  20. cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  21. if (cd == NULL)
  22. return ERR_PTR(-ENOMEM);
  23.  
  24. mutex_lock(&chrdevs_lock);
  25.  
  26. /* temporary */
  27. if (major == 0) {
  28. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  29. if (chrdevs[i] == NULL)
  30. break;
  31. }
  32.  
  33. if (i == 0) {
  34. ret = -EBUSY;
  35. goto out;
  36. }
  37. major = i;
  38. ret = major;
  39. }
  40.  
  41. cd->major = major; //主设备号
  42. cd->baseminor = baseminor;//起始次设备号
  43. cd->minorct = minorct; //主设备号下有多少个次设备号
  44. strlcpy(cd->name, name, sizeof(cd->name));
  45.  
  46. i = major_to_index(major);//根据major获得哈希chrdevs数组索引
  47. //chrdevs数组的一项可能对应多个主设备号
  48. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  49. if ((*cp)->major > major ||
  50. ((*cp)->major == major &&
  51. (((*cp)->baseminor >= baseminor) ||
  52. ((*cp)->baseminor + (*cp)->minorct > baseminor))))
  53. break;
  54.  
  55. /* Check for overlapping minor ranges. */
  56. if (*cp && (*cp)->major == major) { //防止覆盖
  57. int old_min = (*cp)->baseminor;
  58. int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
  59. int new_min = baseminor;
  60. int new_max = baseminor + minorct - 1;
  61.  
  62. /* New driver overlaps from the left. */
  63. if (new_max >= old_min && new_max <= old_max) {
  64. ret = -EBUSY;
  65. goto out;
  66. }
  67.  
  68. /* New driver overlaps from the right. */
  69. if (new_min <= old_max && new_min >= old_min) {
  70. ret = -EBUSY;
  71. goto out;
  72. }
  73. }
  74.  
  75. cd->next = *cp;
  76. *cp = cd;
  77. mutex_unlock(&chrdevs_lock);
  78. return cd;
  79. out:
  80. mutex_unlock(&chrdevs_lock);
  81. kfree(cd);
  82. return ERR_PTR(ret);
  83. }

cdev_add分析:

 

点击(此处)折叠或打开

  1. int cdev_add(struct cdev *p, dev_t dev, unsigned count)
  2. {
  3. p->dev = dev; //设备号
  4. p->count = count; //次设备计数
  5. return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
  6. }
  7. int kobj_map(struct kobj_map *domain, dev_t dev, unsigned long range,
  8. struct module *module, kobj_probe_t *probe,
  9. int (*lock)(dev_t, void *), void *data)
  10. {
  11.  
  12. /*一类设备可以能占有多个主设备号,则此类设备占有连续的主设备号*/
  13. unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1; //一般情况n=1
  14. unsigned index = MAJOR(dev);
  15. unsigned i;
  16. struct probe *p;
  17.  
  18. if (n > 255)
  19. n = 255;
  20.  
  21. p = kmalloc(sizeof(struct probe) * n, GFP_KERNEL);
  22.  
  23. if (p == NULL)
  24. return -ENOMEM;
  25.  
  26. for (i = 0; i < n; i++, p++) { //一般n=1
  27. p->owner = module;
  28. p->get = probe;
  29. p->lock = lock;
  30. p->dev = dev;
  31. p->range = range;
  32. p->data = data;//cdev
  33. }
  34. mutex_lock(domain->lock);
  35. for (i = 0, p -= n; i < n; i++, p++, index++) {
  36. struct probe **s = &domain->probes[index % 255];
  37. while (*s && (*s)->range < range)
  38. s = &(*s)->next;
  39. p->next = *s; //如果major在probes哈希表中的索引一样,则依据rang从小到大排列
  40. *s = p;
  41. }
  42. mutex_unlock(domain->lock);
  43. return 0;
  44. }



在完成register_chrdev的注册以后就可以对设备文件操作,在chrdev_open函数中通过调用kobj_lookup函数找到cdev内嵌的kobject结构体以后就可以得到cdev结构体本身。

点击(此处)折叠或打开

  1. struct kobject *kobj_lookup(struct kobj_map *domain, dev_t dev, int *index)
  2. {
  3. struct kobject *kobj;
  4. struct probe *p;
  5. unsigned long best = ~0UL;
  6.  
  7. retry:
  8. mutex_lock(domain->lock);
  9. for (p = domain->probes[MAJOR(dev) % 255]; p; p = p->next) {
  10. struct kobject *(*probe)(dev_t, int *, void *);
  11. struct module *owner;
  12. void *data;
  13.  
  14. if (p->dev > dev || p->dev + p->range - 1 < dev)
  15. continue;
  16. if (p->range - 1 >= best)
  17. break;
  18. if (!try_module_get(p->owner))
  19. continue;
  20. owner = p->owner;
  21. data = p->data;
  22. probe = p->get;
  23. best = p->range - 1;
  24. *index = dev - p->dev;
  25. if (p->lock && p->lock(dev, data) < 0) {
  26. module_put(owner);
  27. continue;
  28. }
  29. mutex_unlock(domain->lock);
  30. kobj = probe(dev, index, data);
  31. /* Currently ->owner protects _only_ ->probe() itself. */
  32. module_put(owner);
  33. if (kobj)
  34. return kobj;
  35. goto retry;
  36. }
  37. mutex_unlock(domain->lock);
  38. return NULL;
  39. }


本文中使用2.6.30.4内核源代码。

GitHub 加速计划 / li / linux-dash
10.39 K
1.2 K
下载
A beautiful web dashboard for Linux
最近提交(Master分支:1 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐