dd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 "base.h"
  27. #include "power/power.h"
  28. /*
  29. * Deferred Probe infrastructure.
  30. *
  31. * Sometimes driver probe order matters, but the kernel doesn't always have
  32. * dependency information which means some drivers will get probed before a
  33. * resource it depends on is available. For example, an SDHCI driver may
  34. * first need a GPIO line from an i2c GPIO controller before it can be
  35. * initialized. If a required resource is not available yet, a driver can
  36. * request probing to be deferred by returning -EPROBE_DEFER from its probe hook
  37. *
  38. * Deferred probe maintains two lists of devices, a pending list and an active
  39. * list. A driver returning -EPROBE_DEFER causes the device to be added to the
  40. * pending list. A successful driver probe will trigger moving all devices
  41. * from the pending to the active list so that the workqueue will eventually
  42. * retry them.
  43. *
  44. * The deferred_probe_mutex must be held any time the deferred_probe_*_list
  45. * of the (struct device*)->p->deferred_probe pointers are manipulated
  46. */
  47. static DEFINE_MUTEX(deferred_probe_mutex);
  48. static LIST_HEAD(deferred_probe_pending_list);
  49. static LIST_HEAD(deferred_probe_active_list);
  50. static struct workqueue_struct *deferred_wq;
  51. /**
  52. * deferred_probe_work_func() - Retry probing devices in the active list.
  53. */
  54. static void deferred_probe_work_func(struct work_struct *work)
  55. {
  56. struct device *dev;
  57. struct device_private *private;
  58. /*
  59. * This block processes every device in the deferred 'active' list.
  60. * Each device is removed from the active list and passed to
  61. * bus_probe_device() to re-attempt the probe. The loop continues
  62. * until every device in the active list is removed and retried.
  63. *
  64. * Note: Once the device is removed from the list and the mutex is
  65. * released, it is possible for the device get freed by another thread
  66. * and cause a illegal pointer dereference. This code uses
  67. * get/put_device() to ensure the device structure cannot disappear
  68. * from under our feet.
  69. */
  70. mutex_lock(&deferred_probe_mutex);
  71. while (!list_empty(&deferred_probe_active_list)) {
  72. private = list_first_entry(&deferred_probe_active_list,
  73. typeof(*dev->p), deferred_probe);
  74. dev = private->device;
  75. list_del_init(&private->deferred_probe);
  76. get_device(dev);
  77. /*
  78. * Drop the mutex while probing each device; the probe path may
  79. * manipulate the deferred list
  80. */
  81. mutex_unlock(&deferred_probe_mutex);
  82. /*
  83. * Force the device to the end of the dpm_list since
  84. * the PM code assumes that the order we add things to
  85. * the list is a good order for suspend but deferred
  86. * probe makes that very unsafe.
  87. */
  88. device_pm_lock();
  89. device_pm_move_last(dev);
  90. device_pm_unlock();
  91. dev_dbg(dev, "Retrying from deferred list\n");
  92. bus_probe_device(dev);
  93. mutex_lock(&deferred_probe_mutex);
  94. put_device(dev);
  95. }
  96. mutex_unlock(&deferred_probe_mutex);
  97. }
  98. static DECLARE_WORK(deferred_probe_work, deferred_probe_work_func);
  99. static void driver_deferred_probe_add(struct device *dev)
  100. {
  101. mutex_lock(&deferred_probe_mutex);
  102. if (list_empty(&dev->p->deferred_probe)) {
  103. dev_dbg(dev, "Added to deferred list\n");
  104. list_add_tail(&dev->p->deferred_probe, &deferred_probe_pending_list);
  105. }
  106. mutex_unlock(&deferred_probe_mutex);
  107. }
  108. void driver_deferred_probe_del(struct device *dev)
  109. {
  110. mutex_lock(&deferred_probe_mutex);
  111. if (!list_empty(&dev->p->deferred_probe)) {
  112. dev_dbg(dev, "Removed from deferred list\n");
  113. list_del_init(&dev->p->deferred_probe);
  114. }
  115. mutex_unlock(&deferred_probe_mutex);
  116. }
  117. static bool driver_deferred_probe_enable = false;
  118. /**
  119. * driver_deferred_probe_trigger() - Kick off re-probing deferred devices
  120. *
  121. * This functions moves all devices from the pending list to the active
  122. * list and schedules the deferred probe workqueue to process them. It
  123. * should be called anytime a driver is successfully bound to a device.
  124. */
  125. static void driver_deferred_probe_trigger(void)
  126. {
  127. if (!driver_deferred_probe_enable)
  128. return;
  129. /*
  130. * A successful probe means that all the devices in the pending list
  131. * should be triggered to be reprobed. Move all the deferred devices
  132. * into the active list so they can be retried by the workqueue
  133. */
  134. mutex_lock(&deferred_probe_mutex);
  135. list_splice_tail_init(&deferred_probe_pending_list,
  136. &deferred_probe_active_list);
  137. mutex_unlock(&deferred_probe_mutex);
  138. /*
  139. * Kick the re-probe thread. It may already be scheduled, but it is
  140. * safe to kick it again.
  141. */
  142. queue_work(deferred_wq, &deferred_probe_work);
  143. }
  144. /**
  145. * deferred_probe_initcall() - Enable probing of deferred devices
  146. *
  147. * We don't want to get in the way when the bulk of drivers are getting probed.
  148. * Instead, this initcall makes sure that deferred probing is delayed until
  149. * late_initcall time.
  150. */
  151. static int deferred_probe_initcall(void)
  152. {
  153. deferred_wq = create_singlethread_workqueue("deferwq");
  154. if (WARN_ON(!deferred_wq))
  155. return -ENOMEM;
  156. driver_deferred_probe_enable = true;
  157. driver_deferred_probe_trigger();
  158. return 0;
  159. }
  160. late_initcall(deferred_probe_initcall);
  161. static void driver_bound(struct device *dev)
  162. {
  163. if (klist_node_attached(&dev->p->knode_driver)) {
  164. printk(KERN_WARNING "%s: device %s already bound\n",
  165. __func__, kobject_name(&dev->kobj));
  166. return;
  167. }
  168. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
  169. __func__, dev->driver->name);
  170. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  171. /*
  172. * Make sure the device is no longer in one of the deferred lists and
  173. * kick off retrying all pending devices
  174. */
  175. driver_deferred_probe_del(dev);
  176. driver_deferred_probe_trigger();
  177. if (dev->bus)
  178. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  179. BUS_NOTIFY_BOUND_DRIVER, dev);
  180. }
  181. static int driver_sysfs_add(struct device *dev)
  182. {
  183. int ret;
  184. if (dev->bus)
  185. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  186. BUS_NOTIFY_BIND_DRIVER, dev);
  187. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  188. kobject_name(&dev->kobj));
  189. if (ret == 0) {
  190. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  191. "driver");
  192. if (ret)
  193. sysfs_remove_link(&dev->driver->p->kobj,
  194. kobject_name(&dev->kobj));
  195. }
  196. return ret;
  197. }
  198. static void driver_sysfs_remove(struct device *dev)
  199. {
  200. struct device_driver *drv = dev->driver;
  201. if (drv) {
  202. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  203. sysfs_remove_link(&dev->kobj, "driver");
  204. }
  205. }
  206. /**
  207. * device_bind_driver - bind a driver to one device.
  208. * @dev: device.
  209. *
  210. * Allow manual attachment of a driver to a device.
  211. * Caller must have already set @dev->driver.
  212. *
  213. * Note that this does not modify the bus reference count
  214. * nor take the bus's rwsem. Please verify those are accounted
  215. * for before calling this. (It is ok to call with no other effort
  216. * from a driver's probe() method.)
  217. *
  218. * This function must be called with the device lock held.
  219. */
  220. int device_bind_driver(struct device *dev)
  221. {
  222. int ret;
  223. ret = driver_sysfs_add(dev);
  224. if (!ret)
  225. driver_bound(dev);
  226. return ret;
  227. }
  228. EXPORT_SYMBOL_GPL(device_bind_driver);
  229. static atomic_t probe_count = ATOMIC_INIT(0);
  230. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  231. static int really_probe(struct device *dev, struct device_driver *drv)
  232. {
  233. int ret = 0;
  234. atomic_inc(&probe_count);
  235. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  236. drv->bus->name, __func__, drv->name, dev_name(dev));
  237. WARN_ON(!list_empty(&dev->devres_head));
  238. dev->driver = drv;
  239. if (driver_sysfs_add(dev)) {
  240. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  241. __func__, dev_name(dev));
  242. goto probe_failed;
  243. }
  244. if (dev->bus->probe) {
  245. ret = dev->bus->probe(dev);
  246. if (ret)
  247. goto probe_failed;
  248. } else if (drv->probe) {
  249. ret = drv->probe(dev);
  250. if (ret)
  251. goto probe_failed;
  252. }
  253. driver_bound(dev);
  254. ret = 1;
  255. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  256. drv->bus->name, __func__, dev_name(dev), drv->name);
  257. goto done;
  258. probe_failed:
  259. devres_release_all(dev);
  260. driver_sysfs_remove(dev);
  261. dev->driver = NULL;
  262. dev_set_drvdata(dev, NULL);
  263. if (ret == -EPROBE_DEFER) {
  264. /* Driver requested deferred probing */
  265. dev_info(dev, "Driver %s requests probe deferral\n", drv->name);
  266. driver_deferred_probe_add(dev);
  267. } else if (ret != -ENODEV && ret != -ENXIO) {
  268. /* driver matched but the probe failed */
  269. printk(KERN_WARNING
  270. "%s: probe of %s failed with error %d\n",
  271. drv->name, dev_name(dev), ret);
  272. } else {
  273. pr_debug("%s: probe of %s rejects match %d\n",
  274. drv->name, dev_name(dev), ret);
  275. }
  276. /*
  277. * Ignore errors returned by ->probe so that the next driver can try
  278. * its luck.
  279. */
  280. ret = 0;
  281. done:
  282. atomic_dec(&probe_count);
  283. wake_up(&probe_waitqueue);
  284. return ret;
  285. }
  286. /**
  287. * driver_probe_done
  288. * Determine if the probe sequence is finished or not.
  289. *
  290. * Should somehow figure out how to use a semaphore, not an atomic variable...
  291. */
  292. int driver_probe_done(void)
  293. {
  294. pr_debug("%s: probe_count = %d\n", __func__,
  295. atomic_read(&probe_count));
  296. if (atomic_read(&probe_count))
  297. return -EBUSY;
  298. return 0;
  299. }
  300. /**
  301. * wait_for_device_probe
  302. * Wait for device probing to be completed.
  303. */
  304. void wait_for_device_probe(void)
  305. {
  306. /* wait for the known devices to complete their probing */
  307. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  308. async_synchronize_full();
  309. }
  310. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  311. /**
  312. * driver_probe_device - attempt to bind device & driver together
  313. * @drv: driver to bind a device to
  314. * @dev: device to try to bind to the driver
  315. *
  316. * This function returns -ENODEV if the device is not registered,
  317. * 1 if the device is bound successfully and 0 otherwise.
  318. *
  319. * This function must be called with @dev lock held. When called for a
  320. * USB interface, @dev->parent lock must be held as well.
  321. */
  322. int driver_probe_device(struct device_driver *drv, struct device *dev)
  323. {
  324. int ret = 0;
  325. if (!device_is_registered(dev))
  326. return -ENODEV;
  327. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  328. drv->bus->name, __func__, dev_name(dev), drv->name);
  329. pm_runtime_barrier(dev);
  330. ret = really_probe(dev, drv);
  331. pm_runtime_idle(dev);
  332. return ret;
  333. }
  334. static int __device_attach(struct device_driver *drv, void *data)
  335. {
  336. struct device *dev = data;
  337. if (!driver_match_device(drv, dev))
  338. return 0;
  339. return driver_probe_device(drv, dev);
  340. }
  341. /**
  342. * device_attach - try to attach device to a driver.
  343. * @dev: device.
  344. *
  345. * Walk the list of drivers that the bus has and call
  346. * driver_probe_device() for each pair. If a compatible
  347. * pair is found, break out and return.
  348. *
  349. * Returns 1 if the device was bound to a driver;
  350. * 0 if no matching driver was found;
  351. * -ENODEV if the device is not registered.
  352. *
  353. * When called for a USB interface, @dev->parent lock must be held.
  354. */
  355. int device_attach(struct device *dev)
  356. {
  357. int ret = 0;
  358. device_lock(dev);
  359. if (dev->driver) {
  360. if (klist_node_attached(&dev->p->knode_driver)) {
  361. ret = 1;
  362. goto out_unlock;
  363. }
  364. ret = device_bind_driver(dev);
  365. if (ret == 0)
  366. ret = 1;
  367. else {
  368. dev->driver = NULL;
  369. ret = 0;
  370. }
  371. } else {
  372. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  373. pm_runtime_idle(dev);
  374. }
  375. out_unlock:
  376. device_unlock(dev);
  377. return ret;
  378. }
  379. EXPORT_SYMBOL_GPL(device_attach);
  380. static int __driver_attach(struct device *dev, void *data)
  381. {
  382. struct device_driver *drv = data;
  383. /*
  384. * Lock device and try to bind to it. We drop the error
  385. * here and always return 0, because we need to keep trying
  386. * to bind to devices and some drivers will return an error
  387. * simply if it didn't support the device.
  388. *
  389. * driver_probe_device() will spit a warning if there
  390. * is an error.
  391. */
  392. if (!driver_match_device(drv, dev))
  393. return 0;
  394. if (dev->parent) /* Needed for USB */
  395. device_lock(dev->parent);
  396. device_lock(dev);
  397. if (!dev->driver)
  398. driver_probe_device(drv, dev);
  399. device_unlock(dev);
  400. if (dev->parent)
  401. device_unlock(dev->parent);
  402. return 0;
  403. }
  404. /**
  405. * driver_attach - try to bind driver to devices.
  406. * @drv: driver.
  407. *
  408. * Walk the list of devices that the bus has on it and try to
  409. * match the driver with each one. If driver_probe_device()
  410. * returns 0 and the @dev->driver is set, we've found a
  411. * compatible pair.
  412. */
  413. int driver_attach(struct device_driver *drv)
  414. {
  415. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  416. }
  417. EXPORT_SYMBOL_GPL(driver_attach);
  418. /*
  419. * __device_release_driver() must be called with @dev lock held.
  420. * When called for a USB interface, @dev->parent lock must be held as well.
  421. */
  422. static void __device_release_driver(struct device *dev)
  423. {
  424. struct device_driver *drv;
  425. drv = dev->driver;
  426. if (drv) {
  427. pm_runtime_get_sync(dev);
  428. driver_sysfs_remove(dev);
  429. if (dev->bus)
  430. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  431. BUS_NOTIFY_UNBIND_DRIVER,
  432. dev);
  433. pm_runtime_put_sync(dev);
  434. if (dev->bus && dev->bus->remove)
  435. dev->bus->remove(dev);
  436. else if (drv->remove)
  437. drv->remove(dev);
  438. devres_release_all(dev);
  439. dev->driver = NULL;
  440. dev_set_drvdata(dev, NULL);
  441. klist_remove(&dev->p->knode_driver);
  442. if (dev->bus)
  443. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  444. BUS_NOTIFY_UNBOUND_DRIVER,
  445. dev);
  446. }
  447. }
  448. /**
  449. * device_release_driver - manually detach device from driver.
  450. * @dev: device.
  451. *
  452. * Manually detach device from driver.
  453. * When called for a USB interface, @dev->parent lock must be held.
  454. */
  455. void device_release_driver(struct device *dev)
  456. {
  457. /*
  458. * If anyone calls device_release_driver() recursively from
  459. * within their ->remove callback for the same device, they
  460. * will deadlock right here.
  461. */
  462. device_lock(dev);
  463. __device_release_driver(dev);
  464. device_unlock(dev);
  465. }
  466. EXPORT_SYMBOL_GPL(device_release_driver);
  467. /**
  468. * driver_detach - detach driver from all devices it controls.
  469. * @drv: driver.
  470. */
  471. void driver_detach(struct device_driver *drv)
  472. {
  473. struct device_private *dev_prv;
  474. struct device *dev;
  475. for (;;) {
  476. spin_lock(&drv->p->klist_devices.k_lock);
  477. if (list_empty(&drv->p->klist_devices.k_list)) {
  478. spin_unlock(&drv->p->klist_devices.k_lock);
  479. break;
  480. }
  481. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  482. struct device_private,
  483. knode_driver.n_node);
  484. dev = dev_prv->device;
  485. get_device(dev);
  486. spin_unlock(&drv->p->klist_devices.k_lock);
  487. if (dev->parent) /* Needed for USB */
  488. device_lock(dev->parent);
  489. device_lock(dev);
  490. if (dev->driver == drv)
  491. __device_release_driver(dev);
  492. device_unlock(dev);
  493. if (dev->parent)
  494. device_unlock(dev->parent);
  495. put_device(dev);
  496. }
  497. }
  498. /*
  499. * These exports can't be _GPL due to .h files using this within them, and it
  500. * might break something that was previously working...
  501. */
  502. void *dev_get_drvdata(const struct device *dev)
  503. {
  504. if (dev && dev->p)
  505. return dev->p->driver_data;
  506. return NULL;
  507. }
  508. EXPORT_SYMBOL(dev_get_drvdata);
  509. int dev_set_drvdata(struct device *dev, void *data)
  510. {
  511. int error;
  512. if (!dev->p) {
  513. error = device_private_init(dev);
  514. if (error)
  515. return error;
  516. }
  517. dev->p->driver_data = data;
  518. return 0;
  519. }
  520. EXPORT_SYMBOL(dev_set_drvdata);