dio-driver.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * DIO Driver Services
  3. *
  4. * Copyright (C) 2004 Jochen Friedrich
  5. *
  6. * Loosely based on drivers/pci/pci-driver.c and drivers/zorro/zorro-driver.c
  7. *
  8. * This file is subject to the terms and conditions of the GNU General Public
  9. * License. See the file COPYING in the main directory of this archive
  10. * for more details.
  11. */
  12. #include <linux/init.h>
  13. #include <linux/module.h>
  14. #include <linux/dio.h>
  15. /**
  16. * dio_match_device - Tell if a DIO device structure has a matching
  17. * DIO device id structure
  18. * @ids: array of DIO device id structures to search in
  19. * @dev: the DIO device structure to match against
  20. *
  21. * Used by a driver to check whether a DIO device present in the
  22. * system is in its list of supported devices. Returns the matching
  23. * dio_device_id structure or %NULL if there is no match.
  24. */
  25. const struct dio_device_id *
  26. dio_match_device(const struct dio_device_id *ids,
  27. const struct dio_dev *d)
  28. {
  29. while (ids->id) {
  30. if (ids->id == DIO_WILDCARD)
  31. return ids;
  32. if (DIO_NEEDSSECID(ids->id & 0xff)) {
  33. if (ids->id == d->id)
  34. return ids;
  35. } else {
  36. if ((ids->id & 0xff) == (d->id & 0xff))
  37. return ids;
  38. }
  39. ids++;
  40. }
  41. return NULL;
  42. }
  43. static int dio_device_probe(struct device *dev)
  44. {
  45. int error = 0;
  46. struct dio_driver *drv = to_dio_driver(dev->driver);
  47. struct dio_dev *d = to_dio_dev(dev);
  48. if (!d->driver && drv->probe) {
  49. const struct dio_device_id *id;
  50. id = dio_match_device(drv->id_table, d);
  51. if (id)
  52. error = drv->probe(d, id);
  53. if (error >= 0) {
  54. d->driver = drv;
  55. error = 0;
  56. }
  57. }
  58. return error;
  59. }
  60. /**
  61. * dio_register_driver - register a new DIO driver
  62. * @drv: the driver structure to register
  63. *
  64. * Adds the driver structure to the list of registered drivers
  65. * Returns the number of DIO devices which were claimed by the driver
  66. * during registration. The driver remains registered even if the
  67. * return value is zero.
  68. */
  69. int dio_register_driver(struct dio_driver *drv)
  70. {
  71. int count = 0;
  72. /* initialize common driver fields */
  73. drv->driver.name = drv->name;
  74. drv->driver.bus = &dio_bus_type;
  75. /* register with core */
  76. count = driver_register(&drv->driver);
  77. return count ? count : 1;
  78. }
  79. /**
  80. * dio_unregister_driver - unregister a DIO driver
  81. * @drv: the driver structure to unregister
  82. *
  83. * Deletes the driver structure from the list of registered DIO drivers,
  84. * gives it a chance to clean up by calling its remove() function for
  85. * each device it was responsible for, and marks those devices as
  86. * driverless.
  87. */
  88. void dio_unregister_driver(struct dio_driver *drv)
  89. {
  90. driver_unregister(&drv->driver);
  91. }
  92. /**
  93. * dio_bus_match - Tell if a DIO device structure has a matching DIO
  94. * device id structure
  95. * @ids: array of DIO device id structures to search in
  96. * @dev: the DIO device structure to match against
  97. *
  98. * Used by a driver to check whether a DIO device present in the
  99. * system is in its list of supported devices. Returns the matching
  100. * dio_device_id structure or %NULL if there is no match.
  101. */
  102. static int dio_bus_match(struct device *dev, struct device_driver *drv)
  103. {
  104. struct dio_dev *d = to_dio_dev(dev);
  105. struct dio_driver *dio_drv = to_dio_driver(drv);
  106. const struct dio_device_id *ids = dio_drv->id_table;
  107. if (!ids)
  108. return 0;
  109. while (ids->id) {
  110. if (ids->id == DIO_WILDCARD)
  111. return 1;
  112. if (DIO_NEEDSSECID(ids->id & 0xff)) {
  113. if (ids->id == d->id)
  114. return 1;
  115. } else {
  116. if ((ids->id & 0xff) == (d->id & 0xff))
  117. return 1;
  118. }
  119. ids++;
  120. }
  121. return 0;
  122. }
  123. struct bus_type dio_bus_type = {
  124. .name = "dio",
  125. .match = dio_bus_match,
  126. .probe = dio_device_probe,
  127. };
  128. static int __init dio_driver_init(void)
  129. {
  130. return bus_register(&dio_bus_type);
  131. }
  132. postcore_initcall(dio_driver_init);
  133. EXPORT_SYMBOL(dio_match_device);
  134. EXPORT_SYMBOL(dio_register_driver);
  135. EXPORT_SYMBOL(dio_unregister_driver);
  136. EXPORT_SYMBOL(dio_dev_driver);
  137. EXPORT_SYMBOL(dio_bus_type);