dd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960
  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. #ifdef CONFIG_MTPROF
  314. #include "bootprof.h"
  315. #else
  316. #define TIME_LOG_START()
  317. #define TIME_LOG_END()
  318. #define bootprof_probe(ts, dev, drv, probe)
  319. #endif
  320. static atomic_t probe_count = ATOMIC_INIT(0);
  321. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  322. static int really_probe(struct device *dev, struct device_driver *drv)
  323. {
  324. int ret = -EPROBE_DEFER;
  325. int local_trigger_count = atomic_read(&deferred_trigger_count);
  326. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  327. !drv->suppress_bind_attrs;
  328. #ifdef CONFIG_MTPROF
  329. unsigned long long ts = 0;
  330. #endif
  331. if (defer_all_probes) {
  332. /*
  333. * Value of defer_all_probes can be set only by
  334. * device_defer_all_probes_enable() which, in turn, will call
  335. * wait_for_device_probe() right after that to avoid any races.
  336. */
  337. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  338. driver_deferred_probe_add(dev);
  339. return ret;
  340. }
  341. ret = device_links_check_suppliers(dev);
  342. if (ret)
  343. return ret;
  344. atomic_inc(&probe_count);
  345. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  346. drv->bus->name, __func__, drv->name, dev_name(dev));
  347. if (!list_empty(&dev->devres_head)) {
  348. dev_crit(dev, "Resources present before probing\n");
  349. ret = -EBUSY;
  350. goto done;
  351. }
  352. re_probe:
  353. dev->driver = drv;
  354. /* If using pinctrl, bind pins now before probing */
  355. ret = pinctrl_bind_pins(dev);
  356. if (ret)
  357. goto pinctrl_bind_failed;
  358. ret = dma_configure(dev);
  359. if (ret)
  360. goto probe_failed;
  361. if (driver_sysfs_add(dev)) {
  362. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  363. __func__, dev_name(dev));
  364. goto probe_failed;
  365. }
  366. if (dev->pm_domain && dev->pm_domain->activate) {
  367. ret = dev->pm_domain->activate(dev);
  368. if (ret)
  369. goto probe_failed;
  370. }
  371. if (dev->bus->probe) {
  372. TIME_LOG_START();
  373. ret = dev->bus->probe(dev);
  374. TIME_LOG_END();
  375. bootprof_probe(ts, dev, drv, (unsigned long)dev->bus->probe);
  376. if (ret)
  377. goto probe_failed;
  378. } else if (drv->probe) {
  379. TIME_LOG_START();
  380. ret = drv->probe(dev);
  381. TIME_LOG_END();
  382. bootprof_probe(ts, dev, drv, (unsigned long)drv->probe);
  383. if (ret)
  384. goto probe_failed;
  385. }
  386. if (test_remove) {
  387. test_remove = false;
  388. if (dev->bus->remove)
  389. dev->bus->remove(dev);
  390. else if (drv->remove)
  391. drv->remove(dev);
  392. devres_release_all(dev);
  393. driver_sysfs_remove(dev);
  394. dev->driver = NULL;
  395. dev_set_drvdata(dev, NULL);
  396. if (dev->pm_domain && dev->pm_domain->dismiss)
  397. dev->pm_domain->dismiss(dev);
  398. pm_runtime_reinit(dev);
  399. goto re_probe;
  400. }
  401. pinctrl_init_done(dev);
  402. if (dev->pm_domain && dev->pm_domain->sync)
  403. dev->pm_domain->sync(dev);
  404. driver_bound(dev);
  405. ret = 1;
  406. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  407. drv->bus->name, __func__, dev_name(dev), drv->name);
  408. goto done;
  409. probe_failed:
  410. if (dev->bus)
  411. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  412. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  413. pinctrl_bind_failed:
  414. device_links_no_driver(dev);
  415. devres_release_all(dev);
  416. dma_deconfigure(dev);
  417. driver_sysfs_remove(dev);
  418. dev->driver = NULL;
  419. dev_set_drvdata(dev, NULL);
  420. if (dev->pm_domain && dev->pm_domain->dismiss)
  421. dev->pm_domain->dismiss(dev);
  422. pm_runtime_reinit(dev);
  423. switch (ret) {
  424. case -EPROBE_DEFER:
  425. /* Driver requested deferred probing */
  426. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  427. driver_deferred_probe_add(dev);
  428. /* Did a trigger occur while probing? Need to re-trigger if yes */
  429. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  430. driver_deferred_probe_trigger();
  431. break;
  432. case -ENODEV:
  433. case -ENXIO:
  434. pr_debug("%s: probe of %s rejects match %d\n",
  435. drv->name, dev_name(dev), ret);
  436. break;
  437. default:
  438. /* driver matched but the probe failed */
  439. printk(KERN_WARNING
  440. "%s: probe of %s failed with error %d\n",
  441. drv->name, dev_name(dev), ret);
  442. }
  443. /*
  444. * Ignore errors returned by ->probe so that the next driver can try
  445. * its luck.
  446. */
  447. ret = 0;
  448. done:
  449. atomic_dec(&probe_count);
  450. wake_up_all(&probe_waitqueue);
  451. return ret;
  452. }
  453. /**
  454. * driver_probe_done
  455. * Determine if the probe sequence is finished or not.
  456. *
  457. * Should somehow figure out how to use a semaphore, not an atomic variable...
  458. */
  459. int driver_probe_done(void)
  460. {
  461. pr_debug("%s: probe_count = %d\n", __func__,
  462. atomic_read(&probe_count));
  463. if (atomic_read(&probe_count))
  464. return -EBUSY;
  465. return 0;
  466. }
  467. /**
  468. * wait_for_device_probe
  469. * Wait for device probing to be completed.
  470. */
  471. void wait_for_device_probe(void)
  472. {
  473. /* wait for the deferred probe workqueue to finish */
  474. flush_work(&deferred_probe_work);
  475. /* wait for the known devices to complete their probing */
  476. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  477. async_synchronize_full();
  478. }
  479. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  480. /**
  481. * driver_probe_device - attempt to bind device & driver together
  482. * @drv: driver to bind a device to
  483. * @dev: device to try to bind to the driver
  484. *
  485. * This function returns -ENODEV if the device is not registered,
  486. * 1 if the device is bound successfully and 0 otherwise.
  487. *
  488. * This function must be called with @dev lock held. When called for a
  489. * USB interface, @dev->parent lock must be held as well.
  490. *
  491. * If the device has a parent, runtime-resume the parent before driver probing.
  492. */
  493. int driver_probe_device(struct device_driver *drv, struct device *dev)
  494. {
  495. int ret = 0;
  496. if (!device_is_registered(dev))
  497. return -ENODEV;
  498. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  499. drv->bus->name, __func__, dev_name(dev), drv->name);
  500. pm_runtime_get_suppliers(dev);
  501. if (dev->parent)
  502. pm_runtime_get_sync(dev->parent);
  503. pm_runtime_barrier(dev);
  504. ret = really_probe(dev, drv);
  505. pm_request_idle(dev);
  506. if (dev->parent)
  507. pm_runtime_put(dev->parent);
  508. pm_runtime_put_suppliers(dev);
  509. return ret;
  510. }
  511. bool driver_allows_async_probing(struct device_driver *drv)
  512. {
  513. switch (drv->probe_type) {
  514. case PROBE_PREFER_ASYNCHRONOUS:
  515. return true;
  516. case PROBE_FORCE_SYNCHRONOUS:
  517. return false;
  518. default:
  519. if (module_requested_async_probing(drv->owner))
  520. return true;
  521. return false;
  522. }
  523. }
  524. struct device_attach_data {
  525. struct device *dev;
  526. /*
  527. * Indicates whether we are are considering asynchronous probing or
  528. * not. Only initial binding after device or driver registration
  529. * (including deferral processing) may be done asynchronously, the
  530. * rest is always synchronous, as we expect it is being done by
  531. * request from userspace.
  532. */
  533. bool check_async;
  534. /*
  535. * Indicates if we are binding synchronous or asynchronous drivers.
  536. * When asynchronous probing is enabled we'll execute 2 passes
  537. * over drivers: first pass doing synchronous probing and second
  538. * doing asynchronous probing (if synchronous did not succeed -
  539. * most likely because there was no driver requiring synchronous
  540. * probing - and we found asynchronous driver during first pass).
  541. * The 2 passes are done because we can't shoot asynchronous
  542. * probe for given device and driver from bus_for_each_drv() since
  543. * driver pointer is not guaranteed to stay valid once
  544. * bus_for_each_drv() iterates to the next driver on the bus.
  545. */
  546. bool want_async;
  547. /*
  548. * We'll set have_async to 'true' if, while scanning for matching
  549. * driver, we'll encounter one that requests asynchronous probing.
  550. */
  551. bool have_async;
  552. };
  553. static int __device_attach_driver(struct device_driver *drv, void *_data)
  554. {
  555. struct device_attach_data *data = _data;
  556. struct device *dev = data->dev;
  557. bool async_allowed;
  558. int ret;
  559. /*
  560. * Check if device has already been claimed. This may
  561. * happen with driver loading, device discovery/registration,
  562. * and deferred probe processing happens all at once with
  563. * multiple threads.
  564. */
  565. if (dev->driver)
  566. return -EBUSY;
  567. ret = driver_match_device(drv, dev);
  568. if (ret == 0) {
  569. /* no match */
  570. return 0;
  571. } else if (ret == -EPROBE_DEFER) {
  572. dev_dbg(dev, "Device match requests probe deferral\n");
  573. driver_deferred_probe_add(dev);
  574. } else if (ret < 0) {
  575. dev_dbg(dev, "Bus failed to match device: %d", ret);
  576. return ret;
  577. } /* ret > 0 means positive match */
  578. async_allowed = driver_allows_async_probing(drv);
  579. if (async_allowed)
  580. data->have_async = true;
  581. if (data->check_async && async_allowed != data->want_async)
  582. return 0;
  583. return driver_probe_device(drv, dev);
  584. }
  585. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  586. {
  587. struct device *dev = _dev;
  588. struct device_attach_data data = {
  589. .dev = dev,
  590. .check_async = true,
  591. .want_async = true,
  592. };
  593. device_lock(dev);
  594. if (dev->parent)
  595. pm_runtime_get_sync(dev->parent);
  596. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  597. dev_dbg(dev, "async probe completed\n");
  598. pm_request_idle(dev);
  599. if (dev->parent)
  600. pm_runtime_put(dev->parent);
  601. device_unlock(dev);
  602. put_device(dev);
  603. }
  604. static int __device_attach(struct device *dev, bool allow_async)
  605. {
  606. int ret = 0;
  607. device_lock(dev);
  608. if (dev->driver) {
  609. if (device_is_bound(dev)) {
  610. ret = 1;
  611. goto out_unlock;
  612. }
  613. ret = device_bind_driver(dev);
  614. if (ret == 0)
  615. ret = 1;
  616. else {
  617. dev->driver = NULL;
  618. ret = 0;
  619. }
  620. } else {
  621. struct device_attach_data data = {
  622. .dev = dev,
  623. .check_async = allow_async,
  624. .want_async = false,
  625. };
  626. if (dev->parent)
  627. pm_runtime_get_sync(dev->parent);
  628. ret = bus_for_each_drv(dev->bus, NULL, &data,
  629. __device_attach_driver);
  630. if (!ret && allow_async && data.have_async) {
  631. /*
  632. * If we could not find appropriate driver
  633. * synchronously and we are allowed to do
  634. * async probes and there are drivers that
  635. * want to probe asynchronously, we'll
  636. * try them.
  637. */
  638. dev_dbg(dev, "scheduling asynchronous probe\n");
  639. get_device(dev);
  640. async_schedule(__device_attach_async_helper, dev);
  641. } else {
  642. pm_request_idle(dev);
  643. }
  644. if (dev->parent)
  645. pm_runtime_put(dev->parent);
  646. }
  647. out_unlock:
  648. device_unlock(dev);
  649. return ret;
  650. }
  651. /**
  652. * device_attach - try to attach device to a driver.
  653. * @dev: device.
  654. *
  655. * Walk the list of drivers that the bus has and call
  656. * driver_probe_device() for each pair. If a compatible
  657. * pair is found, break out and return.
  658. *
  659. * Returns 1 if the device was bound to a driver;
  660. * 0 if no matching driver was found;
  661. * -ENODEV if the device is not registered.
  662. *
  663. * When called for a USB interface, @dev->parent lock must be held.
  664. */
  665. int device_attach(struct device *dev)
  666. {
  667. return __device_attach(dev, false);
  668. }
  669. EXPORT_SYMBOL_GPL(device_attach);
  670. void device_initial_probe(struct device *dev)
  671. {
  672. __device_attach(dev, true);
  673. }
  674. static int __driver_attach(struct device *dev, void *data)
  675. {
  676. struct device_driver *drv = data;
  677. int ret;
  678. /*
  679. * Lock device and try to bind to it. We drop the error
  680. * here and always return 0, because we need to keep trying
  681. * to bind to devices and some drivers will return an error
  682. * simply if it didn't support the device.
  683. *
  684. * driver_probe_device() will spit a warning if there
  685. * is an error.
  686. */
  687. ret = driver_match_device(drv, dev);
  688. if (ret == 0) {
  689. /* no match */
  690. return 0;
  691. } else if (ret == -EPROBE_DEFER) {
  692. dev_dbg(dev, "Device match requests probe deferral\n");
  693. driver_deferred_probe_add(dev);
  694. } else if (ret < 0) {
  695. dev_dbg(dev, "Bus failed to match device: %d", ret);
  696. return ret;
  697. } /* ret > 0 means positive match */
  698. if (dev->parent) /* Needed for USB */
  699. device_lock(dev->parent);
  700. device_lock(dev);
  701. if (!dev->driver)
  702. driver_probe_device(drv, dev);
  703. device_unlock(dev);
  704. if (dev->parent)
  705. device_unlock(dev->parent);
  706. return 0;
  707. }
  708. /**
  709. * driver_attach - try to bind driver to devices.
  710. * @drv: driver.
  711. *
  712. * Walk the list of devices that the bus has on it and try to
  713. * match the driver with each one. If driver_probe_device()
  714. * returns 0 and the @dev->driver is set, we've found a
  715. * compatible pair.
  716. */
  717. int driver_attach(struct device_driver *drv)
  718. {
  719. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  720. }
  721. EXPORT_SYMBOL_GPL(driver_attach);
  722. /*
  723. * __device_release_driver() must be called with @dev lock held.
  724. * When called for a USB interface, @dev->parent lock must be held as well.
  725. */
  726. static void __device_release_driver(struct device *dev, struct device *parent)
  727. {
  728. struct device_driver *drv;
  729. drv = dev->driver;
  730. if (drv) {
  731. pm_runtime_get_sync(dev);
  732. while (device_links_busy(dev)) {
  733. device_unlock(dev);
  734. if (parent)
  735. device_unlock(parent);
  736. device_links_unbind_consumers(dev);
  737. if (parent)
  738. device_lock(parent);
  739. device_lock(dev);
  740. /*
  741. * A concurrent invocation of the same function might
  742. * have released the driver successfully while this one
  743. * was waiting, so check for that.
  744. */
  745. if (dev->driver != drv) {
  746. pm_runtime_put(dev);
  747. return;
  748. }
  749. }
  750. pm_runtime_clean_up_links(dev);
  751. driver_sysfs_remove(dev);
  752. if (dev->bus)
  753. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  754. BUS_NOTIFY_UNBIND_DRIVER,
  755. dev);
  756. pm_runtime_put_sync(dev);
  757. if (dev->bus && dev->bus->remove)
  758. dev->bus->remove(dev);
  759. else if (drv->remove)
  760. drv->remove(dev);
  761. device_links_driver_cleanup(dev);
  762. devres_release_all(dev);
  763. dma_deconfigure(dev);
  764. dev->driver = NULL;
  765. dev_set_drvdata(dev, NULL);
  766. if (dev->pm_domain && dev->pm_domain->dismiss)
  767. dev->pm_domain->dismiss(dev);
  768. pm_runtime_reinit(dev);
  769. klist_remove(&dev->p->knode_driver);
  770. device_pm_check_callbacks(dev);
  771. if (dev->bus)
  772. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  773. BUS_NOTIFY_UNBOUND_DRIVER,
  774. dev);
  775. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  776. }
  777. }
  778. void device_release_driver_internal(struct device *dev,
  779. struct device_driver *drv,
  780. struct device *parent)
  781. {
  782. if (parent)
  783. device_lock(parent);
  784. device_lock(dev);
  785. if (!drv || drv == dev->driver)
  786. __device_release_driver(dev, parent);
  787. device_unlock(dev);
  788. if (parent)
  789. device_unlock(parent);
  790. }
  791. /**
  792. * device_release_driver - manually detach device from driver.
  793. * @dev: device.
  794. *
  795. * Manually detach device from driver.
  796. * When called for a USB interface, @dev->parent lock must be held.
  797. *
  798. * If this function is to be called with @dev->parent lock held, ensure that
  799. * the device's consumers are unbound in advance or that their locks can be
  800. * acquired under the @dev->parent lock.
  801. */
  802. void device_release_driver(struct device *dev)
  803. {
  804. /*
  805. * If anyone calls device_release_driver() recursively from
  806. * within their ->remove callback for the same device, they
  807. * will deadlock right here.
  808. */
  809. device_release_driver_internal(dev, NULL, NULL);
  810. }
  811. EXPORT_SYMBOL_GPL(device_release_driver);
  812. /**
  813. * driver_detach - detach driver from all devices it controls.
  814. * @drv: driver.
  815. */
  816. void driver_detach(struct device_driver *drv)
  817. {
  818. struct device_private *dev_prv;
  819. struct device *dev;
  820. if (driver_allows_async_probing(drv))
  821. async_synchronize_full();
  822. for (;;) {
  823. spin_lock(&drv->p->klist_devices.k_lock);
  824. if (list_empty(&drv->p->klist_devices.k_list)) {
  825. spin_unlock(&drv->p->klist_devices.k_lock);
  826. break;
  827. }
  828. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  829. struct device_private,
  830. knode_driver.n_node);
  831. dev = dev_prv->device;
  832. get_device(dev);
  833. spin_unlock(&drv->p->klist_devices.k_lock);
  834. device_release_driver_internal(dev, drv, dev->parent);
  835. put_device(dev);
  836. }
  837. }