Linux设备模型分析之device(基于3.10.1内核)

作者:刘昊昱 

博客:http://blog.csdn.net/liuhaoyutz

内核版本:3.10.1 

http://blog.csdn.net/liuhaoyutz/article/details/14228761 

 
一、device定义
device结构体定义在include/linux/device.h文件中:
 
 
  1. 598/** 
  2. 599 * struct device - The basic device structure 
  3. 600 * @parent: The device's "parent" device, the device to which it is attached. 
  4. 601 *      In most cases, a parent device is some sort of bus or host 
  5. 602 *      controller. If parent is NULL, the device, is a top-level device, 
  6. 603 *      which is not usually what you want. 
  7. 604 * @p:      Holds the private data of the driver core portions of the device. 
  8. 605 *      See the comment of the struct device_private for detail. 
  9. 606 * @kobj:   A top-level, abstract class from which other classes are derived. 
  10. 607 * @init_name:  Initial name of the device. 
  11. 608 * @type:   The type of device. 
  12. 609 *      This identifies the device type and carries type-specific 
  13. 610 *      information. 
  14. 611 * @mutex:  Mutex to synchronize calls to its driver. 
  15. 612 * @bus:    Type of bus device is on. 
  16. 613 * @driver: Which driver has allocated this 
  17. 614 * @platform_data: Platform data specific to the device. 
  18. 615 *      Example: For devices on custom boards, as typical of embedded 
  19. 616 *      and SOC based hardware, Linux often uses platform_data to point 
  20. 617 *      to board-specific structures describing devices and how they 
  21. 618 *      are wired.  That can include what ports are available, chip 
  22. 619 *      variants, which GPIO pins act in what additional roles, and so 
  23. 620 *      on.  This shrinks the "Board Support Packages" (BSPs) and 
  24. 621 *      minimizes board-specific #ifdefs in drivers. 
  25. 622 * @power:  For device power management. 
  26. 623 *      See Documentation/power/devices.txt for details. 
  27. 624 * @pm_domain:  Provide callbacks that are executed during system suspend, 
  28. 625 *      hibernation, system resume and during runtime PM transitions 
  29. 626 *      along with subsystem-level and driver-level callbacks. 
  30. 627 * @pins:   For device pin management. 
  31. 628 *      See Documentation/pinctrl.txt for details. 
  32. 629 * @numa_node:  NUMA node this device is close to. 
  33. 630 * @dma_mask:   Dma mask (if dma'ble device). 
  34. 631 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all 
  35. 632 *      hardware supports 64-bit addresses for consistent allocations 
  36. 633 *      such descriptors. 
  37. 634 * @dma_parms:  A low level driver may set these to teach IOMMU code about 
  38. 635 *      segment limitations. 
  39. 636 * @dma_pools:  Dma pools (if dma'ble device). 
  40. 637 * @dma_mem:    Internal for coherent mem override. 
  41. 638 * @archdata:   For arch-specific additions. 
  42. 639 * @of_node:    Associated device tree node. 
  43. 640 * @acpi_node:  Associated ACPI device node. 
  44. 641 * @devt:   For creating the sysfs "dev". 
  45. 642 * @id:     device instance 
  46. 643 * @devres_lock: Spinlock to protect the resource of the device. 
  47. 644 * @devres_head: The resources list of the device. 
  48. 645 * @knode_class: The node used to add the device to the class list. 
  49. 646 * @class:  The class of the device. 
  50. 647 * @groups: Optional attribute groups. 
  51. 648 * @release:    Callback to free the device after all references have 
  52. 649 *      gone away. This should be set by the allocator of the 
  53. 650 *      device (i.e. the bus driver that discovered the device). 
  54. 651 * 
  55. 652 * At the lowest level, every device in a Linux system is represented by an 
  56. 653 * instance of struct device. The device structure contains the information 
  57. 654 * that the device model core needs to model the system. Most subsystems, 
  58. 655 * however, track additional information about the devices they host. As a 
  59. 656 * result, it is rare for devices to be represented by bare device structures; 
  60. 657 * instead, that structure, like kobject structures, is usually embedded within 
  61. 658 * a higher-level representation of the device. 
  62. 659 */  
  63. 660struct device {  
  64. 661    struct device       *parent;  
  65. 662  
  66. 663    struct device_private   *p;  
  67. 664  
  68. 665    struct kobject kobj;  
  69. 666    const char      *init_name; /* initial name of the device */  
  70. 667    const struct device_type *type;  
  71. 668  
  72. 669    struct mutex        mutex;  /* mutex to synchronize calls to 
  73. 670                     * its driver. 
  74. 671                     */  
  75. 672  
  76. 673    struct bus_type *bus;       /* type of bus device is on */  
  77. 674    struct device_driver *driver;   /* which driver has allocated this 
  78. 675                       device */  
  79. 676    void        *platform_data; /* Platform specific data, device 
  80. 677                       core doesn't touch it */  
  81. 678    struct dev_pm_info  power;  
  82. 679    struct dev_pm_domain    *pm_domain;  
  83. 680  
  84. 681#ifdef CONFIG_PINCTRL  
  85. 682    struct dev_pin_info *pins;  
  86. 683#endif  
  87. 684  
  88. 685#ifdef CONFIG_NUMA  
  89. 686    int     numa_node;  /* NUMA node this device is close to */  
  90. 687#endif  
  91. 688    u64     *dma_mask;  /* dma mask (if dma'able device) */  
  92. 689    u64     coherent_dma_mask;/* Like dma_mask, but for 
  93. 690                         alloc_coherent mappings as 
  94. 691                         not all hardware supports 
  95. 692                         64 bit addresses for consistent 
  96. 693                         allocations such descriptors. */  
  97. 694  
  98. 695    struct device_dma_parameters *dma_parms;  
  99. 696  
  100. 697    struct list_head    dma_pools;  /* dma pools (if dma'ble) */  
  101. 698  
  102. 699    struct dma_coherent_mem *dma_mem; /* internal for coherent mem 
  103. 700                         override */  
  104. 701#ifdef CONFIG_CMA  
  105. 702    struct cma *cma_area;       /* contiguous memory area for dma 
  106. 703                       allocations */  
  107. 704#endif  
  108. 705    /* arch specific additions */  
  109. 706    struct dev_archdata archdata;  
  110. 707  
  111. 708    struct device_node  *of_node; /* associated device tree node */  
  112. 709    struct acpi_dev_node    acpi_node; /* associated ACPI device node */  
  113. 710  
  114. 711    dev_t           devt;   /* dev_t, creates the sysfs "dev" */  
  115. 712    u32         id; /* device instance */  
  116. 713  
  117. 714    spinlock_t      devres_lock;  
  118. 715    struct list_head    devres_head;  
  119. 716  
  120. 717    struct klist_node   knode_class;  
  121. 718    struct class        *class;  
  122. 719    const struct attribute_group **groups;  /* optional groups */  
  123. 720  
  124. 721    void    (*release)(struct device *dev);  
  125. 722    struct iommu_group  *iommu_group;  
  126. 723};  
 598/**
 599 * struct device - The basic device structure
 600 * @parent: The device's "parent" device, the device to which it is attached.
 601 *      In most cases, a parent device is some sort of bus or host
 602 *      controller. If parent is NULL, the device, is a top-level device,
 603 *      which is not usually what you want.
 604 * @p:      Holds the private data of the driver core portions of the device.
 605 *      See the comment of the struct device_private for detail.
 606 * @kobj:   A top-level, abstract class from which other classes are derived.
 607 * @init_name:  Initial name of the device.
 608 * @type:   The type of device.
 609 *      This identifies the device type and carries type-specific
 610 *      information.
 611 * @mutex:  Mutex to synchronize calls to its driver.
 612 * @bus:    Type of bus device is on.
 613 * @driver: Which driver has allocated this
 614 * @platform_data: Platform data specific to the device.
 615 *      Example: For devices on custom boards, as typical of embedded
 616 *      and SOC based hardware, Linux often uses platform_data to point
 617 *      to board-specific structures describing devices and how they
 618 *      are wired.  That can include what ports are available, chip
 619 *      variants, which GPIO pins act in what additional roles, and so
 620 *      on.  This shrinks the "Board Support Packages" (BSPs) and
 621 *      minimizes board-specific #ifdefs in drivers.
 622 * @power:  For device power management.
 623 *      See Documentation/power/devices.txt for details.
 624 * @pm_domain:  Provide callbacks that are executed during system suspend,
 625 *      hibernation, system resume and during runtime PM transitions
 626 *      along with subsystem-level and driver-level callbacks.
 627 * @pins:   For device pin management.
 628 *      See Documentation/pinctrl.txt for details.
 629 * @numa_node:  NUMA node this device is close to.
 630 * @dma_mask:   Dma mask (if dma'ble device).
 631 * @coherent_dma_mask: Like dma_mask, but for alloc_coherent mapping as not all
 632 *      hardware supports 64-bit addresses for consistent allocations
 633 *      such descriptors.
 634 * @dma_parms:  A low level driver may set these to teach IOMMU code about
 635 *      segment limitations.
 636 * @dma_pools:  Dma pools (if dma'ble device).
 637 * @dma_mem:    Internal for coherent mem override.
 638 * @archdata:   For arch-specific additions.
 639 * @of_node:    Associated device tree node.
 640 * @acpi_node:  Associated ACPI device node.
 641 * @devt:   For creating the sysfs "dev".
 642 * @id:     device instance
 643 * @devres_lock: Spinlock to protect the resource of the device.
 644 * @devres_head: The resources list of the device.
 645 * @knode_class: The node used to add the device to the class list.
 646 * @class:  The class of the device.
 647 * @groups: Optional attribute groups.
 648 * @release:    Callback to free the device after all references have
 649 *      gone away. This should be set by the allocator of the
 650 *      device (i.e. the bus driver that discovered the device).
 651 *
 652 * At the lowest level, every device in a Linux system is represented by an
 653 * instance of struct device. The device structure contains the information
 654 * that the device model core needs to model the system. Most subsystems,
 655 * however, track additional information about the devices they host. As a
 656 * result, it is rare for devices to be represented by bare device structures;
 657 * instead, that structure, like kobject structures, is usually embedded within
 658 * a higher-level representation of the device.
 659 */
 660struct device {
 661    struct device       *parent;
 662
 663    struct device_private   *p;
 664
 665    struct kobject kobj;
 666    const char      *init_name; /* initial name of the device */
 667    const struct device_type *type;
 668
 669    struct mutex        mutex;  /* mutex to synchronize calls to
 670                     * its driver.
 671                     */
 672
 673    struct bus_type *bus;       /* type of bus device is on */
 674    struct device_driver *driver;   /* which driver has allocated this
 675                       device */
 676    void        *platform_data; /* Platform specific data, device
 677                       core doesn't touch it */
 678    struct dev_pm_info  power;
 679    struct dev_pm_domain    *pm_domain;
 680
 681#ifdef CONFIG_PINCTRL
 682    struct dev_pin_info *pins;
 683#endif
 684
 685#ifdef CONFIG_NUMA
 686    int     numa_node;  /* NUMA node this device is close to */
 687#endif
 688    u64     *dma_mask;  /* dma mask (if dma'able device) */
 689    u64     coherent_dma_mask;/* Like dma_mask, but for
 690                         alloc_coherent mappings as
 691                         not all hardware supports
 692                         64 bit addresses for consistent
 693                         allocations such descriptors. */
 694
 695    struct device_dma_parameters *dma_parms;
 696
 697    struct list_head    dma_pools;  /* dma pools (if dma'ble) */
 698
 699    struct dma_coherent_mem *dma_mem; /* internal for coherent mem
 700                         override */
 701#ifdef CONFIG_CMA
 702    struct cma *cma_area;       /* contiguous memory area for dma
 703                       allocations */
 704#endif
 705    /* arch specific additions */
 706    struct dev_archdata archdata;
 707
 708    struct device_node  *of_node; /* associated device tree node */
 709    struct acpi_dev_node    acpi_node; /* associated ACPI device node */
 710
 711    dev_t           devt;   /* dev_t, creates the sysfs "dev" */
 712    u32         id; /* device instance */
 713
 714    spinlock_t      devres_lock;
 715    struct list_head    devres_head;
 716
 717    struct klist_node   knode_class;
 718    struct class        *class;
 719    const struct attribute_group **groups;  /* optional groups */
 720
 721    void    (*release)(struct device *dev);
 722    struct iommu_group  *iommu_group;
 723};


我们来看一下device结构体的几个重要成员变量。
parent,device的父设备,即device依附的设备,通常父设备是某种类型的bus或host controller。如果parent的值是NULL,那么device将是一个顶层设备,这通常不是我们想要的。
p,device的私有数据,它是struct device_private结构体变量,这个结构体定义在drivers/base/base.h文件中,其内容如下:
 
 
  1. 55/** 
  2. 56 * struct device_private - structure to hold the private to the driver core portions of the device structure. 
  3. 57 * 
  4. 58 * @klist_children - klist containing all children of this device 
  5. 59 * @knode_parent - node in sibling list 
  6. 60 * @knode_driver - node in driver list 
  7. 61 * @knode_bus - node in bus list 
  8. 62 * @deferred_probe - entry in deferred_probe_list which is used to retry the 
  9. 63 *  binding of drivers which were unable to get all the resources needed by 
  10. 64 *  the device; typically because it depends on another driver getting 
  11. 65 *  probed first. 
  12. 66 * @driver_data - private pointer for driver specific info.  Will turn into a 
  13. 67 * list soon. 
  14. 68 * @device - pointer back to the struct class that this structure is 
  15. 69 * associated with. 
  16. 70 * 
  17. 71 * Nothing outside of the driver core should ever touch these fields. 
  18. 72 */  
  19. 73struct device_private {  
  20. 74    struct klist klist_children;  
  21. 75    struct klist_node knode_parent;  
  22. 76    struct klist_node knode_driver;  
  23. 77    struct klist_node knode_bus;  
  24. 78    struct list_head deferred_probe;  
  25. 79    void *driver_data;  
  26. 80    struct device *device;  
  27. 81};  
 55/**
 56 * struct device_private - structure to hold the private to the driver core portions of the device structure.
 57 *
 58 * @klist_children - klist containing all children of this device
 59 * @knode_parent - node in sibling list
 60 * @knode_driver - node in driver list
 61 * @knode_bus - node in bus list
 62 * @deferred_probe - entry in deferred_probe_list which is used to retry the
 63 *  binding of drivers which were unable to get all the resources needed by
 64 *  the device; typically because it depends on another driver getting
 65 *  probed first.
 66 * @driver_data - private pointer for driver specific info.  Will turn into a
 67 * list soon.
 68 * @device - pointer back to the struct class that this structure is
 69 * associated with.
 70 *
 71 * Nothing outside of the driver core should ever touch these fields.
 72 */
 73struct device_private {
 74    struct klist klist_children;
 75    struct klist_node knode_parent;
 76    struct klist_node knode_driver;
 77    struct klist_node knode_bus;
 78    struct list_head deferred_probe;
 79    void *driver_data;
 80    struct device *device;
 81};


kobj,device对应的kobject。
init_name,device的名字。
type,device类型。
bus,device所属的bus。
driver,device所关联的device_driver。
platform_data,平台相关数据。
 
二、devics相关sysfs结构初始化
Linux设备模型中,device相关的sysfs结构的初始化是由devices_init 函数完成的,该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 1395int __init devices_init(void)  
  2. 1396{  
  3. 1397    devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);  
  4. 1398    if (!devices_kset)  
  5. 1399        return -ENOMEM;  
  6. 1400    dev_kobj = kobject_create_and_add("dev", NULL);  
  7. 1401    if (!dev_kobj)  
  8. 1402        goto dev_kobj_err;  
  9. 1403    sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);  
  10. 1404    if (!sysfs_dev_block_kobj)  
  11. 1405        goto block_kobj_err;  
  12. 1406    sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);  
  13. 1407    if (!sysfs_dev_char_kobj)  
  14. 1408        goto char_kobj_err;  
  15. 1409  
  16. 1410    return 0;  
  17. 1411  
  18. 1412 char_kobj_err:  
  19. 1413    kobject_put(sysfs_dev_block_kobj);  
  20. 1414 block_kobj_err:  
  21. 1415    kobject_put(dev_kobj);  
  22. 1416 dev_kobj_err:  
  23. 1417    kset_unregister(devices_kset);  
  24. 1418    return -ENOMEM;  
  25. 1419}  
1395int __init devices_init(void)
1396{
1397    devices_kset = kset_create_and_add("devices", &device_uevent_ops, NULL);
1398    if (!devices_kset)
1399        return -ENOMEM;
1400    dev_kobj = kobject_create_and_add("dev", NULL);
1401    if (!dev_kobj)
1402        goto dev_kobj_err;
1403    sysfs_dev_block_kobj = kobject_create_and_add("block", dev_kobj);
1404    if (!sysfs_dev_block_kobj)
1405        goto block_kobj_err;
1406    sysfs_dev_char_kobj = kobject_create_and_add("char", dev_kobj);
1407    if (!sysfs_dev_char_kobj)
1408        goto char_kobj_err;
1409
1410    return 0;
1411
1412 char_kobj_err:
1413    kobject_put(sysfs_dev_block_kobj);
1414 block_kobj_err:
1415    kobject_put(dev_kobj);
1416 dev_kobj_err:
1417    kset_unregister(devices_kset);
1418    return -ENOMEM;
1419}


1397行,创建devices_kset,它是所有device的集合,它的名字是“devices”,对应/sys/devices目录。
1400行,创建dev_kobj,它的名字是“dev”,对应/sys/dev目录。
1403行,创建sysfs_dev_block_kobj,它的名字是“block”,对应/sys/dev/block目录。
1406行,创建sysfs_dev_char_kobj,它的名字是“char”,对应/sys/dev/char目录。
 
三、device的注册
注册一个device是通过调用device_register函数完成的,该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 1147/** 
  2. 1148 * device_register - register a device with the system. 
  3. 1149 * @dev: pointer to the device structure 
  4. 1150 * 
  5. 1151 * This happens in two clean steps - initialize the device 
  6. 1152 * and add it to the system. The two steps can be called 
  7. 1153 * separately, but this is the easiest and most common. 
  8. 1154 * I.e. you should only call the two helpers separately if 
  9. 1155 * have a clearly defined need to use and refcount the device 
  10. 1156 * before it is added to the hierarchy. 
  11. 1157 * 
  12. 1158 * For more information, see the kerneldoc for device_initialize() 
  13. 1159 * and device_add(). 
  14. 1160 * 
  15. 1161 * NOTE: _Never_ directly free @dev after calling this function, even 
  16. 1162 * if it returned an error! Always use put_device() to give up the 
  17. 1163 * reference initialized in this function instead. 
  18. 1164 */  
  19. 1165int device_register(struct device *dev)  
  20. 1166{  
  21. 1167    device_initialize(dev);  
  22. 1168    return device_add(dev);  
  23. 1169}  
1147/**
1148 * device_register - register a device with the system.
1149 * @dev: pointer to the device structure
1150 *
1151 * This happens in two clean steps - initialize the device
1152 * and add it to the system. The two steps can be called
1153 * separately, but this is the easiest and most common.
1154 * I.e. you should only call the two helpers separately if
1155 * have a clearly defined need to use and refcount the device
1156 * before it is added to the hierarchy.
1157 *
1158 * For more information, see the kerneldoc for device_initialize()
1159 * and device_add().
1160 *
1161 * NOTE: _Never_ directly free @dev after calling this function, even
1162 * if it returned an error! Always use put_device() to give up the
1163 * reference initialized in this function instead.
1164 */
1165int device_register(struct device *dev)
1166{
1167    device_initialize(dev);
1168    return device_add(dev);
1169}


1167行,调用device_initialize函数对device结构体进行初始化。该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 675/** 
  2. 676 * device_initialize - init device structure. 
  3. 677 * @dev: device. 
  4. 678 * 
  5. 679 * This prepares the device for use by other layers by initializing 
  6. 680 * its fields. 
  7. 681 * It is the first half of device_register(), if called by 
  8. 682 * that function, though it can also be called separately, so one 
  9. 683 * may use @dev's fields. In particular, get_device()/put_device() 
  10. 684 * may be used for reference counting of @dev after calling this 
  11. 685 * function. 
  12. 686 * 
  13. 687 * All fields in @dev must be initialized by the caller to 0, except 
  14. 688 * for those explicitly set to some other value.  The simplest 
  15. 689 * approach is to use kzalloc() to allocate the structure containing 
  16. 690 * @dev. 
  17. 691 * 
  18. 692 * NOTE: Use put_device() to give up your reference instead of freeing 
  19. 693 * @dev directly once you have called this function. 
  20. 694 */  
  21. 695void device_initialize(struct device *dev)  
  22. 696{  
  23. 697    dev->kobj.kset = devices_kset;  
  24. 698    kobject_init(&dev->kobj, &device_ktype);  
  25. 699    INIT_LIST_HEAD(&dev->dma_pools);  
  26. 700    mutex_init(&dev->mutex);  
  27. 701    lockdep_set_novalidate_class(&dev->mutex);  
  28. 702    spin_lock_init(&dev->devres_lock);  
  29. 703    INIT_LIST_HEAD(&dev->devres_head);  
  30. 704    device_pm_init(dev);  
  31. 705    set_dev_node(dev, -1);  
  32. 706}  
 675/**
 676 * device_initialize - init device structure.
 677 * @dev: device.
 678 *
 679 * This prepares the device for use by other layers by initializing
 680 * its fields.
 681 * It is the first half of device_register(), if called by
 682 * that function, though it can also be called separately, so one
 683 * may use @dev's fields. In particular, get_device()/put_device()
 684 * may be used for reference counting of @dev after calling this
 685 * function.
 686 *
 687 * All fields in @dev must be initialized by the caller to 0, except
 688 * for those explicitly set to some other value.  The simplest
 689 * approach is to use kzalloc() to allocate the structure containing
 690 * @dev.
 691 *
 692 * NOTE: Use put_device() to give up your reference instead of freeing
 693 * @dev directly once you have called this function.
 694 */
 695void device_initialize(struct device *dev)
 696{
 697    dev->kobj.kset = devices_kset;
 698    kobject_init(&dev->kobj, &device_ktype);
 699    INIT_LIST_HEAD(&dev->dma_pools);
 700    mutex_init(&dev->mutex);
 701    lockdep_set_novalidate_class(&dev->mutex);
 702    spin_lock_init(&dev->devres_lock);
 703    INIT_LIST_HEAD(&dev->devres_head);
 704    device_pm_init(dev);
 705    set_dev_node(dev, -1);
 706}


该函数对device结构体的部分成员进行了初始化。
1168行,调用device_add函数,将device添加到系统中,该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1.  980/** 
  2.  981 * device_add - add device to device hierarchy. 
  3.  982 * @dev: device. 
  4.  983 * 
  5.  984 * This is part 2 of device_register(), though may be called 
  6.  985 * separately _iff_ device_initialize() has been called separately. 
  7.  986 * 
  8.  987 * This adds @dev to the kobject hierarchy via kobject_add(), adds it 
  9.  988 * to the global and sibling lists for the device, then 
  10.  989 * adds it to the other relevant subsystems of the driver model. 
  11.  990 * 
  12.  991 * Do not call this routine or device_register() more than once for 
  13.  992 * any device structure.  The driver model core is not designed to work 
  14.  993 * with devices that get unregistered and then spring back to life. 
  15.  994 * (Among other things, it's very hard to guarantee that all references 
  16.  995 * to the previous incarnation of @dev have been dropped.)  Allocate 
  17.  996 * and register a fresh new struct device instead. 
  18.  997 * 
  19.  998 * NOTE: _Never_ directly free @dev after calling this function, even 
  20.  999 * if it returned an error! Always use put_device() to give up your 
  21. 1000 * reference instead. 
  22. 1001 */  
  23. 1002int device_add(struct device *dev)  
  24. 1003{  
  25. 1004    struct device *parent = NULL;  
  26. 1005    struct kobject *kobj;  
  27. 1006    struct class_interface *class_intf;  
  28. 1007    int error = -EINVAL;  
  29. 1008  
  30. 1009    dev = get_device(dev);  
  31. 1010    if (!dev)  
  32. 1011        goto done;  
  33. 1012  
  34. 1013    if (!dev->p) {  
  35. 1014        error = device_private_init(dev);  
  36. 1015        if (error)  
  37. 1016            goto done;  
  38. 1017    }  
  39. 1018  
  40. 1019    /* 
  41. 1020     * for statically allocated devices, which should all be converted 
  42. 1021     * some day, we need to initialize the name. We prevent reading back 
  43. 1022     * the name, and force the use of dev_name() 
  44. 1023     */  
  45. 1024    if (dev->init_name) {  
  46. 1025        dev_set_name(dev, "%s", dev->init_name);  
  47. 1026        dev->init_name = NULL;  
  48. 1027    }  
  49. 1028  
  50. 1029    /* subsystems can specify simple device enumeration */  
  51. 1030    if (!dev_name(dev) && dev->bus && dev->bus->dev_name)  
  52. 1031        dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);  
  53. 1032  
  54. 1033    if (!dev_name(dev)) {  
  55. 1034        error = -EINVAL;  
  56. 1035        goto name_error;  
  57. 1036    }  
  58. 1037  
  59. 1038    pr_debug("device: '%s': %s\n", dev_name(dev), __func__);  
  60. 1039  
  61. 1040    parent = get_device(dev->parent);  
  62. 1041    kobj = get_device_parent(dev, parent);  
  63. 1042    if (kobj)  
  64. 1043        dev->kobj.parent = kobj;  
  65. 1044  
  66. 1045    /* use parent numa_node */  
  67. 1046    if (parent)  
  68. 1047        set_dev_node(dev, dev_to_node(parent));  
  69. 1048  
  70. 1049    /* first, register with generic layer. */  
  71. 1050    /* we require the name to be set before, and pass NULL */  
  72. 1051    error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);  
  73. 1052    if (error)  
  74. 1053        goto Error;  
  75. 1054  
  76. 1055    /* notify platform of device entry */  
  77. 1056    if (platform_notify)  
  78. 1057        platform_notify(dev);  
  79. 1058  
  80. 1059    error = device_create_file(dev, &uevent_attr);  
  81. 1060    if (error)  
  82. 1061        goto attrError;  
  83. 1062  
  84. 1063    if (MAJOR(dev->devt)) {  
  85. 1064        error = device_create_file(dev, &devt_attr);  
  86. 1065        if (error)  
  87. 1066            goto ueventattrError;  
  88. 1067  
  89. 1068        error = device_create_sys_dev_entry(dev);  
  90. 1069        if (error)  
  91. 1070            goto devtattrError;  
  92. 1071  
  93. 1072        devtmpfs_create_node(dev);  
  94. 1073    }  
  95. 1074  
  96. 1075    error = device_add_class_symlinks(dev);  
  97. 1076    if (error)  
  98. 1077        goto SymlinkError;  
  99. 1078    error = device_add_attrs(dev);  
  100. 1079    if (error)  
  101. 1080        goto AttrsError;  
  102. 1081    error = bus_add_device(dev);  
  103. 1082    if (error)  
  104. 1083        goto BusError;  
  105. 1084    error = dpm_sysfs_add(dev);  
  106. 1085    if (error)  
  107. 1086        goto DPMError;  
  108. 1087    device_pm_add(dev);  
  109. 1088  
  110. 1089    /* Notify clients of device addition.  This call must come 
  111. 1090     * after dpm_sysfs_add() and before kobject_uevent(). 
  112. 1091     */  
  113. 1092    if (dev->bus)  
  114. 1093        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,  
  115. 1094                         BUS_NOTIFY_ADD_DEVICE, dev);  
  116. 1095  
  117. 1096    kobject_uevent(&dev->kobj, KOBJ_ADD);  
  118. 1097    bus_probe_device(dev);  
  119. 1098    if (parent)  
  120. 1099        klist_add_tail(&dev->p->knode_parent,  
  121. 1100                   &parent->p->klist_children);  
  122. 1101  
  123. 1102    if (dev->class) {  
  124. 1103        mutex_lock(&dev->class->p->mutex);  
  125. 1104        /* tie the class to the device */  
  126. 1105        klist_add_tail(&dev->knode_class,  
  127. 1106                   &dev->class->p->klist_devices);  
  128. 1107  
  129. 1108        /* notify any interfaces that the device is here */  
  130. 1109        list_for_each_entry(class_intf,  
  131. 1110                    &dev->class->p->interfaces, node)  
  132. 1111            if (class_intf->add_dev)  
  133. 1112                class_intf->add_dev(dev, class_intf);  
  134. 1113        mutex_unlock(&dev->class->p->mutex);  
  135. 1114    }  
  136. 1115done:  
  137. 1116    put_device(dev);  
  138. 1117    return error;  
  139. 1118 DPMError:  
  140. 1119    bus_remove_device(dev);  
  141. 1120 BusError:  
  142. 1121    device_remove_attrs(dev);  
  143. 1122 AttrsError:  
  144. 1123    device_remove_class_symlinks(dev);  
  145. 1124 SymlinkError:  
  146. 1125    if (MAJOR(dev->devt))  
  147. 1126        devtmpfs_delete_node(dev);  
  148. 1127    if (MAJOR(dev->devt))  
  149. 1128        device_remove_sys_dev_entry(dev);  
  150. 1129 devtattrError:  
  151. 1130    if (MAJOR(dev->devt))  
  152. 1131        device_remove_file(dev, &devt_attr);  
  153. 1132 ueventattrError:  
  154. 1133    device_remove_file(dev, &uevent_attr);  
  155. 1134 attrError:  
  156. 1135    kobject_uevent(&dev->kobj, KOBJ_REMOVE);  
  157. 1136    kobject_del(&dev->kobj);  
  158. 1137 Error:  
  159. 1138    cleanup_device_parent(dev);  
  160. 1139    if (parent)  
  161. 1140        put_device(parent);  
  162. 1141name_error:  
  163. 1142    kfree(dev->p);  
  164. 1143    dev->p = NULL;  
  165. 1144    goto done;  
  166. 1145}  
 980/**
 981 * device_add - add device to device hierarchy.
 982 * @dev: device.
 983 *
 984 * This is part 2 of device_register(), though may be called
 985 * separately _iff_ device_initialize() has been called separately.
 986 *
 987 * This adds @dev to the kobject hierarchy via kobject_add(), adds it
 988 * to the global and sibling lists for the device, then
 989 * adds it to the other relevant subsystems of the driver model.
 990 *
 991 * Do not call this routine or device_register() more than once for
 992 * any device structure.  The driver model core is not designed to work
 993 * with devices that get unregistered and then spring back to life.
 994 * (Among other things, it's very hard to guarantee that all references
 995 * to the previous incarnation of @dev have been dropped.)  Allocate
 996 * and register a fresh new struct device instead.
 997 *
 998 * NOTE: _Never_ directly free @dev after calling this function, even
 999 * if it returned an error! Always use put_device() to give up your
1000 * reference instead.
1001 */
1002int device_add(struct device *dev)
1003{
1004    struct device *parent = NULL;
1005    struct kobject *kobj;
1006    struct class_interface *class_intf;
1007    int error = -EINVAL;
1008
1009    dev = get_device(dev);
1010    if (!dev)
1011        goto done;
1012
1013    if (!dev->p) {
1014        error = device_private_init(dev);
1015        if (error)
1016            goto done;
1017    }
1018
1019    /*
1020     * for statically allocated devices, which should all be converted
1021     * some day, we need to initialize the name. We prevent reading back
1022     * the name, and force the use of dev_name()
1023     */
1024    if (dev->init_name) {
1025        dev_set_name(dev, "%s", dev->init_name);
1026        dev->init_name = NULL;
1027    }
1028
1029    /* subsystems can specify simple device enumeration */
1030    if (!dev_name(dev) && dev->bus && dev->bus->dev_name)
1031        dev_set_name(dev, "%s%u", dev->bus->dev_name, dev->id);
1032
1033    if (!dev_name(dev)) {
1034        error = -EINVAL;
1035        goto name_error;
1036    }
1037
1038    pr_debug("device: '%s': %s\n", dev_name(dev), __func__);
1039
1040    parent = get_device(dev->parent);
1041    kobj = get_device_parent(dev, parent);
1042    if (kobj)
1043        dev->kobj.parent = kobj;
1044
1045    /* use parent numa_node */
1046    if (parent)
1047        set_dev_node(dev, dev_to_node(parent));
1048
1049    /* first, register with generic layer. */
1050    /* we require the name to be set before, and pass NULL */
1051    error = kobject_add(&dev->kobj, dev->kobj.parent, NULL);
1052    if (error)
1053        goto Error;
1054
1055    /* notify platform of device entry */
1056    if (platform_notify)
1057        platform_notify(dev);
1058
1059    error = device_create_file(dev, &uevent_attr);
1060    if (error)
1061        goto attrError;
1062
1063    if (MAJOR(dev->devt)) {
1064        error = device_create_file(dev, &devt_attr);
1065        if (error)
1066            goto ueventattrError;
1067
1068        error = device_create_sys_dev_entry(dev);
1069        if (error)
1070            goto devtattrError;
1071
1072        devtmpfs_create_node(dev);
1073    }
1074
1075    error = device_add_class_symlinks(dev);
1076    if (error)
1077        goto SymlinkError;
1078    error = device_add_attrs(dev);
1079    if (error)
1080        goto AttrsError;
1081    error = bus_add_device(dev);
1082    if (error)
1083        goto BusError;
1084    error = dpm_sysfs_add(dev);
1085    if (error)
1086        goto DPMError;
1087    device_pm_add(dev);
1088
1089    /* Notify clients of device addition.  This call must come
1090     * after dpm_sysfs_add() and before kobject_uevent().
1091     */
1092    if (dev->bus)
1093        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1094                         BUS_NOTIFY_ADD_DEVICE, dev);
1095
1096    kobject_uevent(&dev->kobj, KOBJ_ADD);
1097    bus_probe_device(dev);
1098    if (parent)
1099        klist_add_tail(&dev->p->knode_parent,
1100                   &parent->p->klist_children);
1101
1102    if (dev->class) {
1103        mutex_lock(&dev->class->p->mutex);
1104        /* tie the class to the device */
1105        klist_add_tail(&dev->knode_class,
1106                   &dev->class->p->klist_devices);
1107
1108        /* notify any interfaces that the device is here */
1109        list_for_each_entry(class_intf,
1110                    &dev->class->p->interfaces, node)
1111            if (class_intf->add_dev)
1112                class_intf->add_dev(dev, class_intf);
1113        mutex_unlock(&dev->class->p->mutex);
1114    }
1115done:
1116    put_device(dev);
1117    return error;
1118 DPMError:
1119    bus_remove_device(dev);
1120 BusError:
1121    device_remove_attrs(dev);
1122 AttrsError:
1123    device_remove_class_symlinks(dev);
1124 SymlinkError:
1125    if (MAJOR(dev->devt))
1126        devtmpfs_delete_node(dev);
1127    if (MAJOR(dev->devt))
1128        device_remove_sys_dev_entry(dev);
1129 devtattrError:
1130    if (MAJOR(dev->devt))
1131        device_remove_file(dev, &devt_attr);
1132 ueventattrError:
1133    device_remove_file(dev, &uevent_attr);
1134 attrError:
1135    kobject_uevent(&dev->kobj, KOBJ_REMOVE);
1136    kobject_del(&dev->kobj);
1137 Error:
1138    cleanup_device_parent(dev);
1139    if (parent)
1140        put_device(parent);
1141name_error:
1142    kfree(dev->p);
1143    dev->p = NULL;
1144    goto done;
1145}


1009行,调用get_device增加device(其实是device.kobj)的引用计数。
1013-1017行,如果dev->p是NULL,则调用device_private_init函数为dev->p分配内存空间并进行初始化,该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 968int device_private_init(struct device *dev)  
  2. 969{  
  3. 970    dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);  
  4. 971    if (!dev->p)  
  5. 972        return -ENOMEM;  
  6. 973    dev->p->device = dev;  
  7. 974    klist_init(&dev->p->klist_children, klist_children_get,  
  8. 975           klist_children_put);  
  9. 976    INIT_LIST_HEAD(&dev->p->deferred_probe);  
  10. 977    return 0;  
  11. 978}  
 968int device_private_init(struct device *dev)
 969{
 970    dev->p = kzalloc(sizeof(*dev->p), GFP_KERNEL);
 971    if (!dev->p)
 972        return -ENOMEM;
 973    dev->p->device = dev;
 974    klist_init(&dev->p->klist_children, klist_children_get,
 975           klist_children_put);
 976    INIT_LIST_HEAD(&dev->p->deferred_probe);
 977    return 0;
 978}


1024-1027行,如果dev->init_name不为空,则调用dev_set_name设置device的名字(其实是设置device.kobj的名字)。
1030-1031行,如果device的名字为空,并且dev->bus->dev_name不为空,则用dev->bus->dev_name作为device的名字。
1033-1036行,如果device的名字仍然为空,则出错退出。
1040行,取得dev->parent,保存在parent变量中。
1041行,调用get_device_parent函数,该函数的作用是取得dev->kobj.parent,即device对应的kobject的parent kobject。该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 769static struct kobject *get_device_parent(struct device *dev,  
  2. 770                     struct device *parent)  
  3. 771{  
  4. 772    if (dev->class) {  
  5. 773        static DEFINE_MUTEX(gdp_mutex);  
  6. 774        struct kobject *kobj = NULL;  
  7. 775        struct kobject *parent_kobj;  
  8. 776        struct kobject *k;  
  9. 777  
  10. 778#ifdef CONFIG_BLOCK  
  11. 779        /* block disks show up in /sys/block */  
  12. 780        if (sysfs_deprecated && dev->class == &block_class) {  
  13. 781            if (parent && parent->class == &block_class)  
  14. 782                return &parent->kobj;  
  15. 783            return &block_class.p->subsys.kobj;  
  16. 784        }  
  17. 785#endif  
  18. 786  
  19. 787        /* 
  20. 788         * If we have no parent, we live in "virtual". 
  21. 789         * Class-devices with a non class-device as parent, live 
  22. 790         * in a "glue" directory to prevent namespace collisions. 
  23. 791         */  
  24. 792        if (parent == NULL)  
  25. 793            parent_kobj = virtual_device_parent(dev);  
  26. 794        else if (parent->class && !dev->class->ns_type)  
  27. 795            return &parent->kobj;  
  28. 796        else  
  29. 797            parent_kobj = &parent->kobj;  
  30. 798  
  31. 799        mutex_lock(&gdp_mutex);  
  32. 800  
  33. 801        /* find our class-directory at the parent and reference it */  
  34. 802        spin_lock(&dev->class->p->glue_dirs.list_lock);  
  35. 803        list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)  
  36. 804            if (k->parent == parent_kobj) {  
  37. 805                kobj = kobject_get(k);  
  38. 806                break;  
  39. 807            }  
  40. 808        spin_unlock(&dev->class->p->glue_dirs.list_lock);  
  41. 809        if (kobj) {  
  42. 810            mutex_unlock(&gdp_mutex);  
  43. 811            return kobj;  
  44. 812        }  
  45. 813  
  46. 814        /* or create a new class-directory at the parent device */  
  47. 815        k = class_dir_create_and_add(dev->class, parent_kobj);  
  48. 816        /* do not emit an uevent for this simple "glue" directory */  
  49. 817        mutex_unlock(&gdp_mutex);  
  50. 818        return k;  
  51. 819    }  
  52. 820  
  53. 821    /* subsystems can specify a default root directory for their devices */  
  54. 822    if (!parent && dev->bus && dev->bus->dev_root)  
  55. 823        return &dev->bus->dev_root->kobj;  
  56. 824  
  57. 825    if (parent)  
  58. 826        return &parent->kobj;  
  59. 827    return NULL;  
  60. 828}  
 769static struct kobject *get_device_parent(struct device *dev,
 770                     struct device *parent)
 771{
 772    if (dev->class) {
 773        static DEFINE_MUTEX(gdp_mutex);
 774        struct kobject *kobj = NULL;
 775        struct kobject *parent_kobj;
 776        struct kobject *k;
 777
 778#ifdef CONFIG_BLOCK
 779        /* block disks show up in /sys/block */
 780        if (sysfs_deprecated && dev->class == &block_class) {
 781            if (parent && parent->class == &block_class)
 782                return &parent->kobj;
 783            return &block_class.p->subsys.kobj;
 784        }
 785#endif
 786
 787        /*
 788         * If we have no parent, we live in "virtual".
 789         * Class-devices with a non class-device as parent, live
 790         * in a "glue" directory to prevent namespace collisions.
 791         */
 792        if (parent == NULL)
 793            parent_kobj = virtual_device_parent(dev);
 794        else if (parent->class && !dev->class->ns_type)
 795            return &parent->kobj;
 796        else
 797            parent_kobj = &parent->kobj;
 798
 799        mutex_lock(&gdp_mutex);
 800
 801        /* find our class-directory at the parent and reference it */
 802        spin_lock(&dev->class->p->glue_dirs.list_lock);
 803        list_for_each_entry(k, &dev->class->p->glue_dirs.list, entry)
 804            if (k->parent == parent_kobj) {
 805                kobj = kobject_get(k);
 806                break;
 807            }
 808        spin_unlock(&dev->class->p->glue_dirs.list_lock);
 809        if (kobj) {
 810            mutex_unlock(&gdp_mutex);
 811            return kobj;
 812        }
 813
 814        /* or create a new class-directory at the parent device */
 815        k = class_dir_create_and_add(dev->class, parent_kobj);
 816        /* do not emit an uevent for this simple "glue" directory */
 817        mutex_unlock(&gdp_mutex);
 818        return k;
 819    }
 820
 821    /* subsystems can specify a default root directory for their devices */
 822    if (!parent && dev->bus && dev->bus->dev_root)
 823        return &dev->bus->dev_root->kobj;
 824
 825    if (parent)
 826        return &parent->kobj;
 827    return NULL;
 828}


这个函数功能如下:
1、如果dev->class不为NULL,并且dev->parent为NULL
这种情况下系统将为dev->kobj.parent建立一个虚拟上层对象“virtual”,如此,将dev对象加入系统将会在/sys/devices/virtual中产生一个新的目录/sys/devices/virtual/dev->init_name。
2、如果dev->class不为NULL,并且dev->parent也不为NULL
这种情况下,要看dev->parent->class是否为空,如果不为空,则dev->kobj.parent为dev->parent->kobj,即dev父设备的内嵌kobject。
如果dev->parent->class为空,则内核需要在dev->class->p->class_dirs.list中查找是否有满足如下条件的kobject对象k:k->parent等于&parent->kobj。如果找到满足条件的k,那么dev->kobj.parent就是dev->parent->kobj,即dev父设备的内嵌kobject。如果没有满足条件的k,那么就需要调用class_dir_create_and_add函数,重新生成一个kobject对象作为dev->kobj的父kobject。
3、如果dev->class为NULL,并且dev->parent不为NULL
这种情况下,dev->kobj.parent为dev->parent->kobj,即dev父设备的内嵌kobject。也就是说dev对象对应的目录将建立在dev->parent->kobj对应的目录下。
4、如果dev->class为NULL,并且dev->parent也为NULL
这种情况下,如果dev->bus->dev_root不为NULL,则指定dev->kobj.parent为dev->bus->dev_root->kobj。
我们回到device_add函数:
1042-1043行,如果通过get_device_parent函数取得了父kobject,则用它来设置dev->kobj.parent。
1051行,调用kobject_add将dev->kobj注册到sysfs系统中。
1059行,调用device_create_file创建uevent属性文件。uevent_attr定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 403static struct device_attribute uevent_attr =  
  2. 404    __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);  
 403static struct device_attribute uevent_attr =
 404    __ATTR(uevent, S_IRUGO | S_IWUSR, show_uevent, store_uevent);


device_attribute结构体定义在include/linux/device.h文件中,其内容如下:
 
 
  1. 478/* interface for exporting device attributes */  
  2. 479struct device_attribute {  
  3. 480    struct attribute    attr;  
  4. 481    ssize_t (*show)(struct device *dev, struct device_attribute *attr,  
  5. 482            char *buf);  
  6. 483    ssize_t (*store)(struct device *dev, struct device_attribute *attr,  
  7. 484             const char *buf, size_t count);  
  8. 485};  
 478/* interface for exporting device attributes */
 479struct device_attribute {
 480    struct attribute    attr;
 481    ssize_t (*show)(struct device *dev, struct device_attribute *attr,
 482            char *buf);
 483    ssize_t (*store)(struct device *dev, struct device_attribute *attr,
 484             const char *buf, size_t count);
 485};


device_create_file定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 563/** 
  2. 564 * device_create_file - create sysfs attribute file for device. 
  3. 565 * @dev: device. 
  4. 566 * @attr: device attribute descriptor. 
  5. 567 */  
  6. 568int device_create_file(struct device *dev,  
  7. 569               const struct device_attribute *attr)  
  8. 570{  
  9. 571    int error = 0;  
  10. 572  
  11. 573    if (dev) {  
  12. 574        WARN(((attr->attr.mode & S_IWUGO) && !attr->store),  
  13. 575            "Attribute %s: write permission without 'store'\n",  
  14. 576            attr->attr.name);  
  15. 577        WARN(((attr->attr.mode & S_IRUGO) && !attr->show),  
  16. 578            "Attribute %s: read permission without 'show'\n",  
  17. 579            attr->attr.name);  
  18. 580        error = sysfs_create_file(&dev->kobj, &attr->attr);  
  19. 581    }  
  20. 582  
  21. 583    return error;  
  22. 584}  
 563/**
 564 * device_create_file - create sysfs attribute file for device.
 565 * @dev: device.
 566 * @attr: device attribute descriptor.
 567 */
 568int device_create_file(struct device *dev,
 569               const struct device_attribute *attr)
 570{
 571    int error = 0;
 572
 573    if (dev) {
 574        WARN(((attr->attr.mode & S_IWUGO) && !attr->store),
 575            "Attribute %s: write permission without 'store'\n",
 576            attr->attr.name);
 577        WARN(((attr->attr.mode & S_IRUGO) && !attr->show),
 578            "Attribute %s: read permission without 'show'\n",
 579            attr->attr.name);
 580        error = sysfs_create_file(&dev->kobj, &attr->attr);
 581    }
 582
 583    return error;
 584}


1063行,通过MAJOR(dev->devt)取得设备对应的主设备号。
1064行,如果设备的主设备号不为0,调用device_create_file创建dev属性文件。devt_attr定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 557static struct device_attribute devt_attr =  
  2. 558    __ATTR(dev, S_IRUGO, show_dev, NULL);  
 557static struct device_attribute devt_attr =
 558    __ATTR(dev, S_IRUGO, show_dev, NULL);


1068行,调用device_create_sys_dev_entry(dev)函数,该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 943static int device_create_sys_dev_entry(struct device *dev)  
  2. 944{  
  3. 945    struct kobject *kobj = device_to_dev_kobj(dev);  
  4. 946    int error = 0;  
  5. 947    char devt_str[15];  
  6. 948  
  7. 949    if (kobj) {  
  8. 950        format_dev_t(devt_str, dev->devt);  
  9. 951        error = sysfs_create_link(kobj, &dev->kobj, devt_str);  
  10. 952    }  
  11. 953  
  12. 954    return error;  
  13. 955}  
 943static int device_create_sys_dev_entry(struct device *dev)
 944{
 945    struct kobject *kobj = device_to_dev_kobj(dev);
 946    int error = 0;
 947    char devt_str[15];
 948
 949    if (kobj) {
 950        format_dev_t(devt_str, dev->devt);
 951        error = sysfs_create_link(kobj, &dev->kobj, devt_str);
 952    }
 953
 954    return error;
 955}


945行,调用device_to_dev_kobj(dev),该函数定义在drivers/base/core.c文件中,其内容如下:
 
 
  1. 920/** 
  2. 921 * device_to_dev_kobj - select a /sys/dev/ directory for the device 
  3. 922 * @dev: device 
  4. 923 * 
  5. 924 * By default we select char/ for new entries.  Setting class->dev_obj 
  6. 925 * to NULL prevents an entry from being created.  class->dev_kobj must 
  7. 926 * be set (or cleared) before any devices are registered to the class 
  8. 927 * otherwise device_create_sys_dev_entry() and 
  9. 928 * device_remove_sys_dev_entry() will disagree about the presence of 
  10. 929 * the link. 
  11. 930 */  
  12. 931static struct kobject *device_to_dev_kobj(struct device *dev)  
  13. 932{  
  14. 933    struct kobject *kobj;  
  15. 934  
  16. 935    if (dev->class)  
  17. 936        kobj = dev->class->dev_kobj;  
  18. 937    else  
  19. 938        kobj = sysfs_dev_char_kobj;  
  20. 939  
  21. 940    return kobj;  
  22. 941}  
 920/**
 921 * device_to_dev_kobj - select a /sys/dev/ directory for the device
 922 * @dev: device
 923 *
 924 * By default we select char/ for new entries.  Setting class->dev_obj
 925 * to NULL prevents an entry from being created.  class->dev_kobj must
 926 * be set (or cleared) before any devices are registered to the class
 927 * otherwise device_create_sys_dev_entry() and
 928 * device_remove_sys_dev_entry() will disagree about the presence of
 929 * the link.
 930 */
 931static struct kobject *device_to_dev_kobj(struct device *dev)
 932{
 933    struct kobject *kobj;
 934
 935    if (dev->class)
 936        kobj = dev->class->dev_kobj;
 937    else
 938        kobj = sysfs_dev_char_kobj;
 939
 940    return kobj;
 941}


从该函数的注释可以看出,该函数的作用是返回device对应的/sys/dev/下的一个目录。该函数的返回值是一个kobject,一个kobject在sysfs系统中对应一个目录。如果dev->class不为NULL,该函数返回dev->class->dev_kobj;如果dev->class为NULL,该函数返回sysfs_dev_char_kobj,它对应/sys/dev/char目录。
回到device_create_sys_dev_entry函数:
950行,调用format_dev_t宏,该宏定义在include/linux/kdev_t.h文件中,其内容如下:
 
 
  1. 16#define format_dev_t(buffer, dev)                   \  
  2. 17    ({                              \  
  3. 18        sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev));   \  
  4. 19        buffer;                         \  
  5. 20    })  
16#define format_dev_t(buffer, dev)                   \
17    ({                              \
18        sprintf(buffer, "%u:%u", MAJOR(dev), MINOR(dev));   \
19        buffer;                         \
20    })


可以看到,该宏的作用是把“主设备号:次设备号”字符串写入buffer变量中。
951行,调用sysfs_create_link(kobj, &dev->kobj, devt_str)创建一个链接。该函数定义在fs/sysfs/symlink.c文件中,其内容如下:
 
 
  1. 117/** 
  2. 118 *  sysfs_create_link - create symlink between two objects. 
  3. 119 *  @kobj:  object whose directory we're creating the link in. 
  4. 120 *  @target:    object we're pointing to. 
  5. 121 *  @name:      name of the symlink. 
  6. 122 */  
  7. 123int sysfs_create_link(struct kobject *kobj, struct kobject *target,  
  8. 124              const char *name)  
  9. 125{  
  10. 126    return sysfs_do_create_link(kobj, target, name, 1);  
  11. 127}  
117/**
118 *  sysfs_create_link - create symlink between two objects.
119 *  @kobj:  object whose directory we're creating the link in.
120 *  @target:    object we're pointing to.
121 *  @name:      name of the symlink.
122 */
123int sysfs_create_link(struct kobject *kobj, struct kobject *target,
124              const char *name)
125{
126    return sysfs_do_create_link(kobj, target, name, 1);
127}


由注释可以看出,第一个参数是我们将要创建的链接所在的目录,第二个参数是链接指向的源,第三个参数是链接的名字。
回到device_create_sys_dev_entry函数,这里我们举个例子,如果dev的设备号major=20, minor=1,设备名为dev->init_name,那么,
如果dev->class为NULL,则产生的链接文件为/sys/dev/char/20:1,链接指向的文件为/sys/devices/dev->init_name。
如果dev->class不为NULL,则产生的链接文件将位于dev->class->dev_kobj所对应的目录下,链接指向的文件为/sys/devices/virual/dev->init_name。
回到device_add函数:
1072行,如果device主设备号不为0,还会调用devtmpfs_create_node(dev)函数在/dev目录下自动生成一个设备节点。在Linux早期,/dev下的设备节点是通过mknod命令手动添加的,现在通过devtmpfs文件系统,就可以在调用device_register函数注册设备时自动向/dev目录下添加设备节点,该节点的名字就是dev->init_name
1075行,调用device_add_class_symlinks(dev)函数,该函数定义在drivers/base/core.c文件中,创建dev->class相关的链接。这个函数我们不仔细分析了。
1078行,调用device_add_attrs(dev)函数,该函数定义在drivers/base/core.c文件中,该函数创建与dev->class相关的一些属性文件,这个函数我们不仔细分析了。
1081行,调用bus_add_device(dev)函数,该函数定义在drivers/base/bus.c文件中,该函数在/sys/bus/devices目录下创建一个链接文件,指向/sys/devices/dev->init_name。
1097行,调用bus_probe_device(dev)函数为新添加的device探测匹配的device_driver。该函数定义在drivers/base/bus.c文件中,其内容如下:
 
 
  1. 523/** 
  2. 524 * bus_probe_device - probe drivers for a new device 
  3. 525 * @dev: device to probe 
  4. 526 * 
  5. 527 * - Automatically probe for a driver if the bus allows it. 
  6. 528 */  
  7. 529void bus_probe_device(struct device *dev)  
  8. 530{  
  9. 531    struct bus_type *bus = dev->bus;  
  10. 532    struct subsys_interface *sif;  
  11. 533    int ret;  
  12. 534  
  13. 535    if (!bus)  
  14. 536        return;  
  15. 537  
  16. 538    if (bus->p->drivers_autoprobe) {  
  17. 539        ret = device_attach(dev);  
  18. 540        WARN_ON(ret < 0);  
  19. 541    }  
  20. 542  
  21. 543    mutex_lock(&bus->p->mutex);  
  22. 544    list_for_each_entry(sif, &bus->p->interfaces, node)  
  23. 545        if (sif->add_dev)  
  24. 546            sif->add_dev(dev, sif);  
  25. 547    mutex_unlock(&bus->p->mutex);  
  26. 548}  
 523/**
 524 * bus_probe_device - probe drivers for a new device
 525 * @dev: device to probe
 526 *
 527 * - Automatically probe for a driver if the bus allows it.
 528 */
 529void bus_probe_device(struct device *dev)
 530{
 531    struct bus_type *bus = dev->bus;
 532    struct subsys_interface *sif;
 533    int ret;
 534
 535    if (!bus)
 536        return;
 537
 538    if (bus->p->drivers_autoprobe) {
 539        ret = device_attach(dev);
 540        WARN_ON(ret < 0);
 541    }
 542
 543    mutex_lock(&bus->p->mutex);
 544    list_for_each_entry(sif, &bus->p->interfaces, node)
 545        if (sif->add_dev)
 546            sif->add_dev(dev, sif);
 547    mutex_unlock(&bus->p->mutex);
 548}


531行,取得device所依附的bus。
538-539行,如果bus->p->drivers_autoprobe值为1,调用device_attach(dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 
 
  1. 398/** 
  2. 399 * device_attach - try to attach device to a driver. 
  3. 400 * @dev: device. 
  4. 401 * 
  5. 402 * Walk the list of drivers that the bus has and call 
  6. 403 * driver_probe_device() for each pair. If a compatible 
  7. 404 * pair is found, break out and return. 
  8. 405 * 
  9. 406 * Returns 1 if the device was bound to a driver; 
  10. 407 * 0 if no matching driver was found; 
  11. 408 * -ENODEV if the device is not registered. 
  12. 409 * 
  13. 410 * When called for a USB interface, @dev->parent lock must be held. 
  14. 411 */  
  15. 412int device_attach(struct device *dev)  
  16. 413{  
  17. 414    int ret = 0;  
  18. 415  
  19. 416    device_lock(dev);  
  20. 417    if (dev->driver) {  
  21. 418        if (klist_node_attached(&dev->p->knode_driver)) {  
  22. 419            ret = 1;  
  23. 420            goto out_unlock;  
  24. 421        }  
  25. 422        ret = device_bind_driver(dev);  
  26. 423        if (ret == 0)  
  27. 424            ret = 1;  
  28. 425        else {  
  29. 426            dev->driver = NULL;  
  30. 427            ret = 0;  
  31. 428        }  
  32. 429    } else {  
  33. 430        ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);  
  34. 431        pm_request_idle(dev);  
  35. 432    }  
  36. 433out_unlock:  
  37. 434    device_unlock(dev);  
  38. 435    return ret;  
  39. 436}  
398/**
399 * device_attach - try to attach device to a driver.
400 * @dev: device.
401 *
402 * Walk the list of drivers that the bus has and call
403 * driver_probe_device() for each pair. If a compatible
404 * pair is found, break out and return.
405 *
406 * Returns 1 if the device was bound to a driver;
407 * 0 if no matching driver was found;
408 * -ENODEV if the device is not registered.
409 *
410 * When called for a USB interface, @dev->parent lock must be held.
411 */
412int device_attach(struct device *dev)
413{
414    int ret = 0;
415
416    device_lock(dev);
417    if (dev->driver) {
418        if (klist_node_attached(&dev->p->knode_driver)) {
419            ret = 1;
420            goto out_unlock;
421        }
422        ret = device_bind_driver(dev);
423        if (ret == 0)
424            ret = 1;
425        else {
426            dev->driver = NULL;
427            ret = 0;
428        }
429    } else {
430        ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
431        pm_request_idle(dev);
432    }
433out_unlock:
434    device_unlock(dev);
435    return ret;
436}


417-429行,如果dev->driver不为空,表明device已经与相应的device_driver进行了绑定,这种情况下只需要调用device_bind_driver函数在sysfs文件系统中建立device与其device_driver的相互关联。
430行,调用bus_for_each_drv函数,该函数定义在drivers/base/bus.c文件中,其内容如下:
 
 
  1. 417/** 
  2. 418 * bus_for_each_drv - driver iterator 
  3. 419 * @bus: bus we're dealing with. 
  4. 420 * @start: driver to start iterating on. 
  5. 421 * @data: data to pass to the callback. 
  6. 422 * @fn: function to call for each driver. 
  7. 423 * 
  8. 424 * This is nearly identical to the device iterator above. 
  9. 425 * We iterate over each driver that belongs to @bus, and call 
  10. 426 * @fn for each. If @fn returns anything but 0, we break out 
  11. 427 * and return it. If @start is not NULL, we use it as the head 
  12. 428 * of the list. 
  13. 429 * 
  14. 430 * NOTE: we don't return the driver that returns a non-zero 
  15. 431 * value, nor do we leave the reference count incremented for that 
  16. 432 * driver. If the caller needs to know that info, it must set it 
  17. 433 * in the callback. It must also be sure to increment the refcount 
  18. 434 * so it doesn't disappear before returning to the caller. 
  19. 435 */  
  20. 436int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,  
  21. 437             void *data, int (*fn)(struct device_driver *, void *))  
  22. 438{  
  23. 439    struct klist_iter i;  
  24. 440    struct device_driver *drv;  
  25. 441    int error = 0;  
  26. 442  
  27. 443    if (!bus)  
  28. 444        return -EINVAL;  
  29. 445  
  30. 446    klist_iter_init_node(&bus->p->klist_drivers, &i,  
  31. 447                 start ? &start->p->knode_bus : NULL);  
  32. 448    while ((drv = next_driver(&i)) && !error)  
  33. 449        error = fn(drv, data);  
  34. 450    klist_iter_exit(&i);  
  35. 451    return error;  
  36. 452}  
 417/**
 418 * bus_for_each_drv - driver iterator
 419 * @bus: bus we're dealing with.
 420 * @start: driver to start iterating on.
 421 * @data: data to pass to the callback.
 422 * @fn: function to call for each driver.
 423 *
 424 * This is nearly identical to the device iterator above.
 425 * We iterate over each driver that belongs to @bus, and call
 426 * @fn for each. If @fn returns anything but 0, we break out
 427 * and return it. If @start is not NULL, we use it as the head
 428 * of the list.
 429 *
 430 * NOTE: we don't return the driver that returns a non-zero
 431 * value, nor do we leave the reference count incremented for that
 432 * driver. If the caller needs to know that info, it must set it
 433 * in the callback. It must also be sure to increment the refcount
 434 * so it doesn't disappear before returning to the caller.
 435 */
 436int bus_for_each_drv(struct bus_type *bus, struct device_driver *start,
 437             void *data, int (*fn)(struct device_driver *, void *))
 438{
 439    struct klist_iter i;
 440    struct device_driver *drv;
 441    int error = 0;
 442
 443    if (!bus)
 444        return -EINVAL;
 445
 446    klist_iter_init_node(&bus->p->klist_drivers, &i,
 447                 start ? &start->p->knode_bus : NULL);
 448    while ((drv = next_driver(&i)) && !error)
 449        error = fn(drv, data);
 450    klist_iter_exit(&i);
 451    return error;
 452}


448-449行,这个while循环依次遍历bus->p->klist_drivers中的所有device_driver,对于每个device_driver,调用fn(drv, data)函数,看该device_driver是否支持该device。如果匹配成功,fn函数返回非0值。参数传递过来的fn函数是__device_attach,该函数定义在drivers/base/dd.c文件中,其内容如下:
 
 
  1. 388static int __device_attach(struct device_driver *drv, void *data)  
  2. 389{  
  3. 390    struct device *dev = data;  
  4. 391  
  5. 392    if (!driver_match_device(drv, dev))  
  6. 393        return 0;  
  7. 394  
  8. 395    return driver_probe_device(drv, dev);  
  9. 396}  
388static int __device_attach(struct device_driver *drv, void *data)
389{
390    struct device *dev = data;
391
392    if (!driver_match_device(drv, dev))
393        return 0;
394
395    return driver_probe_device(drv, dev);
396}


392行,调用driver_match_device(drv, dev)函数,检查device_driver是否支持该device,该函数定义在drivers/base/base.h文件中,其内容如下:
 
 
  1. 116static inline int driver_match_device(struct device_driver *drv,  
  2. 117                      struct device *dev)  
  3. 118{  
  4. 119    return drv->bus->match ? drv->bus->match(dev, drv) : 1;  
  5. 120}  
116static inline int driver_match_device(struct device_driver *drv,
117                      struct device *dev)
118{
119    return drv->bus->match ? drv->bus->match(dev, drv) : 1;
120}


如果定义了drv->bus->match函数,则调用该函数,否则返回1。
回到__device_attach函数:
395行,调用driver_probe_device(drv, dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 
 
  1. 360/** 
  2. 361 * driver_probe_device - attempt to bind device & driver together 
  3. 362 * @drv: driver to bind a device to 
  4. 363 * @dev: device to try to bind to the driver 
  5. 364 * 
  6. 365 * This function returns -ENODEV if the device is not registered, 
  7. 366 * 1 if the device is bound successfully and 0 otherwise. 
  8. 367 * 
  9. 368 * This function must be called with @dev lock held.  When called for a 
  10. 369 * USB interface, @dev->parent lock must be held as well. 
  11. 370 */  
  12. 371int driver_probe_device(struct device_driver *drv, struct device *dev)  
  13. 372{  
  14. 373    int ret = 0;  
  15. 374  
  16. 375    if (!device_is_registered(dev))  
  17. 376        return -ENODEV;  
  18. 377  
  19. 378    pr_debug("bus: '%s': %s: matched device %s with driver %s\n",  
  20. 379         drv->bus->name, __func__, dev_name(dev), drv->name);  
  21. 380  
  22. 381    pm_runtime_barrier(dev);  
  23. 382    ret = really_probe(dev, drv);  
  24. 383    pm_request_idle(dev);  
  25. 384  
  26. 385    return ret;  
  27. 386}  
360/**
361 * driver_probe_device - attempt to bind device & driver together
362 * @drv: driver to bind a device to
363 * @dev: device to try to bind to the driver
364 *
365 * This function returns -ENODEV if the device is not registered,
366 * 1 if the device is bound successfully and 0 otherwise.
367 *
368 * This function must be called with @dev lock held.  When called for a
369 * USB interface, @dev->parent lock must be held as well.
370 */
371int driver_probe_device(struct device_driver *drv, struct device *dev)
372{
373    int ret = 0;
374
375    if (!device_is_registered(dev))
376        return -ENODEV;
377
378    pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
379         drv->bus->name, __func__, dev_name(dev), drv->name);
380
381    pm_runtime_barrier(dev);
382    ret = really_probe(dev, drv);
383    pm_request_idle(dev);
384
385    return ret;
386}


375行,调用device_is_registered函数判断device是否已经在sysfs系统中注册过,如果还没有注册过,则返回ENODEV,退出。该函数定义在include/linux/device.h文件中,其内容如下:
 
 
  1. 787static inline int device_is_registered(struct device *dev)  
  2. 788{  
  3. 789    return dev->kobj.state_in_sysfs;  
  4. 790}  
 787static inline int device_is_registered(struct device *dev)
 788{
 789    return dev->kobj.state_in_sysfs;
 790}


382行,调用really_probe(dev, drv)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 
 
  1. 265static int really_probe(struct device *dev, struct device_driver *drv)  
  2. 266{  
  3. 267    int ret = 0;  
  4. 268  
  5. 269    atomic_inc(&probe_count);  
  6. 270    pr_debug("bus: '%s': %s: probing driver %s with device %s\n",  
  7. 271         drv->bus->name, __func__, drv->name, dev_name(dev));  
  8. 272    WARN_ON(!list_empty(&dev->devres_head));  
  9. 273  
  10. 274    dev->driver = drv;  
  11. 275  
  12. 276    /* If using pinctrl, bind pins now before probing */  
  13. 277    ret = pinctrl_bind_pins(dev);  
  14. 278    if (ret)  
  15. 279        goto probe_failed;  
  16. 280  
  17. 281    if (driver_sysfs_add(dev)) {  
  18. 282        printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",  
  19. 283            __func__, dev_name(dev));  
  20. 284        goto probe_failed;  
  21. 285    }  
  22. 286  
  23. 287    if (dev->bus->probe) {  
  24. 288        ret = dev->bus->probe(dev);  
  25. 289        if (ret)  
  26. 290            goto probe_failed;  
  27. 291    } else if (drv->probe) {  
  28. 292        ret = drv->probe(dev);  
  29. 293        if (ret)  
  30. 294            goto probe_failed;  
  31. 295    }  
  32. 296  
  33. 297    driver_bound(dev);  
  34. 298    ret = 1;  
  35. 299    pr_debug("bus: '%s': %s: bound device %s to driver %s\n",  
  36. 300         drv->bus->name, __func__, dev_name(dev), drv->name);  
  37. 301    goto done;  
  38. 302  
  39. 303probe_failed:  
  40. 304    devres_release_all(dev);  
  41. 305    driver_sysfs_remove(dev);  
  42. 306    dev->driver = NULL;  
  43. 307    dev_set_drvdata(dev, NULL);  
  44. 308  
  45. 309    if (ret == -EPROBE_DEFER) {  
  46. 310        /* Driver requested deferred probing */  
  47. 311        dev_info(dev, "Driver %s requests probe deferral\n", drv->name);  
  48. 312        driver_deferred_probe_add(dev);  
  49. 313    } else if (ret != -ENODEV && ret != -ENXIO) {  
  50. 314        /* driver matched but the probe failed */  
  51. 315        printk(KERN_WARNING  
  52. 316               "%s: probe of %s failed with error %d\n",  
  53. 317               drv->name, dev_name(dev), ret);  
  54. 318    } else {  
  55. 319        pr_debug("%s: probe of %s rejects match %d\n",  
  56. 320               drv->name, dev_name(dev), ret);  
  57. 321    }  
  58. 322    /* 
  59. 323     * Ignore errors returned by ->probe so that the next driver can try 
  60. 324     * its luck. 
  61. 325     */  
  62. 326    ret = 0;  
  63. 327done:  
  64. 328    atomic_dec(&probe_count);  
  65. 329    wake_up(&probe_waitqueue);  
  66. 330    return ret;  
  67. 331}  
265static int really_probe(struct device *dev, struct device_driver *drv)
266{
267    int ret = 0;
268
269    atomic_inc(&probe_count);
270    pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
271         drv->bus->name, __func__, drv->name, dev_name(dev));
272    WARN_ON(!list_empty(&dev->devres_head));
273
274    dev->driver = drv;
275
276    /* If using pinctrl, bind pins now before probing */
277    ret = pinctrl_bind_pins(dev);
278    if (ret)
279        goto probe_failed;
280
281    if (driver_sysfs_add(dev)) {
282        printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
283            __func__, dev_name(dev));
284        goto probe_failed;
285    }
286
287    if (dev->bus->probe) {
288        ret = dev->bus->probe(dev);
289        if (ret)
290            goto probe_failed;
291    } else if (drv->probe) {
292        ret = drv->probe(dev);
293        if (ret)
294            goto probe_failed;
295    }
296
297    driver_bound(dev);
298    ret = 1;
299    pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
300         drv->bus->name, __func__, dev_name(dev), drv->name);
301    goto done;
302
303probe_failed:
304    devres_release_all(dev);
305    driver_sysfs_remove(dev);
306    dev->driver = NULL;
307    dev_set_drvdata(dev, NULL);
308
309    if (ret == -EPROBE_DEFER) {
310        /* Driver requested deferred probing */
311        dev_info(dev, "Driver %s requests probe deferral\n", drv->name);
312        driver_deferred_probe_add(dev);
313    } else if (ret != -ENODEV && ret != -ENXIO) {
314        /* driver matched but the probe failed */
315        printk(KERN_WARNING
316               "%s: probe of %s failed with error %d\n",
317               drv->name, dev_name(dev), ret);
318    } else {
319        pr_debug("%s: probe of %s rejects match %d\n",
320               drv->name, dev_name(dev), ret);
321    }
322    /*
323     * Ignore errors returned by ->probe so that the next driver can try
324     * its luck.
325     */
326    ret = 0;
327done:
328    atomic_dec(&probe_count);
329    wake_up(&probe_waitqueue);
330    return ret;
331}


287-295行,如果定义了dev->bus->probe函数,则调用该函数;如果没有定义dev->bus->probe函数,但是定义了drv->probe函数,则调用drv->probe函数。这里,我们一般写Linux驱动程序时都要实现的probe函数就会被调用了。
297行,调用driver_bound(dev)函数,该函数定义在drivers/base/dd.c文件中,其内容如下:
 
 
  1. 182static void driver_bound(struct device *dev)  
  2. 183{  
  3. 184    if (klist_node_attached(&dev->p->knode_driver)) {  
  4. 185        printk(KERN_WARNING "%s: device %s already bound\n",  
  5. 186            __func__, kobject_name(&dev->kobj));  
  6. 187        return;  
  7. 188    }  
  8. 189  
  9. 190    pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),  
  10. 191         __func__, dev->driver->name);  
  11. 192  
  12. 193    klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);  
  13. 194  
  14. 195    /* 
  15. 196     * Make sure the device is no longer in one of the deferred lists and 
  16. 197     * kick off retrying all pending devices 
  17. 198     */  
  18. 199    driver_deferred_probe_del(dev);  
  19. 200    driver_deferred_probe_trigger();  
  20. 201  
  21. 202    if (dev->bus)  
  22. 203        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,  
  23. 204                         BUS_NOTIFY_BOUND_DRIVER, dev);  
  24. 205}  
182static void driver_bound(struct device *dev)
183{
184    if (klist_node_attached(&dev->p->knode_driver)) {
185        printk(KERN_WARNING "%s: device %s already bound\n",
186            __func__, kobject_name(&dev->kobj));
187        return;
188    }
189
190    pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
191         __func__, dev->driver->name);
192
193    klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
194
195    /*
196     * Make sure the device is no longer in one of the deferred lists and
197     * kick off retrying all pending devices
198     */
199    driver_deferred_probe_del(dev);
200    driver_deferred_probe_trigger();
201
202    if (dev->bus)
203        blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
204                         BUS_NOTIFY_BOUND_DRIVER, dev);
205}


193行,调用klist_add_tail函数将device加入到device_driver的driver->p->klist_devices链表中。
至此,我们一步一步回退
driver_bound->really_probe ->driver_probe_device->__device_attach->device_attach->bus_probe_device->device_add->device_register
这样,device_register函数我们就分析完了,也就清楚了device的注册过程。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值