dio-driver.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 zero or a negative error value.
  66. */
  67. int dio_register_driver(struct dio_driver *drv)
  68. {
  69. /* initialize common driver fields */
  70. drv->driver.name = drv->name;
  71. drv->driver.bus = &dio_bus_type;
  72. /* register with core */
  73. return driver_register(&drv->driver);
  74. }
  75. /**
  76. * dio_unregister_driver - unregister a DIO driver
  77. * @drv: the driver structure to unregister
  78. *
  79. * Deletes the driver structure from the list of registered DIO drivers,
  80. * gives it a chance to clean up by calling its remove() function for
  81. * each device it was responsible for, and marks those devices as
  82. * driverless.
  83. */
  84. void dio_unregister_driver(struct dio_driver *drv)
  85. {
  86. driver_unregister(&drv->driver);
  87. }
  88. /**
  89. * dio_bus_match - Tell if a DIO device structure has a matching DIO
  90. * device id structure
  91. * @ids: array of DIO device id structures to search in
  92. * @dev: the DIO device structure to match against
  93. *
  94. * Used by a driver to check whether a DIO device present in the
  95. * system is in its list of supported devices. Returns the matching
  96. * dio_device_id structure or %NULL if there is no match.
  97. */
  98. static int dio_bus_match(struct device *dev, struct device_driver *drv)
  99. {
  100. struct dio_dev *d = to_dio_dev(dev);
  101. struct dio_driver *dio_drv = to_dio_driver(drv);
  102. const struct dio_device_id *ids = dio_drv->id_table;
  103. if (!ids)
  104. return 0;
  105. while (ids->id) {
  106. if (ids->id == DIO_WILDCARD)
  107. return 1;
  108. if (DIO_NEEDSSECID(ids->id & 0xff)) {
  109. if (ids->id == d->id)
  110. return 1;
  111. } else {
  112. if ((ids->id & 0xff) == (d->id & 0xff))
  113. return 1;
  114. }
  115. ids++;
  116. }
  117. return 0;
  118. }
  119. struct bus_type dio_bus_type = {
  120. .name = "dio",
  121. .match = dio_bus_match,
  122. .probe = dio_device_probe,
  123. };
  124. static int __init dio_driver_init(void)
  125. {
  126. return bus_register(&dio_bus_type);
  127. }
  128. postcore_initcall(dio_driver_init);
  129. EXPORT_SYMBOL(dio_match_device);
  130. EXPORT_SYMBOL(dio_register_driver);
  131. EXPORT_SYMBOL(dio_unregister_driver);
  132. EXPORT_SYMBOL(dio_dev_driver);
  133. EXPORT_SYMBOL(dio_bus_type);