dd.c 23 KB

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