dd.c 15 KB

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