dd.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. *
  15. * This file is released under the GPLv2
  16. */
  17. #include <linux/device.h>
  18. #include <linux/module.h>
  19. #include "base.h"
  20. #include "power/power.h"
  21. #define to_drv(node) container_of(node, struct device_driver, kobj.entry)
  22. /**
  23. * device_bind_driver - bind a driver to one device.
  24. * @dev: device.
  25. *
  26. * Allow manual attachment of a driver to a device.
  27. * Caller must have already set @dev->driver.
  28. *
  29. * Note that this does not modify the bus reference count
  30. * nor take the bus's rwsem. Please verify those are accounted
  31. * for before calling this. (It is ok to call with no other effort
  32. * from a driver's probe() method.)
  33. *
  34. * This function must be called with @dev->sem held.
  35. */
  36. void device_bind_driver(struct device * dev)
  37. {
  38. if (klist_node_attached(&dev->knode_driver))
  39. return;
  40. pr_debug("bound device '%s' to driver '%s'\n",
  41. dev->bus_id, dev->driver->name);
  42. klist_add_tail(&dev->knode_driver, &dev->driver->klist_devices);
  43. sysfs_create_link(&dev->driver->kobj, &dev->kobj,
  44. kobject_name(&dev->kobj));
  45. sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
  46. }
  47. /**
  48. * driver_probe_device - attempt to bind device & driver.
  49. * @drv: driver.
  50. * @dev: device.
  51. *
  52. * First, we call the bus's match function, if one present, which
  53. * should compare the device IDs the driver supports with the
  54. * device IDs of the device. Note we don't do this ourselves
  55. * because we don't know the format of the ID structures, nor what
  56. * is to be considered a match and what is not.
  57. *
  58. * This function returns 1 if a match is found, an error if one
  59. * occurs (that is not -ENODEV or -ENXIO), and 0 otherwise.
  60. *
  61. * This function must be called with @dev->sem held. When called
  62. * for a USB interface, @dev->parent->sem must be held as well.
  63. */
  64. int driver_probe_device(struct device_driver * drv, struct device * dev)
  65. {
  66. int ret = 0;
  67. if (drv->bus->match && !drv->bus->match(dev, drv))
  68. goto Done;
  69. pr_debug("%s: Matched Device %s with Driver %s\n",
  70. drv->bus->name, dev->bus_id, drv->name);
  71. dev->driver = drv;
  72. if (dev->bus->probe) {
  73. ret = dev->bus->probe(dev);
  74. if (ret) {
  75. dev->driver = NULL;
  76. goto ProbeFailed;
  77. }
  78. } else if (drv->probe) {
  79. ret = drv->probe(dev);
  80. if (ret) {
  81. dev->driver = NULL;
  82. goto ProbeFailed;
  83. }
  84. }
  85. device_bind_driver(dev);
  86. ret = 1;
  87. pr_debug("%s: Bound Device %s to Driver %s\n",
  88. drv->bus->name, dev->bus_id, drv->name);
  89. goto Done;
  90. ProbeFailed:
  91. if (ret == -ENODEV || ret == -ENXIO) {
  92. /* Driver matched, but didn't support device
  93. * or device not found.
  94. * Not an error; keep going.
  95. */
  96. ret = 0;
  97. } else {
  98. /* driver matched but the probe failed */
  99. printk(KERN_WARNING
  100. "%s: probe of %s failed with error %d\n",
  101. drv->name, dev->bus_id, ret);
  102. }
  103. Done:
  104. return ret;
  105. }
  106. static int __device_attach(struct device_driver * drv, void * data)
  107. {
  108. struct device * dev = data;
  109. return driver_probe_device(drv, dev);
  110. }
  111. /**
  112. * device_attach - try to attach device to a driver.
  113. * @dev: device.
  114. *
  115. * Walk the list of drivers that the bus has and call
  116. * driver_probe_device() for each pair. If a compatible
  117. * pair is found, break out and return.
  118. *
  119. * Returns 1 if the device was bound to a driver;
  120. * 0 if no matching device was found; error code otherwise.
  121. *
  122. * When called for a USB interface, @dev->parent->sem must be held.
  123. */
  124. int device_attach(struct device * dev)
  125. {
  126. int ret = 0;
  127. down(&dev->sem);
  128. if (dev->driver) {
  129. device_bind_driver(dev);
  130. ret = 1;
  131. } else
  132. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  133. up(&dev->sem);
  134. return ret;
  135. }
  136. static int __driver_attach(struct device * dev, void * data)
  137. {
  138. struct device_driver * drv = data;
  139. /*
  140. * Lock device and try to bind to it. We drop the error
  141. * here and always return 0, because we need to keep trying
  142. * to bind to devices and some drivers will return an error
  143. * simply if it didn't support the device.
  144. *
  145. * driver_probe_device() will spit a warning if there
  146. * is an error.
  147. */
  148. if (dev->parent) /* Needed for USB */
  149. down(&dev->parent->sem);
  150. down(&dev->sem);
  151. if (!dev->driver)
  152. driver_probe_device(drv, dev);
  153. up(&dev->sem);
  154. if (dev->parent)
  155. up(&dev->parent->sem);
  156. return 0;
  157. }
  158. /**
  159. * driver_attach - try to bind driver to devices.
  160. * @drv: driver.
  161. *
  162. * Walk the list of devices that the bus has on it and try to
  163. * match the driver with each one. If driver_probe_device()
  164. * returns 0 and the @dev->driver is set, we've found a
  165. * compatible pair.
  166. */
  167. void driver_attach(struct device_driver * drv)
  168. {
  169. bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  170. }
  171. /**
  172. * device_release_driver - manually detach device from driver.
  173. * @dev: device.
  174. *
  175. * Manually detach device from driver.
  176. *
  177. * __device_release_driver() must be called with @dev->sem held.
  178. * When called for a USB interface, @dev->parent->sem must be held
  179. * as well.
  180. */
  181. static void __device_release_driver(struct device * dev)
  182. {
  183. struct device_driver * drv;
  184. drv = dev->driver;
  185. if (drv) {
  186. get_driver(drv);
  187. sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
  188. sysfs_remove_link(&dev->kobj, "driver");
  189. klist_remove(&dev->knode_driver);
  190. if (dev->bus->remove)
  191. dev->bus->remove(dev);
  192. else if (drv->remove)
  193. drv->remove(dev);
  194. dev->driver = NULL;
  195. put_driver(drv);
  196. }
  197. }
  198. void device_release_driver(struct device * dev)
  199. {
  200. /*
  201. * If anyone calls device_release_driver() recursively from
  202. * within their ->remove callback for the same device, they
  203. * will deadlock right here.
  204. */
  205. down(&dev->sem);
  206. __device_release_driver(dev);
  207. up(&dev->sem);
  208. }
  209. /**
  210. * driver_detach - detach driver from all devices it controls.
  211. * @drv: driver.
  212. */
  213. void driver_detach(struct device_driver * drv)
  214. {
  215. struct device * dev;
  216. for (;;) {
  217. spin_lock(&drv->klist_devices.k_lock);
  218. if (list_empty(&drv->klist_devices.k_list)) {
  219. spin_unlock(&drv->klist_devices.k_lock);
  220. break;
  221. }
  222. dev = list_entry(drv->klist_devices.k_list.prev,
  223. struct device, knode_driver.n_node);
  224. get_device(dev);
  225. spin_unlock(&drv->klist_devices.k_lock);
  226. if (dev->parent) /* Needed for USB */
  227. down(&dev->parent->sem);
  228. down(&dev->sem);
  229. if (dev->driver == drv)
  230. __device_release_driver(dev);
  231. up(&dev->sem);
  232. if (dev->parent)
  233. up(&dev->parent->sem);
  234. put_device(dev);
  235. }
  236. }
  237. EXPORT_SYMBOL_GPL(device_bind_driver);
  238. EXPORT_SYMBOL_GPL(device_release_driver);
  239. EXPORT_SYMBOL_GPL(device_attach);
  240. EXPORT_SYMBOL_GPL(driver_attach);