dd.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * drivers/base/dd.c - The core device/driver interactions.
  4. *
  5. * This file contains the (sometimes tricky) code that controls the
  6. * interactions between devices and drivers, which primarily includes
  7. * driver binding and unbinding.
  8. *
  9. * All of this code used to exist in drivers/base/bus.c, but was
  10. * relocated to here in the name of compartmentalization (since it wasn't
  11. * strictly code just for the 'struct bus_type'.
  12. *
  13. * Copyright (c) 2002-5 Patrick Mochel
  14. * Copyright (c) 2002-3 Open Source Development Labs
  15. * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
  16. * Copyright (c) 2007-2009 Novell Inc.
  17. */
  18. #include <linux/debugfs.h>
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/dma-map-ops.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/kthread.h>
  25. #include <linux/wait.h>
  26. #include <linux/async.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pinctrl/devinfo.h>
  29. #include <linux/slab.h>
  30. #include "base.h"
  31. #include "power/power.h"
  32. /*
  33. * Deferred Probe infrastructure.
  34. *
  35. * Sometimes driver probe order matters, but the kernel doesn't always have
  36. * dependency information which means some drivers will get probed before a
  37. * resource it depends on is available. For example, an SDHCI driver may
  38. * first need a GPIO line from an i2c GPIO controller before it can be
  39. * initialized. If a required resource is not available yet, a driver can
  40. * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
  41. *
  42. * Deferred probe maintains two lists of devices, a pending list and an active
  43. * list. A driver returning -EPROBE_DEFER causes the device to be added to the
  44. * pending list. A successful driver probe will trigger moving all devices
  45. * from the pending to the active list so that the workqueue will eventually
  46. * retry them.
  47. *
  48. * The deferred_probe_mutex must be held any time the deferred_probe_*_list
  49. * of the (struct device*)->p->deferred_probe pointers are manipulated
  50. */
  51. static DEFINE_MUTEX(deferred_probe_mutex);
  52. static LIST_HEAD(deferred_probe_pending_list);
  53. static LIST_HEAD(deferred_probe_active_list);
  54. static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
  55. static 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->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. static int driver_deferred_probe_timeout = 10;
  230. #else
  231. static int driver_deferred_probe_timeout;
  232. #endif
  233. static int __init deferred_probe_timeout_setup(char *str)
  234. {
  235. int timeout;
  236. if (!kstrtoint(str, 10, &timeout))
  237. driver_deferred_probe_timeout = timeout;
  238. return 1;
  239. }
  240. __setup("deferred_probe_timeout=", deferred_probe_timeout_setup);
  241. /**
  242. * driver_deferred_probe_check_state() - Check deferred probe state
  243. * @dev: device to check
  244. *
  245. * Return:
  246. * * -ENODEV if initcalls have completed and modules are disabled.
  247. * * -ETIMEDOUT if the deferred probe timeout was set and has expired
  248. * and modules are enabled.
  249. * * -EPROBE_DEFER in other cases.
  250. *
  251. * Drivers or subsystems can opt-in to calling this function instead of directly
  252. * returning -EPROBE_DEFER.
  253. */
  254. int driver_deferred_probe_check_state(struct device *dev)
  255. {
  256. if (!IS_ENABLED(CONFIG_MODULES) && initcalls_done) {
  257. dev_warn(dev, "ignoring dependency for device, assuming no driver\n");
  258. return -ENODEV;
  259. }
  260. if (!driver_deferred_probe_timeout && initcalls_done) {
  261. dev_warn(dev, "deferred probe timeout, ignoring dependency\n");
  262. return -ETIMEDOUT;
  263. }
  264. return -EPROBE_DEFER;
  265. }
  266. EXPORT_SYMBOL_GPL(driver_deferred_probe_check_state);
  267. static void deferred_probe_timeout_work_func(struct work_struct *work)
  268. {
  269. struct device_private *p;
  270. fw_devlink_drivers_done();
  271. driver_deferred_probe_timeout = 0;
  272. driver_deferred_probe_trigger();
  273. flush_work(&deferred_probe_work);
  274. mutex_lock(&deferred_probe_mutex);
  275. list_for_each_entry(p, &deferred_probe_pending_list, deferred_probe)
  276. dev_warn(p->device, "deferred probe pending: %s", p->deferred_probe_reason ?: "(reason unknown)\n");
  277. mutex_unlock(&deferred_probe_mutex);
  278. fw_devlink_probing_done();
  279. }
  280. static DECLARE_DELAYED_WORK(deferred_probe_timeout_work, deferred_probe_timeout_work_func);
  281. void deferred_probe_extend_timeout(void)
  282. {
  283. /*
  284. * If the work hasn't been queued yet or if the work expired, don't
  285. * start a new one.
  286. */
  287. if (cancel_delayed_work(&deferred_probe_timeout_work)) {
  288. schedule_delayed_work(&deferred_probe_timeout_work,
  289. driver_deferred_probe_timeout * HZ);
  290. pr_debug("Extended deferred probe timeout by %d secs\n",
  291. driver_deferred_probe_timeout);
  292. }
  293. }
  294. /**
  295. * deferred_probe_initcall() - Enable probing of deferred devices
  296. *
  297. * We don't want to get in the way when the bulk of drivers are getting probed.
  298. * Instead, this initcall makes sure that deferred probing is delayed until
  299. * late_initcall time.
  300. */
  301. static int deferred_probe_initcall(void)
  302. {
  303. debugfs_create_file("devices_deferred", 0444, NULL, NULL,
  304. &deferred_devs_fops);
  305. driver_deferred_probe_enable = true;
  306. driver_deferred_probe_trigger();
  307. /* Sort as many dependencies as possible before exiting initcalls */
  308. flush_work(&deferred_probe_work);
  309. initcalls_done = true;
  310. if (!IS_ENABLED(CONFIG_MODULES))
  311. fw_devlink_drivers_done();
  312. /*
  313. * Trigger deferred probe again, this time we won't defer anything
  314. * that is optional
  315. */
  316. driver_deferred_probe_trigger();
  317. flush_work(&deferred_probe_work);
  318. if (driver_deferred_probe_timeout > 0) {
  319. schedule_delayed_work(&deferred_probe_timeout_work,
  320. driver_deferred_probe_timeout * HZ);
  321. }
  322. if (!IS_ENABLED(CONFIG_MODULES))
  323. fw_devlink_probing_done();
  324. return 0;
  325. }
  326. late_initcall(deferred_probe_initcall);
  327. static void __exit deferred_probe_exit(void)
  328. {
  329. debugfs_lookup_and_remove("devices_deferred", NULL);
  330. }
  331. __exitcall(deferred_probe_exit);
  332. /**
  333. * device_is_bound() - Check if device is bound to a driver
  334. * @dev: device to check
  335. *
  336. * Returns true if passed device has already finished probing successfully
  337. * against a driver.
  338. *
  339. * This function must be called with the device lock held.
  340. */
  341. bool device_is_bound(struct device *dev)
  342. {
  343. return dev->p && klist_node_attached(&dev->p->knode_driver);
  344. }
  345. EXPORT_SYMBOL_GPL(device_is_bound);
  346. static void driver_bound(struct device *dev)
  347. {
  348. if (device_is_bound(dev)) {
  349. dev_warn(dev, "%s: device already bound\n", __func__);
  350. return;
  351. }
  352. dev_dbg(dev, "driver: '%s': %s: bound to device\n", dev->driver->name,
  353. __func__);
  354. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  355. device_links_driver_bound(dev);
  356. device_pm_check_callbacks(dev);
  357. /*
  358. * Make sure the device is no longer in one of the deferred lists and
  359. * kick off retrying all pending devices
  360. */
  361. driver_deferred_probe_del(dev);
  362. driver_deferred_probe_trigger();
  363. bus_notify(dev, BUS_NOTIFY_BOUND_DRIVER);
  364. kobject_uevent(&dev->kobj, KOBJ_BIND);
  365. }
  366. static ssize_t coredump_store(struct device *dev, struct device_attribute *attr,
  367. const char *buf, size_t count)
  368. {
  369. device_lock(dev);
  370. dev->driver->coredump(dev);
  371. device_unlock(dev);
  372. return count;
  373. }
  374. static DEVICE_ATTR_WO(coredump);
  375. static int driver_sysfs_add(struct device *dev)
  376. {
  377. int ret;
  378. bus_notify(dev, BUS_NOTIFY_BIND_DRIVER);
  379. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  380. kobject_name(&dev->kobj));
  381. if (ret)
  382. goto fail;
  383. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  384. "driver");
  385. if (ret)
  386. goto rm_dev;
  387. if (!IS_ENABLED(CONFIG_DEV_COREDUMP) || !dev->driver->coredump)
  388. return 0;
  389. ret = device_create_file(dev, &dev_attr_coredump);
  390. if (!ret)
  391. return 0;
  392. sysfs_remove_link(&dev->kobj, "driver");
  393. rm_dev:
  394. sysfs_remove_link(&dev->driver->p->kobj,
  395. kobject_name(&dev->kobj));
  396. fail:
  397. return ret;
  398. }
  399. static void driver_sysfs_remove(struct device *dev)
  400. {
  401. struct device_driver *drv = dev->driver;
  402. if (drv) {
  403. if (drv->coredump)
  404. device_remove_file(dev, &dev_attr_coredump);
  405. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  406. sysfs_remove_link(&dev->kobj, "driver");
  407. }
  408. }
  409. /**
  410. * device_bind_driver - bind a driver to one device.
  411. * @dev: device.
  412. *
  413. * Allow manual attachment of a driver to a device.
  414. * Caller must have already set @dev->driver.
  415. *
  416. * Note that this does not modify the bus reference count.
  417. * Please verify that is accounted for before calling this.
  418. * (It is ok to call with no other effort from a driver's probe() method.)
  419. *
  420. * This function must be called with the device lock held.
  421. *
  422. * Callers should prefer to use device_driver_attach() instead.
  423. */
  424. int device_bind_driver(struct device *dev)
  425. {
  426. int ret;
  427. ret = driver_sysfs_add(dev);
  428. if (!ret) {
  429. device_links_force_bind(dev);
  430. driver_bound(dev);
  431. }
  432. else
  433. bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
  434. return ret;
  435. }
  436. EXPORT_SYMBOL_GPL(device_bind_driver);
  437. static atomic_t probe_count = ATOMIC_INIT(0);
  438. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  439. static ssize_t state_synced_store(struct device *dev,
  440. struct device_attribute *attr,
  441. const char *buf, size_t count)
  442. {
  443. int ret = 0;
  444. if (strcmp("1", buf))
  445. return -EINVAL;
  446. device_lock(dev);
  447. if (!dev->state_synced) {
  448. dev->state_synced = true;
  449. dev_sync_state(dev);
  450. } else {
  451. ret = -EINVAL;
  452. }
  453. device_unlock(dev);
  454. return ret ? ret : count;
  455. }
  456. static ssize_t state_synced_show(struct device *dev,
  457. struct device_attribute *attr, char *buf)
  458. {
  459. bool val;
  460. device_lock(dev);
  461. val = dev->state_synced;
  462. device_unlock(dev);
  463. return sysfs_emit(buf, "%u\n", val);
  464. }
  465. static DEVICE_ATTR_RW(state_synced);
  466. static void device_unbind_cleanup(struct device *dev)
  467. {
  468. devres_release_all(dev);
  469. arch_teardown_dma_ops(dev);
  470. kfree(dev->dma_range_map);
  471. dev->dma_range_map = NULL;
  472. device_set_driver(dev, NULL);
  473. dev_set_drvdata(dev, NULL);
  474. if (dev->pm_domain && dev->pm_domain->dismiss)
  475. dev->pm_domain->dismiss(dev);
  476. pm_runtime_reinit(dev);
  477. dev_pm_set_driver_flags(dev, 0);
  478. }
  479. static void device_remove(struct device *dev)
  480. {
  481. device_remove_file(dev, &dev_attr_state_synced);
  482. device_remove_groups(dev, dev->driver->dev_groups);
  483. if (dev->bus && dev->bus->remove)
  484. dev->bus->remove(dev);
  485. else if (dev->driver->remove)
  486. dev->driver->remove(dev);
  487. }
  488. static int call_driver_probe(struct device *dev, const struct device_driver *drv)
  489. {
  490. int ret = 0;
  491. if (dev->bus->probe)
  492. ret = dev->bus->probe(dev);
  493. else if (drv->probe)
  494. ret = drv->probe(dev);
  495. switch (ret) {
  496. case 0:
  497. break;
  498. case -EPROBE_DEFER:
  499. /* Driver requested deferred probing */
  500. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  501. break;
  502. case -ENODEV:
  503. case -ENXIO:
  504. dev_dbg(dev, "probe with driver %s rejects match %d\n",
  505. drv->name, ret);
  506. break;
  507. default:
  508. /* driver matched but the probe failed */
  509. dev_err(dev, "probe with driver %s failed with error %d\n",
  510. drv->name, ret);
  511. break;
  512. }
  513. return ret;
  514. }
  515. static int really_probe(struct device *dev, const struct device_driver *drv)
  516. {
  517. bool test_remove = IS_ENABLED(CONFIG_DEBUG_TEST_DRIVER_REMOVE) &&
  518. !drv->suppress_bind_attrs;
  519. int ret, link_ret;
  520. if (defer_all_probes) {
  521. /*
  522. * Value of defer_all_probes can be set only by
  523. * device_block_probing() which, in turn, will call
  524. * wait_for_device_probe() right after that to avoid any races.
  525. */
  526. dev_dbg(dev, "Driver %s force probe deferral\n", drv->name);
  527. return -EPROBE_DEFER;
  528. }
  529. link_ret = device_links_check_suppliers(dev);
  530. if (link_ret == -EPROBE_DEFER)
  531. return link_ret;
  532. dev_dbg(dev, "bus: '%s': %s: probing driver %s with device\n",
  533. drv->bus->name, __func__, drv->name);
  534. if (!list_empty(&dev->devres_head)) {
  535. dev_crit(dev, "Resources present before probing\n");
  536. ret = -EBUSY;
  537. goto done;
  538. }
  539. re_probe:
  540. device_set_driver(dev, drv);
  541. /* If using pinctrl, bind pins now before probing */
  542. ret = pinctrl_bind_pins(dev);
  543. if (ret)
  544. goto pinctrl_bind_failed;
  545. if (dev->bus->dma_configure) {
  546. ret = dev->bus->dma_configure(dev);
  547. if (ret)
  548. goto pinctrl_bind_failed;
  549. }
  550. ret = driver_sysfs_add(dev);
  551. if (ret) {
  552. dev_err(dev, "%s: driver_sysfs_add failed\n", __func__);
  553. goto sysfs_failed;
  554. }
  555. if (dev->pm_domain && dev->pm_domain->activate) {
  556. ret = dev->pm_domain->activate(dev);
  557. if (ret)
  558. goto probe_failed;
  559. }
  560. ret = call_driver_probe(dev, drv);
  561. if (ret) {
  562. /*
  563. * If fw_devlink_best_effort is active (denoted by -EAGAIN), the
  564. * device might actually probe properly once some of its missing
  565. * suppliers have probed. So, treat this as if the driver
  566. * returned -EPROBE_DEFER.
  567. */
  568. if (link_ret == -EAGAIN)
  569. ret = -EPROBE_DEFER;
  570. /*
  571. * Return probe errors as positive values so that the callers
  572. * can distinguish them from other errors.
  573. */
  574. ret = -ret;
  575. goto probe_failed;
  576. }
  577. ret = device_add_groups(dev, drv->dev_groups);
  578. if (ret) {
  579. dev_err(dev, "device_add_groups() failed\n");
  580. goto dev_groups_failed;
  581. }
  582. if (dev_has_sync_state(dev)) {
  583. ret = device_create_file(dev, &dev_attr_state_synced);
  584. if (ret) {
  585. dev_err(dev, "state_synced sysfs add failed\n");
  586. goto dev_sysfs_state_synced_failed;
  587. }
  588. }
  589. if (test_remove) {
  590. test_remove = false;
  591. device_remove(dev);
  592. driver_sysfs_remove(dev);
  593. if (dev->bus && dev->bus->dma_cleanup)
  594. dev->bus->dma_cleanup(dev);
  595. device_unbind_cleanup(dev);
  596. goto re_probe;
  597. }
  598. pinctrl_init_done(dev);
  599. if (dev->pm_domain && dev->pm_domain->sync)
  600. dev->pm_domain->sync(dev);
  601. driver_bound(dev);
  602. dev_dbg(dev, "bus: '%s': %s: bound device to driver %s\n",
  603. drv->bus->name, __func__, drv->name);
  604. goto done;
  605. dev_sysfs_state_synced_failed:
  606. dev_groups_failed:
  607. device_remove(dev);
  608. probe_failed:
  609. driver_sysfs_remove(dev);
  610. sysfs_failed:
  611. bus_notify(dev, BUS_NOTIFY_DRIVER_NOT_BOUND);
  612. if (dev->bus && dev->bus->dma_cleanup)
  613. dev->bus->dma_cleanup(dev);
  614. pinctrl_bind_failed:
  615. device_links_no_driver(dev);
  616. device_unbind_cleanup(dev);
  617. done:
  618. return ret;
  619. }
  620. /*
  621. * For initcall_debug, show the driver probe time.
  622. */
  623. static int really_probe_debug(struct device *dev, const struct device_driver *drv)
  624. {
  625. ktime_t calltime, rettime;
  626. int ret;
  627. calltime = ktime_get();
  628. ret = really_probe(dev, drv);
  629. rettime = ktime_get();
  630. /*
  631. * Don't change this to pr_debug() because that requires
  632. * CONFIG_DYNAMIC_DEBUG and we want a simple 'initcall_debug' on the
  633. * kernel commandline to print this all the time at the debug level.
  634. */
  635. printk(KERN_DEBUG "probe of %s returned %d after %lld usecs\n",
  636. dev_name(dev), ret, ktime_us_delta(rettime, calltime));
  637. return ret;
  638. }
  639. /**
  640. * driver_probe_done
  641. * Determine if the probe sequence is finished or not.
  642. *
  643. * Should somehow figure out how to use a semaphore, not an atomic variable...
  644. */
  645. bool __init driver_probe_done(void)
  646. {
  647. int local_probe_count = atomic_read(&probe_count);
  648. pr_debug("%s: probe_count = %d\n", __func__, local_probe_count);
  649. return !local_probe_count;
  650. }
  651. /**
  652. * wait_for_device_probe
  653. * Wait for device probing to be completed.
  654. */
  655. void wait_for_device_probe(void)
  656. {
  657. /* wait for the deferred probe workqueue to finish */
  658. flush_work(&deferred_probe_work);
  659. /* wait for the known devices to complete their probing */
  660. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  661. async_synchronize_full();
  662. }
  663. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  664. static int __driver_probe_device(const struct device_driver *drv, struct device *dev)
  665. {
  666. int ret = 0;
  667. if (dev->p->dead || !device_is_registered(dev))
  668. return -ENODEV;
  669. if (dev->driver)
  670. return -EBUSY;
  671. dev->can_match = true;
  672. dev_dbg(dev, "bus: '%s': %s: matched device with driver %s\n",
  673. drv->bus->name, __func__, drv->name);
  674. pm_runtime_get_suppliers(dev);
  675. if (dev->parent)
  676. pm_runtime_get_sync(dev->parent);
  677. pm_runtime_barrier(dev);
  678. if (initcall_debug)
  679. ret = really_probe_debug(dev, drv);
  680. else
  681. ret = really_probe(dev, drv);
  682. pm_request_idle(dev);
  683. if (dev->parent)
  684. pm_runtime_put(dev->parent);
  685. pm_runtime_put_suppliers(dev);
  686. return ret;
  687. }
  688. /**
  689. * driver_probe_device - attempt to bind device & driver together
  690. * @drv: driver to bind a device to
  691. * @dev: device to try to bind to the driver
  692. *
  693. * This function returns -ENODEV if the device is not registered, -EBUSY if it
  694. * already has a driver, 0 if the device is bound successfully and a positive
  695. * (inverted) error code for failures from the ->probe method.
  696. *
  697. * This function must be called with @dev lock held. When called for a
  698. * USB interface, @dev->parent lock must be held as well.
  699. *
  700. * If the device has a parent, runtime-resume the parent before driver probing.
  701. */
  702. static int driver_probe_device(const struct device_driver *drv, struct device *dev)
  703. {
  704. int trigger_count = atomic_read(&deferred_trigger_count);
  705. int ret;
  706. atomic_inc(&probe_count);
  707. ret = __driver_probe_device(drv, dev);
  708. if (ret == -EPROBE_DEFER || ret == EPROBE_DEFER) {
  709. driver_deferred_probe_add(dev);
  710. /*
  711. * Did a trigger occur while probing? Need to re-trigger if yes
  712. */
  713. if (trigger_count != atomic_read(&deferred_trigger_count) &&
  714. !defer_all_probes)
  715. driver_deferred_probe_trigger();
  716. }
  717. atomic_dec(&probe_count);
  718. wake_up_all(&probe_waitqueue);
  719. return ret;
  720. }
  721. static inline bool cmdline_requested_async_probing(const char *drv_name)
  722. {
  723. bool async_drv;
  724. async_drv = parse_option_str(async_probe_drv_names, drv_name);
  725. return (async_probe_default != async_drv);
  726. }
  727. /* The option format is "driver_async_probe=drv_name1,drv_name2,..." */
  728. static int __init save_async_options(char *buf)
  729. {
  730. if (strlen(buf) >= ASYNC_DRV_NAMES_MAX_LEN)
  731. pr_warn("Too long list of driver names for 'driver_async_probe'!\n");
  732. strscpy(async_probe_drv_names, buf, ASYNC_DRV_NAMES_MAX_LEN);
  733. async_probe_default = parse_option_str(async_probe_drv_names, "*");
  734. return 1;
  735. }
  736. __setup("driver_async_probe=", save_async_options);
  737. static bool driver_allows_async_probing(const struct device_driver *drv)
  738. {
  739. switch (drv->probe_type) {
  740. case PROBE_PREFER_ASYNCHRONOUS:
  741. return true;
  742. case PROBE_FORCE_SYNCHRONOUS:
  743. return false;
  744. default:
  745. if (cmdline_requested_async_probing(drv->name))
  746. return true;
  747. if (module_requested_async_probing(drv->owner))
  748. return true;
  749. return false;
  750. }
  751. }
  752. struct device_attach_data {
  753. struct device *dev;
  754. /*
  755. * Indicates whether we are considering asynchronous probing or
  756. * not. Only initial binding after device or driver registration
  757. * (including deferral processing) may be done asynchronously, the
  758. * rest is always synchronous, as we expect it is being done by
  759. * request from userspace.
  760. */
  761. bool check_async;
  762. /*
  763. * Indicates if we are binding synchronous or asynchronous drivers.
  764. * When asynchronous probing is enabled we'll execute 2 passes
  765. * over drivers: first pass doing synchronous probing and second
  766. * doing asynchronous probing (if synchronous did not succeed -
  767. * most likely because there was no driver requiring synchronous
  768. * probing - and we found asynchronous driver during first pass).
  769. * The 2 passes are done because we can't shoot asynchronous
  770. * probe for given device and driver from bus_for_each_drv() since
  771. * driver pointer is not guaranteed to stay valid once
  772. * bus_for_each_drv() iterates to the next driver on the bus.
  773. */
  774. bool want_async;
  775. /*
  776. * We'll set have_async to 'true' if, while scanning for matching
  777. * driver, we'll encounter one that requests asynchronous probing.
  778. */
  779. bool have_async;
  780. };
  781. static int __device_attach_driver(struct device_driver *drv, void *_data)
  782. {
  783. struct device_attach_data *data = _data;
  784. struct device *dev = data->dev;
  785. bool async_allowed;
  786. int ret;
  787. ret = driver_match_device(drv, dev);
  788. if (ret == 0) {
  789. /* no match */
  790. return 0;
  791. } else if (ret == -EPROBE_DEFER) {
  792. dev_dbg(dev, "Device match requests probe deferral\n");
  793. dev->can_match = true;
  794. driver_deferred_probe_add(dev);
  795. /*
  796. * Device can't match with a driver right now, so don't attempt
  797. * to match or bind with other drivers on the bus.
  798. */
  799. return ret;
  800. } else if (ret < 0) {
  801. dev_dbg(dev, "Bus failed to match device: %d\n", ret);
  802. return ret;
  803. } /* ret > 0 means positive match */
  804. async_allowed = driver_allows_async_probing(drv);
  805. if (async_allowed)
  806. data->have_async = true;
  807. if (data->check_async && async_allowed != data->want_async)
  808. return 0;
  809. /*
  810. * Ignore errors returned by ->probe so that the next driver can try
  811. * its luck.
  812. */
  813. ret = driver_probe_device(drv, dev);
  814. if (ret < 0)
  815. return ret;
  816. return ret == 0;
  817. }
  818. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  819. {
  820. struct device *dev = _dev;
  821. struct device_attach_data data = {
  822. .dev = dev,
  823. .check_async = true,
  824. .want_async = true,
  825. };
  826. device_lock(dev);
  827. /*
  828. * Check if device has already been removed or claimed. This may
  829. * happen with driver loading, device discovery/registration,
  830. * and deferred probe processing happens all at once with
  831. * multiple threads.
  832. */
  833. if (dev->p->dead || dev->driver)
  834. goto out_unlock;
  835. if (dev->parent)
  836. pm_runtime_get_sync(dev->parent);
  837. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  838. dev_dbg(dev, "async probe completed\n");
  839. pm_request_idle(dev);
  840. if (dev->parent)
  841. pm_runtime_put(dev->parent);
  842. out_unlock:
  843. device_unlock(dev);
  844. put_device(dev);
  845. }
  846. static int __device_attach(struct device *dev, bool allow_async)
  847. {
  848. int ret = 0;
  849. bool async = false;
  850. device_lock(dev);
  851. if (dev->p->dead) {
  852. goto out_unlock;
  853. } else if (dev->driver) {
  854. if (device_is_bound(dev)) {
  855. ret = 1;
  856. goto out_unlock;
  857. }
  858. ret = device_bind_driver(dev);
  859. if (ret == 0)
  860. ret = 1;
  861. else {
  862. device_set_driver(dev, NULL);
  863. ret = 0;
  864. }
  865. } else {
  866. struct device_attach_data data = {
  867. .dev = dev,
  868. .check_async = allow_async,
  869. .want_async = false,
  870. };
  871. if (dev->parent)
  872. pm_runtime_get_sync(dev->parent);
  873. ret = bus_for_each_drv(dev->bus, NULL, &data,
  874. __device_attach_driver);
  875. if (!ret && allow_async && data.have_async) {
  876. /*
  877. * If we could not find appropriate driver
  878. * synchronously and we are allowed to do
  879. * async probes and there are drivers that
  880. * want to probe asynchronously, we'll
  881. * try them.
  882. */
  883. dev_dbg(dev, "scheduling asynchronous probe\n");
  884. get_device(dev);
  885. async = true;
  886. } else {
  887. pm_request_idle(dev);
  888. }
  889. if (dev->parent)
  890. pm_runtime_put(dev->parent);
  891. }
  892. out_unlock:
  893. device_unlock(dev);
  894. if (async)
  895. async_schedule_dev(__device_attach_async_helper, dev);
  896. return ret;
  897. }
  898. /**
  899. * device_attach - try to attach device to a driver.
  900. * @dev: device.
  901. *
  902. * Walk the list of drivers that the bus has and call
  903. * driver_probe_device() for each pair. If a compatible
  904. * pair is found, break out and return.
  905. *
  906. * Returns 1 if the device was bound to a driver;
  907. * 0 if no matching driver was found;
  908. * -ENODEV if the device is not registered.
  909. *
  910. * When called for a USB interface, @dev->parent lock must be held.
  911. */
  912. int device_attach(struct device *dev)
  913. {
  914. return __device_attach(dev, false);
  915. }
  916. EXPORT_SYMBOL_GPL(device_attach);
  917. void device_initial_probe(struct device *dev)
  918. {
  919. __device_attach(dev, true);
  920. }
  921. /*
  922. * __device_driver_lock - acquire locks needed to manipulate dev->drv
  923. * @dev: Device we will update driver info for
  924. * @parent: Parent device. Needed if the bus requires parent lock
  925. *
  926. * This function will take the required locks for manipulating dev->drv.
  927. * Normally this will just be the @dev lock, but when called for a USB
  928. * interface, @parent lock will be held as well.
  929. */
  930. static void __device_driver_lock(struct device *dev, struct device *parent)
  931. {
  932. if (parent && dev->bus->need_parent_lock)
  933. device_lock(parent);
  934. device_lock(dev);
  935. }
  936. /*
  937. * __device_driver_unlock - release 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 release the required locks for manipulating dev->drv.
  942. * Normally this will just be the @dev lock, but when called for a
  943. * USB interface, @parent lock will be released as well.
  944. */
  945. static void __device_driver_unlock(struct device *dev, struct device *parent)
  946. {
  947. device_unlock(dev);
  948. if (parent && dev->bus->need_parent_lock)
  949. device_unlock(parent);
  950. }
  951. /**
  952. * device_driver_attach - attach a specific driver to a specific device
  953. * @drv: Driver to attach
  954. * @dev: Device to attach it to
  955. *
  956. * Manually attach driver to a device. Will acquire both @dev lock and
  957. * @dev->parent lock if needed. Returns 0 on success, -ERR on failure.
  958. */
  959. int device_driver_attach(const struct device_driver *drv, struct device *dev)
  960. {
  961. int ret;
  962. __device_driver_lock(dev, dev->parent);
  963. ret = __driver_probe_device(drv, dev);
  964. __device_driver_unlock(dev, dev->parent);
  965. /* also return probe errors as normal negative errnos */
  966. if (ret > 0)
  967. ret = -ret;
  968. if (ret == -EPROBE_DEFER)
  969. return -EAGAIN;
  970. return ret;
  971. }
  972. EXPORT_SYMBOL_GPL(device_driver_attach);
  973. static void __driver_attach_async_helper(void *_dev, async_cookie_t cookie)
  974. {
  975. struct device *dev = _dev;
  976. const struct device_driver *drv;
  977. int ret;
  978. __device_driver_lock(dev, dev->parent);
  979. drv = dev->p->async_driver;
  980. dev->p->async_driver = NULL;
  981. ret = driver_probe_device(drv, dev);
  982. __device_driver_unlock(dev, dev->parent);
  983. dev_dbg(dev, "driver %s async attach completed: %d\n", drv->name, ret);
  984. put_device(dev);
  985. }
  986. static int __driver_attach(struct device *dev, void *data)
  987. {
  988. const struct device_driver *drv = data;
  989. bool async = false;
  990. int ret;
  991. /*
  992. * Lock device and try to bind to it. We drop the error
  993. * here and always return 0, because we need to keep trying
  994. * to bind to devices and some drivers will return an error
  995. * simply if it didn't support the device.
  996. *
  997. * driver_probe_device() will spit a warning if there
  998. * is an error.
  999. */
  1000. ret = driver_match_device(drv, dev);
  1001. if (ret == 0) {
  1002. /* no match */
  1003. return 0;
  1004. } else if (ret == -EPROBE_DEFER) {
  1005. dev_dbg(dev, "Device match requests probe deferral\n");
  1006. dev->can_match = true;
  1007. driver_deferred_probe_add(dev);
  1008. /*
  1009. * Driver could not match with device, but may match with
  1010. * another device on the bus.
  1011. */
  1012. return 0;
  1013. } else if (ret < 0) {
  1014. dev_dbg(dev, "Bus failed to match device: %d\n", ret);
  1015. /*
  1016. * Driver could not match with device, but may match with
  1017. * another device on the bus.
  1018. */
  1019. return 0;
  1020. } /* ret > 0 means positive match */
  1021. if (driver_allows_async_probing(drv)) {
  1022. /*
  1023. * Instead of probing the device synchronously we will
  1024. * probe it asynchronously to allow for more parallelism.
  1025. *
  1026. * We only take the device lock here in order to guarantee
  1027. * that the dev->driver and async_driver fields are protected
  1028. */
  1029. dev_dbg(dev, "probing driver %s asynchronously\n", drv->name);
  1030. device_lock(dev);
  1031. if (!dev->driver && !dev->p->async_driver) {
  1032. get_device(dev);
  1033. dev->p->async_driver = drv;
  1034. async = true;
  1035. }
  1036. device_unlock(dev);
  1037. if (async)
  1038. async_schedule_dev(__driver_attach_async_helper, dev);
  1039. return 0;
  1040. }
  1041. __device_driver_lock(dev, dev->parent);
  1042. driver_probe_device(drv, dev);
  1043. __device_driver_unlock(dev, dev->parent);
  1044. return 0;
  1045. }
  1046. /**
  1047. * driver_attach - try to bind driver to devices.
  1048. * @drv: driver.
  1049. *
  1050. * Walk the list of devices that the bus has on it and try to
  1051. * match the driver with each one. If driver_probe_device()
  1052. * returns 0 and the @dev->driver is set, we've found a
  1053. * compatible pair.
  1054. */
  1055. int driver_attach(const struct device_driver *drv)
  1056. {
  1057. /* The (void *) will be put back to const * in __driver_attach() */
  1058. return bus_for_each_dev(drv->bus, NULL, (void *)drv, __driver_attach);
  1059. }
  1060. EXPORT_SYMBOL_GPL(driver_attach);
  1061. /*
  1062. * __device_release_driver() must be called with @dev lock held.
  1063. * When called for a USB interface, @dev->parent lock must be held as well.
  1064. */
  1065. static void __device_release_driver(struct device *dev, struct device *parent)
  1066. {
  1067. struct device_driver *drv;
  1068. drv = dev->driver;
  1069. if (drv) {
  1070. pm_runtime_get_sync(dev);
  1071. while (device_links_busy(dev)) {
  1072. __device_driver_unlock(dev, parent);
  1073. device_links_unbind_consumers(dev);
  1074. __device_driver_lock(dev, parent);
  1075. /*
  1076. * A concurrent invocation of the same function might
  1077. * have released the driver successfully while this one
  1078. * was waiting, so check for that.
  1079. */
  1080. if (dev->driver != drv) {
  1081. pm_runtime_put(dev);
  1082. return;
  1083. }
  1084. }
  1085. driver_sysfs_remove(dev);
  1086. bus_notify(dev, BUS_NOTIFY_UNBIND_DRIVER);
  1087. pm_runtime_put_sync(dev);
  1088. device_remove(dev);
  1089. if (dev->bus && dev->bus->dma_cleanup)
  1090. dev->bus->dma_cleanup(dev);
  1091. device_unbind_cleanup(dev);
  1092. device_links_driver_cleanup(dev);
  1093. klist_remove(&dev->p->knode_driver);
  1094. device_pm_check_callbacks(dev);
  1095. bus_notify(dev, BUS_NOTIFY_UNBOUND_DRIVER);
  1096. kobject_uevent(&dev->kobj, KOBJ_UNBIND);
  1097. }
  1098. }
  1099. void device_release_driver_internal(struct device *dev,
  1100. const struct device_driver *drv,
  1101. struct device *parent)
  1102. {
  1103. __device_driver_lock(dev, parent);
  1104. if (!drv || drv == dev->driver)
  1105. __device_release_driver(dev, parent);
  1106. __device_driver_unlock(dev, parent);
  1107. }
  1108. /**
  1109. * device_release_driver - manually detach device from driver.
  1110. * @dev: device.
  1111. *
  1112. * Manually detach device from driver.
  1113. * When called for a USB interface, @dev->parent lock must be held.
  1114. *
  1115. * If this function is to be called with @dev->parent lock held, ensure that
  1116. * the device's consumers are unbound in advance or that their locks can be
  1117. * acquired under the @dev->parent lock.
  1118. */
  1119. void device_release_driver(struct device *dev)
  1120. {
  1121. /*
  1122. * If anyone calls device_release_driver() recursively from
  1123. * within their ->remove callback for the same device, they
  1124. * will deadlock right here.
  1125. */
  1126. device_release_driver_internal(dev, NULL, NULL);
  1127. }
  1128. EXPORT_SYMBOL_GPL(device_release_driver);
  1129. /**
  1130. * device_driver_detach - detach driver from a specific device
  1131. * @dev: device to detach driver from
  1132. *
  1133. * Detach driver from device. Will acquire both @dev lock and @dev->parent
  1134. * lock if needed.
  1135. */
  1136. void device_driver_detach(struct device *dev)
  1137. {
  1138. device_release_driver_internal(dev, NULL, dev->parent);
  1139. }
  1140. /**
  1141. * driver_detach - detach driver from all devices it controls.
  1142. * @drv: driver.
  1143. */
  1144. void driver_detach(const struct device_driver *drv)
  1145. {
  1146. struct device_private *dev_prv;
  1147. struct device *dev;
  1148. if (driver_allows_async_probing(drv))
  1149. async_synchronize_full();
  1150. for (;;) {
  1151. spin_lock(&drv->p->klist_devices.k_lock);
  1152. if (list_empty(&drv->p->klist_devices.k_list)) {
  1153. spin_unlock(&drv->p->klist_devices.k_lock);
  1154. break;
  1155. }
  1156. dev_prv = list_last_entry(&drv->p->klist_devices.k_list,
  1157. struct device_private,
  1158. knode_driver.n_node);
  1159. dev = dev_prv->device;
  1160. get_device(dev);
  1161. spin_unlock(&drv->p->klist_devices.k_lock);
  1162. device_release_driver_internal(dev, drv, dev->parent);
  1163. put_device(dev);
  1164. }
  1165. }