linux的设备驱动模型,是建立在sysfs和kobject之上的,由总线、设备、驱动、类所组成的关系结构。从本节开始,我们将对linux这一设备驱动模型进行深入分析。

头文件是include/linux/device.h,实现在drivers/base目录中。本节要分析的,是其中的设备,主要在core.c中。

  1. struct device {
  2. struct device       *parent;
  3. struct device_private   *p;
  4. struct kobject kobj;
  5. const char      *init_name; /* initial name of the device */
  6. struct device_type  *type;
  7. struct semaphore    sem;    /* semaphore to synchronize calls to
  8. * its driver.
  9. */
  10. struct bus_type *bus;       /* type of bus device is on */
  11. struct device_driver *driver;   /* which driver has allocated this
  12. device */
  13. void        *platform_data; /* Platform specific data, device
  14. core doesn't touch it */
  15. struct dev_pm_info  power;
  16. #ifdef CONFIG_NUMA
  17. int     numa_node;  /* NUMA node this device is close to */
  18. #endif
  19. u64     *dma_mask;  /* dma mask (if dma'able device) */
  20. u64     coherent_dma_mask;/* Like dma_mask, but for
  21. alloc_coherent mappings as
  22. not all hardware supports
  23. 64 bit addresses for consistent
  24. allocations such descriptors. */
  25. struct device_dma_parameters *dma_parms;
  26. struct list_head    dma_pools;  /* dma pools (if dma'ble) */
  27. struct dma_coherent_mem *dma_mem; /* internal for coherent mem
  28. override */
  29. /* arch specific additions */
  30. struct dev_archdata archdata;
  31. dev_t           devt;   /* dev_t, creates the sysfs "dev" */
  32. spinlock_t      devres_lock;
  33. struct list_head    devres_head;
  34. struct klist_node   knode_class;
  35. struct class        *class;
  36. const struct attribute_group **groups;  /* optional groups */
  37. void    (*release)(struct device *dev);
  38. };

先来分析下struct device的结构变量。首先是指向父节点的指针parent,kobj是内嵌在device中的kobject,用于把它联系到sysfs中。bus是对设备所在总线的指针,driver是对设备所用驱动的指针。还有DMA需要的数据,表示设备号的devt,表示设备资源的devres_head和保护��的devres_lock。指向类的指针class,knode_class是被连入class链表时所用的klist节点。group是设备的属性集合。release应该是设备释放时调用的函数。

  1. struct device_private {
  2. struct klist klist_children;
  3. struct klist_node knode_parent;
  4. struct klist_node knode_driver;
  5. struct klist_node knode_bus;
  6. void *driver_data;
  7. struct device *device;
  8. };
  9. #define to_device_private_parent(obj)   \
  10. container_of(obj, struct device_private, knode_parent)
  11. #define to_device_private_driver(obj)   \
  12. container_of(obj, struct device_private, knode_driver)
  13. #define to_device_private_bus(obj)  \
  14. container_of(obj, struct device_private, knode_bus)

struct device中有一部分不愿意让外界看到,所以做出struct device_private结构,包括了设备驱动模型内部的链接。klist_children是子设备的链表,knode_parent是连入父设备的klist_children时所用的节点,knode_driver是连入驱动的设备链表所用的节点,knode_bus是连入总线的设备链表时所用的节点。driver_data用于在设备结构中存放相关的驱动信息,也许是驱动专门为设备建立的结构实例。device则是指向struct device_private所属的device。

下面还有一些宏,to_device_private_parent()是从父设备的klist_children上节点,获得相应的device_private。to_device_private_driver()是从驱动的设备链表上节点,获得对应的device_private。to_device_private_bus()是从总线的设备链表上节点,获得对应的device_private。

或许会奇怪,为什么knode_class没有被移入struct device_private,或许有外部模块需要用到它。

  1. /*
  2. * The type of device, "struct device" is embedded in. A class
  3. * or bus can contain devices of different types
  4. * like "partitions" and "disks", "mouse" and "event".
  5. * This identifies the device type and carries type-specific
  6. * information, equivalent to the kobj_type of a kobject.
  7. * If "name" is specified, the uevent will contain it in
  8. * the DEVTYPE variable.
  9. */
  10. struct device_type {
  11. const char *name;
  12. const struct attribute_group **groups;
  13. int (*uevent)(struct device *dev, struct kobj_uevent_env *env);
  14. char *(*devnode)(struct device *dev, mode_t *mode);
  15. void (*release)(struct device *dev);
  16. const struct dev_pm_ops *pm;
  17. };

device竟然有device_type,类似于与kobject相对的kobj_type,之后我们再看它怎么用。

  1. /* interface for exporting device attributes */
  2. struct device_attribute {
  3. struct attribute    attr;
  4. ssize_t (*show)(struct device *dev, struct device_attribute *attr,
  5. char *buf);
  6. ssize_t (*store)(struct device *dev, struct device_attribute *attr,
  7. const char *buf, size_t count);
  8. };
  9. #define DEVICE_ATTR(_name, _mode, _show, _store) \
  10. struct device_attribute dev_attr_##_name = __ATTR(_name, _mode, _show, _store)

这个device_attribute显然就是device对struct attribute的封装,新加的show()、store()函数都是以与设备相关的结构调用的。

至于device中其它的archdata、dma、devres,都是作为设备特有的,我们现在主要关心设备驱动模型的建立,这些会尽量忽略。

下面就来看看device的实现,这主要在core.c中。

  1. int __init devices_init(void)
  2. {
  3. devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
  4. if (!devices_kset)
  5. return -ENOMEM;
  6. dev_kobj = kobject_create_and_add("dev", NULL);
  7. if (!dev_kobj)
  8. goto dev_kobj_err;
  9. sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
  10. if (!sysfs_dev_block_kobj)
  11. goto block_kobj_err;
  12. sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
  13. if (!sysfs_dev_char_kobj)
  14. goto char_kobj_err;
  15. return 0;
  16. char_kobj_err:
  17. kobject_put(sysfs_dev_block_kobj);
  18. block_kobj_err:
  19. kobject_put(dev_kobj);
  20. dev_kobj_err:
  21. kset_unregister(devices_kset);
  22. return -ENOMEM;
  23. }

这是在设备驱动模型初始化时调用的device部分初始的函数devices_init()。它干的事情我们都很熟悉,就是建立sysfs中的devices目录,和dev目录。还在dev目录下又建立了block和char两个子目录。因为dev目录只打算存放辅助的设备号,所以没必要使用kset。

  1. static ssize_t dev_attr_show(struct kobject *kobj, struct attribute *attr,
  2. char *buf)
  3. {
  4. struct device_attribute *dev_attr = to_dev_attr(attr);
  5. struct device *dev = to_dev(kobj);
  6. ssize_t ret = -EIO;
  7. if (dev_attr->show)
  8. ret = dev_attr->show(dev, dev_attr, buf);
  9. if (ret >= (ssize_t)PAGE_SIZE) {
  10. print_symbol("dev_attr_show: %s returned bad count\n",
  11. (unsigned long)dev_attr->show);
  12. }
  13. return ret;
  14. }
  15. static ssize_t dev_attr_store(struct kobject *kobj, struct attribute *attr,
  16. const char *buf, size_t count)
  17. {
  18. struct device_attribute *dev_attr = to_dev_attr(attr);
  19. struct device *dev = to_dev(kobj);
  20. ssize_t ret = -EIO;
  21. if (dev_attr->store)
  22. ret = dev_attr->store(dev, dev_attr, buf, count);
  23. return ret;
  24. }
  25. static struct sysfs_ops dev_sysfs_ops = {
  26. .show   = dev_attr_show,
  27. .store  = dev_attr_store,
  28. };

看到这里是不是很熟悉,dev_sysfs_ops就是device准备注册到sysfs中的操作函数。dev_attr_show()和dev_attr_store()都会再调用与属性相关的函数。

  1. static void device_release(struct kobject *kobj)
  2. {
  3. struct device *dev = to_dev(kobj);
  4. struct device_private *p = dev->p;
  5. if (dev->release)
  6. dev->release(dev);
  7. else if (dev->type && dev->type->release)
  8. dev->type->release(dev);
  9. else if (dev->class && dev->class->dev_release)
  10. dev->class->dev_release(dev);
  11. else
  12. WARN(1, KERN_ERR "Device '%s' does not have a release() "
  13. "function, it is broken and must be fixed.\n",
  14. dev_name(dev));
  15. kfree(p);
  16. }
  17. static struct kobj_type device_ktype = {
  18. .release    = device_release,
  19. .sysfs_ops  = &dev_sysfs_ops,
  20. };

使用的release函数是device_release。在释放device时,会依次调用device结构中定义的release函数,device_type中定义的release函数,device所属的class中所定义的release函数,最后会吧device_private结构释放掉。

  1. static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
  2. {
  3. struct kobj_type *ktype = get_ktype(kobj);
  4. if (ktype == &device_ktype) {
  5. struct device *dev = to_dev(kobj);
  6. if (dev->bus)
  7. return 1;
  8. if (dev->class)
  9. return 1;
  10. }
  11. return 0;
  12. }
  13. static const char *dev_uevent_name(struct kset *kset, struct kobject *kobj)
  14. {
  15. struct device *dev = to_dev(kobj);
  16. if (dev->bus)
  17. return dev->bus->name;
  18. if (dev->class)
  19. return dev->class->name;
  20. return NULL;
  21. }
  22. static int dev_uevent(struct kset *kset, struct kobject *kobj,
  23. struct kobj_uevent_env *env)
  24. {
  25. struct device *dev = to_dev(kobj);
  26. int retval = 0;
  27. /* add device node properties if present */
  28. if (MAJOR(dev->devt)) {
  29. const char *tmp;
  30. const char *name;
  31. mode_t mode = 0;
  32. add_uevent_var(env, "MAJOR=%u", MAJOR(dev->devt));
  33. add_uevent_var(env, "MINOR=%u", MINOR(dev->devt));
  34. name = device_get_devnode(dev, &mode, &tmp);
  35. if (name) {
  36. add_uevent_var(env, "DEVNAME=%s", name);
  37. kfree(tmp);
  38. if (mode)
  39. add_uevent_var(env, "DEVMODE=%#o", mode & 0777);
  40. }
  41. }
  42. if (dev->type && dev->type->name)
  43. add_uevent_var(env, "DEVTYPE=%s", dev->type->name);
  44. if (dev->driver)
  45. add_uevent_var(env, "DRIVER=%s", dev->driver->name);
  46. #ifdef CONFIG_SYSFS_DEPRECATED
  47. if (dev->class) {
  48. struct device *parent = dev->parent;
  49. /* find first bus device in parent chain */
  50. while (parent && !parent->bus)
  51. parent = parent->parent;
  52. if (parent && parent->bus) {
  53. const char *path;
  54. path = kobject_get_path(&parent->kobj, GFP_KERNEL);
  55. if (path) {
  56. add_uevent_var(env, "PHYSDEVPATH=%s", path);
  57. kfree(path);
  58. }
  59. add_uevent_var(env, "PHYSDEVBUS=%s", parent->bus->name);
  60. if (parent->driver)
  61. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  62. parent->driver->name);
  63. }
  64. else if (dev->bus) {
  65. add_uevent_var(env, "PHYSDEVBUS=%s", dev->bus->name);
  66. if (dev->driver)
  67. add_uevent_var(env, "PHYSDEVDRIVER=%s",
  68. dev->driver->name);
  69. }
  70. #endif
  71. /* have the bus specific function add its stuff */
  72. if (dev->bus && dev->bus->uevent) {
  73. retval = dev->bus->uevent(dev, env);
  74. if (retval)
  75. pr_debug("device: '%s': %s: bus uevent() returned %d\n",
  76. dev_name(dev), __func__, retval);
  77. }
  78. /* have the class specific function add its stuff */
  79. if (dev->class && dev->class->dev_uevent) {
  80. retval = dev->class->dev_uevent(dev, env);
  81. if (retval)
  82. pr_debug("device: '%s': %s: class uevent() "
  83. "returned %d\n", dev_name(dev),
  84. __func__, retval);
  85. }
  86. /* have the device type specific fuction add its stuff */
  87. if (dev->type && dev->type->uevent) {
  88. retval = dev->type->uevent(dev, env);
  89. if (retval)
  90. pr_debug("device: '%s': %s: dev_type uevent() "
  91. "returned %d\n", dev_name(dev),
  92. __func__, retval);
  93. }
  94. return retval;
  95. }
  96. static struct kset_uevent_ops device_uevent_ops = {
  97. .filter =   dev_uevent_filter,
  98. .name =     dev_uevent_name,
  99. .uevent =   dev_uevent,
  100. };

前面在讲到kset时,我们并未关注其中的kset_event_ops结构变量。但这里device既然用到了,我们就对其中的三个函数做简单介绍。kset_uevent_ops中的函数是用于管理kset内部kobject的uevent操作。其中filter函数用于阻止一个kobject向用户空间发送uevent,返回值为0表示阻止。这里dev_uevent_filter()检查device所属的bus或者class是否存在,如果都不存在,也就没有发送uevent的必要了。name函数是用于覆盖kset发送给用户空间的名称。这里dev_uevent_name()选择使用bus或者class的名称。uevent()函数是在uevent将被发送到用户空间之前调用的,用于向uevent中增加新的环境变量。dev_uevent()的实现很热闹,向uevent中添加了各种环境变量。

  1. static ssize_t show_uevent(struct device *dev, struct device_attribute *attr,
  2. char *buf)
  3. {
  4. struct kobject *top_kobj;
  5. struct kset *kset;
  6. struct kobj_uevent_env *env = NULL;
  7. int i;
  8. size_t count = 0;
  9. int retval;
  10. /* search the kset, the device belongs to */
  11. top_kobj = &dev->kobj;
  12. while (!top_kobj->kset && top_kobj->parent)
  13. top_kobj = top_kobj->parent;
  14. if (!top_kobj->kset)
  15. goto out;
  16. kset = top_kobj->kset;
  17. if (!kset->uevent_ops || !kset->uevent_ops->uevent)
  18. goto out;
  19. /* respect filter */
  20. if (kset->uevent_ops && kset->uevent_ops->filter)
  21. if (!kset->uevent_ops->filter(kset, &dev->kobj))
  22. goto out;
  23. env = kzalloc(sizeof(struct kobj_uevent_env), GFP_KERNEL);
  24. if (!env)
  25. return -ENOMEM;
  26. /* let the kset specific function add its keys */
  27. retval = kset->uevent_ops->uevent(kset, &dev->kobj, env);
  28. if (retval)
  29. goto out;
  30. /* copy keys to file */
  31. for (i = 0; i < env->envp_idx; i++)
  32. count += sprintf(&buf[count], "%s\n", env->envp[i]);
  33. out:
  34. kfree(env);
  35. return count;
  36. }
  37. static ssize_t store_uevent(struct device *dev, struct device_attribute *attr,
  38. const char *buf, size_t count)
  39. {
  40. enum kobject_action action;
  41. if (kobject_action_type(buf, count, &action) == 0) {
  42. kobject_uevent(&dev->kobj, action);
  43. goto out;
  44. }
  45. dev_err(dev, "uevent: unsupported action-string; this will "
  46. "be ignored in a future kernel version\n");
  47. kobject_uevent(&dev->kobj, KOBJ_ADD);
  48. out:
  49. return count;
  50. }
  51. static struct device_attribute uevent_attr =
  52. __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);

device不仅在kset中添加了对uevent的管理,而且还把uevent信息做成设备的一个属性uevent。其中show_event()是显示uevent中环境变量的,store_uevent()是发送uevent的。

  1. static int device_add_attributes(struct device *dev,
  2. struct device_attribute *attrs)
  3. {
  4. int error = 0;
  5. int i;
  6. if (attrs) {
  7. for (i = 0; attr_name(attrs[i]); i++) {
  8. error = device_create_file(dev, &attrs[i]);
  9. if (error)
  10. break;
  11. }
  12. if (error)
  13. while (--i >= 0)
  14. device_remove_file(dev, &attrs[i]);
  15. }
  16. return error;
  17. }
  18. static void device_remove_attributes(struct device *dev,
  19. struct device_attribute *attrs)
  20. {
  21. int i;
  22. if (attrs)
  23. for (i = 0; attr_name(attrs[i]); i++)
  24. device_remove_file(dev, &attrs[i]);
  25. }
  26. static int device_add_groups(struct device *dev,
  27. const struct attribute_group **groups)
  28. {
  29. int error = 0;
  30. int i;
  31. if (groups) {
  32. for (i = 0; groups[i]; i++) {
  33. error = sysfs_create_group(&dev->kobj, groups[i]);
  34. if (error) {
  35. while (--i >= 0)
  36. sysfs_remove_group(&dev->kobj,
  37. groups[i]);
  38. break;
  39. }
  40. }
  41. }
  42. return error;
  43. }
  44. static void device_remove_groups(struct device *dev,
  45. const struct attribute_group **groups)
  46. {
  47. int i;
  48. if (groups)
  49. for (i = 0; groups[i]; i++)
  50. sysfs_remove_group(&dev->kobj, groups[i]);
  51. }

以上四个内部函数是用来向device中添加或删除属性与属性集合的。

device_add_attributes、device_remove_attributes、device_add_groups、device_remove_groups,都是直接通过sysfs提供的API实现。

  1. static int device_add_attrs(struct device *dev)
  2. {
  3. struct class *class = dev->class;
  4. struct device_type *type = dev->type;
  5. int error;
  6. if (class) {
  7. error = device_add_attributes(dev, class->dev_attrs);
  8. if (error)
  9. return error;
  10. }
  11. if (type) {
  12. error = device_add_groups(dev, type->groups);
  13. if (error)
  14. goto err_remove_class_attrs;
  15. }
  16. error = device_add_groups(dev, dev->groups);
  17. if (error)
  18. goto err_remove_type_groups;
  19. return 0;
  20. err_remove_type_groups:
  21. if (type)
  22. device_remove_groups(dev, type->groups);
  23. err_remove_class_attrs:
  24. if (class)
  25. device_remove_attributes(dev, class->dev_attrs);
  26. return error;
  27. }
  28. static void device_remove_attrs(struct device *dev)
  29. {
  30. struct class *class = dev->class;
  31. struct device_type *type = dev->type;
  32. device_remove_groups(dev, dev->groups);
  33. if (type)
  34. device_remove_groups(dev, type->groups);
  35. if (class)
  36. device_remove_attributes(dev, class->dev_attrs);
  37. }

device_add_attrs()实际负责device中的属性添加。也是几个部分的集合,包括class中的dev_attrs,device_type中的groups,还有device本身的groups。

device_remove_attrs()则负责对应的device属性删除工作。

  1. #define print_dev_t(buffer, dev)                    \
  2. sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
  3. static ssize_t show_dev(struct device *dev, struct device_attribute *attr,
  4. char *buf)
  5. {
  6. return print_dev_t(buf, dev->devt);
  7. }
  8. static struct device_attribute devt_attr =
  9. __ATTR(dev, S_IRUGO, show_dev, NULL);

这里又定义了一个名为dev的属性,就是显示设备的设备号。

  1. /**
  2. * device_create_file - create sysfs attribute file for device.
  3. * @dev: device.
  4. * @attr: device attribute descriptor.
  5. */
  6. int device_create_file(struct device *dev, struct device_attribute *attr)
  7. {
  8. int error = 0;
  9. if (dev)
  10. error = sysfs_create_file(&dev->kobj, &attr->attr);
  11. return error;
  12. }
  13. /**
  14. * device_remove_file - remove sysfs attribute file.
  15. * @dev: device.
  16. * @attr: device attribute descriptor.
  17. */
  18. void device_remove_file(struct device *dev, struct device_attribute *attr)
  19. {
  20. if (dev)
  21. sysfs_remove_file(&dev->kobj, &attr->attr);
  22. }
  23. /**
  24. * device_create_bin_file - create sysfs binary attribute file for device.
  25. * @dev: device.
  26. * @attr: device binary attribute descriptor.
  27. */
  28. int device_create_bin_file(struct device *dev, struct bin_attribute *attr)
  29. {
  30. int error = -EINVAL;
  31. if (dev)
  32. error = sysfs_create_bin_file(&dev->kobj, attr);
  33. return error;
  34. }
  35. /**
  36. * device_remove_bin_file - remove sysfs binary attribute file
  37. * @dev: device.
  38. * @attr: device binary attribute descriptor.
  39. */
  40. void device_remove_bin_file(struct device *dev, struct bin_attribute *attr)
  41. {
  42. if (dev)
  43. sysfs_remove_bin_file(&dev->kobj, attr);
  44. }
  45. int device_schedule_callback_owner(struct device *dev,
  46. void (*func)(struct device *), struct module *owner)
  47. {
  48. return sysfs_schedule_callback(&dev->kobj,
  49. (void (*)(void *)) func, dev, owner);
  50. }

这里的五个函数,也是对sysfs提供的API的简单封装。

device_create_file()和device_remove_file()提供直接的属性文件管理方法。

device_create_bin_file()和device_remove_bin_file()则是提供设备管理二进制文件的方法。

device_schedule_callback_owner()也是简单地将func加入工作队列。

  1. static void klist_children_get(struct klist_node *n)
  2. {
  3. struct device_private *p = to_device_private_parent(n);
  4. struct device *dev = p->device;
  5. get_device(dev);
  6. }
  7. static void klist_children_put(struct klist_node *n)
  8. {
  9. struct device_private *p = to_device_private_parent(n);
  10. struct device *dev = p->device;
  11. put_device(dev);
  12. }

如果之前认真看过klist的实现,应该知道,klist_children_get()和klist_children_put()就是在设备挂入和删除父设备的klist_children链表时调用的函数。在父设备klist_children链表上的指针,相当于对device的一个引用计数。

  1. struct device *get_device(struct device *dev)
  2. {
  3. return dev ? to_dev(kobject_get(&dev->kobj)) : NULL;
  4. }
  5. /**
  6. * put_device - decrement reference count.
  7. * @dev: device in question.
  8. */
  9. void put_device(struct device *dev)
  10. {
  11. /* might_sleep(); */
  12. if (dev)
  13. kobject_put(&dev->kobj);
  14. }

device中的引用计数,完全交给内嵌的kobject来做。如果引用计数降为零,自然是调用之前说到的包含甚广的device_release函数。

  1. void device_initialize(struct device *dev)
  2. {
  3. dev->kobj.kset = devices_kset;
  4. kobject_init(&dev->kobj, &device_ktype);
  5. INIT_LIST_HEAD(&dev->dma_pools);
  6. init_MUTEX(&dev->sem);
  7. spin_lock_init(&dev->devres_lock);
  8. INIT_LIST_HEAD(&dev->devres_head);
  9. device_init_wakeup(dev, 0);
  10. device_pm_init(dev);
  11. set_dev_node(dev, -1);
  12. }

device_initialize()就是device结构的初始化函数,它把device中能初始化的部分全初始化。它的界限在其中kobj的位置与device在设备驱动模型中的位置,这些必须由外部设置。可以看到,调用kobject_init()时,object的kobj_type选择了device_ktype,其中主要是sysops的两个函数,还有device_release函数。

  1. static struct kobject *virtual_device_parent(struct device *dev)
  2. {
  3. static struct kobject *virtual_dir = NULL;
  4. if (!virtual_dir)
  5. virtual_dir = kobject_create_and_add("virtual",
  6. &devices_kset->kobj);
  7. return virtual_dir;
  8. }
  9. static struct kobject *get_device_parent(struct device *dev,
  10. struct device *parent)
  11. {
  12. int retval;
  13. if (dev->class) {
  14. struct kobject *kobj = NULL;
  15. struct kobject *parent_kobj;
  16. struct kobject *k;
  17. /*
  18. * If we have no parent, we live in "virtual".
  19. * Class-devices with a non class-device as parent, live
  20. * in a "glue" directory to prevent namespace collisions.
  21. */
  22. if (parent == NULL)
  23. parent_kobj = virtual_device_parent(dev);
  24. else if (parent->class)
  25. return &parent->kobj;
  26. else
  27. parent_kobj = &parent->kobj;
  28. /* find our class-directory at the parent and reference it */
  29. spin_lock(&dev->class->p->class_dirs.list_lock);
  30. list_for_each_entry(k, &dev->class->p->class_dirs.list, entry)
  31. if (k->parent == parent_kobj) {
  32. kobj = kobject_get(k);
  33. break;
  34. }
  35. spin_unlock(&dev->class->p->class_dirs.list_lock);
  36. if (kobj)
  37. return kobj;
  38. /* or create a new class-directory at the parent device */
  39. k = kobject_create();
  40. if (!k)
  41. return NULL;
  42. k->kset = &dev->class->p->class_dirs;
  43. retval = kobject_add(k, parent_kobj, "%s", dev->class->name);
  44. if (retval < 0) {
  45. kobject_put(k);
  46. return NULL;
  47. }
  48. /* do not emit an uevent for this simple "glue" directory */
  49. return k;
  50. }
  51. if (parent)
  52. return &parent->kobj;
  53. return NULL;
  54. }

这里的get_device_parent()就是获取父节点的kobject,但也并非就如此简单。get_device_parent()的返回值直接决定了device将被挂在哪个目录下。到底该挂在哪,是由dev->class、dev->parent、dev->parent->class等因素综合决定的。我们看get_device_parent()中是如何判断的。如果dev->class为空,表示一切随父设备,有parent则返回parent->kobj,没有则返回NULL。如果有dev->class呢,情况就比较复杂了,也许device有着与parent不同的class,也许device还没有一个parent,等等。我们看具体的情况。如果parent不为空,而且存在parent->class,则还放在parent目录下。不然,要么parent不存在,要么parent没有class,很难直接将有class的device放在parent下面。目前的解决方法很简单,在parent与device之间,再加一层表示class的目录。如果parent都没有,那就把/sys/devices/virtual当做parent。class->p->class_dirs就是专门存放这种中间kobject的kset。思路理清后,再结合实际的sysfs,代码就很容易看懂了。

  1. static void cleanup_glue_dir(struct device *dev, struct kobject *glue_dir)
  2. {
  3. /* see if we live in a "glue" directory */
  4. if (!glue_dir || !dev->class ||
  5. glue_dir->kset != &dev->class->p->class_dirs)
  6. return;
  7. kobject_put(glue_dir);
  8. }
  9. static void cleanup_device_parent(struct device *dev)
  10. {
  11. cleanup_glue_dir(dev, dev->kobj.parent);
  12. }

cleanup_device_parent()是取消对parent引用时调用的函数,看起来只针对这种glue形式的目录起作用。

  1. static void setup_parent(struct device *dev, struct device *parent)
  2. {
  3. struct kobject *kobj;
  4. kobj = get_device_parent(dev, parent);
  5. if (kobj)
  6. dev->kobj.parent = kobj;
  7. }

setup_parent()就是调用get_device_parent()获得应该存放的父目录kobj,并把dev->kobj.parent设为它。

  1. static int device_add_class_symlinks(struct device *dev)
  2. {
  3. int error;
  4. if (!dev->class)
  5. return 0;
  6. error = sysfs_create_link(&dev->kobj,
  7. &dev->class->p->class_subsys.kobj,
  8. "subsystem");
  9. if (error)
  10. goto out;
  11. /* link in the class directory pointing to the device */
  12. error = sysfs_create_link(&dev->class->p->class_subsys.kobj,
  13. &dev->kobj, dev_name(dev));
  14. if (error)
  15. goto out_subsys;
  16. if (dev->parent && device_is_not_partition(dev)) {
  17. error = sysfs_create_link(&dev->kobj, &dev->parent->kobj,
  18. "device");
  19. if (error)
  20. goto out_busid;
  21. }
  22. return 0;
  23. out_busid:
  24. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  25. out_subsys:
  26. sysfs_remove_link(&dev->kobj, "subsystem");
  27. out:
  28. return error;
  29. }

device_add_class_symlinks()在device和class直接添加一些软链接。在device目录下创建指向class的subsystem文件,在class目录下创建指向device的同名文件。如果device有父设备,而且device不是块设备分区,则在device目录下建立一个指向父设备的device链接文件。这一点在usb设备和usb接口间很常见。

  1. static void device_remove_class_symlinks(struct device *dev)
  2. {
  3. if (!dev->class)
  4. return;
  5. #ifdef CONFIG_SYSFS_DEPRECATED
  6. if (dev->parent && device_is_not_partition(dev)) {
  7. char *class_name;
  8. class_name = make_class_name(dev->class->name, &dev->kobj);
  9. if (class_name) {
  10. sysfs_remove_link(&dev->parent->kobj, class_name);
  11. kfree(class_name);
  12. }
  13. sysfs_remove_link(&dev->kobj, "device");
  14. }
  15. if (dev->kobj.parent != &dev->class->p->class_subsys.kobj &&
  16. device_is_not_partition(dev))
  17. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  18. dev_name(dev));
  19. #else
  20. if (dev->parent && device_is_not_partition(dev))
  21. sysfs_remove_link(&dev->kobj, "device");
  22. sysfs_remove_link(&dev->class->p->class_subsys.kobj, dev_name(dev));
  23. #endif
  24. sysfs_remove_link(&dev->kobj, "subsystem");
  25. }

device_remove_class_symlinks()删除device和class之间的软链接。

  1. static inline const char *dev_name(const struct device *dev)
  2. {
  3. return kobject_name(&dev->kobj);
  4. }
  5. int dev_set_name(struct device *dev, const char *fmt, ...)
  6. {
  7. va_list vargs;
  8. int err;
  9. va_start(vargs, fmt);
  10. err = kobject_set_name_vargs(&dev->kobj, fmt, vargs);
  11. va_end(vargs);
  12. return err;
  13. }

dev_name()获得设备名称,dev_set_name()设置设备名称。但这里的dev_set_name()只能在设备未注册前使用。device的名称其实是完全靠dev->kobj管理的。

  1. static struct kobject *device_to_dev_kobj(struct device *dev)
  2. {
  3. struct kobject *kobj;
  4. if (dev->class)
  5. kobj = dev->class->dev_kobj;
  6. else
  7. kobj = sysfs_dev_char_kobj;
  8. return kobj;
  9. }

device_to_dev_kobj()为dev选择合适的/sys/dev下的kobject,或者是块设备,或者是字符设备,或者没有。

  1. #define format_dev_t(buffer, dev)                   \
  2. ({                              \
  3. sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev));   \
  4. buffer;                         \
  5. })
  6. static int device_create_sys_dev_entry(struct device *dev)
  7. {
  8. struct kobject *kobj = device_to_dev_kobj(dev);
  9. int error = 0;
  10. char devt_str[15];
  11. if (kobj) {
  12. format_dev_t(devt_str, dev->devt);
  13. error = sysfs_create_link(kobj, &dev->kobj, devt_str);
  14. }
  15. return error;
  16. }
  17. static void device_remove_sys_dev_entry(struct device *dev)
  18. {
  19. struct kobject *kobj = device_to_dev_kobj(dev);
  20. char devt_str[15];
  21. if (kobj) {
  22. format_dev_t(devt_str, dev->devt);
  23. sysfs_remove_link(kobj, devt_str);
  24. }
  25. }

device_create_sys_dev_entry()是在/sys/dev相应的目录下建立对设备的软链接。先是通过device_to_dev_kobj()获得父节点的kobj,然后调用sysfs_create_link()建立软链接。

device_remove_sys_dev_entry()与其操作正相反,删除在/sys/dev下建立的软链接。

  1. int device_private_init(struct device *dev)
  2. {
  3. dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
  4. if (!dev->p)
  5. return -ENOMEM;
  6. dev->p->device = dev;
  7. klist_init(&dev->p->klist_children, klist_children_get,
  8. klist_children_put);
  9. return 0;
  10. }

device_private_init()分配并初始化dev->p。至于空间的释放,是等到释放设备时调用的device_release()中。

之前的函数比较散乱,或许找不出一个整体的印象。但下面马上就要看到重要的部分了,因为代码终于攒到了爆发的程度!

  1. /**
  2. * device_register - register a device with the system.
  3. * @dev: pointer to the device structure
  4. *
  5. * This happens in two clean steps - initialize the device
  6. * and add it to the system. The two steps can be called
  7. * separately, but this is the easiest and most common.
  8. * I.e. you should only call the two helpers separately if
  9. * have a clearly defined need to use and refcount the device
  10. * before it is added to the hierarchy.
  11. *
  12. * NOTE: _Never_ directly free @dev after calling this function, even
  13. * if it returned an error! Always use put_device() to give up the
  14. * reference initialized in this function instead.
  15. */
  16. int device_register(struct device *dev)
  17. {
  18. device_initialize(dev);
  19. return device_add(dev);
  20. }

device_register()是提供给外界注册设备的接口。它先是调用device_initialize()初始化dev结构,然后调用device_add()将其加入系统中。但要注意,在调用device_register()注册dev之前,有一些dev结构变量是需要自行设置的。这其中有指明设备位置的struct device *parent,struct bus_type *bus, struct class *class,有指明设备属性的 const char *init_name, struct device_type *type, const struct attribute_group **groups, void (*release)(struct device *dev), dev_t devt,等等。不同设备的使用方法不同,我们留待之后再具体分析。device_initialize()我们已经看过,下面重点看看device_add()是如何实现的。

  1. int device_add(struct device *dev)
  2. {
  3. struct device *parent = NULL;
  4. struct class_interface *class_intf;
  5. int error = -EINVAL;
  6. dev = get_device(dev);
  7. if (!dev)
  8. goto done;
  9. if (!dev->p) {
  10. error = device_private_init(dev);
  11. if (error)
  12. goto done;
  13. }
  14. /*
  15. * for statically allocated devices, which should all be converted
  16. * some day, we need to initialize the name. We prevent reading back
  17. * the name, and force the use of dev_name()
  18. */
  19. if (dev->init_name) {
  20. dev_set_name(dev, "%s", dev->init_name);
  21. dev->init_name = NULL;
  22. }
  23. if (!dev_name(dev))
  24. goto name_error;
  25. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  26. parent = get_device(dev->parent);
  27. setup_parent(dev, parent);
  28. /* use parent numa_node */
  29. if (parent)
  30. set_dev_node(dev, dev_to_node(parent));
  31. /* first, register with generic layer. */
  32. /* we require the name to be set before, and pass NULL */
  33. error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
  34. if (error)
  35. goto Error;
  36. /* notify platform of device entry */
  37. if (platform_notify)
  38. platform_notify(dev);
  39. error = device_create_file(dev, &uevent_attr);
  40. if (error)
  41. goto attrError;
  42. if (MAJOR(dev->devt)) {
  43. error = device_create_file(dev, &devt_attr);
  44. if (error)
  45. goto ueventattrError;
  46. error = device_create_sys_dev_entry(dev);
  47. if (error)
  48. goto devtattrError;
  49. devtmpfs_create_node(dev);
  50. }
  51. error = device_add_class_symlinks(dev);
  52. if (error)
  53. goto SymlinkError;
  54. error = device_add_attrs(dev);
  55. if (error)
  56. goto AttrsError;
  57. error = bus_add_device(dev);
  58. if (error)
  59. goto BusError;
  60. error = dpm_sysfs_add(dev);
  61. if (error)
  62. goto DPMError;
  63. device_pm_add(dev);
  64. /* Notify clients of device addition.  This call must come
  65. * after dpm_sysf_add() and before kobject_uevent().
  66. */
  67. if (dev->bus)
  68. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  69. BUS_NOTIFY_ADD_DEVICE, dev);
  70. kobject_uevent(&dev->kobj, KOBJ_ADD);
  71. bus_probe_device(dev);
  72. if (parent)
  73. klist_add_tail(&dev->p->knode_parent,
  74. &parent->p->klist_children);
  75. if (dev->class) {
  76. mutex_lock(&dev->class->p->class_mutex);
  77. /* tie the class to the device */
  78. klist_add_tail(&dev->knode_class,
  79. &dev->class->p->class_devices);
  80. /* notify any interfaces that the device is here */
  81. list_for_each_entry(class_intf,
  82. &dev->class->p->class_interfaces, node)
  83. if (class_intf->add_dev)
  84. class_intf->add_dev(dev, class_intf);
  85. mutex_unlock(&dev->class->p->class_mutex);
  86. }
  87. done:
  88. put_device(dev);
  89. return error;
  90. DPMError:
  91. bus_remove_device(dev);
  92. BusError:
  93. device_remove_attrs(dev);
  94. AttrsError:
  95. device_remove_class_symlinks(dev);
  96. SymlinkError:
  97. if (MAJOR(dev->devt))
  98. device_remove_sys_dev_entry(dev);
  99. devtattrError:
  100. if (MAJOR(dev->devt))
  101. device_remove_file(dev, &devt_attr);
  102. ueventattrError:
  103. device_remove_file(dev, &uevent_attr);
  104. attrError:
  105. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  106. kobject_del(&dev->kobj);
  107. Error:
  108. cleanup_device_parent(dev);
  109. if (parent)
  110. put_device(parent);
  111. name_error:
  112. kfree(dev->p);
  113. dev->p = NULL;
  114. goto done;
  115. }

device_add()将dev加入设备驱动模型。它先是调用get_device(dev)增加dev的引用计数,然后调用device_private_init()分配和初始化dev->p,调用dev_set_name()设置dev名字。然后是准备将dev加入sysfs,先是用get_device(parent)增加对parent的引用计数(无论是直接挂在parent下还是通过一个类层挂在parent下都要增加parent的引用计数),然后调用setup_parent()找到实际要加入的父kobject,通过kobject_add()加入其下。然后是添加属性和属性集合的操作,调用device_create_file()添加uevent属性,调用device_add_attrs()添加device/type/class预定义的属性与属性集合。如果dev有被分配设备号,再用device_create_file()添加dev属性,并用device_create_sys_dev_entry()在/sys/dev下添加相应的软链接,最后调用devtmpfs_create_node()在/dev下创建相应的设备文件。然后调用device_add_class_symlinks()添加dev与class间的软链接,调用bus_add_device()添加dev与bus间的软链接,并将dev挂入bus的设备链表。调用dpm_sysfs_add()增加dev下的power属性集合,调用device_pm_add()将dev加入dpm_list链表。

调用kobject_uevent()发布KOBJ_ADD消息,调用bus_probe_device()为dev寻找合适的驱动。如果有parent节点,把dev->p->knode_parent挂入parent->p->klist_children链表。如果dev有所属的class,将dev->knode_class挂在class->p->class_devices上,并调用可能的类设备接口的add_dev()方法。可能对于直接在bus上的设备来说,自然可以调用bus_probe_device()查找驱动,而不与总线直接接触的设备,则要靠class来发现驱动,这里的class_interface中的add_dev()方法,就是一个绝好的机会。最后会调用put_device(dev)释放在函数开头增加的引用计数。

device_add()要做的事很多,但想想每件事都在情理之中。device是设备驱动模型的基本元素,在class、bus、dev、devices中都有它的身影。device_add()要适应各种类型的设备注册,自然会越来越复杂。可以说文件开头定义的内部函数,差不多都是为了这里服务的。

  1. void device_unregister(struct device *dev)
  2. {
  3. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  4. device_del(dev);
  5. put_device(dev);
  6. }

有注册自然又注销。device_unregister()就是用于将dev从系统中注销,并释放创建时产生的引用计数。

  1. void device_del(struct device *dev)
  2. {
  3. struct device *parent = dev->parent;
  4. struct class_interface *class_intf;
  5. /* Notify clients of device removal.  This call must come
  6. * before dpm_sysfs_remove().
  7. */
  8. if (dev->bus)
  9. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  10. BUS_NOTIFY_DEL_DEVICE, dev);
  11. device_pm_remove(dev);
  12. dpm_sysfs_remove(dev);
  13. if (parent)
  14. klist_del(&dev->p->knode_parent);
  15. if (MAJOR(dev->devt)) {
  16. devtmpfs_delete_node(dev);
  17. device_remove_sys_dev_entry(dev);
  18. device_remove_file(dev, &devt_attr);
  19. }
  20. if (dev->class) {
  21. device_remove_class_symlinks(dev);
  22. mutex_lock(&dev->class->p->class_mutex);
  23. /* notify any interfaces that the device is now gone */
  24. list_for_each_entry(class_intf,
  25. &dev->class->p->class_interfaces, node)
  26. if (class_intf->remove_dev)
  27. class_intf->remove_dev(dev, class_intf);
  28. /* remove the device from the class list */
  29. klist_del(&dev->knode_class);
  30. mutex_unlock(&dev->class->p->class_mutex);
  31. }
  32. device_remove_file(dev, &uevent_attr);
  33. device_remove_attrs(dev);
  34. bus_remove_device(dev);
  35. /*
  36. * Some platform devices are driven without driver attached
  37. * and managed resources may have been acquired.  Make sure
  38. * all resources are released.
  39. */
  40. devres_release_all(dev);
  41. /* Notify the platform of the removal, in case they
  42. * need to do anything...
  43. */
  44. if (platform_notify_remove)
  45. platform_notify_remove(dev);
  46. kobject_uevent(&dev->kobj, KOBJ_REMOVE);
  47. cleanup_device_parent(dev);
  48. kobject_del(&dev->kobj);
  49. put_device(parent);
  50. }

device_del()是与device_add()相对的函数,进行实际的将dev从系统中脱离的工作。这其中既有将dev从设备驱动模型各种链表中脱离的工作,又有将dev从sysfs的各个角落删除的工作。大致流程与dev_add()相对,就不一一介绍。

爆发结束,下面来看一些比较轻松的函数。

  1. /**
  2. * device_get_devnode - path of device node file
  3. * @dev: device
  4. * @mode: returned file access mode
  5. * @tmp: possibly allocated string
  6. *
  7. * Return the relative path of a possible device node.
  8. * Non-default names may need to allocate a memory to compose
  9. * a name. This memory is returned in tmp and needs to be
  10. * freed by the caller.
  11. */
  12. const char *device_get_devnode(struct device *dev,
  13. mode_t *mode, const char **tmp)
  14. {
  15. char *s;
  16. *tmp = NULL;
  17. /* the device type may provide a specific name */
  18. if (dev->type && dev->type->devnode)
  19. *tmp = dev->type->devnode(dev, mode);
  20. if (*tmp)
  21. return *tmp;
  22. /* the class may provide a specific name */
  23. if (dev->class && dev->class->devnode)
  24. *tmp = dev->class->devnode(dev, mode);
  25. if (*tmp)
  26. return *tmp;
  27. /* return name without allocation, tmp == NULL */
  28. if (strchr(dev_name(dev), '!') == NULL)
  29. return dev_name(dev);
  30. /* replace '!' in the name with '/' */
  31. *tmp = kstrdup(dev_name(dev), GFP_KERNEL);
  32. if (!*tmp)
  33. return NULL;
  34. while ((s = strchr(*tmp, '!')))
  35. s[0] = '/';
  36. return *tmp;
  37. }

device_get_devnode()返回设备的路径名。不过似乎可以由device_type或者class定义一些独特的返回名称。

  1. static struct device *next_device(struct klist_iter *i)
  2. {
  3. struct klist_node *n = klist_next(i);
  4. struct device *dev = NULL;
  5. struct device_private *p;
  6. if (n) {
  7. p = to_device_private_parent(n);
  8. dev = p->device;
  9. }
  10. return dev;
  11. }
  12. int device_for_each_child(struct device *parent, void *data,
  13. int (*fn)(struct device *dev, void *data))
  14. {
  15. struct klist_iter i;
  16. struct device *child;
  17. int error = 0;
  18. if (!parent->p)
  19. return 0;
  20. klist_iter_init(&parent->p->klist_children, &i);
  21. while ((child = next_device(&i)) && !error)
  22. error = fn(child, data);
  23. klist_iter_exit(&i);
  24. return error;
  25. }
  26. struct device *device_find_child(struct device *parent, void *data,
  27. int (*match)(struct device *dev, void *data))
  28. {
  29. struct klist_iter i;
  30. struct device *child;
  31. if (!parent)
  32. return NULL;
  33. klist_iter_init(&parent->p->klist_children, &i);
  34. while ((child = next_device(&i)))
  35. if (match(child, data) && get_device(child))
  36. break;
  37. klist_iter_exit(&i);
  38. return child;
  39. }

device_for_each_child()对dev下的每个子device,都调用一遍特定的处理函数。

device_find_child()则是查找dev下特点的子device,查找使用特定的match函数。

这两个遍历过程都使用了klist特有的遍历函数,支持遍历过程中的节点删除等功能。next_device()则是为了遍历方便封装的一个内部函数。

下面本该是root_device注册相关的代码。但经过检查,linux内核中使用到的root_device很少见,而且在sysfs中也未能找到一个实际的例子。所以root_device即使还未被弃用,也并非主流,我们将其跳过。

与kobject和kset类似,device也为我们提供了快速device创建方法,下面就看看吧。

  1. static void device_create_release(struct device *dev)
  2. {
  3. pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
  4. kfree(dev);
  5. }
  6. struct device *device_create_vargs(struct class *classstruct device *parent,
  7. dev_t devt, void *drvdata, const char *fmt,
  8. va_list args)
  9. {
  10. struct device *dev = NULL;
  11. int retval = -ENODEV;
  12. if (class == NULL || IS_ERR(class))
  13. goto error;
  14. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  15. if (!dev) {
  16. retval = -ENOMEM;
  17. goto error;
  18. }
  19. dev->devt = devt;
  20. dev->class = class;
  21. dev->parent = parent;
  22. dev->release = device_create_release;
  23. dev_set_drvdata(dev, drvdata);
  24. retval = kobject_set_name_vargs(&dev->kobj, fmt, args);
  25. if (retval)
  26. goto error;
  27. retval = device_register(dev);
  28. if (retval)
  29. goto error;
  30. return dev;
  31. error:
  32. put_device(dev);
  33. return ERR_PTR(retval);
  34. }
  35. struct device *device_create(struct class *classstruct device *parent,
  36. dev_t devt, void *drvdata, const char *fmt, ...)
  37. {
  38. va_list vargs;
  39. struct device *dev;
  40. va_start(vargs, fmt);
  41. dev = device_create_vargs(class, parent, devt, drvdata, fmt, vargs);
  42. va_end(vargs);
  43. return dev;
  44. }

这里的device_create()提供了一个快速的dev创建注册方法。只是中间没有提供设置device_type的方法,或许是这样的device已经够特立独行了,不需要搞出一类来。

  1. static int __match_devt(struct device *dev, void *data)
  2. {
  3. dev_t *devt = data;
  4. return dev->devt == *devt;
  5. }
  6. void device_destroy(struct class *class, dev_t devt)
  7. {
  8. struct device *dev;
  9. dev = class_find_device(class, NULL, &devt, __match_devt);
  10. if (dev) {
  11. put_device(dev);
  12. device_unregister(dev);
  13. }
  14. }

device_destroy()就是与device_create()相对的注销函数。至于这里为什么会多一个put_device(dev),也很简单,因为在class_find_device()找到dev时,调用了get_device()。

  1. struct device *class_find_device(struct class *classstruct device *start,
  2. void *data,
  3. int (*match)(struct device *, void *))
  4. {
  5. struct class_dev_iter iter;
  6. struct device *dev;
  7. if (!class)
  8. return NULL;
  9. if (!class->p) {
  10. WARN(1, "%s called for class '%s' before it was initialized",
  11. __func__, class->name);
  12. return NULL;
  13. }
  14. class_dev_iter_init(&iter, class, start, NULL);
  15. while ((dev = class_dev_iter_next(&iter))) {
  16. if (match(dev, data)) {
  17. get_device(dev);
  18. break;
  19. }
  20. }
  21. class_dev_iter_exit(&iter);
  22. return dev;
  23. }

class_find_device()本来是class.c中的内容,其实现也于之前将的遍历dev->p->klist_children类似,无非是在klist提供的遍历方法上加以封装。但我们这里列出class_find_device()的实现与使用它的device_destroy(),却是为了更好地分析这个调用流程中dev是如何被保护的。它实际上是经历了三个保护手段:首先在class_dev_iter_next()->klist_next()中,是受到struct klist中 spinlock_t k_lock保护的。在找到下一点并解锁之前,就增加了struct klist_node中的struct kref n_ref引用计数。在当前的next()调用完,到下一个next()调用之前,都是受这个增加的引用计数保护的。再看class_find_device()中,使用get_device(dev)增加了dev本身的引用计数保护(当然也要追溯到kobj->kref中),这是第三种保护。知道device_destroy()中主动调用put_device(dev)才去除了这种保护。

本来对dev的保护,应该完全是由dev中的引用计数完成的。但实际上这种保护很多时候是间接完成的。例如这里的klist中的自旋锁,klist_node中的引用计数,都不过是为了保持class的设备链表中对dev的引用计数不消失,这是一种间接保护的手段,保证了这中间即使外界主动释放class设备链表对dev的引用计数,dev仍然不会被实际注销。这种曲折的联系,才真正发挥了引用计数的作用,构成设备驱动模型独特的魅力。

  1. int device_rename(struct device *dev, char *new_name)
  2. {
  3. char *old_device_name = NULL;
  4. int error;
  5. dev = get_device(dev);
  6. if (!dev)
  7. return -EINVAL;
  8. pr_debug("device: '%s': %s: renaming to '%s'\n", dev_name(dev),
  9. __func__, new_name);
  10. old_device_name = kstrdup(dev_name(dev), GFP_KERNEL);
  11. if (!old_device_name) {
  12. error = -ENOMEM;
  13. goto out;
  14. }
  15. error = kobject_rename(&dev->kobj, new_name);
  16. if (error)
  17. goto out;
  18. if (dev->class) {
  19. error = sysfs_create_link_nowarn(&dev->class->p->class_subsys.kobj,
  20. &dev->kobj, dev_name(dev));
  21. if (error)
  22. goto out;
  23. sysfs_remove_link(&dev->class->p->class_subsys.kobj,
  24. old_device_name);
  25. }
  26. out:
  27. put_device(dev);
  28. kfree(old_device_name);
  29. return error;
  30. }

device_rename()是供设备注册后改变名称用的,除了改变/sys/devices下地名称,还改变了/sys/class下地软链接名称。前者很自然,但后者却很难想到。即使简单的地方,经过重重调试,我们也会惊讶于linux的心细如发。

  1. static int device_move_class_links(struct device *dev,
  2. struct device *old_parent,
  3. struct device *new_parent)
  4. {
  5. int error = 0;
  6. if (old_parent)
  7. sysfs_remove_link(&dev->kobj, "device");
  8. if (new_parent)
  9. error = sysfs_create_link(&dev->kobj, &new_parent->kobj,
  10. "device");
  11. return error;
  12. #endif
  13. }

device_move_class_links()只是一个内部函数,后面还有操纵它的那只手。这里的device_move_class_links显得很名不副实,并没用操作class中软链接的举动。这很正常,因为在sysfs中软链接是针对kobject来说的,所以即使位置变掉了,软链接还是很很准确地定位。

  1. /**
  2. * device_move - moves a device to a new parent
  3. * @dev: the pointer to the struct device to be moved
  4. * @new_parent: the new parent of the device (can by NULL)
  5. * @dpm_order: how to reorder the dpm_list
  6. */
  7. int device_move(struct device *dev, struct device *new_parent,
  8. enum dpm_order dpm_order)
  9. {
  10. int error;
  11. struct device *old_parent;
  12. struct kobject *new_parent_kobj;
  13. dev = get_device(dev);
  14. if (!dev)
  15. return -EINVAL;
  16. device_pm_lock();
  17. new_parent = get_device(new_parent);
  18. new_parent_kobj = get_device_parent(dev, new_parent);
  19. pr_debug("device: '%s': %s: moving to '%s'\n", dev_name(dev),
  20. __func__, new_parent ? dev_name(new_parent) : "<NULL>");
  21. error = kobject_move(&dev->kobj, new_parent_kobj);
  22. if (error) {
  23. cleanup_glue_dir(dev, new_parent_kobj);
  24. put_device(new_parent);
  25. goto out;
  26. }
  27. old_parent = dev->parent;
  28. dev->parent = new_parent;
  29. if (old_parent)
  30. klist_remove(&dev->p->knode_parent);
  31. if (new_parent) {
  32. klist_add_tail(&dev->p->knode_parent,
  33. &new_parent->p->klist_children);
  34. set_dev_node(dev, dev_to_node(new_parent));
  35. }
  36. if (!dev->class)
  37. goto out_put;
  38. error = device_move_class_links(dev, old_parent, new_parent);
  39. if (error) {
  40. /* We ignore errors on cleanup since we're hosed anyway... */
  41. device_move_class_links(dev, new_parent, old_parent);
  42. if (!kobject_move(&dev->kobj, &old_parent->kobj)) {
  43. if (new_parent)
  44. klist_remove(&dev->p->knode_parent);
  45. dev->parent = old_parent;
  46. if (old_parent) {
  47. klist_add_tail(&dev->p->knode_parent,
  48. &old_parent->p->klist_children);
  49. set_dev_node(dev, dev_to_node(old_parent));
  50. }
  51. }
  52. cleanup_glue_dir(dev, new_parent_kobj);
  53. put_device(new_parent);
  54. goto out;
  55. }
  56. switch (dpm_order) {
  57. case DPM_ORDER_NONE:
  58. break;
  59. case DPM_ORDER_DEV_AFTER_PARENT:
  60. device_pm_move_after(dev, new_parent);
  61. break;
  62. case DPM_ORDER_PARENT_BEFORE_DEV:
  63. device_pm_move_before(new_parent, dev);
  64. break;
  65. case DPM_ORDER_DEV_LAST:
  66. device_pm_move_last(dev);
  67. break;
  68. }
  69. out_put:
  70. put_device(old_parent);
  71. out:
  72. device_pm_unlock();
  73. put_device(dev);
  74. return error;
  75. }

device_move()就是将dev移到一个新的parent下。但也有可能这个parent是空的。大部分操作围绕在引用计数上,get_device(),put_device()。而且换了新的parent,到底要加到sysfs中哪个目录下,还要再调用get_device_parent()研究一下。主要的操作就是kobject_move()和device_move_class_links()。因为在sysfs中软链接是针对kobject来说的,所以即使位置变掉了,软链接还是很很准确地定位,所以在/sys/dev、/sys/bus、/sys/class中的软链接都不用变,这实在是sysfs的一大优势。除此之外,device_move()还涉及到电源管理的问题,device移动影响到dev在dpm_list上的位置,我们对此不了解,先忽略之。

  1. void device_shutdown(void)
  2. {
  3. struct device *dev, *devn;
  4. list_for_each_entry_safe_reverse(dev, devn, &devices_kset->list,
  5. kobj.entry) {
  6. if (dev->bus && dev->bus->shutdown) {
  7. dev_dbg(dev, "shutdown\n");
  8. dev->bus->shutdown(dev);
  9. else if (dev->driver && dev->driver->shutdown) {
  10. dev_dbg(dev, "shutdown\n");
  11. dev->driver->shutdown(dev);
  12. }
  13. }
  14. kobject_put(sysfs_dev_char_kobj);
  15. kobject_put(sysfs_dev_block_kobj);
  16. kobject_put(dev_kobj);
  17. async_synchronize_full();
  18. }

这个device_shutdown()是在系统关闭时才调用的。它动用了很少使用的devices_kset,从而可以遍历到每个注册到sysfs上的设备,调用相应的总线或驱动定义的shutdown()函数。提起这个,还是在device_initialize()中将dev->kobj->kset统一设为devices_kset的。原来设备虽然有不同的parent,但kset还是一样的。这样我们就能理解/sys/devices下的顶层设备目录是怎么来的,因为没用parent,就在调用kobject_add()时将kset->kobj当成了parent,所以会直接挂在顶层目录下。这样的��录大致有pci0000:00、virtual等等。

看完了core.c,我有种明白机器人也是由零件组成的的感觉。linux设备驱动模型的大门已经打开了四分之一。随着分析的深入,我们大概也会越来越明白linux的良苦用心。

Linux内核部件分析 设备驱动模型之device相关推荐

  1. Linux内核部件分析 设备驱动模型之driver ---mark 详细

    Linux内核部件分析 设备驱动模型之driver 转载:https://www.linuxidc.com/Linux/2011-10/44627p7.htm 上节我们分析设备驱动模型中的device ...

  2. [转载]Linux内核大讲堂 (一) 设备驱动的基石驱动模型(1)

    [转载]Linux内核大讲堂 (一) 设备驱动的基石驱动模型(1) 2011年09月13日 可能把驱动模型放在第一章讲会会有点难度,但是只要能跨过这道坎,后面就会轻松很多,驱动模型是整个linux设备 ...

  3. linux内核部件分析之——设备驱动模型之class

    前面看过了设备驱动模型中的bus.device.driver,这三种都是有迹可循的.其中bus代表实际的总线,device代表实际的设备和接口,而driver则对应存在的驱动.但本节要介绍的class ...

  4. linux内核部件分析(十)——设备驱动模型之class,linux内核部件分析(十)——设备驱动模型之class...

    前面看过了设备驱动模型中的bus.device.driver,这三种都是有迹可循的.其中bus代表实际的总线,device代表实际的设备和接口,而driver则对应存在的驱动.但本节要介绍的class ...

  5. linux内核的块设备驱动框架详解

    1.块设备和字符设备的差异 (1)块设备只能以块为单位接受输入和返回输出,而字符设备则以字节为单位.大多数设备是字符设备,因为它们不需要缓冲而且不以固定块大小进行操作; (2)块设备对于 I/O 请求 ...

  6. Linux内核大讲堂之设备驱动的基石驱动模型(1)

    转自 http://blog.csdn.net/gqb_driver/article/details/8449588 转自:无为和尚的Linux内核大讲堂系列,并对个别地方进行了补充更正(见标红处). ...

  7. Linux内核大讲堂 (一) 设备驱动的基石驱动模型(1)

    可能把驱动模型放在第一章讲会会有点难度,但是只要能跨过这道坎,后面就会轻松很多,驱动模型是整个linux设备驱动的基石.大部分人把驱动模型叫做设备模型,但是我查了linux的帮助文档,就是在下载源码路 ...

  8. 【转】Linux内核大讲堂 (一) 设备驱动的基石驱动模型(1)

    原文传送门http://blog.csdn.net/z2007b/archive/2011/05/03/6388753.aspx 可能把驱动模型放在第一章讲会会有点难度,但是只要能跨过这道坎,后面就会 ...

  9. 设备驱动模型:device, bus, driver之间的联系

    对于驱动工程师而言,在移植porting对应设备的driver时,要在devicetree中增加对应的设备节点,其中有一个compatible属性,这个属性的字符串要和driver里面的of_devi ...

  10. linux设备驱动模型-linux驱动开发第5部分-朱有鹏-专题视频课程

    linux设备驱动模型-linux驱动开发第5部分-4285人已学习 课程介绍         本课程是linux驱动开发的第5个课程,主要内容是linux的设备驱动模型,包括总线.类.设备.驱动等概 ...

最新文章

  1. JNI中java类型的简写
  2. java 010_Java笔记-day010-[String类]
  3. vue.js 三种方式安装--npm安装
  4. 爬取json Swaggerui界面
  5. java,java基础面试笔试题
  6. python中的单例设计模式
  7. 7-4 三角形判断 (15 分)
  8. 图文讲解 sqlserver 2000 评测版 数据库过期 的解决方法
  9. Date类型之组件方法
  10. 使用搭建的GNS3连接交换机互联互通
  11. 小黑课堂c语言题库,未来教育和小黑课堂哪个好 试卷题目是一样的吗
  12. USB大容量存储类规范概述
  13. 【软考:网工】协议篇(非常重要)
  14. SEO优化基础知识大全 SEO新手入门必备知识
  15. 压缩文件已损坏怎么办?恢复压缩文件,解决方法看这里
  16. 分类问题中正负样本分布不均衡问题的解决方法
  17. 小白带你学安卓——初识android
  18. 华为交换机升级包及补丁验证完整性
  19. c语言程序设计实验13文件,第13章_文件---《C语言程序设计》实验指导.ppt
  20. 5G NR PDCP协议(一)

热门文章

  1. 程序员有多少读过《人性的弱点》?项目经理呢?
  2. Spring Boot DATA JPA抓取SQL运行时的传递进去的参数信息
  3. Centos7.4 部署DHCP服务
  4. pyCharm-激活码(2018)
  5. VB.NET与 sql数据库
  6. 用JS做关灯游戏(初级)
  7. reverse() ; sort() ; sorted()
  8. springAOP 之 前置输出
  9. 进程介绍(理论部分)
  10. 转 Kafka入门经典教程