kobject: remove unused KOBJ_MAX action
[linux-block.git] / drivers / base / dd.c
CommitLineData
989d42e8 1// SPDX-License-Identifier: GPL-2.0
07e4a3e2 2/*
4a3ad20c 3 * drivers/base/dd.c - The core device/driver interactions.
07e4a3e2 4 *
4a3ad20c
GKH
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.
07e4a3e2 8 *
4a3ad20c
GKH
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'.
07e4a3e2 12 *
4a3ad20c
GKH
13 * Copyright (c) 2002-5 Patrick Mochel
14 * Copyright (c) 2002-3 Open Source Development Labs
b4028437
GKH
15 * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
16 * Copyright (c) 2007-2009 Novell Inc.
07e4a3e2 17 */
18
28af109a 19#include <linux/debugfs.h>
07e4a3e2 20#include <linux/device.h>
216773a7 21#include <linux/delay.h>
09515ef5 22#include <linux/dma-mapping.h>
1f5000bd 23#include <linux/init.h>
07e4a3e2 24#include <linux/module.h>
d779249e 25#include <linux/kthread.h>
735a7ffb 26#include <linux/wait.h>
216773a7 27#include <linux/async.h>
5e928f77 28#include <linux/pm_runtime.h>
ab78029e 29#include <linux/pinctrl/devinfo.h>
07e4a3e2 30
31#include "base.h"
32#include "power/power.h"
33
d1c3414c
GL
34/*
35 * Deferred Probe infrastructure.
36 *
37 * Sometimes driver probe order matters, but the kernel doesn't always have
38 * dependency information which means some drivers will get probed before a
39 * resource it depends on is available. For example, an SDHCI driver may
40 * first need a GPIO line from an i2c GPIO controller before it can be
41 * initialized. If a required resource is not available yet, a driver can
42 * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
43 *
44 * Deferred probe maintains two lists of devices, a pending list and an active
45 * list. A driver returning -EPROBE_DEFER causes the device to be added to the
46 * pending list. A successful driver probe will trigger moving all devices
47 * from the pending to the active list so that the workqueue will eventually
48 * retry them.
49 *
50 * The deferred_probe_mutex must be held any time the deferred_probe_*_list
ef8a3fd6 51 * of the (struct device*)->p->deferred_probe pointers are manipulated
d1c3414c
GL
52 */
53static DEFINE_MUTEX(deferred_probe_mutex);
54static LIST_HEAD(deferred_probe_pending_list);
55static LIST_HEAD(deferred_probe_active_list);
58b116bc 56static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
28af109a 57static struct dentry *deferred_devices;
25b4e70d 58static bool initcalls_done;
d1c3414c 59
1ea61b68
FT
60/* Save the async probe drivers' name from kernel cmdline */
61#define ASYNC_DRV_NAMES_MAX_LEN 256
62static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
63
013c074f
SG
64/*
65 * In some cases, like suspend to RAM or hibernation, It might be reasonable
66 * to prohibit probing of devices as it could be unsafe.
67 * Once defer_all_probes is true all drivers probes will be forcibly deferred.
68 */
69static bool defer_all_probes;
70
41575335 71/*
d1c3414c
GL
72 * deferred_probe_work_func() - Retry probing devices in the active list.
73 */
74static void deferred_probe_work_func(struct work_struct *work)
75{
76 struct device *dev;
ef8a3fd6 77 struct device_private *private;
d1c3414c
GL
78 /*
79 * This block processes every device in the deferred 'active' list.
80 * Each device is removed from the active list and passed to
81 * bus_probe_device() to re-attempt the probe. The loop continues
82 * until every device in the active list is removed and retried.
83 *
84 * Note: Once the device is removed from the list and the mutex is
85 * released, it is possible for the device get freed by another thread
86 * and cause a illegal pointer dereference. This code uses
87 * get/put_device() to ensure the device structure cannot disappear
88 * from under our feet.
89 */
90 mutex_lock(&deferred_probe_mutex);
91 while (!list_empty(&deferred_probe_active_list)) {
ef8a3fd6
GKH
92 private = list_first_entry(&deferred_probe_active_list,
93 typeof(*dev->p), deferred_probe);
94 dev = private->device;
95 list_del_init(&private->deferred_probe);
d1c3414c
GL
96
97 get_device(dev);
98
8b0372a2
GKH
99 /*
100 * Drop the mutex while probing each device; the probe path may
101 * manipulate the deferred list
102 */
d1c3414c 103 mutex_unlock(&deferred_probe_mutex);
8153584e
MB
104
105 /*
106 * Force the device to the end of the dpm_list since
107 * the PM code assumes that the order we add things to
108 * the list is a good order for suspend but deferred
109 * probe makes that very unsafe.
110 */
494fd7b7 111 device_pm_move_to_tail(dev);
8153584e 112
d1c3414c 113 dev_dbg(dev, "Retrying from deferred list\n");
0a50f61c 114 bus_probe_device(dev);
d1c3414c
GL
115 mutex_lock(&deferred_probe_mutex);
116
117 put_device(dev);
118 }
119 mutex_unlock(&deferred_probe_mutex);
120}
121static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
122
e7dd4010 123void driver_deferred_probe_add(struct device *dev)
d1c3414c
GL
124{
125 mutex_lock(&deferred_probe_mutex);
ef8a3fd6 126 if (list_empty(&dev->p->deferred_probe)) {
d1c3414c 127 dev_dbg(dev, "Added to deferred list\n");
1d29cfa5 128 list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
d1c3414c
GL
129 }
130 mutex_unlock(&deferred_probe_mutex);
131}
132
133void driver_deferred_probe_del(struct device *dev)
134{
135 mutex_lock(&deferred_probe_mutex);
ef8a3fd6 136 if (!list_empty(&dev->p->deferred_probe)) {
d1c3414c 137 dev_dbg(dev, "Removed from deferred list\n");
ef8a3fd6 138 list_del_init(&dev->p->deferred_probe);
d1c3414c
GL
139 }
140 mutex_unlock(&deferred_probe_mutex);
141}
142
143static bool driver_deferred_probe_enable = false;
144/**
145 * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
146 *
147 * This functions moves all devices from the pending list to the active
148 * list and schedules the deferred probe workqueue to process them. It
149 * should be called anytime a driver is successfully bound to a device.
58b116bc
GL
150 *
151 * Note, there is a race condition in multi-threaded probe. In the case where
152 * more than one device is probing at the same time, it is possible for one
153 * probe to complete successfully while another is about to defer. If the second
154 * depends on the first, then it will get put on the pending list after the
9ba8af66 155 * trigger event has already occurred and will be stuck there.
58b116bc
GL
156 *
157 * The atomic 'deferred_trigger_count' is used to determine if a successful
158 * trigger has occurred in the midst of probing a driver. If the trigger count
159 * changes in the midst of a probe, then deferred processing should be triggered
160 * again.
d1c3414c
GL
161 */
162static void driver_deferred_probe_trigger(void)
716a7a25
SK
163{
164 if (!driver_deferred_probe_enable)
165 return;
166
8b0372a2
GKH
167 /*
168 * A successful probe means that all the devices in the pending list
d1c3414c 169 * should be triggered to be reprobed. Move all the deferred devices
8b0372a2
GKH
170 * into the active list so they can be retried by the workqueue
171 */
d1c3414c 172 mutex_lock(&deferred_probe_mutex);
58b116bc 173 atomic_inc(&deferred_trigger_count);
d1c3414c
GL
174 list_splice_tail_init(&deferred_probe_pending_list,
175 &deferred_probe_active_list);
176 mutex_unlock(&deferred_probe_mutex);
177
8b0372a2
GKH
178 /*
179 * Kick the re-probe thread. It may already be scheduled, but it is
180 * safe to kick it again.
181 */
2c507e46 182 schedule_work(&deferred_probe_work);
d1c3414c
GL
183}
184
013c074f 185/**
dbf03d65 186 * device_block_probing() - Block/defer device's probes
013c074f
SG
187 *
188 * It will disable probing of devices and defer their probes instead.
189 */
190void device_block_probing(void)
191{
192 defer_all_probes = true;
193 /* sync with probes to avoid races. */
194 wait_for_device_probe();
195}
196
197/**
198 * device_unblock_probing() - Unblock/enable device's probes
199 *
200 * It will restore normal behavior and trigger re-probing of deferred
201 * devices.
202 */
203void device_unblock_probing(void)
204{
205 defer_all_probes = false;
206 driver_deferred_probe_trigger();
207}
208
28af109a
JMC
209/*
210 * deferred_devs_show() - Show the devices in the deferred probe pending list.
211 */
212static int deferred_devs_show(struct seq_file *s, void *data)
213{
214 struct device_private *curr;
215
216 mutex_lock(&deferred_probe_mutex);
217
218 list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
219 seq_printf(s, "%s\n", dev_name(curr->device));
220
221 mutex_unlock(&deferred_probe_mutex);
222
223 return 0;
224}
225DEFINE_SHOW_ATTRIBUTE(deferred_devs);
226
ce68929f 227int driver_deferred_probe_timeout;
64c775fb 228EXPORT_SYMBOL_GPL(driver_deferred_probe_timeout);
35a67236 229static DECLARE_WAIT_QUEUE_HEAD(probe_timeout_waitqueue);
64c775fb 230
25b4e70d
RH
231static int __init deferred_probe_timeout_setup(char *str)
232{
63c98047
MS
233 int timeout;
234
235 if (!kstrtoint(str, 10, &timeout))
64c775fb 236 driver_deferred_probe_timeout = timeout;
25b4e70d
RH
237 return 1;
238}
239__setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
240
241/**
242 * driver_deferred_probe_check_state() - Check deferred probe state
243 * @dev: device to check
244 *
c8c43cee
JS
245 * Return:
246 * -ENODEV if initcalls have completed and modules are disabled.
247 * -ETIMEDOUT if the deferred probe timeout was set and has expired
248 * and modules are enabled.
249 * -EPROBE_DEFER in other cases.
25b4e70d
RH
250 *
251 * Drivers or subsystems can opt-in to calling this function instead of directly
252 * returning -EPROBE_DEFER.
253 */
254int driver_deferred_probe_check_state(struct device *dev)
255{
0e9f8d09 256 if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
eb7fbc9f 257 dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
0e9f8d09
JS
258 return -ENODEV;
259 }
62a6bc3a 260
ce68929f 261 if (!driver_deferred_probe_timeout && initcalls_done) {
c8be6af9 262 dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
0e9f8d09
JS
263 return -ETIMEDOUT;
264 }
62a6bc3a 265
25b4e70d
RH
266 return -EPROBE_DEFER;
267}
268
269static void deferred_probe_timeout_work_func(struct work_struct *work)
270{
271 struct device_private *private, *p;
272
64c775fb 273 driver_deferred_probe_timeout = 0;
25b4e70d
RH
274 driver_deferred_probe_trigger();
275 flush_work(&deferred_probe_work);
276
277 list_for_each_entry_safe(private, p, &deferred_probe_pending_list, deferred_probe)
eb7fbc9f 278 dev_info(private->device, "deferred probe pending\n");
35a67236 279 wake_up(&probe_timeout_waitqueue);
25b4e70d
RH
280}
281static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
282
d1c3414c
GL
283/**
284 * deferred_probe_initcall() - Enable probing of deferred devices
285 *
286 * We don't want to get in the way when the bulk of drivers are getting probed.
287 * Instead, this initcall makes sure that deferred probing is delayed until
288 * late_initcall time.
289 */
290static int deferred_probe_initcall(void)
291{
28af109a
JMC
292 deferred_devices = debugfs_create_file("devices_deferred", 0444, NULL,
293 NULL, &deferred_devs_fops);
294
d1c3414c
GL
295 driver_deferred_probe_enable = true;
296 driver_deferred_probe_trigger();
d72cca1e 297 /* Sort as many dependencies as possible before exiting initcalls */
2c507e46 298 flush_work(&deferred_probe_work);
25b4e70d
RH
299 initcalls_done = true;
300
301 /*
302 * Trigger deferred probe again, this time we won't defer anything
303 * that is optional
304 */
305 driver_deferred_probe_trigger();
306 flush_work(&deferred_probe_work);
307
64c775fb 308 if (driver_deferred_probe_timeout > 0) {
25b4e70d 309 schedule_delayed_work(&deferred_probe_timeout_work,
64c775fb 310 driver_deferred_probe_timeout * HZ);
25b4e70d 311 }
d1c3414c
GL
312 return 0;
313}
314late_initcall(deferred_probe_initcall);
07e4a3e2 315
28af109a
JMC
316static void __exit deferred_probe_exit(void)
317{
318 debugfs_remove_recursive(deferred_devices);
319}
320__exitcall(deferred_probe_exit);
321
6b9cb427
TV
322/**
323 * device_is_bound() - Check if device is bound to a driver
324 * @dev: device to check
325 *
326 * Returns true if passed device has already finished probing successfully
327 * against a driver.
328 *
329 * This function must be called with the device lock held.
330 */
331bool device_is_bound(struct device *dev)
332{
3ded9104 333 return dev->p && klist_node_attached(&dev->p->knode_driver);
6b9cb427
TV
334}
335
1901fb26 336static void driver_bound(struct device *dev)
07e4a3e2 337{
6b9cb427 338 if (device_is_bound(dev)) {
eb7fbc9f 339 pr_warn("%s: device %s already bound\n",
2b3a302a 340 __func__, kobject_name(&dev->kobj));
1901fb26 341 return;
f86db396 342 }
4c898c7f 343
94f8cc0e
FR
344 pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
345 __func__, dev_name(dev));
116af378 346
fbb88fad 347 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
9ed98953 348 device_links_driver_bound(dev);
fbb88fad 349
aa8e54b5
TV
350 device_pm_check_callbacks(dev);
351
8b0372a2
GKH
352 /*
353 * Make sure the device is no longer in one of the deferred lists and
354 * kick off retrying all pending devices
355 */
d1c3414c
GL
356 driver_deferred_probe_del(dev);
357 driver_deferred_probe_trigger();
358
116af378 359 if (dev->bus)
c6f7e72a 360 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
116af378 361 BUS_NOTIFY_BOUND_DRIVER, dev);
1455cf8d
DT
362
363 kobject_uevent(&dev->kobj, KOBJ_BIND);
1901fb26
KS
364}
365
3c47d19f
AS
366static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
367 const char *buf, size_t count)
368{
369 device_lock(dev);
1fe56e0c 370 dev->driver->coredump(dev);
3c47d19f
AS
371 device_unlock(dev);
372
373 return count;
374}
375static DEVICE_ATTR_WO(coredump);
376
1901fb26
KS
377static int driver_sysfs_add(struct device *dev)
378{
379 int ret;
380
45daef0f
MD
381 if (dev->bus)
382 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
383 BUS_NOTIFY_BIND_DRIVER, dev);
384
e5dd1278 385 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
3c47d19f
AS
386 kobject_name(&dev->kobj));
387 if (ret)
388 goto fail;
389
390 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
391 "driver");
392 if (ret)
393 goto rm_dev;
394
395 if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
396 !device_create_file(dev, &dev_attr_coredump))
397 return 0;
398
399 sysfs_remove_link(&dev->kobj, "driver");
400
401rm_dev:
402 sysfs_remove_link(&dev->driver->p->kobj,
07e4a3e2 403 kobject_name(&dev->kobj));
3c47d19f
AS
404
405fail:
f86db396 406 return ret;
07e4a3e2 407}
408
1901fb26
KS
409static void driver_sysfs_remove(struct device *dev)
410{
411 struct device_driver *drv = dev->driver;
412
413 if (drv) {
3c47d19f
AS
414 if (drv->coredump)
415 device_remove_file(dev, &dev_attr_coredump);
e5dd1278 416 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
1901fb26
KS
417 sysfs_remove_link(&dev->kobj, "driver");
418 }
419}
420
421/**
4a3ad20c
GKH
422 * device_bind_driver - bind a driver to one device.
423 * @dev: device.
1901fb26 424 *
4a3ad20c
GKH
425 * Allow manual attachment of a driver to a device.
426 * Caller must have already set @dev->driver.
1901fb26 427 *
fe940d73
LW
428 * Note that this does not modify the bus reference count.
429 * Please verify that is accounted for before calling this.
430 * (It is ok to call with no other effort from a driver's probe() method.)
1901fb26 431 *
8e9394ce 432 * This function must be called with the device lock held.
1901fb26
KS
433 */
434int device_bind_driver(struct device *dev)
435{
cb986b74
CH
436 int ret;
437
438 ret = driver_sysfs_add(dev);
439 if (!ret)
440 driver_bound(dev);
14b6257a
AS
441 else if (dev->bus)
442 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
443 BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
cb986b74 444 return ret;
1901fb26 445}
4a3ad20c 446EXPORT_SYMBOL_GPL(device_bind_driver);
1901fb26 447
d779249e 448static atomic_t probe_count = ATOMIC_INIT(0);
735a7ffb
AM
449static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
450
0ff26c66
AH
451static void driver_deferred_probe_add_trigger(struct device *dev,
452 int local_trigger_count)
453{
454 driver_deferred_probe_add(dev);
455 /* Did a trigger occur while probing? Need to re-trigger if yes */
456 if (local_trigger_count != atomic_read(&deferred_trigger_count))
457 driver_deferred_probe_trigger();
458}
459
8fd456ec
SK
460static ssize_t state_synced_show(struct device *dev,
461 struct device_attribute *attr, char *buf)
462{
463 bool val;
464
465 device_lock(dev);
466 val = dev->state_synced;
467 device_unlock(dev);
468 return sprintf(buf, "%u\n", val);
469}
470static DEVICE_ATTR_RO(state_synced);
471
21c7f30b 472static int really_probe(struct device *dev, struct device_driver *drv)
07e4a3e2 473{
013c074f 474 int ret = -EPROBE_DEFER;
58b116bc 475 int local_trigger_count = atomic_read(&deferred_trigger_count);
c5f06274
RH
476 bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
477 !drv->suppress_bind_attrs;
07e4a3e2 478
013c074f
SG
479 if (defer_all_probes) {
480 /*
481 * Value of defer_all_probes can be set only by
dbf03d65 482 * device_block_probing() which, in turn, will call
013c074f
SG
483 * wait_for_device_probe() right after that to avoid any races.
484 */
485 dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
486 driver_deferred_probe_add(dev);
487 return ret;
488 }
489
9ed98953 490 ret = device_links_check_suppliers(dev);
0ff26c66
AH
491 if (ret == -EPROBE_DEFER)
492 driver_deferred_probe_add_trigger(dev, local_trigger_count);
9ed98953
RW
493 if (ret)
494 return ret;
495
d779249e 496 atomic_inc(&probe_count);
7dc72b28 497 pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
1e0b2cf9 498 drv->bus->name, __func__, drv->name, dev_name(dev));
7c35e699
GU
499 if (!list_empty(&dev->devres_head)) {
500 dev_crit(dev, "Resources present before probing\n");
501 return -EBUSY;
502 }
07e4a3e2 503
bea5b158 504re_probe:
07e4a3e2 505 dev->driver = drv;
ab78029e
LW
506
507 /* If using pinctrl, bind pins now before probing */
508 ret = pinctrl_bind_pins(dev);
509 if (ret)
14b6257a 510 goto pinctrl_bind_failed;
ab78029e 511
ccf640f4
CH
512 if (dev->bus->dma_configure) {
513 ret = dev->bus->dma_configure(dev);
514 if (ret)
0b777eee 515 goto probe_failed;
ccf640f4 516 }
09515ef5 517
1901fb26 518 if (driver_sysfs_add(dev)) {
eb7fbc9f
CJ
519 pr_err("%s: driver_sysfs_add(%s) failed\n",
520 __func__, dev_name(dev));
1901fb26
KS
521 goto probe_failed;
522 }
523
e90d5532
RW
524 if (dev->pm_domain && dev->pm_domain->activate) {
525 ret = dev->pm_domain->activate(dev);
526 if (ret)
527 goto probe_failed;
528 }
529
594c8281
RK
530 if (dev->bus->probe) {
531 ret = dev->bus->probe(dev);
1901fb26 532 if (ret)
d779249e 533 goto probe_failed;
594c8281 534 } else if (drv->probe) {
0d3e5a2e 535 ret = drv->probe(dev);
1901fb26 536 if (ret)
d779249e 537 goto probe_failed;
f86db396 538 }
1901fb26 539
23b69044
DT
540 if (device_add_groups(dev, drv->dev_groups)) {
541 dev_err(dev, "device_add_groups() failed\n");
542 goto dev_groups_failed;
543 }
544
8fd456ec
SK
545 if (dev_has_sync_state(dev) &&
546 device_create_file(dev, &dev_attr_state_synced)) {
547 dev_err(dev, "state_synced sysfs add failed\n");
548 goto dev_sysfs_state_synced_failed;
549 }
550
bea5b158
RH
551 if (test_remove) {
552 test_remove = false;
553
8fd456ec 554 device_remove_file(dev, &dev_attr_state_synced);
23b69044
DT
555 device_remove_groups(dev, drv->dev_groups);
556
bdacd1b4 557 if (dev->bus->remove)
bea5b158
RH
558 dev->bus->remove(dev);
559 else if (drv->remove)
560 drv->remove(dev);
561
562 devres_release_all(dev);
563 driver_sysfs_remove(dev);
564 dev->driver = NULL;
565 dev_set_drvdata(dev, NULL);
566 if (dev->pm_domain && dev->pm_domain->dismiss)
567 dev->pm_domain->dismiss(dev);
568 pm_runtime_reinit(dev);
569
570 goto re_probe;
571 }
572
ef0eebc0
DA
573 pinctrl_init_done(dev);
574
e90d5532
RW
575 if (dev->pm_domain && dev->pm_domain->sync)
576 dev->pm_domain->sync(dev);
577
1901fb26 578 driver_bound(dev);
0d3e5a2e 579 ret = 1;
7dc72b28 580 pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
1e0b2cf9 581 drv->bus->name, __func__, dev_name(dev), drv->name);
d779249e 582 goto done;
0d3e5a2e 583
8fd456ec
SK
584dev_sysfs_state_synced_failed:
585 device_remove_groups(dev, drv->dev_groups);
23b69044
DT
586dev_groups_failed:
587 if (dev->bus->remove)
588 dev->bus->remove(dev);
589 else if (drv->remove)
590 drv->remove(dev);
d779249e 591probe_failed:
14b6257a
AS
592 if (dev->bus)
593 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
594 BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
595pinctrl_bind_failed:
9ed98953 596 device_links_no_driver(dev);
9ac7849e 597 devres_release_all(dev);
0b777eee 598 arch_teardown_dma_ops(dev);
1901fb26
KS
599 driver_sysfs_remove(dev);
600 dev->driver = NULL;
0998d063 601 dev_set_drvdata(dev, NULL);
e90d5532
RW
602 if (dev->pm_domain && dev->pm_domain->dismiss)
603 dev->pm_domain->dismiss(dev);
5de85b9d 604 pm_runtime_reinit(dev);
08810a41 605 dev_pm_set_driver_flags(dev, 0);
1901fb26 606
bb2b4075
SS
607 switch (ret) {
608 case -EPROBE_DEFER:
d1c3414c 609 /* Driver requested deferred probing */
13fcffbb 610 dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
0ff26c66 611 driver_deferred_probe_add_trigger(dev, local_trigger_count);
bb2b4075
SS
612 break;
613 case -ENODEV:
614 case -ENXIO:
615 pr_debug("%s: probe of %s rejects match %d\n",
616 drv->name, dev_name(dev), ret);
617 break;
618 default:
0d3e5a2e 619 /* driver matched but the probe failed */
eb7fbc9f
CJ
620 pr_warn("%s: probe of %s failed with error %d\n",
621 drv->name, dev_name(dev), ret);
0d3e5a2e 622 }
c578abbc
CH
623 /*
624 * Ignore errors returned by ->probe so that the next driver can try
625 * its luck.
626 */
627 ret = 0;
d779249e 628done:
d779249e 629 atomic_dec(&probe_count);
735a7ffb 630 wake_up(&probe_waitqueue);
d779249e
GKH
631 return ret;
632}
633
0a50f61c
TP
634/*
635 * For initcall_debug, show the driver probe time.
636 */
637static int really_probe_debug(struct device *dev, struct device_driver *drv)
638{
639 ktime_t calltime, delta, rettime;
640 int ret;
641
642 calltime = ktime_get();
643 ret = really_probe(dev, drv);
644 rettime = ktime_get();
645 delta = ktime_sub(rettime, calltime);
eb7fbc9f
CJ
646 pr_debug("probe of %s returned %d after %lld usecs\n",
647 dev_name(dev), ret, (s64) ktime_to_us(delta));
0a50f61c
TP
648 return ret;
649}
650
d779249e
GKH
651/**
652 * driver_probe_done
653 * Determine if the probe sequence is finished or not.
654 *
655 * Should somehow figure out how to use a semaphore, not an atomic variable...
656 */
657int driver_probe_done(void)
658{
927f8287
AS
659 int local_probe_count = atomic_read(&probe_count);
660
661 pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
662 if (local_probe_count)
d779249e
GKH
663 return -EBUSY;
664 return 0;
665}
666
216773a7
AV
667/**
668 * wait_for_device_probe
669 * Wait for device probing to be completed.
216773a7 670 */
b23530eb 671void wait_for_device_probe(void)
216773a7 672{
35a67236
JS
673 /* wait for probe timeout */
674 wait_event(probe_timeout_waitqueue, !driver_deferred_probe_timeout);
675
013c074f 676 /* wait for the deferred probe workqueue to finish */
2c507e46 677 flush_work(&deferred_probe_work);
013c074f 678
216773a7 679 /* wait for the known devices to complete their probing */
b23530eb 680 wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
216773a7 681 async_synchronize_full();
216773a7 682}
d4d5291c 683EXPORT_SYMBOL_GPL(wait_for_device_probe);
216773a7 684
d779249e
GKH
685/**
686 * driver_probe_device - attempt to bind device & driver together
687 * @drv: driver to bind a device to
688 * @dev: device to try to bind to the driver
689 *
49b420a1 690 * This function returns -ENODEV if the device is not registered,
af901ca1 691 * 1 if the device is bound successfully and 0 otherwise.
d779249e 692 *
8e9394ce
GKH
693 * This function must be called with @dev lock held. When called for a
694 * USB interface, @dev->parent lock must be held as well.
ddef08dd
RW
695 *
696 * If the device has a parent, runtime-resume the parent before driver probing.
d779249e 697 */
4a3ad20c 698int driver_probe_device(struct device_driver *drv, struct device *dev)
d779249e 699{
d779249e
GKH
700 int ret = 0;
701
f2eaae19
AS
702 if (!device_is_registered(dev))
703 return -ENODEV;
d779249e 704
7dc72b28 705 pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
1e0b2cf9 706 drv->bus->name, __func__, dev_name(dev), drv->name);
d779249e 707
b06c0b2f 708 pm_runtime_get_suppliers(dev);
ddef08dd
RW
709 if (dev->parent)
710 pm_runtime_get_sync(dev->parent);
711
5e928f77 712 pm_runtime_barrier(dev);
0a50f61c
TP
713 if (initcall_debug)
714 ret = really_probe_debug(dev, drv);
715 else
716 ret = really_probe(dev, drv);
fa180eb4 717 pm_request_idle(dev);
d779249e 718
ddef08dd
RW
719 if (dev->parent)
720 pm_runtime_put(dev->parent);
721
b06c0b2f 722 pm_runtime_put_suppliers(dev);
0d3e5a2e 723 return ret;
07e4a3e2 724}
725
1ea61b68
FT
726static inline bool cmdline_requested_async_probing(const char *drv_name)
727{
728 return parse_option_str(async_probe_drv_names, drv_name);
729}
730
731/* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
732static int __init save_async_options(char *buf)
733{
734 if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
eb7fbc9f 735 pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
1ea61b68
FT
736
737 strlcpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
738 return 0;
739}
740__setup("driver_async_probe=", save_async_options);
741
765230b5 742bool driver_allows_async_probing(struct device_driver *drv)
2287c322 743{
d173a137
LR
744 switch (drv->probe_type) {
745 case PROBE_PREFER_ASYNCHRONOUS:
f2411da7
LR
746 return true;
747
d173a137
LR
748 case PROBE_FORCE_SYNCHRONOUS:
749 return false;
750
751 default:
1ea61b68
FT
752 if (cmdline_requested_async_probing(drv->name))
753 return true;
754
80c6e146 755 if (module_requested_async_probing(drv->owner))
d173a137 756 return true;
f2411da7 757
d173a137
LR
758 return false;
759 }
765230b5
DT
760}
761
762struct device_attach_data {
763 struct device *dev;
764
765 /*
766 * Indicates whether we are are considering asynchronous probing or
767 * not. Only initial binding after device or driver registration
768 * (including deferral processing) may be done asynchronously, the
769 * rest is always synchronous, as we expect it is being done by
770 * request from userspace.
771 */
772 bool check_async;
773
774 /*
775 * Indicates if we are binding synchronous or asynchronous drivers.
776 * When asynchronous probing is enabled we'll execute 2 passes
777 * over drivers: first pass doing synchronous probing and second
778 * doing asynchronous probing (if synchronous did not succeed -
779 * most likely because there was no driver requiring synchronous
780 * probing - and we found asynchronous driver during first pass).
781 * The 2 passes are done because we can't shoot asynchronous
782 * probe for given device and driver from bus_for_each_drv() since
783 * driver pointer is not guaranteed to stay valid once
784 * bus_for_each_drv() iterates to the next driver on the bus.
785 */
786 bool want_async;
787
788 /*
789 * We'll set have_async to 'true' if, while scanning for matching
790 * driver, we'll encounter one that requests asynchronous probing.
791 */
792 bool have_async;
793};
794
795static int __device_attach_driver(struct device_driver *drv, void *_data)
796{
797 struct device_attach_data *data = _data;
798 struct device *dev = data->dev;
799 bool async_allowed;
656b8035 800 int ret;
765230b5 801
656b8035
TV
802 ret = driver_match_device(drv, dev);
803 if (ret == 0) {
804 /* no match */
49b420a1 805 return 0;
656b8035
TV
806 } else if (ret == -EPROBE_DEFER) {
807 dev_dbg(dev, "Device match requests probe deferral\n");
808 driver_deferred_probe_add(dev);
809 } else if (ret < 0) {
eb7fbc9f 810 dev_dbg(dev, "Bus failed to match device: %d\n", ret);
656b8035
TV
811 return ret;
812 } /* ret > 0 means positive match */
49b420a1 813
765230b5
DT
814 async_allowed = driver_allows_async_probing(drv);
815
816 if (async_allowed)
817 data->have_async = true;
818
819 if (data->check_async && async_allowed != data->want_async)
820 return 0;
821
0d3e5a2e 822 return driver_probe_device(drv, dev);
2287c322 823}
824
765230b5
DT
825static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
826{
827 struct device *dev = _dev;
828 struct device_attach_data data = {
829 .dev = dev,
830 .check_async = true,
831 .want_async = true,
832 };
833
834 device_lock(dev);
835
3451a495
AD
836 /*
837 * Check if device has already been removed or claimed. This may
838 * happen with driver loading, device discovery/registration,
839 * and deferred probe processing happens all at once with
840 * multiple threads.
841 */
842 if (dev->p->dead || dev->driver)
843 goto out_unlock;
844
ddef08dd
RW
845 if (dev->parent)
846 pm_runtime_get_sync(dev->parent);
847
765230b5
DT
848 bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
849 dev_dbg(dev, "async probe completed\n");
850
851 pm_request_idle(dev);
852
ddef08dd
RW
853 if (dev->parent)
854 pm_runtime_put(dev->parent);
3451a495 855out_unlock:
765230b5
DT
856 device_unlock(dev);
857
858 put_device(dev);
859}
860
802a87fd 861static int __device_attach(struct device *dev, bool allow_async)
07e4a3e2 862{
0d3e5a2e
PM
863 int ret = 0;
864
8e9394ce 865 device_lock(dev);
07e4a3e2 866 if (dev->driver) {
6b9cb427 867 if (device_is_bound(dev)) {
8497d6a2
SO
868 ret = 1;
869 goto out_unlock;
870 }
f86db396
AM
871 ret = device_bind_driver(dev);
872 if (ret == 0)
873 ret = 1;
c6a46696
CH
874 else {
875 dev->driver = NULL;
876 ret = 0;
877 }
21c7f30b 878 } else {
765230b5
DT
879 struct device_attach_data data = {
880 .dev = dev,
881 .check_async = allow_async,
882 .want_async = false,
883 };
884
ddef08dd
RW
885 if (dev->parent)
886 pm_runtime_get_sync(dev->parent);
887
765230b5
DT
888 ret = bus_for_each_drv(dev->bus, NULL, &data,
889 __device_attach_driver);
890 if (!ret && allow_async && data.have_async) {
891 /*
892 * If we could not find appropriate driver
893 * synchronously and we are allowed to do
894 * async probes and there are drivers that
895 * want to probe asynchronously, we'll
896 * try them.
897 */
898 dev_dbg(dev, "scheduling asynchronous probe\n");
899 get_device(dev);
c37e20ea 900 async_schedule_dev(__device_attach_async_helper, dev);
765230b5
DT
901 } else {
902 pm_request_idle(dev);
903 }
ddef08dd
RW
904
905 if (dev->parent)
906 pm_runtime_put(dev->parent);
21c7f30b 907 }
8497d6a2 908out_unlock:
8e9394ce 909 device_unlock(dev);
0d3e5a2e 910 return ret;
2287c322 911}
765230b5
DT
912
913/**
914 * device_attach - try to attach device to a driver.
915 * @dev: device.
916 *
917 * Walk the list of drivers that the bus has and call
918 * driver_probe_device() for each pair. If a compatible
919 * pair is found, break out and return.
920 *
921 * Returns 1 if the device was bound to a driver;
922 * 0 if no matching driver was found;
923 * -ENODEV if the device is not registered.
924 *
925 * When called for a USB interface, @dev->parent lock must be held.
926 */
927int device_attach(struct device *dev)
928{
929 return __device_attach(dev, false);
930}
4a3ad20c 931EXPORT_SYMBOL_GPL(device_attach);
2287c322 932
765230b5
DT
933void device_initial_probe(struct device *dev)
934{
935 __device_attach(dev, true);
936}
937
ed88747c
AD
938/*
939 * __device_driver_lock - acquire locks needed to manipulate dev->drv
940 * @dev: Device we will update driver info for
941 * @parent: Parent device. Needed if the bus requires parent lock
942 *
943 * This function will take the required locks for manipulating dev->drv.
944 * Normally this will just be the @dev lock, but when called for a USB
945 * interface, @parent lock will be held as well.
946 */
947static void __device_driver_lock(struct device *dev, struct device *parent)
948{
949 if (parent && dev->bus->need_parent_lock)
950 device_lock(parent);
951 device_lock(dev);
952}
953
954/*
955 * __device_driver_unlock - release locks needed to manipulate dev->drv
956 * @dev: Device we will update driver info for
957 * @parent: Parent device. Needed if the bus requires parent lock
958 *
959 * This function will release the required locks for manipulating dev->drv.
960 * Normally this will just be the the @dev lock, but when called for a
961 * USB interface, @parent lock will be released as well.
962 */
963static void __device_driver_unlock(struct device *dev, struct device *parent)
964{
965 device_unlock(dev);
966 if (parent && dev->bus->need_parent_lock)
967 device_unlock(parent);
968}
969
970/**
971 * device_driver_attach - attach a specific driver to a specific device
972 * @drv: Driver to attach
973 * @dev: Device to attach it to
974 *
975 * Manually attach driver to a device. Will acquire both @dev lock and
976 * @dev->parent lock if needed.
977 */
978int device_driver_attach(struct device_driver *drv, struct device *dev)
979{
980 int ret = 0;
981
982 __device_driver_lock(dev, dev->parent);
983
984 /*
985 * If device has been removed or someone has already successfully
986 * bound a driver before us just skip the driver probe call.
987 */
988 if (!dev->p->dead && !dev->driver)
989 ret = driver_probe_device(drv, dev);
990
991 __device_driver_unlock(dev, dev->parent);
992
993 return ret;
994}
995
ef0ff683
AD
996static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
997{
998 struct device *dev = _dev;
999 struct device_driver *drv;
1000 int ret = 0;
1001
1002 __device_driver_lock(dev, dev->parent);
1003
1004 drv = dev->p->async_driver;
1005
1006 /*
1007 * If device has been removed or someone has already successfully
1008 * bound a driver before us just skip the driver probe call.
1009 */
1010 if (!dev->p->dead && !dev->driver)
1011 ret = driver_probe_device(drv, dev);
1012
1013 __device_driver_unlock(dev, dev->parent);
1014
1015 dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
1016
1017 put_device(dev);
1018}
1019
4a3ad20c 1020static int __driver_attach(struct device *dev, void *data)
2287c322 1021{
4a3ad20c 1022 struct device_driver *drv = data;
656b8035 1023 int ret;
0d3e5a2e
PM
1024
1025 /*
1026 * Lock device and try to bind to it. We drop the error
1027 * here and always return 0, because we need to keep trying
1028 * to bind to devices and some drivers will return an error
1029 * simply if it didn't support the device.
1030 *
1031 * driver_probe_device() will spit a warning if there
1032 * is an error.
1033 */
1034
656b8035
TV
1035 ret = driver_match_device(drv, dev);
1036 if (ret == 0) {
1037 /* no match */
6cd49586 1038 return 0;
656b8035
TV
1039 } else if (ret == -EPROBE_DEFER) {
1040 dev_dbg(dev, "Device match requests probe deferral\n");
1041 driver_deferred_probe_add(dev);
1042 } else if (ret < 0) {
eb7fbc9f 1043 dev_dbg(dev, "Bus failed to match device: %d\n", ret);
656b8035
TV
1044 return ret;
1045 } /* ret > 0 means positive match */
6cd49586 1046
ef0ff683
AD
1047 if (driver_allows_async_probing(drv)) {
1048 /*
1049 * Instead of probing the device synchronously we will
1050 * probe it asynchronously to allow for more parallelism.
1051 *
1052 * We only take the device lock here in order to guarantee
1053 * that the dev->driver and async_driver fields are protected
1054 */
1055 dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
1056 device_lock(dev);
1057 if (!dev->driver) {
1058 get_device(dev);
1059 dev->p->async_driver = drv;
c37e20ea 1060 async_schedule_dev(__driver_attach_async_helper, dev);
ef0ff683
AD
1061 }
1062 device_unlock(dev);
1063 return 0;
1064 }
1065
ed88747c 1066 device_driver_attach(drv, dev);
0d3e5a2e 1067
07e4a3e2 1068 return 0;
1069}
1070
1071/**
4a3ad20c
GKH
1072 * driver_attach - try to bind driver to devices.
1073 * @drv: driver.
07e4a3e2 1074 *
4a3ad20c
GKH
1075 * Walk the list of devices that the bus has on it and try to
1076 * match the driver with each one. If driver_probe_device()
1077 * returns 0 and the @dev->driver is set, we've found a
1078 * compatible pair.
07e4a3e2 1079 */
4a3ad20c 1080int driver_attach(struct device_driver *drv)
07e4a3e2 1081{
f86db396 1082 return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
07e4a3e2 1083}
4a3ad20c 1084EXPORT_SYMBOL_GPL(driver_attach);
07e4a3e2 1085
ab71c6f0 1086/*
8e9394ce
GKH
1087 * __device_release_driver() must be called with @dev lock held.
1088 * When called for a USB interface, @dev->parent lock must be held as well.
07e4a3e2 1089 */
9ed98953 1090static void __device_release_driver(struct device *dev, struct device *parent)
07e4a3e2 1091{
4a3ad20c 1092 struct device_driver *drv;
07e4a3e2 1093
ef2c5174 1094 drv = dev->driver;
c95a6b05 1095 if (drv) {
9ed98953 1096 while (device_links_busy(dev)) {
ed88747c 1097 __device_driver_unlock(dev, parent);
9ed98953
RW
1098
1099 device_links_unbind_consumers(dev);
9ed98953 1100
ed88747c 1101 __device_driver_lock(dev, parent);
9ed98953
RW
1102 /*
1103 * A concurrent invocation of the same function might
1104 * have released the driver successfully while this one
1105 * was waiting, so check for that.
1106 */
1107 if (dev->driver != drv)
1108 return;
1109 }
1110
e1866b33 1111 pm_runtime_get_sync(dev);
21d5c57b 1112 pm_runtime_clean_up_links(dev);
5e928f77 1113
1901fb26 1114 driver_sysfs_remove(dev);
0d3e5a2e 1115
116af378 1116 if (dev->bus)
c6f7e72a 1117 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
116af378
BH
1118 BUS_NOTIFY_UNBIND_DRIVER,
1119 dev);
1120
baab52de 1121 pm_runtime_put_sync(dev);
e1866b33 1122
8fd456ec 1123 device_remove_file(dev, &dev_attr_state_synced);
23b69044
DT
1124 device_remove_groups(dev, drv->dev_groups);
1125
0f836ca4 1126 if (dev->bus && dev->bus->remove)
594c8281
RK
1127 dev->bus->remove(dev);
1128 else if (drv->remove)
0d3e5a2e 1129 drv->remove(dev);
9ed98953
RW
1130
1131 device_links_driver_cleanup(dev);
09515ef5 1132
9ac7849e 1133 devres_release_all(dev);
376991db 1134 arch_teardown_dma_ops(dev);
0d3e5a2e 1135 dev->driver = NULL;
0998d063 1136 dev_set_drvdata(dev, NULL);
e90d5532
RW
1137 if (dev->pm_domain && dev->pm_domain->dismiss)
1138 dev->pm_domain->dismiss(dev);
5de85b9d 1139 pm_runtime_reinit(dev);
08810a41 1140 dev_pm_set_driver_flags(dev, 0);
e90d5532 1141
8940b4f3 1142 klist_remove(&dev->p->knode_driver);
aa8e54b5 1143 device_pm_check_callbacks(dev);
309b7d60
JR
1144 if (dev->bus)
1145 blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
1146 BUS_NOTIFY_UNBOUND_DRIVER,
1147 dev);
1455cf8d
DT
1148
1149 kobject_uevent(&dev->kobj, KOBJ_UNBIND);
0d3e5a2e 1150 }
07e4a3e2 1151}
1152
9ed98953
RW
1153void device_release_driver_internal(struct device *dev,
1154 struct device_driver *drv,
1155 struct device *parent)
4bdb3550 1156{
ed88747c 1157 __device_driver_lock(dev, parent);
4bdb3550 1158
4bdb3550 1159 if (!drv || drv == dev->driver)
9ed98953 1160 __device_release_driver(dev, parent);
4bdb3550 1161
ed88747c 1162 __device_driver_unlock(dev, parent);
4bdb3550
RW
1163}
1164
ab71c6f0 1165/**
4a3ad20c
GKH
1166 * device_release_driver - manually detach device from driver.
1167 * @dev: device.
ab71c6f0 1168 *
4a3ad20c 1169 * Manually detach device from driver.
8e9394ce 1170 * When called for a USB interface, @dev->parent lock must be held.
9ed98953
RW
1171 *
1172 * If this function is to be called with @dev->parent lock held, ensure that
1173 * the device's consumers are unbound in advance or that their locks can be
1174 * acquired under the @dev->parent lock.
ab71c6f0 1175 */
4a3ad20c 1176void device_release_driver(struct device *dev)
94e7b1c5 1177{
c95a6b05
AS
1178 /*
1179 * If anyone calls device_release_driver() recursively from
1180 * within their ->remove callback for the same device, they
1181 * will deadlock right here.
1182 */
4bdb3550 1183 device_release_driver_internal(dev, NULL, NULL);
94e7b1c5 1184}
4a3ad20c 1185EXPORT_SYMBOL_GPL(device_release_driver);
c95a6b05 1186
ed88747c
AD
1187/**
1188 * device_driver_detach - detach driver from a specific device
1189 * @dev: device to detach driver from
1190 *
1191 * Detach driver from device. Will acquire both @dev lock and @dev->parent
1192 * lock if needed.
1193 */
1194void device_driver_detach(struct device *dev)
1195{
1196 device_release_driver_internal(dev, NULL, dev->parent);
1197}
1198
07e4a3e2 1199/**
1200 * driver_detach - detach driver from all devices it controls.
1201 * @drv: driver.
1202 */
4a3ad20c 1203void driver_detach(struct device_driver *drv)
07e4a3e2 1204{
8940b4f3 1205 struct device_private *dev_prv;
4a3ad20c 1206 struct device *dev;
c95a6b05 1207
c37d721c
AD
1208 if (driver_allows_async_probing(drv))
1209 async_synchronize_full();
1210
c95a6b05 1211 for (;;) {
e5dd1278
GKH
1212 spin_lock(&drv->p->klist_devices.k_lock);
1213 if (list_empty(&drv->p->klist_devices.k_list)) {
1214 spin_unlock(&drv->p->klist_devices.k_lock);
c95a6b05
AS
1215 break;
1216 }
a3a87d66 1217 dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
8940b4f3
GKH
1218 struct device_private,
1219 knode_driver.n_node);
1220 dev = dev_prv->device;
c95a6b05 1221 get_device(dev);
e5dd1278 1222 spin_unlock(&drv->p->klist_devices.k_lock);
4bdb3550 1223 device_release_driver_internal(dev, drv, dev->parent);
c95a6b05
AS
1224 put_device(dev);
1225 }
07e4a3e2 1226}