xref: /linux/drivers/base/dd.c (revision 7d1f84f1b94e79cb95d974ab3ec54cb3a4d64f5b)
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_deferred() to re-attempt the probe.  The loop
90 	 * continues until every device in the active list is removed and
91 	 * retried.
92 	 *
93 	 * Note: Once the device is removed from the list and the mutex is
94 	 * released, it is possible for the device get freed by another thread
95 	 * and cause a illegal pointer dereference.  This code uses
96 	 * get/put_device() to ensure the device structure cannot disappear
97 	 * from under our feet.
98 	 */
99 	mutex_lock(&deferred_probe_mutex);
100 	while (!list_empty(&deferred_probe_active_list)) {
101 		private = list_first_entry(&deferred_probe_active_list,
102 					typeof(*dev->p), deferred_probe);
103 		dev = private->device;
104 		list_del_init(&private->deferred_probe);
105 
106 		get_device(dev);
107 
108 		__device_set_deferred_probe_reason(dev, NULL);
109 
110 		/*
111 		 * Drop the mutex while probing each device; the probe path may
112 		 * manipulate the deferred list
113 		 */
114 		mutex_unlock(&deferred_probe_mutex);
115 
116 		/*
117 		 * Force the device to the end of the dpm_list since
118 		 * the PM code assumes that the order we add things to
119 		 * the list is a good order for suspend but deferred
120 		 * probe makes that very unsafe.
121 		 */
122 		device_pm_move_to_tail(dev);
123 
124 		dev_dbg(dev, "Retrying from deferred list\n");
125 		bus_probe_device_deferred(dev);
126 		mutex_lock(&deferred_probe_mutex);
127 
128 		put_device(dev);
129 	}
130 	mutex_unlock(&deferred_probe_mutex);
131 }
132 static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
133 
134 void driver_deferred_probe_add(struct device *dev)
135 {
136 	if (!dev_can_match(dev))
137 		return;
138 
139 	mutex_lock(&deferred_probe_mutex);
140 	if (list_empty(&dev->p->deferred_probe)) {
141 		dev_dbg(dev, "Added to deferred list\n");
142 		list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
143 	}
144 	mutex_unlock(&deferred_probe_mutex);
145 }
146 
147 void driver_deferred_probe_del(struct device *dev)
148 {
149 	mutex_lock(&deferred_probe_mutex);
150 	if (!list_empty(&dev->p->deferred_probe)) {
151 		dev_dbg(dev, "Removed from deferred list\n");
152 		list_del_init(&dev->p->deferred_probe);
153 		__device_set_deferred_probe_reason(dev, NULL);
154 	}
155 	mutex_unlock(&deferred_probe_mutex);
156 }
157 
158 static bool driver_deferred_probe_enable;
159 /**
160  * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
161  *
162  * This functions moves all devices from the pending list to the active
163  * list and schedules the deferred probe workqueue to process them.  It
164  * should be called anytime a driver is successfully bound to a device.
165  *
166  * Note, there is a race condition in multi-threaded probe. In the case where
167  * more than one device is probing at the same time, it is possible for one
168  * probe to complete successfully while another is about to defer. If the second
169  * depends on the first, then it will get put on the pending list after the
170  * trigger event has already occurred and will be stuck there.
171  *
172  * The atomic 'deferred_trigger_count' is used to determine if a successful
173  * trigger has occurred in the midst of probing a driver. If the trigger count
174  * changes in the midst of a probe, then deferred processing should be triggered
175  * again.
176  */
177 void driver_deferred_probe_trigger(void)
178 {
179 	if (!driver_deferred_probe_enable)
180 		return;
181 
182 	/*
183 	 * A successful probe means that all the devices in the pending list
184 	 * should be triggered to be reprobed.  Move all the deferred devices
185 	 * into the active list so they can be retried by the workqueue
186 	 */
187 	mutex_lock(&deferred_probe_mutex);
188 	atomic_inc(&deferred_trigger_count);
189 	list_splice_tail_init(&deferred_probe_pending_list,
190 			      &deferred_probe_active_list);
191 	mutex_unlock(&deferred_probe_mutex);
192 
193 	/*
194 	 * Kick the re-probe thread.  It may already be scheduled, but it is
195 	 * safe to kick it again.
196 	 */
197 	queue_work(system_dfl_wq, &deferred_probe_work);
198 }
199 
200 /**
201  * device_block_probing() - Block/defer device's probes
202  *
203  *	It will disable probing of devices and defer their probes instead.
204  */
205 void device_block_probing(void)
206 {
207 	defer_all_probes = true;
208 	/* sync with probes to avoid races. */
209 	wait_for_device_probe();
210 }
211 
212 /**
213  * device_unblock_probing() - Unblock/enable device's probes
214  *
215  *	It will restore normal behavior and trigger re-probing of deferred
216  * devices.
217  */
218 void device_unblock_probing(void)
219 {
220 	defer_all_probes = false;
221 	driver_deferred_probe_trigger();
222 }
223 
224 /**
225  * device_set_deferred_probe_reason() - Set defer probe reason message for device
226  * @dev: the pointer to the struct device
227  * @vaf: the pointer to va_format structure with message
228  */
229 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
230 {
231 	const char *drv = dev_driver_string(dev);
232 	char *reason;
233 
234 	mutex_lock(&deferred_probe_mutex);
235 
236 	reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
237 	__device_set_deferred_probe_reason(dev, reason);
238 
239 	mutex_unlock(&deferred_probe_mutex);
240 }
241 
242 /*
243  * deferred_devs_show() - Show the devices in the deferred probe pending list.
244  */
245 static int deferred_devs_show(struct seq_file *s, void *data)
246 {
247 	struct device_private *curr;
248 
249 	mutex_lock(&deferred_probe_mutex);
250 
251 	list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
252 		seq_printf(s, "%s\t%s", dev_name(curr->device),
253 			   curr->deferred_probe_reason ?: "\n");
254 
255 	mutex_unlock(&deferred_probe_mutex);
256 
257 	return 0;
258 }
259 DEFINE_SHOW_ATTRIBUTE(deferred_devs);
260 
261 static int driver_deferred_probe_timeout = CONFIG_DRIVER_DEFERRED_PROBE_TIMEOUT;
262 
263 static int __init deferred_probe_timeout_setup(char *str)
264 {
265 	int timeout;
266 
267 	if (!kstrtoint(str, 10, &timeout))
268 		driver_deferred_probe_timeout = timeout;
269 	return 1;
270 }
271 __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
272 
273 /**
274  * driver_deferred_probe_check_state() - Check deferred probe state
275  * @dev: device to check
276  *
277  * Return:
278  * * -ENODEV if initcalls have completed and modules are disabled.
279  * * -ETIMEDOUT if the deferred probe timeout was set and has expired
280  *   and modules are enabled.
281  * * -EPROBE_DEFER in other cases.
282  *
283  * Drivers or subsystems can opt-in to calling this function instead of directly
284  * returning -EPROBE_DEFER.
285  */
286 int driver_deferred_probe_check_state(struct device *dev)
287 {
288 	if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
289 		dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
290 		return -ENODEV;
291 	}
292 
293 	if (!driver_deferred_probe_timeout && initcalls_done) {
294 		dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
295 		return -ETIMEDOUT;
296 	}
297 
298 	return -EPROBE_DEFER;
299 }
300 EXPORT_SYMBOL_GPL(driver_deferred_probe_check_state);
301 
302 static void deferred_probe_timeout_work_func(struct work_struct *work)
303 {
304 	struct device_private *p;
305 
306 	fw_devlink_drivers_done();
307 
308 	driver_deferred_probe_timeout = 0;
309 	driver_deferred_probe_trigger();
310 	flush_work(&deferred_probe_work);
311 
312 	mutex_lock(&deferred_probe_mutex);
313 	list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
314 		dev_warn(p->device, "deferred probe pending: %s", p->deferred_probe_reason ?: "(reason unknown)\n");
315 	mutex_unlock(&deferred_probe_mutex);
316 
317 	fw_devlink_probing_done();
318 }
319 static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
320 
321 void deferred_probe_extend_timeout(void)
322 {
323 	/*
324 	 * If the work hasn't been queued yet or if the work expired, don't
325 	 * start a new one.
326 	 */
327 	if (delayed_work_pending(&deferred_probe_timeout_work) &&
328 	    mod_delayed_work(system_percpu_wq, &deferred_probe_timeout_work,
329 			     secs_to_jiffies(driver_deferred_probe_timeout)))
330 		pr_debug("Extended deferred probe timeout by %d secs\n",
331 					driver_deferred_probe_timeout);
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 	if (dev->driver->p_cb.post_unbind_rust)
597 		dev->driver->p_cb.post_unbind_rust(dev);
598 	devres_release_all(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 with driver %s returned %d after %lld usecs\n",
795 	       dev_name(dev), drv->name, 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 /*
1034  * __device_driver_lock - acquire locks needed to manipulate dev->drv
1035  * @dev: Device we will update driver info for
1036  * @parent: Parent device. Needed if the device type requires parent lock
1037  *
1038  * This function will take the required locks for manipulating dev->drv.
1039  * Normally this will just be the @dev lock, but when called for a USB
1040  * interface, @parent lock will be held as well.
1041  */
1042 static void __device_driver_lock(struct device *dev, struct device *parent)
1043 {
1044 	if (dev->type && dev->type->need_parent_lock && parent)
1045 		device_lock(parent);
1046 	device_lock(dev);
1047 }
1048 
1049 /*
1050  * __device_driver_unlock - release locks needed to manipulate dev->drv
1051  * @dev: Device we will update driver info for
1052  * @parent: Parent device. Needed if the device type requires parent lock
1053  *
1054  * This function will release the required locks for manipulating dev->drv.
1055  * Normally this will just be the @dev lock, but when called for a
1056  * USB interface, @parent lock will be released as well.
1057  */
1058 static void __device_driver_unlock(struct device *dev, struct device *parent)
1059 {
1060 	device_unlock(dev);
1061 	if (dev->type && dev->type->need_parent_lock && parent)
1062 		device_unlock(parent);
1063 }
1064 
1065 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
1066 {
1067 	struct device *dev = _dev;
1068 	struct device_attach_data data = {
1069 		.dev		= dev,
1070 		.check_async	= true,
1071 		.want_async	= true,
1072 	};
1073 
1074 	__device_driver_lock(dev, dev->parent);
1075 
1076 	/*
1077 	 * Check if device has already been removed or claimed. This may
1078 	 * happen with driver loading, device discovery/registration,
1079 	 * and deferred probe processing happens all at once with
1080 	 * multiple threads.
1081 	 */
1082 	if (dev->p->dead || dev->driver)
1083 		goto out_unlock;
1084 
1085 	if (dev->parent)
1086 		pm_runtime_get_sync(dev->parent);
1087 
1088 	bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
1089 	dev_dbg(dev, "async probe completed\n");
1090 
1091 	pm_request_idle(dev);
1092 
1093 	if (dev->parent)
1094 		pm_runtime_put(dev->parent);
1095 out_unlock:
1096 	__device_driver_unlock(dev, dev->parent);
1097 
1098 	put_device(dev);
1099 }
1100 
1101 static int __device_attach(struct device *dev, bool allow_async)
1102 {
1103 	int ret = 0;
1104 	bool async = false;
1105 
1106 	device_lock(dev);
1107 	if (dev->p->dead) {
1108 		goto out_unlock;
1109 	} else if (dev->driver) {
1110 		if (device_is_bound(dev)) {
1111 			ret = 1;
1112 			goto out_unlock;
1113 		}
1114 		ret = device_bind_driver(dev);
1115 		if (ret == 0)
1116 			ret = 1;
1117 		else {
1118 			device_set_driver(dev, NULL);
1119 			ret = 0;
1120 		}
1121 	} else {
1122 		struct device_attach_data data = {
1123 			.dev = dev,
1124 			.check_async = allow_async,
1125 			.want_async = false,
1126 		};
1127 
1128 		if (dev->parent)
1129 			pm_runtime_get_sync(dev->parent);
1130 
1131 		ret = bus_for_each_drv(dev->bus, NULL, &data,
1132 					__device_attach_driver);
1133 		if (!ret && allow_async && data.have_async) {
1134 			/*
1135 			 * If we could not find appropriate driver
1136 			 * synchronously and we are allowed to do
1137 			 * async probes and there are drivers that
1138 			 * want to probe asynchronously, we'll
1139 			 * try them.
1140 			 */
1141 			dev_dbg(dev, "scheduling asynchronous probe\n");
1142 			get_device(dev);
1143 			async = true;
1144 		} else {
1145 			pm_request_idle(dev);
1146 		}
1147 
1148 		if (dev->parent)
1149 			pm_runtime_put(dev->parent);
1150 	}
1151 out_unlock:
1152 	device_unlock(dev);
1153 	if (async && !async_schedule_dev_nocall(__device_attach_async_helper, dev)) {
1154 		put_device(dev);
1155 		ret = 2;	/* retry synchronously */
1156 	}
1157 
1158 	return ret;
1159 }
1160 
1161 /**
1162  * device_attach - try to attach device to a driver.
1163  * @dev: device.
1164  *
1165  * Walk the list of drivers that the bus has and call
1166  * driver_probe_device() for each pair. If a compatible
1167  * pair is found, break out and return.
1168  *
1169  * Returns 1 if the device was bound to a driver;
1170  * 0 if no matching driver was found;
1171  * -ENODEV if the device is not registered.
1172  *
1173  * When called for a USB interface, @dev->parent lock must be held.
1174  */
1175 int device_attach(struct device *dev)
1176 {
1177 	return __device_attach(dev, false);
1178 }
1179 EXPORT_SYMBOL_GPL(device_attach);
1180 
1181 void device_initial_probe(struct device *dev)
1182 {
1183 	struct subsys_private *sp = bus_to_subsys(dev->bus);
1184 	int ret;
1185 
1186 	if (!sp)
1187 		return;
1188 
1189 	if (sp->drivers_autoprobe) {
1190 		ret = __device_attach(dev, true);
1191 		if (ret == 2)
1192 			__device_attach(dev, false);
1193 	}
1194 
1195 	subsys_put(sp);
1196 }
1197 
1198 /**
1199  * device_driver_attach - attach a specific driver to a specific device
1200  * @drv: Driver to attach
1201  * @dev: Device to attach it to
1202  *
1203  * Manually attach driver to a device. Will acquire both @dev lock and
1204  * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
1205  */
1206 int device_driver_attach(const struct device_driver *drv, struct device *dev)
1207 {
1208 	int ret;
1209 
1210 	__device_driver_lock(dev, dev->parent);
1211 	ret = __driver_probe_device(drv, dev);
1212 	__device_driver_unlock(dev, dev->parent);
1213 
1214 	/* also return probe errors as normal negative errnos */
1215 	if (ret > 0)
1216 		ret = -ret;
1217 	if (ret == -EPROBE_DEFER)
1218 		return -EAGAIN;
1219 	return ret;
1220 }
1221 EXPORT_SYMBOL_GPL(device_driver_attach);
1222 
1223 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
1224 {
1225 	struct device *dev = _dev;
1226 	const struct device_driver *drv;
1227 	int ret;
1228 
1229 	__device_driver_lock(dev, dev->parent);
1230 	drv = dev->p->async_driver;
1231 	dev->p->async_driver = NULL;
1232 	ret = driver_probe_device(drv, dev);
1233 	__device_driver_unlock(dev, dev->parent);
1234 
1235 	dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1236 
1237 	put_device(dev);
1238 }
1239 
1240 static int __driver_attach(struct device *dev, void *data)
1241 {
1242 	const struct device_driver *drv = data;
1243 	bool async = false;
1244 	int ret;
1245 
1246 	/*
1247 	 * Lock device and try to bind to it. We drop the error
1248 	 * here and always return 0, because we need to keep trying
1249 	 * to bind to devices and some drivers will return an error
1250 	 * simply if it didn't support the device.
1251 	 *
1252 	 * driver_probe_device() will spit a warning if there
1253 	 * is an error.
1254 	 */
1255 
1256 	ret = driver_match_device(drv, dev);
1257 	if (ret == 0) {
1258 		/* no match */
1259 		return 0;
1260 	} else if (ret == -EPROBE_DEFER) {
1261 		dev_dbg(dev, "Device match requests probe deferral\n");
1262 		dev_set_can_match(dev);
1263 		driver_deferred_probe_add(dev);
1264 		/*
1265 		 * Driver could not match with device, but may match with
1266 		 * another device on the bus.
1267 		 */
1268 		return 0;
1269 	} else if (ret < 0) {
1270 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1271 		/*
1272 		 * Driver could not match with device, but may match with
1273 		 * another device on the bus.
1274 		 */
1275 		return 0;
1276 	} /* ret > 0 means positive match */
1277 
1278 	if (driver_allows_async_probing(drv)) {
1279 		/*
1280 		 * Instead of probing the device synchronously we will
1281 		 * probe it asynchronously to allow for more parallelism.
1282 		 *
1283 		 * We only take the device lock here in order to guarantee
1284 		 * that the dev->driver and async_driver fields are protected
1285 		 */
1286 		dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1287 		device_lock(dev);
1288 		if (!dev->driver && !dev->p->async_driver) {
1289 			get_device(dev);
1290 			dev->p->async_driver = drv;
1291 			async = true;
1292 		}
1293 		device_unlock(dev);
1294 		if (async)
1295 			async_schedule_dev(__driver_attach_async_helper, dev);
1296 		return 0;
1297 	}
1298 
1299 	__device_driver_lock(dev, dev->parent);
1300 	driver_probe_device(drv, dev);
1301 	__device_driver_unlock(dev, dev->parent);
1302 
1303 	return 0;
1304 }
1305 
1306 /**
1307  * driver_attach - try to bind driver to devices.
1308  * @drv: driver.
1309  *
1310  * Walk the list of devices that the bus has on it and try to
1311  * match the driver with each one.  If driver_probe_device()
1312  * returns 0 and the @dev->driver is set, we've found a
1313  * compatible pair.
1314  */
1315 int driver_attach(const struct device_driver *drv)
1316 {
1317 	/* The (void *) will be put back to const * in __driver_attach() */
1318 	return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
1319 }
1320 EXPORT_SYMBOL_GPL(driver_attach);
1321 
1322 /*
1323  * __device_release_driver() must be called with @dev lock held.
1324  * When called for a USB interface, @dev->parent lock must be held as well.
1325  */
1326 static void __device_release_driver(struct device *dev, struct device *parent)
1327 {
1328 	struct device_driver *drv;
1329 
1330 	drv = dev->driver;
1331 	if (drv) {
1332 		pm_runtime_get_sync(dev);
1333 
1334 		while (device_links_busy(dev)) {
1335 			__device_driver_unlock(dev, parent);
1336 
1337 			device_links_unbind_consumers(dev);
1338 
1339 			__device_driver_lock(dev, parent);
1340 			/*
1341 			 * A concurrent invocation of the same function might
1342 			 * have released the driver successfully while this one
1343 			 * was waiting, so check for that.
1344 			 */
1345 			if (dev->driver != drv) {
1346 				pm_runtime_put(dev);
1347 				return;
1348 			}
1349 		}
1350 
1351 		driver_sysfs_remove(dev);
1352 
1353 		bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER);
1354 
1355 		pm_runtime_put_sync(dev);
1356 
1357 		device_remove(dev);
1358 
1359 		if (dev->bus && dev->bus->dma_cleanup)
1360 			dev->bus->dma_cleanup(dev);
1361 
1362 		device_unbind_cleanup(dev);
1363 		device_links_driver_cleanup(dev);
1364 
1365 		klist_remove(&dev->p->knode_driver);
1366 		device_pm_check_callbacks(dev);
1367 
1368 		bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER);
1369 		kobject_uevent(&dev->kobj, KOBJ_UNBIND);
1370 	}
1371 }
1372 
1373 void device_release_driver_internal(struct device *dev,
1374 				    const struct device_driver *drv,
1375 				    struct device *parent)
1376 {
1377 	__device_driver_lock(dev, parent);
1378 
1379 	if (!drv || drv == dev->driver)
1380 		__device_release_driver(dev, parent);
1381 
1382 	__device_driver_unlock(dev, parent);
1383 }
1384 
1385 /**
1386  * device_release_driver - manually detach device from driver.
1387  * @dev: device.
1388  *
1389  * Manually detach device from driver.
1390  * When called for a USB interface, @dev->parent lock must be held.
1391  *
1392  * If this function is to be called with @dev->parent lock held, ensure that
1393  * the device's consumers are unbound in advance or that their locks can be
1394  * acquired under the @dev->parent lock.
1395  */
1396 void device_release_driver(struct device *dev)
1397 {
1398 	/*
1399 	 * If anyone calls device_release_driver() recursively from
1400 	 * within their ->remove callback for the same device, they
1401 	 * will deadlock right here.
1402 	 */
1403 	device_release_driver_internal(dev, NULL, NULL);
1404 }
1405 EXPORT_SYMBOL_GPL(device_release_driver);
1406 
1407 /**
1408  * device_driver_detach - detach driver from a specific device
1409  * @dev: device to detach driver from
1410  *
1411  * Detach driver from device. Will acquire both @dev lock and @dev->parent
1412  * lock if needed.
1413  */
1414 void device_driver_detach(struct device *dev)
1415 {
1416 	device_release_driver_internal(dev, NULL, dev->parent);
1417 }
1418 
1419 /**
1420  * driver_detach - detach driver from all devices it controls.
1421  * @drv: driver.
1422  */
1423 void driver_detach(const struct device_driver *drv)
1424 {
1425 	struct device_private *dev_prv;
1426 	struct device *dev;
1427 
1428 	if (driver_allows_async_probing(drv))
1429 		async_synchronize_full();
1430 
1431 	for (;;) {
1432 		spin_lock(&drv->p->klist_devices.k_lock);
1433 		if (list_empty(&drv->p->klist_devices.k_list)) {
1434 			spin_unlock(&drv->p->klist_devices.k_lock);
1435 			break;
1436 		}
1437 		dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
1438 				     struct device_private,
1439 				     knode_driver.n_node);
1440 		dev = dev_prv->device;
1441 		get_device(dev);
1442 		spin_unlock(&drv->p->klist_devices.k_lock);
1443 		device_release_driver_internal(dev, drv, dev->parent);
1444 		put_device(dev);
1445 	}
1446 }
1447