dd.c 28 KB

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