dd.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. pr_debug("bound device '%s' to driver '%s'\n",
  39. dev->bus_id, dev->driver->name);
  40. klist_add_tail(&dev->driver->klist_devices, &dev->knode_driver);
  41. sysfs_create_link(&dev->driver->kobj, &dev->kobj,
  42. kobject_name(&dev->kobj));
  43. sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
  44. }
  45. /**
  46. * driver_probe_device - attempt to bind device & driver.
  47. * @drv: driver.
  48. * @dev: device.
  49. *
  50. * First, we call the bus's match function, if one present, which
  51. * should compare the device IDs the driver supports with the
  52. * device IDs of the device. Note we don't do this ourselves
  53. * because we don't know the format of the ID structures, nor what
  54. * is to be considered a match and what is not.
  55. *
  56. *
  57. * This function returns 1 if a match is found, an error if one
  58. * occurs (that is not -ENODEV or -ENXIO), and 0 otherwise.
  59. *
  60. * This function must be called with @dev->sem held.
  61. */
  62. int driver_probe_device(struct device_driver * drv, struct device * dev)
  63. {
  64. int ret = 0;
  65. if (drv->bus->match && !drv->bus->match(dev, drv))
  66. goto Done;
  67. pr_debug("%s: Matched Device %s with Driver %s\n",
  68. drv->bus->name, dev->bus_id, drv->name);
  69. dev->driver = drv;
  70. if (drv->probe) {
  71. ret = drv->probe(dev);
  72. if (ret) {
  73. dev->driver = NULL;
  74. goto ProbeFailed;
  75. }
  76. }
  77. device_bind_driver(dev);
  78. ret = 1;
  79. pr_debug("%s: Bound Device %s to Driver %s\n",
  80. drv->bus->name, dev->bus_id, drv->name);
  81. goto Done;
  82. ProbeFailed:
  83. if (ret == -ENODEV || ret == -ENXIO) {
  84. /* Driver matched, but didn't support device
  85. * or device not found.
  86. * Not an error; keep going.
  87. */
  88. ret = 0;
  89. } else {
  90. /* driver matched but the probe failed */
  91. printk(KERN_WARNING
  92. "%s: probe of %s failed with error %d\n",
  93. drv->name, dev->bus_id, ret);
  94. }
  95. Done:
  96. return ret;
  97. }
  98. static int __device_attach(struct device_driver * drv, void * data)
  99. {
  100. struct device * dev = data;
  101. return driver_probe_device(drv, dev);
  102. }
  103. /**
  104. * device_attach - try to attach device to a driver.
  105. * @dev: device.
  106. *
  107. * Walk the list of drivers that the bus has and call
  108. * driver_probe_device() for each pair. If a compatible
  109. * pair is found, break out and return.
  110. *
  111. * Returns 1 if the device was bound to a driver;
  112. * 0 if no matching device was found; error code otherwise.
  113. */
  114. int device_attach(struct device * dev)
  115. {
  116. int ret = 0;
  117. down(&dev->sem);
  118. if (dev->driver) {
  119. device_bind_driver(dev);
  120. ret = 1;
  121. } else
  122. ret = bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  123. up(&dev->sem);
  124. return ret;
  125. }
  126. static int __driver_attach(struct device * dev, void * data)
  127. {
  128. struct device_driver * drv = data;
  129. /*
  130. * Lock device and try to bind to it. We drop the error
  131. * here and always return 0, because we need to keep trying
  132. * to bind to devices and some drivers will return an error
  133. * simply if it didn't support the device.
  134. *
  135. * driver_probe_device() will spit a warning if there
  136. * is an error.
  137. */
  138. down(&dev->sem);
  139. if (!dev->driver)
  140. driver_probe_device(drv, dev);
  141. up(&dev->sem);
  142. return 0;
  143. }
  144. /**
  145. * driver_attach - try to bind driver to devices.
  146. * @drv: driver.
  147. *
  148. * Walk the list of devices that the bus has on it and try to
  149. * match the driver with each one. If driver_probe_device()
  150. * returns 0 and the @dev->driver is set, we've found a
  151. * compatible pair.
  152. */
  153. void driver_attach(struct device_driver * drv)
  154. {
  155. bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  156. }
  157. /**
  158. * device_release_driver - manually detach device from driver.
  159. * @dev: device.
  160. *
  161. * Manually detach device from driver.
  162. *
  163. * __device_release_driver() must be called with @dev->sem held.
  164. */
  165. static void __device_release_driver(struct device * dev)
  166. {
  167. struct device_driver * drv;
  168. drv = dev->driver;
  169. if (drv) {
  170. get_driver(drv);
  171. sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
  172. sysfs_remove_link(&dev->kobj, "driver");
  173. klist_remove(&dev->knode_driver);
  174. if (drv->remove)
  175. drv->remove(dev);
  176. dev->driver = NULL;
  177. put_driver(drv);
  178. }
  179. }
  180. void device_release_driver(struct device * dev)
  181. {
  182. /*
  183. * If anyone calls device_release_driver() recursively from
  184. * within their ->remove callback for the same device, they
  185. * will deadlock right here.
  186. */
  187. down(&dev->sem);
  188. __device_release_driver(dev);
  189. up(&dev->sem);
  190. }
  191. /**
  192. * driver_detach - detach driver from all devices it controls.
  193. * @drv: driver.
  194. */
  195. void driver_detach(struct device_driver * drv)
  196. {
  197. struct device * dev;
  198. for (;;) {
  199. spin_lock_irq(&drv->klist_devices.k_lock);
  200. if (list_empty(&drv->klist_devices.k_list)) {
  201. spin_unlock_irq(&drv->klist_devices.k_lock);
  202. break;
  203. }
  204. dev = list_entry(drv->klist_devices.k_list.prev,
  205. struct device, knode_driver.n_node);
  206. get_device(dev);
  207. spin_unlock_irq(&drv->klist_devices.k_lock);
  208. down(&dev->sem);
  209. if (dev->driver == drv)
  210. __device_release_driver(dev);
  211. up(&dev->sem);
  212. put_device(dev);
  213. }
  214. }
  215. EXPORT_SYMBOL_GPL(device_bind_driver);
  216. EXPORT_SYMBOL_GPL(device_release_driver);
  217. EXPORT_SYMBOL_GPL(device_attach);
  218. EXPORT_SYMBOL_GPL(driver_attach);