platform_driver_register()--如何match之后调用probe

来源:互联网 发布:麦迪巅峰数据 编辑:程序博客网 时间:2024/05/30 04:31
  1. int platform_driver_register(struct platform_driver *drv)  
  2. {  
  3.     drv->driver.bus = &platform_bus_type;/*关联总线*/  
  4.     /*关联driver的设备方法*/  
  5.     if (drv->probe)  
  6.         drv->driver.probe = platform_drv_probe;  
  7.     if (drv->remove)  
  8.         drv->driver.remove = platform_drv_remove;  
  9.     if (drv->shutdown)  
  10.         drv->driver.shutdown = platform_drv_shutdown;  
  11.   
  12.     return driver_register(&drv->driver);/*注册驱动*/  
  13. }  
  14.   
  15. /******************************************************************************/  
  16. struct platform_driver {  
  17.     int (*probe)(struct platform_device *);/*匹配到设备后调用,下面分析内核代码怎么调用的*/  
  18.     int (*remove)(struct platform_device *);  
  19.     void (*shutdown)(struct platform_device *);  
  20.     int (*suspend)(struct platform_device *, pm_message_t state);  
  21.     int (*resume)(struct platform_device *);  
  22.     struct device_driver driver;  
  23.     const struct platform_device_id *id_table;  
  24. };  
  25.   
  26. struct bus_type platform_bus_type = {  
  27.     .name        = "platform",  
  28.     .dev_attrs    = platform_dev_attrs,  
  29.     .match        = platform_match,  
  30.     .uevent        = platform_uevent,  
  31.     .pm        = &platform_dev_pm_ops,  
  32. };  
  33. /********************************************************************************/  
  34.   
  35. int driver_register(struct device_driver *drv)  
  36. {  
  37.     int ret;  
  38.     struct device_driver *other;  
  39.   
  40.     BUG_ON(!drv->bus->p);  
  41.   
  42.     if ((drv->bus->probe && drv->probe) ||  
  43.         (drv->bus->remove && drv->remove) ||  
  44.         (drv->bus->shutdown && drv->shutdown))  
  45.         printk(KERN_WARNING "Driver '%s' needs updating - please use "  
  46.             "bus_type methods\n", drv->name);  
  47.   
  48.     other = driver_find(drv->name, drv->bus);  
  49.     if (other) {  
  50.         put_driver(other);  
  51.         printk(KERN_ERR "Error: Driver '%s' is already registered, "  
  52.             "aborting...\n", drv->name);  
  53.         return -EBUSY;  
  54.     }  
  55.   
  56.     ret = bus_add_driver(drv);  
  57.     if (ret)  
  58.         return ret;  
  59.     ret = driver_add_groups(drv, drv->groups);  
  60.     if (ret)  
  61.         bus_remove_driver(drv);  
  62.     return ret;  
  63. }  
  64.   
  65. int bus_add_driver(struct device_driver *drv)  
  66. {  
  67.     struct bus_type *bus;  
  68.     struct driver_private *priv;  
  69.     int error = 0;  
  70.   
  71.     bus = bus_get(drv->bus);  
  72.     if (!bus)  
  73.         return -EINVAL;  
  74.   
  75.     pr_debug("bus: '%s': add driver %s\n", bus->name, drv->name);  
  76.   
  77.     priv = kzalloc(sizeof(*priv), GFP_KERNEL);  
  78.     if (!priv) {  
  79.         error = -ENOMEM;  
  80.         goto out_put_bus;  
  81.     }  
  82.     klist_init(&priv->klist_devices, NULL, NULL);  
  83.     priv->driver = drv;  
  84.     drv->p = priv;  
  85.     priv->kobj.kset = bus->p->drivers_kset;  
  86.     error = kobject_init_and_add(&priv->kobj, &driver_ktype, NULL,  
  87.                      "%s", drv->name);  
  88.     if (error)  
  89.         goto out_unregister;  
  90.   
  91.     if (drv->bus->p->drivers_autoprobe) {  
  92.         error = driver_attach(drv);  
  93.         if (error)  
  94.             goto out_unregister;  
  95.     }  
  96.     klist_add_tail(&priv->knode_bus, &bus->p->klist_drivers);  
  97.     module_add_driver(drv->owner, drv);  
  98.   
  99.     error = driver_create_file(drv, &driver_attr_uevent);  
  100.     if (error) {  
  101.         printk(KERN_ERR "%s: uevent attr (%s) failed\n",  
  102.             __func__, drv->name);  
  103.     }  
  104.     error = driver_add_attrs(bus, drv);  
  105.     if (error) {  
  106.         /* How the hell do we get out of this pickle? Give up */  
  107.         printk(KERN_ERR "%s: driver_add_attrs(%s) failed\n",  
  108.             __func__, drv->name);  
  109.     }  
  110.   
  111.     if (!drv->suppress_bind_attrs) {  
  112.         error = add_bind_files(drv);  
  113.         if (error) {  
  114.             /* Ditto */  
  115.             printk(KERN_ERR "%s: add_bind_files(%s) failed\n",  
  116.                 __func__, drv->name);  
  117.         }  
  118.     }  
  119.   
  120.     kobject_uevent(&priv->kobj, KOBJ_ADD);  
  121.     return 0;  
  122.   
  123. out_unregister:  
  124.     kobject_put(&priv->kobj);  
  125.     kfree(drv->p);  
  126.     drv->p = NULL;  
  127. out_put_bus:  
  128.     bus_put(bus);  
  129.     return error;  
  130. }  
  131.   
  132. int driver_attach(struct device_driver *drv)  
  133. {  
  134.     /*对总线上的每一个设备都调用__driver_attach*/  
  135.     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);  
  136. }  
  137.   
  138. static int __driver_attach(struct device *dev, void *data)  
  139. {  
  140.     struct device_driver *drv = data;  
  141.   
  142.     /* 
  143.      * Lock device and try to bind to it. We drop the error 
  144.      * here and always return 0, because we need to keep trying 
  145.      * to bind to devices and some drivers will return an error 
  146.      * simply if it didn't support the device. 
  147.      * 
  148.      * driver_probe_device() will spit a warning if there 
  149.      * is an error. 
  150.      */  
  151.   
  152.     if (!driver_match_device(drv, dev))  
  153.         return 0;  
  154.   
  155.     if (dev->parent)    /* Needed for USB */  
  156.         device_lock(dev->parent);  
  157.     device_lock(dev);  
  158.     if (!dev->driver)  
  159.         driver_probe_device(drv, dev);  
  160.     device_unlock(dev);  
  161.     if (dev->parent)  
  162.         device_unlock(dev->parent);  
  163.   
  164.     return 0;  
  165. }  
  166.   
  167. static inline int driver_match_device(struct device_driver *drv,  
  168.                       struct device *dev)  
  169. {  
  170.     /*调用总线的match去匹配设备和驱动*/  
  171.     return drv->bus->match ? drv->bus->match(dev, drv) : 1;  
  172. }  
  173.   
  174. int driver_probe_device(struct device_driver *drv, struct device *dev)  
  175. {  
  176.     int ret = 0;  
  177.   
  178.     if (!device_is_registered(dev))  
  179.         return -ENODEV;  
  180.   
  181.     pr_debug("bus: '%s': %s: matched device %s with driver %s\n",  
  182.          drv->bus->name, __func__, dev_name(dev), drv->name);  
  183.   
  184.     pm_runtime_get_noresume(dev);  
  185.     pm_runtime_barrier(dev);  
  186.     ret = really_probe(dev, drv);  
  187.     pm_runtime_put_sync(dev);  
  188.   
  189.     return ret;  
  190. }  
  191. static int really_probe(struct device *dev, struct device_driver *drv)  
  192. {  
  193.     int ret = 0;  
  194.   
  195.     atomic_inc(&probe_count);  
  196.     pr_debug("bus: '%s': %s: probing driver %s with device %s\n",  
  197.          drv->bus->name, __func__, drv->name, dev_name(dev));  
  198.     WARN_ON(!list_empty(&dev->devres_head));  
  199.   
  200.     dev->driver = drv;  
  201.     if (driver_sysfs_add(dev)) {  
  202.         printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",  
  203.             __func__, dev_name(dev));  
  204.         goto probe_failed;  
  205.     }  
  206. /**********************************************************************************/  
  207.     if (dev->bus->probe) {/*首先看总线有没有probe函数,若有则调用,而平台总线没有probe*/  
  208.         ret = dev->bus->probe(dev);  
  209.         if (ret)  
  210.             goto probe_failed;  
  211.     } else if (drv->probe) {/*然后看驱动有没有probe函数,若有则调用,*/  
  212.         ret = drv->probe(dev);  
  213.         if (ret)  
  214.             goto probe_failed;  
  215.     }  
  216. /************************************************************************************/  
  217.   
  218.     driver_bound(dev);  
  219.     ret = 1;  
  220.     pr_debug("bus: '%s': %s: bound device %s to driver %s\n",  
  221.          drv->bus->name, __func__, dev_name(dev), drv->name);  
  222.     goto done;  
  223.   
  224. probe_failed:  
  225.     devres_release_all(dev);  
  226.     driver_sysfs_remove(dev);  
  227.     dev->driver = NULL;  
  228.   
  229.     if (ret != -ENODEV && ret != -ENXIO) {  
  230.         /* driver matched but the probe failed */  
  231.         printk(KERN_WARNING  
  232.                "%s: probe of %s failed with error %d\n",  
  233.                drv->name, dev_name(dev), ret);  
  234.     }  
  235.     /* 
  236.      * Ignore errors returned by ->probe so that the next driver can try 
  237.      * its luck. 
  238.      */  
  239.     ret = 0;  
  240. done:  
  241.     atomic_dec(&probe_count);  
  242.     wake_up(&probe_waitqueue);  
  243.     return ret;  
  244. }  
  245.   
  246.   
  247. /*平台总线的match逻辑*/  
  248. static int platform_match(struct device *dev, struct device_driver *drv)  
  249. {  
  250.     struct platform_device *pdev = to_platform_device(dev);  
  251.     struct platform_driver *pdrv = to_platform_driver(drv);  
  252.   
  253.     /* match against the id table first */  
  254.     if (pdrv->id_table)  
  255.         return platform_match_id(pdrv->id_table, pdev) != NULL;  
  256.   
  257.     /* fall-back to driver name match */  
  258.     return (strcmp(pdev->name, drv->name) == 0);/*驱动名字与设备名字要匹配*/  
  259. }  
原创粉丝点击