dd.c 37 KB

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