dd.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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_pm_check_callbacks(dev);
  225. /*
  226. * Make sure the device is no longer in one of the deferred lists and
  227. * kick off retrying all pending devices
  228. */
  229. driver_deferred_probe_del(dev);
  230. driver_deferred_probe_trigger();
  231. if (dev->bus)
  232. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  233. BUS_NOTIFY_BOUND_DRIVER, dev);
  234. }
  235. static int driver_sysfs_add(struct device *dev)
  236. {
  237. int ret;
  238. if (dev->bus)
  239. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  240. BUS_NOTIFY_BIND_DRIVER, dev);
  241. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  242. kobject_name(&dev->kobj));
  243. if (ret == 0) {
  244. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  245. "driver");
  246. if (ret)
  247. sysfs_remove_link(&dev->driver->p->kobj,
  248. kobject_name(&dev->kobj));
  249. }
  250. return ret;
  251. }
  252. static void driver_sysfs_remove(struct device *dev)
  253. {
  254. struct device_driver *drv = dev->driver;
  255. if (drv) {
  256. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  257. sysfs_remove_link(&dev->kobj, "driver");
  258. }
  259. }
  260. /**
  261. * device_bind_driver - bind a driver to one device.
  262. * @dev: device.
  263. *
  264. * Allow manual attachment of a driver to a device.
  265. * Caller must have already set @dev->driver.
  266. *
  267. * Note that this does not modify the bus reference count
  268. * nor take the bus's rwsem. Please verify those are accounted
  269. * for before calling this. (It is ok to call with no other effort
  270. * from a driver's probe() method.)
  271. *
  272. * This function must be called with the device lock held.
  273. */
  274. int device_bind_driver(struct device *dev)
  275. {
  276. int ret;
  277. ret = driver_sysfs_add(dev);
  278. if (!ret)
  279. driver_bound(dev);
  280. else if (dev->bus)
  281. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  282. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  283. return ret;
  284. }
  285. EXPORT_SYMBOL_GPL(device_bind_driver);
  286. static atomic_t probe_count = ATOMIC_INIT(0);
  287. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  288. static int really_probe(struct device *dev, struct device_driver *drv)
  289. {
  290. int ret = -EPROBE_DEFER;
  291. int local_trigger_count = atomic_read(&deferred_trigger_count);
  292. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  293. !drv->suppress_bind_attrs;
  294. if (defer_all_probes) {
  295. /*
  296. * Value of defer_all_probes can be set only by
  297. * device_defer_all_probes_enable() which, in turn, will call
  298. * wait_for_device_probe() right after that to avoid any races.
  299. */
  300. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  301. driver_deferred_probe_add(dev);
  302. return ret;
  303. }
  304. atomic_inc(&probe_count);
  305. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  306. drv->bus->name, __func__, drv->name, dev_name(dev));
  307. WARN_ON(!list_empty(&dev->devres_head));
  308. re_probe:
  309. dev->driver = drv;
  310. /* If using pinctrl, bind pins now before probing */
  311. ret = pinctrl_bind_pins(dev);
  312. if (ret)
  313. goto pinctrl_bind_failed;
  314. if (driver_sysfs_add(dev)) {
  315. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  316. __func__, dev_name(dev));
  317. goto probe_failed;
  318. }
  319. if (dev->pm_domain && dev->pm_domain->activate) {
  320. ret = dev->pm_domain->activate(dev);
  321. if (ret)
  322. goto probe_failed;
  323. }
  324. if (dev->bus->probe) {
  325. ret = dev->bus->probe(dev);
  326. if (ret)
  327. goto probe_failed;
  328. } else if (drv->probe) {
  329. ret = drv->probe(dev);
  330. if (ret)
  331. goto probe_failed;
  332. }
  333. if (test_remove) {
  334. test_remove = false;
  335. if (dev->bus->remove)
  336. dev->bus->remove(dev);
  337. else if (drv->remove)
  338. drv->remove(dev);
  339. devres_release_all(dev);
  340. driver_sysfs_remove(dev);
  341. dev->driver = NULL;
  342. dev_set_drvdata(dev, NULL);
  343. if (dev->pm_domain && dev->pm_domain->dismiss)
  344. dev->pm_domain->dismiss(dev);
  345. pm_runtime_reinit(dev);
  346. goto re_probe;
  347. }
  348. pinctrl_init_done(dev);
  349. if (dev->pm_domain && dev->pm_domain->sync)
  350. dev->pm_domain->sync(dev);
  351. driver_bound(dev);
  352. ret = 1;
  353. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  354. drv->bus->name, __func__, dev_name(dev), drv->name);
  355. goto done;
  356. probe_failed:
  357. if (dev->bus)
  358. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  359. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  360. pinctrl_bind_failed:
  361. devres_release_all(dev);
  362. driver_sysfs_remove(dev);
  363. dev->driver = NULL;
  364. dev_set_drvdata(dev, NULL);
  365. if (dev->pm_domain && dev->pm_domain->dismiss)
  366. dev->pm_domain->dismiss(dev);
  367. pm_runtime_reinit(dev);
  368. switch (ret) {
  369. case -EPROBE_DEFER:
  370. /* Driver requested deferred probing */
  371. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  372. driver_deferred_probe_add(dev);
  373. /* Did a trigger occur while probing? Need to re-trigger if yes */
  374. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  375. driver_deferred_probe_trigger();
  376. break;
  377. case -ENODEV:
  378. case -ENXIO:
  379. pr_debug("%s: probe of %s rejects match %d\n",
  380. drv->name, dev_name(dev), ret);
  381. break;
  382. default:
  383. /* driver matched but the probe failed */
  384. printk(KERN_WARNING
  385. "%s: probe of %s failed with error %d\n",
  386. drv->name, dev_name(dev), ret);
  387. }
  388. /*
  389. * Ignore errors returned by ->probe so that the next driver can try
  390. * its luck.
  391. */
  392. ret = 0;
  393. done:
  394. atomic_dec(&probe_count);
  395. wake_up(&probe_waitqueue);
  396. return ret;
  397. }
  398. /**
  399. * driver_probe_done
  400. * Determine if the probe sequence is finished or not.
  401. *
  402. * Should somehow figure out how to use a semaphore, not an atomic variable...
  403. */
  404. int driver_probe_done(void)
  405. {
  406. pr_debug("%s: probe_count = %d\n", __func__,
  407. atomic_read(&probe_count));
  408. if (atomic_read(&probe_count))
  409. return -EBUSY;
  410. return 0;
  411. }
  412. /**
  413. * wait_for_device_probe
  414. * Wait for device probing to be completed.
  415. */
  416. void wait_for_device_probe(void)
  417. {
  418. /* wait for the deferred probe workqueue to finish */
  419. flush_work(&deferred_probe_work);
  420. /* wait for the known devices to complete their probing */
  421. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  422. async_synchronize_full();
  423. }
  424. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  425. /**
  426. * driver_probe_device - attempt to bind device & driver together
  427. * @drv: driver to bind a device to
  428. * @dev: device to try to bind to the driver
  429. *
  430. * This function returns -ENODEV if the device is not registered,
  431. * 1 if the device is bound successfully and 0 otherwise.
  432. *
  433. * This function must be called with @dev lock held. When called for a
  434. * USB interface, @dev->parent lock must be held as well.
  435. *
  436. * If the device has a parent, runtime-resume the parent before driver probing.
  437. */
  438. int driver_probe_device(struct device_driver *drv, struct device *dev)
  439. {
  440. int ret = 0;
  441. if (!device_is_registered(dev))
  442. return -ENODEV;
  443. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  444. drv->bus->name, __func__, dev_name(dev), drv->name);
  445. if (dev->parent)
  446. pm_runtime_get_sync(dev->parent);
  447. pm_runtime_barrier(dev);
  448. ret = really_probe(dev, drv);
  449. pm_request_idle(dev);
  450. if (dev->parent)
  451. pm_runtime_put(dev->parent);
  452. return ret;
  453. }
  454. bool driver_allows_async_probing(struct device_driver *drv)
  455. {
  456. switch (drv->probe_type) {
  457. case PROBE_PREFER_ASYNCHRONOUS:
  458. return true;
  459. case PROBE_FORCE_SYNCHRONOUS:
  460. return false;
  461. default:
  462. if (module_requested_async_probing(drv->owner))
  463. return true;
  464. return false;
  465. }
  466. }
  467. struct device_attach_data {
  468. struct device *dev;
  469. /*
  470. * Indicates whether we are are considering asynchronous probing or
  471. * not. Only initial binding after device or driver registration
  472. * (including deferral processing) may be done asynchronously, the
  473. * rest is always synchronous, as we expect it is being done by
  474. * request from userspace.
  475. */
  476. bool check_async;
  477. /*
  478. * Indicates if we are binding synchronous or asynchronous drivers.
  479. * When asynchronous probing is enabled we'll execute 2 passes
  480. * over drivers: first pass doing synchronous probing and second
  481. * doing asynchronous probing (if synchronous did not succeed -
  482. * most likely because there was no driver requiring synchronous
  483. * probing - and we found asynchronous driver during first pass).
  484. * The 2 passes are done because we can't shoot asynchronous
  485. * probe for given device and driver from bus_for_each_drv() since
  486. * driver pointer is not guaranteed to stay valid once
  487. * bus_for_each_drv() iterates to the next driver on the bus.
  488. */
  489. bool want_async;
  490. /*
  491. * We'll set have_async to 'true' if, while scanning for matching
  492. * driver, we'll encounter one that requests asynchronous probing.
  493. */
  494. bool have_async;
  495. };
  496. static int __device_attach_driver(struct device_driver *drv, void *_data)
  497. {
  498. struct device_attach_data *data = _data;
  499. struct device *dev = data->dev;
  500. bool async_allowed;
  501. int ret;
  502. /*
  503. * Check if device has already been claimed. This may
  504. * happen with driver loading, device discovery/registration,
  505. * and deferred probe processing happens all at once with
  506. * multiple threads.
  507. */
  508. if (dev->driver)
  509. return -EBUSY;
  510. ret = driver_match_device(drv, dev);
  511. if (ret == 0) {
  512. /* no match */
  513. return 0;
  514. } else if (ret == -EPROBE_DEFER) {
  515. dev_dbg(dev, "Device match requests probe deferral\n");
  516. driver_deferred_probe_add(dev);
  517. } else if (ret < 0) {
  518. dev_dbg(dev, "Bus failed to match device: %d", ret);
  519. return ret;
  520. } /* ret > 0 means positive match */
  521. async_allowed = driver_allows_async_probing(drv);
  522. if (async_allowed)
  523. data->have_async = true;
  524. if (data->check_async && async_allowed != data->want_async)
  525. return 0;
  526. return driver_probe_device(drv, dev);
  527. }
  528. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  529. {
  530. struct device *dev = _dev;
  531. struct device_attach_data data = {
  532. .dev = dev,
  533. .check_async = true,
  534. .want_async = true,
  535. };
  536. device_lock(dev);
  537. if (dev->parent)
  538. pm_runtime_get_sync(dev->parent);
  539. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  540. dev_dbg(dev, "async probe completed\n");
  541. pm_request_idle(dev);
  542. if (dev->parent)
  543. pm_runtime_put(dev->parent);
  544. device_unlock(dev);
  545. put_device(dev);
  546. }
  547. static int __device_attach(struct device *dev, bool allow_async)
  548. {
  549. int ret = 0;
  550. device_lock(dev);
  551. if (dev->driver) {
  552. if (device_is_bound(dev)) {
  553. ret = 1;
  554. goto out_unlock;
  555. }
  556. ret = device_bind_driver(dev);
  557. if (ret == 0)
  558. ret = 1;
  559. else {
  560. dev->driver = NULL;
  561. ret = 0;
  562. }
  563. } else {
  564. struct device_attach_data data = {
  565. .dev = dev,
  566. .check_async = allow_async,
  567. .want_async = false,
  568. };
  569. if (dev->parent)
  570. pm_runtime_get_sync(dev->parent);
  571. ret = bus_for_each_drv(dev->bus, NULL, &data,
  572. __device_attach_driver);
  573. if (!ret && allow_async && data.have_async) {
  574. /*
  575. * If we could not find appropriate driver
  576. * synchronously and we are allowed to do
  577. * async probes and there are drivers that
  578. * want to probe asynchronously, we'll
  579. * try them.
  580. */
  581. dev_dbg(dev, "scheduling asynchronous probe\n");
  582. get_device(dev);
  583. async_schedule(__device_attach_async_helper, dev);
  584. } else {
  585. pm_request_idle(dev);
  586. }
  587. if (dev->parent)
  588. pm_runtime_put(dev->parent);
  589. }
  590. out_unlock:
  591. device_unlock(dev);
  592. return ret;
  593. }
  594. /**
  595. * device_attach - try to attach device to a driver.
  596. * @dev: device.
  597. *
  598. * Walk the list of drivers that the bus has and call
  599. * driver_probe_device() for each pair. If a compatible
  600. * pair is found, break out and return.
  601. *
  602. * Returns 1 if the device was bound to a driver;
  603. * 0 if no matching driver was found;
  604. * -ENODEV if the device is not registered.
  605. *
  606. * When called for a USB interface, @dev->parent lock must be held.
  607. */
  608. int device_attach(struct device *dev)
  609. {
  610. return __device_attach(dev, false);
  611. }
  612. EXPORT_SYMBOL_GPL(device_attach);
  613. void device_initial_probe(struct device *dev)
  614. {
  615. __device_attach(dev, true);
  616. }
  617. static int __driver_attach(struct device *dev, void *data)
  618. {
  619. struct device_driver *drv = data;
  620. int ret;
  621. /*
  622. * Lock device and try to bind to it. We drop the error
  623. * here and always return 0, because we need to keep trying
  624. * to bind to devices and some drivers will return an error
  625. * simply if it didn't support the device.
  626. *
  627. * driver_probe_device() will spit a warning if there
  628. * is an error.
  629. */
  630. ret = driver_match_device(drv, dev);
  631. if (ret == 0) {
  632. /* no match */
  633. return 0;
  634. } else if (ret == -EPROBE_DEFER) {
  635. dev_dbg(dev, "Device match requests probe deferral\n");
  636. driver_deferred_probe_add(dev);
  637. } else if (ret < 0) {
  638. dev_dbg(dev, "Bus failed to match device: %d", ret);
  639. return ret;
  640. } /* ret > 0 means positive match */
  641. if (dev->parent) /* Needed for USB */
  642. device_lock(dev->parent);
  643. device_lock(dev);
  644. if (!dev->driver)
  645. driver_probe_device(drv, dev);
  646. device_unlock(dev);
  647. if (dev->parent)
  648. device_unlock(dev->parent);
  649. return 0;
  650. }
  651. /**
  652. * driver_attach - try to bind driver to devices.
  653. * @drv: driver.
  654. *
  655. * Walk the list of devices that the bus has on it and try to
  656. * match the driver with each one. If driver_probe_device()
  657. * returns 0 and the @dev->driver is set, we've found a
  658. * compatible pair.
  659. */
  660. int driver_attach(struct device_driver *drv)
  661. {
  662. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  663. }
  664. EXPORT_SYMBOL_GPL(driver_attach);
  665. /*
  666. * __device_release_driver() must be called with @dev lock held.
  667. * When called for a USB interface, @dev->parent lock must be held as well.
  668. */
  669. static void __device_release_driver(struct device *dev)
  670. {
  671. struct device_driver *drv;
  672. drv = dev->driver;
  673. if (drv) {
  674. if (driver_allows_async_probing(drv))
  675. async_synchronize_full();
  676. pm_runtime_get_sync(dev);
  677. driver_sysfs_remove(dev);
  678. if (dev->bus)
  679. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  680. BUS_NOTIFY_UNBIND_DRIVER,
  681. dev);
  682. pm_runtime_put_sync(dev);
  683. if (dev->bus && dev->bus->remove)
  684. dev->bus->remove(dev);
  685. else if (drv->remove)
  686. drv->remove(dev);
  687. devres_release_all(dev);
  688. dev->driver = NULL;
  689. dev_set_drvdata(dev, NULL);
  690. if (dev->pm_domain && dev->pm_domain->dismiss)
  691. dev->pm_domain->dismiss(dev);
  692. pm_runtime_reinit(dev);
  693. klist_remove(&dev->p->knode_driver);
  694. device_pm_check_callbacks(dev);
  695. if (dev->bus)
  696. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  697. BUS_NOTIFY_UNBOUND_DRIVER,
  698. dev);
  699. }
  700. }
  701. /**
  702. * device_release_driver - manually detach device from driver.
  703. * @dev: device.
  704. *
  705. * Manually detach device from driver.
  706. * When called for a USB interface, @dev->parent lock must be held.
  707. */
  708. void device_release_driver(struct device *dev)
  709. {
  710. /*
  711. * If anyone calls device_release_driver() recursively from
  712. * within their ->remove callback for the same device, they
  713. * will deadlock right here.
  714. */
  715. device_lock(dev);
  716. __device_release_driver(dev);
  717. device_unlock(dev);
  718. }
  719. EXPORT_SYMBOL_GPL(device_release_driver);
  720. /**
  721. * driver_detach - detach driver from all devices it controls.
  722. * @drv: driver.
  723. */
  724. void driver_detach(struct device_driver *drv)
  725. {
  726. struct device_private *dev_prv;
  727. struct device *dev;
  728. for (;;) {
  729. spin_lock(&drv->p->klist_devices.k_lock);
  730. if (list_empty(&drv->p->klist_devices.k_list)) {
  731. spin_unlock(&drv->p->klist_devices.k_lock);
  732. break;
  733. }
  734. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  735. struct device_private,
  736. knode_driver.n_node);
  737. dev = dev_prv->device;
  738. get_device(dev);
  739. spin_unlock(&drv->p->klist_devices.k_lock);
  740. if (dev->parent) /* Needed for USB */
  741. device_lock(dev->parent);
  742. device_lock(dev);
  743. if (dev->driver == drv)
  744. __device_release_driver(dev);
  745. device_unlock(dev);
  746. if (dev->parent)
  747. device_unlock(dev->parent);
  748. put_device(dev);
  749. }
  750. }