dd.c 36 KB

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