driver.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. /**
  19. * driver_create_file - create sysfs file for driver.
  20. * @drv: driver.
  21. * @attr: driver attribute descriptor.
  22. */
  23. int driver_create_file(struct device_driver * drv, struct driver_attribute * attr)
  24. {
  25. int error;
  26. if (get_driver(drv)) {
  27. error = sysfs_create_file(&drv->kobj, &attr->attr);
  28. put_driver(drv);
  29. } else
  30. error = -EINVAL;
  31. return error;
  32. }
  33. /**
  34. * driver_remove_file - remove sysfs file for driver.
  35. * @drv: driver.
  36. * @attr: driver attribute descriptor.
  37. */
  38. void driver_remove_file(struct device_driver * drv, struct driver_attribute * attr)
  39. {
  40. if (get_driver(drv)) {
  41. sysfs_remove_file(&drv->kobj, &attr->attr);
  42. put_driver(drv);
  43. }
  44. }
  45. /**
  46. * get_driver - increment driver reference count.
  47. * @drv: driver.
  48. */
  49. struct device_driver * get_driver(struct device_driver * drv)
  50. {
  51. return drv ? to_drv(kobject_get(&drv->kobj)) : NULL;
  52. }
  53. /**
  54. * put_driver - decrement driver's refcount.
  55. * @drv: driver.
  56. */
  57. void put_driver(struct device_driver * drv)
  58. {
  59. kobject_put(&drv->kobj);
  60. }
  61. /**
  62. * driver_register - register driver with bus
  63. * @drv: driver to register
  64. *
  65. * We pass off most of the work to the bus_add_driver() call,
  66. * since most of the things we have to do deal with the bus
  67. * structures.
  68. *
  69. * The one interesting aspect is that we setup @drv->unloaded
  70. * as a completion that gets complete when the driver reference
  71. * count reaches 0.
  72. */
  73. int driver_register(struct device_driver * drv)
  74. {
  75. INIT_LIST_HEAD(&drv->devices);
  76. init_completion(&drv->unloaded);
  77. return bus_add_driver(drv);
  78. }
  79. /**
  80. * driver_unregister - remove driver from system.
  81. * @drv: driver.
  82. *
  83. * Again, we pass off most of the work to the bus-level call.
  84. *
  85. * Though, once that is done, we wait until @drv->unloaded is completed.
  86. * This will block until the driver refcount reaches 0, and it is
  87. * released. Only modular drivers will call this function, and we
  88. * have to guarantee that it won't complete, letting the driver
  89. * unload until all references are gone.
  90. */
  91. void driver_unregister(struct device_driver * drv)
  92. {
  93. bus_remove_driver(drv);
  94. wait_for_completion(&drv->unloaded);
  95. }
  96. /**
  97. * driver_find - locate driver on a bus by its name.
  98. * @name: name of the driver.
  99. * @bus: bus to scan for the driver.
  100. *
  101. * Call kset_find_obj() to iterate over list of drivers on
  102. * a bus to find driver by name. Return driver if found.
  103. *
  104. * Note that kset_find_obj increments driver's reference count.
  105. */
  106. struct device_driver *driver_find(const char *name, struct bus_type *bus)
  107. {
  108. struct kobject *k = kset_find_obj(&bus->drivers, name);
  109. if (k)
  110. return to_drv(k);
  111. return NULL;
  112. }
  113. EXPORT_SYMBOL_GPL(driver_register);
  114. EXPORT_SYMBOL_GPL(driver_unregister);
  115. EXPORT_SYMBOL_GPL(get_driver);
  116. EXPORT_SYMBOL_GPL(put_driver);
  117. EXPORT_SYMBOL_GPL(driver_find);
  118. EXPORT_SYMBOL_GPL(driver_create_file);
  119. EXPORT_SYMBOL_GPL(driver_remove_file);