zorro-driver.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Zorro Driver Services
  3. *
  4. * Copyright (C) 2003 Geert Uytterhoeven
  5. *
  6. * Loosely based on drivers/pci/pci-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/zorro.h>
  15. /**
  16. * zorro_match_device - Tell if a Zorro device structure has a matching
  17. * Zorro device id structure
  18. * @ids: array of Zorro device id structures to search in
  19. * @dev: the Zorro device structure to match against
  20. *
  21. * Used by a driver to check whether a Zorro device present in the
  22. * system is in its list of supported devices. Returns the matching
  23. * zorro_device_id structure or %NULL if there is no match.
  24. */
  25. const struct zorro_device_id *
  26. zorro_match_device(const struct zorro_device_id *ids,
  27. const struct zorro_dev *z)
  28. {
  29. while (ids->id) {
  30. if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
  31. return ids;
  32. ids++;
  33. }
  34. return NULL;
  35. }
  36. static int zorro_device_probe(struct device *dev)
  37. {
  38. int error = 0;
  39. struct zorro_driver *drv = to_zorro_driver(dev->driver);
  40. struct zorro_dev *z = to_zorro_dev(dev);
  41. if (!z->driver && drv->probe) {
  42. const struct zorro_device_id *id;
  43. id = zorro_match_device(drv->id_table, z);
  44. if (id)
  45. error = drv->probe(z, id);
  46. if (error >= 0) {
  47. z->driver = drv;
  48. error = 0;
  49. }
  50. }
  51. return error;
  52. }
  53. static int zorro_device_remove(struct device *dev)
  54. {
  55. struct zorro_dev *z = to_zorro_dev(dev);
  56. struct zorro_driver *drv = to_zorro_driver(dev->driver);
  57. if (drv) {
  58. if (drv->remove)
  59. drv->remove(z);
  60. z->driver = NULL;
  61. }
  62. return 0;
  63. }
  64. /**
  65. * zorro_register_driver - register a new Zorro driver
  66. * @drv: the driver structure to register
  67. *
  68. * Adds the driver structure to the list of registered drivers
  69. * Returns zero or a negative error value.
  70. */
  71. int zorro_register_driver(struct zorro_driver *drv)
  72. {
  73. /* initialize common driver fields */
  74. drv->driver.name = drv->name;
  75. drv->driver.bus = &zorro_bus_type;
  76. /* register with core */
  77. return driver_register(&drv->driver);
  78. }
  79. /**
  80. * zorro_unregister_driver - unregister a zorro driver
  81. * @drv: the driver structure to unregister
  82. *
  83. * Deletes the driver structure from the list of registered Zorro 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 zorro_unregister_driver(struct zorro_driver *drv)
  89. {
  90. driver_unregister(&drv->driver);
  91. }
  92. /**
  93. * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
  94. * device id structure
  95. * @ids: array of Zorro device id structures to search in
  96. * @dev: the Zorro device structure to match against
  97. *
  98. * Used by a driver to check whether a Zorro device present in the
  99. * system is in its list of supported devices.Returns the matching
  100. * zorro_device_id structure or %NULL if there is no match.
  101. */
  102. static int zorro_bus_match(struct device *dev, struct device_driver *drv)
  103. {
  104. struct zorro_dev *z = to_zorro_dev(dev);
  105. struct zorro_driver *zorro_drv = to_zorro_driver(drv);
  106. const struct zorro_device_id *ids = zorro_drv->id_table;
  107. if (!ids)
  108. return 0;
  109. while (ids->id) {
  110. if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
  111. return 1;
  112. ids++;
  113. }
  114. return 0;
  115. }
  116. struct bus_type zorro_bus_type = {
  117. .name = "zorro",
  118. .match = zorro_bus_match,
  119. .probe = zorro_device_probe,
  120. .remove = zorro_device_remove,
  121. };
  122. static int __init zorro_driver_init(void)
  123. {
  124. return bus_register(&zorro_bus_type);
  125. }
  126. postcore_initcall(zorro_driver_init);
  127. EXPORT_SYMBOL(zorro_match_device);
  128. EXPORT_SYMBOL(zorro_register_driver);
  129. EXPORT_SYMBOL(zorro_unregister_driver);
  130. EXPORT_SYMBOL(zorro_dev_driver);
  131. EXPORT_SYMBOL(zorro_bus_type);