zorro-driver.c 4.4 KB

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