dd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. void device_bind_driver(struct device * dev)
  35. {
  36. pr_debug("bound device '%s' to driver '%s'\n",
  37. dev->bus_id, dev->driver->name);
  38. klist_add_tail(&dev->driver->klist_devices, &dev->knode_driver);
  39. sysfs_create_link(&dev->driver->kobj, &dev->kobj,
  40. kobject_name(&dev->kobj));
  41. sysfs_create_link(&dev->kobj, &dev->driver->kobj, "driver");
  42. }
  43. /**
  44. * driver_probe_device - attempt to bind device & driver.
  45. * @drv: driver.
  46. * @dev: device.
  47. *
  48. * First, we call the bus's match function, if one present, which
  49. * should compare the device IDs the driver supports with the
  50. * device IDs of the device. Note we don't do this ourselves
  51. * because we don't know the format of the ID structures, nor what
  52. * is to be considered a match and what is not.
  53. *
  54. * If we find a match, we call @drv->probe(@dev) if it exists, and
  55. * call device_bind_driver() above.
  56. */
  57. int driver_probe_device(struct device_driver * drv, struct device * dev)
  58. {
  59. int error = 0;
  60. if (drv->bus->match && !drv->bus->match(dev, drv))
  61. return -ENODEV;
  62. down(&dev->sem);
  63. dev->driver = drv;
  64. if (drv->probe) {
  65. error = drv->probe(dev);
  66. if (error) {
  67. dev->driver = NULL;
  68. up(&dev->sem);
  69. return error;
  70. }
  71. }
  72. up(&dev->sem);
  73. device_bind_driver(dev);
  74. return 0;
  75. }
  76. static int __device_attach(struct device_driver * drv, void * data)
  77. {
  78. struct device * dev = data;
  79. int error;
  80. error = driver_probe_device(drv, dev);
  81. if (error) {
  82. if ((error == -ENODEV) || (error == -ENXIO)) {
  83. /* Driver matched, but didn't support device
  84. * or device not found.
  85. * Not an error; keep going.
  86. */
  87. error = 0;
  88. } else {
  89. /* driver matched but the probe failed */
  90. printk(KERN_WARNING
  91. "%s: probe of %s failed with error %d\n",
  92. drv->name, dev->bus_id, error);
  93. }
  94. return error;
  95. }
  96. /* stop looking, this device is attached */
  97. return 1;
  98. }
  99. /**
  100. * device_attach - try to attach device to a driver.
  101. * @dev: device.
  102. *
  103. * Walk the list of drivers that the bus has and call
  104. * driver_probe_device() for each pair. If a compatible
  105. * pair is found, break out and return.
  106. */
  107. int device_attach(struct device * dev)
  108. {
  109. if (dev->driver) {
  110. device_bind_driver(dev);
  111. return 1;
  112. }
  113. return bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  114. }
  115. static int __driver_attach(struct device * dev, void * data)
  116. {
  117. struct device_driver * drv = data;
  118. int error = 0;
  119. if (!dev->driver) {
  120. error = driver_probe_device(drv, dev);
  121. if (error) {
  122. if (error != -ENODEV) {
  123. /* driver matched but the probe failed */
  124. printk(KERN_WARNING
  125. "%s: probe of %s failed with error %d\n",
  126. drv->name, dev->bus_id, error);
  127. } else
  128. error = 0;
  129. return error;
  130. }
  131. /* stop looking, this driver is attached */
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. /**
  137. * driver_attach - try to bind driver to devices.
  138. * @drv: driver.
  139. *
  140. * Walk the list of devices that the bus has on it and try to
  141. * match the driver with each one. If driver_probe_device()
  142. * returns 0 and the @dev->driver is set, we've found a
  143. * compatible pair.
  144. *
  145. * Note that we ignore the -ENODEV error from driver_probe_device(),
  146. * since it's perfectly valid for a driver not to bind to any devices.
  147. */
  148. void driver_attach(struct device_driver * drv)
  149. {
  150. bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  151. }
  152. /**
  153. * device_release_driver - manually detach device from driver.
  154. * @dev: device.
  155. *
  156. * Manually detach device from driver.
  157. * Note that this is called without incrementing the bus
  158. * reference count nor taking the bus's rwsem. Be sure that
  159. * those are accounted for before calling this function.
  160. */
  161. void device_release_driver(struct device * dev)
  162. {
  163. struct device_driver * drv = dev->driver;
  164. if (!drv)
  165. return;
  166. sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
  167. sysfs_remove_link(&dev->kobj, "driver");
  168. klist_del(&dev->knode_driver);
  169. down(&dev->sem);
  170. if (drv->remove)
  171. drv->remove(dev);
  172. dev->driver = NULL;
  173. up(&dev->sem);
  174. }
  175. static int __remove_driver(struct device * dev, void * unused)
  176. {
  177. device_release_driver(dev);
  178. return 0;
  179. }
  180. /**
  181. * driver_detach - detach driver from all devices it controls.
  182. * @drv: driver.
  183. */
  184. void driver_detach(struct device_driver * drv)
  185. {
  186. driver_for_each_device(drv, NULL, NULL, __remove_driver);
  187. }
  188. EXPORT_SYMBOL_GPL(driver_probe_device);
  189. EXPORT_SYMBOL_GPL(device_bind_driver);
  190. EXPORT_SYMBOL_GPL(device_release_driver);
  191. EXPORT_SYMBOL_GPL(device_attach);
  192. EXPORT_SYMBOL_GPL(driver_attach);