xref: /linux/drivers/base/dd.c (revision 4aca5e62f37dd10cc771d5489900f927d133a9f1)
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 (cancel_delayed_work(&deferred_probe_timeout_work)) {
327 		schedule_delayed_work(&deferred_probe_timeout_work,
328 				driver_deferred_probe_timeout * HZ);
329 		pr_debug("Extended deferred probe timeout by %d secs\n",
330 					driver_deferred_probe_timeout);
331 	}
332 }
333 
334 /**
335  * deferred_probe_initcall() - Enable probing of deferred devices
336  *
337  * We don't want to get in the way when the bulk of drivers are getting probed.
338  * Instead, this initcall makes sure that deferred probing is delayed until
339  * late_initcall time.
340  */
341 static int deferred_probe_initcall(void)
342 {
343 	debugfs_create_file("devices_deferred", 0444, NULL, NULL,
344 			    &deferred_devs_fops);
345 
346 	driver_deferred_probe_enable = true;
347 	driver_deferred_probe_trigger();
348 	/* Sort as many dependencies as possible before exiting initcalls */
349 	flush_work(&deferred_probe_work);
350 	initcalls_done = true;
351 
352 	if (!IS_ENABLED(CONFIG_MODULES))
353 		fw_devlink_drivers_done();
354 
355 	/*
356 	 * Trigger deferred probe again, this time we won't defer anything
357 	 * that is optional
358 	 */
359 	driver_deferred_probe_trigger();
360 	flush_work(&deferred_probe_work);
361 
362 	if (driver_deferred_probe_timeout > 0) {
363 		schedule_delayed_work(&deferred_probe_timeout_work,
364 			driver_deferred_probe_timeout * HZ);
365 	}
366 
367 	if (!IS_ENABLED(CONFIG_MODULES))
368 		fw_devlink_probing_done();
369 
370 	return 0;
371 }
372 late_initcall(deferred_probe_initcall);
373 
374 static void __exit deferred_probe_exit(void)
375 {
376 	debugfs_lookup_and_remove("devices_deferred", NULL);
377 }
378 __exitcall(deferred_probe_exit);
379 
380 int __device_set_driver_override(struct device *dev, const char *s, size_t len)
381 {
382 	const char *new = NULL, *old;
383 
384 	if (!s)
385 		return -EINVAL;
386 
387 	/*
388 	 * The stored value will be used in sysfs show callback (sysfs_emit()),
389 	 * which has a length limit of PAGE_SIZE and adds a trailing newline.
390 	 * Thus we can store one character less to avoid truncation during sysfs
391 	 * show.
392 	 */
393 	if (len >= (PAGE_SIZE - 1))
394 		return -EINVAL;
395 
396 	/*
397 	 * Compute the real length of the string in case userspace sends us a
398 	 * bunch of \0 characters like python likes to do.
399 	 */
400 	len = strlen(s);
401 
402 	/* Handle trailing newline */
403 	if (len) {
404 		char *cp;
405 
406 		cp = strnchr(s, len, '\n');
407 		if (cp)
408 			len = cp - s;
409 	}
410 
411 	/*
412 	 * If empty string or "\n" passed, new remains NULL, clearing
413 	 * the driver_override.name.
414 	 */
415 	if (len) {
416 		new = kstrndup(s, len, GFP_KERNEL);
417 		if (!new)
418 			return -ENOMEM;
419 	}
420 
421 	scoped_guard(spinlock, &dev->driver_override.lock) {
422 		old = dev->driver_override.name;
423 		dev->driver_override.name = new;
424 	}
425 
426 	kfree(old);
427 
428 	return 0;
429 }
430 EXPORT_SYMBOL_GPL(__device_set_driver_override);
431 
432 /**
433  * device_is_bound() - Check if device is bound to a driver
434  * @dev: device to check
435  *
436  * Returns true if passed device has already finished probing successfully
437  * against a driver.
438  *
439  * This function must be called with the device lock held.
440  */
441 bool device_is_bound(struct device *dev)
442 {
443 	return dev->p && klist_node_attached(&dev->p->knode_driver);
444 }
445 EXPORT_SYMBOL_GPL(device_is_bound);
446 
447 static void driver_bound(struct device *dev)
448 {
449 	if (device_is_bound(dev)) {
450 		dev_warn(dev, "%s: device already bound\n", __func__);
451 		return;
452 	}
453 
454 	dev_dbg(dev, "driver: '%s': %s: bound to device\n", dev->driver->name,
455 		__func__);
456 
457 	klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
458 	device_links_driver_bound(dev);
459 
460 	device_pm_check_callbacks(dev);
461 
462 	/*
463 	 * Make sure the device is no longer in one of the deferred lists and
464 	 * kick off retrying all pending devices
465 	 */
466 	driver_deferred_probe_del(dev);
467 	driver_deferred_probe_trigger();
468 
469 	bus_notify(dev, BUS_NOTIFY_BOUND_DRIVER);
470 	kobject_uevent(&dev->kobj, KOBJ_BIND);
471 }
472 
473 static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
474 			    const char *buf, size_t count)
475 {
476 	device_lock(dev);
477 	dev->driver->coredump(dev);
478 	device_unlock(dev);
479 
480 	return count;
481 }
482 static DEVICE_ATTR_WO(coredump);
483 
484 static int driver_sysfs_add(struct device *dev)
485 {
486 	int ret;
487 
488 	bus_notify(dev, BUS_NOTIFY_BIND_DRIVER);
489 
490 	ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
491 				kobject_name(&dev->kobj));
492 	if (ret)
493 		goto fail;
494 
495 	ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
496 				"driver");
497 	if (ret)
498 		goto rm_dev;
499 
500 	if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump)
501 		return 0;
502 
503 	ret = device_create_file(dev, &dev_attr_coredump);
504 	if (!ret)
505 		return 0;
506 
507 	sysfs_remove_link(&dev->kobj, "driver");
508 
509 rm_dev:
510 	sysfs_remove_link(&dev->driver->p->kobj,
511 			  kobject_name(&dev->kobj));
512 
513 fail:
514 	return ret;
515 }
516 
517 static void driver_sysfs_remove(struct device *dev)
518 {
519 	struct device_driver *drv = dev->driver;
520 
521 	if (drv) {
522 		if (drv->coredump)
523 			device_remove_file(dev, &dev_attr_coredump);
524 		sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
525 		sysfs_remove_link(&dev->kobj, "driver");
526 	}
527 }
528 
529 /**
530  * device_bind_driver - bind a driver to one device.
531  * @dev: device.
532  *
533  * Allow manual attachment of a driver to a device.
534  * Caller must have already set @dev->driver.
535  *
536  * Note that this does not modify the bus reference count.
537  * Please verify that is accounted for before calling this.
538  * (It is ok to call with no other effort from a driver's probe() method.)
539  *
540  * This function must be called with the device lock held.
541  *
542  * Callers should prefer to use device_driver_attach() instead.
543  */
544 int device_bind_driver(struct device *dev)
545 {
546 	int ret;
547 
548 	ret = driver_sysfs_add(dev);
549 	if (!ret) {
550 		device_links_force_bind(dev);
551 		driver_bound(dev);
552 	}
553 	else
554 		bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
555 	return ret;
556 }
557 EXPORT_SYMBOL_GPL(device_bind_driver);
558 
559 static atomic_t probe_count = ATOMIC_INIT(0);
560 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
561 
562 static ssize_t state_synced_store(struct device *dev,
563 				  struct device_attribute *attr,
564 				  const char *buf, size_t count)
565 {
566 	int ret = 0;
567 
568 	if (strcmp("1", buf))
569 		return -EINVAL;
570 
571 	device_lock(dev);
572 	if (!dev_test_and_set_state_synced(dev))
573 		dev_sync_state(dev);
574 	else
575 		ret = -EINVAL;
576 	device_unlock(dev);
577 
578 	return ret ? ret : count;
579 }
580 
581 static ssize_t state_synced_show(struct device *dev,
582 				 struct device_attribute *attr, char *buf)
583 {
584 	bool val;
585 
586 	device_lock(dev);
587 	val = dev_state_synced(dev);
588 	device_unlock(dev);
589 
590 	return sysfs_emit(buf, "%u\n", val);
591 }
592 static DEVICE_ATTR_RW(state_synced);
593 
594 static void device_unbind_cleanup(struct device *dev)
595 {
596 	devres_release_all(dev);
597 	if (dev->driver->p_cb.post_unbind_rust)
598 		dev->driver->p_cb.post_unbind_rust(dev);
599 	arch_teardown_dma_ops(dev);
600 	kfree(dev->dma_range_map);
601 	dev->dma_range_map = NULL;
602 	device_set_driver(dev, NULL);
603 	dev_set_drvdata(dev, NULL);
604 	dev_pm_domain_detach(dev, dev->power.detach_power_off);
605 	if (dev->pm_domain && dev->pm_domain->dismiss)
606 		dev->pm_domain->dismiss(dev);
607 	pm_runtime_reinit(dev);
608 	dev_pm_set_driver_flags(dev, 0);
609 }
610 
611 static void device_remove(struct device *dev)
612 {
613 	device_remove_file(dev, &dev_attr_state_synced);
614 	device_remove_groups(dev, dev->driver->dev_groups);
615 
616 	if (dev->bus && dev->bus->remove)
617 		dev->bus->remove(dev);
618 	else if (dev->driver->remove)
619 		dev->driver->remove(dev);
620 }
621 
622 static int call_driver_probe(struct device *dev, const struct device_driver *drv)
623 {
624 	int ret = 0;
625 
626 	if (dev->bus->probe)
627 		ret = dev->bus->probe(dev);
628 	else if (drv->probe)
629 		ret = drv->probe(dev);
630 
631 	switch (ret) {
632 	case 0:
633 		break;
634 	case -EPROBE_DEFER:
635 		/* Driver requested deferred probing */
636 		dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
637 		break;
638 	case -ENODEV:
639 	case -ENXIO:
640 		dev_dbg(dev, "probe with driver %s rejects match %d\n",
641 			drv->name, ret);
642 		break;
643 	default:
644 		/* driver matched but the probe failed */
645 		dev_err(dev, "probe with driver %s failed with error %d\n",
646 			drv->name, ret);
647 		break;
648 	}
649 
650 	return ret;
651 }
652 
653 static int really_probe(struct device *dev, const struct device_driver *drv)
654 {
655 	bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
656 			   !drv->suppress_bind_attrs;
657 	int ret, link_ret;
658 
659 	if (defer_all_probes) {
660 		/*
661 		 * Value of defer_all_probes can be set only by
662 		 * device_block_probing() which, in turn, will call
663 		 * wait_for_device_probe() right after that to avoid any races.
664 		 */
665 		dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
666 		return -EPROBE_DEFER;
667 	}
668 
669 	link_ret = device_links_check_suppliers(dev);
670 	if (link_ret == -EPROBE_DEFER)
671 		return link_ret;
672 
673 	dev_dbg(dev, "bus: '%s': %s: probing driver %s with device\n",
674 		drv->bus->name, __func__, drv->name);
675 	if (!list_empty(&dev->devres_head)) {
676 		dev_crit(dev, "Resources present before probing\n");
677 		ret = -EBUSY;
678 		goto done;
679 	}
680 
681 re_probe:
682 	device_set_driver(dev, drv);
683 
684 	/* If using pinctrl, bind pins now before probing */
685 	ret = pinctrl_bind_pins(dev);
686 	if (ret)
687 		goto pinctrl_bind_failed;
688 
689 	if (dev->bus->dma_configure) {
690 		ret = dev->bus->dma_configure(dev);
691 		if (ret)
692 			goto pinctrl_bind_failed;
693 	}
694 
695 	ret = driver_sysfs_add(dev);
696 	if (ret) {
697 		dev_err(dev, "%s: driver_sysfs_add failed\n", __func__);
698 		goto sysfs_failed;
699 	}
700 
701 	if (dev->pm_domain && dev->pm_domain->activate) {
702 		ret = dev->pm_domain->activate(dev);
703 		if (ret)
704 			goto probe_failed;
705 	}
706 
707 	ret = call_driver_probe(dev, drv);
708 	if (ret) {
709 		/*
710 		 * If fw_devlink_best_effort is active (denoted by -EAGAIN), the
711 		 * device might actually probe properly once some of its missing
712 		 * suppliers have probed. So, treat this as if the driver
713 		 * returned -EPROBE_DEFER.
714 		 */
715 		if (link_ret == -EAGAIN)
716 			ret = -EPROBE_DEFER;
717 
718 		/*
719 		 * Return probe errors as positive values so that the callers
720 		 * can distinguish them from other errors.
721 		 */
722 		ret = -ret;
723 		goto probe_failed;
724 	}
725 
726 	ret = device_add_groups(dev, drv->dev_groups);
727 	if (ret) {
728 		dev_err(dev, "device_add_groups() failed\n");
729 		goto dev_groups_failed;
730 	}
731 
732 	if (dev_has_sync_state(dev)) {
733 		ret = device_create_file(dev, &dev_attr_state_synced);
734 		if (ret) {
735 			dev_err(dev, "state_synced sysfs add failed\n");
736 			goto dev_sysfs_state_synced_failed;
737 		}
738 	}
739 
740 	if (test_remove) {
741 		test_remove = false;
742 
743 		device_remove(dev);
744 		driver_sysfs_remove(dev);
745 		if (dev->bus && dev->bus->dma_cleanup)
746 			dev->bus->dma_cleanup(dev);
747 		device_unbind_cleanup(dev);
748 
749 		goto re_probe;
750 	}
751 
752 	pinctrl_init_done(dev);
753 
754 	if (dev->pm_domain && dev->pm_domain->sync)
755 		dev->pm_domain->sync(dev);
756 
757 	driver_bound(dev);
758 	dev_dbg(dev, "bus: '%s': %s: bound device to driver %s\n",
759 		drv->bus->name, __func__, drv->name);
760 	goto done;
761 
762 dev_sysfs_state_synced_failed:
763 dev_groups_failed:
764 	device_remove(dev);
765 probe_failed:
766 	driver_sysfs_remove(dev);
767 sysfs_failed:
768 	bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
769 	if (dev->bus && dev->bus->dma_cleanup)
770 		dev->bus->dma_cleanup(dev);
771 pinctrl_bind_failed:
772 	device_links_no_driver(dev);
773 	device_unbind_cleanup(dev);
774 done:
775 	return ret;
776 }
777 
778 /*
779  * For initcall_debug, show the driver probe time.
780  */
781 static int really_probe_debug(struct device *dev, const struct device_driver *drv)
782 {
783 	ktime_t calltime, rettime;
784 	int ret;
785 
786 	calltime = ktime_get();
787 	ret = really_probe(dev, drv);
788 	rettime = ktime_get();
789 	/*
790 	 * Don't change this to pr_debug() because that requires
791 	 * CONFIG_DYNAMIC_DEBUG and we want a simple 'initcall_debug' on the
792 	 * kernel commandline to print this all the time at the debug level.
793 	 */
794 	printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
795 		 dev_name(dev), ret, ktime_us_delta(rettime, calltime));
796 	return ret;
797 }
798 
799 /**
800  * driver_probe_done
801  * Determine if the probe sequence is finished or not.
802  *
803  * Should somehow figure out how to use a semaphore, not an atomic variable...
804  */
805 bool __init driver_probe_done(void)
806 {
807 	int local_probe_count = atomic_read(&probe_count);
808 
809 	pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
810 	return !local_probe_count;
811 }
812 
813 /**
814  * wait_for_device_probe
815  * Wait for device probing to be completed.
816  */
817 void wait_for_device_probe(void)
818 {
819 	/* wait for the deferred probe workqueue to finish */
820 	flush_work(&deferred_probe_work);
821 
822 	/* wait for the known devices to complete their probing */
823 	wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
824 	async_synchronize_full();
825 }
826 EXPORT_SYMBOL_GPL(wait_for_device_probe);
827 
828 static int __driver_probe_device(const struct device_driver *drv, struct device *dev)
829 {
830 	int ret = 0;
831 
832 	if (dev->p->dead || !device_is_registered(dev))
833 		return -ENODEV;
834 	if (dev->driver)
835 		return -EBUSY;
836 
837 	/*
838 	 * In device_add(), the "struct device" gets linked into the subsystem's
839 	 * list of devices and broadcast to userspace (via uevent) before we're
840 	 * quite ready to probe. Those open pathways to driver probe before
841 	 * we've finished enough of device_add() to reliably support probe.
842 	 * Detect this and tell other pathways to try again later. device_add()
843 	 * itself will also try to probe immediately after setting
844 	 * "ready_to_probe".
845 	 */
846 	if (!dev_ready_to_probe(dev))
847 		return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n");
848 
849 	/*
850 	 * Call dev_set_can_match() after calling dev_ready_to_probe(), so
851 	 * driver_deferred_probe_add() won't actually add the device to the
852 	 * deferred probe list when dev_ready_to_probe() returns false.
853 	 *
854 	 * When dev_ready_to_probe() returns false, it means that device_add()
855 	 * will do another probe() attempt for us.
856 	 */
857 	dev_set_can_match(dev);
858 	dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
859 		drv->bus->name, __func__, drv->name);
860 
861 	pm_runtime_get_suppliers(dev);
862 	if (dev->parent)
863 		pm_runtime_get_sync(dev->parent);
864 
865 	pm_runtime_barrier(dev);
866 	if (initcall_debug)
867 		ret = really_probe_debug(dev, drv);
868 	else
869 		ret = really_probe(dev, drv);
870 	pm_request_idle(dev);
871 
872 	if (dev->parent)
873 		pm_runtime_put(dev->parent);
874 
875 	pm_runtime_put_suppliers(dev);
876 	return ret;
877 }
878 
879 /**
880  * driver_probe_device - attempt to bind device & driver together
881  * @drv: driver to bind a device to
882  * @dev: device to try to bind to the driver
883  *
884  * This function returns -ENODEV if the device is not registered, -EBUSY if it
885  * already has a driver, 0 if the device is bound successfully and a positive
886  * (inverted) error code for failures from the ->probe method.
887  *
888  * This function must be called with @dev lock held.  When called for a
889  * USB interface, @dev->parent lock must be held as well.
890  *
891  * If the device has a parent, runtime-resume the parent before driver probing.
892  */
893 static int driver_probe_device(const struct device_driver *drv, struct device *dev)
894 {
895 	int trigger_count = atomic_read(&deferred_trigger_count);
896 	int ret;
897 
898 	atomic_inc(&probe_count);
899 	ret = __driver_probe_device(drv, dev);
900 	if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
901 		driver_deferred_probe_add(dev);
902 
903 		/*
904 		 * Did a trigger occur while probing? Need to re-trigger if yes
905 		 */
906 		if (trigger_count != atomic_read(&deferred_trigger_count) &&
907 		    !defer_all_probes)
908 			driver_deferred_probe_trigger();
909 	}
910 	atomic_dec(&probe_count);
911 	wake_up_all(&probe_waitqueue);
912 	return ret;
913 }
914 
915 static inline bool cmdline_requested_async_probing(const char *drv_name)
916 {
917 	bool async_drv;
918 
919 	async_drv = parse_option_str(async_probe_drv_names, drv_name);
920 
921 	return (async_probe_default != async_drv);
922 }
923 
924 /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
925 static int __init save_async_options(char *buf)
926 {
927 	if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
928 		pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
929 
930 	strscpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
931 	async_probe_default = parse_option_str(async_probe_drv_names, "*");
932 
933 	return 1;
934 }
935 __setup("driver_async_probe=", save_async_options);
936 
937 static bool driver_allows_async_probing(const struct device_driver *drv)
938 {
939 	switch (drv->probe_type) {
940 	case PROBE_PREFER_ASYNCHRONOUS:
941 		return true;
942 
943 	case PROBE_FORCE_SYNCHRONOUS:
944 		return false;
945 
946 	default:
947 		if (cmdline_requested_async_probing(drv->name))
948 			return true;
949 
950 		if (module_requested_async_probing(drv->owner))
951 			return true;
952 
953 		return false;
954 	}
955 }
956 
957 struct device_attach_data {
958 	struct device *dev;
959 
960 	/*
961 	 * Indicates whether we are considering asynchronous probing or
962 	 * not. Only initial binding after device or driver registration
963 	 * (including deferral processing) may be done asynchronously, the
964 	 * rest is always synchronous, as we expect it is being done by
965 	 * request from userspace.
966 	 */
967 	bool check_async;
968 
969 	/*
970 	 * Indicates if we are binding synchronous or asynchronous drivers.
971 	 * When asynchronous probing is enabled we'll execute 2 passes
972 	 * over drivers: first pass doing synchronous probing and second
973 	 * doing asynchronous probing (if synchronous did not succeed -
974 	 * most likely because there was no driver requiring synchronous
975 	 * probing - and we found asynchronous driver during first pass).
976 	 * The 2 passes are done because we can't shoot asynchronous
977 	 * probe for given device and driver from bus_for_each_drv() since
978 	 * driver pointer is not guaranteed to stay valid once
979 	 * bus_for_each_drv() iterates to the next driver on the bus.
980 	 */
981 	bool want_async;
982 
983 	/*
984 	 * We'll set have_async to 'true' if, while scanning for matching
985 	 * driver, we'll encounter one that requests asynchronous probing.
986 	 */
987 	bool have_async;
988 };
989 
990 static int __device_attach_driver(struct device_driver *drv, void *_data)
991 {
992 	struct device_attach_data *data = _data;
993 	struct device *dev = data->dev;
994 	bool async_allowed;
995 	int ret;
996 
997 	ret = driver_match_device(drv, dev);
998 	if (ret == 0) {
999 		/* no match */
1000 		return 0;
1001 	} else if (ret == -EPROBE_DEFER) {
1002 		dev_dbg(dev, "Device match requests probe deferral\n");
1003 		dev_set_can_match(dev);
1004 		driver_deferred_probe_add(dev);
1005 		/*
1006 		 * Device can't match with a driver right now, so don't attempt
1007 		 * to match or bind with other drivers on the bus.
1008 		 */
1009 		return ret;
1010 	} else if (ret < 0) {
1011 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1012 		return ret;
1013 	} /* ret > 0 means positive match */
1014 
1015 	async_allowed = driver_allows_async_probing(drv);
1016 
1017 	if (async_allowed)
1018 		data->have_async = true;
1019 
1020 	if (data->check_async && async_allowed != data->want_async)
1021 		return 0;
1022 
1023 	/*
1024 	 * Ignore errors returned by ->probe so that the next driver can try
1025 	 * its luck.
1026 	 */
1027 	ret = driver_probe_device(drv, dev);
1028 	if (ret < 0)
1029 		return ret;
1030 	return ret == 0;
1031 }
1032 
1033 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
1034 {
1035 	struct device *dev = _dev;
1036 	struct device_attach_data data = {
1037 		.dev		= dev,
1038 		.check_async	= true,
1039 		.want_async	= true,
1040 	};
1041 
1042 	device_lock(dev);
1043 
1044 	/*
1045 	 * Check if device has already been removed or claimed. This may
1046 	 * happen with driver loading, device discovery/registration,
1047 	 * and deferred probe processing happens all at once with
1048 	 * multiple threads.
1049 	 */
1050 	if (dev->p->dead || dev->driver)
1051 		goto out_unlock;
1052 
1053 	if (dev->parent)
1054 		pm_runtime_get_sync(dev->parent);
1055 
1056 	bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
1057 	dev_dbg(dev, "async probe completed\n");
1058 
1059 	pm_request_idle(dev);
1060 
1061 	if (dev->parent)
1062 		pm_runtime_put(dev->parent);
1063 out_unlock:
1064 	device_unlock(dev);
1065 
1066 	put_device(dev);
1067 }
1068 
1069 static int __device_attach(struct device *dev, bool allow_async)
1070 {
1071 	int ret = 0;
1072 	bool async = false;
1073 
1074 	device_lock(dev);
1075 	if (dev->p->dead) {
1076 		goto out_unlock;
1077 	} else if (dev->driver) {
1078 		if (device_is_bound(dev)) {
1079 			ret = 1;
1080 			goto out_unlock;
1081 		}
1082 		ret = device_bind_driver(dev);
1083 		if (ret == 0)
1084 			ret = 1;
1085 		else {
1086 			device_set_driver(dev, NULL);
1087 			ret = 0;
1088 		}
1089 	} else {
1090 		struct device_attach_data data = {
1091 			.dev = dev,
1092 			.check_async = allow_async,
1093 			.want_async = false,
1094 		};
1095 
1096 		if (dev->parent)
1097 			pm_runtime_get_sync(dev->parent);
1098 
1099 		ret = bus_for_each_drv(dev->bus, NULL, &data,
1100 					__device_attach_driver);
1101 		if (!ret && allow_async && data.have_async) {
1102 			/*
1103 			 * If we could not find appropriate driver
1104 			 * synchronously and we are allowed to do
1105 			 * async probes and there are drivers that
1106 			 * want to probe asynchronously, we'll
1107 			 * try them.
1108 			 */
1109 			dev_dbg(dev, "scheduling asynchronous probe\n");
1110 			get_device(dev);
1111 			async = true;
1112 		} else {
1113 			pm_request_idle(dev);
1114 		}
1115 
1116 		if (dev->parent)
1117 			pm_runtime_put(dev->parent);
1118 	}
1119 out_unlock:
1120 	device_unlock(dev);
1121 	if (async)
1122 		async_schedule_dev(__device_attach_async_helper, dev);
1123 	return ret;
1124 }
1125 
1126 /**
1127  * device_attach - try to attach device to a driver.
1128  * @dev: device.
1129  *
1130  * Walk the list of drivers that the bus has and call
1131  * driver_probe_device() for each pair. If a compatible
1132  * pair is found, break out and return.
1133  *
1134  * Returns 1 if the device was bound to a driver;
1135  * 0 if no matching driver was found;
1136  * -ENODEV if the device is not registered.
1137  *
1138  * When called for a USB interface, @dev->parent lock must be held.
1139  */
1140 int device_attach(struct device *dev)
1141 {
1142 	return __device_attach(dev, false);
1143 }
1144 EXPORT_SYMBOL_GPL(device_attach);
1145 
1146 void device_initial_probe(struct device *dev)
1147 {
1148 	struct subsys_private *sp = bus_to_subsys(dev->bus);
1149 
1150 	if (!sp)
1151 		return;
1152 
1153 	if (sp->drivers_autoprobe)
1154 		__device_attach(dev, true);
1155 
1156 	subsys_put(sp);
1157 }
1158 
1159 /*
1160  * __device_driver_lock - acquire locks needed to manipulate dev->drv
1161  * @dev: Device we will update driver info for
1162  * @parent: Parent device. Needed if the bus requires parent lock
1163  *
1164  * This function will take the required locks for manipulating dev->drv.
1165  * Normally this will just be the @dev lock, but when called for a USB
1166  * interface, @parent lock will be held as well.
1167  */
1168 static void __device_driver_lock(struct device *dev, struct device *parent)
1169 {
1170 	if (parent && dev->bus->need_parent_lock)
1171 		device_lock(parent);
1172 	device_lock(dev);
1173 }
1174 
1175 /*
1176  * __device_driver_unlock - release locks needed to manipulate dev->drv
1177  * @dev: Device we will update driver info for
1178  * @parent: Parent device. Needed if the bus requires parent lock
1179  *
1180  * This function will release the required locks for manipulating dev->drv.
1181  * Normally this will just be the @dev lock, but when called for a
1182  * USB interface, @parent lock will be released as well.
1183  */
1184 static void __device_driver_unlock(struct device *dev, struct device *parent)
1185 {
1186 	device_unlock(dev);
1187 	if (parent && dev->bus->need_parent_lock)
1188 		device_unlock(parent);
1189 }
1190 
1191 /**
1192  * device_driver_attach - attach a specific driver to a specific device
1193  * @drv: Driver to attach
1194  * @dev: Device to attach it to
1195  *
1196  * Manually attach driver to a device. Will acquire both @dev lock and
1197  * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
1198  */
1199 int device_driver_attach(const struct device_driver *drv, struct device *dev)
1200 {
1201 	int ret;
1202 
1203 	__device_driver_lock(dev, dev->parent);
1204 	ret = __driver_probe_device(drv, dev);
1205 	__device_driver_unlock(dev, dev->parent);
1206 
1207 	/* also return probe errors as normal negative errnos */
1208 	if (ret > 0)
1209 		ret = -ret;
1210 	if (ret == -EPROBE_DEFER)
1211 		return -EAGAIN;
1212 	return ret;
1213 }
1214 EXPORT_SYMBOL_GPL(device_driver_attach);
1215 
1216 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
1217 {
1218 	struct device *dev = _dev;
1219 	const struct device_driver *drv;
1220 	int ret;
1221 
1222 	__device_driver_lock(dev, dev->parent);
1223 	drv = dev->p->async_driver;
1224 	dev->p->async_driver = NULL;
1225 	ret = driver_probe_device(drv, dev);
1226 	__device_driver_unlock(dev, dev->parent);
1227 
1228 	dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1229 
1230 	put_device(dev);
1231 }
1232 
1233 static int __driver_attach(struct device *dev, void *data)
1234 {
1235 	const struct device_driver *drv = data;
1236 	bool async = false;
1237 	int ret;
1238 
1239 	/*
1240 	 * Lock device and try to bind to it. We drop the error
1241 	 * here and always return 0, because we need to keep trying
1242 	 * to bind to devices and some drivers will return an error
1243 	 * simply if it didn't support the device.
1244 	 *
1245 	 * driver_probe_device() will spit a warning if there
1246 	 * is an error.
1247 	 */
1248 
1249 	ret = driver_match_device(drv, dev);
1250 	if (ret == 0) {
1251 		/* no match */
1252 		return 0;
1253 	} else if (ret == -EPROBE_DEFER) {
1254 		dev_dbg(dev, "Device match requests probe deferral\n");
1255 		dev_set_can_match(dev);
1256 		driver_deferred_probe_add(dev);
1257 		/*
1258 		 * Driver could not match with device, but may match with
1259 		 * another device on the bus.
1260 		 */
1261 		return 0;
1262 	} else if (ret < 0) {
1263 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1264 		/*
1265 		 * Driver could not match with device, but may match with
1266 		 * another device on the bus.
1267 		 */
1268 		return 0;
1269 	} /* ret > 0 means positive match */
1270 
1271 	if (driver_allows_async_probing(drv)) {
1272 		/*
1273 		 * Instead of probing the device synchronously we will
1274 		 * probe it asynchronously to allow for more parallelism.
1275 		 *
1276 		 * We only take the device lock here in order to guarantee
1277 		 * that the dev->driver and async_driver fields are protected
1278 		 */
1279 		dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1280 		device_lock(dev);
1281 		if (!dev->driver && !dev->p->async_driver) {
1282 			get_device(dev);
1283 			dev->p->async_driver = drv;
1284 			async = true;
1285 		}
1286 		device_unlock(dev);
1287 		if (async)
1288 			async_schedule_dev(__driver_attach_async_helper, dev);
1289 		return 0;
1290 	}
1291 
1292 	__device_driver_lock(dev, dev->parent);
1293 	driver_probe_device(drv, dev);
1294 	__device_driver_unlock(dev, dev->parent);
1295 
1296 	return 0;
1297 }
1298 
1299 /**
1300  * driver_attach - try to bind driver to devices.
1301  * @drv: driver.
1302  *
1303  * Walk the list of devices that the bus has on it and try to
1304  * match the driver with each one.  If driver_probe_device()
1305  * returns 0 and the @dev->driver is set, we've found a
1306  * compatible pair.
1307  */
1308 int driver_attach(const struct device_driver *drv)
1309 {
1310 	/* The (void *) will be put back to const * in __driver_attach() */
1311 	return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
1312 }
1313 EXPORT_SYMBOL_GPL(driver_attach);
1314 
1315 /*
1316  * __device_release_driver() must be called with @dev lock held.
1317  * When called for a USB interface, @dev->parent lock must be held as well.
1318  */
1319 static void __device_release_driver(struct device *dev, struct device *parent)
1320 {
1321 	struct device_driver *drv;
1322 
1323 	drv = dev->driver;
1324 	if (drv) {
1325 		pm_runtime_get_sync(dev);
1326 
1327 		while (device_links_busy(dev)) {
1328 			__device_driver_unlock(dev, parent);
1329 
1330 			device_links_unbind_consumers(dev);
1331 
1332 			__device_driver_lock(dev, parent);
1333 			/*
1334 			 * A concurrent invocation of the same function might
1335 			 * have released the driver successfully while this one
1336 			 * was waiting, so check for that.
1337 			 */
1338 			if (dev->driver != drv) {
1339 				pm_runtime_put(dev);
1340 				return;
1341 			}
1342 		}
1343 
1344 		driver_sysfs_remove(dev);
1345 
1346 		bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER);
1347 
1348 		pm_runtime_put_sync(dev);
1349 
1350 		device_remove(dev);
1351 
1352 		if (dev->bus && dev->bus->dma_cleanup)
1353 			dev->bus->dma_cleanup(dev);
1354 
1355 		device_unbind_cleanup(dev);
1356 		device_links_driver_cleanup(dev);
1357 
1358 		klist_remove(&dev->p->knode_driver);
1359 		device_pm_check_callbacks(dev);
1360 
1361 		bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER);
1362 		kobject_uevent(&dev->kobj, KOBJ_UNBIND);
1363 	}
1364 }
1365 
1366 void device_release_driver_internal(struct device *dev,
1367 				    const struct device_driver *drv,
1368 				    struct device *parent)
1369 {
1370 	__device_driver_lock(dev, parent);
1371 
1372 	if (!drv || drv == dev->driver)
1373 		__device_release_driver(dev, parent);
1374 
1375 	__device_driver_unlock(dev, parent);
1376 }
1377 
1378 /**
1379  * device_release_driver - manually detach device from driver.
1380  * @dev: device.
1381  *
1382  * Manually detach device from driver.
1383  * When called for a USB interface, @dev->parent lock must be held.
1384  *
1385  * If this function is to be called with @dev->parent lock held, ensure that
1386  * the device's consumers are unbound in advance or that their locks can be
1387  * acquired under the @dev->parent lock.
1388  */
1389 void device_release_driver(struct device *dev)
1390 {
1391 	/*
1392 	 * If anyone calls device_release_driver() recursively from
1393 	 * within their ->remove callback for the same device, they
1394 	 * will deadlock right here.
1395 	 */
1396 	device_release_driver_internal(dev, NULL, NULL);
1397 }
1398 EXPORT_SYMBOL_GPL(device_release_driver);
1399 
1400 /**
1401  * device_driver_detach - detach driver from a specific device
1402  * @dev: device to detach driver from
1403  *
1404  * Detach driver from device. Will acquire both @dev lock and @dev->parent
1405  * lock if needed.
1406  */
1407 void device_driver_detach(struct device *dev)
1408 {
1409 	device_release_driver_internal(dev, NULL, dev->parent);
1410 }
1411 
1412 /**
1413  * driver_detach - detach driver from all devices it controls.
1414  * @drv: driver.
1415  */
1416 void driver_detach(const struct device_driver *drv)
1417 {
1418 	struct device_private *dev_prv;
1419 	struct device *dev;
1420 
1421 	if (driver_allows_async_probing(drv))
1422 		async_synchronize_full();
1423 
1424 	for (;;) {
1425 		spin_lock(&drv->p->klist_devices.k_lock);
1426 		if (list_empty(&drv->p->klist_devices.k_list)) {
1427 			spin_unlock(&drv->p->klist_devices.k_lock);
1428 			break;
1429 		}
1430 		dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
1431 				     struct device_private,
1432 				     knode_driver.n_node);
1433 		dev = dev_prv->device;
1434 		get_device(dev);
1435 		spin_unlock(&drv->p->klist_devices.k_lock);
1436 		device_release_driver_internal(dev, drv, dev->parent);
1437 		put_device(dev);
1438 	}
1439 }
1440