zorro-driver.c 4.3 KB

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