dd.c 24 KB

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