zorro-driver.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. #include "zorro.h"
  16. /**
  17. * zorro_match_device - Tell if a Zorro device structure has a matching
  18. * Zorro device id structure
  19. * @ids: array of Zorro device id structures to search in
  20. * @dev: the Zorro device structure to match against
  21. *
  22. * Used by a driver to check whether a Zorro device present in the
  23. * system is in its list of supported devices. Returns the matching
  24. * zorro_device_id structure or %NULL if there is no match.
  25. */
  26. static const struct zorro_device_id *
  27. zorro_match_device(const struct zorro_device_id *ids,
  28. const struct zorro_dev *z)
  29. {
  30. while (ids->id) {
  31. if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
  32. return ids;
  33. ids++;
  34. }
  35. return NULL;
  36. }
  37. static int zorro_device_probe(struct device *dev)
  38. {
  39. int error = 0;
  40. struct zorro_driver *drv = to_zorro_driver(dev->driver);
  41. struct zorro_dev *z = to_zorro_dev(dev);
  42. if (!z->driver && drv->probe) {
  43. const struct zorro_device_id *id;
  44. id = zorro_match_device(drv->id_table, z);
  45. if (id)
  46. error = drv->probe(z, id);
  47. if (error >= 0) {
  48. z->driver = drv;
  49. error = 0;
  50. }
  51. }
  52. return error;
  53. }
  54. static int zorro_device_remove(struct device *dev)
  55. {
  56. struct zorro_dev *z = to_zorro_dev(dev);
  57. struct zorro_driver *drv = to_zorro_driver(dev->driver);
  58. if (drv) {
  59. if (drv->remove)
  60. drv->remove(z);
  61. z->driver = NULL;
  62. }
  63. return 0;
  64. }
  65. /**
  66. * zorro_register_driver - register a new Zorro driver
  67. * @drv: the driver structure to register
  68. *
  69. * Adds the driver structure to the list of registered drivers
  70. * Returns zero or a negative error value.
  71. */
  72. int zorro_register_driver(struct zorro_driver *drv)
  73. {
  74. /* initialize common driver fields */
  75. drv->driver.name = drv->name;
  76. drv->driver.bus = &zorro_bus_type;
  77. /* register with core */
  78. return driver_register(&drv->driver);
  79. }
  80. EXPORT_SYMBOL(zorro_register_driver);
  81. /**
  82. * zorro_unregister_driver - unregister a zorro driver
  83. * @drv: the driver structure to unregister
  84. *
  85. * Deletes the driver structure from the list of registered Zorro drivers,
  86. * gives it a chance to clean up by calling its remove() function for
  87. * each device it was responsible for, and marks those devices as
  88. * driverless.
  89. */
  90. void zorro_unregister_driver(struct zorro_driver *drv)
  91. {
  92. driver_unregister(&drv->driver);
  93. }
  94. EXPORT_SYMBOL(zorro_unregister_driver);
  95. /**
  96. * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
  97. * device id structure
  98. * @ids: array of Zorro device id structures to search in
  99. * @dev: the Zorro device structure to match against
  100. *
  101. * Used by the driver core to check whether a Zorro device present in the
  102. * system is in a driver's list of supported devices. Returns 1 if
  103. * supported, and 0 if there is no match.
  104. */
  105. static int zorro_bus_match(struct device *dev, struct device_driver *drv)
  106. {
  107. struct zorro_dev *z = to_zorro_dev(dev);
  108. struct zorro_driver *zorro_drv = to_zorro_driver(drv);
  109. const struct zorro_device_id *ids = zorro_drv->id_table;
  110. if (!ids)
  111. return 0;
  112. return !!zorro_match_device(ids, z);
  113. }
  114. static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
  115. {
  116. struct zorro_dev *z;
  117. if (!dev)
  118. return -ENODEV;
  119. z = to_zorro_dev(dev);
  120. if (!z)
  121. return -ENODEV;
  122. if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
  123. add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
  124. add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
  125. add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
  126. return -ENOMEM;
  127. return 0;
  128. }
  129. struct bus_type zorro_bus_type = {
  130. .name = "zorro",
  131. .dev_name = "zorro",
  132. .dev_groups = zorro_device_attribute_groups,
  133. .match = zorro_bus_match,
  134. .uevent = zorro_uevent,
  135. .probe = zorro_device_probe,
  136. .remove = zorro_device_remove,
  137. };
  138. EXPORT_SYMBOL(zorro_bus_type);
  139. static int __init zorro_driver_init(void)
  140. {
  141. return bus_register(&zorro_bus_type);
  142. }
  143. postcore_initcall(zorro_driver_init);