dd.c 5.2 KB

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