dd.c 26 KB

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