dd.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. /*
  2. * drivers/base/dd.c - The core device/driver interactions.
  3. *
  4. * This file contains the (sometimes tricky) code that controls the
  5. * interactions between devices and drivers, which primarily includes
  6. * driver binding and unbinding.
  7. *
  8. * All of this code used to exist in drivers/base/bus.c, but was
  9. * relocated to here in the name of compartmentalization (since it wasn't
  10. * strictly code just for the 'struct bus_type'.
  11. *
  12. * Copyright (c) 2002-5 Patrick Mochel
  13. * Copyright (c) 2002-3 Open Source Development Labs
  14. * Copyright (c) 2007-2009 Greg Kroah-Hartman <gregkh@suse.de>
  15. * Copyright (c) 2007-2009 Novell Inc.
  16. *
  17. * This file is released under the GPLv2
  18. */
  19. #include <linux/device.h>
  20. #include <linux/delay.h>
  21. #include <linux/module.h>
  22. #include <linux/kthread.h>
  23. #include <linux/wait.h>
  24. #include <linux/async.h>
  25. #include <linux/pm_runtime.h>
  26. #include <linux/pinctrl/devinfo.h>
  27. #include "base.h"
  28. #include "power/power.h"
  29. /*
  30. * Deferred Probe infrastructure.
  31. *
  32. * Sometimes driver probe order matters, but the kernel doesn't always have
  33. * dependency information which means some drivers will get probed before a
  34. * resource it depends on is available. For example, an SDHCI driver may
  35. * first need a GPIO line from an i2c GPIO controller before it can be
  36. * initialized. If a required resource is not available yet, a driver can
  37. * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
  38. *
  39. * Deferred probe maintains two lists of devices, a pending list and an active
  40. * list. A driver returning -EPROBE_DEFER causes the device to be added to the
  41. * pending list. A successful driver probe will trigger moving all devices
  42. * from the pending to the active list so that the workqueue will eventually
  43. * retry them.
  44. *
  45. * The deferred_probe_mutex must be held any time the deferred_probe_*_list
  46. * of the (struct device*)->p->deferred_probe pointers are manipulated
  47. */
  48. static DEFINE_MUTEX(deferred_probe_mutex);
  49. static LIST_HEAD(deferred_probe_pending_list);
  50. static LIST_HEAD(deferred_probe_active_list);
  51. static struct workqueue_struct *deferred_wq;
  52. static atomic_t deferred_trigger_count = ATOMIC_INIT(0);
  53. /*
  54. * deferred_probe_work_func() - Retry probing devices in the active list.
  55. */
  56. static void deferred_probe_work_func(struct work_struct *work)
  57. {
  58. struct device *dev;
  59. struct device_private *private;
  60. /*
  61. * This block processes every device in the deferred 'active' list.
  62. * Each device is removed from the active list and passed to
  63. * bus_probe_device() to re-attempt the probe. The loop continues
  64. * until every device in the active list is removed and retried.
  65. *
  66. * Note: Once the device is removed from the list and the mutex is
  67. * released, it is possible for the device get freed by another thread
  68. * and cause a illegal pointer dereference. This code uses
  69. * get/put_device() to ensure the device structure cannot disappear
  70. * from under our feet.
  71. */
  72. mutex_lock(&deferred_probe_mutex);
  73. while (!list_empty(&deferred_probe_active_list)) {
  74. private = list_first_entry(&deferred_probe_active_list,
  75. typeof(*dev->p), deferred_probe);
  76. dev = private->device;
  77. list_del_init(&private->deferred_probe);
  78. get_device(dev);
  79. /*
  80. * Drop the mutex while probing each device; the probe path may
  81. * manipulate the deferred list
  82. */
  83. mutex_unlock(&deferred_probe_mutex);
  84. /*
  85. * Force the device to the end of the dpm_list since
  86. * the PM code assumes that the order we add things to
  87. * the list is a good order for suspend but deferred
  88. * probe makes that very unsafe.
  89. */
  90. device_pm_lock();
  91. device_pm_move_last(dev);
  92. device_pm_unlock();
  93. dev_dbg(dev, "Retrying from deferred list\n");
  94. bus_probe_device(dev);
  95. mutex_lock(&deferred_probe_mutex);
  96. put_device(dev);
  97. }
  98. mutex_unlock(&deferred_probe_mutex);
  99. }
  100. static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
  101. static void driver_deferred_probe_add(struct device *dev)
  102. {
  103. mutex_lock(&deferred_probe_mutex);
  104. if (list_empty(&dev->p->deferred_probe)) {
  105. dev_dbg(dev, "Added to deferred list\n");
  106. list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
  107. }
  108. mutex_unlock(&deferred_probe_mutex);
  109. }
  110. void driver_deferred_probe_del(struct device *dev)
  111. {
  112. mutex_lock(&deferred_probe_mutex);
  113. if (!list_empty(&dev->p->deferred_probe)) {
  114. dev_dbg(dev, "Removed from deferred list\n");
  115. list_del_init(&dev->p->deferred_probe);
  116. }
  117. mutex_unlock(&deferred_probe_mutex);
  118. }
  119. static bool driver_deferred_probe_enable = false;
  120. /**
  121. * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
  122. *
  123. * This functions moves all devices from the pending list to the active
  124. * list and schedules the deferred probe workqueue to process them. It
  125. * should be called anytime a driver is successfully bound to a device.
  126. *
  127. * Note, there is a race condition in multi-threaded probe. In the case where
  128. * more than one device is probing at the same time, it is possible for one
  129. * probe to complete successfully while another is about to defer. If the second
  130. * depends on the first, then it will get put on the pending list after the
  131. * trigger event has already occurred and will be stuck there.
  132. *
  133. * The atomic 'deferred_trigger_count' is used to determine if a successful
  134. * trigger has occurred in the midst of probing a driver. If the trigger count
  135. * changes in the midst of a probe, then deferred processing should be triggered
  136. * again.
  137. */
  138. static void driver_deferred_probe_trigger(void)
  139. {
  140. if (!driver_deferred_probe_enable)
  141. return;
  142. /*
  143. * A successful probe means that all the devices in the pending list
  144. * should be triggered to be reprobed. Move all the deferred devices
  145. * into the active list so they can be retried by the workqueue
  146. */
  147. mutex_lock(&deferred_probe_mutex);
  148. atomic_inc(&deferred_trigger_count);
  149. list_splice_tail_init(&deferred_probe_pending_list,
  150. &deferred_probe_active_list);
  151. mutex_unlock(&deferred_probe_mutex);
  152. /*
  153. * Kick the re-probe thread. It may already be scheduled, but it is
  154. * safe to kick it again.
  155. */
  156. queue_work(deferred_wq, &deferred_probe_work);
  157. }
  158. /**
  159. * deferred_probe_initcall() - Enable probing of deferred devices
  160. *
  161. * We don't want to get in the way when the bulk of drivers are getting probed.
  162. * Instead, this initcall makes sure that deferred probing is delayed until
  163. * late_initcall time.
  164. */
  165. static int deferred_probe_initcall(void)
  166. {
  167. deferred_wq = create_singlethread_workqueue("deferwq");
  168. if (WARN_ON(!deferred_wq))
  169. return -ENOMEM;
  170. driver_deferred_probe_enable = true;
  171. driver_deferred_probe_trigger();
  172. /* Sort as many dependencies as possible before exiting initcalls */
  173. flush_workqueue(deferred_wq);
  174. return 0;
  175. }
  176. late_initcall(deferred_probe_initcall);
  177. static void driver_bound(struct device *dev)
  178. {
  179. if (klist_node_attached(&dev->p->knode_driver)) {
  180. printk(KERN_WARNING "%s: device %s already bound\n",
  181. __func__, kobject_name(&dev->kobj));
  182. return;
  183. }
  184. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev->driver->name,
  185. __func__, dev_name(dev));
  186. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  187. /*
  188. * Make sure the device is no longer in one of the deferred lists and
  189. * kick off retrying all pending devices
  190. */
  191. driver_deferred_probe_del(dev);
  192. driver_deferred_probe_trigger();
  193. if (dev->bus)
  194. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  195. BUS_NOTIFY_BOUND_DRIVER, dev);
  196. }
  197. static int driver_sysfs_add(struct device *dev)
  198. {
  199. int ret;
  200. if (dev->bus)
  201. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  202. BUS_NOTIFY_BIND_DRIVER, dev);
  203. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  204. kobject_name(&dev->kobj));
  205. if (ret == 0) {
  206. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  207. "driver");
  208. if (ret)
  209. sysfs_remove_link(&dev->driver->p->kobj,
  210. kobject_name(&dev->kobj));
  211. }
  212. return ret;
  213. }
  214. static void driver_sysfs_remove(struct device *dev)
  215. {
  216. struct device_driver *drv = dev->driver;
  217. if (drv) {
  218. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  219. sysfs_remove_link(&dev->kobj, "driver");
  220. }
  221. }
  222. /**
  223. * device_bind_driver - bind a driver to one device.
  224. * @dev: device.
  225. *
  226. * Allow manual attachment of a driver to a device.
  227. * Caller must have already set @dev->driver.
  228. *
  229. * Note that this does not modify the bus reference count
  230. * nor take the bus's rwsem. Please verify those are accounted
  231. * for before calling this. (It is ok to call with no other effort
  232. * from a driver's probe() method.)
  233. *
  234. * This function must be called with the device lock held.
  235. */
  236. int device_bind_driver(struct device *dev)
  237. {
  238. int ret;
  239. ret = driver_sysfs_add(dev);
  240. if (!ret)
  241. driver_bound(dev);
  242. return ret;
  243. }
  244. EXPORT_SYMBOL_GPL(device_bind_driver);
  245. static atomic_t probe_count = ATOMIC_INIT(0);
  246. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  247. static int really_probe(struct device *dev, struct device_driver *drv)
  248. {
  249. int ret = 0;
  250. int local_trigger_count = atomic_read(&deferred_trigger_count);
  251. atomic_inc(&probe_count);
  252. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  253. drv->bus->name, __func__, drv->name, dev_name(dev));
  254. WARN_ON(!list_empty(&dev->devres_head));
  255. dev->driver = drv;
  256. /* If using pinctrl, bind pins now before probing */
  257. ret = pinctrl_bind_pins(dev);
  258. if (ret)
  259. goto probe_failed;
  260. if (driver_sysfs_add(dev)) {
  261. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  262. __func__, dev_name(dev));
  263. goto probe_failed;
  264. }
  265. if (dev->pm_domain && dev->pm_domain->activate) {
  266. ret = dev->pm_domain->activate(dev);
  267. if (ret)
  268. goto probe_failed;
  269. }
  270. /*
  271. * Ensure devices are listed in devices_kset in correct order
  272. * It's important to move Dev to the end of devices_kset before
  273. * calling .probe, because it could be recursive and parent Dev
  274. * should always go first
  275. */
  276. devices_kset_move_last(dev);
  277. if (dev->bus->probe) {
  278. ret = dev->bus->probe(dev);
  279. if (ret)
  280. goto probe_failed;
  281. } else if (drv->probe) {
  282. ret = drv->probe(dev);
  283. if (ret)
  284. goto probe_failed;
  285. }
  286. pinctrl_init_done(dev);
  287. if (dev->pm_domain && dev->pm_domain->sync)
  288. dev->pm_domain->sync(dev);
  289. driver_bound(dev);
  290. ret = 1;
  291. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  292. drv->bus->name, __func__, dev_name(dev), drv->name);
  293. goto done;
  294. probe_failed:
  295. devres_release_all(dev);
  296. driver_sysfs_remove(dev);
  297. dev->driver = NULL;
  298. dev_set_drvdata(dev, NULL);
  299. if (dev->pm_domain && dev->pm_domain->dismiss)
  300. dev->pm_domain->dismiss(dev);
  301. switch (ret) {
  302. case -EPROBE_DEFER:
  303. /* Driver requested deferred probing */
  304. dev_dbg(dev, "Driver %s requests probe deferral\n", drv->name);
  305. driver_deferred_probe_add(dev);
  306. /* Did a trigger occur while probing? Need to re-trigger if yes */
  307. if (local_trigger_count != atomic_read(&deferred_trigger_count))
  308. driver_deferred_probe_trigger();
  309. break;
  310. case -ENODEV:
  311. case -ENXIO:
  312. pr_debug("%s: probe of %s rejects match %d\n",
  313. drv->name, dev_name(dev), ret);
  314. break;
  315. default:
  316. /* driver matched but the probe failed */
  317. printk(KERN_WARNING
  318. "%s: probe of %s failed with error %d\n",
  319. drv->name, dev_name(dev), ret);
  320. }
  321. /*
  322. * Ignore errors returned by ->probe so that the next driver can try
  323. * its luck.
  324. */
  325. ret = 0;
  326. done:
  327. atomic_dec(&probe_count);
  328. wake_up(&probe_waitqueue);
  329. return ret;
  330. }
  331. /**
  332. * driver_probe_done
  333. * Determine if the probe sequence is finished or not.
  334. *
  335. * Should somehow figure out how to use a semaphore, not an atomic variable...
  336. */
  337. int driver_probe_done(void)
  338. {
  339. pr_debug("%s: probe_count = %d\n", __func__,
  340. atomic_read(&probe_count));
  341. if (atomic_read(&probe_count))
  342. return -EBUSY;
  343. return 0;
  344. }
  345. /**
  346. * wait_for_device_probe
  347. * Wait for device probing to be completed.
  348. */
  349. void wait_for_device_probe(void)
  350. {
  351. /* wait for the known devices to complete their probing */
  352. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  353. async_synchronize_full();
  354. }
  355. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  356. /**
  357. * driver_probe_device - attempt to bind device & driver together
  358. * @drv: driver to bind a device to
  359. * @dev: device to try to bind to the driver
  360. *
  361. * This function returns -ENODEV if the device is not registered,
  362. * 1 if the device is bound successfully and 0 otherwise.
  363. *
  364. * This function must be called with @dev lock held. When called for a
  365. * USB interface, @dev->parent lock must be held as well.
  366. *
  367. * If the device has a parent, runtime-resume the parent before driver probing.
  368. */
  369. int driver_probe_device(struct device_driver *drv, struct device *dev)
  370. {
  371. int ret = 0;
  372. if (!device_is_registered(dev))
  373. return -ENODEV;
  374. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  375. drv->bus->name, __func__, dev_name(dev), drv->name);
  376. if (dev->parent)
  377. pm_runtime_get_sync(dev->parent);
  378. pm_runtime_barrier(dev);
  379. ret = really_probe(dev, drv);
  380. pm_request_idle(dev);
  381. if (dev->parent)
  382. pm_runtime_put(dev->parent);
  383. return ret;
  384. }
  385. bool driver_allows_async_probing(struct device_driver *drv)
  386. {
  387. switch (drv->probe_type) {
  388. case PROBE_PREFER_ASYNCHRONOUS:
  389. return true;
  390. case PROBE_FORCE_SYNCHRONOUS:
  391. return false;
  392. default:
  393. if (module_requested_async_probing(drv->owner))
  394. return true;
  395. return false;
  396. }
  397. }
  398. struct device_attach_data {
  399. struct device *dev;
  400. /*
  401. * Indicates whether we are are considering asynchronous probing or
  402. * not. Only initial binding after device or driver registration
  403. * (including deferral processing) may be done asynchronously, the
  404. * rest is always synchronous, as we expect it is being done by
  405. * request from userspace.
  406. */
  407. bool check_async;
  408. /*
  409. * Indicates if we are binding synchronous or asynchronous drivers.
  410. * When asynchronous probing is enabled we'll execute 2 passes
  411. * over drivers: first pass doing synchronous probing and second
  412. * doing asynchronous probing (if synchronous did not succeed -
  413. * most likely because there was no driver requiring synchronous
  414. * probing - and we found asynchronous driver during first pass).
  415. * The 2 passes are done because we can't shoot asynchronous
  416. * probe for given device and driver from bus_for_each_drv() since
  417. * driver pointer is not guaranteed to stay valid once
  418. * bus_for_each_drv() iterates to the next driver on the bus.
  419. */
  420. bool want_async;
  421. /*
  422. * We'll set have_async to 'true' if, while scanning for matching
  423. * driver, we'll encounter one that requests asynchronous probing.
  424. */
  425. bool have_async;
  426. };
  427. static int __device_attach_driver(struct device_driver *drv, void *_data)
  428. {
  429. struct device_attach_data *data = _data;
  430. struct device *dev = data->dev;
  431. bool async_allowed;
  432. /*
  433. * Check if device has already been claimed. This may
  434. * happen with driver loading, device discovery/registration,
  435. * and deferred probe processing happens all at once with
  436. * multiple threads.
  437. */
  438. if (dev->driver)
  439. return -EBUSY;
  440. if (!driver_match_device(drv, dev))
  441. return 0;
  442. async_allowed = driver_allows_async_probing(drv);
  443. if (async_allowed)
  444. data->have_async = true;
  445. if (data->check_async && async_allowed != data->want_async)
  446. return 0;
  447. return driver_probe_device(drv, dev);
  448. }
  449. static void __device_attach_async_helper(void *_dev, async_cookie_t cookie)
  450. {
  451. struct device *dev = _dev;
  452. struct device_attach_data data = {
  453. .dev = dev,
  454. .check_async = true,
  455. .want_async = true,
  456. };
  457. device_lock(dev);
  458. if (dev->parent)
  459. pm_runtime_get_sync(dev->parent);
  460. bus_for_each_drv(dev->bus, NULL, &data, __device_attach_driver);
  461. dev_dbg(dev, "async probe completed\n");
  462. pm_request_idle(dev);
  463. if (dev->parent)
  464. pm_runtime_put(dev->parent);
  465. device_unlock(dev);
  466. put_device(dev);
  467. }
  468. static int __device_attach(struct device *dev, bool allow_async)
  469. {
  470. int ret = 0;
  471. device_lock(dev);
  472. if (dev->driver) {
  473. if (klist_node_attached(&dev->p->knode_driver)) {
  474. ret = 1;
  475. goto out_unlock;
  476. }
  477. ret = device_bind_driver(dev);
  478. if (ret == 0)
  479. ret = 1;
  480. else {
  481. dev->driver = NULL;
  482. ret = 0;
  483. }
  484. } else {
  485. struct device_attach_data data = {
  486. .dev = dev,
  487. .check_async = allow_async,
  488. .want_async = false,
  489. };
  490. if (dev->parent)
  491. pm_runtime_get_sync(dev->parent);
  492. ret = bus_for_each_drv(dev->bus, NULL, &data,
  493. __device_attach_driver);
  494. if (!ret && allow_async && data.have_async) {
  495. /*
  496. * If we could not find appropriate driver
  497. * synchronously and we are allowed to do
  498. * async probes and there are drivers that
  499. * want to probe asynchronously, we'll
  500. * try them.
  501. */
  502. dev_dbg(dev, "scheduling asynchronous probe\n");
  503. get_device(dev);
  504. async_schedule(__device_attach_async_helper, dev);
  505. } else {
  506. pm_request_idle(dev);
  507. }
  508. if (dev->parent)
  509. pm_runtime_put(dev->parent);
  510. }
  511. out_unlock:
  512. device_unlock(dev);
  513. return ret;
  514. }
  515. /**
  516. * device_attach - try to attach device to a driver.
  517. * @dev: device.
  518. *
  519. * Walk the list of drivers that the bus has and call
  520. * driver_probe_device() for each pair. If a compatible
  521. * pair is found, break out and return.
  522. *
  523. * Returns 1 if the device was bound to a driver;
  524. * 0 if no matching driver was found;
  525. * -ENODEV if the device is not registered.
  526. *
  527. * When called for a USB interface, @dev->parent lock must be held.
  528. */
  529. int device_attach(struct device *dev)
  530. {
  531. return __device_attach(dev, false);
  532. }
  533. EXPORT_SYMBOL_GPL(device_attach);
  534. void device_initial_probe(struct device *dev)
  535. {
  536. __device_attach(dev, true);
  537. }
  538. static int __driver_attach(struct device *dev, void *data)
  539. {
  540. struct device_driver *drv = data;
  541. /*
  542. * Lock device and try to bind to it. We drop the error
  543. * here and always return 0, because we need to keep trying
  544. * to bind to devices and some drivers will return an error
  545. * simply if it didn't support the device.
  546. *
  547. * driver_probe_device() will spit a warning if there
  548. * is an error.
  549. */
  550. if (!driver_match_device(drv, dev))
  551. return 0;
  552. if (dev->parent) /* Needed for USB */
  553. device_lock(dev->parent);
  554. device_lock(dev);
  555. if (!dev->driver)
  556. driver_probe_device(drv, dev);
  557. device_unlock(dev);
  558. if (dev->parent)
  559. device_unlock(dev->parent);
  560. return 0;
  561. }
  562. /**
  563. * driver_attach - try to bind driver to devices.
  564. * @drv: driver.
  565. *
  566. * Walk the list of devices that the bus has on it and try to
  567. * match the driver with each one. If driver_probe_device()
  568. * returns 0 and the @dev->driver is set, we've found a
  569. * compatible pair.
  570. */
  571. int driver_attach(struct device_driver *drv)
  572. {
  573. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  574. }
  575. EXPORT_SYMBOL_GPL(driver_attach);
  576. /*
  577. * __device_release_driver() must be called with @dev lock held.
  578. * When called for a USB interface, @dev->parent lock must be held as well.
  579. */
  580. static void __device_release_driver(struct device *dev)
  581. {
  582. struct device_driver *drv;
  583. drv = dev->driver;
  584. if (drv) {
  585. if (driver_allows_async_probing(drv))
  586. async_synchronize_full();
  587. pm_runtime_get_sync(dev);
  588. driver_sysfs_remove(dev);
  589. if (dev->bus)
  590. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  591. BUS_NOTIFY_UNBIND_DRIVER,
  592. dev);
  593. pm_runtime_put_sync(dev);
  594. if (dev->bus && dev->bus->remove)
  595. dev->bus->remove(dev);
  596. else if (drv->remove)
  597. drv->remove(dev);
  598. devres_release_all(dev);
  599. dev->driver = NULL;
  600. dev_set_drvdata(dev, NULL);
  601. if (dev->pm_domain && dev->pm_domain->dismiss)
  602. dev->pm_domain->dismiss(dev);
  603. klist_remove(&dev->p->knode_driver);
  604. if (dev->bus)
  605. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  606. BUS_NOTIFY_UNBOUND_DRIVER,
  607. dev);
  608. }
  609. }
  610. /**
  611. * device_release_driver - manually detach device from driver.
  612. * @dev: device.
  613. *
  614. * Manually detach device from driver.
  615. * When called for a USB interface, @dev->parent lock must be held.
  616. */
  617. void device_release_driver(struct device *dev)
  618. {
  619. /*
  620. * If anyone calls device_release_driver() recursively from
  621. * within their ->remove callback for the same device, they
  622. * will deadlock right here.
  623. */
  624. device_lock(dev);
  625. __device_release_driver(dev);
  626. device_unlock(dev);
  627. }
  628. EXPORT_SYMBOL_GPL(device_release_driver);
  629. /**
  630. * driver_detach - detach driver from all devices it controls.
  631. * @drv: driver.
  632. */
  633. void driver_detach(struct device_driver *drv)
  634. {
  635. struct device_private *dev_prv;
  636. struct device *dev;
  637. for (;;) {
  638. spin_lock(&drv->p->klist_devices.k_lock);
  639. if (list_empty(&drv->p->klist_devices.k_list)) {
  640. spin_unlock(&drv->p->klist_devices.k_lock);
  641. break;
  642. }
  643. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  644. struct device_private,
  645. knode_driver.n_node);
  646. dev = dev_prv->device;
  647. get_device(dev);
  648. spin_unlock(&drv->p->klist_devices.k_lock);
  649. if (dev->parent) /* Needed for USB */
  650. device_lock(dev->parent);
  651. device_lock(dev);
  652. if (dev->driver == drv)
  653. __device_release_driver(dev);
  654. device_unlock(dev);
  655. if (dev->parent)
  656. device_unlock(dev->parent);
  657. put_device(dev);
  658. }
  659. }