dd.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. static void driver_bound(struct device *dev)
  29. {
  30. if (klist_node_attached(&dev->p->knode_driver)) {
  31. printk(KERN_WARNING "%s: device %s already bound\n",
  32. __func__, kobject_name(&dev->kobj));
  33. return;
  34. }
  35. pr_debug("driver: '%s': %s: bound to device '%s'\n", dev_name(dev),
  36. __func__, dev->driver->name);
  37. if (dev->bus)
  38. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  39. BUS_NOTIFY_BOUND_DRIVER, dev);
  40. klist_add_tail(&dev->p->knode_driver, &dev->driver->p->klist_devices);
  41. }
  42. static int driver_sysfs_add(struct device *dev)
  43. {
  44. int ret;
  45. ret = sysfs_create_link(&dev->driver->p->kobj, &dev->kobj,
  46. kobject_name(&dev->kobj));
  47. if (ret == 0) {
  48. ret = sysfs_create_link(&dev->kobj, &dev->driver->p->kobj,
  49. "driver");
  50. if (ret)
  51. sysfs_remove_link(&dev->driver->p->kobj,
  52. kobject_name(&dev->kobj));
  53. }
  54. return ret;
  55. }
  56. static void driver_sysfs_remove(struct device *dev)
  57. {
  58. struct device_driver *drv = dev->driver;
  59. if (drv) {
  60. sysfs_remove_link(&drv->p->kobj, kobject_name(&dev->kobj));
  61. sysfs_remove_link(&dev->kobj, "driver");
  62. }
  63. }
  64. /**
  65. * device_bind_driver - bind a driver to one device.
  66. * @dev: device.
  67. *
  68. * Allow manual attachment of a driver to a device.
  69. * Caller must have already set @dev->driver.
  70. *
  71. * Note that this does not modify the bus reference count
  72. * nor take the bus's rwsem. Please verify those are accounted
  73. * for before calling this. (It is ok to call with no other effort
  74. * from a driver's probe() method.)
  75. *
  76. * This function must be called with @dev->sem held.
  77. */
  78. int device_bind_driver(struct device *dev)
  79. {
  80. int ret;
  81. ret = driver_sysfs_add(dev);
  82. if (!ret)
  83. driver_bound(dev);
  84. return ret;
  85. }
  86. EXPORT_SYMBOL_GPL(device_bind_driver);
  87. static atomic_t probe_count = ATOMIC_INIT(0);
  88. static DECLARE_WAIT_QUEUE_HEAD(probe_waitqueue);
  89. static int really_probe(struct device *dev, struct device_driver *drv)
  90. {
  91. int ret = 0;
  92. atomic_inc(&probe_count);
  93. pr_debug("bus: '%s': %s: probing driver %s with device %s\n",
  94. drv->bus->name, __func__, drv->name, dev_name(dev));
  95. WARN_ON(!list_empty(&dev->devres_head));
  96. dev->driver = drv;
  97. if (driver_sysfs_add(dev)) {
  98. printk(KERN_ERR "%s: driver_sysfs_add(%s) failed\n",
  99. __func__, dev_name(dev));
  100. goto probe_failed;
  101. }
  102. if (dev->bus->probe) {
  103. ret = dev->bus->probe(dev);
  104. if (ret)
  105. goto probe_failed;
  106. } else if (drv->probe) {
  107. ret = drv->probe(dev);
  108. if (ret)
  109. goto probe_failed;
  110. }
  111. driver_bound(dev);
  112. ret = 1;
  113. pr_debug("bus: '%s': %s: bound device %s to driver %s\n",
  114. drv->bus->name, __func__, dev_name(dev), drv->name);
  115. goto done;
  116. probe_failed:
  117. devres_release_all(dev);
  118. driver_sysfs_remove(dev);
  119. dev->driver = NULL;
  120. if (ret != -ENODEV && ret != -ENXIO) {
  121. /* driver matched but the probe failed */
  122. printk(KERN_WARNING
  123. "%s: probe of %s failed with error %d\n",
  124. drv->name, dev_name(dev), ret);
  125. }
  126. /*
  127. * Ignore errors returned by ->probe so that the next driver can try
  128. * its luck.
  129. */
  130. ret = 0;
  131. done:
  132. atomic_dec(&probe_count);
  133. wake_up(&probe_waitqueue);
  134. return ret;
  135. }
  136. /**
  137. * driver_probe_done
  138. * Determine if the probe sequence is finished or not.
  139. *
  140. * Should somehow figure out how to use a semaphore, not an atomic variable...
  141. */
  142. int driver_probe_done(void)
  143. {
  144. pr_debug("%s: probe_count = %d\n", __func__,
  145. atomic_read(&probe_count));
  146. if (atomic_read(&probe_count))
  147. return -EBUSY;
  148. return 0;
  149. }
  150. /**
  151. * wait_for_device_probe
  152. * Wait for device probing to be completed.
  153. */
  154. void wait_for_device_probe(void)
  155. {
  156. /* wait for the known devices to complete their probing */
  157. wait_event(probe_waitqueue, atomic_read(&probe_count) == 0);
  158. async_synchronize_full();
  159. }
  160. EXPORT_SYMBOL_GPL(wait_for_device_probe);
  161. /**
  162. * driver_probe_device - attempt to bind device & driver together
  163. * @drv: driver to bind a device to
  164. * @dev: device to try to bind to the driver
  165. *
  166. * This function returns -ENODEV if the device is not registered,
  167. * 1 if the device is bound successfully and 0 otherwise.
  168. *
  169. * This function must be called with @dev->sem held. When called for a
  170. * USB interface, @dev->parent->sem must be held as well.
  171. */
  172. int driver_probe_device(struct device_driver *drv, struct device *dev)
  173. {
  174. int ret = 0;
  175. if (!device_is_registered(dev))
  176. return -ENODEV;
  177. pr_debug("bus: '%s': %s: matched device %s with driver %s\n",
  178. drv->bus->name, __func__, dev_name(dev), drv->name);
  179. pm_runtime_get_noresume(dev);
  180. pm_runtime_barrier(dev);
  181. ret = really_probe(dev, drv);
  182. pm_runtime_put_sync(dev);
  183. return ret;
  184. }
  185. static int __device_attach(struct device_driver *drv, void *data)
  186. {
  187. struct device *dev = data;
  188. if (!driver_match_device(drv, dev))
  189. return 0;
  190. return driver_probe_device(drv, dev);
  191. }
  192. /**
  193. * device_attach - try to attach device to a driver.
  194. * @dev: device.
  195. *
  196. * Walk the list of drivers that the bus has and call
  197. * driver_probe_device() for each pair. If a compatible
  198. * pair is found, break out and return.
  199. *
  200. * Returns 1 if the device was bound to a driver;
  201. * 0 if no matching driver was found;
  202. * -ENODEV if the device is not registered.
  203. *
  204. * When called for a USB interface, @dev->parent->sem must be held.
  205. */
  206. int device_attach(struct device *dev)
  207. {
  208. int ret = 0;
  209. down(&dev->sem);
  210. if (dev->driver) {
  211. ret = device_bind_driver(dev);
  212. if (ret == 0)
  213. ret = 1;
  214. else {
  215. dev->driver = NULL;
  216. ret = 0;
  217. }
  218. } else {
  219. pm_runtime_get_noresume(dev);
  220. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  221. pm_runtime_put_sync(dev);
  222. }
  223. up(&dev->sem);
  224. return ret;
  225. }
  226. EXPORT_SYMBOL_GPL(device_attach);
  227. static int __driver_attach(struct device *dev, void *data)
  228. {
  229. struct device_driver *drv = data;
  230. /*
  231. * Lock device and try to bind to it. We drop the error
  232. * here and always return 0, because we need to keep trying
  233. * to bind to devices and some drivers will return an error
  234. * simply if it didn't support the device.
  235. *
  236. * driver_probe_device() will spit a warning if there
  237. * is an error.
  238. */
  239. if (!driver_match_device(drv, dev))
  240. return 0;
  241. if (dev->parent) /* Needed for USB */
  242. down(&dev->parent->sem);
  243. down(&dev->sem);
  244. if (!dev->driver)
  245. driver_probe_device(drv, dev);
  246. up(&dev->sem);
  247. if (dev->parent)
  248. up(&dev->parent->sem);
  249. return 0;
  250. }
  251. /**
  252. * driver_attach - try to bind driver to devices.
  253. * @drv: driver.
  254. *
  255. * Walk the list of devices that the bus has on it and try to
  256. * match the driver with each one. If driver_probe_device()
  257. * returns 0 and the @dev->driver is set, we've found a
  258. * compatible pair.
  259. */
  260. int driver_attach(struct device_driver *drv)
  261. {
  262. return bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  263. }
  264. EXPORT_SYMBOL_GPL(driver_attach);
  265. /*
  266. * __device_release_driver() must be called with @dev->sem held.
  267. * When called for a USB interface, @dev->parent->sem must be held as well.
  268. */
  269. static void __device_release_driver(struct device *dev)
  270. {
  271. struct device_driver *drv;
  272. drv = dev->driver;
  273. if (drv) {
  274. pm_runtime_get_noresume(dev);
  275. pm_runtime_barrier(dev);
  276. driver_sysfs_remove(dev);
  277. if (dev->bus)
  278. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  279. BUS_NOTIFY_UNBIND_DRIVER,
  280. dev);
  281. if (dev->bus && dev->bus->remove)
  282. dev->bus->remove(dev);
  283. else if (drv->remove)
  284. drv->remove(dev);
  285. devres_release_all(dev);
  286. dev->driver = NULL;
  287. klist_remove(&dev->p->knode_driver);
  288. if (dev->bus)
  289. blocking_notifier_call_chain(&dev->bus->p->bus_notifier,
  290. BUS_NOTIFY_UNBOUND_DRIVER,
  291. dev);
  292. pm_runtime_put_sync(dev);
  293. }
  294. }
  295. /**
  296. * device_release_driver - manually detach device from driver.
  297. * @dev: device.
  298. *
  299. * Manually detach device from driver.
  300. * When called for a USB interface, @dev->parent->sem must be held.
  301. */
  302. void device_release_driver(struct device *dev)
  303. {
  304. /*
  305. * If anyone calls device_release_driver() recursively from
  306. * within their ->remove callback for the same device, they
  307. * will deadlock right here.
  308. */
  309. down(&dev->sem);
  310. __device_release_driver(dev);
  311. up(&dev->sem);
  312. }
  313. EXPORT_SYMBOL_GPL(device_release_driver);
  314. /**
  315. * driver_detach - detach driver from all devices it controls.
  316. * @drv: driver.
  317. */
  318. void driver_detach(struct device_driver *drv)
  319. {
  320. struct device_private *dev_prv;
  321. struct device *dev;
  322. for (;;) {
  323. spin_lock(&drv->p->klist_devices.k_lock);
  324. if (list_empty(&drv->p->klist_devices.k_list)) {
  325. spin_unlock(&drv->p->klist_devices.k_lock);
  326. break;
  327. }
  328. dev_prv = list_entry(drv->p->klist_devices.k_list.prev,
  329. struct device_private,
  330. knode_driver.n_node);
  331. dev = dev_prv->device;
  332. get_device(dev);
  333. spin_unlock(&drv->p->klist_devices.k_lock);
  334. if (dev->parent) /* Needed for USB */
  335. down(&dev->parent->sem);
  336. down(&dev->sem);
  337. if (dev->driver == drv)
  338. __device_release_driver(dev);
  339. up(&dev->sem);
  340. if (dev->parent)
  341. up(&dev->parent->sem);
  342. put_device(dev);
  343. }
  344. }
  345. /*
  346. * These exports can't be _GPL due to .h files using this within them, and it
  347. * might break something that was previously working...
  348. */
  349. void *dev_get_drvdata(const struct device *dev)
  350. {
  351. if (dev && dev->p)
  352. return dev->p->driver_data;
  353. return NULL;
  354. }
  355. EXPORT_SYMBOL(dev_get_drvdata);
  356. void dev_set_drvdata(struct device *dev, void *data)
  357. {
  358. int error;
  359. if (!dev)
  360. return;
  361. if (!dev->p) {
  362. error = device_private_init(dev);
  363. if (error)
  364. return;
  365. }
  366. dev->p->driver_data = data;
  367. }
  368. EXPORT_SYMBOL(dev_set_drvdata);