zorro-driver.c 3.6 KB

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