1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * drivers/base/dd.c - The core device/driver interactions. 4 * 5 * This file contains the (sometimes tricky) code that controls the 6 * interactions between devices and drivers, which primarily includes 7 * driver binding and unbinding. 8 * 9 * All of this code used to exist in drivers/base/bus.c, but was 10 * relocated to here in the name of compartmentalization (since it wasn't 11 * strictly code just for the 'struct bus_type'. 12 * 13 * Copyright (c) 2002-5 Patrick Mochel 14 * Copyright (c) 2002-3 Open Source Development Labs 15 * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de> 16 * Copyright (c) 2007-2009 Novell Inc. 17 */ 18 19 #include <linux/debugfs.h> 20 #include <linux/device.h> 21 #include <linux/delay.h> 22 #include <linux/dma-map-ops.h> 23 #include <linux/init.h> 24 #include <linux/module.h> 25 #include <linux/kthread.h> 26 #include <linux/wait.h> 27 #include <linux/async.h> 28 #include <linux/pm_domain.h> 29 #include <linux/pm_runtime.h> 30 #include <linux/pinctrl/devinfo.h> 31 #include <linux/slab.h> 32 33 #include "base.h" 34 #include "power/power.h" 35 36 /* 37 * Deferred Probe infrastructure. 38 * 39 * Sometimes driver probe order matters, but the kernel doesn't always have 40 * dependency information which means some drivers will get probed before a 41 * resource it depends on is available. For example, an SDHCI driver may 42 * first need a GPIO line from an i2c GPIO controller before it can be 43 * initialized. If a required resource is not available yet, a driver can 44 * request probing to be deferred by returning -EPROBE_DEFER from its probe hook 45 * 46 * Deferred probe maintains two lists of devices, a pending list and an active 47 * list. A driver returning -EPROBE_DEFER causes the device to be added to the 48 * pending list. A successful driver probe will trigger moving all devices 49 * from the pending to the active list so that the workqueue will eventually 50 * retry them. 51 * 52 * The deferred_probe_mutex must be held any time the deferred_probe_*_list 53 * of the (struct device*)->p->deferred_probe pointers are manipulated 54 */ 55 static DEFINE_MUTEX(deferred_probe_mutex); 56 static LIST_HEAD(deferred_probe_pending_list); 57 static LIST_HEAD(deferred_probe_active_list); 58 static atomic_t deferred_trigger_count = ATOMIC_INIT(0); 59 static bool initcalls_done; 60 61 /* Save the async probe drivers' name from kernel cmdline */ 62 #define ASYNC_DRV_NAMES_MAX_LEN 256 63 static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN]; 64 static bool async_probe_default; 65 66 /* 67 * In some cases, like suspend to RAM or hibernation, It might be reasonable 68 * to prohibit probing of devices as it could be unsafe. 69 * Once defer_all_probes is true all drivers probes will be forcibly deferred. 70 */ 71 static bool defer_all_probes; 72 73 static void __device_set_deferred_probe_reason(const struct device *dev, char *reason) 74 { 75 kfree(dev->p->deferred_probe_reason); 76 dev->p->deferred_probe_reason = reason; 77 } 78 79 /* 80 * deferred_probe_work_func() - Retry probing devices in the active list. 81 */ 82 static void deferred_probe_work_func(struct work_struct *work) 83 { 84 struct device *dev; 85 struct device_private *private; 86 /* 87 * This block processes every device in the deferred 'active' list. 88 * Each device is removed from the active list and passed to 89 * bus_probe_device() to re-attempt the probe. The loop continues 90 * until every device in the active list is removed and retried. 91 * 92 * Note: Once the device is removed from the list and the mutex is 93 * released, it is possible for the device get freed by another thread 94 * and cause a illegal pointer dereference. This code uses 95 * get/put_device() to ensure the device structure cannot disappear 96 * from under our feet. 97 */ 98 mutex_lock(&deferred_probe_mutex); 99 while (!list_empty(&deferred_probe_active_list)) { 100 private = list_first_entry(&deferred_probe_active_list, 101 typeof(*dev->p), deferred_probe); 102 dev = private->device; 103 list_del_init(&private->deferred_probe); 104 105 get_device(dev); 106 107 __device_set_deferred_probe_reason(dev, NULL); 108 109 /* 110 * Drop the mutex while probing each device; the probe path may 111 * manipulate the deferred list 112 */ 113 mutex_unlock(&deferred_probe_mutex); 114 115 /* 116 * Force the device to the end of the dpm_list since 117 * the PM code assumes that the order we add things to 118 * the list is a good order for suspend but deferred 119 * probe makes that very unsafe. 120 */ 121 device_pm_move_to_tail(dev); 122 123 dev_dbg(dev, "Retrying from deferred list\n"); 124 bus_probe_device(dev); 125 mutex_lock(&deferred_probe_mutex); 126 127 put_device(dev); 128 } 129 mutex_unlock(&deferred_probe_mutex); 130 } 131 static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func); 132 133 void driver_deferred_probe_add(struct device *dev) 134 { 135 if (!dev->can_match) 136 return; 137 138 mutex_lock(&deferred_probe_mutex); 139 if (list_empty(&dev->p->deferred_probe)) { 140 dev_dbg(dev, "Added to deferred list\n"); 141 list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list); 142 } 143 mutex_unlock(&deferred_probe_mutex); 144 } 145 146 void driver_deferred_probe_del(struct device *dev) 147 { 148 mutex_lock(&deferred_probe_mutex); 149 if (!list_empty(&dev->p->deferred_probe)) { 150 dev_dbg(dev, "Removed from deferred list\n"); 151 list_del_init(&dev->p->deferred_probe); 152 __device_set_deferred_probe_reason(dev, NULL); 153 } 154 mutex_unlock(&deferred_probe_mutex); 155 } 156 157 static bool driver_deferred_probe_enable; 158 /** 159 * driver_deferred_probe_trigger() - Kick off re-probing deferred devices 160 * 161 * This functions moves all devices from the pending list to the active 162 * list and schedules the deferred probe workqueue to process them. It 163 * should be called anytime a driver is successfully bound to a device. 164 * 165 * Note, there is a race condition in multi-threaded probe. In the case where 166 * more than one device is probing at the same time, it is possible for one 167 * probe to complete successfully while another is about to defer. If the second 168 * depends on the first, then it will get put on the pending list after the 169 * trigger event has already occurred and will be stuck there. 170 * 171 * The atomic 'deferred_trigger_count' is used to determine if a successful 172 * trigger has occurred in the midst of probing a driver. If the trigger count 173 * changes in the midst of a probe, then deferred processing should be triggered 174 * again. 175 */ 176 void driver_deferred_probe_trigger(void) 177 { 178 if (!driver_deferred_probe_enable) 179 return; 180 181 /* 182 * A successful probe means that all the devices in the pending list 183 * should be triggered to be reprobed. Move all the deferred devices 184 * into the active list so they can be retried by the workqueue 185 */ 186 mutex_lock(&deferred_probe_mutex); 187 atomic_inc(&deferred_trigger_count); 188 list_splice_tail_init(&deferred_probe_pending_list, 189 &deferred_probe_active_list); 190 mutex_unlock(&deferred_probe_mutex); 191 192 /* 193 * Kick the re-probe thread. It may already be scheduled, but it is 194 * safe to kick it again. 195 */ 196 queue_work(system_dfl_wq, &deferred_probe_work); 197 } 198 199 /** 200 * device_block_probing() - Block/defer device's probes 201 * 202 * It will disable probing of devices and defer their probes instead. 203 */ 204 void device_block_probing(void) 205 { 206 defer_all_probes = true; 207 /* sync with probes to avoid races. */ 208 wait_for_device_probe(); 209 } 210 211 /** 212 * device_unblock_probing() - Unblock/enable device's probes 213 * 214 * It will restore normal behavior and trigger re-probing of deferred 215 * devices. 216 */ 217 void device_unblock_probing(void) 218 { 219 defer_all_probes = false; 220 driver_deferred_probe_trigger(); 221 } 222 223 /** 224 * device_set_deferred_probe_reason() - Set defer probe reason message for device 225 * @dev: the pointer to the struct device 226 * @vaf: the pointer to va_format structure with message 227 */ 228 void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf) 229 { 230 const char *drv = dev_driver_string(dev); 231 char *reason; 232 233 mutex_lock(&deferred_probe_mutex); 234 235 reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf); 236 __device_set_deferred_probe_reason(dev, reason); 237 238 mutex_unlock(&deferred_probe_mutex); 239 } 240 241 /* 242 * deferred_devs_show() - Show the devices in the deferred probe pending list. 243 */ 244 static int deferred_devs_show(struct seq_file *s, void *data) 245 { 246 struct device_private *curr; 247 248 mutex_lock(&deferred_probe_mutex); 249 250 list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe) 251 seq_printf(s, "%s\t%s", dev_name(curr->device), 252 curr->deferred_probe_reason ?: "\n"); 253 254 mutex_unlock(&deferred_probe_mutex); 255 256 return 0; 257 } 258 DEFINE_SHOW_ATTRIBUTE(deferred_devs); 259 260 static int driver_deferred_probe_timeout = CONFIG_DRIVER_DEFERRED_PROBE_TIMEOUT; 261 262 static int __init deferred_probe_timeout_setup(char *str) 263 { 264 int timeout; 265 266 if (!kstrtoint(str, 10, &timeout)) 267 driver_deferred_probe_timeout = timeout; 268 return 1; 269 } 270 __setup("deferred_probe_timeout=", deferred_probe_timeout_setup); 271 272 /** 273 * driver_deferred_probe_check_state() - Check deferred probe state 274 * @dev: device to check 275 * 276 * Return: 277 * * -ENODEV if initcalls have completed and modules are disabled. 278 * * -ETIMEDOUT if the deferred probe timeout was set and has expired 279 * and modules are enabled. 280 * * -EPROBE_DEFER in other cases. 281 * 282 * Drivers or subsystems can opt-in to calling this function instead of directly 283 * returning -EPROBE_DEFER. 284 */ 285 int driver_deferred_probe_check_state(struct device *dev) 286 { 287 if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) { 288 dev_warn(dev, "ignoring dependency for device, assuming no driver\n"); 289 return -ENODEV; 290 } 291 292 if (!driver_deferred_probe_timeout && initcalls_done) { 293 dev_warn(dev, "deferred probe timeout, ignoring dependency\n"); 294 return -ETIMEDOUT; 295 } 296 297 return -EPROBE_DEFER; 298 } 299 EXPORT_SYMBOL_GPL(driver_deferred_probe_check_state); 300 301 static void deferred_probe_timeout_work_func(struct work_struct *work) 302 { 303 struct device_private *p; 304 305 fw_devlink_drivers_done(); 306 307 driver_deferred_probe_timeout = 0; 308 driver_deferred_probe_trigger(); 309 flush_work(&deferred_probe_work); 310 311 mutex_lock(&deferred_probe_mutex); 312 list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe) 313 dev_warn(p->device, "deferred probe pending: %s", p->deferred_probe_reason ?: "(reason unknown)\n"); 314 mutex_unlock(&deferred_probe_mutex); 315 316 fw_devlink_probing_done(); 317 } 318 static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func); 319 320 void deferred_probe_extend_timeout(void) 321 { 322 /* 323 * If the work hasn't been queued yet or if the work expired, don't 324 * start a new one. 325 */ 326 if (cancel_delayed_work(&deferred_probe_timeout_work)) { 327 schedule_delayed_work(&deferred_probe_timeout_work, 328 driver_deferred_probe_timeout * HZ); 329 pr_debug("Extended deferred probe timeout by %d secs\n", 330 driver_deferred_probe_timeout); 331 } 332 } 333 334 /** 335 * deferred_probe_initcall() - Enable probing of deferred devices 336 * 337 * We don't want to get in the way when the bulk of drivers are getting probed. 338 * Instead, this initcall makes sure that deferred probing is delayed until 339 * late_initcall time. 340 */ 341 static int deferred_probe_initcall(void) 342 { 343 debugfs_create_file("devices_deferred", 0444, NULL, NULL, 344 &deferred_devs_fops); 345 346 driver_deferred_probe_enable = true; 347 driver_deferred_probe_trigger(); 348 /* Sort as many dependencies as possible before exiting initcalls */ 349 flush_work(&deferred_probe_work); 350 initcalls_done = true; 351 352 if (!IS_ENABLED(CONFIG_MODULES)) 353 fw_devlink_drivers_done(); 354 355 /* 356 * Trigger deferred probe again, this time we won't defer anything 357 * that is optional 358 */ 359 driver_deferred_probe_trigger(); 360 flush_work(&deferred_probe_work); 361 362 if (driver_deferred_probe_timeout > 0) { 363 schedule_delayed_work(&deferred_probe_timeout_work, 364 driver_deferred_probe_timeout * HZ); 365 } 366 367 if (!IS_ENABLED(CONFIG_MODULES)) 368 fw_devlink_probing_done(); 369 370 return 0; 371 } 372 late_initcall(deferred_probe_initcall); 373 374 static void __exit deferred_probe_exit(void) 375 { 376 debugfs_lookup_and_remove("devices_deferred", NULL); 377 } 378 __exitcall(deferred_probe_exit); 379 380 int __device_set_driver_override(struct device *dev, const char *s, size_t len) 381 { 382 const char *new = NULL, *old; 383 384 if (!s) 385 return -EINVAL; 386 387 /* 388 * The stored value will be used in sysfs show callback (sysfs_emit()), 389 * which has a length limit of PAGE_SIZE and adds a trailing newline. 390 * Thus we can store one character less to avoid truncation during sysfs 391 * show. 392 */ 393 if (len >= (PAGE_SIZE - 1)) 394 return -EINVAL; 395 396 /* 397 * Compute the real length of the string in case userspace sends us a 398 * bunch of \0 characters like python likes to do. 399 */ 400 len = strlen(s); 401 402 /* Handle trailing newline */ 403 if (len) { 404 char *cp; 405 406 cp = strnchr(s, len, '\n'); 407 if (cp) 408 len = cp - s; 409 } 410 411 /* 412 * If empty string or "\n" passed, new remains NULL, clearing 413 * the driver_override.name. 414 */ 415 if (len) { 416 new = kstrndup(s, len, GFP_KERNEL); 417 if (!new) 418 return -ENOMEM; 419 } 420 421 scoped_guard(spinlock, &dev->driver_override.lock) { 422 old = dev->driver_override.name; 423 dev->driver_override.name = new; 424 } 425 426 kfree(old); 427 428 return 0; 429 } 430 EXPORT_SYMBOL_GPL(__device_set_driver_override); 431 432 /** 433 * device_is_bound() - Check if device is bound to a driver 434 * @dev: device to check 435 * 436 * Returns true if passed device has already finished probing successfully 437 * against a driver. 438 * 439 * This function must be called with the device lock held. 440 */ 441 bool device_is_bound(struct device *dev) 442 { 443 return dev->p && klist_node_attached(&dev->p->knode_driver); 444 } 445 EXPORT_SYMBOL_GPL(device_is_bound); 446 447 static void driver_bound(struct device *dev) 448 { 449 if (device_is_bound(dev)) { 450 dev_warn(dev, "%s: device already bound\n", __func__); 451 return; 452 } 453 454 dev_dbg(dev, "driver: '%s': %s: bound to device\n", dev->driver->name, 455 __func__); 456 457 klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices); 458 device_links_driver_bound(dev); 459 460 device_pm_check_callbacks(dev); 461 462 /* 463 * Make sure the device is no longer in one of the deferred lists and 464 * kick off retrying all pending devices 465 */ 466 driver_deferred_probe_del(dev); 467 driver_deferred_probe_trigger(); 468 469 bus_notify(dev, BUS_NOTIFY_BOUND_DRIVER); 470 kobject_uevent(&dev->kobj, KOBJ_BIND); 471 } 472 473 static ssize_t coredump_store(struct device *dev, struct device_attribute *attr, 474 const char *buf, size_t count) 475 { 476 device_lock(dev); 477 dev->driver->coredump(dev); 478 device_unlock(dev); 479 480 return count; 481 } 482 static DEVICE_ATTR_WO(coredump); 483 484 static int driver_sysfs_add(struct device *dev) 485 { 486 int ret; 487 488 bus_notify(dev, BUS_NOTIFY_BIND_DRIVER); 489 490 ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj, 491 kobject_name(&dev->kobj)); 492 if (ret) 493 goto fail; 494 495 ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj, 496 "driver"); 497 if (ret) 498 goto rm_dev; 499 500 if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump) 501 return 0; 502 503 ret = device_create_file(dev, &dev_attr_coredump); 504 if (!ret) 505 return 0; 506 507 sysfs_remove_link(&dev->kobj, "driver"); 508 509 rm_dev: 510 sysfs_remove_link(&dev->driver->p->kobj, 511 kobject_name(&dev->kobj)); 512 513 fail: 514 return ret; 515 } 516 517 static void driver_sysfs_remove(struct device *dev) 518 { 519 struct device_driver *drv = dev->driver; 520 521 if (drv) { 522 if (drv->coredump) 523 device_remove_file(dev, &dev_attr_coredump); 524 sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj)); 525 sysfs_remove_link(&dev->kobj, "driver"); 526 } 527 } 528 529 /** 530 * device_bind_driver - bind a driver to one device. 531 * @dev: device. 532 * 533 * Allow manual attachment of a driver to a device. 534 * Caller must have already set @dev->driver. 535 * 536 * Note that this does not modify the bus reference count. 537 * Please verify that is accounted for before calling this. 538 * (It is ok to call with no other effort from a driver's probe() method.) 539 * 540 * This function must be called with the device lock held. 541 * 542 * Callers should prefer to use device_driver_attach() instead. 543 */ 544 int device_bind_driver(struct device *dev) 545 { 546 int ret; 547 548 ret = driver_sysfs_add(dev); 549 if (!ret) { 550 device_links_force_bind(dev); 551 driver_bound(dev); 552 } 553 else 554 bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND); 555 return ret; 556 } 557 EXPORT_SYMBOL_GPL(device_bind_driver); 558 559 static atomic_t probe_count = ATOMIC_INIT(0); 560 static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue); 561 562 static ssize_t state_synced_store(struct device *dev, 563 struct device_attribute *attr, 564 const char *buf, size_t count) 565 { 566 int ret = 0; 567 568 if (strcmp("1", buf)) 569 return -EINVAL; 570 571 device_lock(dev); 572 if (!dev->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 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 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 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 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 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 */ 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 */ 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 */ 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 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 /* 840 * In device_add(), the "struct device" gets linked into the subsystem's 841 * list of devices and broadcast to userspace (via uevent) before we're 842 * quite ready to probe. Those open pathways to driver probe before 843 * we've finished enough of device_add() to reliably support probe. 844 * Detect this and tell other pathways to try again later. device_add() 845 * itself will also try to probe immediately after setting 846 * "ready_to_probe". 847 */ 848 if (!dev_ready_to_probe(dev)) 849 return dev_err_probe(dev, -EPROBE_DEFER, "Device not ready to probe\n"); 850 851 /* 852 * Set can_match = true after calling dev_ready_to_probe(), so 853 * driver_deferred_probe_add() won't actually add the device to the 854 * deferred probe list when dev_ready_to_probe() returns false. 855 * 856 * When dev_ready_to_probe() returns false, it means that device_add() 857 * will do another probe() attempt for us. 858 */ 859 dev->can_match = true; 860 dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n", 861 drv->bus->name, __func__, drv->name); 862 863 pm_runtime_get_suppliers(dev); 864 if (dev->parent) 865 pm_runtime_get_sync(dev->parent); 866 867 pm_runtime_barrier(dev); 868 if (initcall_debug) 869 ret = really_probe_debug(dev, drv); 870 else 871 ret = really_probe(dev, drv); 872 pm_request_idle(dev); 873 874 if (dev->parent) 875 pm_runtime_put(dev->parent); 876 877 pm_runtime_put_suppliers(dev); 878 return ret; 879 } 880 881 /** 882 * driver_probe_device - attempt to bind device & driver together 883 * @drv: driver to bind a device to 884 * @dev: device to try to bind to the driver 885 * 886 * This function returns -ENODEV if the device is not registered, -EBUSY if it 887 * already has a driver, 0 if the device is bound successfully and a positive 888 * (inverted) error code for failures from the ->probe method. 889 * 890 * This function must be called with @dev lock held. When called for a 891 * USB interface, @dev->parent lock must be held as well. 892 * 893 * If the device has a parent, runtime-resume the parent before driver probing. 894 */ 895 static int driver_probe_device(const struct device_driver *drv, struct device *dev) 896 { 897 int trigger_count = atomic_read(&deferred_trigger_count); 898 int ret; 899 900 atomic_inc(&probe_count); 901 ret = __driver_probe_device(drv, dev); 902 if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) { 903 driver_deferred_probe_add(dev); 904 905 /* 906 * Did a trigger occur while probing? Need to re-trigger if yes 907 */ 908 if (trigger_count != atomic_read(&deferred_trigger_count) && 909 !defer_all_probes) 910 driver_deferred_probe_trigger(); 911 } 912 atomic_dec(&probe_count); 913 wake_up_all(&probe_waitqueue); 914 return ret; 915 } 916 917 static inline bool cmdline_requested_async_probing(const char *drv_name) 918 { 919 bool async_drv; 920 921 async_drv = parse_option_str(async_probe_drv_names, drv_name); 922 923 return (async_probe_default != async_drv); 924 } 925 926 /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */ 927 static int __init save_async_options(char *buf) 928 { 929 if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN) 930 pr_warn("Too long list of driver names for 'driver_async_probe'!\n"); 931 932 strscpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN); 933 async_probe_default = parse_option_str(async_probe_drv_names, "*"); 934 935 return 1; 936 } 937 __setup("driver_async_probe=", save_async_options); 938 939 static bool driver_allows_async_probing(const struct device_driver *drv) 940 { 941 switch (drv->probe_type) { 942 case PROBE_PREFER_ASYNCHRONOUS: 943 return true; 944 945 case PROBE_FORCE_SYNCHRONOUS: 946 return false; 947 948 default: 949 if (cmdline_requested_async_probing(drv->name)) 950 return true; 951 952 if (module_requested_async_probing(drv->owner)) 953 return true; 954 955 return false; 956 } 957 } 958 959 struct device_attach_data { 960 struct device *dev; 961 962 /* 963 * Indicates whether we are considering asynchronous probing or 964 * not. Only initial binding after device or driver registration 965 * (including deferral processing) may be done asynchronously, the 966 * rest is always synchronous, as we expect it is being done by 967 * request from userspace. 968 */ 969 bool check_async; 970 971 /* 972 * Indicates if we are binding synchronous or asynchronous drivers. 973 * When asynchronous probing is enabled we'll execute 2 passes 974 * over drivers: first pass doing synchronous probing and second 975 * doing asynchronous probing (if synchronous did not succeed - 976 * most likely because there was no driver requiring synchronous 977 * probing - and we found asynchronous driver during first pass). 978 * The 2 passes are done because we can't shoot asynchronous 979 * probe for given device and driver from bus_for_each_drv() since 980 * driver pointer is not guaranteed to stay valid once 981 * bus_for_each_drv() iterates to the next driver on the bus. 982 */ 983 bool want_async; 984 985 /* 986 * We'll set have_async to 'true' if, while scanning for matching 987 * driver, we'll encounter one that requests asynchronous probing. 988 */ 989 bool have_async; 990 }; 991 992 static int __device_attach_driver(struct device_driver *drv, void *_data) 993 { 994 struct device_attach_data *data = _data; 995 struct device *dev = data->dev; 996 bool async_allowed; 997 int ret; 998 999 ret = driver_match_device(drv, dev); 1000 if (ret == 0) { 1001 /* no match */ 1002 return 0; 1003 } else if (ret == -EPROBE_DEFER) { 1004 dev_dbg(dev, "Device match requests probe deferral\n"); 1005 dev->can_match = true; 1006 driver_deferred_probe_add(dev); 1007 /* 1008 * Device can't match with a driver right now, so don't attempt 1009 * to match or bind with other drivers on the bus. 1010 */ 1011 return ret; 1012 } else if (ret < 0) { 1013 dev_dbg(dev, "Bus failed to match device: %d\n", ret); 1014 return ret; 1015 } /* ret > 0 means positive match */ 1016 1017 async_allowed = driver_allows_async_probing(drv); 1018 1019 if (async_allowed) 1020 data->have_async = true; 1021 1022 if (data->check_async && async_allowed != data->want_async) 1023 return 0; 1024 1025 /* 1026 * Ignore errors returned by ->probe so that the next driver can try 1027 * its luck. 1028 */ 1029 ret = driver_probe_device(drv, dev); 1030 if (ret < 0) 1031 return ret; 1032 return ret == 0; 1033 } 1034 1035 static void __device_attach_async_helper(void *_dev, async_cookie_t cookie) 1036 { 1037 struct device *dev = _dev; 1038 struct device_attach_data data = { 1039 .dev = dev, 1040 .check_async = true, 1041 .want_async = true, 1042 }; 1043 1044 device_lock(dev); 1045 1046 /* 1047 * Check if device has already been removed or claimed. This may 1048 * happen with driver loading, device discovery/registration, 1049 * and deferred probe processing happens all at once with 1050 * multiple threads. 1051 */ 1052 if (dev->p->dead || dev->driver) 1053 goto out_unlock; 1054 1055 if (dev->parent) 1056 pm_runtime_get_sync(dev->parent); 1057 1058 bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver); 1059 dev_dbg(dev, "async probe completed\n"); 1060 1061 pm_request_idle(dev); 1062 1063 if (dev->parent) 1064 pm_runtime_put(dev->parent); 1065 out_unlock: 1066 device_unlock(dev); 1067 1068 put_device(dev); 1069 } 1070 1071 static int __device_attach(struct device *dev, bool allow_async) 1072 { 1073 int ret = 0; 1074 bool async = false; 1075 1076 device_lock(dev); 1077 if (dev->p->dead) { 1078 goto out_unlock; 1079 } else if (dev->driver) { 1080 if (device_is_bound(dev)) { 1081 ret = 1; 1082 goto out_unlock; 1083 } 1084 ret = device_bind_driver(dev); 1085 if (ret == 0) 1086 ret = 1; 1087 else { 1088 device_set_driver(dev, NULL); 1089 ret = 0; 1090 } 1091 } else { 1092 struct device_attach_data data = { 1093 .dev = dev, 1094 .check_async = allow_async, 1095 .want_async = false, 1096 }; 1097 1098 if (dev->parent) 1099 pm_runtime_get_sync(dev->parent); 1100 1101 ret = bus_for_each_drv(dev->bus, NULL, &data, 1102 __device_attach_driver); 1103 if (!ret && allow_async && data.have_async) { 1104 /* 1105 * If we could not find appropriate driver 1106 * synchronously and we are allowed to do 1107 * async probes and there are drivers that 1108 * want to probe asynchronously, we'll 1109 * try them. 1110 */ 1111 dev_dbg(dev, "scheduling asynchronous probe\n"); 1112 get_device(dev); 1113 async = true; 1114 } else { 1115 pm_request_idle(dev); 1116 } 1117 1118 if (dev->parent) 1119 pm_runtime_put(dev->parent); 1120 } 1121 out_unlock: 1122 device_unlock(dev); 1123 if (async) 1124 async_schedule_dev(__device_attach_async_helper, dev); 1125 return ret; 1126 } 1127 1128 /** 1129 * device_attach - try to attach device to a driver. 1130 * @dev: device. 1131 * 1132 * Walk the list of drivers that the bus has and call 1133 * driver_probe_device() for each pair. If a compatible 1134 * pair is found, break out and return. 1135 * 1136 * Returns 1 if the device was bound to a driver; 1137 * 0 if no matching driver was found; 1138 * -ENODEV if the device is not registered. 1139 * 1140 * When called for a USB interface, @dev->parent lock must be held. 1141 */ 1142 int device_attach(struct device *dev) 1143 { 1144 return __device_attach(dev, false); 1145 } 1146 EXPORT_SYMBOL_GPL(device_attach); 1147 1148 void device_initial_probe(struct device *dev) 1149 { 1150 struct subsys_private *sp = bus_to_subsys(dev->bus); 1151 1152 if (!sp) 1153 return; 1154 1155 if (sp->drivers_autoprobe) 1156 __device_attach(dev, true); 1157 1158 subsys_put(sp); 1159 } 1160 1161 /* 1162 * __device_driver_lock - acquire locks needed to manipulate dev->drv 1163 * @dev: Device we will update driver info for 1164 * @parent: Parent device. Needed if the bus requires parent lock 1165 * 1166 * This function will take the required locks for manipulating dev->drv. 1167 * Normally this will just be the @dev lock, but when called for a USB 1168 * interface, @parent lock will be held as well. 1169 */ 1170 static void __device_driver_lock(struct device *dev, struct device *parent) 1171 { 1172 if (parent && dev->bus->need_parent_lock) 1173 device_lock(parent); 1174 device_lock(dev); 1175 } 1176 1177 /* 1178 * __device_driver_unlock - release locks needed to manipulate dev->drv 1179 * @dev: Device we will update driver info for 1180 * @parent: Parent device. Needed if the bus requires parent lock 1181 * 1182 * This function will release the required locks for manipulating dev->drv. 1183 * Normally this will just be the @dev lock, but when called for a 1184 * USB interface, @parent lock will be released as well. 1185 */ 1186 static void __device_driver_unlock(struct device *dev, struct device *parent) 1187 { 1188 device_unlock(dev); 1189 if (parent && dev->bus->need_parent_lock) 1190 device_unlock(parent); 1191 } 1192 1193 /** 1194 * device_driver_attach - attach a specific driver to a specific device 1195 * @drv: Driver to attach 1196 * @dev: Device to attach it to 1197 * 1198 * Manually attach driver to a device. Will acquire both @dev lock and 1199 * @dev->parent lock if needed. Returns 0 on success, -ERR on failure. 1200 */ 1201 int device_driver_attach(const struct device_driver *drv, struct device *dev) 1202 { 1203 int ret; 1204 1205 __device_driver_lock(dev, dev->parent); 1206 ret = __driver_probe_device(drv, dev); 1207 __device_driver_unlock(dev, dev->parent); 1208 1209 /* also return probe errors as normal negative errnos */ 1210 if (ret > 0) 1211 ret = -ret; 1212 if (ret == -EPROBE_DEFER) 1213 return -EAGAIN; 1214 return ret; 1215 } 1216 EXPORT_SYMBOL_GPL(device_driver_attach); 1217 1218 static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie) 1219 { 1220 struct device *dev = _dev; 1221 const struct device_driver *drv; 1222 int ret; 1223 1224 __device_driver_lock(dev, dev->parent); 1225 drv = dev->p->async_driver; 1226 dev->p->async_driver = NULL; 1227 ret = driver_probe_device(drv, dev); 1228 __device_driver_unlock(dev, dev->parent); 1229 1230 dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret); 1231 1232 put_device(dev); 1233 } 1234 1235 static int __driver_attach(struct device *dev, void *data) 1236 { 1237 const struct device_driver *drv = data; 1238 bool async = false; 1239 int ret; 1240 1241 /* 1242 * Lock device and try to bind to it. We drop the error 1243 * here and always return 0, because we need to keep trying 1244 * to bind to devices and some drivers will return an error 1245 * simply if it didn't support the device. 1246 * 1247 * driver_probe_device() will spit a warning if there 1248 * is an error. 1249 */ 1250 1251 ret = driver_match_device(drv, dev); 1252 if (ret == 0) { 1253 /* no match */ 1254 return 0; 1255 } else if (ret == -EPROBE_DEFER) { 1256 dev_dbg(dev, "Device match requests probe deferral\n"); 1257 dev->can_match = true; 1258 driver_deferred_probe_add(dev); 1259 /* 1260 * Driver could not match with device, but may match with 1261 * another device on the bus. 1262 */ 1263 return 0; 1264 } else if (ret < 0) { 1265 dev_dbg(dev, "Bus failed to match device: %d\n", ret); 1266 /* 1267 * Driver could not match with device, but may match with 1268 * another device on the bus. 1269 */ 1270 return 0; 1271 } /* ret > 0 means positive match */ 1272 1273 if (driver_allows_async_probing(drv)) { 1274 /* 1275 * Instead of probing the device synchronously we will 1276 * probe it asynchronously to allow for more parallelism. 1277 * 1278 * We only take the device lock here in order to guarantee 1279 * that the dev->driver and async_driver fields are protected 1280 */ 1281 dev_dbg(dev, "probing driver %s asynchronously\n", drv->name); 1282 device_lock(dev); 1283 if (!dev->driver && !dev->p->async_driver) { 1284 get_device(dev); 1285 dev->p->async_driver = drv; 1286 async = true; 1287 } 1288 device_unlock(dev); 1289 if (async) 1290 async_schedule_dev(__driver_attach_async_helper, dev); 1291 return 0; 1292 } 1293 1294 __device_driver_lock(dev, dev->parent); 1295 driver_probe_device(drv, dev); 1296 __device_driver_unlock(dev, dev->parent); 1297 1298 return 0; 1299 } 1300 1301 /** 1302 * driver_attach - try to bind driver to devices. 1303 * @drv: driver. 1304 * 1305 * Walk the list of devices that the bus has on it and try to 1306 * match the driver with each one. If driver_probe_device() 1307 * returns 0 and the @dev->driver is set, we've found a 1308 * compatible pair. 1309 */ 1310 int driver_attach(const struct device_driver *drv) 1311 { 1312 /* The (void *) will be put back to const * in __driver_attach() */ 1313 return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach); 1314 } 1315 EXPORT_SYMBOL_GPL(driver_attach); 1316 1317 /* 1318 * __device_release_driver() must be called with @dev lock held. 1319 * When called for a USB interface, @dev->parent lock must be held as well. 1320 */ 1321 static void __device_release_driver(struct device *dev, struct device *parent) 1322 { 1323 struct device_driver *drv; 1324 1325 drv = dev->driver; 1326 if (drv) { 1327 pm_runtime_get_sync(dev); 1328 1329 while (device_links_busy(dev)) { 1330 __device_driver_unlock(dev, parent); 1331 1332 device_links_unbind_consumers(dev); 1333 1334 __device_driver_lock(dev, parent); 1335 /* 1336 * A concurrent invocation of the same function might 1337 * have released the driver successfully while this one 1338 * was waiting, so check for that. 1339 */ 1340 if (dev->driver != drv) { 1341 pm_runtime_put(dev); 1342 return; 1343 } 1344 } 1345 1346 driver_sysfs_remove(dev); 1347 1348 bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER); 1349 1350 pm_runtime_put_sync(dev); 1351 1352 device_remove(dev); 1353 1354 if (dev->bus && dev->bus->dma_cleanup) 1355 dev->bus->dma_cleanup(dev); 1356 1357 device_unbind_cleanup(dev); 1358 device_links_driver_cleanup(dev); 1359 1360 klist_remove(&dev->p->knode_driver); 1361 device_pm_check_callbacks(dev); 1362 1363 bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER); 1364 kobject_uevent(&dev->kobj, KOBJ_UNBIND); 1365 } 1366 } 1367 1368 void device_release_driver_internal(struct device *dev, 1369 const struct device_driver *drv, 1370 struct device *parent) 1371 { 1372 __device_driver_lock(dev, parent); 1373 1374 if (!drv || drv == dev->driver) 1375 __device_release_driver(dev, parent); 1376 1377 __device_driver_unlock(dev, parent); 1378 } 1379 1380 /** 1381 * device_release_driver - manually detach device from driver. 1382 * @dev: device. 1383 * 1384 * Manually detach device from driver. 1385 * When called for a USB interface, @dev->parent lock must be held. 1386 * 1387 * If this function is to be called with @dev->parent lock held, ensure that 1388 * the device's consumers are unbound in advance or that their locks can be 1389 * acquired under the @dev->parent lock. 1390 */ 1391 void device_release_driver(struct device *dev) 1392 { 1393 /* 1394 * If anyone calls device_release_driver() recursively from 1395 * within their ->remove callback for the same device, they 1396 * will deadlock right here. 1397 */ 1398 device_release_driver_internal(dev, NULL, NULL); 1399 } 1400 EXPORT_SYMBOL_GPL(device_release_driver); 1401 1402 /** 1403 * device_driver_detach - detach driver from a specific device 1404 * @dev: device to detach driver from 1405 * 1406 * Detach driver from device. Will acquire both @dev lock and @dev->parent 1407 * lock if needed. 1408 */ 1409 void device_driver_detach(struct device *dev) 1410 { 1411 device_release_driver_internal(dev, NULL, dev->parent); 1412 } 1413 1414 /** 1415 * driver_detach - detach driver from all devices it controls. 1416 * @drv: driver. 1417 */ 1418 void driver_detach(const struct device_driver *drv) 1419 { 1420 struct device_private *dev_prv; 1421 struct device *dev; 1422 1423 if (driver_allows_async_probing(drv)) 1424 async_synchronize_full(); 1425 1426 for (;;) { 1427 spin_lock(&drv->p->klist_devices.k_lock); 1428 if (list_empty(&drv->p->klist_devices.k_list)) { 1429 spin_unlock(&drv->p->klist_devices.k_lock); 1430 break; 1431 } 1432 dev_prv = list_last_entry(&drv->p->klist_devices.k_list, 1433 struct device_private, 1434 knode_driver.n_node); 1435 dev = dev_prv->device; 1436 get_device(dev); 1437 spin_unlock(&drv->p->klist_devices.k_lock); 1438 device_release_driver_internal(dev, drv, dev->parent); 1439 put_device(dev); 1440 } 1441 } 1442