dd.c 25 KB

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