Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * drivers/base/dd.c - The core device/driver interactions.
0004  *
0005  * This file contains the (sometimes tricky) code that controls the
0006  * interactions between devices and drivers, which primarily includes
0007  * driver binding and unbinding.
0008  *
0009  * All of this code used to exist in drivers/base/bus.c, but was
0010  * relocated to here in the name of compartmentalization (since it wasn't
0011  * strictly code just for the 'struct bus_type'.
0012  *
0013  * Copyright (c) 2002-5 Patrick Mochel
0014  * Copyright (c) 2002-3 Open Source Development Labs
0015  * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
0016  * Copyright (c) 2007-2009 Novell Inc.
0017  */
0018 
0019 #include <linux/debugfs.h>
0020 #include <linux/device.h>
0021 #include <linux/delay.h>
0022 #include <linux/dma-map-ops.h>
0023 #include <linux/init.h>
0024 #include <linux/module.h>
0025 #include <linux/kthread.h>
0026 #include <linux/wait.h>
0027 #include <linux/async.h>
0028 #include <linux/pm_runtime.h>
0029 #include <linux/pinctrl/devinfo.h>
0030 #include <linux/slab.h>
0031 
0032 #include "base.h"
0033 #include "power/power.h"
0034 
0035 /*
0036  * Deferred Probe infrastructure.
0037  *
0038  * Sometimes driver probe order matters, but the kernel doesn't always have
0039  * dependency information which means some drivers will get probed before a
0040  * resource it depends on is available.  For example, an SDHCI driver may
0041  * first need a GPIO line from an i2c GPIO controller before it can be
0042  * initialized.  If a required resource is not available yet, a driver can
0043  * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
0044  *
0045  * Deferred probe maintains two lists of devices, a pending list and an active
0046  * list.  A driver returning -EPROBE_DEFER causes the device to be added to the
0047  * pending list.  A successful driver probe will trigger moving all devices
0048  * from the pending to the active list so that the workqueue will eventually
0049  * retry them.
0050  *
0051  * The deferred_probe_mutex must be held any time the deferred_probe_*_list
0052  * of the (struct device*)->p->deferred_probe pointers are manipulated
0053  */
0054 static DEFINE_MUTEX(deferred_probe_mutex);
0055 static LIST_HEAD(deferred_probe_pending_list);
0056 static LIST_HEAD(deferred_probe_active_list);
0057 static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
0058 static bool initcalls_done;
0059 
0060 /* Save the async probe drivers' name from kernel cmdline */
0061 #define ASYNC_DRV_NAMES_MAX_LEN 256
0062 static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
0063 static bool async_probe_default;
0064 
0065 /*
0066  * In some cases, like suspend to RAM or hibernation, It might be reasonable
0067  * to prohibit probing of devices as it could be unsafe.
0068  * Once defer_all_probes is true all drivers probes will be forcibly deferred.
0069  */
0070 static bool defer_all_probes;
0071 
0072 static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
0073 {
0074     kfree(dev->p->deferred_probe_reason);
0075     dev->p->deferred_probe_reason = reason;
0076 }
0077 
0078 /*
0079  * deferred_probe_work_func() - Retry probing devices in the active list.
0080  */
0081 static void deferred_probe_work_func(struct work_struct *work)
0082 {
0083     struct device *dev;
0084     struct device_private *private;
0085     /*
0086      * This block processes every device in the deferred 'active' list.
0087      * Each device is removed from the active list and passed to
0088      * bus_probe_device() to re-attempt the probe.  The loop continues
0089      * until every device in the active list is removed and retried.
0090      *
0091      * Note: Once the device is removed from the list and the mutex is
0092      * released, it is possible for the device get freed by another thread
0093      * and cause a illegal pointer dereference.  This code uses
0094      * get/put_device() to ensure the device structure cannot disappear
0095      * from under our feet.
0096      */
0097     mutex_lock(&deferred_probe_mutex);
0098     while (!list_empty(&deferred_probe_active_list)) {
0099         private = list_first_entry(&deferred_probe_active_list,
0100                     typeof(*dev->p), deferred_probe);
0101         dev = private->device;
0102         list_del_init(&private->deferred_probe);
0103 
0104         get_device(dev);
0105 
0106         __device_set_deferred_probe_reason(dev, NULL);
0107 
0108         /*
0109          * Drop the mutex while probing each device; the probe path may
0110          * manipulate the deferred list
0111          */
0112         mutex_unlock(&deferred_probe_mutex);
0113 
0114         /*
0115          * Force the device to the end of the dpm_list since
0116          * the PM code assumes that the order we add things to
0117          * the list is a good order for suspend but deferred
0118          * probe makes that very unsafe.
0119          */
0120         device_pm_move_to_tail(dev);
0121 
0122         dev_dbg(dev, "Retrying from deferred list\n");
0123         bus_probe_device(dev);
0124         mutex_lock(&deferred_probe_mutex);
0125 
0126         put_device(dev);
0127     }
0128     mutex_unlock(&deferred_probe_mutex);
0129 }
0130 static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
0131 
0132 void driver_deferred_probe_add(struct device *dev)
0133 {
0134     if (!dev->can_match)
0135         return;
0136 
0137     mutex_lock(&deferred_probe_mutex);
0138     if (list_empty(&dev->p->deferred_probe)) {
0139         dev_dbg(dev, "Added to deferred list\n");
0140         list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
0141     }
0142     mutex_unlock(&deferred_probe_mutex);
0143 }
0144 
0145 void driver_deferred_probe_del(struct device *dev)
0146 {
0147     mutex_lock(&deferred_probe_mutex);
0148     if (!list_empty(&dev->p->deferred_probe)) {
0149         dev_dbg(dev, "Removed from deferred list\n");
0150         list_del_init(&dev->p->deferred_probe);
0151         __device_set_deferred_probe_reason(dev, NULL);
0152     }
0153     mutex_unlock(&deferred_probe_mutex);
0154 }
0155 
0156 static bool driver_deferred_probe_enable;
0157 /**
0158  * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
0159  *
0160  * This functions moves all devices from the pending list to the active
0161  * list and schedules the deferred probe workqueue to process them.  It
0162  * should be called anytime a driver is successfully bound to a device.
0163  *
0164  * Note, there is a race condition in multi-threaded probe. In the case where
0165  * more than one device is probing at the same time, it is possible for one
0166  * probe to complete successfully while another is about to defer. If the second
0167  * depends on the first, then it will get put on the pending list after the
0168  * trigger event has already occurred and will be stuck there.
0169  *
0170  * The atomic 'deferred_trigger_count' is used to determine if a successful
0171  * trigger has occurred in the midst of probing a driver. If the trigger count
0172  * changes in the midst of a probe, then deferred processing should be triggered
0173  * again.
0174  */
0175 void driver_deferred_probe_trigger(void)
0176 {
0177     if (!driver_deferred_probe_enable)
0178         return;
0179 
0180     /*
0181      * A successful probe means that all the devices in the pending list
0182      * should be triggered to be reprobed.  Move all the deferred devices
0183      * into the active list so they can be retried by the workqueue
0184      */
0185     mutex_lock(&deferred_probe_mutex);
0186     atomic_inc(&deferred_trigger_count);
0187     list_splice_tail_init(&deferred_probe_pending_list,
0188                   &deferred_probe_active_list);
0189     mutex_unlock(&deferred_probe_mutex);
0190 
0191     /*
0192      * Kick the re-probe thread.  It may already be scheduled, but it is
0193      * safe to kick it again.
0194      */
0195     queue_work(system_unbound_wq, &deferred_probe_work);
0196 }
0197 
0198 /**
0199  * device_block_probing() - Block/defer device's probes
0200  *
0201  *  It will disable probing of devices and defer their probes instead.
0202  */
0203 void device_block_probing(void)
0204 {
0205     defer_all_probes = true;
0206     /* sync with probes to avoid races. */
0207     wait_for_device_probe();
0208 }
0209 
0210 /**
0211  * device_unblock_probing() - Unblock/enable device's probes
0212  *
0213  *  It will restore normal behavior and trigger re-probing of deferred
0214  * devices.
0215  */
0216 void device_unblock_probing(void)
0217 {
0218     defer_all_probes = false;
0219     driver_deferred_probe_trigger();
0220 }
0221 
0222 /**
0223  * device_set_deferred_probe_reason() - Set defer probe reason message for device
0224  * @dev: the pointer to the struct device
0225  * @vaf: the pointer to va_format structure with message
0226  */
0227 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
0228 {
0229     const char *drv = dev_driver_string(dev);
0230     char *reason;
0231 
0232     mutex_lock(&deferred_probe_mutex);
0233 
0234     reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
0235     __device_set_deferred_probe_reason(dev, reason);
0236 
0237     mutex_unlock(&deferred_probe_mutex);
0238 }
0239 
0240 /*
0241  * deferred_devs_show() - Show the devices in the deferred probe pending list.
0242  */
0243 static int deferred_devs_show(struct seq_file *s, void *data)
0244 {
0245     struct device_private *curr;
0246 
0247     mutex_lock(&deferred_probe_mutex);
0248 
0249     list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
0250         seq_printf(s, "%s\t%s", dev_name(curr->device),
0251                curr->device->p->deferred_probe_reason ?: "\n");
0252 
0253     mutex_unlock(&deferred_probe_mutex);
0254 
0255     return 0;
0256 }
0257 DEFINE_SHOW_ATTRIBUTE(deferred_devs);
0258 
0259 #ifdef CONFIG_MODULES
0260 int driver_deferred_probe_timeout = 10;
0261 #else
0262 int driver_deferred_probe_timeout;
0263 #endif
0264 
0265 EXPORT_SYMBOL_GPL(driver_deferred_probe_timeout);
0266 
0267 static int __init deferred_probe_timeout_setup(char *str)
0268 {
0269     int timeout;
0270 
0271     if (!kstrtoint(str, 10, &timeout))
0272         driver_deferred_probe_timeout = timeout;
0273     return 1;
0274 }
0275 __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
0276 
0277 /**
0278  * driver_deferred_probe_check_state() - Check deferred probe state
0279  * @dev: device to check
0280  *
0281  * Return:
0282  * * -ENODEV if initcalls have completed and modules are disabled.
0283  * * -ETIMEDOUT if the deferred probe timeout was set and has expired
0284  *   and modules are enabled.
0285  * * -EPROBE_DEFER in other cases.
0286  *
0287  * Drivers or subsystems can opt-in to calling this function instead of directly
0288  * returning -EPROBE_DEFER.
0289  */
0290 int driver_deferred_probe_check_state(struct device *dev)
0291 {
0292     if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
0293         dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
0294         return -ENODEV;
0295     }
0296 
0297     if (!driver_deferred_probe_timeout && initcalls_done) {
0298         dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
0299         return -ETIMEDOUT;
0300     }
0301 
0302     return -EPROBE_DEFER;
0303 }
0304 EXPORT_SYMBOL_GPL(driver_deferred_probe_check_state);
0305 
0306 static void deferred_probe_timeout_work_func(struct work_struct *work)
0307 {
0308     struct device_private *p;
0309 
0310     fw_devlink_drivers_done();
0311 
0312     driver_deferred_probe_timeout = 0;
0313     driver_deferred_probe_trigger();
0314     flush_work(&deferred_probe_work);
0315 
0316     mutex_lock(&deferred_probe_mutex);
0317     list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
0318         dev_info(p->device, "deferred probe pending\n");
0319     mutex_unlock(&deferred_probe_mutex);
0320 }
0321 static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
0322 
0323 void deferred_probe_extend_timeout(void)
0324 {
0325     /*
0326      * If the work hasn't been queued yet or if the work expired, don't
0327      * start a new one.
0328      */
0329     if (cancel_delayed_work(&deferred_probe_timeout_work)) {
0330         schedule_delayed_work(&deferred_probe_timeout_work,
0331                 driver_deferred_probe_timeout * HZ);
0332         pr_debug("Extended deferred probe timeout by %d secs\n",
0333                     driver_deferred_probe_timeout);
0334     }
0335 }
0336 
0337 /**
0338  * deferred_probe_initcall() - Enable probing of deferred devices
0339  *
0340  * We don't want to get in the way when the bulk of drivers are getting probed.
0341  * Instead, this initcall makes sure that deferred probing is delayed until
0342  * late_initcall time.
0343  */
0344 static int deferred_probe_initcall(void)
0345 {
0346     debugfs_create_file("devices_deferred", 0444, NULL, NULL,
0347                 &deferred_devs_fops);
0348 
0349     driver_deferred_probe_enable = true;
0350     driver_deferred_probe_trigger();
0351     /* Sort as many dependencies as possible before exiting initcalls */
0352     flush_work(&deferred_probe_work);
0353     initcalls_done = true;
0354 
0355     if (!IS_ENABLED(CONFIG_MODULES))
0356         fw_devlink_drivers_done();
0357 
0358     /*
0359      * Trigger deferred probe again, this time we won't defer anything
0360      * that is optional
0361      */
0362     driver_deferred_probe_trigger();
0363     flush_work(&deferred_probe_work);
0364 
0365     if (driver_deferred_probe_timeout > 0) {
0366         schedule_delayed_work(&deferred_probe_timeout_work,
0367             driver_deferred_probe_timeout * HZ);
0368     }
0369     return 0;
0370 }
0371 late_initcall(deferred_probe_initcall);
0372 
0373 static void __exit deferred_probe_exit(void)
0374 {
0375     debugfs_remove_recursive(debugfs_lookup("devices_deferred", NULL));
0376 }
0377 __exitcall(deferred_probe_exit);
0378 
0379 /**
0380  * device_is_bound() - Check if device is bound to a driver
0381  * @dev: device to check
0382  *
0383  * Returns true if passed device has already finished probing successfully
0384  * against a driver.
0385  *
0386  * This function must be called with the device lock held.
0387  */
0388 bool device_is_bound(struct device *dev)
0389 {
0390     return dev->p && klist_node_attached(&dev->p->knode_driver);
0391 }
0392 
0393 static void driver_bound(struct device *dev)
0394 {
0395     if (device_is_bound(dev)) {
0396         pr_warn("%s: device %s already bound\n",
0397             __func__, kobject_name(&dev->kobj));
0398         return;
0399     }
0400 
0401     pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
0402          __func__, dev_name(dev));
0403 
0404     klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
0405     device_links_driver_bound(dev);
0406 
0407     device_pm_check_callbacks(dev);
0408 
0409     /*
0410      * Make sure the device is no longer in one of the deferred lists and
0411      * kick off retrying all pending devices
0412      */
0413     driver_deferred_probe_del(dev);
0414     driver_deferred_probe_trigger();
0415 
0416     if (dev->bus)
0417         blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
0418                          BUS_NOTIFY_BOUND_DRIVER, dev);
0419 
0420     kobject_uevent(&dev->kobj, KOBJ_BIND);
0421 }
0422 
0423 static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
0424                 const char *buf, size_t count)
0425 {
0426     device_lock(dev);
0427     dev->driver->coredump(dev);
0428     device_unlock(dev);
0429 
0430     return count;
0431 }
0432 static DEVICE_ATTR_WO(coredump);
0433 
0434 static int driver_sysfs_add(struct device *dev)
0435 {
0436     int ret;
0437 
0438     if (dev->bus)
0439         blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
0440                          BUS_NOTIFY_BIND_DRIVER, dev);
0441 
0442     ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
0443                 kobject_name(&dev->kobj));
0444     if (ret)
0445         goto fail;
0446 
0447     ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
0448                 "driver");
0449     if (ret)
0450         goto rm_dev;
0451 
0452     if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump)
0453         return 0;
0454 
0455     ret = device_create_file(dev, &dev_attr_coredump);
0456     if (!ret)
0457         return 0;
0458 
0459     sysfs_remove_link(&dev->kobj, "driver");
0460 
0461 rm_dev:
0462     sysfs_remove_link(&dev->driver->p->kobj,
0463               kobject_name(&dev->kobj));
0464 
0465 fail:
0466     return ret;
0467 }
0468 
0469 static void driver_sysfs_remove(struct device *dev)
0470 {
0471     struct device_driver *drv = dev->driver;
0472 
0473     if (drv) {
0474         if (drv->coredump)
0475             device_remove_file(dev, &dev_attr_coredump);
0476         sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
0477         sysfs_remove_link(&dev->kobj, "driver");
0478     }
0479 }
0480 
0481 /**
0482  * device_bind_driver - bind a driver to one device.
0483  * @dev: device.
0484  *
0485  * Allow manual attachment of a driver to a device.
0486  * Caller must have already set @dev->driver.
0487  *
0488  * Note that this does not modify the bus reference count.
0489  * Please verify that is accounted for before calling this.
0490  * (It is ok to call with no other effort from a driver's probe() method.)
0491  *
0492  * This function must be called with the device lock held.
0493  *
0494  * Callers should prefer to use device_driver_attach() instead.
0495  */
0496 int device_bind_driver(struct device *dev)
0497 {
0498     int ret;
0499 
0500     ret = driver_sysfs_add(dev);
0501     if (!ret) {
0502         device_links_force_bind(dev);
0503         driver_bound(dev);
0504     }
0505     else if (dev->bus)
0506         blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
0507                          BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
0508     return ret;
0509 }
0510 EXPORT_SYMBOL_GPL(device_bind_driver);
0511 
0512 static atomic_t probe_count = ATOMIC_INIT(0);
0513 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
0514 
0515 static ssize_t state_synced_show(struct device *dev,
0516                  struct device_attribute *attr, char *buf)
0517 {
0518     bool val;
0519 
0520     device_lock(dev);
0521     val = dev->state_synced;
0522     device_unlock(dev);
0523 
0524     return sysfs_emit(buf, "%u\n", val);
0525 }
0526 static DEVICE_ATTR_RO(state_synced);
0527 
0528 static void device_unbind_cleanup(struct device *dev)
0529 {
0530     devres_release_all(dev);
0531     arch_teardown_dma_ops(dev);
0532     kfree(dev->dma_range_map);
0533     dev->dma_range_map = NULL;
0534     dev->driver = NULL;
0535     dev_set_drvdata(dev, NULL);
0536     if (dev->pm_domain && dev->pm_domain->dismiss)
0537         dev->pm_domain->dismiss(dev);
0538     pm_runtime_reinit(dev);
0539     dev_pm_set_driver_flags(dev, 0);
0540 }
0541 
0542 static void device_remove(struct device *dev)
0543 {
0544     device_remove_file(dev, &dev_attr_state_synced);
0545     device_remove_groups(dev, dev->driver->dev_groups);
0546 
0547     if (dev->bus && dev->bus->remove)
0548         dev->bus->remove(dev);
0549     else if (dev->driver->remove)
0550         dev->driver->remove(dev);
0551 }
0552 
0553 static int call_driver_probe(struct device *dev, struct device_driver *drv)
0554 {
0555     int ret = 0;
0556 
0557     if (dev->bus->probe)
0558         ret = dev->bus->probe(dev);
0559     else if (drv->probe)
0560         ret = drv->probe(dev);
0561 
0562     switch (ret) {
0563     case 0:
0564         break;
0565     case -EPROBE_DEFER:
0566         /* Driver requested deferred probing */
0567         dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
0568         break;
0569     case -ENODEV:
0570     case -ENXIO:
0571         pr_debug("%s: probe of %s rejects match %d\n",
0572              drv->name, dev_name(dev), ret);
0573         break;
0574     default:
0575         /* driver matched but the probe failed */
0576         pr_warn("%s: probe of %s failed with error %d\n",
0577             drv->name, dev_name(dev), ret);
0578         break;
0579     }
0580 
0581     return ret;
0582 }
0583 
0584 static int really_probe(struct device *dev, struct device_driver *drv)
0585 {
0586     bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
0587                !drv->suppress_bind_attrs;
0588     int ret, link_ret;
0589 
0590     if (defer_all_probes) {
0591         /*
0592          * Value of defer_all_probes can be set only by
0593          * device_block_probing() which, in turn, will call
0594          * wait_for_device_probe() right after that to avoid any races.
0595          */
0596         dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
0597         return -EPROBE_DEFER;
0598     }
0599 
0600     link_ret = device_links_check_suppliers(dev);
0601     if (link_ret == -EPROBE_DEFER)
0602         return link_ret;
0603 
0604     pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
0605          drv->bus->name, __func__, drv->name, dev_name(dev));
0606     if (!list_empty(&dev->devres_head)) {
0607         dev_crit(dev, "Resources present before probing\n");
0608         ret = -EBUSY;
0609         goto done;
0610     }
0611 
0612 re_probe:
0613     dev->driver = drv;
0614 
0615     /* If using pinctrl, bind pins now before probing */
0616     ret = pinctrl_bind_pins(dev);
0617     if (ret)
0618         goto pinctrl_bind_failed;
0619 
0620     if (dev->bus->dma_configure) {
0621         ret = dev->bus->dma_configure(dev);
0622         if (ret)
0623             goto pinctrl_bind_failed;
0624     }
0625 
0626     ret = driver_sysfs_add(dev);
0627     if (ret) {
0628         pr_err("%s: driver_sysfs_add(%s) failed\n",
0629                __func__, dev_name(dev));
0630         goto sysfs_failed;
0631     }
0632 
0633     if (dev->pm_domain && dev->pm_domain->activate) {
0634         ret = dev->pm_domain->activate(dev);
0635         if (ret)
0636             goto probe_failed;
0637     }
0638 
0639     ret = call_driver_probe(dev, drv);
0640     if (ret) {
0641         /*
0642          * If fw_devlink_best_effort is active (denoted by -EAGAIN), the
0643          * device might actually probe properly once some of its missing
0644          * suppliers have probed. So, treat this as if the driver
0645          * returned -EPROBE_DEFER.
0646          */
0647         if (link_ret == -EAGAIN)
0648             ret = -EPROBE_DEFER;
0649 
0650         /*
0651          * Return probe errors as positive values so that the callers
0652          * can distinguish them from other errors.
0653          */
0654         ret = -ret;
0655         goto probe_failed;
0656     }
0657 
0658     ret = device_add_groups(dev, drv->dev_groups);
0659     if (ret) {
0660         dev_err(dev, "device_add_groups() failed\n");
0661         goto dev_groups_failed;
0662     }
0663 
0664     if (dev_has_sync_state(dev)) {
0665         ret = device_create_file(dev, &dev_attr_state_synced);
0666         if (ret) {
0667             dev_err(dev, "state_synced sysfs add failed\n");
0668             goto dev_sysfs_state_synced_failed;
0669         }
0670     }
0671 
0672     if (test_remove) {
0673         test_remove = false;
0674 
0675         device_remove(dev);
0676         driver_sysfs_remove(dev);
0677         device_unbind_cleanup(dev);
0678 
0679         goto re_probe;
0680     }
0681 
0682     pinctrl_init_done(dev);
0683 
0684     if (dev->pm_domain && dev->pm_domain->sync)
0685         dev->pm_domain->sync(dev);
0686 
0687     driver_bound(dev);
0688     pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
0689          drv->bus->name, __func__, dev_name(dev), drv->name);
0690     goto done;
0691 
0692 dev_sysfs_state_synced_failed:
0693 dev_groups_failed:
0694     device_remove(dev);
0695 probe_failed:
0696     driver_sysfs_remove(dev);
0697 sysfs_failed:
0698     if (dev->bus)
0699         blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
0700                          BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
0701     if (dev->bus && dev->bus->dma_cleanup)
0702         dev->bus->dma_cleanup(dev);
0703 pinctrl_bind_failed:
0704     device_links_no_driver(dev);
0705     device_unbind_cleanup(dev);
0706 done:
0707     return ret;
0708 }
0709 
0710 /*
0711  * For initcall_debug, show the driver probe time.
0712  */
0713 static int really_probe_debug(struct device *dev, struct device_driver *drv)
0714 {
0715     ktime_t calltime, rettime;
0716     int ret;
0717 
0718     calltime = ktime_get();
0719     ret = really_probe(dev, drv);
0720     rettime = ktime_get();
0721     pr_debug("probe of %s returned %d after %lld usecs\n",
0722          dev_name(dev), ret, ktime_us_delta(rettime, calltime));
0723     return ret;
0724 }
0725 
0726 /**
0727  * driver_probe_done
0728  * Determine if the probe sequence is finished or not.
0729  *
0730  * Should somehow figure out how to use a semaphore, not an atomic variable...
0731  */
0732 int driver_probe_done(void)
0733 {
0734     int local_probe_count = atomic_read(&probe_count);
0735 
0736     pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
0737     if (local_probe_count)
0738         return -EBUSY;
0739     return 0;
0740 }
0741 
0742 /**
0743  * wait_for_device_probe
0744  * Wait for device probing to be completed.
0745  */
0746 void wait_for_device_probe(void)
0747 {
0748     /* wait for the deferred probe workqueue to finish */
0749     flush_work(&deferred_probe_work);
0750 
0751     /* wait for the known devices to complete their probing */
0752     wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
0753     async_synchronize_full();
0754 }
0755 EXPORT_SYMBOL_GPL(wait_for_device_probe);
0756 
0757 static int __driver_probe_device(struct device_driver *drv, struct device *dev)
0758 {
0759     int ret = 0;
0760 
0761     if (dev->p->dead || !device_is_registered(dev))
0762         return -ENODEV;
0763     if (dev->driver)
0764         return -EBUSY;
0765 
0766     dev->can_match = true;
0767     pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
0768          drv->bus->name, __func__, dev_name(dev), drv->name);
0769 
0770     pm_runtime_get_suppliers(dev);
0771     if (dev->parent)
0772         pm_runtime_get_sync(dev->parent);
0773 
0774     pm_runtime_barrier(dev);
0775     if (initcall_debug)
0776         ret = really_probe_debug(dev, drv);
0777     else
0778         ret = really_probe(dev, drv);
0779     pm_request_idle(dev);
0780 
0781     if (dev->parent)
0782         pm_runtime_put(dev->parent);
0783 
0784     pm_runtime_put_suppliers(dev);
0785     return ret;
0786 }
0787 
0788 /**
0789  * driver_probe_device - attempt to bind device & driver together
0790  * @drv: driver to bind a device to
0791  * @dev: device to try to bind to the driver
0792  *
0793  * This function returns -ENODEV if the device is not registered, -EBUSY if it
0794  * already has a driver, 0 if the device is bound successfully and a positive
0795  * (inverted) error code for failures from the ->probe method.
0796  *
0797  * This function must be called with @dev lock held.  When called for a
0798  * USB interface, @dev->parent lock must be held as well.
0799  *
0800  * If the device has a parent, runtime-resume the parent before driver probing.
0801  */
0802 static int driver_probe_device(struct device_driver *drv, struct device *dev)
0803 {
0804     int trigger_count = atomic_read(&deferred_trigger_count);
0805     int ret;
0806 
0807     atomic_inc(&probe_count);
0808     ret = __driver_probe_device(drv, dev);
0809     if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
0810         driver_deferred_probe_add(dev);
0811 
0812         /*
0813          * Did a trigger occur while probing? Need to re-trigger if yes
0814          */
0815         if (trigger_count != atomic_read(&deferred_trigger_count) &&
0816             !defer_all_probes)
0817             driver_deferred_probe_trigger();
0818     }
0819     atomic_dec(&probe_count);
0820     wake_up_all(&probe_waitqueue);
0821     return ret;
0822 }
0823 
0824 static inline bool cmdline_requested_async_probing(const char *drv_name)
0825 {
0826     bool async_drv;
0827 
0828     async_drv = parse_option_str(async_probe_drv_names, drv_name);
0829 
0830     return (async_probe_default != async_drv);
0831 }
0832 
0833 /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
0834 static int __init save_async_options(char *buf)
0835 {
0836     if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
0837         pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
0838 
0839     strlcpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
0840     async_probe_default = parse_option_str(async_probe_drv_names, "*");
0841 
0842     return 1;
0843 }
0844 __setup("driver_async_probe=", save_async_options);
0845 
0846 bool driver_allows_async_probing(struct device_driver *drv)
0847 {
0848     switch (drv->probe_type) {
0849     case PROBE_PREFER_ASYNCHRONOUS:
0850         return true;
0851 
0852     case PROBE_FORCE_SYNCHRONOUS:
0853         return false;
0854 
0855     default:
0856         if (cmdline_requested_async_probing(drv->name))
0857             return true;
0858 
0859         if (module_requested_async_probing(drv->owner))
0860             return true;
0861 
0862         return false;
0863     }
0864 }
0865 
0866 struct device_attach_data {
0867     struct device *dev;
0868 
0869     /*
0870      * Indicates whether we are considering asynchronous probing or
0871      * not. Only initial binding after device or driver registration
0872      * (including deferral processing) may be done asynchronously, the
0873      * rest is always synchronous, as we expect it is being done by
0874      * request from userspace.
0875      */
0876     bool check_async;
0877 
0878     /*
0879      * Indicates if we are binding synchronous or asynchronous drivers.
0880      * When asynchronous probing is enabled we'll execute 2 passes
0881      * over drivers: first pass doing synchronous probing and second
0882      * doing asynchronous probing (if synchronous did not succeed -
0883      * most likely because there was no driver requiring synchronous
0884      * probing - and we found asynchronous driver during first pass).
0885      * The 2 passes are done because we can't shoot asynchronous
0886      * probe for given device and driver from bus_for_each_drv() since
0887      * driver pointer is not guaranteed to stay valid once
0888      * bus_for_each_drv() iterates to the next driver on the bus.
0889      */
0890     bool want_async;
0891 
0892     /*
0893      * We'll set have_async to 'true' if, while scanning for matching
0894      * driver, we'll encounter one that requests asynchronous probing.
0895      */
0896     bool have_async;
0897 };
0898 
0899 static int __device_attach_driver(struct device_driver *drv, void *_data)
0900 {
0901     struct device_attach_data *data = _data;
0902     struct device *dev = data->dev;
0903     bool async_allowed;
0904     int ret;
0905 
0906     ret = driver_match_device(drv, dev);
0907     if (ret == 0) {
0908         /* no match */
0909         return 0;
0910     } else if (ret == -EPROBE_DEFER) {
0911         dev_dbg(dev, "Device match requests probe deferral\n");
0912         dev->can_match = true;
0913         driver_deferred_probe_add(dev);
0914         /*
0915          * Device can't match with a driver right now, so don't attempt
0916          * to match or bind with other drivers on the bus.
0917          */
0918         return ret;
0919     } else if (ret < 0) {
0920         dev_dbg(dev, "Bus failed to match device: %d\n", ret);
0921         return ret;
0922     } /* ret > 0 means positive match */
0923 
0924     async_allowed = driver_allows_async_probing(drv);
0925 
0926     if (async_allowed)
0927         data->have_async = true;
0928 
0929     if (data->check_async && async_allowed != data->want_async)
0930         return 0;
0931 
0932     /*
0933      * Ignore errors returned by ->probe so that the next driver can try
0934      * its luck.
0935      */
0936     ret = driver_probe_device(drv, dev);
0937     if (ret < 0)
0938         return ret;
0939     return ret == 0;
0940 }
0941 
0942 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
0943 {
0944     struct device *dev = _dev;
0945     struct device_attach_data data = {
0946         .dev        = dev,
0947         .check_async    = true,
0948         .want_async = true,
0949     };
0950 
0951     device_lock(dev);
0952 
0953     /*
0954      * Check if device has already been removed or claimed. This may
0955      * happen with driver loading, device discovery/registration,
0956      * and deferred probe processing happens all at once with
0957      * multiple threads.
0958      */
0959     if (dev->p->dead || dev->driver)
0960         goto out_unlock;
0961 
0962     if (dev->parent)
0963         pm_runtime_get_sync(dev->parent);
0964 
0965     bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
0966     dev_dbg(dev, "async probe completed\n");
0967 
0968     pm_request_idle(dev);
0969 
0970     if (dev->parent)
0971         pm_runtime_put(dev->parent);
0972 out_unlock:
0973     device_unlock(dev);
0974 
0975     put_device(dev);
0976 }
0977 
0978 static int __device_attach(struct device *dev, bool allow_async)
0979 {
0980     int ret = 0;
0981     bool async = false;
0982 
0983     device_lock(dev);
0984     if (dev->p->dead) {
0985         goto out_unlock;
0986     } else if (dev->driver) {
0987         if (device_is_bound(dev)) {
0988             ret = 1;
0989             goto out_unlock;
0990         }
0991         ret = device_bind_driver(dev);
0992         if (ret == 0)
0993             ret = 1;
0994         else {
0995             dev->driver = NULL;
0996             ret = 0;
0997         }
0998     } else {
0999         struct device_attach_data data = {
1000             .dev = dev,
1001             .check_async = allow_async,
1002             .want_async = false,
1003         };
1004 
1005         if (dev->parent)
1006             pm_runtime_get_sync(dev->parent);
1007 
1008         ret = bus_for_each_drv(dev->bus, NULL, &data,
1009                     __device_attach_driver);
1010         if (!ret && allow_async && data.have_async) {
1011             /*
1012              * If we could not find appropriate driver
1013              * synchronously and we are allowed to do
1014              * async probes and there are drivers that
1015              * want to probe asynchronously, we'll
1016              * try them.
1017              */
1018             dev_dbg(dev, "scheduling asynchronous probe\n");
1019             get_device(dev);
1020             async = true;
1021         } else {
1022             pm_request_idle(dev);
1023         }
1024 
1025         if (dev->parent)
1026             pm_runtime_put(dev->parent);
1027     }
1028 out_unlock:
1029     device_unlock(dev);
1030     if (async)
1031         async_schedule_dev(__device_attach_async_helper, dev);
1032     return ret;
1033 }
1034 
1035 /**
1036  * device_attach - try to attach device to a driver.
1037  * @dev: device.
1038  *
1039  * Walk the list of drivers that the bus has and call
1040  * driver_probe_device() for each pair. If a compatible
1041  * pair is found, break out and return.
1042  *
1043  * Returns 1 if the device was bound to a driver;
1044  * 0 if no matching driver was found;
1045  * -ENODEV if the device is not registered.
1046  *
1047  * When called for a USB interface, @dev->parent lock must be held.
1048  */
1049 int device_attach(struct device *dev)
1050 {
1051     return __device_attach(dev, false);
1052 }
1053 EXPORT_SYMBOL_GPL(device_attach);
1054 
1055 void device_initial_probe(struct device *dev)
1056 {
1057     __device_attach(dev, true);
1058 }
1059 
1060 /*
1061  * __device_driver_lock - acquire locks needed to manipulate dev->drv
1062  * @dev: Device we will update driver info for
1063  * @parent: Parent device. Needed if the bus requires parent lock
1064  *
1065  * This function will take the required locks for manipulating dev->drv.
1066  * Normally this will just be the @dev lock, but when called for a USB
1067  * interface, @parent lock will be held as well.
1068  */
1069 static void __device_driver_lock(struct device *dev, struct device *parent)
1070 {
1071     if (parent && dev->bus->need_parent_lock)
1072         device_lock(parent);
1073     device_lock(dev);
1074 }
1075 
1076 /*
1077  * __device_driver_unlock - release locks needed to manipulate dev->drv
1078  * @dev: Device we will update driver info for
1079  * @parent: Parent device. Needed if the bus requires parent lock
1080  *
1081  * This function will release the required locks for manipulating dev->drv.
1082  * Normally this will just be the @dev lock, but when called for a
1083  * USB interface, @parent lock will be released as well.
1084  */
1085 static void __device_driver_unlock(struct device *dev, struct device *parent)
1086 {
1087     device_unlock(dev);
1088     if (parent && dev->bus->need_parent_lock)
1089         device_unlock(parent);
1090 }
1091 
1092 /**
1093  * device_driver_attach - attach a specific driver to a specific device
1094  * @drv: Driver to attach
1095  * @dev: Device to attach it to
1096  *
1097  * Manually attach driver to a device. Will acquire both @dev lock and
1098  * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
1099  */
1100 int device_driver_attach(struct device_driver *drv, struct device *dev)
1101 {
1102     int ret;
1103 
1104     __device_driver_lock(dev, dev->parent);
1105     ret = __driver_probe_device(drv, dev);
1106     __device_driver_unlock(dev, dev->parent);
1107 
1108     /* also return probe errors as normal negative errnos */
1109     if (ret > 0)
1110         ret = -ret;
1111     if (ret == -EPROBE_DEFER)
1112         return -EAGAIN;
1113     return ret;
1114 }
1115 EXPORT_SYMBOL_GPL(device_driver_attach);
1116 
1117 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
1118 {
1119     struct device *dev = _dev;
1120     struct device_driver *drv;
1121     int ret;
1122 
1123     __device_driver_lock(dev, dev->parent);
1124     drv = dev->p->async_driver;
1125     dev->p->async_driver = NULL;
1126     ret = driver_probe_device(drv, dev);
1127     __device_driver_unlock(dev, dev->parent);
1128 
1129     dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1130 
1131     put_device(dev);
1132 }
1133 
1134 static int __driver_attach(struct device *dev, void *data)
1135 {
1136     struct device_driver *drv = data;
1137     bool async = false;
1138     int ret;
1139 
1140     /*
1141      * Lock device and try to bind to it. We drop the error
1142      * here and always return 0, because we need to keep trying
1143      * to bind to devices and some drivers will return an error
1144      * simply if it didn't support the device.
1145      *
1146      * driver_probe_device() will spit a warning if there
1147      * is an error.
1148      */
1149 
1150     ret = driver_match_device(drv, dev);
1151     if (ret == 0) {
1152         /* no match */
1153         return 0;
1154     } else if (ret == -EPROBE_DEFER) {
1155         dev_dbg(dev, "Device match requests probe deferral\n");
1156         dev->can_match = true;
1157         driver_deferred_probe_add(dev);
1158         /*
1159          * Driver could not match with device, but may match with
1160          * another device on the bus.
1161          */
1162         return 0;
1163     } else if (ret < 0) {
1164         dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1165         return ret;
1166     } /* ret > 0 means positive match */
1167 
1168     if (driver_allows_async_probing(drv)) {
1169         /*
1170          * Instead of probing the device synchronously we will
1171          * probe it asynchronously to allow for more parallelism.
1172          *
1173          * We only take the device lock here in order to guarantee
1174          * that the dev->driver and async_driver fields are protected
1175          */
1176         dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1177         device_lock(dev);
1178         if (!dev->driver && !dev->p->async_driver) {
1179             get_device(dev);
1180             dev->p->async_driver = drv;
1181             async = true;
1182         }
1183         device_unlock(dev);
1184         if (async)
1185             async_schedule_dev(__driver_attach_async_helper, dev);
1186         return 0;
1187     }
1188 
1189     __device_driver_lock(dev, dev->parent);
1190     driver_probe_device(drv, dev);
1191     __device_driver_unlock(dev, dev->parent);
1192 
1193     return 0;
1194 }
1195 
1196 /**
1197  * driver_attach - try to bind driver to devices.
1198  * @drv: driver.
1199  *
1200  * Walk the list of devices that the bus has on it and try to
1201  * match the driver with each one.  If driver_probe_device()
1202  * returns 0 and the @dev->driver is set, we've found a
1203  * compatible pair.
1204  */
1205 int driver_attach(struct device_driver *drv)
1206 {
1207     return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
1208 }
1209 EXPORT_SYMBOL_GPL(driver_attach);
1210 
1211 /*
1212  * __device_release_driver() must be called with @dev lock held.
1213  * When called for a USB interface, @dev->parent lock must be held as well.
1214  */
1215 static void __device_release_driver(struct device *dev, struct device *parent)
1216 {
1217     struct device_driver *drv;
1218 
1219     drv = dev->driver;
1220     if (drv) {
1221         pm_runtime_get_sync(dev);
1222 
1223         while (device_links_busy(dev)) {
1224             __device_driver_unlock(dev, parent);
1225 
1226             device_links_unbind_consumers(dev);
1227 
1228             __device_driver_lock(dev, parent);
1229             /*
1230              * A concurrent invocation of the same function might
1231              * have released the driver successfully while this one
1232              * was waiting, so check for that.
1233              */
1234             if (dev->driver != drv) {
1235                 pm_runtime_put(dev);
1236                 return;
1237             }
1238         }
1239 
1240         driver_sysfs_remove(dev);
1241 
1242         if (dev->bus)
1243             blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1244                              BUS_NOTIFY_UNBIND_DRIVER,
1245                              dev);
1246 
1247         pm_runtime_put_sync(dev);
1248 
1249         device_remove(dev);
1250 
1251         if (dev->bus && dev->bus->dma_cleanup)
1252             dev->bus->dma_cleanup(dev);
1253 
1254         device_links_driver_cleanup(dev);
1255         device_unbind_cleanup(dev);
1256 
1257         klist_remove(&dev->p->knode_driver);
1258         device_pm_check_callbacks(dev);
1259         if (dev->bus)
1260             blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1261                              BUS_NOTIFY_UNBOUND_DRIVER,
1262                              dev);
1263 
1264         kobject_uevent(&dev->kobj, KOBJ_UNBIND);
1265     }
1266 }
1267 
1268 void device_release_driver_internal(struct device *dev,
1269                     struct device_driver *drv,
1270                     struct device *parent)
1271 {
1272     __device_driver_lock(dev, parent);
1273 
1274     if (!drv || drv == dev->driver)
1275         __device_release_driver(dev, parent);
1276 
1277     __device_driver_unlock(dev, parent);
1278 }
1279 
1280 /**
1281  * device_release_driver - manually detach device from driver.
1282  * @dev: device.
1283  *
1284  * Manually detach device from driver.
1285  * When called for a USB interface, @dev->parent lock must be held.
1286  *
1287  * If this function is to be called with @dev->parent lock held, ensure that
1288  * the device's consumers are unbound in advance or that their locks can be
1289  * acquired under the @dev->parent lock.
1290  */
1291 void device_release_driver(struct device *dev)
1292 {
1293     /*
1294      * If anyone calls device_release_driver() recursively from
1295      * within their ->remove callback for the same device, they
1296      * will deadlock right here.
1297      */
1298     device_release_driver_internal(dev, NULL, NULL);
1299 }
1300 EXPORT_SYMBOL_GPL(device_release_driver);
1301 
1302 /**
1303  * device_driver_detach - detach driver from a specific device
1304  * @dev: device to detach driver from
1305  *
1306  * Detach driver from device. Will acquire both @dev lock and @dev->parent
1307  * lock if needed.
1308  */
1309 void device_driver_detach(struct device *dev)
1310 {
1311     device_release_driver_internal(dev, NULL, dev->parent);
1312 }
1313 
1314 /**
1315  * driver_detach - detach driver from all devices it controls.
1316  * @drv: driver.
1317  */
1318 void driver_detach(struct device_driver *drv)
1319 {
1320     struct device_private *dev_prv;
1321     struct device *dev;
1322 
1323     if (driver_allows_async_probing(drv))
1324         async_synchronize_full();
1325 
1326     for (;;) {
1327         spin_lock(&drv->p->klist_devices.k_lock);
1328         if (list_empty(&drv->p->klist_devices.k_list)) {
1329             spin_unlock(&drv->p->klist_devices.k_lock);
1330             break;
1331         }
1332         dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
1333                      struct device_private,
1334                      knode_driver.n_node);
1335         dev = dev_prv->device;
1336         get_device(dev);
1337         spin_unlock(&drv->p->klist_devices.k_lock);
1338         device_release_driver_internal(dev, drv, dev->parent);
1339         put_device(dev);
1340     }
1341 }