driver.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_create_file - create sysfs file for driver.
  49. * @drv: driver.
  50. * @attr: driver attribute descriptor.
  51. */
  52. int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
  53. {
  54. int error;
  55. if (get_driver(drv)) {
  56. error = sysfs_create_file(&drv->kobj, &attr->attr);
  57. put_driver(drv);
  58. } else
  59. error = -EINVAL;
  60. return error;
  61. }
  62. /**
  63. * driver_remove_file - remove sysfs file for driver.
  64. * @drv: driver.
  65. * @attr: driver attribute descriptor.
  66. */
  67. void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
  68. {
  69. if (get_driver(drv)) {
  70. sysfs_remove_file(&drv->kobj, &attr->attr);
  71. put_driver(drv);
  72. }
  73. }
  74. /**
  75. * get_driver - increment driver reference count.
  76. * @drv: driver.
  77. */
  78. struct device_driver * get_driver(struct device_driver * drv)
  79. {
  80. return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
  81. }
  82. /**
  83. * put_driver - decrement driver's refcount.
  84. * @drv: driver.
  85. */
  86. void put_driver(struct device_driver * drv)
  87. {
  88. kobject_put(&drv->kobj);
  89. }
  90. /**
  91. * driver_register - register driver with bus
  92. * @drv: driver to register
  93. *
  94. * We pass off most of the work to the bus_add_driver() call,
  95. * since most of the things we have to do deal with the bus
  96. * structures.
  97. *
  98. * The one interesting aspect is that we setup @drv->unloaded
  99. * as a completion that gets complete when the driver reference
  100. * count reaches 0.
  101. */
  102. int driver_register(struct device_driver * drv)
  103. {
  104. klist_init(&drv->klist_devices);
  105. init_completion(&drv->unloaded);
  106. return bus_add_driver(drv);
  107. }
  108. /**
  109. * driver_unregister - remove driver from system.
  110. * @drv: driver.
  111. *
  112. * Again, we pass off most of the work to the bus-level call.
  113. *
  114. * Though, once that is done, we wait until @drv->unloaded is completed.
  115. * This will block until the driver refcount reaches 0, and it is
  116. * released. Only modular drivers will call this function, and we
  117. * have to guarantee that it won't complete, letting the driver
  118. * unload until all references are gone.
  119. */
  120. void driver_unregister(struct device_driver * drv)
  121. {
  122. bus_remove_driver(drv);
  123. wait_for_completion(&drv->unloaded);
  124. }
  125. /**
  126. * driver_find - locate driver on a bus by its name.
  127. * @name: name of the driver.
  128. * @bus: bus to scan for the driver.
  129. *
  130. * Call kset_find_obj() to iterate over list of drivers on
  131. * a bus to find driver by name. Return driver if found.
  132. *
  133. * Note that kset_find_obj increments driver's reference count.
  134. */
  135. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  136. {
  137. struct kobject *k = kset_find_obj(&bus->drivers, name);
  138. if (k)
  139. return to_drv(k);
  140. return NULL;
  141. }
  142. EXPORT_SYMBOL_GPL(driver_register);
  143. EXPORT_SYMBOL_GPL(driver_unregister);
  144. EXPORT_SYMBOL_GPL(get_driver);
  145. EXPORT_SYMBOL_GPL(put_driver);
  146. EXPORT_SYMBOL_GPL(driver_find);
  147. EXPORT_SYMBOL_GPL(driver_create_file);
  148. EXPORT_SYMBOL_GPL(driver_remove_file);