dd.c 26 KB

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