driver.c 5.6 KB

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