dd.c 25 KB

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