xref: /linux/drivers/base/dd.c (revision fda8355f13ea3c0f9499acdeff3024995b474948)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * drivers/base/dd.c - The core device/driver interactions.
4  *
5  * This file contains the (sometimes tricky) code that controls the
6  * interactions between devices and drivers, which primarily includes
7  * driver binding and unbinding.
8  *
9  * All of this code used to exist in drivers/base/bus.c, but was
10  * relocated to here in the name of compartmentalization (since it wasn't
11  * strictly code just for the 'struct bus_type'.
12  *
13  * Copyright (c) 2002-5 Patrick Mochel
14  * Copyright (c) 2002-3 Open Source Development Labs
15  * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
16  * Copyright (c) 2007-2009 Novell Inc.
17  */
18 
19 #include <linux/debugfs.h>
20 #include <linux/device.h>
21 #include <linux/delay.h>
22 #include <linux/dma-map-ops.h>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/kthread.h>
26 #include <linux/wait.h>
27 #include <linux/async.h>
28 #include <linux/pm_domain.h>
29 #include <linux/pm_runtime.h>
30 #include <linux/pinctrl/devinfo.h>
31 #include <linux/slab.h>
32 
33 #include "base.h"
34 #include "power/power.h"
35 
36 /*
37  * Deferred Probe infrastructure.
38  *
39  * Sometimes driver probe order matters, but the kernel doesn't always have
40  * dependency information which means some drivers will get probed before a
41  * resource it depends on is available.  For example, an SDHCI driver may
42  * first need a GPIO line from an i2c GPIO controller before it can be
43  * initialized.  If a required resource is not available yet, a driver can
44  * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
45  *
46  * Deferred probe maintains two lists of devices, a pending list and an active
47  * list.  A driver returning -EPROBE_DEFER causes the device to be added to the
48  * pending list.  A successful driver probe will trigger moving all devices
49  * from the pending to the active list so that the workqueue will eventually
50  * retry them.
51  *
52  * The deferred_probe_mutex must be held any time the deferred_probe_*_list
53  * of the (struct device*)->p->deferred_probe pointers are manipulated
54  */
55 static DEFINE_MUTEX(deferred_probe_mutex);
56 static LIST_HEAD(deferred_probe_pending_list);
57 static LIST_HEAD(deferred_probe_active_list);
58 static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
59 static bool initcalls_done;
60 
61 /* Save the async probe drivers' name from kernel cmdline */
62 #define ASYNC_DRV_NAMES_MAX_LEN	256
63 static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
64 static bool async_probe_default;
65 
66 /*
67  * In some cases, like suspend to RAM or hibernation, It might be reasonable
68  * to prohibit probing of devices as it could be unsafe.
69  * Once defer_all_probes is true all drivers probes will be forcibly deferred.
70  */
71 static bool defer_all_probes;
72 
73 static void __device_set_deferred_probe_reason(const struct device *dev, char *reason)
74 {
75 	kfree(dev->p->deferred_probe_reason);
76 	dev->p->deferred_probe_reason = reason;
77 }
78 
79 /*
80  * deferred_probe_work_func() - Retry probing devices in the active list.
81  */
82 static void deferred_probe_work_func(struct work_struct *work)
83 {
84 	struct device *dev;
85 	struct device_private *private;
86 	/*
87 	 * This block processes every device in the deferred 'active' list.
88 	 * Each device is removed from the active list and passed to
89 	 * bus_probe_device() to re-attempt the probe.  The loop continues
90 	 * until every device in the active list is removed and retried.
91 	 *
92 	 * Note: Once the device is removed from the list and the mutex is
93 	 * released, it is possible for the device get freed by another thread
94 	 * and cause a illegal pointer dereference.  This code uses
95 	 * get/put_device() to ensure the device structure cannot disappear
96 	 * from under our feet.
97 	 */
98 	mutex_lock(&deferred_probe_mutex);
99 	while (!list_empty(&deferred_probe_active_list)) {
100 		private = list_first_entry(&deferred_probe_active_list,
101 					typeof(*dev->p), deferred_probe);
102 		dev = private->device;
103 		list_del_init(&private->deferred_probe);
104 
105 		get_device(dev);
106 
107 		__device_set_deferred_probe_reason(dev, NULL);
108 
109 		/*
110 		 * Drop the mutex while probing each device; the probe path may
111 		 * manipulate the deferred list
112 		 */
113 		mutex_unlock(&deferred_probe_mutex);
114 
115 		/*
116 		 * Force the device to the end of the dpm_list since
117 		 * the PM code assumes that the order we add things to
118 		 * the list is a good order for suspend but deferred
119 		 * probe makes that very unsafe.
120 		 */
121 		device_pm_move_to_tail(dev);
122 
123 		dev_dbg(dev, "Retrying from deferred list\n");
124 		bus_probe_device(dev);
125 		mutex_lock(&deferred_probe_mutex);
126 
127 		put_device(dev);
128 	}
129 	mutex_unlock(&deferred_probe_mutex);
130 }
131 static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
132 
133 void driver_deferred_probe_add(struct device *dev)
134 {
135 	if (!dev_can_match(dev))
136 		return;
137 
138 	mutex_lock(&deferred_probe_mutex);
139 	if (list_empty(&dev->p->deferred_probe)) {
140 		dev_dbg(dev, "Added to deferred list\n");
141 		list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
142 	}
143 	mutex_unlock(&deferred_probe_mutex);
144 }
145 
146 void driver_deferred_probe_del(struct device *dev)
147 {
148 	mutex_lock(&deferred_probe_mutex);
149 	if (!list_empty(&dev->p->deferred_probe)) {
150 		dev_dbg(dev, "Removed from deferred list\n");
151 		list_del_init(&dev->p->deferred_probe);
152 		__device_set_deferred_probe_reason(dev, NULL);
153 	}
154 	mutex_unlock(&deferred_probe_mutex);
155 }
156 
157 static bool driver_deferred_probe_enable;
158 /**
159  * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
160  *
161  * This functions moves all devices from the pending list to the active
162  * list and schedules the deferred probe workqueue to process them.  It
163  * should be called anytime a driver is successfully bound to a device.
164  *
165  * Note, there is a race condition in multi-threaded probe. In the case where
166  * more than one device is probing at the same time, it is possible for one
167  * probe to complete successfully while another is about to defer. If the second
168  * depends on the first, then it will get put on the pending list after the
169  * trigger event has already occurred and will be stuck there.
170  *
171  * The atomic 'deferred_trigger_count' is used to determine if a successful
172  * trigger has occurred in the midst of probing a driver. If the trigger count
173  * changes in the midst of a probe, then deferred processing should be triggered
174  * again.
175  */
176 void driver_deferred_probe_trigger(void)
177 {
178 	if (!driver_deferred_probe_enable)
179 		return;
180 
181 	/*
182 	 * A successful probe means that all the devices in the pending list
183 	 * should be triggered to be reprobed.  Move all the deferred devices
184 	 * into the active list so they can be retried by the workqueue
185 	 */
186 	mutex_lock(&deferred_probe_mutex);
187 	atomic_inc(&deferred_trigger_count);
188 	list_splice_tail_init(&deferred_probe_pending_list,
189 			      &deferred_probe_active_list);
190 	mutex_unlock(&deferred_probe_mutex);
191 
192 	/*
193 	 * Kick the re-probe thread.  It may already be scheduled, but it is
194 	 * safe to kick it again.
195 	 */
196 	queue_work(system_dfl_wq, &deferred_probe_work);
197 }
198 
199 /**
200  * device_block_probing() - Block/defer device's probes
201  *
202  *	It will disable probing of devices and defer their probes instead.
203  */
204 void device_block_probing(void)
205 {
206 	defer_all_probes = true;
207 	/* sync with probes to avoid races. */
208 	wait_for_device_probe();
209 }
210 
211 /**
212  * device_unblock_probing() - Unblock/enable device's probes
213  *
214  *	It will restore normal behavior and trigger re-probing of deferred
215  * devices.
216  */
217 void device_unblock_probing(void)
218 {
219 	defer_all_probes = false;
220 	driver_deferred_probe_trigger();
221 }
222 
223 /**
224  * device_set_deferred_probe_reason() - Set defer probe reason message for device
225  * @dev: the pointer to the struct device
226  * @vaf: the pointer to va_format structure with message
227  */
228 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
229 {
230 	const char *drv = dev_driver_string(dev);
231 	char *reason;
232 
233 	mutex_lock(&deferred_probe_mutex);
234 
235 	reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
236 	__device_set_deferred_probe_reason(dev, reason);
237 
238 	mutex_unlock(&deferred_probe_mutex);
239 }
240 
241 /*
242  * deferred_devs_show() - Show the devices in the deferred probe pending list.
243  */
244 static int deferred_devs_show(struct seq_file *s, void *data)
245 {
246 	struct device_private *curr;
247 
248 	mutex_lock(&deferred_probe_mutex);
249 
250 	list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
251 		seq_printf(s, "%s\t%s", dev_name(curr->device),
252 			   curr->deferred_probe_reason ?: "\n");
253 
254 	mutex_unlock(&deferred_probe_mutex);
255 
256 	return 0;
257 }
258 DEFINE_SHOW_ATTRIBUTE(deferred_devs);
259 
260 static int driver_deferred_probe_timeout = CONFIG_DRIVER_DEFERRED_PROBE_TIMEOUT;
261 
262 static int __init deferred_probe_timeout_setup(char *str)
263 {
264 	int timeout;
265 
266 	if (!kstrtoint(str, 10, &timeout))
267 		driver_deferred_probe_timeout = timeout;
268 	return 1;
269 }
270 __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
271 
272 /**
273  * driver_deferred_probe_check_state() - Check deferred probe state
274  * @dev: device to check
275  *
276  * Return:
277  * * -ENODEV if initcalls have completed and modules are disabled.
278  * * -ETIMEDOUT if the deferred probe timeout was set and has expired
279  *   and modules are enabled.
280  * * -EPROBE_DEFER in other cases.
281  *
282  * Drivers or subsystems can opt-in to calling this function instead of directly
283  * returning -EPROBE_DEFER.
284  */
285 int driver_deferred_probe_check_state(struct device *dev)
286 {
287 	if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
288 		dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
289 		return -ENODEV;
290 	}
291 
292 	if (!driver_deferred_probe_timeout && initcalls_done) {
293 		dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
294 		return -ETIMEDOUT;
295 	}
296 
297 	return -EPROBE_DEFER;
298 }
299 EXPORT_SYMBOL_GPL(driver_deferred_probe_check_state);
300 
301 static void deferred_probe_timeout_work_func(struct work_struct *work)
302 {
303 	struct device_private *p;
304 
305 	fw_devlink_drivers_done();
306 
307 	driver_deferred_probe_timeout = 0;
308 	driver_deferred_probe_trigger();
309 	flush_work(&deferred_probe_work);
310 
311 	mutex_lock(&deferred_probe_mutex);
312 	list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
313 		dev_warn(p->device, "deferred probe pending: %s", p->deferred_probe_reason ?: "(reason unknown)\n");
314 	mutex_unlock(&deferred_probe_mutex);
315 
316 	fw_devlink_probing_done();
317 }
318 static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
319 
320 void deferred_probe_extend_timeout(void)
321 {
322 	/*
323 	 * If the work hasn't been queued yet or if the work expired, don't
324 	 * start a new one.
325 	 */
326 	if (delayed_work_pending(&deferred_probe_timeout_work) &&
327 	    mod_delayed_work(system_percpu_wq, &deferred_probe_timeout_work,
328 			     secs_to_jiffies(driver_deferred_probe_timeout)))
329 		pr_debug("Extended deferred probe timeout by %d secs\n",
330 					driver_deferred_probe_timeout);
331 }
332 
333 /**
334  * deferred_probe_initcall() - Enable probing of deferred devices
335  *
336  * We don't want to get in the way when the bulk of drivers are getting probed.
337  * Instead, this initcall makes sure that deferred probing is delayed until
338  * late_initcall time.
339  */
340 static int deferred_probe_initcall(void)
341 {
342 	debugfs_create_file("devices_deferred", 0444, NULL, NULL,
343 			    &deferred_devs_fops);
344 
345 	driver_deferred_probe_enable = true;
346 	driver_deferred_probe_trigger();
347 	/* Sort as many dependencies as possible before exiting initcalls */
348 	flush_work(&deferred_probe_work);
349 	initcalls_done = true;
350 
351 	if (!IS_ENABLED(CONFIG_MODULES))
352 		fw_devlink_drivers_done();
353 
354 	/*
355 	 * Trigger deferred probe again, this time we won't defer anything
356 	 * that is optional
357 	 */
358 	driver_deferred_probe_trigger();
359 	flush_work(&deferred_probe_work);
360 
361 	if (driver_deferred_probe_timeout > 0) {
362 		schedule_delayed_work(&deferred_probe_timeout_work,
363 			driver_deferred_probe_timeout * HZ);
364 	}
365 
366 	if (!IS_ENABLED(CONFIG_MODULES))
367 		fw_devlink_probing_done();
368 
369 	return 0;
370 }
371 late_initcall(deferred_probe_initcall);
372 
373 static void __exit deferred_probe_exit(void)
374 {
375 	debugfs_lookup_and_remove("devices_deferred", NULL);
376 }
377 __exitcall(deferred_probe_exit);
378 
379 int __device_set_driver_override(struct device *dev, const char *s, size_t len)
380 {
381 	const char *new = NULL, *old;
382 
383 	if (!s)
384 		return -EINVAL;
385 
386 	/*
387 	 * The stored value will be used in sysfs show callback (sysfs_emit()),
388 	 * which has a length limit of PAGE_SIZE and adds a trailing newline.
389 	 * Thus we can store one character less to avoid truncation during sysfs
390 	 * show.
391 	 */
392 	if (len >= (PAGE_SIZE - 1))
393 		return -EINVAL;
394 
395 	/*
396 	 * Compute the real length of the string in case userspace sends us a
397 	 * bunch of \0 characters like python likes to do.
398 	 */
399 	len = strlen(s);
400 
401 	/* Handle trailing newline */
402 	if (len) {
403 		char *cp;
404 
405 		cp = strnchr(s, len, '\n');
406 		if (cp)
407 			len = cp - s;
408 	}
409 
410 	/*
411 	 * If empty string or "\n" passed, new remains NULL, clearing
412 	 * the driver_override.name.
413 	 */
414 	if (len) {
415 		new = kstrndup(s, len, GFP_KERNEL);
416 		if (!new)
417 			return -ENOMEM;
418 	}
419 
420 	scoped_guard(spinlock, &dev->driver_override.lock) {
421 		old = dev->driver_override.name;
422 		dev->driver_override.name = new;
423 	}
424 
425 	kfree(old);
426 
427 	return 0;
428 }
429 EXPORT_SYMBOL_GPL(__device_set_driver_override);
430 
431 /**
432  * device_is_bound() - Check if device is bound to a driver
433  * @dev: device to check
434  *
435  * Returns true if passed device has already finished probing successfully
436  * against a driver.
437  *
438  * This function must be called with the device lock held.
439  */
440 bool device_is_bound(struct device *dev)
441 {
442 	return dev->p && klist_node_attached(&dev->p->knode_driver);
443 }
444 EXPORT_SYMBOL_GPL(device_is_bound);
445 
446 static void driver_bound(struct device *dev)
447 {
448 	if (device_is_bound(dev)) {
449 		dev_warn(dev, "%s: device already bound\n", __func__);
450 		return;
451 	}
452 
453 	dev_dbg(dev, "driver: '%s': %s: bound to device\n", dev->driver->name,
454 		__func__);
455 
456 	klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
457 	device_links_driver_bound(dev);
458 
459 	device_pm_check_callbacks(dev);
460 
461 	/*
462 	 * Make sure the device is no longer in one of the deferred lists and
463 	 * kick off retrying all pending devices
464 	 */
465 	driver_deferred_probe_del(dev);
466 	driver_deferred_probe_trigger();
467 
468 	bus_notify(dev, BUS_NOTIFY_BOUND_DRIVER);
469 	kobject_uevent(&dev->kobj, KOBJ_BIND);
470 }
471 
472 static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
473 			    const char *buf, size_t count)
474 {
475 	device_lock(dev);
476 	dev->driver->coredump(dev);
477 	device_unlock(dev);
478 
479 	return count;
480 }
481 static DEVICE_ATTR_WO(coredump);
482 
483 static int driver_sysfs_add(struct device *dev)
484 {
485 	int ret;
486 
487 	bus_notify(dev, BUS_NOTIFY_BIND_DRIVER);
488 
489 	ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
490 				kobject_name(&dev->kobj));
491 	if (ret)
492 		goto fail;
493 
494 	ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
495 				"driver");
496 	if (ret)
497 		goto rm_dev;
498 
499 	if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump)
500 		return 0;
501 
502 	ret = device_create_file(dev, &dev_attr_coredump);
503 	if (!ret)
504 		return 0;
505 
506 	sysfs_remove_link(&dev->kobj, "driver");
507 
508 rm_dev:
509 	sysfs_remove_link(&dev->driver->p->kobj,
510 			  kobject_name(&dev->kobj));
511 
512 fail:
513 	return ret;
514 }
515 
516 static void driver_sysfs_remove(struct device *dev)
517 {
518 	struct device_driver *drv = dev->driver;
519 
520 	if (drv) {
521 		if (drv->coredump)
522 			device_remove_file(dev, &dev_attr_coredump);
523 		sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
524 		sysfs_remove_link(&dev->kobj, "driver");
525 	}
526 }
527 
528 /**
529  * device_bind_driver - bind a driver to one device.
530  * @dev: device.
531  *
532  * Allow manual attachment of a driver to a device.
533  * Caller must have already set @dev->driver.
534  *
535  * Note that this does not modify the bus reference count.
536  * Please verify that is accounted for before calling this.
537  * (It is ok to call with no other effort from a driver's probe() method.)
538  *
539  * This function must be called with the device lock held.
540  *
541  * Callers should prefer to use device_driver_attach() instead.
542  */
543 int device_bind_driver(struct device *dev)
544 {
545 	int ret;
546 
547 	ret = driver_sysfs_add(dev);
548 	if (!ret) {
549 		device_links_force_bind(dev);
550 		driver_bound(dev);
551 	}
552 	else
553 		bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
554 	return ret;
555 }
556 EXPORT_SYMBOL_GPL(device_bind_driver);
557 
558 static atomic_t probe_count = ATOMIC_INIT(0);
559 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
560 
561 static ssize_t state_synced_store(struct device *dev,
562 				  struct device_attribute *attr,
563 				  const char *buf, size_t count)
564 {
565 	int ret = 0;
566 
567 	if (strcmp("1", buf))
568 		return -EINVAL;
569 
570 	device_lock(dev);
571 	if (!dev_test_and_set_state_synced(dev))
572 		dev_sync_state(dev);
573 	else
574 		ret = -EINVAL;
575 	device_unlock(dev);
576 
577 	return ret ? ret : count;
578 }
579 
580 static ssize_t state_synced_show(struct device *dev,
581 				 struct device_attribute *attr, char *buf)
582 {
583 	bool val;
584 
585 	device_lock(dev);
586 	val = dev_state_synced(dev);
587 	device_unlock(dev);
588 
589 	return sysfs_emit(buf, "%u\n", val);
590 }
591 static DEVICE_ATTR_RW(state_synced);
592 
593 static void device_unbind_cleanup(struct device *dev)
594 {
595 	if (dev->driver->p_cb.post_unbind_rust)
596 		dev->driver->p_cb.post_unbind_rust(dev);
597 	devres_release_all(dev);
598 	arch_teardown_dma_ops(dev);
599 	kfree(dev->dma_range_map);
600 	dev->dma_range_map = NULL;
601 	device_set_driver(dev, NULL);
602 	dev_set_drvdata(dev, NULL);
603 	dev_pm_domain_detach(dev, dev->power.detach_power_off);
604 	if (dev->pm_domain && dev->pm_domain->dismiss)
605 		dev->pm_domain->dismiss(dev);
606 	pm_runtime_reinit(dev);
607 	dev_pm_set_driver_flags(dev, 0);
608 }
609 
610 static void device_remove(struct device *dev)
611 {
612 	device_remove_file(dev, &dev_attr_state_synced);
613 	device_remove_groups(dev, dev->driver->dev_groups);
614 
615 	if (dev->bus && dev->bus->remove)
616 		dev->bus->remove(dev);
617 	else if (dev->driver->remove)
618 		dev->driver->remove(dev);
619 }
620 
621 static int call_driver_probe(struct device *dev, const struct device_driver *drv)
622 {
623 	int ret = 0;
624 
625 	if (dev->bus->probe)
626 		ret = dev->bus->probe(dev);
627 	else if (drv->probe)
628 		ret = drv->probe(dev);
629 
630 	switch (ret) {
631 	case 0:
632 		break;
633 	case -EPROBE_DEFER:
634 		/* Driver requested deferred probing */
635 		dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
636 		break;
637 	case -ENODEV:
638 	case -ENXIO:
639 		dev_dbg(dev, "probe with driver %s rejects match %d\n",
640 			drv->name, ret);
641 		break;
642 	default:
643 		/* driver matched but the probe failed */
644 		dev_err(dev, "probe with driver %s failed with error %d\n",
645 			drv->name, ret);
646 		break;
647 	}
648 
649 	return ret;
650 }
651 
652 static int really_probe(struct device *dev, const struct device_driver *drv)
653 {
654 	bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
655 			   !drv->suppress_bind_attrs;
656 	int ret, link_ret;
657 
658 	if (defer_all_probes) {
659 		/*
660 		 * Value of defer_all_probes can be set only by
661 		 * device_block_probing() which, in turn, will call
662 		 * wait_for_device_probe() right after that to avoid any races.
663 		 */
664 		dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
665 		return -EPROBE_DEFER;
666 	}
667 
668 	link_ret = device_links_check_suppliers(dev);
669 	if (link_ret == -EPROBE_DEFER)
670 		return link_ret;
671 
672 	dev_dbg(dev, "bus: '%s': %s: probing driver %s with device\n",
673 		drv->bus->name, __func__, drv->name);
674 	if (!list_empty(&dev->devres_head)) {
675 		dev_crit(dev, "Resources present before probing\n");
676 		ret = -EBUSY;
677 		goto done;
678 	}
679 
680 re_probe:
681 	device_set_driver(dev, drv);
682 
683 	/* If using pinctrl, bind pins now before probing */
684 	ret = pinctrl_bind_pins(dev);
685 	if (ret)
686 		goto pinctrl_bind_failed;
687 
688 	if (dev->bus->dma_configure) {
689 		ret = dev->bus->dma_configure(dev);
690 		if (ret)
691 			goto pinctrl_bind_failed;
692 	}
693 
694 	ret = driver_sysfs_add(dev);
695 	if (ret) {
696 		dev_err(dev, "%s: driver_sysfs_add failed\n", __func__);
697 		goto sysfs_failed;
698 	}
699 
700 	if (dev->pm_domain && dev->pm_domain->activate) {
701 		ret = dev->pm_domain->activate(dev);
702 		if (ret)
703 			goto probe_failed;
704 	}
705 
706 	ret = call_driver_probe(dev, drv);
707 	if (ret) {
708 		/*
709 		 * If fw_devlink_best_effort is active (denoted by -EAGAIN), the
710 		 * device might actually probe properly once some of its missing
711 		 * suppliers have probed. So, treat this as if the driver
712 		 * returned -EPROBE_DEFER.
713 		 */
714 		if (link_ret == -EAGAIN)
715 			ret = -EPROBE_DEFER;
716 
717 		/*
718 		 * Return probe errors as positive values so that the callers
719 		 * can distinguish them from other errors.
720 		 */
721 		ret = -ret;
722 		goto probe_failed;
723 	}
724 
725 	ret = device_add_groups(dev, drv->dev_groups);
726 	if (ret) {
727 		dev_err(dev, "device_add_groups() failed\n");
728 		goto dev_groups_failed;
729 	}
730 
731 	if (dev_has_sync_state(dev)) {
732 		ret = device_create_file(dev, &dev_attr_state_synced);
733 		if (ret) {
734 			dev_err(dev, "state_synced sysfs add failed\n");
735 			goto dev_sysfs_state_synced_failed;
736 		}
737 	}
738 
739 	if (test_remove) {
740 		test_remove = false;
741 
742 		device_remove(dev);
743 		driver_sysfs_remove(dev);
744 		if (dev->bus && dev->bus->dma_cleanup)
745 			dev->bus->dma_cleanup(dev);
746 		device_unbind_cleanup(dev);
747 
748 		goto re_probe;
749 	}
750 
751 	pinctrl_init_done(dev);
752 
753 	if (dev->pm_domain && dev->pm_domain->sync)
754 		dev->pm_domain->sync(dev);
755 
756 	driver_bound(dev);
757 	dev_dbg(dev, "bus: '%s': %s: bound device to driver %s\n",
758 		drv->bus->name, __func__, drv->name);
759 	goto done;
760 
761 dev_sysfs_state_synced_failed:
762 dev_groups_failed:
763 	device_remove(dev);
764 probe_failed:
765 	driver_sysfs_remove(dev);
766 sysfs_failed:
767 	bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
768 	if (dev->bus && dev->bus->dma_cleanup)
769 		dev->bus->dma_cleanup(dev);
770 pinctrl_bind_failed:
771 	device_links_no_driver(dev);
772 	device_unbind_cleanup(dev);
773 done:
774 	return ret;
775 }
776 
777 /*
778  * For initcall_debug, show the driver probe time.
779  */
780 static int really_probe_debug(struct device *dev, const struct device_driver *drv)
781 {
782 	ktime_t calltime, rettime;
783 	int ret;
784 
785 	calltime = ktime_get();
786 	ret = really_probe(dev, drv);
787 	rettime = ktime_get();
788 	/*
789 	 * Don't change this to pr_debug() because that requires
790 	 * CONFIG_DYNAMIC_DEBUG and we want a simple 'initcall_debug' on the
791 	 * kernel commandline to print this all the time at the debug level.
792 	 */
793 	printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
794 		 dev_name(dev), ret, ktime_us_delta(rettime, calltime));
795 	return ret;
796 }
797 
798 /**
799  * driver_probe_done
800  * Determine if the probe sequence is finished or not.
801  *
802  * Should somehow figure out how to use a semaphore, not an atomic variable...
803  */
804 bool __init driver_probe_done(void)
805 {
806 	int local_probe_count = atomic_read(&probe_count);
807 
808 	pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
809 	return !local_probe_count;
810 }
811 
812 /**
813  * wait_for_device_probe
814  * Wait for device probing to be completed.
815  */
816 void wait_for_device_probe(void)
817 {
818 	/* wait for the deferred probe workqueue to finish */
819 	flush_work(&deferred_probe_work);
820 
821 	/* wait for the known devices to complete their probing */
822 	wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
823 	async_synchronize_full();
824 }
825 EXPORT_SYMBOL_GPL(wait_for_device_probe);
826 
827 static int __driver_probe_device(const struct device_driver *drv, struct device *dev)
828 {
829 	int ret = 0;
830 
831 	if (dev->p->dead || !device_is_registered(dev))
832 		return -ENODEV;
833 	if (dev->driver)
834 		return -EBUSY;
835 
836 	/*
837 	 * In device_add(), the "struct device" gets linked into the subsystem's
838 	 * list of devices and broadcast to userspace (via uevent) before we're
839 	 * quite ready to probe. Those open pathways to driver probe before
840 	 * we've finished enough of device_add() to reliably support probe.
841 	 * Detect this and tell other pathways to try again later. device_add()
842 	 * itself will also try to probe immediately after setting
843 	 * "ready_to_probe".
844 	 */
845 	if (!dev_ready_to_probe(dev))
846 		return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n");
847 
848 	/*
849 	 * Call dev_set_can_match() after calling dev_ready_to_probe(), so
850 	 * driver_deferred_probe_add() won't actually add the device to the
851 	 * deferred probe list when dev_ready_to_probe() returns false.
852 	 *
853 	 * When dev_ready_to_probe() returns false, it means that device_add()
854 	 * will do another probe() attempt for us.
855 	 */
856 	dev_set_can_match(dev);
857 	dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
858 		drv->bus->name, __func__, drv->name);
859 
860 	pm_runtime_get_suppliers(dev);
861 	if (dev->parent)
862 		pm_runtime_get_sync(dev->parent);
863 
864 	pm_runtime_barrier(dev);
865 	if (initcall_debug)
866 		ret = really_probe_debug(dev, drv);
867 	else
868 		ret = really_probe(dev, drv);
869 	pm_request_idle(dev);
870 
871 	if (dev->parent)
872 		pm_runtime_put(dev->parent);
873 
874 	pm_runtime_put_suppliers(dev);
875 	return ret;
876 }
877 
878 /**
879  * driver_probe_device - attempt to bind device & driver together
880  * @drv: driver to bind a device to
881  * @dev: device to try to bind to the driver
882  *
883  * This function returns -ENODEV if the device is not registered, -EBUSY if it
884  * already has a driver, 0 if the device is bound successfully and a positive
885  * (inverted) error code for failures from the ->probe method.
886  *
887  * This function must be called with @dev lock held.  When called for a
888  * USB interface, @dev->parent lock must be held as well.
889  *
890  * If the device has a parent, runtime-resume the parent before driver probing.
891  */
892 static int driver_probe_device(const struct device_driver *drv, struct device *dev)
893 {
894 	int trigger_count = atomic_read(&deferred_trigger_count);
895 	int ret;
896 
897 	atomic_inc(&probe_count);
898 	ret = __driver_probe_device(drv, dev);
899 	if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
900 		driver_deferred_probe_add(dev);
901 
902 		/*
903 		 * Did a trigger occur while probing? Need to re-trigger if yes
904 		 */
905 		if (trigger_count != atomic_read(&deferred_trigger_count) &&
906 		    !defer_all_probes)
907 			driver_deferred_probe_trigger();
908 	}
909 	atomic_dec(&probe_count);
910 	wake_up_all(&probe_waitqueue);
911 	return ret;
912 }
913 
914 static inline bool cmdline_requested_async_probing(const char *drv_name)
915 {
916 	bool async_drv;
917 
918 	async_drv = parse_option_str(async_probe_drv_names, drv_name);
919 
920 	return (async_probe_default != async_drv);
921 }
922 
923 /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
924 static int __init save_async_options(char *buf)
925 {
926 	if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
927 		pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
928 
929 	strscpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
930 	async_probe_default = parse_option_str(async_probe_drv_names, "*");
931 
932 	return 1;
933 }
934 __setup("driver_async_probe=", save_async_options);
935 
936 static bool driver_allows_async_probing(const struct device_driver *drv)
937 {
938 	switch (drv->probe_type) {
939 	case PROBE_PREFER_ASYNCHRONOUS:
940 		return true;
941 
942 	case PROBE_FORCE_SYNCHRONOUS:
943 		return false;
944 
945 	default:
946 		if (cmdline_requested_async_probing(drv->name))
947 			return true;
948 
949 		if (module_requested_async_probing(drv->owner))
950 			return true;
951 
952 		return false;
953 	}
954 }
955 
956 struct device_attach_data {
957 	struct device *dev;
958 
959 	/*
960 	 * Indicates whether we are considering asynchronous probing or
961 	 * not. Only initial binding after device or driver registration
962 	 * (including deferral processing) may be done asynchronously, the
963 	 * rest is always synchronous, as we expect it is being done by
964 	 * request from userspace.
965 	 */
966 	bool check_async;
967 
968 	/*
969 	 * Indicates if we are binding synchronous or asynchronous drivers.
970 	 * When asynchronous probing is enabled we'll execute 2 passes
971 	 * over drivers: first pass doing synchronous probing and second
972 	 * doing asynchronous probing (if synchronous did not succeed -
973 	 * most likely because there was no driver requiring synchronous
974 	 * probing - and we found asynchronous driver during first pass).
975 	 * The 2 passes are done because we can't shoot asynchronous
976 	 * probe for given device and driver from bus_for_each_drv() since
977 	 * driver pointer is not guaranteed to stay valid once
978 	 * bus_for_each_drv() iterates to the next driver on the bus.
979 	 */
980 	bool want_async;
981 
982 	/*
983 	 * We'll set have_async to 'true' if, while scanning for matching
984 	 * driver, we'll encounter one that requests asynchronous probing.
985 	 */
986 	bool have_async;
987 };
988 
989 static int __device_attach_driver(struct device_driver *drv, void *_data)
990 {
991 	struct device_attach_data *data = _data;
992 	struct device *dev = data->dev;
993 	bool async_allowed;
994 	int ret;
995 
996 	ret = driver_match_device(drv, dev);
997 	if (ret == 0) {
998 		/* no match */
999 		return 0;
1000 	} else if (ret == -EPROBE_DEFER) {
1001 		dev_dbg(dev, "Device match requests probe deferral\n");
1002 		dev_set_can_match(dev);
1003 		driver_deferred_probe_add(dev);
1004 		/*
1005 		 * Device can't match with a driver right now, so don't attempt
1006 		 * to match or bind with other drivers on the bus.
1007 		 */
1008 		return ret;
1009 	} else if (ret < 0) {
1010 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1011 		return ret;
1012 	} /* ret > 0 means positive match */
1013 
1014 	async_allowed = driver_allows_async_probing(drv);
1015 
1016 	if (async_allowed)
1017 		data->have_async = true;
1018 
1019 	if (data->check_async && async_allowed != data->want_async)
1020 		return 0;
1021 
1022 	/*
1023 	 * Ignore errors returned by ->probe so that the next driver can try
1024 	 * its luck.
1025 	 */
1026 	ret = driver_probe_device(drv, dev);
1027 	if (ret < 0)
1028 		return ret;
1029 	return ret == 0;
1030 }
1031 
1032 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
1033 {
1034 	struct device *dev = _dev;
1035 	struct device_attach_data data = {
1036 		.dev		= dev,
1037 		.check_async	= true,
1038 		.want_async	= true,
1039 	};
1040 
1041 	device_lock(dev);
1042 
1043 	/*
1044 	 * Check if device has already been removed or claimed. This may
1045 	 * happen with driver loading, device discovery/registration,
1046 	 * and deferred probe processing happens all at once with
1047 	 * multiple threads.
1048 	 */
1049 	if (dev->p->dead || dev->driver)
1050 		goto out_unlock;
1051 
1052 	if (dev->parent)
1053 		pm_runtime_get_sync(dev->parent);
1054 
1055 	bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
1056 	dev_dbg(dev, "async probe completed\n");
1057 
1058 	pm_request_idle(dev);
1059 
1060 	if (dev->parent)
1061 		pm_runtime_put(dev->parent);
1062 out_unlock:
1063 	device_unlock(dev);
1064 
1065 	put_device(dev);
1066 }
1067 
1068 static int __device_attach(struct device *dev, bool allow_async)
1069 {
1070 	int ret = 0;
1071 	bool async = false;
1072 
1073 	device_lock(dev);
1074 	if (dev->p->dead) {
1075 		goto out_unlock;
1076 	} else if (dev->driver) {
1077 		if (device_is_bound(dev)) {
1078 			ret = 1;
1079 			goto out_unlock;
1080 		}
1081 		ret = device_bind_driver(dev);
1082 		if (ret == 0)
1083 			ret = 1;
1084 		else {
1085 			device_set_driver(dev, NULL);
1086 			ret = 0;
1087 		}
1088 	} else {
1089 		struct device_attach_data data = {
1090 			.dev = dev,
1091 			.check_async = allow_async,
1092 			.want_async = false,
1093 		};
1094 
1095 		if (dev->parent)
1096 			pm_runtime_get_sync(dev->parent);
1097 
1098 		ret = bus_for_each_drv(dev->bus, NULL, &data,
1099 					__device_attach_driver);
1100 		if (!ret && allow_async && data.have_async) {
1101 			/*
1102 			 * If we could not find appropriate driver
1103 			 * synchronously and we are allowed to do
1104 			 * async probes and there are drivers that
1105 			 * want to probe asynchronously, we'll
1106 			 * try them.
1107 			 */
1108 			dev_dbg(dev, "scheduling asynchronous probe\n");
1109 			get_device(dev);
1110 			async = true;
1111 		} else {
1112 			pm_request_idle(dev);
1113 		}
1114 
1115 		if (dev->parent)
1116 			pm_runtime_put(dev->parent);
1117 	}
1118 out_unlock:
1119 	device_unlock(dev);
1120 	if (async)
1121 		async_schedule_dev(__device_attach_async_helper, dev);
1122 	return ret;
1123 }
1124 
1125 /**
1126  * device_attach - try to attach device to a driver.
1127  * @dev: device.
1128  *
1129  * Walk the list of drivers that the bus has and call
1130  * driver_probe_device() for each pair. If a compatible
1131  * pair is found, break out and return.
1132  *
1133  * Returns 1 if the device was bound to a driver;
1134  * 0 if no matching driver was found;
1135  * -ENODEV if the device is not registered.
1136  *
1137  * When called for a USB interface, @dev->parent lock must be held.
1138  */
1139 int device_attach(struct device *dev)
1140 {
1141 	return __device_attach(dev, false);
1142 }
1143 EXPORT_SYMBOL_GPL(device_attach);
1144 
1145 void device_initial_probe(struct device *dev)
1146 {
1147 	struct subsys_private *sp = bus_to_subsys(dev->bus);
1148 
1149 	if (!sp)
1150 		return;
1151 
1152 	if (sp->drivers_autoprobe)
1153 		__device_attach(dev, true);
1154 
1155 	subsys_put(sp);
1156 }
1157 
1158 /*
1159  * __device_driver_lock - acquire locks needed to manipulate dev->drv
1160  * @dev: Device we will update driver info for
1161  * @parent: Parent device. Needed if the bus requires parent lock
1162  *
1163  * This function will take the required locks for manipulating dev->drv.
1164  * Normally this will just be the @dev lock, but when called for a USB
1165  * interface, @parent lock will be held as well.
1166  */
1167 static void __device_driver_lock(struct device *dev, struct device *parent)
1168 {
1169 	if (parent && dev->bus->need_parent_lock)
1170 		device_lock(parent);
1171 	device_lock(dev);
1172 }
1173 
1174 /*
1175  * __device_driver_unlock - release locks needed to manipulate dev->drv
1176  * @dev: Device we will update driver info for
1177  * @parent: Parent device. Needed if the bus requires parent lock
1178  *
1179  * This function will release the required locks for manipulating dev->drv.
1180  * Normally this will just be the @dev lock, but when called for a
1181  * USB interface, @parent lock will be released as well.
1182  */
1183 static void __device_driver_unlock(struct device *dev, struct device *parent)
1184 {
1185 	device_unlock(dev);
1186 	if (parent && dev->bus->need_parent_lock)
1187 		device_unlock(parent);
1188 }
1189 
1190 /**
1191  * device_driver_attach - attach a specific driver to a specific device
1192  * @drv: Driver to attach
1193  * @dev: Device to attach it to
1194  *
1195  * Manually attach driver to a device. Will acquire both @dev lock and
1196  * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
1197  */
1198 int device_driver_attach(const struct device_driver *drv, struct device *dev)
1199 {
1200 	int ret;
1201 
1202 	__device_driver_lock(dev, dev->parent);
1203 	ret = __driver_probe_device(drv, dev);
1204 	__device_driver_unlock(dev, dev->parent);
1205 
1206 	/* also return probe errors as normal negative errnos */
1207 	if (ret > 0)
1208 		ret = -ret;
1209 	if (ret == -EPROBE_DEFER)
1210 		return -EAGAIN;
1211 	return ret;
1212 }
1213 EXPORT_SYMBOL_GPL(device_driver_attach);
1214 
1215 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
1216 {
1217 	struct device *dev = _dev;
1218 	const struct device_driver *drv;
1219 	int ret;
1220 
1221 	__device_driver_lock(dev, dev->parent);
1222 	drv = dev->p->async_driver;
1223 	dev->p->async_driver = NULL;
1224 	ret = driver_probe_device(drv, dev);
1225 	__device_driver_unlock(dev, dev->parent);
1226 
1227 	dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1228 
1229 	put_device(dev);
1230 }
1231 
1232 static int __driver_attach(struct device *dev, void *data)
1233 {
1234 	const struct device_driver *drv = data;
1235 	bool async = false;
1236 	int ret;
1237 
1238 	/*
1239 	 * Lock device and try to bind to it. We drop the error
1240 	 * here and always return 0, because we need to keep trying
1241 	 * to bind to devices and some drivers will return an error
1242 	 * simply if it didn't support the device.
1243 	 *
1244 	 * driver_probe_device() will spit a warning if there
1245 	 * is an error.
1246 	 */
1247 
1248 	ret = driver_match_device(drv, dev);
1249 	if (ret == 0) {
1250 		/* no match */
1251 		return 0;
1252 	} else if (ret == -EPROBE_DEFER) {
1253 		dev_dbg(dev, "Device match requests probe deferral\n");
1254 		dev_set_can_match(dev);
1255 		driver_deferred_probe_add(dev);
1256 		/*
1257 		 * Driver could not match with device, but may match with
1258 		 * another device on the bus.
1259 		 */
1260 		return 0;
1261 	} else if (ret < 0) {
1262 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1263 		/*
1264 		 * Driver could not match with device, but may match with
1265 		 * another device on the bus.
1266 		 */
1267 		return 0;
1268 	} /* ret > 0 means positive match */
1269 
1270 	if (driver_allows_async_probing(drv)) {
1271 		/*
1272 		 * Instead of probing the device synchronously we will
1273 		 * probe it asynchronously to allow for more parallelism.
1274 		 *
1275 		 * We only take the device lock here in order to guarantee
1276 		 * that the dev->driver and async_driver fields are protected
1277 		 */
1278 		dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1279 		device_lock(dev);
1280 		if (!dev->driver && !dev->p->async_driver) {
1281 			get_device(dev);
1282 			dev->p->async_driver = drv;
1283 			async = true;
1284 		}
1285 		device_unlock(dev);
1286 		if (async)
1287 			async_schedule_dev(__driver_attach_async_helper, dev);
1288 		return 0;
1289 	}
1290 
1291 	__device_driver_lock(dev, dev->parent);
1292 	driver_probe_device(drv, dev);
1293 	__device_driver_unlock(dev, dev->parent);
1294 
1295 	return 0;
1296 }
1297 
1298 /**
1299  * driver_attach - try to bind driver to devices.
1300  * @drv: driver.
1301  *
1302  * Walk the list of devices that the bus has on it and try to
1303  * match the driver with each one.  If driver_probe_device()
1304  * returns 0 and the @dev->driver is set, we've found a
1305  * compatible pair.
1306  */
1307 int driver_attach(const struct device_driver *drv)
1308 {
1309 	/* The (void *) will be put back to const * in __driver_attach() */
1310 	return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
1311 }
1312 EXPORT_SYMBOL_GPL(driver_attach);
1313 
1314 /*
1315  * __device_release_driver() must be called with @dev lock held.
1316  * When called for a USB interface, @dev->parent lock must be held as well.
1317  */
1318 static void __device_release_driver(struct device *dev, struct device *parent)
1319 {
1320 	struct device_driver *drv;
1321 
1322 	drv = dev->driver;
1323 	if (drv) {
1324 		pm_runtime_get_sync(dev);
1325 
1326 		while (device_links_busy(dev)) {
1327 			__device_driver_unlock(dev, parent);
1328 
1329 			device_links_unbind_consumers(dev);
1330 
1331 			__device_driver_lock(dev, parent);
1332 			/*
1333 			 * A concurrent invocation of the same function might
1334 			 * have released the driver successfully while this one
1335 			 * was waiting, so check for that.
1336 			 */
1337 			if (dev->driver != drv) {
1338 				pm_runtime_put(dev);
1339 				return;
1340 			}
1341 		}
1342 
1343 		driver_sysfs_remove(dev);
1344 
1345 		bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER);
1346 
1347 		pm_runtime_put_sync(dev);
1348 
1349 		device_remove(dev);
1350 
1351 		if (dev->bus && dev->bus->dma_cleanup)
1352 			dev->bus->dma_cleanup(dev);
1353 
1354 		device_unbind_cleanup(dev);
1355 		device_links_driver_cleanup(dev);
1356 
1357 		klist_remove(&dev->p->knode_driver);
1358 		device_pm_check_callbacks(dev);
1359 
1360 		bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER);
1361 		kobject_uevent(&dev->kobj, KOBJ_UNBIND);
1362 	}
1363 }
1364 
1365 void device_release_driver_internal(struct device *dev,
1366 				    const struct device_driver *drv,
1367 				    struct device *parent)
1368 {
1369 	__device_driver_lock(dev, parent);
1370 
1371 	if (!drv || drv == dev->driver)
1372 		__device_release_driver(dev, parent);
1373 
1374 	__device_driver_unlock(dev, parent);
1375 }
1376 
1377 /**
1378  * device_release_driver - manually detach device from driver.
1379  * @dev: device.
1380  *
1381  * Manually detach device from driver.
1382  * When called for a USB interface, @dev->parent lock must be held.
1383  *
1384  * If this function is to be called with @dev->parent lock held, ensure that
1385  * the device's consumers are unbound in advance or that their locks can be
1386  * acquired under the @dev->parent lock.
1387  */
1388 void device_release_driver(struct device *dev)
1389 {
1390 	/*
1391 	 * If anyone calls device_release_driver() recursively from
1392 	 * within their ->remove callback for the same device, they
1393 	 * will deadlock right here.
1394 	 */
1395 	device_release_driver_internal(dev, NULL, NULL);
1396 }
1397 EXPORT_SYMBOL_GPL(device_release_driver);
1398 
1399 /**
1400  * device_driver_detach - detach driver from a specific device
1401  * @dev: device to detach driver from
1402  *
1403  * Detach driver from device. Will acquire both @dev lock and @dev->parent
1404  * lock if needed.
1405  */
1406 void device_driver_detach(struct device *dev)
1407 {
1408 	device_release_driver_internal(dev, NULL, dev->parent);
1409 }
1410 
1411 /**
1412  * driver_detach - detach driver from all devices it controls.
1413  * @drv: driver.
1414  */
1415 void driver_detach(const struct device_driver *drv)
1416 {
1417 	struct device_private *dev_prv;
1418 	struct device *dev;
1419 
1420 	if (driver_allows_async_probing(drv))
1421 		async_synchronize_full();
1422 
1423 	for (;;) {
1424 		spin_lock(&drv->p->klist_devices.k_lock);
1425 		if (list_empty(&drv->p->klist_devices.k_list)) {
1426 			spin_unlock(&drv->p->klist_devices.k_lock);
1427 			break;
1428 		}
1429 		dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
1430 				     struct device_private,
1431 				     knode_driver.n_node);
1432 		dev = dev_prv->device;
1433 		get_device(dev);
1434 		spin_unlock(&drv->p->klist_devices.k_lock);
1435 		device_release_driver_internal(dev, drv, dev->parent);
1436 		put_device(dev);
1437 	}
1438 }
1439