driver.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /**
  115. * driver_add_kobj - add a kobject below the specified driver
  116. * @drv: requesting device driver
  117. * @kobj: kobject to add below this driver
  118. * @fmt: format string that names the kobject
  119. *
  120. * You really don't want to do this, this is only here due to one looney
  121. * iseries driver, go poke those developers if you are annoyed about
  122. * this...
  123. */
  124. int driver_add_kobj(struct device_driver *drv, struct kobject *kobj,
  125. const char *fmt, ...)
  126. {
  127. va_list args;
  128. char *name;
  129. int ret;
  130. va_start(args, fmt);
  131. name = kvasprintf(GFP_KERNEL, fmt, args);
  132. va_end(args);
  133. if (!name)
  134. return -ENOMEM;
  135. ret = kobject_add(kobj, &drv->p->kobj, "%s", name);
  136. kfree(name);
  137. return ret;
  138. }
  139. EXPORT_SYMBOL_GPL(driver_add_kobj);
  140. /**
  141. * get_driver - increment driver reference count.
  142. * @drv: driver.
  143. */
  144. struct device_driver *get_driver(struct device_driver *drv)
  145. {
  146. if (drv) {
  147. struct driver_private *priv;
  148. struct kobject *kobj;
  149. kobj = kobject_get(&drv->p->kobj);
  150. priv = to_driver(kobj);
  151. return priv->driver;
  152. }
  153. return NULL;
  154. }
  155. EXPORT_SYMBOL_GPL(get_driver);
  156. /**
  157. * put_driver - decrement driver's refcount.
  158. * @drv: driver.
  159. */
  160. void put_driver(struct device_driver *drv)
  161. {
  162. kobject_put(&drv->p->kobj);
  163. }
  164. EXPORT_SYMBOL_GPL(put_driver);
  165. static int driver_add_groups(struct device_driver *drv,
  166. const struct attribute_group **groups)
  167. {
  168. int error = 0;
  169. int i;
  170. if (groups) {
  171. for (i = 0; groups[i]; i++) {
  172. error = sysfs_create_group(&drv->p->kobj, groups[i]);
  173. if (error) {
  174. while (--i >= 0)
  175. sysfs_remove_group(&drv->p->kobj,
  176. groups[i]);
  177. break;
  178. }
  179. }
  180. }
  181. return error;
  182. }
  183. static void driver_remove_groups(struct device_driver *drv,
  184. const struct attribute_group **groups)
  185. {
  186. int i;
  187. if (groups)
  188. for (i = 0; groups[i]; i++)
  189. sysfs_remove_group(&drv->p->kobj, groups[i]);
  190. }
  191. /**
  192. * driver_register - register driver with bus
  193. * @drv: driver to register
  194. *
  195. * We pass off most of the work to the bus_add_driver() call,
  196. * since most of the things we have to do deal with the bus
  197. * structures.
  198. */
  199. int driver_register(struct device_driver *drv)
  200. {
  201. int ret;
  202. struct device_driver *other;
  203. BUG_ON(!drv->bus->p);
  204. if ((drv->bus->probe && drv->probe) ||
  205. (drv->bus->remove && drv->remove) ||
  206. (drv->bus->shutdown && drv->shutdown))
  207. printk(KERN_WARNING "Driver '%s' needs updating - please use "
  208. "bus_type methods\n", drv->name);
  209. other = driver_find(drv->name, drv->bus);
  210. if (other) {
  211. put_driver(other);
  212. printk(KERN_ERR "Error: Driver '%s' is already registered, "
  213. "aborting...\n", drv->name);
  214. return -EBUSY;
  215. }
  216. ret = bus_add_driver(drv);
  217. if (ret)
  218. return ret;
  219. ret = driver_add_groups(drv, drv->groups);
  220. if (ret)
  221. bus_remove_driver(drv);
  222. return ret;
  223. }
  224. EXPORT_SYMBOL_GPL(driver_register);
  225. /**
  226. * driver_unregister - remove driver from system.
  227. * @drv: driver.
  228. *
  229. * Again, we pass off most of the work to the bus-level call.
  230. */
  231. void driver_unregister(struct device_driver *drv)
  232. {
  233. if (!drv || !drv->p) {
  234. WARN(1, "Unexpected driver unregister!\n");
  235. return;
  236. }
  237. driver_remove_groups(drv, drv->groups);
  238. bus_remove_driver(drv);
  239. }
  240. EXPORT_SYMBOL_GPL(driver_unregister);
  241. /**
  242. * driver_find - locate driver on a bus by its name.
  243. * @name: name of the driver.
  244. * @bus: bus to scan for the driver.
  245. *
  246. * Call kset_find_obj() to iterate over list of drivers on
  247. * a bus to find driver by name. Return driver if found.
  248. *
  249. * Note that kset_find_obj increments driver's reference count.
  250. */
  251. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  252. {
  253. struct kobject *k = kset_find_obj(bus->p->drivers_kset, name);
  254. struct driver_private *priv;
  255. if (k) {
  256. priv = to_driver(k);
  257. return priv->driver;
  258. }
  259. return NULL;
  260. }
  261. EXPORT_SYMBOL_GPL(driver_find);