zorro-driver.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. /**
  54. * zorro_register_driver - register a new Zorro driver
  55. * @drv: the driver structure to register
  56. *
  57. * Adds the driver structure to the list of registered drivers
  58. * Returns the number of Zorro devices which were claimed by the driver
  59. * during registration. The driver remains registered even if the
  60. * return value is zero.
  61. */
  62. int zorro_register_driver(struct zorro_driver *drv)
  63. {
  64. int count = 0;
  65. /* initialize common driver fields */
  66. drv->driver.name = drv->name;
  67. drv->driver.bus = &zorro_bus_type;
  68. /* register with core */
  69. count = driver_register(&drv->driver);
  70. return count ? count : 1;
  71. }
  72. /**
  73. * zorro_unregister_driver - unregister a zorro driver
  74. * @drv: the driver structure to unregister
  75. *
  76. * Deletes the driver structure from the list of registered Zorro drivers,
  77. * gives it a chance to clean up by calling its remove() function for
  78. * each device it was responsible for, and marks those devices as
  79. * driverless.
  80. */
  81. void zorro_unregister_driver(struct zorro_driver *drv)
  82. {
  83. driver_unregister(&drv->driver);
  84. }
  85. /**
  86. * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
  87. * device id structure
  88. * @ids: array of Zorro device id structures to search in
  89. * @dev: the Zorro device structure to match against
  90. *
  91. * Used by a driver to check whether a Zorro device present in the
  92. * system is in its list of supported devices.Returns the matching
  93. * zorro_device_id structure or %NULL if there is no match.
  94. */
  95. static int zorro_bus_match(struct device *dev, struct device_driver *drv)
  96. {
  97. struct zorro_dev *z = to_zorro_dev(dev);
  98. struct zorro_driver *zorro_drv = to_zorro_driver(drv);
  99. const struct zorro_device_id *ids = zorro_drv->id_table;
  100. if (!ids)
  101. return 0;
  102. while (ids->id) {
  103. if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
  104. return 1;
  105. ids++;
  106. }
  107. return 0;
  108. }
  109. struct bus_type zorro_bus_type = {
  110. .name = "zorro",
  111. .match = zorro_bus_match,
  112. .probe = zorro_device_probe,
  113. };
  114. static int __init zorro_driver_init(void)
  115. {
  116. return bus_register(&zorro_bus_type);
  117. }
  118. postcore_initcall(zorro_driver_init);
  119. EXPORT_SYMBOL(zorro_match_device);
  120. EXPORT_SYMBOL(zorro_register_driver);
  121. EXPORT_SYMBOL(zorro_unregister_driver);
  122. EXPORT_SYMBOL(zorro_dev_driver);
  123. EXPORT_SYMBOL(zorro_bus_type);