dd.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051
  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. ret = dma_configure(dev);
  421. if (ret)
  422. goto probe_failed;
  423. if (driver_sysfs_add(dev)) {
  424. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  425. __func__, dev_name(dev));
  426. goto probe_failed;
  427. }
  428. if (dev->pm_domain && dev->pm_domain->activate) {
  429. ret = dev->pm_domain->activate(dev);
  430. if (ret)
  431. goto probe_failed;
  432. }
  433. if (dev->bus->probe) {
  434. ret = dev->bus->probe(dev);
  435. if (ret)
  436. goto probe_failed;
  437. } else if (drv->probe) {
  438. ret = drv->probe(dev);
  439. if (ret)
  440. goto probe_failed;
  441. }
  442. if (test_remove) {
  443. test_remove = false;
  444. if (dev->bus->remove)
  445. dev->bus->remove(dev);
  446. else if (drv->remove)
  447. drv->remove(dev);
  448. devres_release_all(dev);
  449. driver_sysfs_remove(dev);
  450. dev->driver = NULL;
  451. dev_set_drvdata(dev, NULL);
  452. if (dev->pm_domain && dev->pm_domain->dismiss)
  453. dev->pm_domain->dismiss(dev);
  454. pm_runtime_reinit(dev);
  455. goto re_probe;
  456. }
  457. pinctrl_init_done(dev);
  458. if (dev->pm_domain && dev->pm_domain->sync)
  459. dev->pm_domain->sync(dev);
  460. driver_bound(dev);
  461. ret = 1;
  462. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  463. drv->bus->name, __func__, dev_name(dev), drv->name);
  464. goto done;
  465. probe_failed:
  466. if (dev->bus)
  467. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  468. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  469. pinctrl_bind_failed:
  470. device_links_no_driver(dev);
  471. devres_release_all(dev);
  472. dma_deconfigure(dev);
  473. driver_sysfs_remove(dev);
  474. dev->driver = NULL;
  475. dev_set_drvdata(dev, NULL);
  476. if (dev->pm_domain && dev->pm_domain->dismiss)
  477. dev->pm_domain->dismiss(dev);
  478. pm_runtime_reinit(dev);
  479. dev_pm_set_driver_flags(dev, 0);
  480. switch (ret) {
  481. case -EPROBE_DEFER:
  482. /* Driver requested deferred probing */
  483. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  484. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  485. break;
  486. case -ENODEV:
  487. case -ENXIO:
  488. pr_debug("%s: probe of %s rejects match %d\n",
  489. drv->name, dev_name(dev), ret);
  490. break;
  491. default:
  492. /* driver matched but the probe failed */
  493. printk(KERN_WARNING
  494. "%s: probe of %s failed with error %d\n",
  495. drv->name, dev_name(dev), ret);
  496. }
  497. /*
  498. * Ignore errors returned by ->probe so that the next driver can try
  499. * its luck.
  500. */
  501. ret = 0;
  502. done:
  503. atomic_dec(&probe_count);
  504. wake_up(&probe_waitqueue);
  505. return ret;
  506. }
  507. /*
  508. * For initcall_debug, show the driver probe time.
  509. */
  510. static int really_probe_debug(struct device *dev, struct device_driver *drv)
  511. {
  512. ktime_t calltime, delta, rettime;
  513. int ret;
  514. calltime = ktime_get();
  515. ret = really_probe(dev, drv);
  516. rettime = ktime_get();
  517. delta = ktime_sub(rettime, calltime);
  518. printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
  519. dev_name(dev), ret, (s64) ktime_to_us(delta));
  520. return ret;
  521. }
  522. /**
  523. * driver_probe_done
  524. * Determine if the probe sequence is finished or not.
  525. *
  526. * Should somehow figure out how to use a semaphore, not an atomic variable...
  527. */
  528. int driver_probe_done(void)
  529. {
  530. pr_debug("%s: probe_count = %d\n", __func__,
  531. atomic_read(&probe_count));
  532. if (atomic_read(&probe_count))
  533. return -EBUSY;
  534. return 0;
  535. }
  536. /**
  537. * wait_for_device_probe
  538. * Wait for device probing to be completed.
  539. */
  540. void wait_for_device_probe(void)
  541. {
  542. /* wait for the deferred probe workqueue to finish */
  543. flush_work(&deferred_probe_work);
  544. /* wait for the known devices to complete their probing */
  545. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  546. async_synchronize_full();
  547. }
  548. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  549. /**
  550. * driver_probe_device - attempt to bind device & driver together
  551. * @drv: driver to bind a device to
  552. * @dev: device to try to bind to the driver
  553. *
  554. * This function returns -ENODEV if the device is not registered,
  555. * 1 if the device is bound successfully and 0 otherwise.
  556. *
  557. * This function must be called with @dev lock held. When called for a
  558. * USB interface, @dev->parent lock must be held as well.
  559. *
  560. * If the device has a parent, runtime-resume the parent before driver probing.
  561. */
  562. int driver_probe_device(struct device_driver *drv, struct device *dev)
  563. {
  564. int ret = 0;
  565. if (!device_is_registered(dev))
  566. return -ENODEV;
  567. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  568. drv->bus->name, __func__, dev_name(dev), drv->name);
  569. pm_runtime_get_suppliers(dev);
  570. if (dev->parent)
  571. pm_runtime_get_sync(dev->parent);
  572. pm_runtime_barrier(dev);
  573. if (initcall_debug)
  574. ret = really_probe_debug(dev, drv);
  575. else
  576. ret = really_probe(dev, drv);
  577. pm_request_idle(dev);
  578. if (dev->parent)
  579. pm_runtime_put(dev->parent);
  580. pm_runtime_put_suppliers(dev);
  581. return ret;
  582. }
  583. bool driver_allows_async_probing(struct device_driver *drv)
  584. {
  585. switch (drv->probe_type) {
  586. case PROBE_PREFER_ASYNCHRONOUS:
  587. return true;
  588. case PROBE_FORCE_SYNCHRONOUS:
  589. return false;
  590. default:
  591. if (module_requested_async_probing(drv->owner))
  592. return true;
  593. return false;
  594. }
  595. }
  596. struct device_attach_data {
  597. struct device *dev;
  598. /*
  599. * Indicates whether we are are considering asynchronous probing or
  600. * not. Only initial binding after device or driver registration
  601. * (including deferral processing) may be done asynchronously, the
  602. * rest is always synchronous, as we expect it is being done by
  603. * request from userspace.
  604. */
  605. bool check_async;
  606. /*
  607. * Indicates if we are binding synchronous or asynchronous drivers.
  608. * When asynchronous probing is enabled we'll execute 2 passes
  609. * over drivers: first pass doing synchronous probing and second
  610. * doing asynchronous probing (if synchronous did not succeed -
  611. * most likely because there was no driver requiring synchronous
  612. * probing - and we found asynchronous driver during first pass).
  613. * The 2 passes are done because we can't shoot asynchronous
  614. * probe for given device and driver from bus_for_each_drv() since
  615. * driver pointer is not guaranteed to stay valid once
  616. * bus_for_each_drv() iterates to the next driver on the bus.
  617. */
  618. bool want_async;
  619. /*
  620. * We'll set have_async to 'true' if, while scanning for matching
  621. * driver, we'll encounter one that requests asynchronous probing.
  622. */
  623. bool have_async;
  624. };
  625. static int __device_attach_driver(struct device_driver *drv, void *_data)
  626. {
  627. struct device_attach_data *data = _data;
  628. struct device *dev = data->dev;
  629. bool async_allowed;
  630. int ret;
  631. ret = driver_match_device(drv, dev);
  632. if (ret == 0) {
  633. /* no match */
  634. return 0;
  635. } else if (ret == -EPROBE_DEFER) {
  636. dev_dbg(dev, "Device match requests probe deferral\n");
  637. driver_deferred_probe_add(dev);
  638. } else if (ret < 0) {
  639. dev_dbg(dev, "Bus failed to match device: %d", ret);
  640. return ret;
  641. } /* ret > 0 means positive match */
  642. async_allowed = driver_allows_async_probing(drv);
  643. if (async_allowed)
  644. data->have_async = true;
  645. if (data->check_async && async_allowed != data->want_async)
  646. return 0;
  647. return driver_probe_device(drv, dev);
  648. }
  649. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  650. {
  651. struct device *dev = _dev;
  652. struct device_attach_data data = {
  653. .dev = dev,
  654. .check_async = true,
  655. .want_async = true,
  656. };
  657. device_lock(dev);
  658. /*
  659. * Check if device has already been removed or claimed. This may
  660. * happen with driver loading, device discovery/registration,
  661. * and deferred probe processing happens all at once with
  662. * multiple threads.
  663. */
  664. if (dev->p->dead || dev->driver)
  665. goto out_unlock;
  666. if (dev->parent)
  667. pm_runtime_get_sync(dev->parent);
  668. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  669. dev_dbg(dev, "async probe completed\n");
  670. pm_request_idle(dev);
  671. if (dev->parent)
  672. pm_runtime_put(dev->parent);
  673. out_unlock:
  674. device_unlock(dev);
  675. put_device(dev);
  676. }
  677. static int __device_attach(struct device *dev, bool allow_async)
  678. {
  679. int ret = 0;
  680. device_lock(dev);
  681. if (dev->driver) {
  682. if (device_is_bound(dev)) {
  683. ret = 1;
  684. goto out_unlock;
  685. }
  686. ret = device_bind_driver(dev);
  687. if (ret == 0)
  688. ret = 1;
  689. else {
  690. dev->driver = NULL;
  691. ret = 0;
  692. }
  693. } else {
  694. struct device_attach_data data = {
  695. .dev = dev,
  696. .check_async = allow_async,
  697. .want_async = false,
  698. };
  699. if (dev->parent)
  700. pm_runtime_get_sync(dev->parent);
  701. ret = bus_for_each_drv(dev->bus, NULL, &data,
  702. __device_attach_driver);
  703. if (!ret && allow_async && data.have_async) {
  704. /*
  705. * If we could not find appropriate driver
  706. * synchronously and we are allowed to do
  707. * async probes and there are drivers that
  708. * want to probe asynchronously, we'll
  709. * try them.
  710. */
  711. dev_dbg(dev, "scheduling asynchronous probe\n");
  712. get_device(dev);
  713. async_schedule(__device_attach_async_helper, dev);
  714. } else {
  715. pm_request_idle(dev);
  716. }
  717. if (dev->parent)
  718. pm_runtime_put(dev->parent);
  719. }
  720. out_unlock:
  721. device_unlock(dev);
  722. return ret;
  723. }
  724. /**
  725. * device_attach - try to attach device to a driver.
  726. * @dev: device.
  727. *
  728. * Walk the list of drivers that the bus has and call
  729. * driver_probe_device() for each pair. If a compatible
  730. * pair is found, break out and return.
  731. *
  732. * Returns 1 if the device was bound to a driver;
  733. * 0 if no matching driver was found;
  734. * -ENODEV if the device is not registered.
  735. *
  736. * When called for a USB interface, @dev->parent lock must be held.
  737. */
  738. int device_attach(struct device *dev)
  739. {
  740. return __device_attach(dev, false);
  741. }
  742. EXPORT_SYMBOL_GPL(device_attach);
  743. void device_initial_probe(struct device *dev)
  744. {
  745. __device_attach(dev, true);
  746. }
  747. static int __driver_attach(struct device *dev, void *data)
  748. {
  749. struct device_driver *drv = data;
  750. int ret;
  751. /*
  752. * Lock device and try to bind to it. We drop the error
  753. * here and always return 0, because we need to keep trying
  754. * to bind to devices and some drivers will return an error
  755. * simply if it didn't support the device.
  756. *
  757. * driver_probe_device() will spit a warning if there
  758. * is an error.
  759. */
  760. ret = driver_match_device(drv, dev);
  761. if (ret == 0) {
  762. /* no match */
  763. return 0;
  764. } else if (ret == -EPROBE_DEFER) {
  765. dev_dbg(dev, "Device match requests probe deferral\n");
  766. driver_deferred_probe_add(dev);
  767. } else if (ret < 0) {
  768. dev_dbg(dev, "Bus failed to match device: %d", ret);
  769. return ret;
  770. } /* ret > 0 means positive match */
  771. if (dev->parent && dev->bus->need_parent_lock)
  772. device_lock(dev->parent);
  773. device_lock(dev);
  774. if (!dev->p->dead && !dev->driver)
  775. driver_probe_device(drv, dev);
  776. device_unlock(dev);
  777. if (dev->parent && dev->bus->need_parent_lock)
  778. device_unlock(dev->parent);
  779. return 0;
  780. }
  781. /**
  782. * driver_attach - try to bind driver to devices.
  783. * @drv: driver.
  784. *
  785. * Walk the list of devices that the bus has on it and try to
  786. * match the driver with each one. If driver_probe_device()
  787. * returns 0 and the @dev->driver is set, we've found a
  788. * compatible pair.
  789. */
  790. int driver_attach(struct device_driver *drv)
  791. {
  792. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  793. }
  794. EXPORT_SYMBOL_GPL(driver_attach);
  795. /*
  796. * __device_release_driver() must be called with @dev lock held.
  797. * When called for a USB interface, @dev->parent lock must be held as well.
  798. */
  799. static void __device_release_driver(struct device *dev, struct device *parent)
  800. {
  801. struct device_driver *drv;
  802. drv = dev->driver;
  803. if (drv) {
  804. while (device_links_busy(dev)) {
  805. device_unlock(dev);
  806. if (parent && dev->bus->need_parent_lock)
  807. device_unlock(parent);
  808. device_links_unbind_consumers(dev);
  809. if (parent && dev->bus->need_parent_lock)
  810. device_lock(parent);
  811. device_lock(dev);
  812. /*
  813. * A concurrent invocation of the same function might
  814. * have released the driver successfully while this one
  815. * was waiting, so check for that.
  816. */
  817. if (dev->driver != drv)
  818. return;
  819. }
  820. pm_runtime_get_sync(dev);
  821. pm_runtime_clean_up_links(dev);
  822. driver_sysfs_remove(dev);
  823. if (dev->bus)
  824. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  825. BUS_NOTIFY_UNBIND_DRIVER,
  826. dev);
  827. pm_runtime_put_sync(dev);
  828. if (dev->bus && dev->bus->remove)
  829. dev->bus->remove(dev);
  830. else if (drv->remove)
  831. drv->remove(dev);
  832. device_links_driver_cleanup(dev);
  833. devres_release_all(dev);
  834. dma_deconfigure(dev);
  835. dev->driver = NULL;
  836. dev_set_drvdata(dev, NULL);
  837. if (dev->pm_domain && dev->pm_domain->dismiss)
  838. dev->pm_domain->dismiss(dev);
  839. pm_runtime_reinit(dev);
  840. dev_pm_set_driver_flags(dev, 0);
  841. klist_remove(&dev->p->knode_driver);
  842. device_pm_check_callbacks(dev);
  843. if (dev->bus)
  844. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  845. BUS_NOTIFY_UNBOUND_DRIVER,
  846. dev);
  847. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  848. }
  849. }
  850. void device_release_driver_internal(struct device *dev,
  851. struct device_driver *drv,
  852. struct device *parent)
  853. {
  854. if (parent && dev->bus->need_parent_lock)
  855. device_lock(parent);
  856. device_lock(dev);
  857. if (!drv || drv == dev->driver)
  858. __device_release_driver(dev, parent);
  859. device_unlock(dev);
  860. if (parent && dev->bus->need_parent_lock)
  861. device_unlock(parent);
  862. }
  863. /**
  864. * device_release_driver - manually detach device from driver.
  865. * @dev: device.
  866. *
  867. * Manually detach device from driver.
  868. * When called for a USB interface, @dev->parent lock must be held.
  869. *
  870. * If this function is to be called with @dev->parent lock held, ensure that
  871. * the device's consumers are unbound in advance or that their locks can be
  872. * acquired under the @dev->parent lock.
  873. */
  874. void device_release_driver(struct device *dev)
  875. {
  876. /*
  877. * If anyone calls device_release_driver() recursively from
  878. * within their ->remove callback for the same device, they
  879. * will deadlock right here.
  880. */
  881. device_release_driver_internal(dev, NULL, NULL);
  882. }
  883. EXPORT_SYMBOL_GPL(device_release_driver);
  884. /**
  885. * driver_detach - detach driver from all devices it controls.
  886. * @drv: driver.
  887. */
  888. void driver_detach(struct device_driver *drv)
  889. {
  890. struct device_private *dev_prv;
  891. struct device *dev;
  892. if (driver_allows_async_probing(drv))
  893. async_synchronize_full();
  894. for (;;) {
  895. spin_lock(&drv->p->klist_devices.k_lock);
  896. if (list_empty(&drv->p->klist_devices.k_list)) {
  897. spin_unlock(&drv->p->klist_devices.k_lock);
  898. break;
  899. }
  900. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  901. struct device_private,
  902. knode_driver.n_node);
  903. dev = dev_prv->device;
  904. get_device(dev);
  905. spin_unlock(&drv->p->klist_devices.k_lock);
  906. device_release_driver_internal(dev, drv, dev->parent);
  907. put_device(dev);
  908. }
  909. }