Chinaunix首页 | 论坛 | 博客
  • 博客访问: 38398
  • 博文数量: 9
  • 博客积分: 125
  • 博客等级: 入伍新兵
  • 技术积分: 83
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-07 16:17
文章分类

全部博文(9)

文章存档

2012年(9)

我的朋友

分类: LINUX

2012-05-30 11:53:25

在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. cd = kzalloc(sizeof(struct char_device_struct), GFP_KERNEL);
  20. if (cd == NULL)
  21. return ERR_PTR(-ENOMEM);
  22. mutex_lock(&chrdevs_lock);
  23. /* temporary */
  24. if (major == 0) {
  25. for (i = ARRAY_SIZE(chrdevs)-1; i > 0; i--) {
  26. if (chrdevs[i] == NULL)
  27. break;
  28. }
  29. if (i == 0) {
  30. ret = -EBUSY;
  31. goto out;
  32. }
  33. major = i;
  34. ret = major;
  35. }
  36. cd->major = major; //主设备号
  37. cd->baseminor = baseminor;//起始次设备号
  38. cd->minorct = minorct; //主设备号下有多少个次设备号
  39. strlcpy(cd->name, name, sizeof(cd->name));
  40. i = major_to_index(major);//根据major获得哈希chrdevs数组索引
  41. //chrdevs数组的一项可能对应多个主设备号
  42. for (cp = &chrdevs[i]; *cp; cp = &(*cp)->next)
  43. if ((*cp)->major > major ||
  44. ((*cp)->major == major &&
  45. (((*cp)->baseminor >= baseminor) ||
  46. ((*cp)->baseminor + (*cp)->minorct > baseminor))))
  47. break;
  48. /* Check for overlapping minor ranges. */
  49. if (*cp && (*cp)->major == major) { //防止覆盖
  50. int old_min = (*cp)->baseminor;
  51. int old_max = (*cp)->baseminor + (*cp)->minorct - 1;
  52. int new_min = baseminor;
  53. int new_max = baseminor + minorct - 1;
  54. /* New driver overlaps from the left. */
  55. if (new_max >= old_min && new_max <= old_max) {
  56. ret = -EBUSY;
  57. goto out;
  58. }
  59. /* New driver overlaps from the right. */
  60. if (new_min <= old_max && new_min >= old_min) {
  61. ret = -EBUSY;
  62. goto out;
  63. }
  64. }
  65. cd->next = *cp;
  66. *cp = cd;
  67. mutex_unlock(&chrdevs_lock);
  68. return cd;
  69. out:
  70. mutex_unlock(&chrdevs_lock);
  71. kfree(cd);
  72. return ERR_PTR(ret);
  73. }
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. unsigned n = MAJOR(dev + range - 1) - MAJOR(dev) + 1; //一般情况n=1
  13. unsigned index = MAJOR(dev);
  14. unsigned i;
  15. struct probe *p;
  16. if (n > 255)
  17. n = 255;
  18. p = kmalloc(sizeof(struct probe) * n, GFP_KERNEL);
  19. if (p == NULL)
  20. return -ENOMEM;
  21. for (i = 0; i < n; i++, p++) { //一般n=1
  22. p->owner = module;
  23. p->get = probe;
  24. p->lock = lock;
  25. p->dev = dev;
  26. p->range = range;
  27. p->data = data;//cdev
  28. }
  29. mutex_lock(domain->lock);
  30. for (i = 0, p -= n; i < n; i++, p++, index++) {
  31. struct probe **s = &domain->probes[index % 255];
  32. while (*s && (*s)->range < range)
  33. s = &(*s)->next;
  34. p->next = *s; //如果major在probes哈希表中的索引一样,则依据rang从小到大排列
  35. *s = p;
  36. }
  37. mutex_unlock(domain->lock);
  38. return 0;
  39. }


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

本文中使用2.6.30.4内核源代码。
阅读(8009) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~