dd.c 24 KB

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