dd.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  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. 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 *p;
  233. deferred_probe_timeout = 0;
  234. driver_deferred_probe_trigger();
  235. flush_work(&deferred_probe_work);
  236. mutex_lock(&deferred_probe_mutex);
  237. list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
  238. dev_info(p->device, "deferred probe pending\n");
  239. mutex_unlock(&deferred_probe_mutex);
  240. }
  241. static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
  242. /**
  243. * deferred_probe_initcall() - Enable probing of deferred devices
  244. *
  245. * We don't want to get in the way when the bulk of drivers are getting probed.
  246. * Instead, this initcall makes sure that deferred probing is delayed until
  247. * late_initcall time.
  248. */
  249. static int deferred_probe_initcall(void)
  250. {
  251. deferred_devices = debugfs_create_file("devices_deferred", 0444, NULL,
  252. NULL, &deferred_devs_fops);
  253. driver_deferred_probe_enable = true;
  254. driver_deferred_probe_trigger();
  255. /* Sort as many dependencies as possible before exiting initcalls */
  256. flush_work(&deferred_probe_work);
  257. initcalls_done = true;
  258. /*
  259. * Trigger deferred probe again, this time we won't defer anything
  260. * that is optional
  261. */
  262. driver_deferred_probe_trigger();
  263. flush_work(&deferred_probe_work);
  264. if (deferred_probe_timeout > 0) {
  265. schedule_delayed_work(&deferred_probe_timeout_work,
  266. deferred_probe_timeout * HZ);
  267. }
  268. return 0;
  269. }
  270. late_initcall(deferred_probe_initcall);
  271. static void __exit deferred_probe_exit(void)
  272. {
  273. debugfs_remove_recursive(deferred_devices);
  274. }
  275. __exitcall(deferred_probe_exit);
  276. /**
  277. * device_is_bound() - Check if device is bound to a driver
  278. * @dev: device to check
  279. *
  280. * Returns true if passed device has already finished probing successfully
  281. * against a driver.
  282. *
  283. * This function must be called with the device lock held.
  284. */
  285. bool device_is_bound(struct device *dev)
  286. {
  287. return dev->p && klist_node_attached(&dev->p->knode_driver);
  288. }
  289. static void driver_bound(struct device *dev)
  290. {
  291. if (device_is_bound(dev)) {
  292. printk(KERN_WARNING "%s: device %s already bound\n",
  293. __func__, kobject_name(&dev->kobj));
  294. return;
  295. }
  296. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
  297. __func__, dev_name(dev));
  298. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  299. device_links_driver_bound(dev);
  300. device_pm_check_callbacks(dev);
  301. /*
  302. * Make sure the device is no longer in one of the deferred lists and
  303. * kick off retrying all pending devices
  304. */
  305. driver_deferred_probe_del(dev);
  306. driver_deferred_probe_trigger();
  307. if (dev->bus)
  308. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  309. BUS_NOTIFY_BOUND_DRIVER, dev);
  310. kobject_uevent(&dev->kobj, KOBJ_BIND);
  311. }
  312. static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
  313. const char *buf, size_t count)
  314. {
  315. device_lock(dev);
  316. dev->driver->coredump(dev);
  317. device_unlock(dev);
  318. return count;
  319. }
  320. static DEVICE_ATTR_WO(coredump);
  321. static int driver_sysfs_add(struct device *dev)
  322. {
  323. int ret;
  324. if (dev->bus)
  325. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  326. BUS_NOTIFY_BIND_DRIVER, dev);
  327. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  328. kobject_name(&dev->kobj));
  329. if (ret)
  330. goto fail;
  331. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  332. "driver");
  333. if (ret)
  334. goto rm_dev;
  335. if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
  336. !device_create_file(dev, &dev_attr_coredump))
  337. return 0;
  338. sysfs_remove_link(&dev->kobj, "driver");
  339. rm_dev:
  340. sysfs_remove_link(&dev->driver->p->kobj,
  341. kobject_name(&dev->kobj));
  342. fail:
  343. return ret;
  344. }
  345. static void driver_sysfs_remove(struct device *dev)
  346. {
  347. struct device_driver *drv = dev->driver;
  348. if (drv) {
  349. if (drv->coredump)
  350. device_remove_file(dev, &dev_attr_coredump);
  351. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  352. sysfs_remove_link(&dev->kobj, "driver");
  353. }
  354. }
  355. /**
  356. * device_bind_driver - bind a driver to one device.
  357. * @dev: device.
  358. *
  359. * Allow manual attachment of a driver to a device.
  360. * Caller must have already set @dev->driver.
  361. *
  362. * Note that this does not modify the bus reference count
  363. * nor take the bus's rwsem. Please verify those are accounted
  364. * for before calling this. (It is ok to call with no other effort
  365. * from a driver's probe() method.)
  366. *
  367. * This function must be called with the device lock held.
  368. */
  369. int device_bind_driver(struct device *dev)
  370. {
  371. int ret;
  372. ret = driver_sysfs_add(dev);
  373. if (!ret)
  374. driver_bound(dev);
  375. else if (dev->bus)
  376. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  377. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  378. return ret;
  379. }
  380. EXPORT_SYMBOL_GPL(device_bind_driver);
  381. static atomic_t probe_count = ATOMIC_INIT(0);
  382. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  383. static void driver_deferred_probe_add_trigger(struct device *dev,
  384. int local_trigger_count)
  385. {
  386. driver_deferred_probe_add(dev);
  387. /* Did a trigger occur while probing? Need to re-trigger if yes */
  388. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  389. driver_deferred_probe_trigger();
  390. }
  391. static int really_probe(struct device *dev, struct device_driver *drv)
  392. {
  393. int ret = -EPROBE_DEFER;
  394. int local_trigger_count = atomic_read(&deferred_trigger_count);
  395. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  396. !drv->suppress_bind_attrs;
  397. if (defer_all_probes) {
  398. /*
  399. * Value of defer_all_probes can be set only by
  400. * device_defer_all_probes_enable() which, in turn, will call
  401. * wait_for_device_probe() right after that to avoid any races.
  402. */
  403. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  404. driver_deferred_probe_add(dev);
  405. return ret;
  406. }
  407. ret = device_links_check_suppliers(dev);
  408. if (ret == -EPROBE_DEFER)
  409. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  410. if (ret)
  411. return ret;
  412. atomic_inc(&probe_count);
  413. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  414. drv->bus->name, __func__, drv->name, dev_name(dev));
  415. if (!list_empty(&dev->devres_head)) {
  416. dev_crit(dev, "Resources present before probing\n");
  417. ret = -EBUSY;
  418. goto done;
  419. }
  420. re_probe:
  421. dev->driver = drv;
  422. /* If using pinctrl, bind pins now before probing */
  423. ret = pinctrl_bind_pins(dev);
  424. if (ret)
  425. goto pinctrl_bind_failed;
  426. ret = dma_configure(dev);
  427. if (ret)
  428. goto probe_failed;
  429. if (driver_sysfs_add(dev)) {
  430. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  431. __func__, dev_name(dev));
  432. goto probe_failed;
  433. }
  434. if (dev->pm_domain && dev->pm_domain->activate) {
  435. ret = dev->pm_domain->activate(dev);
  436. if (ret)
  437. goto probe_failed;
  438. }
  439. if (dev->bus->probe) {
  440. ret = dev->bus->probe(dev);
  441. if (ret)
  442. goto probe_failed;
  443. } else if (drv->probe) {
  444. ret = drv->probe(dev);
  445. if (ret)
  446. goto probe_failed;
  447. }
  448. if (test_remove) {
  449. test_remove = false;
  450. if (dev->bus->remove)
  451. dev->bus->remove(dev);
  452. else if (drv->remove)
  453. drv->remove(dev);
  454. devres_release_all(dev);
  455. driver_sysfs_remove(dev);
  456. dev->driver = NULL;
  457. dev_set_drvdata(dev, NULL);
  458. if (dev->pm_domain && dev->pm_domain->dismiss)
  459. dev->pm_domain->dismiss(dev);
  460. pm_runtime_reinit(dev);
  461. goto re_probe;
  462. }
  463. pinctrl_init_done(dev);
  464. if (dev->pm_domain && dev->pm_domain->sync)
  465. dev->pm_domain->sync(dev);
  466. driver_bound(dev);
  467. ret = 1;
  468. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  469. drv->bus->name, __func__, dev_name(dev), drv->name);
  470. goto done;
  471. probe_failed:
  472. if (dev->bus)
  473. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  474. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  475. pinctrl_bind_failed:
  476. device_links_no_driver(dev);
  477. devres_release_all(dev);
  478. dma_deconfigure(dev);
  479. driver_sysfs_remove(dev);
  480. dev->driver = NULL;
  481. dev_set_drvdata(dev, NULL);
  482. if (dev->pm_domain && dev->pm_domain->dismiss)
  483. dev->pm_domain->dismiss(dev);
  484. pm_runtime_reinit(dev);
  485. dev_pm_set_driver_flags(dev, 0);
  486. switch (ret) {
  487. case -EPROBE_DEFER:
  488. /* Driver requested deferred probing */
  489. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  490. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  491. break;
  492. case -ENODEV:
  493. case -ENXIO:
  494. pr_debug("%s: probe of %s rejects match %d\n",
  495. drv->name, dev_name(dev), ret);
  496. break;
  497. default:
  498. /* driver matched but the probe failed */
  499. printk(KERN_WARNING
  500. "%s: probe of %s failed with error %d\n",
  501. drv->name, dev_name(dev), ret);
  502. }
  503. /*
  504. * Ignore errors returned by ->probe so that the next driver can try
  505. * its luck.
  506. */
  507. ret = 0;
  508. done:
  509. atomic_dec(&probe_count);
  510. wake_up_all(&probe_waitqueue);
  511. return ret;
  512. }
  513. /*
  514. * For initcall_debug, show the driver probe time.
  515. */
  516. static int really_probe_debug(struct device *dev, struct device_driver *drv)
  517. {
  518. ktime_t calltime, delta, rettime;
  519. int ret;
  520. calltime = ktime_get();
  521. ret = really_probe(dev, drv);
  522. rettime = ktime_get();
  523. delta = ktime_sub(rettime, calltime);
  524. printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
  525. dev_name(dev), ret, (s64) ktime_to_us(delta));
  526. return ret;
  527. }
  528. /**
  529. * driver_probe_done
  530. * Determine if the probe sequence is finished or not.
  531. *
  532. * Should somehow figure out how to use a semaphore, not an atomic variable...
  533. */
  534. int driver_probe_done(void)
  535. {
  536. pr_debug("%s: probe_count = %d\n", __func__,
  537. atomic_read(&probe_count));
  538. if (atomic_read(&probe_count))
  539. return -EBUSY;
  540. return 0;
  541. }
  542. /**
  543. * wait_for_device_probe
  544. * Wait for device probing to be completed.
  545. */
  546. void wait_for_device_probe(void)
  547. {
  548. /* wait for the deferred probe workqueue to finish */
  549. flush_work(&deferred_probe_work);
  550. /* wait for the known devices to complete their probing */
  551. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  552. async_synchronize_full();
  553. }
  554. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  555. /**
  556. * driver_probe_device - attempt to bind device & driver together
  557. * @drv: driver to bind a device to
  558. * @dev: device to try to bind to the driver
  559. *
  560. * This function returns -ENODEV if the device is not registered,
  561. * 1 if the device is bound successfully and 0 otherwise.
  562. *
  563. * This function must be called with @dev lock held. When called for a
  564. * USB interface, @dev->parent lock must be held as well.
  565. *
  566. * If the device has a parent, runtime-resume the parent before driver probing.
  567. */
  568. int driver_probe_device(struct device_driver *drv, struct device *dev)
  569. {
  570. int ret = 0;
  571. if (!device_is_registered(dev))
  572. return -ENODEV;
  573. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  574. drv->bus->name, __func__, dev_name(dev), drv->name);
  575. pm_runtime_get_suppliers(dev);
  576. if (dev->parent)
  577. pm_runtime_get_sync(dev->parent);
  578. pm_runtime_barrier(dev);
  579. if (initcall_debug)
  580. ret = really_probe_debug(dev, drv);
  581. else
  582. ret = really_probe(dev, drv);
  583. pm_request_idle(dev);
  584. if (dev->parent)
  585. pm_runtime_put(dev->parent);
  586. pm_runtime_put_suppliers(dev);
  587. return ret;
  588. }
  589. bool driver_allows_async_probing(struct device_driver *drv)
  590. {
  591. switch (drv->probe_type) {
  592. case PROBE_PREFER_ASYNCHRONOUS:
  593. return true;
  594. case PROBE_FORCE_SYNCHRONOUS:
  595. return false;
  596. default:
  597. if (module_requested_async_probing(drv->owner))
  598. return true;
  599. return false;
  600. }
  601. }
  602. struct device_attach_data {
  603. struct device *dev;
  604. /*
  605. * Indicates whether we are are considering asynchronous probing or
  606. * not. Only initial binding after device or driver registration
  607. * (including deferral processing) may be done asynchronously, the
  608. * rest is always synchronous, as we expect it is being done by
  609. * request from userspace.
  610. */
  611. bool check_async;
  612. /*
  613. * Indicates if we are binding synchronous or asynchronous drivers.
  614. * When asynchronous probing is enabled we'll execute 2 passes
  615. * over drivers: first pass doing synchronous probing and second
  616. * doing asynchronous probing (if synchronous did not succeed -
  617. * most likely because there was no driver requiring synchronous
  618. * probing - and we found asynchronous driver during first pass).
  619. * The 2 passes are done because we can't shoot asynchronous
  620. * probe for given device and driver from bus_for_each_drv() since
  621. * driver pointer is not guaranteed to stay valid once
  622. * bus_for_each_drv() iterates to the next driver on the bus.
  623. */
  624. bool want_async;
  625. /*
  626. * We'll set have_async to 'true' if, while scanning for matching
  627. * driver, we'll encounter one that requests asynchronous probing.
  628. */
  629. bool have_async;
  630. };
  631. static int __device_attach_driver(struct device_driver *drv, void *_data)
  632. {
  633. struct device_attach_data *data = _data;
  634. struct device *dev = data->dev;
  635. bool async_allowed;
  636. int ret;
  637. ret = driver_match_device(drv, dev);
  638. if (ret == 0) {
  639. /* no match */
  640. return 0;
  641. } else if (ret == -EPROBE_DEFER) {
  642. dev_dbg(dev, "Device match requests probe deferral\n");
  643. driver_deferred_probe_add(dev);
  644. } else if (ret < 0) {
  645. dev_dbg(dev, "Bus failed to match device: %d", ret);
  646. return ret;
  647. } /* ret > 0 means positive match */
  648. async_allowed = driver_allows_async_probing(drv);
  649. if (async_allowed)
  650. data->have_async = true;
  651. if (data->check_async && async_allowed != data->want_async)
  652. return 0;
  653. return driver_probe_device(drv, dev);
  654. }
  655. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  656. {
  657. struct device *dev = _dev;
  658. struct device_attach_data data = {
  659. .dev = dev,
  660. .check_async = true,
  661. .want_async = true,
  662. };
  663. device_lock(dev);
  664. /*
  665. * Check if device has already been removed or claimed. This may
  666. * happen with driver loading, device discovery/registration,
  667. * and deferred probe processing happens all at once with
  668. * multiple threads.
  669. */
  670. if (dev->p->dead || dev->driver)
  671. goto out_unlock;
  672. if (dev->parent)
  673. pm_runtime_get_sync(dev->parent);
  674. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  675. dev_dbg(dev, "async probe completed\n");
  676. pm_request_idle(dev);
  677. if (dev->parent)
  678. pm_runtime_put(dev->parent);
  679. out_unlock:
  680. device_unlock(dev);
  681. put_device(dev);
  682. }
  683. static int __device_attach(struct device *dev, bool allow_async)
  684. {
  685. int ret = 0;
  686. device_lock(dev);
  687. if (dev->p->dead) {
  688. goto out_unlock;
  689. } else if (dev->driver) {
  690. if (device_is_bound(dev)) {
  691. ret = 1;
  692. goto out_unlock;
  693. }
  694. ret = device_bind_driver(dev);
  695. if (ret == 0)
  696. ret = 1;
  697. else {
  698. dev->driver = NULL;
  699. ret = 0;
  700. }
  701. } else {
  702. struct device_attach_data data = {
  703. .dev = dev,
  704. .check_async = allow_async,
  705. .want_async = false,
  706. };
  707. if (dev->parent)
  708. pm_runtime_get_sync(dev->parent);
  709. ret = bus_for_each_drv(dev->bus, NULL, &data,
  710. __device_attach_driver);
  711. if (!ret && allow_async && data.have_async) {
  712. /*
  713. * If we could not find appropriate driver
  714. * synchronously and we are allowed to do
  715. * async probes and there are drivers that
  716. * want to probe asynchronously, we'll
  717. * try them.
  718. */
  719. dev_dbg(dev, "scheduling asynchronous probe\n");
  720. get_device(dev);
  721. async_schedule(__device_attach_async_helper, dev);
  722. } else {
  723. pm_request_idle(dev);
  724. }
  725. if (dev->parent)
  726. pm_runtime_put(dev->parent);
  727. }
  728. out_unlock:
  729. device_unlock(dev);
  730. return ret;
  731. }
  732. /**
  733. * device_attach - try to attach device to a driver.
  734. * @dev: device.
  735. *
  736. * Walk the list of drivers that the bus has and call
  737. * driver_probe_device() for each pair. If a compatible
  738. * pair is found, break out and return.
  739. *
  740. * Returns 1 if the device was bound to a driver;
  741. * 0 if no matching driver was found;
  742. * -ENODEV if the device is not registered.
  743. *
  744. * When called for a USB interface, @dev->parent lock must be held.
  745. */
  746. int device_attach(struct device *dev)
  747. {
  748. return __device_attach(dev, false);
  749. }
  750. EXPORT_SYMBOL_GPL(device_attach);
  751. void device_initial_probe(struct device *dev)
  752. {
  753. __device_attach(dev, true);
  754. }
  755. static int __driver_attach(struct device *dev, void *data)
  756. {
  757. struct device_driver *drv = data;
  758. int ret;
  759. /*
  760. * Lock device and try to bind to it. We drop the error
  761. * here and always return 0, because we need to keep trying
  762. * to bind to devices and some drivers will return an error
  763. * simply if it didn't support the device.
  764. *
  765. * driver_probe_device() will spit a warning if there
  766. * is an error.
  767. */
  768. ret = driver_match_device(drv, dev);
  769. if (ret == 0) {
  770. /* no match */
  771. return 0;
  772. } else if (ret == -EPROBE_DEFER) {
  773. dev_dbg(dev, "Device match requests probe deferral\n");
  774. driver_deferred_probe_add(dev);
  775. } else if (ret < 0) {
  776. dev_dbg(dev, "Bus failed to match device: %d", ret);
  777. return ret;
  778. } /* ret > 0 means positive match */
  779. if (dev->parent && dev->bus->need_parent_lock)
  780. device_lock(dev->parent);
  781. device_lock(dev);
  782. if (!dev->p->dead && !dev->driver)
  783. driver_probe_device(drv, dev);
  784. device_unlock(dev);
  785. if (dev->parent && dev->bus->need_parent_lock)
  786. device_unlock(dev->parent);
  787. return 0;
  788. }
  789. /**
  790. * driver_attach - try to bind driver to devices.
  791. * @drv: driver.
  792. *
  793. * Walk the list of devices that the bus has on it and try to
  794. * match the driver with each one. If driver_probe_device()
  795. * returns 0 and the @dev->driver is set, we've found a
  796. * compatible pair.
  797. */
  798. int driver_attach(struct device_driver *drv)
  799. {
  800. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  801. }
  802. EXPORT_SYMBOL_GPL(driver_attach);
  803. /*
  804. * __device_release_driver() must be called with @dev lock held.
  805. * When called for a USB interface, @dev->parent lock must be held as well.
  806. */
  807. static void __device_release_driver(struct device *dev, struct device *parent)
  808. {
  809. struct device_driver *drv;
  810. drv = dev->driver;
  811. if (drv) {
  812. pm_runtime_get_sync(dev);
  813. while (device_links_busy(dev)) {
  814. device_unlock(dev);
  815. if (parent && dev->bus->need_parent_lock)
  816. device_unlock(parent);
  817. device_links_unbind_consumers(dev);
  818. if (parent && dev->bus->need_parent_lock)
  819. device_lock(parent);
  820. device_lock(dev);
  821. /*
  822. * A concurrent invocation of the same function might
  823. * have released the driver successfully while this one
  824. * was waiting, so check for that.
  825. */
  826. if (dev->driver != drv) {
  827. pm_runtime_put(dev);
  828. return;
  829. }
  830. }
  831. pm_runtime_clean_up_links(dev);
  832. driver_sysfs_remove(dev);
  833. if (dev->bus)
  834. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  835. BUS_NOTIFY_UNBIND_DRIVER,
  836. dev);
  837. pm_runtime_put_sync(dev);
  838. if (dev->bus && dev->bus->remove)
  839. dev->bus->remove(dev);
  840. else if (drv->remove)
  841. drv->remove(dev);
  842. device_links_driver_cleanup(dev);
  843. devres_release_all(dev);
  844. dma_deconfigure(dev);
  845. dev->driver = NULL;
  846. dev_set_drvdata(dev, NULL);
  847. if (dev->pm_domain && dev->pm_domain->dismiss)
  848. dev->pm_domain->dismiss(dev);
  849. pm_runtime_reinit(dev);
  850. dev_pm_set_driver_flags(dev, 0);
  851. klist_remove(&dev->p->knode_driver);
  852. device_pm_check_callbacks(dev);
  853. if (dev->bus)
  854. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  855. BUS_NOTIFY_UNBOUND_DRIVER,
  856. dev);
  857. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  858. }
  859. }
  860. void device_release_driver_internal(struct device *dev,
  861. struct device_driver *drv,
  862. struct device *parent)
  863. {
  864. if (parent && dev->bus->need_parent_lock)
  865. device_lock(parent);
  866. device_lock(dev);
  867. if (!drv || drv == dev->driver)
  868. __device_release_driver(dev, parent);
  869. device_unlock(dev);
  870. if (parent && dev->bus->need_parent_lock)
  871. device_unlock(parent);
  872. }
  873. /**
  874. * device_release_driver - manually detach device from driver.
  875. * @dev: device.
  876. *
  877. * Manually detach device from driver.
  878. * When called for a USB interface, @dev->parent lock must be held.
  879. *
  880. * If this function is to be called with @dev->parent lock held, ensure that
  881. * the device's consumers are unbound in advance or that their locks can be
  882. * acquired under the @dev->parent lock.
  883. */
  884. void device_release_driver(struct device *dev)
  885. {
  886. /*
  887. * If anyone calls device_release_driver() recursively from
  888. * within their ->remove callback for the same device, they
  889. * will deadlock right here.
  890. */
  891. device_release_driver_internal(dev, NULL, NULL);
  892. }
  893. EXPORT_SYMBOL_GPL(device_release_driver);
  894. /**
  895. * driver_detach - detach driver from all devices it controls.
  896. * @drv: driver.
  897. */
  898. void driver_detach(struct device_driver *drv)
  899. {
  900. struct device_private *dev_prv;
  901. struct device *dev;
  902. if (driver_allows_async_probing(drv))
  903. async_synchronize_full();
  904. for (;;) {
  905. spin_lock(&drv->p->klist_devices.k_lock);
  906. if (list_empty(&drv->p->klist_devices.k_list)) {
  907. spin_unlock(&drv->p->klist_devices.k_lock);
  908. break;
  909. }
  910. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  911. struct device_private,
  912. knode_driver.n_node);
  913. dev = dev_prv->device;
  914. get_device(dev);
  915. spin_unlock(&drv->p->klist_devices.k_lock);
  916. device_release_driver_internal(dev, drv, dev->parent);
  917. put_device(dev);
  918. }
  919. }