dd.c 24 KB

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