driver.c 5.5 KB

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