blob: 093af41ec710e88a427b09727962590bbb61c309 [file] [log] [blame]
xjb04a4022021-11-25 15:01:52 +08001// 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-mapping.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_runtime.h>
29#include <linux/pinctrl/devinfo.h>
30#include <linux/bootprof.h>
31
32#include "base.h"
33#include "power/power.h"
34
35/*
36 * Deferred Probe infrastructure.
37 *
38 * Sometimes driver probe order matters, but the kernel doesn't always have
39 * dependency information which means some drivers will get probed before a
40 * resource it depends on is available. For example, an SDHCI driver may
41 * first need a GPIO line from an i2c GPIO controller before it can be
42 * initialized. If a required resource is not available yet, a driver can
43 * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
44 *
45 * Deferred probe maintains two lists of devices, a pending list and an active
46 * list. A driver returning -EPROBE_DEFER causes the device to be added to the
47 * pending list. A successful driver probe will trigger moving all devices
48 * from the pending to the active list so that the workqueue will eventually
49 * retry them.
50 *
51 * The deferred_probe_mutex must be held any time the deferred_probe_*_list
52 * of the (struct device*)->p->deferred_probe pointers are manipulated
53 */
54static DEFINE_MUTEX(deferred_probe_mutex);
55static LIST_HEAD(deferred_probe_pending_list);
56static LIST_HEAD(deferred_probe_active_list);
57static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
58static struct dentry *deferred_devices;
59static bool initcalls_done;
60
61/*
62 * In some cases, like suspend to RAM or hibernation, It might be reasonable
63 * to prohibit probing of devices as it could be unsafe.
64 * Once defer_all_probes is true all drivers probes will be forcibly deferred.
65 */
66static bool defer_all_probes;
67
68/*
69 * deferred_probe_work_func() - Retry probing devices in the active list.
70 */
71static void deferred_probe_work_func(struct work_struct *work)
72{
73 struct device *dev;
74 struct device_private *private;
75 /*
76 * This block processes every device in the deferred 'active' list.
77 * Each device is removed from the active list and passed to
78 * bus_probe_device() to re-attempt the probe. The loop continues
79 * until every device in the active list is removed and retried.
80 *
81 * Note: Once the device is removed from the list and the mutex is
82 * released, it is possible for the device get freed by another thread
83 * and cause a illegal pointer dereference. This code uses
84 * get/put_device() to ensure the device structure cannot disappear
85 * from under our feet.
86 */
87 mutex_lock(&deferred_probe_mutex);
88 while (!list_empty(&deferred_probe_active_list)) {
89 private = list_first_entry(&deferred_probe_active_list,
90 typeof(*dev->p), deferred_probe);
91 dev = private->device;
92 list_del_init(&private->deferred_probe);
93
94 get_device(dev);
95
96 /*
97 * Drop the mutex while probing each device; the probe path may
98 * manipulate the deferred list
99 */
100 mutex_unlock(&deferred_probe_mutex);
101
102 /*
103 * Force the device to the end of the dpm_list since
104 * the PM code assumes that the order we add things to
105 * the list is a good order for suspend but deferred
106 * probe makes that very unsafe.
107 */
108 device_pm_move_to_tail(dev);
109
110 dev_dbg(dev, "Retrying from deferred list\n");
111 bus_probe_device(dev);
112 mutex_lock(&deferred_probe_mutex);
113
114 put_device(dev);
115 }
116 mutex_unlock(&deferred_probe_mutex);
117}
118static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
119
120static void driver_deferred_probe_add(struct device *dev)
121{
122 mutex_lock(&deferred_probe_mutex);
123 if (list_empty(&dev->p->deferred_probe)) {
124 dev_dbg(dev, "Added to deferred list\n");
125 list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
126 }
127 mutex_unlock(&deferred_probe_mutex);
128}
129
130void driver_deferred_probe_del(struct device *dev)
131{
132 mutex_lock(&deferred_probe_mutex);
133 if (!list_empty(&dev->p->deferred_probe)) {
134 dev_dbg(dev, "Removed from deferred list\n");
135 list_del_init(&dev->p->deferred_probe);
136 }
137 mutex_unlock(&deferred_probe_mutex);
138}
139
140static bool driver_deferred_probe_enable = false;
141/**
142 * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
143 *
144 * This functions moves all devices from the pending list to the active
145 * list and schedules the deferred probe workqueue to process them. It
146 * should be called anytime a driver is successfully bound to a device.
147 *
148 * Note, there is a race condition in multi-threaded probe. In the case where
149 * more than one device is probing at the same time, it is possible for one
150 * probe to complete successfully while another is about to defer. If the second
151 * depends on the first, then it will get put on the pending list after the
152 * trigger event has already occurred and will be stuck there.
153 *
154 * The atomic 'deferred_trigger_count' is used to determine if a successful
155 * trigger has occurred in the midst of probing a driver. If the trigger count
156 * changes in the midst of a probe, then deferred processing should be triggered
157 * again.
158 */
159static void driver_deferred_probe_trigger(void)
160{
161 if (!driver_deferred_probe_enable)
162 return;
163
164 /*
165 * A successful probe means that all the devices in the pending list
166 * should be triggered to be reprobed. Move all the deferred devices
167 * into the active list so they can be retried by the workqueue
168 */
169 mutex_lock(&deferred_probe_mutex);
170 atomic_inc(&deferred_trigger_count);
171 list_splice_tail_init(&deferred_probe_pending_list,
172 &deferred_probe_active_list);
173 mutex_unlock(&deferred_probe_mutex);
174
175 /*
176 * Kick the re-probe thread. It may already be scheduled, but it is
177 * safe to kick it again.
178 */
179 schedule_work(&deferred_probe_work);
180}
181
182/**
183 * device_block_probing() - Block/defere device's probes
184 *
185 * It will disable probing of devices and defer their probes instead.
186 */
187void device_block_probing(void)
188{
189 defer_all_probes = true;
190 /* sync with probes to avoid races. */
191 wait_for_device_probe();
192}
193
194/**
195 * device_unblock_probing() - Unblock/enable device's probes
196 *
197 * It will restore normal behavior and trigger re-probing of deferred
198 * devices.
199 */
200void device_unblock_probing(void)
201{
202 defer_all_probes = false;
203 driver_deferred_probe_trigger();
204}
205
206/*
207 * deferred_devs_show() - Show the devices in the deferred probe pending list.
208 */
209static int deferred_devs_show(struct seq_file *s, void *data)
210{
211 struct device_private *curr;
212
213 mutex_lock(&deferred_probe_mutex);
214
215 list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
216 seq_printf(s, "%s\n", dev_name(curr->device));
217
218 mutex_unlock(&deferred_probe_mutex);
219
220 return 0;
221}
222DEFINE_SHOW_ATTRIBUTE(deferred_devs);
223
224static int deferred_probe_timeout = -1;
225static int __init deferred_probe_timeout_setup(char *str)
226{
227 deferred_probe_timeout = simple_strtol(str, NULL, 10);
228 return 1;
229}
230__setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
231
232/**
233 * driver_deferred_probe_check_state() - Check deferred probe state
234 * @dev: device to check
235 *
236 * Returns -ENODEV if init is done and all built-in drivers have had a chance
237 * to probe (i.e. initcalls are done), -ETIMEDOUT if deferred probe debug
238 * timeout has expired, or -EPROBE_DEFER if none of those conditions are met.
239 *
240 * Drivers or subsystems can opt-in to calling this function instead of directly
241 * returning -EPROBE_DEFER.
242 */
243int driver_deferred_probe_check_state(struct device *dev)
244{
245 if (initcalls_done) {
246 if (!deferred_probe_timeout) {
247 dev_WARN(dev, "deferred probe timeout, ignoring dependency");
248 return -ETIMEDOUT;
249 }
250 dev_warn(dev, "ignoring dependency for device, assuming no driver");
251 return -ENODEV;
252 }
253 return -EPROBE_DEFER;
254}
255
256static void deferred_probe_timeout_work_func(struct work_struct *work)
257{
258 struct device_private *private, *p;
259
260 deferred_probe_timeout = 0;
261 driver_deferred_probe_trigger();
262 flush_work(&deferred_probe_work);
263
264 list_for_each_entry_safe(private, p, &deferred_probe_pending_list, deferred_probe)
265 dev_info(private->device, "deferred probe pending");
266}
267static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
268
269/**
270 * deferred_probe_initcall() - Enable probing of deferred devices
271 *
272 * We don't want to get in the way when the bulk of drivers are getting probed.
273 * Instead, this initcall makes sure that deferred probing is delayed until
274 * late_initcall time.
275 */
276static int deferred_probe_initcall(void)
277{
278 deferred_devices = debugfs_create_file("devices_deferred", 0444, NULL,
279 NULL, &deferred_devs_fops);
280
281 driver_deferred_probe_enable = true;
282 driver_deferred_probe_trigger();
283 /* Sort as many dependencies as possible before exiting initcalls */
284 flush_work(&deferred_probe_work);
285 initcalls_done = true;
286
287 /*
288 * Trigger deferred probe again, this time we won't defer anything
289 * that is optional
290 */
291 driver_deferred_probe_trigger();
292 flush_work(&deferred_probe_work);
293
294 if (deferred_probe_timeout > 0) {
295 schedule_delayed_work(&deferred_probe_timeout_work,
296 deferred_probe_timeout * HZ);
297 }
298 return 0;
299}
300late_initcall(deferred_probe_initcall);
301
302static void __exit deferred_probe_exit(void)
303{
304 debugfs_remove_recursive(deferred_devices);
305}
306__exitcall(deferred_probe_exit);
307
308/**
309 * device_is_bound() - Check if device is bound to a driver
310 * @dev: device to check
311 *
312 * Returns true if passed device has already finished probing successfully
313 * against a driver.
314 *
315 * This function must be called with the device lock held.
316 */
317bool device_is_bound(struct device *dev)
318{
319 return dev->p && klist_node_attached(&dev->p->knode_driver);
320}
321
322static void driver_bound(struct device *dev)
323{
324 if (device_is_bound(dev)) {
325 printk(KERN_WARNING "%s: device %s already bound\n",
326 __func__, kobject_name(&dev->kobj));
327 return;
328 }
329
330 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
331 __func__, dev_name(dev));
332
333 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
334 device_links_driver_bound(dev);
335
336 device_pm_check_callbacks(dev);
337
338 /*
339 * Make sure the device is no longer in one of the deferred lists and
340 * kick off retrying all pending devices
341 */
342 driver_deferred_probe_del(dev);
343 driver_deferred_probe_trigger();
344
345 if (dev->bus)
346 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
347 BUS_NOTIFY_BOUND_DRIVER, dev);
348
349 kobject_uevent(&dev->kobj, KOBJ_BIND);
350}
351
352static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
353 const char *buf, size_t count)
354{
355 device_lock(dev);
356 dev->driver->coredump(dev);
357 device_unlock(dev);
358
359 return count;
360}
361static DEVICE_ATTR_WO(coredump);
362
363static int driver_sysfs_add(struct device *dev)
364{
365 int ret;
366
367 if (dev->bus)
368 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
369 BUS_NOTIFY_BIND_DRIVER, dev);
370
371 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
372 kobject_name(&dev->kobj));
373 if (ret)
374 goto fail;
375
376 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
377 "driver");
378 if (ret)
379 goto rm_dev;
380
381 if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
382 !device_create_file(dev, &dev_attr_coredump))
383 return 0;
384
385 sysfs_remove_link(&dev->kobj, "driver");
386
387rm_dev:
388 sysfs_remove_link(&dev->driver->p->kobj,
389 kobject_name(&dev->kobj));
390
391fail:
392 return ret;
393}
394
395static void driver_sysfs_remove(struct device *dev)
396{
397 struct device_driver *drv = dev->driver;
398
399 if (drv) {
400 if (drv->coredump)
401 device_remove_file(dev, &dev_attr_coredump);
402 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
403 sysfs_remove_link(&dev->kobj, "driver");
404 }
405}
406
407/**
408 * device_bind_driver - bind a driver to one device.
409 * @dev: device.
410 *
411 * Allow manual attachment of a driver to a device.
412 * Caller must have already set @dev->driver.
413 *
414 * Note that this does not modify the bus reference count
415 * nor take the bus's rwsem. Please verify those are accounted
416 * for before calling this. (It is ok to call with no other effort
417 * from a driver's probe() method.)
418 *
419 * This function must be called with the device lock held.
420 */
421int device_bind_driver(struct device *dev)
422{
423 int ret;
424
425 ret = driver_sysfs_add(dev);
426 if (!ret)
427 driver_bound(dev);
428 else if (dev->bus)
429 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
430 BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
431 return ret;
432}
433EXPORT_SYMBOL_GPL(device_bind_driver);
434
435static atomic_t probe_count = ATOMIC_INIT(0);
436static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
437
438static void driver_deferred_probe_add_trigger(struct device *dev,
439 int local_trigger_count)
440{
441 driver_deferred_probe_add(dev);
442 /* Did a trigger occur while probing? Need to re-trigger if yes */
443 if (local_trigger_count != atomic_read(&deferred_trigger_count))
444 driver_deferred_probe_trigger();
445}
446
447static int really_probe(struct device *dev, struct device_driver *drv)
448{
449 int ret = -EPROBE_DEFER;
450 int local_trigger_count = atomic_read(&deferred_trigger_count);
451 bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
452 !drv->suppress_bind_attrs;
453#ifdef CONFIG_MTPROF
454 unsigned long long ts;
455#endif
456
457 if (defer_all_probes) {
458 /*
459 * Value of defer_all_probes can be set only by
460 * device_defer_all_probes_enable() which, in turn, will call
461 * wait_for_device_probe() right after that to avoid any races.
462 */
463 dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
464 driver_deferred_probe_add(dev);
465 return ret;
466 }
467
468 ret = device_links_check_suppliers(dev);
469 if (ret == -EPROBE_DEFER)
470 driver_deferred_probe_add_trigger(dev, local_trigger_count);
471 if (ret)
472 return ret;
473
474 atomic_inc(&probe_count);
475 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
476 drv->bus->name, __func__, drv->name, dev_name(dev));
477 WARN_ON(!list_empty(&dev->devres_head));
478
479re_probe:
480 dev->driver = drv;
481
482 /* If using pinctrl, bind pins now before probing */
483 ret = pinctrl_bind_pins(dev);
484 if (ret)
485 goto pinctrl_bind_failed;
486
487 ret = dma_configure(dev);
488 if (ret)
489 goto probe_failed;
490
491 if (driver_sysfs_add(dev)) {
492 printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
493 __func__, dev_name(dev));
494 goto probe_failed;
495 }
496
497 if (dev->pm_domain && dev->pm_domain->activate) {
498 ret = dev->pm_domain->activate(dev);
499 if (ret)
500 goto probe_failed;
501 }
502
503 if (dev->bus->probe) {
504 BOOTPROF_TIME_LOG_START(ts);
505 ret = dev->bus->probe(dev);
506 BOOTPROF_TIME_LOG_END(ts);
507 bootprof_probe(ts, dev, drv, (unsigned long)dev->bus->probe);
508 if (ret)
509 goto probe_failed;
510 } else if (drv->probe) {
511 BOOTPROF_TIME_LOG_START(ts);
512 ret = drv->probe(dev);
513 BOOTPROF_TIME_LOG_END(ts);
514 bootprof_probe(ts, dev, drv, (unsigned long)drv->probe);
515 if (ret)
516 goto probe_failed;
517 }
518
519 if (test_remove) {
520 test_remove = false;
521
522 if (dev->bus->remove)
523 dev->bus->remove(dev);
524 else if (drv->remove)
525 drv->remove(dev);
526
527 devres_release_all(dev);
528 driver_sysfs_remove(dev);
529 dev->driver = NULL;
530 dev_set_drvdata(dev, NULL);
531 if (dev->pm_domain && dev->pm_domain->dismiss)
532 dev->pm_domain->dismiss(dev);
533 pm_runtime_reinit(dev);
534
535 goto re_probe;
536 }
537
538 pinctrl_init_done(dev);
539
540 if (dev->pm_domain && dev->pm_domain->sync)
541 dev->pm_domain->sync(dev);
542
543 driver_bound(dev);
544 ret = 1;
545 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
546 drv->bus->name, __func__, dev_name(dev), drv->name);
547 goto done;
548
549probe_failed:
550 if (dev->bus)
551 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
552 BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
553pinctrl_bind_failed:
554 device_links_no_driver(dev);
555 devres_release_all(dev);
556 dma_deconfigure(dev);
557 driver_sysfs_remove(dev);
558 dev->driver = NULL;
559 dev_set_drvdata(dev, NULL);
560 if (dev->pm_domain && dev->pm_domain->dismiss)
561 dev->pm_domain->dismiss(dev);
562 pm_runtime_reinit(dev);
563 dev_pm_set_driver_flags(dev, 0);
564
565 switch (ret) {
566 case -EPROBE_DEFER:
567 /* Driver requested deferred probing */
568 dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
569 driver_deferred_probe_add_trigger(dev, local_trigger_count);
570 break;
571 case -ENODEV:
572 case -ENXIO:
573 pr_debug("%s: probe of %s rejects match %d\n",
574 drv->name, dev_name(dev), ret);
575 break;
576 default:
577 /* driver matched but the probe failed */
578 printk(KERN_WARNING
579 "%s: probe of %s failed with error %d\n",
580 drv->name, dev_name(dev), ret);
581 }
582 /*
583 * Ignore errors returned by ->probe so that the next driver can try
584 * its luck.
585 */
586 ret = 0;
587done:
588 atomic_dec(&probe_count);
589 wake_up(&probe_waitqueue);
590 return ret;
591}
592
593/*
594 * For initcall_debug, show the driver probe time.
595 */
596static int really_probe_debug(struct device *dev, struct device_driver *drv)
597{
598 ktime_t calltime, delta, rettime;
599 int ret;
600
601 calltime = ktime_get();
602 ret = really_probe(dev, drv);
603 rettime = ktime_get();
604 delta = ktime_sub(rettime, calltime);
605 printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
606 dev_name(dev), ret, (s64) ktime_to_us(delta));
607 return ret;
608}
609
610/**
611 * driver_probe_done
612 * Determine if the probe sequence is finished or not.
613 *
614 * Should somehow figure out how to use a semaphore, not an atomic variable...
615 */
616int driver_probe_done(void)
617{
618 pr_debug("%s: probe_count = %d\n", __func__,
619 atomic_read(&probe_count));
620 if (atomic_read(&probe_count))
621 return -EBUSY;
622 return 0;
623}
624
625/**
626 * wait_for_device_probe
627 * Wait for device probing to be completed.
628 */
629void wait_for_device_probe(void)
630{
631 /* wait for the deferred probe workqueue to finish */
632 flush_work(&deferred_probe_work);
633
634 /* wait for the known devices to complete their probing */
635 wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
636 async_synchronize_full();
637}
638EXPORT_SYMBOL_GPL(wait_for_device_probe);
639
640/**
641 * driver_probe_device - attempt to bind device & driver together
642 * @drv: driver to bind a device to
643 * @dev: device to try to bind to the driver
644 *
645 * This function returns -ENODEV if the device is not registered,
646 * 1 if the device is bound successfully and 0 otherwise.
647 *
648 * This function must be called with @dev lock held. When called for a
649 * USB interface, @dev->parent lock must be held as well.
650 *
651 * If the device has a parent, runtime-resume the parent before driver probing.
652 */
653int driver_probe_device(struct device_driver *drv, struct device *dev)
654{
655 int ret = 0;
656
657 if (!device_is_registered(dev))
658 return -ENODEV;
659
660 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
661 drv->bus->name, __func__, dev_name(dev), drv->name);
662
663 pm_runtime_get_suppliers(dev);
664 if (dev->parent)
665 pm_runtime_get_sync(dev->parent);
666
667 pm_runtime_barrier(dev);
668 if (initcall_debug)
669 ret = really_probe_debug(dev, drv);
670 else
671 ret = really_probe(dev, drv);
672 pm_request_idle(dev);
673
674 if (dev->parent)
675 pm_runtime_put(dev->parent);
676
677 pm_runtime_put_suppliers(dev);
678 return ret;
679}
680
681bool driver_allows_async_probing(struct device_driver *drv)
682{
683 switch (drv->probe_type) {
684 case PROBE_PREFER_ASYNCHRONOUS:
685 return true;
686
687 case PROBE_FORCE_SYNCHRONOUS:
688 return false;
689
690 default:
691 if (module_requested_async_probing(drv->owner))
692 return true;
693
694 return false;
695 }
696}
697
698struct device_attach_data {
699 struct device *dev;
700
701 /*
702 * Indicates whether we are are considering asynchronous probing or
703 * not. Only initial binding after device or driver registration
704 * (including deferral processing) may be done asynchronously, the
705 * rest is always synchronous, as we expect it is being done by
706 * request from userspace.
707 */
708 bool check_async;
709
710 /*
711 * Indicates if we are binding synchronous or asynchronous drivers.
712 * When asynchronous probing is enabled we'll execute 2 passes
713 * over drivers: first pass doing synchronous probing and second
714 * doing asynchronous probing (if synchronous did not succeed -
715 * most likely because there was no driver requiring synchronous
716 * probing - and we found asynchronous driver during first pass).
717 * The 2 passes are done because we can't shoot asynchronous
718 * probe for given device and driver from bus_for_each_drv() since
719 * driver pointer is not guaranteed to stay valid once
720 * bus_for_each_drv() iterates to the next driver on the bus.
721 */
722 bool want_async;
723
724 /*
725 * We'll set have_async to 'true' if, while scanning for matching
726 * driver, we'll encounter one that requests asynchronous probing.
727 */
728 bool have_async;
729};
730
731static int __device_attach_driver(struct device_driver *drv, void *_data)
732{
733 struct device_attach_data *data = _data;
734 struct device *dev = data->dev;
735 bool async_allowed;
736 int ret;
737
738 ret = driver_match_device(drv, dev);
739 if (ret == 0) {
740 /* no match */
741 return 0;
742 } else if (ret == -EPROBE_DEFER) {
743 dev_dbg(dev, "Device match requests probe deferral\n");
744 driver_deferred_probe_add(dev);
745 } else if (ret < 0) {
746 dev_dbg(dev, "Bus failed to match device: %d", ret);
747 return ret;
748 } /* ret > 0 means positive match */
749
750 async_allowed = driver_allows_async_probing(drv);
751
752 if (async_allowed)
753 data->have_async = true;
754
755 if (data->check_async && async_allowed != data->want_async)
756 return 0;
757
758 return driver_probe_device(drv, dev);
759}
760
761static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
762{
763 struct device *dev = _dev;
764 struct device_attach_data data = {
765 .dev = dev,
766 .check_async = true,
767 .want_async = true,
768 };
769
770 device_lock(dev);
771
772 /*
773 * Check if device has already been removed or claimed. This may
774 * happen with driver loading, device discovery/registration,
775 * and deferred probe processing happens all at once with
776 * multiple threads.
777 */
778 if (dev->p->dead || dev->driver)
779 goto out_unlock;
780
781 if (dev->parent)
782 pm_runtime_get_sync(dev->parent);
783
784 bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
785 dev_dbg(dev, "async probe completed\n");
786
787 pm_request_idle(dev);
788
789 if (dev->parent)
790 pm_runtime_put(dev->parent);
791out_unlock:
792 device_unlock(dev);
793
794 put_device(dev);
795}
796
797static int __device_attach(struct device *dev, bool allow_async)
798{
799 int ret = 0;
800
801 device_lock(dev);
802 if (dev->driver) {
803 if (device_is_bound(dev)) {
804 ret = 1;
805 goto out_unlock;
806 }
807 ret = device_bind_driver(dev);
808 if (ret == 0)
809 ret = 1;
810 else {
811 dev->driver = NULL;
812 ret = 0;
813 }
814 } else {
815 struct device_attach_data data = {
816 .dev = dev,
817 .check_async = allow_async,
818 .want_async = false,
819 };
820
821 if (dev->parent)
822 pm_runtime_get_sync(dev->parent);
823
824 ret = bus_for_each_drv(dev->bus, NULL, &data,
825 __device_attach_driver);
826 if (!ret && allow_async && data.have_async) {
827 /*
828 * If we could not find appropriate driver
829 * synchronously and we are allowed to do
830 * async probes and there are drivers that
831 * want to probe asynchronously, we'll
832 * try them.
833 */
834 dev_dbg(dev, "scheduling asynchronous probe\n");
835 get_device(dev);
836 async_schedule(__device_attach_async_helper, dev);
837 } else {
838 pm_request_idle(dev);
839 }
840
841 if (dev->parent)
842 pm_runtime_put(dev->parent);
843 }
844out_unlock:
845 device_unlock(dev);
846 return ret;
847}
848
849/**
850 * device_attach - try to attach device to a driver.
851 * @dev: device.
852 *
853 * Walk the list of drivers that the bus has and call
854 * driver_probe_device() for each pair. If a compatible
855 * pair is found, break out and return.
856 *
857 * Returns 1 if the device was bound to a driver;
858 * 0 if no matching driver was found;
859 * -ENODEV if the device is not registered.
860 *
861 * When called for a USB interface, @dev->parent lock must be held.
862 */
863int device_attach(struct device *dev)
864{
865 return __device_attach(dev, false);
866}
867EXPORT_SYMBOL_GPL(device_attach);
868
869void device_initial_probe(struct device *dev)
870{
871 __device_attach(dev, true);
872}
873
874static int __driver_attach(struct device *dev, void *data)
875{
876 struct device_driver *drv = data;
877 int ret;
878
879 /*
880 * Lock device and try to bind to it. We drop the error
881 * here and always return 0, because we need to keep trying
882 * to bind to devices and some drivers will return an error
883 * simply if it didn't support the device.
884 *
885 * driver_probe_device() will spit a warning if there
886 * is an error.
887 */
888
889 ret = driver_match_device(drv, dev);
890 if (ret == 0) {
891 /* no match */
892 return 0;
893 } else if (ret == -EPROBE_DEFER) {
894 dev_dbg(dev, "Device match requests probe deferral\n");
895 driver_deferred_probe_add(dev);
896 } else if (ret < 0) {
897 dev_dbg(dev, "Bus failed to match device: %d", ret);
898 return ret;
899 } /* ret > 0 means positive match */
900
901 if (dev->parent && dev->bus->need_parent_lock)
902 device_lock(dev->parent);
903 device_lock(dev);
904 if (!dev->p->dead && !dev->driver)
905 driver_probe_device(drv, dev);
906 device_unlock(dev);
907 if (dev->parent && dev->bus->need_parent_lock)
908 device_unlock(dev->parent);
909
910 return 0;
911}
912
913/**
914 * driver_attach - try to bind driver to devices.
915 * @drv: driver.
916 *
917 * Walk the list of devices that the bus has on it and try to
918 * match the driver with each one. If driver_probe_device()
919 * returns 0 and the @dev->driver is set, we've found a
920 * compatible pair.
921 */
922int driver_attach(struct device_driver *drv)
923{
924 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
925}
926EXPORT_SYMBOL_GPL(driver_attach);
927
928/*
929 * __device_release_driver() must be called with @dev lock held.
930 * When called for a USB interface, @dev->parent lock must be held as well.
931 */
932static void __device_release_driver(struct device *dev, struct device *parent)
933{
934 struct device_driver *drv;
935
936 drv = dev->driver;
937 if (drv) {
938 while (device_links_busy(dev)) {
939 device_unlock(dev);
940 if (parent && dev->bus->need_parent_lock)
941 device_unlock(parent);
942
943 device_links_unbind_consumers(dev);
944 if (parent && dev->bus->need_parent_lock)
945 device_lock(parent);
946
947 device_lock(dev);
948 /*
949 * A concurrent invocation of the same function might
950 * have released the driver successfully while this one
951 * was waiting, so check for that.
952 */
953 if (dev->driver != drv)
954 return;
955 }
956
957 pm_runtime_get_sync(dev);
958 pm_runtime_clean_up_links(dev);
959
960 driver_sysfs_remove(dev);
961
962 if (dev->bus)
963 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
964 BUS_NOTIFY_UNBIND_DRIVER,
965 dev);
966
967 pm_runtime_put_sync(dev);
968
969 if (dev->bus && dev->bus->remove)
970 dev->bus->remove(dev);
971 else if (drv->remove)
972 drv->remove(dev);
973
974 device_links_driver_cleanup(dev);
975
976 devres_release_all(dev);
977 dma_deconfigure(dev);
978 dev->driver = NULL;
979 dev_set_drvdata(dev, NULL);
980 if (dev->pm_domain && dev->pm_domain->dismiss)
981 dev->pm_domain->dismiss(dev);
982 pm_runtime_reinit(dev);
983 dev_pm_set_driver_flags(dev, 0);
984
985 klist_remove(&dev->p->knode_driver);
986 device_pm_check_callbacks(dev);
987 if (dev->bus)
988 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
989 BUS_NOTIFY_UNBOUND_DRIVER,
990 dev);
991
992 kobject_uevent(&dev->kobj, KOBJ_UNBIND);
993 }
994}
995
996void device_release_driver_internal(struct device *dev,
997 struct device_driver *drv,
998 struct device *parent)
999{
1000 if (parent && dev->bus->need_parent_lock)
1001 device_lock(parent);
1002
1003 device_lock(dev);
1004 if (!drv || drv == dev->driver)
1005 __device_release_driver(dev, parent);
1006
1007 device_unlock(dev);
1008 if (parent && dev->bus->need_parent_lock)
1009 device_unlock(parent);
1010}
1011
1012/**
1013 * device_release_driver - manually detach device from driver.
1014 * @dev: device.
1015 *
1016 * Manually detach device from driver.
1017 * When called for a USB interface, @dev->parent lock must be held.
1018 *
1019 * If this function is to be called with @dev->parent lock held, ensure that
1020 * the device's consumers are unbound in advance or that their locks can be
1021 * acquired under the @dev->parent lock.
1022 */
1023void device_release_driver(struct device *dev)
1024{
1025 /*
1026 * If anyone calls device_release_driver() recursively from
1027 * within their ->remove callback for the same device, they
1028 * will deadlock right here.
1029 */
1030 device_release_driver_internal(dev, NULL, NULL);
1031}
1032EXPORT_SYMBOL_GPL(device_release_driver);
1033
1034/**
1035 * driver_detach - detach driver from all devices it controls.
1036 * @drv: driver.
1037 */
1038void driver_detach(struct device_driver *drv)
1039{
1040 struct device_private *dev_prv;
1041 struct device *dev;
1042
1043 if (driver_allows_async_probing(drv))
1044 async_synchronize_full();
1045
1046 for (;;) {
1047 spin_lock(&drv->p->klist_devices.k_lock);
1048 if (list_empty(&drv->p->klist_devices.k_list)) {
1049 spin_unlock(&drv->p->klist_devices.k_lock);
1050 break;
1051 }
1052 dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
1053 struct device_private,
1054 knode_driver.n_node);
1055 dev = dev_prv->device;
1056 get_device(dev);
1057 spin_unlock(&drv->p->klist_devices.k_lock);
1058 device_release_driver_internal(dev, drv, dev->parent);
1059 put_device(dev);
1060 }
1061}