以linux-5.14.10内核为例来分析总线注册过程:
是基于设备树的处理流程分析的,不过对于bus/dev/drv的模式只是在match函数匹配的过程中稍有差异,其他的流程是一样的。
本篇文章 1,总线注册匹配过程2,设备驱动注册匹配过程是介绍的两种不同的设备驱动注册的流程触发的初始化流程,而 3 device注册流程则是通过设备注册的处理流程来介绍整个总线设备驱动流程关系的。

1,总线类驱动注册匹配过程

总线注册过程以rk3399的PCIe控制器注册过程为例去做分析。

1.1 struct platform_driver

代码路径:include/linux/platform_device.h

  206 struct platform_driver { 
  207     int (*probe)(struct platform_device *); 
  208     int (*remove)(struct platform_device *); 
  209     void (*shutdown)(struct platform_device *); 
  210     int (*suspend)(struct platform_device *, pm_message_t state);                                                                                                                                       
  211     int (*resume)(struct platform_device *); 
  212     struct device_driver driver; 
  213     const struct platform_device_id *id_table; 
  214     bool prevent_deferred_probe; 
  215 };

1.2 struct device_driver

代码路径:include/linux/device/device.h

   50 /** 
   51  * struct device_driver - The basic device driver structure 
   52  * @name:   Name of the device driver.                                                                                                                                                                  
   53  * @bus:    The bus which the device of this driver belongs to. 
   54  * @owner:  The module owner. 
   55  * @mod_name:   Used for built-in modules. 
   56  * @suppress_bind_attrs: Disables bind/unbind via sysfs. 
   57  * @probe_type: Type of the probe (synchronous or asynchronous) to use. 
   58  * @of_match_table: The open firmware table. 
   59  * @acpi_match_table: The ACPI match table. 
   60  * @probe:  Called to query the existence of a specific device, 
   61  *      whether this driver can work with it, and bind the driver 
   62  *      to a specific device. 
   63  * @sync_state: Called to sync device state to software state after all the 
   64  *      state tracking consumers linked to this device (present at 
   65  *      the time of late_initcall) have successfully bound to a 
   66  *      driver. If the device has no consumers, this function will 
   67  *      be called at late_initcall_sync level. If the device has 
   68  *      consumers that are never bound to a driver, this function 
   69  *      will never get called until they do. 
   70  * @remove: Called when the device is removed from the system to 
   71  *      unbind a device from this driver. 
   72  * @shutdown:   Called at shut-down time to quiesce the device. 
   73  * @suspend:    Called to put the device to sleep mode. Usually to a 
   74  *      low power state. 
   75  * @resume: Called to bring a device from sleep mode. 
   76  * @groups: Default attributes that get created by the driver core 
   77  *      automatically. 
   78  * @dev_groups: Additional attributes attached to device instance once 
   79  *      it is bound to the driver. 
   80  * @pm:     Power management operations of the device which matched 
   81  *      this driver. 
   82  * @coredump:   Called when sysfs entry is written to. The device driver 
   83  *      is expected to call the dev_coredump API resulting in a 
   84  *      uevent. 
   85  * @p:      Driver core's private data, no one other than the driver 
   86  *      core can touch this. 
   87  * 
   88  * The device driver-model tracks all of the drivers known to the system. 
   89  * The main reason for this tracking is to enable the driver core to match 
   90  * up drivers with new devices. Once drivers are known objects within the 
   91  * system, however, a number of other things become possible. Device drivers 
   92  * can export information and configuration variables that are independent 
   93  * of any specific device. 
   94  */
   95 struct device_driver { 
   96     const char      *name; 
   97     struct bus_type     *bus; 
   98  
   99     struct module       *owner; 
  100     const char      *mod_name;  /* used for built-in modules */ 
  101  
  102     bool suppress_bind_attrs;   /* disables bind/unbind via sysfs */ 
  103     enum probe_type probe_type; 
  104  
  105     const struct of_device_id   *of_match_table; 
  106     const struct acpi_device_id *acpi_match_table; 
  107  
  108     int (*probe) (struct device *dev); 
  109     void (*sync_state)(struct device *dev); 
  110     int (*remove) (struct device *dev); 
  111     void (*shutdown) (struct device *dev); 
  112     int (*suspend) (struct device *dev, pm_message_t state); 
  113     int (*resume) (struct device *dev); 
  114     const struct attribute_group **groups; 
  115     const struct attribute_group **dev_groups; 
  116  
  117     const struct dev_pm_ops *pm; 
  118     void (*coredump) (struct device *dev);                                                                                                                                                              
  119  
  120     struct driver_private *p; 
  121 };

1.3 PCIe总线控制器驱动往platform注册的过程

|- module_platform_driver
  |- platform_driver_register
    |- __platform_driver_register
      |- driver_register
        |- bus_add_driver
          |- driver_attach
            |- bus_for_each_dev
            |- __driver_attach
              |- driver_match_device
                |- platform_match
              |- driver_probe_device
                |- really_probe
                  |- call_driver_probe
                  	|- platform_drv_probe
                  		|- rockchip_pcie_probe

1.3.1 rockchip_pcie_driver

代码路径:drivers/pci/controller/pcie-rockchip-host.c

  1041 static const struct dev_pm_ops rockchip_pcie_pm_ops = { 
  1042     SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(rockchip_pcie_suspend_noirq, 
  1043                       rockchip_pcie_resume_noirq) 
  1044 }; 
  1045  
  1046 static const struct of_device_id rockchip_pcie_of_match[] = { 
  1047     { .compatible = "rockchip,rk3399-pcie", }, 
  1048     {} 
  1049 }; 
  1050 MODULE_DEVICE_TABLE(of, rockchip_pcie_of_match); 
  1051  
  1052 static struct platform_driver rockchip_pcie_driver = { 
  1053     .driver = { 
  1054         .name = "rockchip-pcie", 
  1055         .of_match_table = rockchip_pcie_of_match, 
  1056         .pm = &rockchip_pcie_pm_ops, 
  1057     },                                                                                                                                                                                                 
  1058     .probe = rockchip_pcie_probe, 
  1059     .remove = rockchip_pcie_remove, 
  1060 }; 
  1061 module_platform_driver(rockchip_pcie_driver);

1.3.2 module_platform_driver

代码路径:include/linux/platform_device.h

  248 /* module_platform_driver() - Helper macro for drivers that don't do 
  249  * anything special in module init/exit.  This eliminates a lot of 
  250  * boilerplate.  Each module may only use this macro once, and 
  251  * calling it replaces module_init() and module_exit() 
  252  */ 
  253 #define module_platform_driver(__platform_driver) \ 
  254     module_driver(__platform_driver, platform_driver_register, \ 
  255             platform_driver_unregister)

1.3.3 platform_driver_register

代码路径:include/linux/platform_device.h

  220 /* 
  221  * use a macro to avoid include chaining to get THIS_MODULE 
  222  */ 
  223 #define platform_driver_register(drv) \ 
  224     __platform_driver_register(drv, THIS_MODULE)

1.3.4 __platform_driver_register and platform_bus_type

代码路径:drivers/base/platform.c

   863 /** 
   864  * __platform_driver_register - register a driver for platform-level devices 
   865  * @drv: platform driver structure 
   866  * @owner: owning module/driver 
   867  */ 
   868 int __platform_driver_register(struct platform_driver *drv, 
   869                 struct module *owner) 
   870 {                   
   871     drv->driver.owner = owner;                                                                                                                                                                         
   872     drv->driver.bus = &platform_bus_type; 
   873  
   874     return driver_register(&drv->driver); 
   875 } 
   876 EXPORT_SYMBOL_GPL(__platform_driver_register);

代码路径:drivers/base/platform.c

  1492 struct bus_type platform_bus_type = { 
  1493     .name       = "platform", 
  1494     .dev_groups = platform_dev_groups, 
  1495     .match      = platform_match, 
  1496     .uevent     = platform_uevent, 
  1497     .probe      = platform_probe, 
  1498     .remove     = platform_remove, 
  1499     .shutdown   = platform_shutdown, 
  1500     .dma_configure  = platform_dma_configure, 
  1501     .pm     = &platform_dev_pm_ops, 
  1502 };                                                                                                                                                                                                     
  1503 EXPORT_SYMBOL_GPL(platform_bus_type);

1.3.5 driver_register

代码路径:drivers/base/driver.c

  139 /** 
  140  * driver_register - register driver with bus 
  141  * @drv: driver to register                                                                                                                                                                             
  142  * 
  143  * We pass off most of the work to the bus_add_driver() call, 
  144  * since most of the things we have to do deal with the bus 
  145  * structures. 
  146  */ 
  147 int driver_register(struct device_driver *drv) 
  148 { 
  149     int ret; 
  150     struct device_driver *other; 
  151  
  152     if (!drv->bus->p) { 
  153         pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n", 
  154                drv->name, drv->bus->name); 
  155         return -EINVAL; 
  156     } 
  157  
  158     if ((drv->bus->probe && drv->probe) || 
  159         (drv->bus->remove && drv->remove) || 
  160         (drv->bus->shutdown && drv->shutdown)) 
  161         pr_warn("Driver '%s' needs updating - please use " 
  162             "bus_type methods\n", drv->name); 
  163  
  164     other = driver_find(drv->name, drv->bus); 
  165     if (other) { 
  166         pr_err("Error: Driver '%s' is already registered, " 
  167             "aborting...\n", drv->name); 
  168         return -EBUSY; 
  169     } 
  170  
  171     ret = bus_add_driver(drv); 
  172     if (ret) 
  173         return ret; 
  174     ret = driver_add_groups(drv, drv->groups); 
  175     if (ret) { 
  176         bus_remove_driver(drv); 
  177         return ret; 
  178     } 
  179     kobject_uevent(&drv->p->kobj, KOBJ_ADD); 
  180  
  181     return ret; 
  182 } 
  183 EXPORT_SYMBOL_GPL(driver_register);

1.3.6 bus_add_driver

代码路径:drivers/base/bus.c

   586 /** 
   587  * bus_add_driver - Add a driver to the bus. 
   588  * @drv: driver. 
   589  */ 
   590 int bus_add_driver(struct device_driver *drv) 
   591 { 
   592     struct bus_type *bus; 
   593     struct driver_private *priv; 
   594     int error = 0; 
   595  
   596     bus = bus_get(drv->bus); 
   597     if (!bus) 
   598         return -EINVAL; 
   599     
   600     pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 
   601     
   602     priv = kzalloc(sizeof(*priv), GFP_KERNEL); 
   603     if (!priv) { 
   604         error = -ENOMEM; 
   605         goto out_put_bus; 
   606     } 
   607     klist_init(&priv->klist_devices, NULL, NULL); 
   608     priv->driver = drv; 
   609     drv->p = priv; 
   610     priv->kobj.kset = bus->p->drivers_kset; 
   611     error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 
   612                      "%s", drv->name); 
   613     if (error) 
   614         goto out_unregister;                                                                                                                                                                           
   615         
   616     klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 
   617     if (drv->bus->p->drivers_autoprobe) { 
   618         error = driver_attach(drv); 
   619         if (error) 
   620             goto out_unregister; 
   621     }  
   622     module_add_driver(drv->owner, drv); 
   623     
   624     error = driver_create_file(drv, &driver_attr_uevent); 
   625     if (error) { 
   626         printk(KERN_ERR "%s: uevent attr (%s) failed\n", 
   627             __func__, drv->name); 
   628     } 
   629     error = driver_add_groups(drv, bus->drv_groups); 
   630     if (error) {
   631         /* How the hell do we get out of this pickle? Give up */ 
   632         printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 
   633             __func__, drv->name); 
   634     } 
   635  
   636     if (!drv->suppress_bind_attrs) { 
   637         error = add_bind_files(drv); 
   638         if (error) { 
   639             /* Ditto */ 
   640             printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 
   641                 __func__, drv->name);                                                                                                                                                                  
   642         } 
   643     } 
   644  
   645     return 0; 
   646  
   647 out_unregister: 
   648     kobject_put(&priv->kobj); 
   649     /* drv->p is freed in driver_release()  */ 
   650     drv->p = NULL; 
   651 out_put_bus: 
   652     bus_put(bus); 
   653     return error; 
   654 }

1.3.7 driver_attach

代码路径:drivers/base/dd.c

  1146 /** 
  1147  * driver_attach - try to bind driver to devices. 
  1148  * @drv: driver. 
  1149  * 
  1150  * Walk the list of devices that the bus has on it and try to 
  1151  * match the driver with each one.  If driver_probe_device() 
  1152  * returns 0 and the @dev->driver is set, we've found a 
  1153  * compatible pair. 
  1154  */ 
  1155 int driver_attach(struct device_driver *drv) 
  1156 {    
  1157     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); 
  1158 }                                                                                                                                                                                                      
  1159 EXPORT_SYMBOL_GPL(driver_attach);

1.3.8 bus_for_each_dev

代码路径:drivers/base/bus.c

   269 /** 
   270  * bus_for_each_dev - device iterator. 
   271  * @bus: bus type. 
   272  * @start: device to start iterating from. 
   273  * @data: data for the callback. 
   274  * @fn: function to be called for each device. 
   275  * 
   276  * Iterate over @bus's list of devices, and call @fn for each, 
   277  * passing it @data. If @start is not NULL, we use that device to 
   278  * begin iterating from. 
   279  * 
   280  * We check the return of @fn each time. If it returns anything 
   281  * other than 0, we break out and return that value. 
   282  *       
   283  * NOTE: The device that returns a non-zero value is not retained 
   284  * in any way, nor is its refcount incremented. If the caller needs 
   285  * to retain this data, it should do so, and increment the reference 
   286  * count in the supplied callback.                                                                                                                                                                     
   287  */  
   288 int bus_for_each_dev(struct bus_type *bus, struct device *start, 
   289              void *data, int (*fn)(struct device *, void *)) 
   290 {    
   291     struct klist_iter i; 
   292     struct device *dev; 
   293     int error = 0; 
   294  
   295     if (!bus || !bus->p) 
   296         return -EINVAL; 
   297  
   298     klist_iter_init_node(&bus->p->klist_devices, &i, 
   299                  (start ? &start->p->knode_bus : NULL)); 
   300     while (!error && (dev = next_device(&i))) 
   301         error = fn(dev, data);
   302     klist_iter_exit(&i); 
   303     return error; 
   304 }    
   305 EXPORT_SYMBOL_GPL(bus_for_each_dev);

1.3.9 __driver_attach

  • driver_match_device首先判断驱动和总线驱动是否匹配
  • driver_probe_device调用对应的总线驱动probe函数做对应的初始化处理
    代码路径:drivers/base/dd.c
  1092 static int __driver_attach(struct device *dev, void *data) 
  1093 { 
  1094     struct device_driver *drv = data; 
  1095     int ret; 
  1096  
  1097     /*                                                                                                                                                                                                 
  1098      * Lock device and try to bind to it. We drop the error 
  1099      * here and always return 0, because we need to keep trying 
  1100      * to bind to devices and some drivers will return an error 
  1101      * simply if it didn't support the device. 
  1102      * 
  1103      * driver_probe_device() will spit a warning if there 
  1104      * is an error. 
  1105      */ 
  1106  
  1107     ret = driver_match_device(drv, dev); 
  1108     if (ret == 0) { 
  1109         /* no match */ 
  1110         return 0; 
  1111     } else if (ret == -EPROBE_DEFER) { 
  1112         dev_dbg(dev, "Device match requests probe deferral\n"); 
  1113         dev->can_match = true; 
  1114         driver_deferred_probe_add(dev); 
  1115     } else if (ret < 0) { 
  1116         dev_dbg(dev, "Bus failed to match device: %d\n", ret); 
  1117         return ret; 
  1118     } /* ret > 0 means positive match */ 
  1119  
  1120     if (driver_allows_async_probing(drv)) { 
  1121         /* 
  1122          * Instead of probing the device synchronously we will 
  1123          * probe it asynchronously to allow for more parallelism. 
  1124          * 
  1125          * We only take the device lock here in order to guarantee 
  1126          * that the dev->driver and async_driver fields are protected 
  1127          */ 
  1128         dev_dbg(dev, "probing driver %s asynchronously\n", drv->name); 
  1129         device_lock(dev); 
  1130         if (!dev->driver) { 
  1131             get_device(dev); 
  1132             dev->p->async_driver = drv; 
  1133             async_schedule_dev(__driver_attach_async_helper, dev); 
  1134         } 
  1135         device_unlock(dev); 
  1136         return 0; 
  1137     }
  1138  
  1139     __device_driver_lock(dev, dev->parent); 
  1140     driver_probe_device(drv, dev); 
  1141     __device_driver_unlock(dev, dev->parent); 
  1142  
  1143     return 0;                                                                                                                                                                                          
  1144 }

1.3.10 driver_match_device

对于drv->bus->match(dev, drv),match函数对应着在前面注册的platform_bus_type里面的platform_match函数
代码路径:drivers/base/base.h

  144 static inline int driver_match_device(struct device_driver *drv, 
  145                       struct device *dev) 
  146 {                                                                                                                                                                                                       
  147     return drv->bus->match ? drv->bus->match(dev, drv) : 1; 
  148 

1.3.11 platform_match

判断platform driver和platform device是否匹配按照下面的流程处理,有一个成功, 即匹配成功

  • 比较 platform_dev.driver_overrideplatform_driver.drv->name
  • 比较 platform_dev.dev.of_nodecompatible属性 和 platform_driver.drv->of_match_table
  • 比较 platform_dev.nameplatform_driver.id_table
  • 比较 platform_dev.nameplatform_driver.drv->name

代码路径:drivers/base/platform.c

  1345 /** 
  1346  * platform_match - bind platform device to platform driver. 
  1347  * @dev: device. 
  1348  * @drv: driver. 
  1349  * 
  1350  * Platform device IDs are assumed to be encoded like this: 
  1351  * "<name><instance>", where <name> is a short description of the type of                                                                                                                              
  1352  * device, like "pci" or "floppy", and <instance> is the enumerated 
  1353  * instance of the device, like '0' or '42'.  Driver IDs are simply 
  1354  * "<name>".  So, extract the <name> from the platform_device structure, 
  1355  * and compare it against the name of the driver. Return whether they match 
  1356  * or not. 
  1357  */ 
  1358 static int platform_match(struct device *dev, struct device_driver *drv) 
  1359 { 
  1360     struct platform_device *pdev = to_platform_device(dev); 
  1361     struct platform_driver *pdrv = to_platform_driver(drv); 
  1362  
  1363     /* When driver_override is set, only bind to the matching driver */ 
  1364     if (pdev->driver_override) 
  1365         return !strcmp(pdev->driver_override, drv->name); 
  1366  
  1367     /* Attempt an OF style match first */ 
  1368     if (of_driver_match_device(dev, drv)) 
  1369         return 1; 
  1370  
  1371     /* Then try ACPI style match */ 
  1372     if (acpi_driver_match_device(dev, drv)) 
  1373         return 1; 
  1374  
  1375     /* Then try to match against the id table */ 
  1376     if (pdrv->id_table) 
  1377         return platform_match_id(pdrv->id_table, pdev) != NULL; 
  1378  
  1379     /* fall-back to driver name match */ 
  1380     return (strcmp(pdev->name, drv->name) == 0); 
  1381 }

1.3.12 driver_probe_device

代码路径:drivers/base/dd.c

   761 /** 
   762  * driver_probe_device - attempt to bind device & driver together 
   763  * @drv: driver to bind a device to 
   764  * @dev: device to try to bind to the driver 
   765  * 
   766  * This function returns -ENODEV if the device is not registered, -EBUSY if it 
   767  * already has a driver, 0 if the device is bound successfully and a positive 
   768  * (inverted) error code for failures from the ->probe method. 
   769  * 
   770  * This function must be called with @dev lock held.  When called for a 
   771  * USB interface, @dev->parent lock must be held as well. 
   772  * 
   773  * If the device has a parent, runtime-resume the parent before driver probing. 
   774  */ 
   775 static int driver_probe_device(struct device_driver *drv, struct device *dev)                                                                                                                          
   776 { 
   777     int trigger_count = atomic_read(&deferred_trigger_count); 
   778     int ret; 
   779  
   780     atomic_inc(&probe_count); 
   781     ret = __driver_probe_device(drv, dev); 
   782     if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) { 
   783         driver_deferred_probe_add(dev); 
   784  
   785         /* 
   786          * Did a trigger occur while probing? Need to re-trigger if yes 
   787          */ 
   788         if (trigger_count != atomic_read(&deferred_trigger_count) && 
   789             !defer_all_probes) 
   790             driver_deferred_probe_trigger(); 
   791     } 
   792     atomic_dec(&probe_count); 
   793     wake_up_all(&probe_waitqueue); 
   794     return ret; 
   795 }

2.1.12 __driver_probe_device

代码路径:drivers/base/dd.c

   730 static int __driver_probe_device(struct device_driver *drv, struct device *dev) 
   731 { 
   732     int ret = 0; 
   733  
   734     if (dev->p->dead || !device_is_registered(dev)) 
   735         return -ENODEV; 
   736     if (dev->driver) 
   737         return -EBUSY; 
   738  
   739     dev->can_match = true; 
   740     pr_debug("bus: '%s': %s: matched device %s with driver %s\n", 
   741          drv->bus->name, __func__, dev_name(dev), drv->name); 
   742  
   743     pm_runtime_get_suppliers(dev); 
   744     if (dev->parent) 
   745         pm_runtime_get_sync(dev->parent); 
   746                                                                                                                                                                                                        
   747     pm_runtime_barrier(dev); 
   748     if (initcall_debug) 
   749         ret = really_probe_debug(dev, drv); 
   750     else 
   751         ret = really_probe(dev, drv); 
   752     pm_request_idle(dev); 
   753  
   754     if (dev->parent) 
   755         pm_runtime_put(dev->parent); 
   756  
   757     pm_runtime_put_suppliers(dev); 
   758     return ret; 
   759 }

1.3.13 really_probe

代码路径:drivers/base/dd.c

   541 static int really_probe(struct device *dev, struct device_driver *drv) 
   542 { 
   543     bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) && 
   544                !drv->suppress_bind_attrs; 
   545     int ret; 
   546  
   547     if (defer_all_probes) { 
   548         /* 
   549          * Value of defer_all_probes can be set only by 
   550          * device_block_probing() which, in turn, will call 
   551          * wait_for_device_probe() right after that to avoid any races. 
   552          */ 
   553         dev_dbg(dev, "Driver %s force probe deferral\n", drv->name); 
   554         return -EPROBE_DEFER; 
   555     } 
   556  
   557     ret = device_links_check_suppliers(dev); 
   558     if (ret) 
   559         return ret; 
   560  
   561     pr_debug("bus: '%s': %s: probing driver %s with device %s\n", 
   562          drv->bus->name, __func__, drv->name, dev_name(dev)); 
   563     if (!list_empty(&dev->devres_head)) { 
   564         dev_crit(dev, "Resources present before probing\n"); 
   565         ret = -EBUSY; 
   566         goto done; 
   567     } 
   568  
   569 re_probe: 
   570     dev->driver = drv; 
   571  
   572     /* If using pinctrl, bind pins now before probing */ 
   573     ret = pinctrl_bind_pins(dev); 
   574     if (ret) 
   575         goto pinctrl_bind_failed; 
   576  
   577     if (dev->bus->dma_configure) { 
   578         ret = dev->bus->dma_configure(dev); 
   579         if (ret) 
   580             goto probe_failed; 
   581     } 
   582  
   583     ret = driver_sysfs_add(dev); 
   584     if (ret) {
   585         pr_err("%s: driver_sysfs_add(%s) failed\n", 
   586                __func__, dev_name(dev)); 
   587         goto probe_failed; 
   588     } 
   589  
   590     if (dev->pm_domain && dev->pm_domain->activate) { 
   591         ret = dev->pm_domain->activate(dev); 
   592         if (ret) 
   593             goto probe_failed; 
   594     } 
   595  
   596     ret = call_driver_probe(dev, drv); 
   597     if (ret) { 
   598         /* 
   599          * Return probe errors as positive values so that the callers 
   600          * can distinguish them from other errors. 
   601          */ 
   602         ret = -ret; 
   603         goto probe_failed; 
   604     } 
   605  
   606     ret = device_add_groups(dev, drv->dev_groups); 
   607     if (ret) { 
   608         dev_err(dev, "device_add_groups() failed\n"); 
   609         goto dev_groups_failed; 
   610     } 
   611  
   612     if (dev_has_sync_state(dev)) { 
   613         ret = device_create_file(dev, &dev_attr_state_synced); 
   614         if (ret) { 
   615             dev_err(dev, "state_synced sysfs add failed\n"); 
   616             goto dev_sysfs_state_synced_failed;                                                                                                                                                        
   617         } 
   618     } 
   619  
   620     if (test_remove) { 
   621         test_remove = false; 
   622  
   623         device_remove_file(dev, &dev_attr_state_synced); 
   624         device_remove_groups(dev, drv->dev_groups); 
   625  
   626         if (dev->bus->remove) 
   627             dev->bus->remove(dev); 
   628         else if (drv->remove) 
   629             drv->remove(dev);
   630  
   631         devres_release_all(dev); 
   632         driver_sysfs_remove(dev); 
   633         dev->driver = NULL; 
   634         dev_set_drvdata(dev, NULL); 
   635         if (dev->pm_domain && dev->pm_domain->dismiss) 
   636             dev->pm_domain->dismiss(dev); 
   637         pm_runtime_reinit(dev); 
   638  
   639         goto re_probe; 
   640     } 
   641  
   642     pinctrl_init_done(dev); 
   643  
   644     if (dev->pm_domain && dev->pm_domain->sync) 
   645         dev->pm_domain->sync(dev); 
   646  
   647     driver_bound(dev); 
   648     pr_debug("bus: '%s': %s: bound device %s to driver %s\n", 
   649          drv->bus->name, __func__, dev_name(dev), drv->name); 
   650     goto done; 
   651  
   652 dev_sysfs_state_synced_failed: 
   653     device_remove_groups(dev, drv->dev_groups); 
   654 dev_groups_failed: 
   655     if (dev->bus->remove) 
   656         dev->bus->remove(dev); 
   657     else if (drv->remove) 
   658         drv->remove(dev); 
   659 probe_failed: 
   660     if (dev->bus) 
   661         blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 
   662                          BUS_NOTIFY_DRIVER_NOT_BOUND, dev); 
   663 pinctrl_bind_failed:                                                                                                                                                                                   
   664     device_links_no_driver(dev); 
   665     devres_release_all(dev); 
   666     arch_teardown_dma_ops(dev); 
   667     kfree(dev->dma_range_map); 
   668     dev->dma_range_map = NULL; 
   669     driver_sysfs_remove(dev); 
   670     dev->driver = NULL; 
   671     dev_set_drvdata(dev, NULL); 
   672     if (dev->pm_domain && dev->pm_domain->dismiss) 
   673         dev->pm_domain->dismiss(dev); 
   674     pm_runtime_reinit(dev); 
   675     dev_pm_set_driver_flags(dev, 0); 
   676 done:
   677     return ret; 
   678 }

1.3.14 call_driver_probe

代码路径:drivers/base/dd.c

   510 static int call_driver_probe(struct device *dev, struct device_driver *drv) 
   511 { 
   512     int ret = 0; 
   513  
   514     if (dev->bus->probe) 
   515         ret = dev->bus->probe(dev); 
   516     else if (drv->probe) 
   517         ret = drv->probe(dev); 
   518  
   519     switch (ret) { 
   520     case 0: 
   521         break; 
   522     case -EPROBE_DEFER: 
   523         /* Driver requested deferred probing */ 
   524         dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name); 
   525         break; 
   526     case -ENODEV:                                                                                                                                                                                      
   527     case -ENXIO: 
   528         pr_debug("%s: probe of %s rejects match %d\n", 
   529              drv->name, dev_name(dev), ret); 
   530         break; 
   531     default: 
   532         /* driver matched but the probe failed */ 
   533         pr_warn("%s: probe of %s failed with error %d\n", 
   534             drv->name, dev_name(dev), ret); 
   535         break; 
   536     } 
   537  
   538     return ret; 
   539 }

1.3.15 platform_drv_probe

代码路径:drivers/base/platform.c

   505 static int platform_drv_probe(struct device *_dev) 
   506 { 
   507     struct platform_driver *drv = to_platform_driver(_dev->driver); 
   508     struct platform_device *dev = to_platform_device(_dev); 
   509     int ret; 
   510  
   511     ret = of_clk_set_defaults(_dev->of_node, false); 
   512     if (ret < 0) 
   513         return ret; 
   514  
   515     ret = dev_pm_domain_attach(_dev, true); 
   516     if (ret != -EPROBE_DEFER) { 
   517         if (drv->probe) { 
   518             ret = drv->probe(dev); //point to rockchip_pcie_probe
   519             if (ret) 
   520                 dev_pm_domain_detach(_dev, true); 
   521         } else { 
   522             /* don't fail if just dev_pm_domain_attach failed */ 
   523             ret = 0; 
   524         } 
   525     } 
   526  
   527     if (drv->prevent_deferred_probe && ret == -EPROBE_DEFER) { 
   528         dev_warn(_dev, "probe deferral not supported\n"); 
   529         ret = -ENXIO; 
   530     } 
   531  
   532     return ret; 
   533 }

1.3.16 rockchip_pcie_probe

代码路径:drivers/base/platform.c

   934 static int rockchip_pcie_probe(struct platform_device *pdev) 
   935 {
        ...
  1015 }

2,设备驱动注册匹配过程:

在该过程中以nvme驱动为例去介绍匹配的过程,nvme是挂载在PCIe总线上的一款设备。

2.1 pci_register_driver

pci_register_driver在注册的时候会去根据注册到PCI总线上的dev_driver和devices去做对应的匹配校验。整个的流程如下所示:

|- nvme_init
  |- pci_register_driver
    |- __pci_register_driver
      |- driver_register
        |- bus_add_driver
          |- driver_attach
            |- bus_for_each_dev
            |- __driver_attach
              |- driver_match_device
                |- pci_bus_match
                  |- pci_match_device
                    |- pci_match_one_device
              |- driver_probe_device
                |- really_probe
                  |- call_driver_probe
                  	|- pci_device_probe
                  		|- __pci_device_probe
                  			|- pci_call_probe
                  				|- local_pci_probe
                  					|- nvme_probe

2.1.1 nvme_init

nvme设备驱动注册
代码路径:drivers/nvme/host/pci.c

  3308 static int __init nvme_init(void) 
  3309 { 
  3310     BUILD_BUG_ON(sizeof(struct nvme_create_cq) != 64); 
  3311     BUILD_BUG_ON(sizeof(struct nvme_create_sq) != 64); 
  3312     BUILD_BUG_ON(sizeof(struct nvme_delete_queue) != 64); 
  3313     BUILD_BUG_ON(IRQ_AFFINITY_MAX_SETS < 2);                                                                                                                                                           
  3314  
  3315     return pci_register_driver(&nvme_driver); 
  3316 } 
  3317  
  3318 static void __exit nvme_exit(void) 
  3319 { 
  3320     pci_unregister_driver(&nvme_driver); 
  3321     flush_workqueue(nvme_wq); 
  3322 }

2.1.2 pci_register_driver

代码路径:include/linux/pci.h

  1400 /* pci_register_driver() must be a macro so KBUILD_MODNAME can be expanded */                                                                                                                          
  1401 #define pci_register_driver(driver)     \ 
  1402     __pci_register_driver(driver, THIS_MODULE, KBUILD_MODNAME)

2.1.3 __pci_register_driver

drv->driver.bus = &pci_bus_type;表示nvme这个设备是挂载在PCI总线上的pci device,而pci_bus_type 的match函数用脚检测挂载在PCI/PCIe总线上的设备和驱动是否匹配。
代码路径:drivers/pci/pci-driver.c

  1368 /** 
  1369  * __pci_register_driver - register a new pci driver 
  1370  * @drv: the driver structure to register 
  1371  * @owner: owner module of drv 
  1372  * @mod_name: module name string 
  1373  * 
  1374  * Adds the driver structure to the list of registered drivers. 
  1375  * Returns a negative value on error, otherwise 0. 
  1376  * If no error occurred, the driver remains registered even if 
  1377  * no device was claimed during registration. 
  1378  */ 
  1379 int __pci_register_driver(struct pci_driver *drv, struct module *owner, 
  1380               const char *mod_name) 
  1381 { 
  1382     /* initialize common driver fields */ 
  1383     drv->driver.name = drv->name; 
  1384     drv->driver.bus = &pci_bus_type; 
  1385     drv->driver.owner = owner; 
  1386     drv->driver.mod_name = mod_name; 
  1387     drv->driver.groups = drv->groups; 
  1388     drv->driver.dev_groups = drv->dev_groups; 
  1389                                                                                                                                                                                                        
  1390     spin_lock_init(&drv->dynids.lock); 
  1391     INIT_LIST_HEAD(&drv->dynids.list); 
  1392  
  1393     /* register with core */ 
  1394     return driver_register(&drv->driver); 
  1395 } 
  1396 EXPORT_SYMBOL(__pci_register_driver);

2.1.4 driver_register

代码路径:drivers/base/driver.c

  139 /** 
  140  * driver_register - register driver with bus 
  141  * @drv: driver to register                                                                                                                                                                             
  142  * 
  143  * We pass off most of the work to the bus_add_driver() call, 
  144  * since most of the things we have to do deal with the bus 
  145  * structures. 
  146  */ 
  147 int driver_register(struct device_driver *drv) 
  148 { 
  149     int ret; 
  150     struct device_driver *other; 
  151  
  152     if (!drv->bus->p) { 
  153         pr_err("Driver '%s' was unable to register with bus_type '%s' because the bus was not initialized.\n", 
  154                drv->name, drv->bus->name); 
  155         return -EINVAL; 
  156     } 
  157  
  158     if ((drv->bus->probe && drv->probe) || 
  159         (drv->bus->remove && drv->remove) || 
  160         (drv->bus->shutdown && drv->shutdown)) 
  161         pr_warn("Driver '%s' needs updating - please use " 
  162             "bus_type methods\n", drv->name); 
  163  
  164     other = driver_find(drv->name, drv->bus); 
  165     if (other) { 
  166         pr_err("Error: Driver '%s' is already registered, " 
  167             "aborting...\n", drv->name); 
  168         return -EBUSY; 
  169     } 
  170  
  171     ret = bus_add_driver(drv); 
  172     if (ret) 
  173         return ret; 
  174     ret = driver_add_groups(drv, drv->groups); 
  175     if (ret) { 
  176         bus_remove_driver(drv); 
  177         return ret; 
  178     } 
  179     kobject_uevent(&drv->p->kobj, KOBJ_ADD); 
  180  
  181     return ret; 
  182 } 
  183 EXPORT_SYMBOL_GPL(driver_register);

2.1.5 bus_add_driver

代码路径:drivers/base/bus.c

   586 /** 
   587  * bus_add_driver - Add a driver to the bus. 
   588  * @drv: driver. 
   589  */ 
   590 int bus_add_driver(struct device_driver *drv) 
   591 { 
   592     struct bus_type *bus; 
   593     struct driver_private *priv; 
   594     int error = 0; 
   595  
   596     bus = bus_get(drv->bus); 
   597     if (!bus) 
   598         return -EINVAL; 
   599     
   600     pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name); 
   601     
   602     priv = kzalloc(sizeof(*priv), GFP_KERNEL); 
   603     if (!priv) { 
   604         error = -ENOMEM; 
   605         goto out_put_bus; 
   606     } 
   607     klist_init(&priv->klist_devices, NULL, NULL); 
   608     priv->driver = drv; 
   609     drv->p = priv; 
   610     priv->kobj.kset = bus->p->drivers_kset; 
   611     error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL, 
   612                      "%s", drv->name); 
   613     if (error) 
   614         goto out_unregister;                                                                                                                                                                           
   615         
   616     klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers); 
   617     if (drv->bus->p->drivers_autoprobe) { 
   618         error = driver_attach(drv); 
   619         if (error) 
   620             goto out_unregister; 
   621     }  
   622     module_add_driver(drv->owner, drv); 
   623     
   624     error = driver_create_file(drv, &driver_attr_uevent); 
   625     if (error) { 
   626         printk(KERN_ERR "%s: uevent attr (%s) failed\n", 
   627             __func__, drv->name); 
   628     } 
   629     error = driver_add_groups(drv, bus->drv_groups); 
   630     if (error) {
   631         /* How the hell do we get out of this pickle? Give up */ 
   632         printk(KERN_ERR "%s: driver_add_groups(%s) failed\n", 
   633             __func__, drv->name); 
   634     } 
   635  
   636     if (!drv->suppress_bind_attrs) { 
   637         error = add_bind_files(drv); 
   638         if (error) { 
   639             /* Ditto */ 
   640             printk(KERN_ERR "%s: add_bind_files(%s) failed\n", 
   641                 __func__, drv->name);                                                                                                                                                                  
   642         } 
   643     } 
   644  
   645     return 0; 
   646  
   647 out_unregister: 
   648     kobject_put(&priv->kobj); 
   649     /* drv->p is freed in driver_release()  */ 
   650     drv->p = NULL; 
   651 out_put_bus: 
   652     bus_put(bus); 
   653     return error; 
   654 }

2.1.6 driver_attach

代码路径:drivers/base/dd.c

  1146 /** 
  1147  * driver_attach - try to bind driver to devices. 
  1148  * @drv: driver. 
  1149  * 
  1150  * Walk the list of devices that the bus has on it and try to 
  1151  * match the driver with each one.  If driver_probe_device() 
  1152  * returns 0 and the @dev->driver is set, we've found a 
  1153  * compatible pair. 
  1154  */ 
  1155 int driver_attach(struct device_driver *drv) 
  1156 {    
  1157     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach); 
  1158 }                                                                                                                                                                                                      
  1159 EXPORT_SYMBOL_GPL(driver_attach);

2.1.7 bus_for_each_dev

代码路径:drivers/base/bus.c

   269 /** 
   270  * bus_for_each_dev - device iterator. 
   271  * @bus: bus type. 
   272  * @start: device to start iterating from. 
   273  * @data: data for the callback. 
   274  * @fn: function to be called for each device. 
   275  * 
   276  * Iterate over @bus's list of devices, and call @fn for each, 
   277  * passing it @data. If @start is not NULL, we use that device to 
   278  * begin iterating from. 
   279  * 
   280  * We check the return of @fn each time. If it returns anything 
   281  * other than 0, we break out and return that value. 
   282  *       
   283  * NOTE: The device that returns a non-zero value is not retained 
   284  * in any way, nor is its refcount incremented. If the caller needs 
   285  * to retain this data, it should do so, and increment the reference 
   286  * count in the supplied callback.                                                                                                                                                                     
   287  */  
   288 int bus_for_each_dev(struct bus_type *bus, struct device *start, 
   289              void *data, int (*fn)(struct device *, void *)) 
   290 {    
   291     struct klist_iter i; 
   292     struct device *dev; 
   293     int error = 0; 
   294  
   295     if (!bus || !bus->p) 
   296         return -EINVAL; 
   297  
   298     klist_iter_init_node(&bus->p->klist_devices, &i, 
   299                  (start ? &start->p->knode_bus : NULL)); 
   300     while (!error && (dev = next_device(&i))) 
   301         error = fn(dev, data);
   302     klist_iter_exit(&i); 
   303     return error; 
   304 }    
   305 EXPORT_SYMBOL_GPL(bus_for_each_dev);

2.1.8 __driver_attach

代码路径:drivers/base/dd.c

  1092 static int __driver_attach(struct device *dev, void *data) 
  1093 { 
  1094     struct device_driver *drv = data; 
  1095     int ret; 
  1096  
  1097     /*                                                                                                                                                                                                 
  1098      * Lock device and try to bind to it. We drop the error 
  1099      * here and always return 0, because we need to keep trying 
  1100      * to bind to devices and some drivers will return an error 
  1101      * simply if it didn't support the device. 
  1102      * 
  1103      * driver_probe_device() will spit a warning if there 
  1104      * is an error. 
  1105      */ 
  1106  
  1107     ret = driver_match_device(drv, dev); 
  1108     if (ret == 0) { 
  1109         /* no match */ 
  1110         return 0; 
  1111     } else if (ret == -EPROBE_DEFER) { 
  1112         dev_dbg(dev, "Device match requests probe deferral\n"); 
  1113         dev->can_match = true; 
  1114         driver_deferred_probe_add(dev); 
  1115     } else if (ret < 0) { 
  1116         dev_dbg(dev, "Bus failed to match device: %d\n", ret); 
  1117         return ret; 
  1118     } /* ret > 0 means positive match */ 
  1119  
  1120     if (driver_allows_async_probing(drv)) { 
  1121         /* 
  1122          * Instead of probing the device synchronously we will 
  1123          * probe it asynchronously to allow for more parallelism. 
  1124          * 
  1125          * We only take the device lock here in order to guarantee 
  1126          * that the dev->driver and async_driver fields are protected 
  1127          */ 
  1128         dev_dbg(dev, "probing driver %s asynchronously\n", drv->name); 
  1129         device_lock(dev); 
  1130         if (!dev->driver) { 
  1131             get_device(dev); 
  1132             dev->p->async_driver = drv; 
  1133             async_schedule_dev(__driver_attach_async_helper, dev); 
  1134         } 
  1135         device_unlock(dev); 
  1136         return 0; 
  1137     }
  1138  
  1139     __device_driver_lock(dev, dev->parent); 
  1140     driver_probe_device(drv, dev); 
  1141     __device_driver_unlock(dev, dev->parent); 
  1142  
  1143     return 0;                                                                                                                                                                                          
  1144 }

2.1.9 driver_match_device

drv->bus->match(dev, drv)的match函数指向前面注册的pci_bus_type所定义的pci_bus_match函数。
代码路径:drivers/base/base.h

  144 static inline int driver_match_device(struct device_driver *drv, 
  145                       struct device *dev) 
  146 {                                                                                                                                                                                                       
  147     return drv->bus->match ? drv->bus->match(dev, drv) : 1; 
  148 

2.1.10 pci_bus_match

代码路径:drivers/pci/pci-driver.c

  1440 /** 
  1441  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure 
  1442  * @dev: the PCI device structure to match against 
  1443  * @drv: the device driver to search for matching PCI device id structures 
  1444  *                                                                                                                                                                                                     
  1445  * Used by a driver to check whether a PCI device present in the 
  1446  * system is in its list of supported devices. Returns the matching 
  1447  * pci_device_id structure or %NULL if there is no match. 
  1448  */ 
  1449 static int pci_bus_match(struct device *dev, struct device_driver *drv) 
  1450 { 
  1451     struct pci_dev *pci_dev = to_pci_dev(dev); 
  1452     struct pci_driver *pci_drv; 
  1453     const struct pci_device_id *found_id; 
  1454  
  1455     if (!pci_dev->match_driver) 
  1456         return 0; 
  1457  
  1458     pci_drv = to_pci_driver(drv); 
  1459     found_id = pci_match_device(pci_drv, pci_dev); 
  1460     if (found_id) 
  1461         return 1; 
  1462  
  1463     return 0; 
  1464 }

2.1.11 pci_match_device

代码路径:drivers/pci/pci-driver.c

   125 /**     
   126  * pci_match_device - See if a device matches a driver's list of IDs 
   127  * @drv: the PCI driver to match against 
   128  * @dev: the PCI device structure to match against 
   129  * 
   130  * Used by a driver to check whether a PCI device is in its list of 
   131  * supported devices or in the dynids list, which may have been augmented 
   132  * via the sysfs "new_id" file.  Returns the matching pci_device_id 
   133  * structure or %NULL if there is no match. 
   134  */ 
   135 static const struct pci_device_id *pci_match_device(struct pci_driver *drv, 
   136                             struct pci_dev *dev) 
   137 { 
   138     struct pci_dynid *dynid; 
   139     const struct pci_device_id *found_id = NULL; 
   140  
   141     /* When driver_override is set, only bind to the matching driver */ 
   142     if (dev->driver_override && strcmp(dev->driver_override, drv->name))                                                                                                                               
   143         return NULL; 
   144  
   145     /* Look at the dynamic ids first, before the static ones */ 
   146     spin_lock(&drv->dynids.lock); 
   147     list_for_each_entry(dynid, &drv->dynids.list, node) { 
   148         if (pci_match_one_device(&dynid->id, dev)) { 
   149             found_id = &dynid->id; 
   150             break;  
   151         } 
   152     }   
   153     spin_unlock(&drv->dynids.lock);  
   154         
   155     if (!found_id) 
   156         found_id = pci_match_id(drv->id_table, dev); 
   157  
   158     /* driver_override will always match, send a dummy id */ 
   159     if (!found_id && dev->driver_override) 
   160         found_id = &pci_device_id_any; 
   161  
   162     return found_id; 
   163 }

2.1.12 pci_match_one_device

代码路径:drivers/pci/pci-driver.c

  204 /**   
  205  * pci_match_one_device - Tell if a PCI device structure has a matching 
  206  *            PCI device id structure 
  207  * @id: single PCI device id structure to match 
  208  * @dev: the PCI device structure to match against 
  209  * 
  210  * Returns the matching pci_device_id structure or %NULL if there is no match. 
  211  */   
  212 static inline const struct pci_device_id * 
  213 pci_match_one_device(const struct pci_device_id *id, const struct pci_dev *dev) 
  214 {215     if ((id->vendor == PCI_ANY_ID || id->vendor == dev->vendor) &&216         (id->device == PCI_ANY_ID || id->device == dev->device) &&217         (id->subvendor == PCI_ANY_ID || id->subvendor == dev->subsystem_vendor) &&218         (id->subdevice == PCI_ANY_ID || id->subdevice == dev->subsystem_device) &&219         !((id->class ^ dev->class) & id->class_mask))220         return id;221     return NULL; 
  222 }

2.1.13 driver_probe_device

代码路径:drivers/base/dd.c

   761 /** 
   762  * driver_probe_device - attempt to bind device & driver together 
   763  * @drv: driver to bind a device to 
   764  * @dev: device to try to bind to the driver 
   765  * 
   766  * This function returns -ENODEV if the device is not registered, -EBUSY if it 
   767  * already has a driver, 0 if the device is bound successfully and a positive 
   768  * (inverted) error code for failures from the ->probe method. 
   769  * 
   770  * This function must be called with @dev lock held.  When called for a 
   771  * USB interface, @dev->parent lock must be held as well. 
   772  * 
   773  * If the device has a parent, runtime-resume the parent before driver probing. 
   774  */ 
   775 static int driver_probe_device(struct device_driver *drv, struct device *dev)                                                                                                                          
   776 { 
   777     int trigger_count = atomic_read(&deferred_trigger_count); 
   778     int ret; 
   779  
   780     atomic_inc(&probe_count); 
   781     ret = __driver_probe_device(drv, dev); 
   782     if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) { 
   783         driver_deferred_probe_add(dev); 
   784  
   785         /* 
   786          * Did a trigger occur while probing? Need to re-trigger if yes 
   787          */ 
   788         if (trigger_count != atomic_read(&deferred_trigger_count) && 
   789             !defer_all_probes) 
   790             driver_deferred_probe_trigger(); 
   791     } 
   792     atomic_dec(&probe_count); 
   793     wake_up_all(&probe_waitqueue); 
   794     return ret; 
   795 }

2.1.12 __driver_probe_device

代码路径:drivers/base/dd.c

   730 static int __driver_probe_device(struct device_driver *drv, struct device *dev) 
   731 { 
   732     int ret = 0; 
   733  
   734     if (dev->p->dead || !device_is_registered(dev)) 
   735         return -ENODEV; 
   736     if (dev->driver) 
   737         return -EBUSY; 
   738  
   739     dev->can_match = true; 
   740     pr_debug("bus: '%s': %s: matched device %s with driver %s\n", 
   741          drv->bus->name, __func__, dev_name(dev), drv->name); 
   742  
   743     pm_runtime_get_suppliers(dev); 
   744     if (dev->parent) 
   745         pm_runtime_get_sync(dev->parent); 
   746                                                                                                                                                                                                        
   747     pm_runtime_barrier(dev); 
   748     if (initcall_debug) 
   749         ret = really_probe_debug(dev, drv); 
   750     else 
   751         ret = really_probe(dev, drv); 
   752     pm_request_idle(dev); 
   753  
   754     if (dev->parent) 
   755         pm_runtime_put(dev->parent); 
   756  
   757     pm_runtime_put_suppliers(dev); 
   758     return ret; 
   759 }

2.1.14 really_probe

代码路径:drivers/base/dd.c

   541 static int really_probe(struct device *dev, struct device_driver *drv) 
   542 { 
   543     bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) && 
   544                !drv->suppress_bind_attrs; 
   545     int ret; 
   546  
   547     if (defer_all_probes) {
   548         /*
   549          * Value of defer_all_probes can be set only by 
   550          * device_block_probing() which, in turn, will call 
   551          * wait_for_device_probe() right after that to avoid any races. 
   552          */
   553         dev_dbg(dev, "Driver %s force probe deferral\n", drv->name); 
   554         return -EPROBE_DEFER; 
   555     }
   556  
   557     ret = device_links_check_suppliers(dev);
   558     if (ret)
   559         return ret;
   560  
   561     pr_debug("bus: '%s': %s: probing driver %s with device %s\n", 
   562          drv->bus->name, __func__, drv->name, dev_name(dev)); 
   563     if (!list_empty(&dev->devres_head)) { 
   564         dev_crit(dev, "Resources present before probing\n"); 
   565         ret = -EBUSY; 
   566         goto done; 
   567     } 
   568  
   569 re_probe: 
   570     dev->driver = drv; 
   571  
   572     /* If using pinctrl, bind pins now before probing */ 
   573     ret = pinctrl_bind_pins(dev); 
   574     if (ret) 
   575         goto pinctrl_bind_failed; 
   576  
   577     if (dev->bus->dma_configure) { 
   578         ret = dev->bus->dma_configure(dev); 
   579         if (ret) 
   580             goto probe_failed; 
   581     } 
   582  
   583     ret = driver_sysfs_add(dev); 
   584     if (ret) {
   585         pr_err("%s: driver_sysfs_add(%s) failed\n", 
   586                __func__, dev_name(dev)); 
   587         goto probe_failed; 
   588     } 
   589  
   590     if (dev->pm_domain && dev->pm_domain->activate) { 
   591         ret = dev->pm_domain->activate(dev); 
   592         if (ret) 
   593             goto probe_failed; 
   594     } 
   595  
   596     ret = call_driver_probe(dev, drv); 
   597     if (ret) { 
   598         /* 
   599          * Return probe errors as positive values so that the callers 
   600          * can distinguish them from other errors. 
   601          */ 
   602         ret = -ret; 
   603         goto probe_failed; 
   604     } 
   605  
   606     ret = device_add_groups(dev, drv->dev_groups); 
   607     if (ret) { 
   608         dev_err(dev, "device_add_groups() failed\n"); 
   609         goto dev_groups_failed; 
   610     } 
   611  
   612     if (dev_has_sync_state(dev)) { 
   613         ret = device_create_file(dev, &dev_attr_state_synced); 
   614         if (ret) { 
   615             dev_err(dev, "state_synced sysfs add failed\n"); 
   616             goto dev_sysfs_state_synced_failed;                                                                                                                                                        
   617         } 
   618     } 
   619  
   620     if (test_remove) { 
   621         test_remove = false; 
   622  
   623         device_remove_file(dev, &dev_attr_state_synced); 
   624         device_remove_groups(dev, drv->dev_groups); 
   625  
   626         if (dev->bus->remove) 
   627             dev->bus->remove(dev); 
   628         else if (drv->remove) 
   629             drv->remove(dev);
   630  
   631         devres_release_all(dev); 
   632         driver_sysfs_remove(dev); 
   633         dev->driver = NULL; 
   634         dev_set_drvdata(dev, NULL); 
   635         if (dev->pm_domain && dev->pm_domain->dismiss) 
   636             dev->pm_domain->dismiss(dev); 
   637         pm_runtime_reinit(dev); 
   638  
   639         goto re_probe; 
   640     } 
   641  
   642     pinctrl_init_done(dev); 
   643  
   644     if (dev->pm_domain && dev->pm_domain->sync) 
   645         dev->pm_domain->sync(dev); 
   646  
   647     driver_bound(dev); 
   648     pr_debug("bus: '%s': %s: bound device %s to driver %s\n", 
   649          drv->bus->name, __func__, dev_name(dev), drv->name); 
   650     goto done; 
   651  
   652 dev_sysfs_state_synced_failed: 
   653     device_remove_groups(dev, drv->dev_groups); 
   654 dev_groups_failed: 
   655     if (dev->bus->remove) 
   656         dev->bus->remove(dev); 
   657     else if (drv->remove) 
   658         drv->remove(dev); 
   659 probe_failed: 
   660     if (dev->bus) 
   661         blocking_notifier_call_chain(&dev->bus->p->bus_notifier, 
   662                          BUS_NOTIFY_DRIVER_NOT_BOUND, dev); 
   663 pinctrl_bind_failed:                                                                                                                                                                                   
   664     device_links_no_driver(dev); 
   665     devres_release_all(dev); 
   666     arch_teardown_dma_ops(dev); 
   667     kfree(dev->dma_range_map); 
   668     dev->dma_range_map = NULL; 
   669     driver_sysfs_remove(dev); 
   670     dev->driver = NULL; 
   671     dev_set_drvdata(dev, NULL); 
   672     if (dev->pm_domain && dev->pm_domain->dismiss) 
   673         dev->pm_domain->dismiss(dev); 
   674     pm_runtime_reinit(dev); 
   675     dev_pm_set_driver_flags(dev, 0); 
   676 done:
   677     return ret; 
   678 }

2.1.15 call_driver_probe

对于挂载在PCI/PCIe总线上的设备来说,在调用call_driver_probe 的时候就会通过ret = drv->probe(dev)调用到对应的设备驱动的probe函数。
代码路径:drivers/base/dd.c

   510 static int call_driver_probe(struct device *dev, struct device_driver *drv) 
   511 { 
   512     int ret = 0; 
   513  
   514     if (dev->bus->probe) 
   515         ret = dev->bus->probe(dev); 
   516     else if (drv->probe)
   517         ret = drv->probe(dev); 
   518  
   519     switch (ret) {
   520     case 0: 
   521         break; 
   522     case -EPROBE_DEFER: 
   523         /* Driver requested deferred probing */ 
   524         dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name); 
   525         break; 
   526     case -ENODEV:                                                                                                                                                                                      
   527     case -ENXIO: 
   528         pr_debug("%s: probe of %s rejects match %d\n", 
   529              drv->name, dev_name(dev), ret); 
   530         break; 
   531     default: 
   532         /* driver matched but the probe failed */ 
   533         pr_warn("%s: probe of %s failed with error %d\n", 
   534             drv->name, dev_name(dev), ret); 
   535         break; 
   536     } 
   537  
   538     return ret; 
   539 }

2.1.16 pci_bus_type

代码路径:drivers/pci/pci-driver.c

  1600 struct bus_type pci_bus_type = { 
  1601     .name       = "pci", 
  1602     .match      = pci_bus_match, 
  1603     .uevent     = pci_uevent, 
  1604     .probe      = pci_device_probe, 
  1605     .remove     = pci_device_remove, 
  1606     .shutdown   = pci_device_shutdown, 
  1607     .dev_groups = pci_dev_groups, 
  1608     .bus_groups = pci_bus_groups,                                                                                                                                                                      
  1609     .drv_groups = pci_drv_groups, 
  1610     .pm     = PCI_PM_OPS_PTR, 
  1611     .num_vf     = pci_bus_num_vf, 
  1612     .dma_configure  = pci_dma_configure, 
  1613 }; 
  1614 EXPORT_SYMBOL(pci_bus_type);

2.1.17 pci_device_probe

pci_device_probe对应着drv->probe(dev),通过pci_device_probe函数会调用到对应的设备驱动的probe函数。
代码路径:drivers/pci/pci-driver.c

   418 static int pci_device_probe(struct device *dev) 
   419 { 
   420     int error;          
   421     struct pci_dev *pci_dev = to_pci_dev(dev); 
   422     struct pci_driver *drv = to_pci_driver(dev->driver); 
   423  
   424     if (!pci_device_can_probe(pci_dev)) 
   425         return -ENODEV; 
   426  
   427     pci_assign_irq(pci_dev); 
   428  
   429     error = pcibios_alloc_irq(pci_dev); 
   430     if (error < 0)      
   431         return error;   
   432  
   433     pci_dev_get(pci_dev); 
   434     error = __pci_device_probe(drv, pci_dev);                                                                                                                                                          
   435     if (error) { 
   436         pcibios_free_irq(pci_dev);      
   437         pci_dev_put(pci_dev); 
   438     } 
   439  
   440     return error; 
   441 }

2.1.18 __pci_device_probe

代码路径:drivers/pci/pci-driver.c

   373 /** 
   374  * __pci_device_probe - check if a driver wants to claim a specific PCI device 
   375  * @drv: driver to call to check if it wants the PCI device 
   376  * @pci_dev: PCI device being probed 
   377  * 
   378  * returns 0 on success, else error. 
   379  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev. 
   380  */ 
   381 static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev) 
   382 { 
   383     const struct pci_device_id *id; 
   384     int error = 0; 
   385  
   386     if (!pci_dev->driver && drv->probe) { 
   387         error = -ENODEV;                                                                                                                                                                               
   388  
   389         id = pci_match_device(drv, pci_dev);  -->2.1.11的代码流程分析
   390         if (id) 
   391             error = pci_call_probe(drv, pci_dev, id); 
   392     } 
   393     return error; 
   394 }

2.1.19 pci_call_probe

代码路径:drivers/pci/pci-driver.c

   335 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev, 
   336               const struct pci_device_id *id) 
   337 { 
   338     int error, node, cpu; 
   339     int hk_flags = HK_FLAG_DOMAIN | HK_FLAG_WQ; 
   340     struct drv_dev_and_id ddi = { drv, dev, id }; 
   341  
   342     /* 
   343      * Execute driver initialization on node where the device is 
   344      * attached.  This way the driver likely allocates its local memory 
   345      * on the right node. 
   346      */ 
   347     node = dev_to_node(&dev->dev); 
   348     dev->is_probed = 1; 
   349  
   350     cpu_hotplug_disable(); 
   351  
   352     /* 
   353      * Prevent nesting work_on_cpu() for the case where a Virtual Function 
   354      * device is probed from work_on_cpu() of the Physical device. 
   355      */ 
   356     if (node < 0 || node >= MAX_NUMNODES || !node_online(node) || 
   357         pci_physfn_is_probed(dev)) 
   358         cpu = nr_cpu_ids; 
   359     else 
   360         cpu = cpumask_any_and(cpumask_of_node(node), 
   361                       housekeeping_cpumask(hk_flags));                                                                                                                                                 
   362  
   363     if (cpu < nr_cpu_ids) 
   364         error = work_on_cpu(cpu, local_pci_probe, &ddi); 
   365     else 
   366         error = local_pci_probe(&ddi); 
   367  
   368     dev->is_probed = 0; 
   369     cpu_hotplug_enable(); 
   370     return error; 
   371 }

2.1.20 local_pci_probe

代码路径:drivers/pci/pci-driver.c

   290 static long local_pci_probe(void *_ddi) 
   291 { 
   292     struct drv_dev_and_id *ddi = _ddi; 
   293     struct pci_dev *pci_dev = ddi->dev; 
   294     struct pci_driver *pci_drv = ddi->drv; 
   295     struct device *dev = &pci_dev->dev; 
   296     int rc; 
   297  
   298     /* 
   299      * Unbound PCI devices are always put in D0, regardless of 
   300      * runtime PM status.  During probe, the device is set to 
   301      * active and the usage count is incremented.  If the driver 
   302      * supports runtime PM, it should call pm_runtime_put_noidle(), 
   303      * or any other runtime PM helper function decrementing the usage 
   304      * count, in its probe routine and pm_runtime_get_noresume() in 
   305      * its remove routine. 
   306      */ 
   307     pm_runtime_get_sync(dev); 
   308     pci_dev->driver = pci_drv; 
   309     rc = pci_drv->probe(pci_dev, ddi->id); 
   310     if (!rc) 
   311         return rc; 
   312     if (rc < 0) { 
   313         pci_dev->driver = NULL; 
   314         pm_runtime_put_sync(dev); 
   315         return rc;                                                                                                                                                                                     
   316     } 
   317     /* 
   318      * Probe function should return < 0 for failure, 0 for success 
   319      * Treat values > 0 as success, but warn. 
   320      */ 
   321     pci_warn(pci_dev, "Driver probe function unexpectedly returned %d\n", 
   322          rc); 
   323     return 0; 
   324 }

2.1.21 nvme_probe

  2882 static int nvme_probe(struct pci_dev *pdev, const struct pci_device_id *id) 
  2883 { 
          ...
  2972 }

3 device注册流程

以register_virtio_device为例来介绍,其中register_virtio_device会的注册会使得前期注册的驱动probe函数被调用到。
在这里插入图片描述

Logo

更多推荐