driver.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * driver.c - centralized device driver management
  3. *
  4. * Copyright (c) 2002-3 Patrick Mochel
  5. * Copyright (c) 2002-3 Open Source Development Labs
  6. *
  7. * This file is released under the GPLv2
  8. *
  9. */
  10. #include <linux/config.h>
  11. #include <linux/device.h>
  12. #include <linux/module.h>
  13. #include <linux/errno.h>
  14. #include <linux/string.h>
  15. #include "base.h"
  16. #define to_dev(node) container_of(node, struct device, driver_list)
  17. #define to_drv(obj) container_of(obj, struct device_driver, kobj)
  18. static struct device * next_device(struct klist_iter * i)
  19. {
  20. struct klist_node * n = klist_next(i);
  21. return n ? container_of(n, struct device, knode_driver) : NULL;
  22. }
  23. /**
  24. * driver_for_each_device - Iterator for devices bound to a driver.
  25. * @drv: Driver we're iterating.
  26. * @start: Device to begin with
  27. * @data: Data to pass to the callback.
  28. * @fn: Function to call for each device.
  29. *
  30. * Iterate over the @drv's list of devices calling @fn for each one.
  31. */
  32. int driver_for_each_device(struct device_driver * drv, struct device * start,
  33. void * data, int (*fn)(struct device *, void *))
  34. {
  35. struct klist_iter i;
  36. struct device * dev;
  37. int error = 0;
  38. if (!drv)
  39. return -EINVAL;
  40. klist_iter_init_node(&drv->klist_devices, &i,
  41. start ? &start->knode_driver : NULL);
  42. while ((dev = next_device(&i)) && !error)
  43. error = fn(dev, data);
  44. klist_iter_exit(&i);
  45. return error;
  46. }
  47. EXPORT_SYMBOL_GPL(driver_for_each_device);
  48. /**
  49. * driver_find_device - device iterator for locating a particular device.
  50. * @drv: The device's driver
  51. * @start: Device to begin with
  52. * @data: Data to pass to match function
  53. * @match: Callback function to check device
  54. *
  55. * This is similar to the driver_for_each_device() function above, but
  56. * it returns a reference to a device that is 'found' for later use, as
  57. * determined by the @match callback.
  58. *
  59. * The callback should return 0 if the device doesn't match and non-zero
  60. * if it does. If the callback returns non-zero, this function will
  61. * return to the caller and not iterate over any more devices.
  62. */
  63. struct device * driver_find_device(struct device_driver *drv,
  64. struct device * start, void * data,
  65. int (*match)(struct device *, void *))
  66. {
  67. struct klist_iter i;
  68. struct device *dev;
  69. if (!drv)
  70. return NULL;
  71. klist_iter_init_node(&drv->klist_devices, &i,
  72. (start ? &start->knode_driver : NULL));
  73. while ((dev = next_device(&i)))
  74. if (match(dev, data) && get_device(dev))
  75. break;
  76. klist_iter_exit(&i);
  77. return dev;
  78. }
  79. EXPORT_SYMBOL_GPL(driver_find_device);
  80. /**
  81. * driver_create_file - create sysfs file for driver.
  82. * @drv: driver.
  83. * @attr: driver attribute descriptor.
  84. */
  85. int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
  86. {
  87. int error;
  88. if (get_driver(drv)) {
  89. error = sysfs_create_file(&drv->kobj, &attr->attr);
  90. put_driver(drv);
  91. } else
  92. error = -EINVAL;
  93. return error;
  94. }
  95. /**
  96. * driver_remove_file - remove sysfs file for driver.
  97. * @drv: driver.
  98. * @attr: driver attribute descriptor.
  99. */
  100. void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
  101. {
  102. if (get_driver(drv)) {
  103. sysfs_remove_file(&drv->kobj, &attr->attr);
  104. put_driver(drv);
  105. }
  106. }
  107. /**
  108. * get_driver - increment driver reference count.
  109. * @drv: driver.
  110. */
  111. struct device_driver * get_driver(struct device_driver * drv)
  112. {
  113. return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
  114. }
  115. /**
  116. * put_driver - decrement driver's refcount.
  117. * @drv: driver.
  118. */
  119. void put_driver(struct device_driver * drv)
  120. {
  121. kobject_put(&drv->kobj);
  122. }
  123. static void klist_devices_get(struct klist_node *n)
  124. {
  125. struct device *dev = container_of(n, struct device, knode_driver);
  126. get_device(dev);
  127. }
  128. static void klist_devices_put(struct klist_node *n)
  129. {
  130. struct device *dev = container_of(n, struct device, knode_driver);
  131. put_device(dev);
  132. }
  133. /**
  134. * driver_register - register driver with bus
  135. * @drv: driver to register
  136. *
  137. * We pass off most of the work to the bus_add_driver() call,
  138. * since most of the things we have to do deal with the bus
  139. * structures.
  140. *
  141. * The one interesting aspect is that we setup @drv->unloaded
  142. * as a completion that gets complete when the driver reference
  143. * count reaches 0.
  144. */
  145. int driver_register(struct device_driver * drv)
  146. {
  147. if ((drv->bus->probe && drv->probe) ||
  148. (drv->bus->remove && drv->remove) ||
  149. (drv->bus->shutdown && drv->shutdown)) {
  150. printk(KERN_WARNING "Driver '%s' needs updating - please use bus_type methods\n", drv->name);
  151. }
  152. klist_init(&drv->klist_devices, klist_devices_get, klist_devices_put);
  153. init_completion(&drv->unloaded);
  154. return bus_add_driver(drv);
  155. }
  156. /**
  157. * driver_unregister - remove driver from system.
  158. * @drv: driver.
  159. *
  160. * Again, we pass off most of the work to the bus-level call.
  161. *
  162. * Though, once that is done, we wait until @drv->unloaded is completed.
  163. * This will block until the driver refcount reaches 0, and it is
  164. * released. Only modular drivers will call this function, and we
  165. * have to guarantee that it won't complete, letting the driver
  166. * unload until all references are gone.
  167. */
  168. void driver_unregister(struct device_driver * drv)
  169. {
  170. bus_remove_driver(drv);
  171. wait_for_completion(&drv->unloaded);
  172. }
  173. /**
  174. * driver_find - locate driver on a bus by its name.
  175. * @name: name of the driver.
  176. * @bus: bus to scan for the driver.
  177. *
  178. * Call kset_find_obj() to iterate over list of drivers on
  179. * a bus to find driver by name. Return driver if found.
  180. *
  181. * Note that kset_find_obj increments driver's reference count.
  182. */
  183. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  184. {
  185. struct kobject *k = kset_find_obj(&bus->drivers, name);
  186. if (k)
  187. return to_drv(k);
  188. return NULL;
  189. }
  190. EXPORT_SYMBOL_GPL(driver_register);
  191. EXPORT_SYMBOL_GPL(driver_unregister);
  192. EXPORT_SYMBOL_GPL(get_driver);
  193. EXPORT_SYMBOL_GPL(put_driver);
  194. EXPORT_SYMBOL_GPL(driver_find);
  195. EXPORT_SYMBOL_GPL(driver_create_file);
  196. EXPORT_SYMBOL_GPL(driver_remove_file);