dd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/dd.c - The core device/driver interactions.
  4. *
  5. * This file contains the (sometimes tricky) code that controls the
  6. * interactions between devices and drivers, which primarily includes
  7. * driver binding and unbinding.
  8. *
  9. * All of this code used to exist in drivers/base/bus.c, but was
  10. * relocated to here in the name of compartmentalization (since it wasn't
  11. * strictly code just for the 'struct bus_type'.
  12. *
  13. * Copyright (c) 2002-5 Patrick Mochel
  14. * Copyright (c) 2002-3 Open Source Development Labs
  15. * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
  16. * Copyright (c) 2007-2009 Novell Inc.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/delay.h>
  20. #include <linux/dma-mapping.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. /**
  209. * deferred_probe_initcall() - Enable probing of deferred devices
  210. *
  211. * We don't want to get in the way when the bulk of drivers are getting probed.
  212. * Instead, this initcall makes sure that deferred probing is delayed until
  213. * late_initcall time.
  214. */
  215. static int deferred_probe_initcall(void)
  216. {
  217. driver_deferred_probe_enable = true;
  218. driver_deferred_probe_trigger();
  219. /* Sort as many dependencies as possible before exiting initcalls */
  220. flush_work(&deferred_probe_work);
  221. initcalls_done = true;
  222. return 0;
  223. }
  224. late_initcall(deferred_probe_initcall);
  225. /**
  226. * device_is_bound() - Check if device is bound to a driver
  227. * @dev: device to check
  228. *
  229. * Returns true if passed device has already finished probing successfully
  230. * against a driver.
  231. *
  232. * This function must be called with the device lock held.
  233. */
  234. bool device_is_bound(struct device *dev)
  235. {
  236. return dev->p && klist_node_attached(&dev->p->knode_driver);
  237. }
  238. static void driver_bound(struct device *dev)
  239. {
  240. if (device_is_bound(dev)) {
  241. printk(KERN_WARNING "%s: device %s already bound\n",
  242. __func__, kobject_name(&dev->kobj));
  243. return;
  244. }
  245. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
  246. __func__, dev_name(dev));
  247. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  248. device_links_driver_bound(dev);
  249. device_pm_check_callbacks(dev);
  250. /*
  251. * Make sure the device is no longer in one of the deferred lists and
  252. * kick off retrying all pending devices
  253. */
  254. driver_deferred_probe_del(dev);
  255. driver_deferred_probe_trigger();
  256. if (dev->bus)
  257. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  258. BUS_NOTIFY_BOUND_DRIVER, dev);
  259. kobject_uevent(&dev->kobj, KOBJ_BIND);
  260. }
  261. static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
  262. const char *buf, size_t count)
  263. {
  264. device_lock(dev);
  265. if (dev->driver->coredump)
  266. dev->driver->coredump(dev);
  267. device_unlock(dev);
  268. return count;
  269. }
  270. static DEVICE_ATTR_WO(coredump);
  271. static int driver_sysfs_add(struct device *dev)
  272. {
  273. int ret;
  274. if (dev->bus)
  275. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  276. BUS_NOTIFY_BIND_DRIVER, dev);
  277. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  278. kobject_name(&dev->kobj));
  279. if (ret)
  280. goto fail;
  281. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  282. "driver");
  283. if (ret)
  284. goto rm_dev;
  285. if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
  286. !device_create_file(dev, &dev_attr_coredump))
  287. return 0;
  288. sysfs_remove_link(&dev->kobj, "driver");
  289. rm_dev:
  290. sysfs_remove_link(&dev->driver->p->kobj,
  291. kobject_name(&dev->kobj));
  292. fail:
  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. if (drv->coredump)
  300. device_remove_file(dev, &dev_attr_coredump);
  301. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  302. sysfs_remove_link(&dev->kobj, "driver");
  303. }
  304. }
  305. /**
  306. * device_bind_driver - bind a driver to one device.
  307. * @dev: device.
  308. *
  309. * Allow manual attachment of a driver to a device.
  310. * Caller must have already set @dev->driver.
  311. *
  312. * Note that this does not modify the bus reference count
  313. * nor take the bus's rwsem. Please verify those are accounted
  314. * for before calling this. (It is ok to call with no other effort
  315. * from a driver's probe() method.)
  316. *
  317. * This function must be called with the device lock held.
  318. */
  319. int device_bind_driver(struct device *dev)
  320. {
  321. int ret;
  322. ret = driver_sysfs_add(dev);
  323. if (!ret)
  324. driver_bound(dev);
  325. else if (dev->bus)
  326. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  327. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  328. return ret;
  329. }
  330. EXPORT_SYMBOL_GPL(device_bind_driver);
  331. static atomic_t probe_count = ATOMIC_INIT(0);
  332. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  333. static void driver_deferred_probe_add_trigger(struct device *dev,
  334. int local_trigger_count)
  335. {
  336. driver_deferred_probe_add(dev);
  337. /* Did a trigger occur while probing? Need to re-trigger if yes */
  338. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  339. driver_deferred_probe_trigger();
  340. }
  341. static int really_probe(struct device *dev, struct device_driver *drv)
  342. {
  343. int ret = -EPROBE_DEFER;
  344. int local_trigger_count = atomic_read(&deferred_trigger_count);
  345. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  346. !drv->suppress_bind_attrs;
  347. if (defer_all_probes) {
  348. /*
  349. * Value of defer_all_probes can be set only by
  350. * device_defer_all_probes_enable() which, in turn, will call
  351. * wait_for_device_probe() right after that to avoid any races.
  352. */
  353. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  354. driver_deferred_probe_add(dev);
  355. return ret;
  356. }
  357. ret = device_links_check_suppliers(dev);
  358. if (ret == -EPROBE_DEFER)
  359. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  360. if (ret)
  361. return ret;
  362. atomic_inc(&probe_count);
  363. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  364. drv->bus->name, __func__, drv->name, dev_name(dev));
  365. WARN_ON(!list_empty(&dev->devres_head));
  366. re_probe:
  367. dev->driver = drv;
  368. /* If using pinctrl, bind pins now before probing */
  369. ret = pinctrl_bind_pins(dev);
  370. if (ret)
  371. goto pinctrl_bind_failed;
  372. ret = dma_configure(dev);
  373. if (ret)
  374. goto dma_failed;
  375. if (driver_sysfs_add(dev)) {
  376. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  377. __func__, dev_name(dev));
  378. goto probe_failed;
  379. }
  380. if (dev->pm_domain && dev->pm_domain->activate) {
  381. ret = dev->pm_domain->activate(dev);
  382. if (ret)
  383. goto probe_failed;
  384. }
  385. /*
  386. * Ensure devices are listed in devices_kset in correct order
  387. * It's important to move Dev to the end of devices_kset before
  388. * calling .probe, because it could be recursive and parent Dev
  389. * should always go first
  390. */
  391. devices_kset_move_last(dev);
  392. if (dev->bus->probe) {
  393. ret = dev->bus->probe(dev);
  394. if (ret)
  395. goto probe_failed;
  396. } else if (drv->probe) {
  397. ret = drv->probe(dev);
  398. if (ret)
  399. goto probe_failed;
  400. }
  401. if (test_remove) {
  402. test_remove = false;
  403. if (dev->bus->remove)
  404. dev->bus->remove(dev);
  405. else if (drv->remove)
  406. drv->remove(dev);
  407. devres_release_all(dev);
  408. driver_sysfs_remove(dev);
  409. dev->driver = NULL;
  410. dev_set_drvdata(dev, NULL);
  411. if (dev->pm_domain && dev->pm_domain->dismiss)
  412. dev->pm_domain->dismiss(dev);
  413. pm_runtime_reinit(dev);
  414. goto re_probe;
  415. }
  416. pinctrl_init_done(dev);
  417. if (dev->pm_domain && dev->pm_domain->sync)
  418. dev->pm_domain->sync(dev);
  419. driver_bound(dev);
  420. ret = 1;
  421. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  422. drv->bus->name, __func__, dev_name(dev), drv->name);
  423. goto done;
  424. probe_failed:
  425. dma_deconfigure(dev);
  426. dma_failed:
  427. if (dev->bus)
  428. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  429. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  430. pinctrl_bind_failed:
  431. device_links_no_driver(dev);
  432. devres_release_all(dev);
  433. driver_sysfs_remove(dev);
  434. dev->driver = NULL;
  435. dev_set_drvdata(dev, NULL);
  436. if (dev->pm_domain && dev->pm_domain->dismiss)
  437. dev->pm_domain->dismiss(dev);
  438. pm_runtime_reinit(dev);
  439. dev_pm_set_driver_flags(dev, 0);
  440. switch (ret) {
  441. case -EPROBE_DEFER:
  442. /* Driver requested deferred probing */
  443. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  444. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  445. break;
  446. case -ENODEV:
  447. case -ENXIO:
  448. pr_debug("%s: probe of %s rejects match %d\n",
  449. drv->name, dev_name(dev), ret);
  450. break;
  451. default:
  452. /* driver matched but the probe failed */
  453. printk(KERN_WARNING
  454. "%s: probe of %s failed with error %d\n",
  455. drv->name, dev_name(dev), ret);
  456. }
  457. /*
  458. * Ignore errors returned by ->probe so that the next driver can try
  459. * its luck.
  460. */
  461. ret = 0;
  462. done:
  463. atomic_dec(&probe_count);
  464. wake_up(&probe_waitqueue);
  465. return ret;
  466. }
  467. /**
  468. * driver_probe_done
  469. * Determine if the probe sequence is finished or not.
  470. *
  471. * Should somehow figure out how to use a semaphore, not an atomic variable...
  472. */
  473. int driver_probe_done(void)
  474. {
  475. pr_debug("%s: probe_count = %d\n", __func__,
  476. atomic_read(&probe_count));
  477. if (atomic_read(&probe_count))
  478. return -EBUSY;
  479. return 0;
  480. }
  481. /**
  482. * wait_for_device_probe
  483. * Wait for device probing to be completed.
  484. */
  485. void wait_for_device_probe(void)
  486. {
  487. /* wait for the deferred probe workqueue to finish */
  488. flush_work(&deferred_probe_work);
  489. /* wait for the known devices to complete their probing */
  490. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  491. async_synchronize_full();
  492. }
  493. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  494. /**
  495. * driver_probe_device - attempt to bind device & driver together
  496. * @drv: driver to bind a device to
  497. * @dev: device to try to bind to the driver
  498. *
  499. * This function returns -ENODEV if the device is not registered,
  500. * 1 if the device is bound successfully and 0 otherwise.
  501. *
  502. * This function must be called with @dev lock held. When called for a
  503. * USB interface, @dev->parent lock must be held as well.
  504. *
  505. * If the device has a parent, runtime-resume the parent before driver probing.
  506. */
  507. int driver_probe_device(struct device_driver *drv, struct device *dev)
  508. {
  509. int ret = 0;
  510. if (!device_is_registered(dev))
  511. return -ENODEV;
  512. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  513. drv->bus->name, __func__, dev_name(dev), drv->name);
  514. pm_runtime_get_suppliers(dev);
  515. if (dev->parent)
  516. pm_runtime_get_sync(dev->parent);
  517. pm_runtime_barrier(dev);
  518. ret = really_probe(dev, drv);
  519. pm_request_idle(dev);
  520. if (dev->parent)
  521. pm_runtime_put(dev->parent);
  522. pm_runtime_put_suppliers(dev);
  523. return ret;
  524. }
  525. bool driver_allows_async_probing(struct device_driver *drv)
  526. {
  527. switch (drv->probe_type) {
  528. case PROBE_PREFER_ASYNCHRONOUS:
  529. return true;
  530. case PROBE_FORCE_SYNCHRONOUS:
  531. return false;
  532. default:
  533. if (module_requested_async_probing(drv->owner))
  534. return true;
  535. return false;
  536. }
  537. }
  538. struct device_attach_data {
  539. struct device *dev;
  540. /*
  541. * Indicates whether we are are considering asynchronous probing or
  542. * not. Only initial binding after device or driver registration
  543. * (including deferral processing) may be done asynchronously, the
  544. * rest is always synchronous, as we expect it is being done by
  545. * request from userspace.
  546. */
  547. bool check_async;
  548. /*
  549. * Indicates if we are binding synchronous or asynchronous drivers.
  550. * When asynchronous probing is enabled we'll execute 2 passes
  551. * over drivers: first pass doing synchronous probing and second
  552. * doing asynchronous probing (if synchronous did not succeed -
  553. * most likely because there was no driver requiring synchronous
  554. * probing - and we found asynchronous driver during first pass).
  555. * The 2 passes are done because we can't shoot asynchronous
  556. * probe for given device and driver from bus_for_each_drv() since
  557. * driver pointer is not guaranteed to stay valid once
  558. * bus_for_each_drv() iterates to the next driver on the bus.
  559. */
  560. bool want_async;
  561. /*
  562. * We'll set have_async to 'true' if, while scanning for matching
  563. * driver, we'll encounter one that requests asynchronous probing.
  564. */
  565. bool have_async;
  566. };
  567. static int __device_attach_driver(struct device_driver *drv, void *_data)
  568. {
  569. struct device_attach_data *data = _data;
  570. struct device *dev = data->dev;
  571. bool async_allowed;
  572. int ret;
  573. /*
  574. * Check if device has already been claimed. This may
  575. * happen with driver loading, device discovery/registration,
  576. * and deferred probe processing happens all at once with
  577. * multiple threads.
  578. */
  579. if (dev->driver)
  580. return -EBUSY;
  581. ret = driver_match_device(drv, dev);
  582. if (ret == 0) {
  583. /* no match */
  584. return 0;
  585. } else if (ret == -EPROBE_DEFER) {
  586. dev_dbg(dev, "Device match requests probe deferral\n");
  587. driver_deferred_probe_add(dev);
  588. } else if (ret < 0) {
  589. dev_dbg(dev, "Bus failed to match device: %d", ret);
  590. return ret;
  591. } /* ret > 0 means positive match */
  592. async_allowed = driver_allows_async_probing(drv);
  593. if (async_allowed)
  594. data->have_async = true;
  595. if (data->check_async && async_allowed != data->want_async)
  596. return 0;
  597. return driver_probe_device(drv, dev);
  598. }
  599. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  600. {
  601. struct device *dev = _dev;
  602. struct device_attach_data data = {
  603. .dev = dev,
  604. .check_async = true,
  605. .want_async = true,
  606. };
  607. device_lock(dev);
  608. if (dev->parent)
  609. pm_runtime_get_sync(dev->parent);
  610. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  611. dev_dbg(dev, "async probe completed\n");
  612. pm_request_idle(dev);
  613. if (dev->parent)
  614. pm_runtime_put(dev->parent);
  615. device_unlock(dev);
  616. put_device(dev);
  617. }
  618. static int __device_attach(struct device *dev, bool allow_async)
  619. {
  620. int ret = 0;
  621. device_lock(dev);
  622. if (dev->driver) {
  623. if (device_is_bound(dev)) {
  624. ret = 1;
  625. goto out_unlock;
  626. }
  627. ret = device_bind_driver(dev);
  628. if (ret == 0)
  629. ret = 1;
  630. else {
  631. dev->driver = NULL;
  632. ret = 0;
  633. }
  634. } else {
  635. struct device_attach_data data = {
  636. .dev = dev,
  637. .check_async = allow_async,
  638. .want_async = false,
  639. };
  640. if (dev->parent)
  641. pm_runtime_get_sync(dev->parent);
  642. ret = bus_for_each_drv(dev->bus, NULL, &data,
  643. __device_attach_driver);
  644. if (!ret && allow_async && data.have_async) {
  645. /*
  646. * If we could not find appropriate driver
  647. * synchronously and we are allowed to do
  648. * async probes and there are drivers that
  649. * want to probe asynchronously, we'll
  650. * try them.
  651. */
  652. dev_dbg(dev, "scheduling asynchronous probe\n");
  653. get_device(dev);
  654. async_schedule(__device_attach_async_helper, dev);
  655. } else {
  656. pm_request_idle(dev);
  657. }
  658. if (dev->parent)
  659. pm_runtime_put(dev->parent);
  660. }
  661. out_unlock:
  662. device_unlock(dev);
  663. return ret;
  664. }
  665. /**
  666. * device_attach - try to attach device to a driver.
  667. * @dev: device.
  668. *
  669. * Walk the list of drivers that the bus has and call
  670. * driver_probe_device() for each pair. If a compatible
  671. * pair is found, break out and return.
  672. *
  673. * Returns 1 if the device was bound to a driver;
  674. * 0 if no matching driver was found;
  675. * -ENODEV if the device is not registered.
  676. *
  677. * When called for a USB interface, @dev->parent lock must be held.
  678. */
  679. int device_attach(struct device *dev)
  680. {
  681. return __device_attach(dev, false);
  682. }
  683. EXPORT_SYMBOL_GPL(device_attach);
  684. void device_initial_probe(struct device *dev)
  685. {
  686. __device_attach(dev, true);
  687. }
  688. static int __driver_attach(struct device *dev, void *data)
  689. {
  690. struct device_driver *drv = data;
  691. int ret;
  692. /*
  693. * Lock device and try to bind to it. We drop the error
  694. * here and always return 0, because we need to keep trying
  695. * to bind to devices and some drivers will return an error
  696. * simply if it didn't support the device.
  697. *
  698. * driver_probe_device() will spit a warning if there
  699. * is an error.
  700. */
  701. ret = driver_match_device(drv, dev);
  702. if (ret == 0) {
  703. /* no match */
  704. return 0;
  705. } else if (ret == -EPROBE_DEFER) {
  706. dev_dbg(dev, "Device match requests probe deferral\n");
  707. driver_deferred_probe_add(dev);
  708. } else if (ret < 0) {
  709. dev_dbg(dev, "Bus failed to match device: %d", ret);
  710. return ret;
  711. } /* ret > 0 means positive match */
  712. if (dev->parent) /* Needed for USB */
  713. device_lock(dev->parent);
  714. device_lock(dev);
  715. if (!dev->driver)
  716. driver_probe_device(drv, dev);
  717. device_unlock(dev);
  718. if (dev->parent)
  719. device_unlock(dev->parent);
  720. return 0;
  721. }
  722. /**
  723. * driver_attach - try to bind driver to devices.
  724. * @drv: driver.
  725. *
  726. * Walk the list of devices that the bus has on it and try to
  727. * match the driver with each one. If driver_probe_device()
  728. * returns 0 and the @dev->driver is set, we've found a
  729. * compatible pair.
  730. */
  731. int driver_attach(struct device_driver *drv)
  732. {
  733. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  734. }
  735. EXPORT_SYMBOL_GPL(driver_attach);
  736. /*
  737. * __device_release_driver() must be called with @dev lock held.
  738. * When called for a USB interface, @dev->parent lock must be held as well.
  739. */
  740. static void __device_release_driver(struct device *dev, struct device *parent)
  741. {
  742. struct device_driver *drv;
  743. drv = dev->driver;
  744. if (drv) {
  745. if (driver_allows_async_probing(drv))
  746. async_synchronize_full();
  747. while (device_links_busy(dev)) {
  748. device_unlock(dev);
  749. if (parent)
  750. device_unlock(parent);
  751. device_links_unbind_consumers(dev);
  752. if (parent)
  753. device_lock(parent);
  754. device_lock(dev);
  755. /*
  756. * A concurrent invocation of the same function might
  757. * have released the driver successfully while this one
  758. * was waiting, so check for that.
  759. */
  760. if (dev->driver != drv)
  761. return;
  762. }
  763. pm_runtime_get_sync(dev);
  764. pm_runtime_clean_up_links(dev);
  765. driver_sysfs_remove(dev);
  766. if (dev->bus)
  767. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  768. BUS_NOTIFY_UNBIND_DRIVER,
  769. dev);
  770. pm_runtime_put_sync(dev);
  771. if (dev->bus && dev->bus->remove)
  772. dev->bus->remove(dev);
  773. else if (drv->remove)
  774. drv->remove(dev);
  775. device_links_driver_cleanup(dev);
  776. dma_deconfigure(dev);
  777. devres_release_all(dev);
  778. dev->driver = NULL;
  779. dev_set_drvdata(dev, NULL);
  780. if (dev->pm_domain && dev->pm_domain->dismiss)
  781. dev->pm_domain->dismiss(dev);
  782. pm_runtime_reinit(dev);
  783. dev_pm_set_driver_flags(dev, 0);
  784. klist_remove(&dev->p->knode_driver);
  785. device_pm_check_callbacks(dev);
  786. if (dev->bus)
  787. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  788. BUS_NOTIFY_UNBOUND_DRIVER,
  789. dev);
  790. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  791. }
  792. }
  793. void device_release_driver_internal(struct device *dev,
  794. struct device_driver *drv,
  795. struct device *parent)
  796. {
  797. if (parent)
  798. device_lock(parent);
  799. device_lock(dev);
  800. if (!drv || drv == dev->driver)
  801. __device_release_driver(dev, parent);
  802. device_unlock(dev);
  803. if (parent)
  804. device_unlock(parent);
  805. }
  806. /**
  807. * device_release_driver - manually detach device from driver.
  808. * @dev: device.
  809. *
  810. * Manually detach device from driver.
  811. * When called for a USB interface, @dev->parent lock must be held.
  812. *
  813. * If this function is to be called with @dev->parent lock held, ensure that
  814. * the device's consumers are unbound in advance or that their locks can be
  815. * acquired under the @dev->parent lock.
  816. */
  817. void device_release_driver(struct device *dev)
  818. {
  819. /*
  820. * If anyone calls device_release_driver() recursively from
  821. * within their ->remove callback for the same device, they
  822. * will deadlock right here.
  823. */
  824. device_release_driver_internal(dev, NULL, NULL);
  825. }
  826. EXPORT_SYMBOL_GPL(device_release_driver);
  827. /**
  828. * driver_detach - detach driver from all devices it controls.
  829. * @drv: driver.
  830. */
  831. void driver_detach(struct device_driver *drv)
  832. {
  833. struct device_private *dev_prv;
  834. struct device *dev;
  835. for (;;) {
  836. spin_lock(&drv->p->klist_devices.k_lock);
  837. if (list_empty(&drv->p->klist_devices.k_list)) {
  838. spin_unlock(&drv->p->klist_devices.k_lock);
  839. break;
  840. }
  841. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  842. struct device_private,
  843. knode_driver.n_node);
  844. dev = dev_prv->device;
  845. get_device(dev);
  846. spin_unlock(&drv->p->klist_devices.k_lock);
  847. device_release_driver_internal(dev, drv, dev->parent);
  848. put_device(dev);
  849. }
  850. }