dd.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 == -ENODEV && error == -ENXIO) {
  82. /* Driver matched, but didn't support device
  83. * or device not found.
  84. * Not an error; keep going.
  85. */
  86. error = 0;
  87. } else {
  88. /* driver matched but the probe failed */
  89. printk(KERN_WARNING
  90. "%s: probe of %s failed with error %d\n",
  91. drv->name, dev->bus_id, error);
  92. }
  93. return 0;
  94. }
  95. /**
  96. * device_attach - try to attach device to a driver.
  97. * @dev: device.
  98. *
  99. * Walk the list of drivers that the bus has and call
  100. * driver_probe_device() for each pair. If a compatible
  101. * pair is found, break out and return.
  102. */
  103. int device_attach(struct device * dev)
  104. {
  105. if (dev->driver) {
  106. device_bind_driver(dev);
  107. return 1;
  108. }
  109. return bus_for_each_drv(dev->bus, NULL, dev, __device_attach);
  110. }
  111. static int __driver_attach(struct device * dev, void * data)
  112. {
  113. struct device_driver * drv = data;
  114. int error = 0;
  115. if (!dev->driver) {
  116. error = driver_probe_device(drv, dev);
  117. if (error) {
  118. if (error != -ENODEV) {
  119. /* driver matched but the probe failed */
  120. printk(KERN_WARNING
  121. "%s: probe of %s failed with error %d\n",
  122. drv->name, dev->bus_id, error);
  123. } else
  124. error = 0;
  125. }
  126. }
  127. return 0;
  128. }
  129. /**
  130. * driver_attach - try to bind driver to devices.
  131. * @drv: driver.
  132. *
  133. * Walk the list of devices that the bus has on it and try to
  134. * match the driver with each one. If driver_probe_device()
  135. * returns 0 and the @dev->driver is set, we've found a
  136. * compatible pair.
  137. *
  138. * Note that we ignore the -ENODEV error from driver_probe_device(),
  139. * since it's perfectly valid for a driver not to bind to any devices.
  140. */
  141. void driver_attach(struct device_driver * drv)
  142. {
  143. bus_for_each_dev(drv->bus, NULL, drv, __driver_attach);
  144. }
  145. /**
  146. * device_release_driver - manually detach device from driver.
  147. * @dev: device.
  148. *
  149. * Manually detach device from driver.
  150. * Note that this is called without incrementing the bus
  151. * reference count nor taking the bus's rwsem. Be sure that
  152. * those are accounted for before calling this function.
  153. */
  154. void device_release_driver(struct device * dev)
  155. {
  156. struct device_driver * drv = dev->driver;
  157. if (!drv)
  158. return;
  159. sysfs_remove_link(&drv->kobj, kobject_name(&dev->kobj));
  160. sysfs_remove_link(&dev->kobj, "driver");
  161. klist_remove(&dev->knode_driver);
  162. down(&dev->sem);
  163. if (drv->remove)
  164. drv->remove(dev);
  165. dev->driver = NULL;
  166. up(&dev->sem);
  167. }
  168. static int __remove_driver(struct device * dev, void * unused)
  169. {
  170. device_release_driver(dev);
  171. return 0;
  172. }
  173. /**
  174. * driver_detach - detach driver from all devices it controls.
  175. * @drv: driver.
  176. */
  177. void driver_detach(struct device_driver * drv)
  178. {
  179. driver_for_each_device(drv, NULL, NULL, __remove_driver);
  180. }
  181. EXPORT_SYMBOL_GPL(driver_probe_device);
  182. EXPORT_SYMBOL_GPL(device_bind_driver);
  183. EXPORT_SYMBOL_GPL(device_release_driver);
  184. EXPORT_SYMBOL_GPL(device_attach);
  185. EXPORT_SYMBOL_GPL(driver_attach);