dd.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  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/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-map-ops.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/kthread.h>
  25. #include <linux/wait.h>
  26. #include <linux/async.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pinctrl/devinfo.h>
  29. #include <linux/slab.h>
  30. #include "base.h"
  31. #include "power/power.h"
  32. /*
  33. * Deferred Probe infrastructure.
  34. *
  35. * Sometimes driver probe order matters, but the kernel doesn't always have
  36. * dependency information which means some drivers will get probed before a
  37. * resource it depends on is available. For example, an SDHCI driver may
  38. * first need a GPIO line from an i2c GPIO controller before it can be
  39. * initialized. If a required resource is not available yet, a driver can
  40. * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
  41. *
  42. * Deferred probe maintains two lists of devices, a pending list and an active
  43. * list. A driver returning -EPROBE_DEFER causes the device to be added to the
  44. * pending list. A successful driver probe will trigger moving all devices
  45. * from the pending to the active list so that the workqueue will eventually
  46. * retry them.
  47. *
  48. * The deferred_probe_mutex must be held any time the deferred_probe_*_list
  49. * of the (struct device*)->p->deferred_probe pointers are manipulated
  50. */
  51. static DEFINE_MUTEX(deferred_probe_mutex);
  52. static LIST_HEAD(deferred_probe_pending_list);
  53. static LIST_HEAD(deferred_probe_active_list);
  54. static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
  55. static struct dentry *deferred_devices;
  56. static bool initcalls_done;
  57. /* Save the async probe drivers' name from kernel cmdline */
  58. #define ASYNC_DRV_NAMES_MAX_LEN 256
  59. static char async_probe_drv_names[ASYNC_DRV_NAMES_MAX_LEN];
  60. /*
  61. * In some cases, like suspend to RAM or hibernation, It might be reasonable
  62. * to prohibit probing of devices as it could be unsafe.
  63. * Once defer_all_probes is true all drivers probes will be forcibly deferred.
  64. */
  65. static bool defer_all_probes;
  66. /*
  67. * deferred_probe_work_func() - Retry probing devices in the active list.
  68. */
  69. static void deferred_probe_work_func(struct work_struct *work)
  70. {
  71. struct device *dev;
  72. struct device_private *private;
  73. /*
  74. * This block processes every device in the deferred 'active' list.
  75. * Each device is removed from the active list and passed to
  76. * bus_probe_device() to re-attempt the probe. The loop continues
  77. * until every device in the active list is removed and retried.
  78. *
  79. * Note: Once the device is removed from the list and the mutex is
  80. * released, it is possible for the device get freed by another thread
  81. * and cause a illegal pointer dereference. This code uses
  82. * get/put_device() to ensure the device structure cannot disappear
  83. * from under our feet.
  84. */
  85. mutex_lock(&deferred_probe_mutex);
  86. while (!list_empty(&deferred_probe_active_list)) {
  87. private = list_first_entry(&deferred_probe_active_list,
  88. typeof(*dev->p), deferred_probe);
  89. dev = private->device;
  90. list_del_init(&private->deferred_probe);
  91. get_device(dev);
  92. kfree(dev->p->deferred_probe_reason);
  93. dev->p->deferred_probe_reason = NULL;
  94. /*
  95. * Drop the mutex while probing each device; the probe path may
  96. * manipulate the deferred list
  97. */
  98. mutex_unlock(&deferred_probe_mutex);
  99. /*
  100. * Force the device to the end of the dpm_list since
  101. * the PM code assumes that the order we add things to
  102. * the list is a good order for suspend but deferred
  103. * probe makes that very unsafe.
  104. */
  105. device_pm_move_to_tail(dev);
  106. dev_dbg(dev, "Retrying from deferred list\n");
  107. bus_probe_device(dev);
  108. mutex_lock(&deferred_probe_mutex);
  109. put_device(dev);
  110. }
  111. mutex_unlock(&deferred_probe_mutex);
  112. }
  113. static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
  114. void driver_deferred_probe_add(struct device *dev)
  115. {
  116. mutex_lock(&deferred_probe_mutex);
  117. if (list_empty(&dev->p->deferred_probe)) {
  118. dev_dbg(dev, "Added to deferred list\n");
  119. list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
  120. }
  121. mutex_unlock(&deferred_probe_mutex);
  122. }
  123. void driver_deferred_probe_del(struct device *dev)
  124. {
  125. mutex_lock(&deferred_probe_mutex);
  126. if (!list_empty(&dev->p->deferred_probe)) {
  127. dev_dbg(dev, "Removed from deferred list\n");
  128. list_del_init(&dev->p->deferred_probe);
  129. kfree(dev->p->deferred_probe_reason);
  130. dev->p->deferred_probe_reason = NULL;
  131. }
  132. mutex_unlock(&deferred_probe_mutex);
  133. }
  134. static bool driver_deferred_probe_enable = false;
  135. /**
  136. * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
  137. *
  138. * This functions moves all devices from the pending list to the active
  139. * list and schedules the deferred probe workqueue to process them. It
  140. * should be called anytime a driver is successfully bound to a device.
  141. *
  142. * Note, there is a race condition in multi-threaded probe. In the case where
  143. * more than one device is probing at the same time, it is possible for one
  144. * probe to complete successfully while another is about to defer. If the second
  145. * depends on the first, then it will get put on the pending list after the
  146. * trigger event has already occurred and will be stuck there.
  147. *
  148. * The atomic 'deferred_trigger_count' is used to determine if a successful
  149. * trigger has occurred in the midst of probing a driver. If the trigger count
  150. * changes in the midst of a probe, then deferred processing should be triggered
  151. * again.
  152. */
  153. static void driver_deferred_probe_trigger(void)
  154. {
  155. if (!driver_deferred_probe_enable)
  156. return;
  157. /*
  158. * A successful probe means that all the devices in the pending list
  159. * should be triggered to be reprobed. Move all the deferred devices
  160. * into the active list so they can be retried by the workqueue
  161. */
  162. mutex_lock(&deferred_probe_mutex);
  163. atomic_inc(&deferred_trigger_count);
  164. list_splice_tail_init(&deferred_probe_pending_list,
  165. &deferred_probe_active_list);
  166. mutex_unlock(&deferred_probe_mutex);
  167. /*
  168. * Kick the re-probe thread. It may already be scheduled, but it is
  169. * safe to kick it again.
  170. */
  171. queue_work(system_unbound_wq, &deferred_probe_work);
  172. }
  173. /**
  174. * device_block_probing() - Block/defer device's probes
  175. *
  176. * It will disable probing of devices and defer their probes instead.
  177. */
  178. void device_block_probing(void)
  179. {
  180. defer_all_probes = true;
  181. /* sync with probes to avoid races. */
  182. wait_for_device_probe();
  183. }
  184. /**
  185. * device_unblock_probing() - Unblock/enable device's probes
  186. *
  187. * It will restore normal behavior and trigger re-probing of deferred
  188. * devices.
  189. */
  190. void device_unblock_probing(void)
  191. {
  192. defer_all_probes = false;
  193. driver_deferred_probe_trigger();
  194. }
  195. /**
  196. * device_set_deferred_probe_reason() - Set defer probe reason message for device
  197. * @dev: the pointer to the struct device
  198. * @vaf: the pointer to va_format structure with message
  199. */
  200. void device_set_deferred_probe_reason(const struct device *dev, struct va_format *vaf)
  201. {
  202. const char *drv = dev_driver_string(dev);
  203. mutex_lock(&deferred_probe_mutex);
  204. kfree(dev->p->deferred_probe_reason);
  205. dev->p->deferred_probe_reason = kasprintf(GFP_KERNEL, "%s: %pV", drv, vaf);
  206. mutex_unlock(&deferred_probe_mutex);
  207. }
  208. /*
  209. * deferred_devs_show() - Show the devices in the deferred probe pending list.
  210. */
  211. static int deferred_devs_show(struct seq_file *s, void *data)
  212. {
  213. struct device_private *curr;
  214. mutex_lock(&deferred_probe_mutex);
  215. list_for_each_entry(curr, &deferred_probe_pending_list, deferred_probe)
  216. seq_printf(s, "%s\t%s", dev_name(curr->device),
  217. curr->device->p->deferred_probe_reason ?: "\n");
  218. mutex_unlock(&deferred_probe_mutex);
  219. return 0;
  220. }
  221. DEFINE_SHOW_ATTRIBUTE(deferred_devs);
  222. int driver_deferred_probe_timeout;
  223. EXPORT_SYMBOL_GPL(driver_deferred_probe_timeout);
  224. static DECLARE_WAIT_QUEUE_HEAD(probe_timeout_waitqueue);
  225. static int __init deferred_probe_timeout_setup(char *str)
  226. {
  227. int timeout;
  228. if (!kstrtoint(str, 10, &timeout))
  229. driver_deferred_probe_timeout = timeout;
  230. return 1;
  231. }
  232. __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
  233. /**
  234. * driver_deferred_probe_check_state() - Check deferred probe state
  235. * @dev: device to check
  236. *
  237. * Return:
  238. * -ENODEV if initcalls have completed and modules are disabled.
  239. * -ETIMEDOUT if the deferred probe timeout was set and has expired
  240. * and modules are enabled.
  241. * -EPROBE_DEFER in other cases.
  242. *
  243. * Drivers or subsystems can opt-in to calling this function instead of directly
  244. * returning -EPROBE_DEFER.
  245. */
  246. int driver_deferred_probe_check_state(struct device *dev)
  247. {
  248. if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
  249. dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
  250. return -ENODEV;
  251. }
  252. if (!driver_deferred_probe_timeout && initcalls_done) {
  253. dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
  254. return -ETIMEDOUT;
  255. }
  256. return -EPROBE_DEFER;
  257. }
  258. static void deferred_probe_timeout_work_func(struct work_struct *work)
  259. {
  260. struct device_private *p;
  261. driver_deferred_probe_timeout = 0;
  262. driver_deferred_probe_trigger();
  263. flush_work(&deferred_probe_work);
  264. mutex_lock(&deferred_probe_mutex);
  265. list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
  266. dev_info(p->device, "deferred probe pending\n");
  267. mutex_unlock(&deferred_probe_mutex);
  268. wake_up_all(&probe_timeout_waitqueue);
  269. }
  270. static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
  271. /**
  272. * deferred_probe_initcall() - Enable probing of deferred devices
  273. *
  274. * We don't want to get in the way when the bulk of drivers are getting probed.
  275. * Instead, this initcall makes sure that deferred probing is delayed until
  276. * late_initcall time.
  277. */
  278. static int deferred_probe_initcall(void)
  279. {
  280. deferred_devices = debugfs_create_file("devices_deferred", 0444, NULL,
  281. NULL, &deferred_devs_fops);
  282. driver_deferred_probe_enable = true;
  283. driver_deferred_probe_trigger();
  284. /* Sort as many dependencies as possible before exiting initcalls */
  285. flush_work(&deferred_probe_work);
  286. initcalls_done = true;
  287. /*
  288. * Trigger deferred probe again, this time we won't defer anything
  289. * that is optional
  290. */
  291. driver_deferred_probe_trigger();
  292. flush_work(&deferred_probe_work);
  293. if (driver_deferred_probe_timeout > 0) {
  294. schedule_delayed_work(&deferred_probe_timeout_work,
  295. driver_deferred_probe_timeout * HZ);
  296. }
  297. return 0;
  298. }
  299. late_initcall(deferred_probe_initcall);
  300. static void __exit deferred_probe_exit(void)
  301. {
  302. debugfs_remove_recursive(deferred_devices);
  303. }
  304. __exitcall(deferred_probe_exit);
  305. /**
  306. * device_is_bound() - Check if device is bound to a driver
  307. * @dev: device to check
  308. *
  309. * Returns true if passed device has already finished probing successfully
  310. * against a driver.
  311. *
  312. * This function must be called with the device lock held.
  313. */
  314. bool device_is_bound(struct device *dev)
  315. {
  316. return dev->p && klist_node_attached(&dev->p->knode_driver);
  317. }
  318. static void driver_bound(struct device *dev)
  319. {
  320. if (device_is_bound(dev)) {
  321. pr_warn("%s: device %s already bound\n",
  322. __func__, kobject_name(&dev->kobj));
  323. return;
  324. }
  325. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
  326. __func__, dev_name(dev));
  327. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  328. device_links_driver_bound(dev);
  329. device_pm_check_callbacks(dev);
  330. /*
  331. * Make sure the device is no longer in one of the deferred lists and
  332. * kick off retrying all pending devices
  333. */
  334. driver_deferred_probe_del(dev);
  335. driver_deferred_probe_trigger();
  336. if (dev->bus)
  337. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  338. BUS_NOTIFY_BOUND_DRIVER, dev);
  339. kobject_uevent(&dev->kobj, KOBJ_BIND);
  340. }
  341. static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
  342. const char *buf, size_t count)
  343. {
  344. device_lock(dev);
  345. dev->driver->coredump(dev);
  346. device_unlock(dev);
  347. return count;
  348. }
  349. static DEVICE_ATTR_WO(coredump);
  350. static int driver_sysfs_add(struct device *dev)
  351. {
  352. int ret;
  353. if (dev->bus)
  354. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  355. BUS_NOTIFY_BIND_DRIVER, dev);
  356. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  357. kobject_name(&dev->kobj));
  358. if (ret)
  359. goto fail;
  360. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  361. "driver");
  362. if (ret)
  363. goto rm_dev;
  364. if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump ||
  365. !device_create_file(dev, &dev_attr_coredump))
  366. return 0;
  367. sysfs_remove_link(&dev->kobj, "driver");
  368. rm_dev:
  369. sysfs_remove_link(&dev->driver->p->kobj,
  370. kobject_name(&dev->kobj));
  371. fail:
  372. return ret;
  373. }
  374. static void driver_sysfs_remove(struct device *dev)
  375. {
  376. struct device_driver *drv = dev->driver;
  377. if (drv) {
  378. if (drv->coredump)
  379. device_remove_file(dev, &dev_attr_coredump);
  380. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  381. sysfs_remove_link(&dev->kobj, "driver");
  382. }
  383. }
  384. /**
  385. * device_bind_driver - bind a driver to one device.
  386. * @dev: device.
  387. *
  388. * Allow manual attachment of a driver to a device.
  389. * Caller must have already set @dev->driver.
  390. *
  391. * Note that this does not modify the bus reference count.
  392. * Please verify that is accounted for before calling this.
  393. * (It is ok to call with no other effort from a driver's probe() method.)
  394. *
  395. * This function must be called with the device lock held.
  396. */
  397. int device_bind_driver(struct device *dev)
  398. {
  399. int ret;
  400. ret = driver_sysfs_add(dev);
  401. if (!ret)
  402. driver_bound(dev);
  403. else if (dev->bus)
  404. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  405. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  406. return ret;
  407. }
  408. EXPORT_SYMBOL_GPL(device_bind_driver);
  409. static atomic_t probe_count = ATOMIC_INIT(0);
  410. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  411. static void driver_deferred_probe_add_trigger(struct device *dev,
  412. int local_trigger_count)
  413. {
  414. driver_deferred_probe_add(dev);
  415. /* Did a trigger occur while probing? Need to re-trigger if yes */
  416. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  417. driver_deferred_probe_trigger();
  418. }
  419. static ssize_t state_synced_show(struct device *dev,
  420. struct device_attribute *attr, char *buf)
  421. {
  422. bool val;
  423. device_lock(dev);
  424. val = dev->state_synced;
  425. device_unlock(dev);
  426. return sysfs_emit(buf, "%u\n", val);
  427. }
  428. static DEVICE_ATTR_RO(state_synced);
  429. static int really_probe(struct device *dev, struct device_driver *drv)
  430. {
  431. int ret = -EPROBE_DEFER;
  432. int local_trigger_count = atomic_read(&deferred_trigger_count);
  433. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  434. !drv->suppress_bind_attrs;
  435. if (defer_all_probes) {
  436. /*
  437. * Value of defer_all_probes can be set only by
  438. * device_block_probing() which, in turn, will call
  439. * wait_for_device_probe() right after that to avoid any races.
  440. */
  441. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  442. driver_deferred_probe_add(dev);
  443. return ret;
  444. }
  445. ret = device_links_check_suppliers(dev);
  446. if (ret == -EPROBE_DEFER)
  447. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  448. if (ret)
  449. return ret;
  450. atomic_inc(&probe_count);
  451. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  452. drv->bus->name, __func__, drv->name, dev_name(dev));
  453. if (!list_empty(&dev->devres_head)) {
  454. dev_crit(dev, "Resources present before probing\n");
  455. ret = -EBUSY;
  456. goto done;
  457. }
  458. re_probe:
  459. dev->driver = drv;
  460. /* If using pinctrl, bind pins now before probing */
  461. ret = pinctrl_bind_pins(dev);
  462. if (ret)
  463. goto pinctrl_bind_failed;
  464. if (dev->bus->dma_configure) {
  465. ret = dev->bus->dma_configure(dev);
  466. if (ret)
  467. goto probe_failed;
  468. }
  469. ret = driver_sysfs_add(dev);
  470. if (ret) {
  471. pr_err("%s: driver_sysfs_add(%s) failed\n",
  472. __func__, dev_name(dev));
  473. goto probe_failed;
  474. }
  475. if (dev->pm_domain && dev->pm_domain->activate) {
  476. ret = dev->pm_domain->activate(dev);
  477. if (ret)
  478. goto probe_failed;
  479. }
  480. if (dev->bus->probe) {
  481. ret = dev->bus->probe(dev);
  482. if (ret)
  483. goto probe_failed;
  484. } else if (drv->probe) {
  485. ret = drv->probe(dev);
  486. if (ret)
  487. goto probe_failed;
  488. }
  489. ret = device_add_groups(dev, drv->dev_groups);
  490. if (ret) {
  491. dev_err(dev, "device_add_groups() failed\n");
  492. goto dev_groups_failed;
  493. }
  494. if (dev_has_sync_state(dev)) {
  495. ret = device_create_file(dev, &dev_attr_state_synced);
  496. if (ret) {
  497. dev_err(dev, "state_synced sysfs add failed\n");
  498. goto dev_sysfs_state_synced_failed;
  499. }
  500. }
  501. if (test_remove) {
  502. test_remove = false;
  503. device_remove_file(dev, &dev_attr_state_synced);
  504. device_remove_groups(dev, drv->dev_groups);
  505. if (dev->bus->remove)
  506. dev->bus->remove(dev);
  507. else if (drv->remove)
  508. drv->remove(dev);
  509. devres_release_all(dev);
  510. arch_teardown_dma_ops(dev);
  511. kfree(dev->dma_range_map);
  512. dev->dma_range_map = NULL;
  513. driver_sysfs_remove(dev);
  514. dev->driver = NULL;
  515. dev_set_drvdata(dev, NULL);
  516. if (dev->pm_domain && dev->pm_domain->dismiss)
  517. dev->pm_domain->dismiss(dev);
  518. pm_runtime_reinit(dev);
  519. goto re_probe;
  520. }
  521. pinctrl_init_done(dev);
  522. if (dev->pm_domain && dev->pm_domain->sync)
  523. dev->pm_domain->sync(dev);
  524. driver_bound(dev);
  525. ret = 1;
  526. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  527. drv->bus->name, __func__, dev_name(dev), drv->name);
  528. goto done;
  529. dev_sysfs_state_synced_failed:
  530. device_remove_groups(dev, drv->dev_groups);
  531. dev_groups_failed:
  532. if (dev->bus->remove)
  533. dev->bus->remove(dev);
  534. else if (drv->remove)
  535. drv->remove(dev);
  536. probe_failed:
  537. if (dev->bus)
  538. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  539. BUS_NOTIFY_DRIVER_NOT_BOUND, dev);
  540. pinctrl_bind_failed:
  541. device_links_no_driver(dev);
  542. devres_release_all(dev);
  543. arch_teardown_dma_ops(dev);
  544. kfree(dev->dma_range_map);
  545. dev->dma_range_map = NULL;
  546. driver_sysfs_remove(dev);
  547. dev->driver = NULL;
  548. dev_set_drvdata(dev, NULL);
  549. if (dev->pm_domain && dev->pm_domain->dismiss)
  550. dev->pm_domain->dismiss(dev);
  551. pm_runtime_reinit(dev);
  552. dev_pm_set_driver_flags(dev, 0);
  553. switch (ret) {
  554. case -EPROBE_DEFER:
  555. /* Driver requested deferred probing */
  556. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  557. driver_deferred_probe_add_trigger(dev, local_trigger_count);
  558. break;
  559. case -ENODEV:
  560. case -ENXIO:
  561. pr_debug("%s: probe of %s rejects match %d\n",
  562. drv->name, dev_name(dev), ret);
  563. break;
  564. default:
  565. /* driver matched but the probe failed */
  566. pr_warn("%s: probe of %s failed with error %d\n",
  567. drv->name, dev_name(dev), ret);
  568. }
  569. /*
  570. * Ignore errors returned by ->probe so that the next driver can try
  571. * its luck.
  572. */
  573. ret = 0;
  574. done:
  575. atomic_dec(&probe_count);
  576. wake_up_all(&probe_waitqueue);
  577. return ret;
  578. }
  579. /*
  580. * For initcall_debug, show the driver probe time.
  581. */
  582. static int really_probe_debug(struct device *dev, struct device_driver *drv)
  583. {
  584. ktime_t calltime, rettime;
  585. int ret;
  586. calltime = ktime_get();
  587. ret = really_probe(dev, drv);
  588. rettime = ktime_get();
  589. pr_debug("probe of %s returned %d after %lld usecs\n",
  590. dev_name(dev), ret, ktime_us_delta(rettime, calltime));
  591. return ret;
  592. }
  593. /**
  594. * driver_probe_done
  595. * Determine if the probe sequence is finished or not.
  596. *
  597. * Should somehow figure out how to use a semaphore, not an atomic variable...
  598. */
  599. int driver_probe_done(void)
  600. {
  601. int local_probe_count = atomic_read(&probe_count);
  602. pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
  603. if (local_probe_count)
  604. return -EBUSY;
  605. return 0;
  606. }
  607. /**
  608. * wait_for_device_probe
  609. * Wait for device probing to be completed.
  610. */
  611. void wait_for_device_probe(void)
  612. {
  613. /* wait for probe timeout */
  614. wait_event(probe_timeout_waitqueue, !driver_deferred_probe_timeout);
  615. /* wait for the deferred probe workqueue to finish */
  616. flush_work(&deferred_probe_work);
  617. /* wait for the known devices to complete their probing */
  618. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  619. async_synchronize_full();
  620. }
  621. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  622. /**
  623. * driver_probe_device - attempt to bind device & driver together
  624. * @drv: driver to bind a device to
  625. * @dev: device to try to bind to the driver
  626. *
  627. * This function returns -ENODEV if the device is not registered,
  628. * 1 if the device is bound successfully and 0 otherwise.
  629. *
  630. * This function must be called with @dev lock held. When called for a
  631. * USB interface, @dev->parent lock must be held as well.
  632. *
  633. * If the device has a parent, runtime-resume the parent before driver probing.
  634. */
  635. int driver_probe_device(struct device_driver *drv, struct device *dev)
  636. {
  637. int ret = 0;
  638. if (!device_is_registered(dev))
  639. return -ENODEV;
  640. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  641. drv->bus->name, __func__, dev_name(dev), drv->name);
  642. pm_runtime_get_suppliers(dev);
  643. if (dev->parent)
  644. pm_runtime_get_sync(dev->parent);
  645. pm_runtime_barrier(dev);
  646. if (initcall_debug)
  647. ret = really_probe_debug(dev, drv);
  648. else
  649. ret = really_probe(dev, drv);
  650. pm_request_idle(dev);
  651. if (dev->parent)
  652. pm_runtime_put(dev->parent);
  653. pm_runtime_put_suppliers(dev);
  654. return ret;
  655. }
  656. static inline bool cmdline_requested_async_probing(const char *drv_name)
  657. {
  658. return parse_option_str(async_probe_drv_names, drv_name);
  659. }
  660. /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
  661. static int __init save_async_options(char *buf)
  662. {
  663. if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
  664. pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
  665. strlcpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
  666. return 1;
  667. }
  668. __setup("driver_async_probe=", save_async_options);
  669. bool driver_allows_async_probing(struct device_driver *drv)
  670. {
  671. switch (drv->probe_type) {
  672. case PROBE_PREFER_ASYNCHRONOUS:
  673. return true;
  674. case PROBE_FORCE_SYNCHRONOUS:
  675. return false;
  676. default:
  677. if (cmdline_requested_async_probing(drv->name))
  678. return true;
  679. if (module_requested_async_probing(drv->owner))
  680. return true;
  681. return false;
  682. }
  683. }
  684. struct device_attach_data {
  685. struct device *dev;
  686. /*
  687. * Indicates whether we are are considering asynchronous probing or
  688. * not. Only initial binding after device or driver registration
  689. * (including deferral processing) may be done asynchronously, the
  690. * rest is always synchronous, as we expect it is being done by
  691. * request from userspace.
  692. */
  693. bool check_async;
  694. /*
  695. * Indicates if we are binding synchronous or asynchronous drivers.
  696. * When asynchronous probing is enabled we'll execute 2 passes
  697. * over drivers: first pass doing synchronous probing and second
  698. * doing asynchronous probing (if synchronous did not succeed -
  699. * most likely because there was no driver requiring synchronous
  700. * probing - and we found asynchronous driver during first pass).
  701. * The 2 passes are done because we can't shoot asynchronous
  702. * probe for given device and driver from bus_for_each_drv() since
  703. * driver pointer is not guaranteed to stay valid once
  704. * bus_for_each_drv() iterates to the next driver on the bus.
  705. */
  706. bool want_async;
  707. /*
  708. * We'll set have_async to 'true' if, while scanning for matching
  709. * driver, we'll encounter one that requests asynchronous probing.
  710. */
  711. bool have_async;
  712. };
  713. static int __device_attach_driver(struct device_driver *drv, void *_data)
  714. {
  715. struct device_attach_data *data = _data;
  716. struct device *dev = data->dev;
  717. bool async_allowed;
  718. int ret;
  719. ret = driver_match_device(drv, dev);
  720. if (ret == 0) {
  721. /* no match */
  722. return 0;
  723. } else if (ret == -EPROBE_DEFER) {
  724. dev_dbg(dev, "Device match requests probe deferral\n");
  725. driver_deferred_probe_add(dev);
  726. } else if (ret < 0) {
  727. dev_dbg(dev, "Bus failed to match device: %d\n", ret);
  728. return ret;
  729. } /* ret > 0 means positive match */
  730. async_allowed = driver_allows_async_probing(drv);
  731. if (async_allowed)
  732. data->have_async = true;
  733. if (data->check_async && async_allowed != data->want_async)
  734. return 0;
  735. return driver_probe_device(drv, dev);
  736. }
  737. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  738. {
  739. struct device *dev = _dev;
  740. struct device_attach_data data = {
  741. .dev = dev,
  742. .check_async = true,
  743. .want_async = true,
  744. };
  745. device_lock(dev);
  746. /*
  747. * Check if device has already been removed or claimed. This may
  748. * happen with driver loading, device discovery/registration,
  749. * and deferred probe processing happens all at once with
  750. * multiple threads.
  751. */
  752. if (dev->p->dead || dev->driver)
  753. goto out_unlock;
  754. if (dev->parent)
  755. pm_runtime_get_sync(dev->parent);
  756. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  757. dev_dbg(dev, "async probe completed\n");
  758. pm_request_idle(dev);
  759. if (dev->parent)
  760. pm_runtime_put(dev->parent);
  761. out_unlock:
  762. device_unlock(dev);
  763. put_device(dev);
  764. }
  765. static int __device_attach(struct device *dev, bool allow_async)
  766. {
  767. int ret = 0;
  768. device_lock(dev);
  769. if (dev->p->dead) {
  770. goto out_unlock;
  771. } else if (dev->driver) {
  772. if (device_is_bound(dev)) {
  773. ret = 1;
  774. goto out_unlock;
  775. }
  776. ret = device_bind_driver(dev);
  777. if (ret == 0)
  778. ret = 1;
  779. else {
  780. dev->driver = NULL;
  781. ret = 0;
  782. }
  783. } else {
  784. struct device_attach_data data = {
  785. .dev = dev,
  786. .check_async = allow_async,
  787. .want_async = false,
  788. };
  789. if (dev->parent)
  790. pm_runtime_get_sync(dev->parent);
  791. ret = bus_for_each_drv(dev->bus, NULL, &data,
  792. __device_attach_driver);
  793. if (!ret && allow_async && data.have_async) {
  794. /*
  795. * If we could not find appropriate driver
  796. * synchronously and we are allowed to do
  797. * async probes and there are drivers that
  798. * want to probe asynchronously, we'll
  799. * try them.
  800. */
  801. dev_dbg(dev, "scheduling asynchronous probe\n");
  802. get_device(dev);
  803. async_schedule_dev(__device_attach_async_helper, dev);
  804. } else {
  805. pm_request_idle(dev);
  806. }
  807. if (dev->parent)
  808. pm_runtime_put(dev->parent);
  809. }
  810. out_unlock:
  811. device_unlock(dev);
  812. return ret;
  813. }
  814. /**
  815. * device_attach - try to attach device to a driver.
  816. * @dev: device.
  817. *
  818. * Walk the list of drivers that the bus has and call
  819. * driver_probe_device() for each pair. If a compatible
  820. * pair is found, break out and return.
  821. *
  822. * Returns 1 if the device was bound to a driver;
  823. * 0 if no matching driver was found;
  824. * -ENODEV if the device is not registered.
  825. *
  826. * When called for a USB interface, @dev->parent lock must be held.
  827. */
  828. int device_attach(struct device *dev)
  829. {
  830. return __device_attach(dev, false);
  831. }
  832. EXPORT_SYMBOL_GPL(device_attach);
  833. void device_initial_probe(struct device *dev)
  834. {
  835. __device_attach(dev, true);
  836. }
  837. /*
  838. * __device_driver_lock - acquire locks needed to manipulate dev->drv
  839. * @dev: Device we will update driver info for
  840. * @parent: Parent device. Needed if the bus requires parent lock
  841. *
  842. * This function will take the required locks for manipulating dev->drv.
  843. * Normally this will just be the @dev lock, but when called for a USB
  844. * interface, @parent lock will be held as well.
  845. */
  846. static void __device_driver_lock(struct device *dev, struct device *parent)
  847. {
  848. if (parent && dev->bus->need_parent_lock)
  849. device_lock(parent);
  850. device_lock(dev);
  851. }
  852. /*
  853. * __device_driver_unlock - release locks needed to manipulate dev->drv
  854. * @dev: Device we will update driver info for
  855. * @parent: Parent device. Needed if the bus requires parent lock
  856. *
  857. * This function will release the required locks for manipulating dev->drv.
  858. * Normally this will just be the the @dev lock, but when called for a
  859. * USB interface, @parent lock will be released as well.
  860. */
  861. static void __device_driver_unlock(struct device *dev, struct device *parent)
  862. {
  863. device_unlock(dev);
  864. if (parent && dev->bus->need_parent_lock)
  865. device_unlock(parent);
  866. }
  867. /**
  868. * device_driver_attach - attach a specific driver to a specific device
  869. * @drv: Driver to attach
  870. * @dev: Device to attach it to
  871. *
  872. * Manually attach driver to a device. Will acquire both @dev lock and
  873. * @dev->parent lock if needed.
  874. */
  875. int device_driver_attach(struct device_driver *drv, struct device *dev)
  876. {
  877. int ret = 0;
  878. __device_driver_lock(dev, dev->parent);
  879. /*
  880. * If device has been removed or someone has already successfully
  881. * bound a driver before us just skip the driver probe call.
  882. */
  883. if (!dev->p->dead && !dev->driver)
  884. ret = driver_probe_device(drv, dev);
  885. __device_driver_unlock(dev, dev->parent);
  886. return ret;
  887. }
  888. static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
  889. {
  890. struct device *dev = _dev;
  891. struct device_driver *drv;
  892. int ret = 0;
  893. __device_driver_lock(dev, dev->parent);
  894. drv = dev->p->async_driver;
  895. /*
  896. * If device has been removed or someone has already successfully
  897. * bound a driver before us just skip the driver probe call.
  898. */
  899. if (!dev->p->dead && !dev->driver)
  900. ret = driver_probe_device(drv, dev);
  901. __device_driver_unlock(dev, dev->parent);
  902. dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
  903. put_device(dev);
  904. }
  905. static int __driver_attach(struct device *dev, void *data)
  906. {
  907. struct device_driver *drv = data;
  908. int ret;
  909. /*
  910. * Lock device and try to bind to it. We drop the error
  911. * here and always return 0, because we need to keep trying
  912. * to bind to devices and some drivers will return an error
  913. * simply if it didn't support the device.
  914. *
  915. * driver_probe_device() will spit a warning if there
  916. * is an error.
  917. */
  918. ret = driver_match_device(drv, dev);
  919. if (ret == 0) {
  920. /* no match */
  921. return 0;
  922. } else if (ret == -EPROBE_DEFER) {
  923. dev_dbg(dev, "Device match requests probe deferral\n");
  924. driver_deferred_probe_add(dev);
  925. } else if (ret < 0) {
  926. dev_dbg(dev, "Bus failed to match device: %d\n", ret);
  927. return ret;
  928. } /* ret > 0 means positive match */
  929. if (driver_allows_async_probing(drv)) {
  930. /*
  931. * Instead of probing the device synchronously we will
  932. * probe it asynchronously to allow for more parallelism.
  933. *
  934. * We only take the device lock here in order to guarantee
  935. * that the dev->driver and async_driver fields are protected
  936. */
  937. dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
  938. device_lock(dev);
  939. if (!dev->driver) {
  940. get_device(dev);
  941. dev->p->async_driver = drv;
  942. async_schedule_dev(__driver_attach_async_helper, dev);
  943. }
  944. device_unlock(dev);
  945. return 0;
  946. }
  947. device_driver_attach(drv, dev);
  948. return 0;
  949. }
  950. /**
  951. * driver_attach - try to bind driver to devices.
  952. * @drv: driver.
  953. *
  954. * Walk the list of devices that the bus has on it and try to
  955. * match the driver with each one. If driver_probe_device()
  956. * returns 0 and the @dev->driver is set, we've found a
  957. * compatible pair.
  958. */
  959. int driver_attach(struct device_driver *drv)
  960. {
  961. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  962. }
  963. EXPORT_SYMBOL_GPL(driver_attach);
  964. /*
  965. * __device_release_driver() must be called with @dev lock held.
  966. * When called for a USB interface, @dev->parent lock must be held as well.
  967. */
  968. static void __device_release_driver(struct device *dev, struct device *parent)
  969. {
  970. struct device_driver *drv;
  971. drv = dev->driver;
  972. if (drv) {
  973. pm_runtime_get_sync(dev);
  974. while (device_links_busy(dev)) {
  975. __device_driver_unlock(dev, parent);
  976. device_links_unbind_consumers(dev);
  977. __device_driver_lock(dev, parent);
  978. /*
  979. * A concurrent invocation of the same function might
  980. * have released the driver successfully while this one
  981. * was waiting, so check for that.
  982. */
  983. if (dev->driver != drv) {
  984. pm_runtime_put(dev);
  985. return;
  986. }
  987. }
  988. driver_sysfs_remove(dev);
  989. if (dev->bus)
  990. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  991. BUS_NOTIFY_UNBIND_DRIVER,
  992. dev);
  993. pm_runtime_put_sync(dev);
  994. device_remove_file(dev, &dev_attr_state_synced);
  995. device_remove_groups(dev, drv->dev_groups);
  996. if (dev->bus && dev->bus->remove)
  997. dev->bus->remove(dev);
  998. else if (drv->remove)
  999. drv->remove(dev);
  1000. device_links_driver_cleanup(dev);
  1001. devres_release_all(dev);
  1002. arch_teardown_dma_ops(dev);
  1003. kfree(dev->dma_range_map);
  1004. dev->dma_range_map = NULL;
  1005. dev->driver = NULL;
  1006. dev_set_drvdata(dev, NULL);
  1007. if (dev->pm_domain && dev->pm_domain->dismiss)
  1008. dev->pm_domain->dismiss(dev);
  1009. pm_runtime_reinit(dev);
  1010. dev_pm_set_driver_flags(dev, 0);
  1011. klist_remove(&dev->p->knode_driver);
  1012. device_pm_check_callbacks(dev);
  1013. if (dev->bus)
  1014. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  1015. BUS_NOTIFY_UNBOUND_DRIVER,
  1016. dev);
  1017. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  1018. }
  1019. }
  1020. void device_release_driver_internal(struct device *dev,
  1021. struct device_driver *drv,
  1022. struct device *parent)
  1023. {
  1024. __device_driver_lock(dev, parent);
  1025. if (!drv || drv == dev->driver)
  1026. __device_release_driver(dev, parent);
  1027. __device_driver_unlock(dev, parent);
  1028. }
  1029. /**
  1030. * device_release_driver - manually detach device from driver.
  1031. * @dev: device.
  1032. *
  1033. * Manually detach device from driver.
  1034. * When called for a USB interface, @dev->parent lock must be held.
  1035. *
  1036. * If this function is to be called with @dev->parent lock held, ensure that
  1037. * the device's consumers are unbound in advance or that their locks can be
  1038. * acquired under the @dev->parent lock.
  1039. */
  1040. void device_release_driver(struct device *dev)
  1041. {
  1042. /*
  1043. * If anyone calls device_release_driver() recursively from
  1044. * within their ->remove callback for the same device, they
  1045. * will deadlock right here.
  1046. */
  1047. device_release_driver_internal(dev, NULL, NULL);
  1048. }
  1049. EXPORT_SYMBOL_GPL(device_release_driver);
  1050. /**
  1051. * device_driver_detach - detach driver from a specific device
  1052. * @dev: device to detach driver from
  1053. *
  1054. * Detach driver from device. Will acquire both @dev lock and @dev->parent
  1055. * lock if needed.
  1056. */
  1057. void device_driver_detach(struct device *dev)
  1058. {
  1059. device_release_driver_internal(dev, NULL, dev->parent);
  1060. }
  1061. /**
  1062. * driver_detach - detach driver from all devices it controls.
  1063. * @drv: driver.
  1064. */
  1065. void driver_detach(struct device_driver *drv)
  1066. {
  1067. struct device_private *dev_prv;
  1068. struct device *dev;
  1069. if (driver_allows_async_probing(drv))
  1070. async_synchronize_full();
  1071. for (;;) {
  1072. spin_lock(&drv->p->klist_devices.k_lock);
  1073. if (list_empty(&drv->p->klist_devices.k_list)) {
  1074. spin_unlock(&drv->p->klist_devices.k_lock);
  1075. break;
  1076. }
  1077. dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
  1078. struct device_private,
  1079. knode_driver.n_node);
  1080. dev = dev_prv->device;
  1081. get_device(dev);
  1082. spin_unlock(&drv->p->klist_devices.k_lock);
  1083. device_release_driver_internal(dev, drv, dev->parent);
  1084. put_device(dev);
  1085. }
  1086. }