1 int driver_attach(struct device_driver *drv)
  2 {
  3     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);   //  這個函式的功能就是:   依次去匹配bus匯流排下的各個裝置
  4 }
  5 

6 7 int bus_for_each_dev(struct bus_type *bus, struct device *start, 8 void *data, int (*fn)(struct device *, void *)) 9 { 10 struct klist_iter i; // 定義一個klist_iter 結構體變數 包含: struct klist 和 struct klist_node 11 struct device *dev; 12 int error = 0; 13 14 if (!bus) 15 return -EINVAL; 16 17 klist_iter_init_node(&bus->p->klist_devices, &i, // 這個函式的功能就是將 klist_devices 和 knode_bus填充到 i 變數中 18 (start ? &start->p->knode_bus : NULL)); 19 while ((dev = next_device(&i)) && !error) //  依次返回出總線上的各個裝置結構體device 20 error = fn(dev, data); // 對於每一個裝置和裝置驅動都呼叫fn這個函式 直道成功 或者全部都匹配不上 21 klist_iter_exit(&i); 22 return error; 23 } 24 25

26 27 static int __driver_attach(struct device *dev, void *data) 28 { 29 struct device_driver *drv = data; // 定義一個device_driver 指標 30 31 /* 32 * Lock device and try to bind to it. We drop the error 33 * here and always return 0, because we need to keep trying 34 * to bind to devices and some drivers will return an error 35 * simply if it didn't support the device. 36 * 37 * driver_probe_device() will spit a warning if there 38 * is an error. 39 */ 40 41 if (!driver_match_device(drv, dev)) // 通過這個函式進行匹配 呼叫匯流排下的match 函式 42 return 0; 43 44 if (dev->parent) /* Needed for USB */ 45 device_lock(dev->parent); 46 device_lock(dev); 47 if (!dev->driver) 48 driver_probe_device(drv, dev); // 呼叫probe函式 49 device_unlock(dev); 50 if (dev->parent) 51 device_unlock(dev->parent); 52 53 return 0; 54 } 55 56

57 58 int driver_probe_device(struct device_driver *drv, struct device *dev) 59 { 60 int ret = 0; 61 62 if (!device_is_registered(dev)) // 判斷這個裝置是否已經註冊了 63 return -ENODEV; 64 65 pr_debug("bus: '%s': %s: matched device %s with driver %s\n", 66 drv->bus->name, __func__, dev_name(dev), drv->name); 67 68 pm_runtime_get_noresume(dev); 69 pm_runtime_barrier(dev); 70 ret = really_probe(dev, drv); // 在這個函式中就會呼叫裝置驅動或者是匯流排下的 probe 函式 71 pm_runtime_put_sync(dev); 72 73 return ret; 74 } 75

76 77 static int really_probe(struct device *dev, struct device_driver *drv) 78 { 79 int ret = 0; 80 81 atomic_inc(&probe_count); 82 pr_debug("bus: '%s': %s: probing driver %s with device %s\n", 83 drv->bus->name, __func__, drv->name, dev_name(dev)); 84 WARN_ON(!list_empty(&dev->devres_head)); 85 86 dev->driver = drv; // 使用 dev->driver 指標去指向 drv 這就使得這兩者建立了一種關係 87 if (driver_sysfs_add(dev)) { 88 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n", 89 __func__, dev_name(dev)); 90 goto probe_failed; 91 } 92 93 if (dev->bus->probe) { // 如果匯流排下的probe函式存在 則呼叫優先呼叫這個函式 94 ret = dev->bus->probe(dev); 95 if (ret) 96 goto probe_failed; 97 } else if (drv->probe) { // 否則呼叫裝置驅動中的probe函式 98 ret = drv->probe(dev); // 所以由此可知: 匯流排中的probe函式具有更高的優先順序 99 if (ret) 100 goto probe_failed; 101 } 102 103 driver_bound(dev); 104 ret = 1; 105 pr_debug("bus: '%s': %s: bound device %s to driver %s\n", 106 drv->bus->name, __func__, dev_name(dev), drv->name); 107 goto done; 108 109 probe_failed: 110 devres_release_all(dev); 111 driver_sysfs_remove(dev); 112 dev->driver = NULL; 113 114 if (ret != -ENODEV && ret != -ENXIO) { 115 /* driver matched but the probe failed */ 116 printk(KERN_WARNING 117 "%s: probe of %s failed with error %d\n", 118 drv->name, dev_name(dev), ret); 119 } 120 /* 121 * Ignore errors returned by ->probe so that the next driver can try 122 * its luck. 123 */ 124 ret = 0; 125 done: 126 atomic_dec(&probe_count); 127 wake_up(&probe_waitqueue); 128 return ret; 129 }
.