dd.c 28 KB

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