dd.c 24 KB

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