xref: /linux/drivers/base/dd.c (revision 4793dae01f47754e288cdbb3a22581cac2317f2b)
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 
__device_set_deferred_probe_reason(const struct device * dev,char * reason)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  */
deferred_probe_work_func(struct work_struct * work)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 
driver_deferred_probe_add(struct device * dev)133 void driver_deferred_probe_add(struct device *dev)
134 {
135 	if (!dev->can_match)
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 
driver_deferred_probe_del(struct device * dev)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  */
driver_deferred_probe_trigger(void)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  */
device_block_probing(void)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  */
device_unblock_probing(void)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  */
device_set_deferred_probe_reason(const struct device * dev,struct va_format * vaf)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  */
deferred_devs_show(struct seq_file * s,void * data)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 
deferred_probe_timeout_setup(char * str)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  */
driver_deferred_probe_check_state(struct device * dev)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 
deferred_probe_timeout_work_func(struct work_struct * work)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 
deferred_probe_extend_timeout(void)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  */
deferred_probe_initcall(void)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 
deferred_probe_exit(void)374 static void __exit deferred_probe_exit(void)
375 {
376 	debugfs_lookup_and_remove("devices_deferred", NULL);
377 }
378 __exitcall(deferred_probe_exit);
379 
__device_set_driver_override(struct device * dev,const char * s,size_t len)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  */
device_is_bound(struct device * dev)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 
driver_bound(struct device * dev)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 
coredump_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 
driver_sysfs_add(struct device * dev)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 
driver_sysfs_remove(struct device * dev)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  */
device_bind_driver(struct device * dev)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 
state_synced_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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->state_synced) {
573 		dev->state_synced = true;
574 		dev_sync_state(dev);
575 	} else {
576 		ret = -EINVAL;
577 	}
578 	device_unlock(dev);
579 
580 	return ret ? ret : count;
581 }
582 
state_synced_show(struct device * dev,struct device_attribute * attr,char * buf)583 static ssize_t state_synced_show(struct device *dev,
584 				 struct device_attribute *attr, char *buf)
585 {
586 	bool val;
587 
588 	device_lock(dev);
589 	val = dev->state_synced;
590 	device_unlock(dev);
591 
592 	return sysfs_emit(buf, "%u\n", val);
593 }
594 static DEVICE_ATTR_RW(state_synced);
595 
device_unbind_cleanup(struct device * dev)596 static void device_unbind_cleanup(struct device *dev)
597 {
598 	devres_release_all(dev);
599 	if (dev->driver->p_cb.post_unbind_rust)
600 		dev->driver->p_cb.post_unbind_rust(dev);
601 	arch_teardown_dma_ops(dev);
602 	kfree(dev->dma_range_map);
603 	dev->dma_range_map = NULL;
604 	device_set_driver(dev, NULL);
605 	dev_set_drvdata(dev, NULL);
606 	dev_pm_domain_detach(dev, dev->power.detach_power_off);
607 	if (dev->pm_domain && dev->pm_domain->dismiss)
608 		dev->pm_domain->dismiss(dev);
609 	pm_runtime_reinit(dev);
610 	dev_pm_set_driver_flags(dev, 0);
611 }
612 
device_remove(struct device * dev)613 static void device_remove(struct device *dev)
614 {
615 	device_remove_file(dev, &dev_attr_state_synced);
616 	device_remove_groups(dev, dev->driver->dev_groups);
617 
618 	if (dev->bus && dev->bus->remove)
619 		dev->bus->remove(dev);
620 	else if (dev->driver->remove)
621 		dev->driver->remove(dev);
622 }
623 
call_driver_probe(struct device * dev,const struct device_driver * drv)624 static int call_driver_probe(struct device *dev, const struct device_driver *drv)
625 {
626 	int ret = 0;
627 
628 	if (dev->bus->probe)
629 		ret = dev->bus->probe(dev);
630 	else if (drv->probe)
631 		ret = drv->probe(dev);
632 
633 	switch (ret) {
634 	case 0:
635 		break;
636 	case -EPROBE_DEFER:
637 		/* Driver requested deferred probing */
638 		dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
639 		break;
640 	case -ENODEV:
641 	case -ENXIO:
642 		dev_dbg(dev, "probe with driver %s rejects match %d\n",
643 			drv->name, ret);
644 		break;
645 	default:
646 		/* driver matched but the probe failed */
647 		dev_err(dev, "probe with driver %s failed with error %d\n",
648 			drv->name, ret);
649 		break;
650 	}
651 
652 	return ret;
653 }
654 
really_probe(struct device * dev,const struct device_driver * drv)655 static int really_probe(struct device *dev, const struct device_driver *drv)
656 {
657 	bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
658 			   !drv->suppress_bind_attrs;
659 	int ret, link_ret;
660 
661 	if (defer_all_probes) {
662 		/*
663 		 * Value of defer_all_probes can be set only by
664 		 * device_block_probing() which, in turn, will call
665 		 * wait_for_device_probe() right after that to avoid any races.
666 		 */
667 		dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
668 		return -EPROBE_DEFER;
669 	}
670 
671 	link_ret = device_links_check_suppliers(dev);
672 	if (link_ret == -EPROBE_DEFER)
673 		return link_ret;
674 
675 	dev_dbg(dev, "bus: '%s': %s: probing driver %s with device\n",
676 		drv->bus->name, __func__, drv->name);
677 	if (!list_empty(&dev->devres_head)) {
678 		dev_crit(dev, "Resources present before probing\n");
679 		ret = -EBUSY;
680 		goto done;
681 	}
682 
683 re_probe:
684 	device_set_driver(dev, drv);
685 
686 	/* If using pinctrl, bind pins now before probing */
687 	ret = pinctrl_bind_pins(dev);
688 	if (ret)
689 		goto pinctrl_bind_failed;
690 
691 	if (dev->bus->dma_configure) {
692 		ret = dev->bus->dma_configure(dev);
693 		if (ret)
694 			goto pinctrl_bind_failed;
695 	}
696 
697 	ret = driver_sysfs_add(dev);
698 	if (ret) {
699 		dev_err(dev, "%s: driver_sysfs_add failed\n", __func__);
700 		goto sysfs_failed;
701 	}
702 
703 	if (dev->pm_domain && dev->pm_domain->activate) {
704 		ret = dev->pm_domain->activate(dev);
705 		if (ret)
706 			goto probe_failed;
707 	}
708 
709 	ret = call_driver_probe(dev, drv);
710 	if (ret) {
711 		/*
712 		 * If fw_devlink_best_effort is active (denoted by -EAGAIN), the
713 		 * device might actually probe properly once some of its missing
714 		 * suppliers have probed. So, treat this as if the driver
715 		 * returned -EPROBE_DEFER.
716 		 */
717 		if (link_ret == -EAGAIN)
718 			ret = -EPROBE_DEFER;
719 
720 		/*
721 		 * Return probe errors as positive values so that the callers
722 		 * can distinguish them from other errors.
723 		 */
724 		ret = -ret;
725 		goto probe_failed;
726 	}
727 
728 	ret = device_add_groups(dev, drv->dev_groups);
729 	if (ret) {
730 		dev_err(dev, "device_add_groups() failed\n");
731 		goto dev_groups_failed;
732 	}
733 
734 	if (dev_has_sync_state(dev)) {
735 		ret = device_create_file(dev, &dev_attr_state_synced);
736 		if (ret) {
737 			dev_err(dev, "state_synced sysfs add failed\n");
738 			goto dev_sysfs_state_synced_failed;
739 		}
740 	}
741 
742 	if (test_remove) {
743 		test_remove = false;
744 
745 		device_remove(dev);
746 		driver_sysfs_remove(dev);
747 		if (dev->bus && dev->bus->dma_cleanup)
748 			dev->bus->dma_cleanup(dev);
749 		device_unbind_cleanup(dev);
750 
751 		goto re_probe;
752 	}
753 
754 	pinctrl_init_done(dev);
755 
756 	if (dev->pm_domain && dev->pm_domain->sync)
757 		dev->pm_domain->sync(dev);
758 
759 	driver_bound(dev);
760 	dev_dbg(dev, "bus: '%s': %s: bound device to driver %s\n",
761 		drv->bus->name, __func__, drv->name);
762 	goto done;
763 
764 dev_sysfs_state_synced_failed:
765 dev_groups_failed:
766 	device_remove(dev);
767 probe_failed:
768 	driver_sysfs_remove(dev);
769 sysfs_failed:
770 	bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
771 	if (dev->bus && dev->bus->dma_cleanup)
772 		dev->bus->dma_cleanup(dev);
773 pinctrl_bind_failed:
774 	device_links_no_driver(dev);
775 	device_unbind_cleanup(dev);
776 done:
777 	return ret;
778 }
779 
780 /*
781  * For initcall_debug, show the driver probe time.
782  */
really_probe_debug(struct device * dev,const struct device_driver * drv)783 static int really_probe_debug(struct device *dev, const struct device_driver *drv)
784 {
785 	ktime_t calltime, rettime;
786 	int ret;
787 
788 	calltime = ktime_get();
789 	ret = really_probe(dev, drv);
790 	rettime = ktime_get();
791 	/*
792 	 * Don't change this to pr_debug() because that requires
793 	 * CONFIG_DYNAMIC_DEBUG and we want a simple 'initcall_debug' on the
794 	 * kernel commandline to print this all the time at the debug level.
795 	 */
796 	printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
797 		 dev_name(dev), ret, ktime_us_delta(rettime, calltime));
798 	return ret;
799 }
800 
801 /**
802  * driver_probe_done
803  * Determine if the probe sequence is finished or not.
804  *
805  * Should somehow figure out how to use a semaphore, not an atomic variable...
806  */
driver_probe_done(void)807 bool __init driver_probe_done(void)
808 {
809 	int local_probe_count = atomic_read(&probe_count);
810 
811 	pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
812 	return !local_probe_count;
813 }
814 
815 /**
816  * wait_for_device_probe
817  * Wait for device probing to be completed.
818  */
wait_for_device_probe(void)819 void wait_for_device_probe(void)
820 {
821 	/* wait for the deferred probe workqueue to finish */
822 	flush_work(&deferred_probe_work);
823 
824 	/* wait for the known devices to complete their probing */
825 	wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
826 	async_synchronize_full();
827 }
828 EXPORT_SYMBOL_GPL(wait_for_device_probe);
829 
__driver_probe_device(const struct device_driver * drv,struct device * dev)830 static int __driver_probe_device(const struct device_driver *drv, struct device *dev)
831 {
832 	int ret = 0;
833 
834 	if (dev->p->dead || !device_is_registered(dev))
835 		return -ENODEV;
836 	if (dev->driver)
837 		return -EBUSY;
838 
839 	dev->can_match = true;
840 	dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
841 		drv->bus->name, __func__, drv->name);
842 
843 	pm_runtime_get_suppliers(dev);
844 	if (dev->parent)
845 		pm_runtime_get_sync(dev->parent);
846 
847 	pm_runtime_barrier(dev);
848 	if (initcall_debug)
849 		ret = really_probe_debug(dev, drv);
850 	else
851 		ret = really_probe(dev, drv);
852 	pm_request_idle(dev);
853 
854 	if (dev->parent)
855 		pm_runtime_put(dev->parent);
856 
857 	pm_runtime_put_suppliers(dev);
858 	return ret;
859 }
860 
861 /**
862  * driver_probe_device - attempt to bind device & driver together
863  * @drv: driver to bind a device to
864  * @dev: device to try to bind to the driver
865  *
866  * This function returns -ENODEV if the device is not registered, -EBUSY if it
867  * already has a driver, 0 if the device is bound successfully and a positive
868  * (inverted) error code for failures from the ->probe method.
869  *
870  * This function must be called with @dev lock held.  When called for a
871  * USB interface, @dev->parent lock must be held as well.
872  *
873  * If the device has a parent, runtime-resume the parent before driver probing.
874  */
driver_probe_device(const struct device_driver * drv,struct device * dev)875 static int driver_probe_device(const struct device_driver *drv, struct device *dev)
876 {
877 	int trigger_count = atomic_read(&deferred_trigger_count);
878 	int ret;
879 
880 	atomic_inc(&probe_count);
881 	ret = __driver_probe_device(drv, dev);
882 	if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
883 		driver_deferred_probe_add(dev);
884 
885 		/*
886 		 * Did a trigger occur while probing? Need to re-trigger if yes
887 		 */
888 		if (trigger_count != atomic_read(&deferred_trigger_count) &&
889 		    !defer_all_probes)
890 			driver_deferred_probe_trigger();
891 	}
892 	atomic_dec(&probe_count);
893 	wake_up_all(&probe_waitqueue);
894 	return ret;
895 }
896 
cmdline_requested_async_probing(const char * drv_name)897 static inline bool cmdline_requested_async_probing(const char *drv_name)
898 {
899 	bool async_drv;
900 
901 	async_drv = parse_option_str(async_probe_drv_names, drv_name);
902 
903 	return (async_probe_default != async_drv);
904 }
905 
906 /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
save_async_options(char * buf)907 static int __init save_async_options(char *buf)
908 {
909 	if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
910 		pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
911 
912 	strscpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
913 	async_probe_default = parse_option_str(async_probe_drv_names, "*");
914 
915 	return 1;
916 }
917 __setup("driver_async_probe=", save_async_options);
918 
driver_allows_async_probing(const struct device_driver * drv)919 static bool driver_allows_async_probing(const struct device_driver *drv)
920 {
921 	switch (drv->probe_type) {
922 	case PROBE_PREFER_ASYNCHRONOUS:
923 		return true;
924 
925 	case PROBE_FORCE_SYNCHRONOUS:
926 		return false;
927 
928 	default:
929 		if (cmdline_requested_async_probing(drv->name))
930 			return true;
931 
932 		if (module_requested_async_probing(drv->owner))
933 			return true;
934 
935 		return false;
936 	}
937 }
938 
939 struct device_attach_data {
940 	struct device *dev;
941 
942 	/*
943 	 * Indicates whether we are considering asynchronous probing or
944 	 * not. Only initial binding after device or driver registration
945 	 * (including deferral processing) may be done asynchronously, the
946 	 * rest is always synchronous, as we expect it is being done by
947 	 * request from userspace.
948 	 */
949 	bool check_async;
950 
951 	/*
952 	 * Indicates if we are binding synchronous or asynchronous drivers.
953 	 * When asynchronous probing is enabled we'll execute 2 passes
954 	 * over drivers: first pass doing synchronous probing and second
955 	 * doing asynchronous probing (if synchronous did not succeed -
956 	 * most likely because there was no driver requiring synchronous
957 	 * probing - and we found asynchronous driver during first pass).
958 	 * The 2 passes are done because we can't shoot asynchronous
959 	 * probe for given device and driver from bus_for_each_drv() since
960 	 * driver pointer is not guaranteed to stay valid once
961 	 * bus_for_each_drv() iterates to the next driver on the bus.
962 	 */
963 	bool want_async;
964 
965 	/*
966 	 * We'll set have_async to 'true' if, while scanning for matching
967 	 * driver, we'll encounter one that requests asynchronous probing.
968 	 */
969 	bool have_async;
970 };
971 
__device_attach_driver(struct device_driver * drv,void * _data)972 static int __device_attach_driver(struct device_driver *drv, void *_data)
973 {
974 	struct device_attach_data *data = _data;
975 	struct device *dev = data->dev;
976 	bool async_allowed;
977 	int ret;
978 
979 	ret = driver_match_device(drv, dev);
980 	if (ret == 0) {
981 		/* no match */
982 		return 0;
983 	} else if (ret == -EPROBE_DEFER) {
984 		dev_dbg(dev, "Device match requests probe deferral\n");
985 		dev->can_match = true;
986 		driver_deferred_probe_add(dev);
987 		/*
988 		 * Device can't match with a driver right now, so don't attempt
989 		 * to match or bind with other drivers on the bus.
990 		 */
991 		return ret;
992 	} else if (ret < 0) {
993 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
994 		return ret;
995 	} /* ret > 0 means positive match */
996 
997 	async_allowed = driver_allows_async_probing(drv);
998 
999 	if (async_allowed)
1000 		data->have_async = true;
1001 
1002 	if (data->check_async && async_allowed != data->want_async)
1003 		return 0;
1004 
1005 	/*
1006 	 * Ignore errors returned by ->probe so that the next driver can try
1007 	 * its luck.
1008 	 */
1009 	ret = driver_probe_device(drv, dev);
1010 	if (ret < 0)
1011 		return ret;
1012 	return ret == 0;
1013 }
1014 
__device_attach_async_helper(void * _dev,async_cookie_t cookie)1015 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
1016 {
1017 	struct device *dev = _dev;
1018 	struct device_attach_data data = {
1019 		.dev		= dev,
1020 		.check_async	= true,
1021 		.want_async	= true,
1022 	};
1023 
1024 	device_lock(dev);
1025 
1026 	/*
1027 	 * Check if device has already been removed or claimed. This may
1028 	 * happen with driver loading, device discovery/registration,
1029 	 * and deferred probe processing happens all at once with
1030 	 * multiple threads.
1031 	 */
1032 	if (dev->p->dead || dev->driver)
1033 		goto out_unlock;
1034 
1035 	if (dev->parent)
1036 		pm_runtime_get_sync(dev->parent);
1037 
1038 	bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
1039 	dev_dbg(dev, "async probe completed\n");
1040 
1041 	pm_request_idle(dev);
1042 
1043 	if (dev->parent)
1044 		pm_runtime_put(dev->parent);
1045 out_unlock:
1046 	device_unlock(dev);
1047 
1048 	put_device(dev);
1049 }
1050 
__device_attach(struct device * dev,bool allow_async)1051 static int __device_attach(struct device *dev, bool allow_async)
1052 {
1053 	int ret = 0;
1054 	bool async = false;
1055 
1056 	device_lock(dev);
1057 	if (dev->p->dead) {
1058 		goto out_unlock;
1059 	} else if (dev->driver) {
1060 		if (device_is_bound(dev)) {
1061 			ret = 1;
1062 			goto out_unlock;
1063 		}
1064 		ret = device_bind_driver(dev);
1065 		if (ret == 0)
1066 			ret = 1;
1067 		else {
1068 			device_set_driver(dev, NULL);
1069 			ret = 0;
1070 		}
1071 	} else {
1072 		struct device_attach_data data = {
1073 			.dev = dev,
1074 			.check_async = allow_async,
1075 			.want_async = false,
1076 		};
1077 
1078 		if (dev->parent)
1079 			pm_runtime_get_sync(dev->parent);
1080 
1081 		ret = bus_for_each_drv(dev->bus, NULL, &data,
1082 					__device_attach_driver);
1083 		if (!ret && allow_async && data.have_async) {
1084 			/*
1085 			 * If we could not find appropriate driver
1086 			 * synchronously and we are allowed to do
1087 			 * async probes and there are drivers that
1088 			 * want to probe asynchronously, we'll
1089 			 * try them.
1090 			 */
1091 			dev_dbg(dev, "scheduling asynchronous probe\n");
1092 			get_device(dev);
1093 			async = true;
1094 		} else {
1095 			pm_request_idle(dev);
1096 		}
1097 
1098 		if (dev->parent)
1099 			pm_runtime_put(dev->parent);
1100 	}
1101 out_unlock:
1102 	device_unlock(dev);
1103 	if (async)
1104 		async_schedule_dev(__device_attach_async_helper, dev);
1105 	return ret;
1106 }
1107 
1108 /**
1109  * device_attach - try to attach device to a driver.
1110  * @dev: device.
1111  *
1112  * Walk the list of drivers that the bus has and call
1113  * driver_probe_device() for each pair. If a compatible
1114  * pair is found, break out and return.
1115  *
1116  * Returns 1 if the device was bound to a driver;
1117  * 0 if no matching driver was found;
1118  * -ENODEV if the device is not registered.
1119  *
1120  * When called for a USB interface, @dev->parent lock must be held.
1121  */
device_attach(struct device * dev)1122 int device_attach(struct device *dev)
1123 {
1124 	return __device_attach(dev, false);
1125 }
1126 EXPORT_SYMBOL_GPL(device_attach);
1127 
device_initial_probe(struct device * dev)1128 void device_initial_probe(struct device *dev)
1129 {
1130 	struct subsys_private *sp = bus_to_subsys(dev->bus);
1131 
1132 	if (!sp)
1133 		return;
1134 
1135 	if (sp->drivers_autoprobe)
1136 		__device_attach(dev, true);
1137 
1138 	subsys_put(sp);
1139 }
1140 
1141 /*
1142  * __device_driver_lock - acquire locks needed to manipulate dev->drv
1143  * @dev: Device we will update driver info for
1144  * @parent: Parent device. Needed if the bus requires parent lock
1145  *
1146  * This function will take the required locks for manipulating dev->drv.
1147  * Normally this will just be the @dev lock, but when called for a USB
1148  * interface, @parent lock will be held as well.
1149  */
__device_driver_lock(struct device * dev,struct device * parent)1150 static void __device_driver_lock(struct device *dev, struct device *parent)
1151 {
1152 	if (parent && dev->bus->need_parent_lock)
1153 		device_lock(parent);
1154 	device_lock(dev);
1155 }
1156 
1157 /*
1158  * __device_driver_unlock - release locks needed to manipulate dev->drv
1159  * @dev: Device we will update driver info for
1160  * @parent: Parent device. Needed if the bus requires parent lock
1161  *
1162  * This function will release the required locks for manipulating dev->drv.
1163  * Normally this will just be the @dev lock, but when called for a
1164  * USB interface, @parent lock will be released as well.
1165  */
__device_driver_unlock(struct device * dev,struct device * parent)1166 static void __device_driver_unlock(struct device *dev, struct device *parent)
1167 {
1168 	device_unlock(dev);
1169 	if (parent && dev->bus->need_parent_lock)
1170 		device_unlock(parent);
1171 }
1172 
1173 /**
1174  * device_driver_attach - attach a specific driver to a specific device
1175  * @drv: Driver to attach
1176  * @dev: Device to attach it to
1177  *
1178  * Manually attach driver to a device. Will acquire both @dev lock and
1179  * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
1180  */
device_driver_attach(const struct device_driver * drv,struct device * dev)1181 int device_driver_attach(const struct device_driver *drv, struct device *dev)
1182 {
1183 	int ret;
1184 
1185 	__device_driver_lock(dev, dev->parent);
1186 	ret = __driver_probe_device(drv, dev);
1187 	__device_driver_unlock(dev, dev->parent);
1188 
1189 	/* also return probe errors as normal negative errnos */
1190 	if (ret > 0)
1191 		ret = -ret;
1192 	if (ret == -EPROBE_DEFER)
1193 		return -EAGAIN;
1194 	return ret;
1195 }
1196 EXPORT_SYMBOL_GPL(device_driver_attach);
1197 
__driver_attach_async_helper(void * _dev,async_cookie_t cookie)1198 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
1199 {
1200 	struct device *dev = _dev;
1201 	const struct device_driver *drv;
1202 	int ret;
1203 
1204 	__device_driver_lock(dev, dev->parent);
1205 	drv = dev->p->async_driver;
1206 	dev->p->async_driver = NULL;
1207 	ret = driver_probe_device(drv, dev);
1208 	__device_driver_unlock(dev, dev->parent);
1209 
1210 	dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1211 
1212 	put_device(dev);
1213 }
1214 
__driver_attach(struct device * dev,void * data)1215 static int __driver_attach(struct device *dev, void *data)
1216 {
1217 	const struct device_driver *drv = data;
1218 	bool async = false;
1219 	int ret;
1220 
1221 	/*
1222 	 * Lock device and try to bind to it. We drop the error
1223 	 * here and always return 0, because we need to keep trying
1224 	 * to bind to devices and some drivers will return an error
1225 	 * simply if it didn't support the device.
1226 	 *
1227 	 * driver_probe_device() will spit a warning if there
1228 	 * is an error.
1229 	 */
1230 
1231 	ret = driver_match_device(drv, dev);
1232 	if (ret == 0) {
1233 		/* no match */
1234 		return 0;
1235 	} else if (ret == -EPROBE_DEFER) {
1236 		dev_dbg(dev, "Device match requests probe deferral\n");
1237 		dev->can_match = true;
1238 		driver_deferred_probe_add(dev);
1239 		/*
1240 		 * Driver could not match with device, but may match with
1241 		 * another device on the bus.
1242 		 */
1243 		return 0;
1244 	} else if (ret < 0) {
1245 		dev_dbg(dev, "Bus failed to match device: %d\n", ret);
1246 		/*
1247 		 * Driver could not match with device, but may match with
1248 		 * another device on the bus.
1249 		 */
1250 		return 0;
1251 	} /* ret > 0 means positive match */
1252 
1253 	if (driver_allows_async_probing(drv)) {
1254 		/*
1255 		 * Instead of probing the device synchronously we will
1256 		 * probe it asynchronously to allow for more parallelism.
1257 		 *
1258 		 * We only take the device lock here in order to guarantee
1259 		 * that the dev->driver and async_driver fields are protected
1260 		 */
1261 		dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1262 		device_lock(dev);
1263 		if (!dev->driver && !dev->p->async_driver) {
1264 			get_device(dev);
1265 			dev->p->async_driver = drv;
1266 			async = true;
1267 		}
1268 		device_unlock(dev);
1269 		if (async)
1270 			async_schedule_dev(__driver_attach_async_helper, dev);
1271 		return 0;
1272 	}
1273 
1274 	__device_driver_lock(dev, dev->parent);
1275 	driver_probe_device(drv, dev);
1276 	__device_driver_unlock(dev, dev->parent);
1277 
1278 	return 0;
1279 }
1280 
1281 /**
1282  * driver_attach - try to bind driver to devices.
1283  * @drv: driver.
1284  *
1285  * Walk the list of devices that the bus has on it and try to
1286  * match the driver with each one.  If driver_probe_device()
1287  * returns 0 and the @dev->driver is set, we've found a
1288  * compatible pair.
1289  */
driver_attach(const struct device_driver * drv)1290 int driver_attach(const struct device_driver *drv)
1291 {
1292 	/* The (void *) will be put back to const * in __driver_attach() */
1293 	return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
1294 }
1295 EXPORT_SYMBOL_GPL(driver_attach);
1296 
1297 /*
1298  * __device_release_driver() must be called with @dev lock held.
1299  * When called for a USB interface, @dev->parent lock must be held as well.
1300  */
__device_release_driver(struct device * dev,struct device * parent)1301 static void __device_release_driver(struct device *dev, struct device *parent)
1302 {
1303 	struct device_driver *drv;
1304 
1305 	drv = dev->driver;
1306 	if (drv) {
1307 		pm_runtime_get_sync(dev);
1308 
1309 		while (device_links_busy(dev)) {
1310 			__device_driver_unlock(dev, parent);
1311 
1312 			device_links_unbind_consumers(dev);
1313 
1314 			__device_driver_lock(dev, parent);
1315 			/*
1316 			 * A concurrent invocation of the same function might
1317 			 * have released the driver successfully while this one
1318 			 * was waiting, so check for that.
1319 			 */
1320 			if (dev->driver != drv) {
1321 				pm_runtime_put(dev);
1322 				return;
1323 			}
1324 		}
1325 
1326 		driver_sysfs_remove(dev);
1327 
1328 		bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER);
1329 
1330 		pm_runtime_put_sync(dev);
1331 
1332 		device_remove(dev);
1333 
1334 		if (dev->bus && dev->bus->dma_cleanup)
1335 			dev->bus->dma_cleanup(dev);
1336 
1337 		device_unbind_cleanup(dev);
1338 		device_links_driver_cleanup(dev);
1339 
1340 		klist_remove(&dev->p->knode_driver);
1341 		device_pm_check_callbacks(dev);
1342 
1343 		bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER);
1344 		kobject_uevent(&dev->kobj, KOBJ_UNBIND);
1345 	}
1346 }
1347 
device_release_driver_internal(struct device * dev,const struct device_driver * drv,struct device * parent)1348 void device_release_driver_internal(struct device *dev,
1349 				    const struct device_driver *drv,
1350 				    struct device *parent)
1351 {
1352 	__device_driver_lock(dev, parent);
1353 
1354 	if (!drv || drv == dev->driver)
1355 		__device_release_driver(dev, parent);
1356 
1357 	__device_driver_unlock(dev, parent);
1358 }
1359 
1360 /**
1361  * device_release_driver - manually detach device from driver.
1362  * @dev: device.
1363  *
1364  * Manually detach device from driver.
1365  * When called for a USB interface, @dev->parent lock must be held.
1366  *
1367  * If this function is to be called with @dev->parent lock held, ensure that
1368  * the device's consumers are unbound in advance or that their locks can be
1369  * acquired under the @dev->parent lock.
1370  */
device_release_driver(struct device * dev)1371 void device_release_driver(struct device *dev)
1372 {
1373 	/*
1374 	 * If anyone calls device_release_driver() recursively from
1375 	 * within their ->remove callback for the same device, they
1376 	 * will deadlock right here.
1377 	 */
1378 	device_release_driver_internal(dev, NULL, NULL);
1379 }
1380 EXPORT_SYMBOL_GPL(device_release_driver);
1381 
1382 /**
1383  * device_driver_detach - detach driver from a specific device
1384  * @dev: device to detach driver from
1385  *
1386  * Detach driver from device. Will acquire both @dev lock and @dev->parent
1387  * lock if needed.
1388  */
device_driver_detach(struct device * dev)1389 void device_driver_detach(struct device *dev)
1390 {
1391 	device_release_driver_internal(dev, NULL, dev->parent);
1392 }
1393 
1394 /**
1395  * driver_detach - detach driver from all devices it controls.
1396  * @drv: driver.
1397  */
driver_detach(const struct device_driver * drv)1398 void driver_detach(const struct device_driver *drv)
1399 {
1400 	struct device_private *dev_prv;
1401 	struct device *dev;
1402 
1403 	if (driver_allows_async_probing(drv))
1404 		async_synchronize_full();
1405 
1406 	for (;;) {
1407 		spin_lock(&drv->p->klist_devices.k_lock);
1408 		if (list_empty(&drv->p->klist_devices.k_list)) {
1409 			spin_unlock(&drv->p->klist_devices.k_lock);
1410 			break;
1411 		}
1412 		dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
1413 				     struct device_private,
1414 				     knode_driver.n_node);
1415 		dev = dev_prv->device;
1416 		get_device(dev);
1417 		spin_unlock(&drv->p->klist_devices.k_lock);
1418 		device_release_driver_internal(dev, drv, dev->parent);
1419 		put_device(dev);
1420 	}
1421 }
1422