driver.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. * Copyright (c) 2007 Greg Kroah-Hartman <gregkh@suse.de>
  7. * Copyright (c) 2007 Novell Inc.
  8. *
  9. * This file is released under the GPLv2
  10. *
  11. */
  12. #include <linux/device.h>
  13. #include <linux/module.h>
  14. #include <linux/errno.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include "base.h"
  18. static struct device *next_device(struct klist_iter *i)
  19. {
  20. struct klist_node *n = klist_next(i);
  21. struct device *dev = NULL;
  22. struct device_private *dev_prv;
  23. if (n) {
  24. dev_prv = to_device_private_driver(n);
  25. dev = dev_prv->device;
  26. }
  27. return dev;
  28. }
  29. /**
  30. * driver_for_each_device - Iterator for devices bound to a driver.
  31. * @drv: Driver we're iterating.
  32. * @start: Device to begin with
  33. * @data: Data to pass to the callback.
  34. * @fn: Function to call for each device.
  35. *
  36. * Iterate over the @drv's list of devices calling @fn for each one.
  37. */
  38. int driver_for_each_device(struct device_driver *drv, struct device *start,
  39. void *data, int (*fn)(struct device *, void *))
  40. {
  41. struct klist_iter i;
  42. struct device *dev;
  43. int error = 0;
  44. if (!drv)
  45. return -EINVAL;
  46. klist_iter_init_node(&drv->p->klist_devices, &i,
  47. start ? &start->p->knode_driver : NULL);
  48. while ((dev = next_device(&i)) && !error)
  49. error = fn(dev, data);
  50. klist_iter_exit(&i);
  51. return error;
  52. }
  53. EXPORT_SYMBOL_GPL(driver_for_each_device);
  54. /**
  55. * driver_find_device - device iterator for locating a particular device.
  56. * @drv: The device's driver
  57. * @start: Device to begin with
  58. * @data: Data to pass to match function
  59. * @match: Callback function to check device
  60. *
  61. * This is similar to the driver_for_each_device() function above, but
  62. * it returns a reference to a device that is 'found' for later use, as
  63. * determined by the @match callback.
  64. *
  65. * The callback should return 0 if the device doesn't match and non-zero
  66. * if it does. If the callback returns non-zero, this function will
  67. * return to the caller and not iterate over any more devices.
  68. */
  69. struct device *driver_find_device(struct device_driver *drv,
  70. struct device *start, void *data,
  71. int (*match)(struct device *dev, void *data))
  72. {
  73. struct klist_iter i;
  74. struct device *dev;
  75. if (!drv)
  76. return NULL;
  77. klist_iter_init_node(&drv->p->klist_devices, &i,
  78. (start ? &start->p->knode_driver : NULL));
  79. while ((dev = next_device(&i)))
  80. if (match(dev, data) && get_device(dev))
  81. break;
  82. klist_iter_exit(&i);
  83. return dev;
  84. }
  85. EXPORT_SYMBOL_GPL(driver_find_device);
  86. /**
  87. * driver_create_file - create sysfs file for driver.
  88. * @drv: driver.
  89. * @attr: driver attribute descriptor.
  90. */
  91. int driver_create_file(struct device_driver *drv,
  92. const struct driver_attribute *attr)
  93. {
  94. int error;
  95. if (drv)
  96. error = sysfs_create_file(&drv->p->kobj, &attr->attr);
  97. else
  98. error = -EINVAL;
  99. return error;
  100. }
  101. EXPORT_SYMBOL_GPL(driver_create_file);
  102. /**
  103. * driver_remove_file - remove sysfs file for driver.
  104. * @drv: driver.
  105. * @attr: driver attribute descriptor.
  106. */
  107. void driver_remove_file(struct device_driver *drv,
  108. const struct driver_attribute *attr)
  109. {
  110. if (drv)
  111. sysfs_remove_file(&drv->p->kobj, &attr->attr);
  112. }
  113. EXPORT_SYMBOL_GPL(driver_remove_file);
  114. static int driver_add_groups(struct device_driver *drv,
  115. const struct attribute_group **groups)
  116. {
  117. int error = 0;
  118. int i;
  119. if (groups) {
  120. for (i = 0; groups[i]; i++) {
  121. error = sysfs_create_group(&drv->p->kobj, groups[i]);
  122. if (error) {
  123. while (--i >= 0)
  124. sysfs_remove_group(&drv->p->kobj,
  125. groups[i]);
  126. break;
  127. }
  128. }
  129. }
  130. return error;
  131. }
  132. static void driver_remove_groups(struct device_driver *drv,
  133. const struct attribute_group **groups)
  134. {
  135. int i;
  136. if (groups)
  137. for (i = 0; groups[i]; i++)
  138. sysfs_remove_group(&drv->p->kobj, groups[i]);
  139. }
  140. /**
  141. * driver_register - register driver with bus
  142. * @drv: driver to register
  143. *
  144. * We pass off most of the work to the bus_add_driver() call,
  145. * since most of the things we have to do deal with the bus
  146. * structures.
  147. */
  148. int driver_register(struct device_driver *drv)
  149. {
  150. int ret;
  151. struct device_driver *other;
  152. BUG_ON(!drv->bus->p);
  153. if ((drv->bus->probe && drv->probe) ||
  154. (drv->bus->remove && drv->remove) ||
  155. (drv->bus->shutdown && drv->shutdown))
  156. printk(KERN_WARNING "Driver '%s' needs updating - please use "
  157. "bus_type methods\n", drv->name);
  158. other = driver_find(drv->name, drv->bus);
  159. if (other) {
  160. printk(KERN_ERR "Error: Driver '%s' is already registered, "
  161. "aborting...\n", drv->name);
  162. return -EBUSY;
  163. }
  164. ret = bus_add_driver(drv);
  165. if (ret)
  166. return ret;
  167. ret = driver_add_groups(drv, drv->groups);
  168. if (ret)
  169. bus_remove_driver(drv);
  170. return ret;
  171. }
  172. EXPORT_SYMBOL_GPL(driver_register);
  173. /**
  174. * driver_unregister - remove driver from system.
  175. * @drv: driver.
  176. *
  177. * Again, we pass off most of the work to the bus-level call.
  178. */
  179. void driver_unregister(struct device_driver *drv)
  180. {
  181. if (!drv || !drv->p) {
  182. WARN(1, "Unexpected driver unregister!\n");
  183. return;
  184. }
  185. driver_remove_groups(drv, drv->groups);
  186. bus_remove_driver(drv);
  187. }
  188. EXPORT_SYMBOL_GPL(driver_unregister);
  189. /**
  190. * driver_find - locate driver on a bus by its name.
  191. * @name: name of the driver.
  192. * @bus: bus to scan for the driver.
  193. *
  194. * Call kset_find_obj() to iterate over list of drivers on
  195. * a bus to find driver by name. Return driver if found.
  196. *
  197. * This routine provides no locking to prevent the driver it returns
  198. * from being unregistered or unloaded while the caller is using it.
  199. * The caller is responsible for preventing this.
  200. */
  201. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  202. {
  203. struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
  204. struct driver_private *priv;
  205. if (k) {
  206. /* Drop reference added by kset_find_obj() */
  207. kobject_put(k);
  208. priv = to_driver(k);
  209. return priv->driver;
  210. }
  211. return NULL;
  212. }
  213. EXPORT_SYMBOL_GPL(driver_find);