zorro-driver.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. EXPORT_SYMBOL(zorro_match_device);
  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 a driver to check whether a Zorro device present in the
  102. * system is in its list of supported devices.Returns the matching
  103. * zorro_device_id structure or %NULL 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. while (ids->id) {
  113. if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
  114. return 1;
  115. ids++;
  116. }
  117. return 0;
  118. }
  119. static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
  120. {
  121. struct zorro_dev *z;
  122. if (!dev)
  123. return -ENODEV;
  124. z = to_zorro_dev(dev);
  125. if (!z)
  126. return -ENODEV;
  127. if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
  128. add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
  129. add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
  130. add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
  131. return -ENOMEM;
  132. return 0;
  133. }
  134. struct bus_type zorro_bus_type = {
  135. .name = "zorro",
  136. .dev_name = "zorro",
  137. .match = zorro_bus_match,
  138. .uevent = zorro_uevent,
  139. .probe = zorro_device_probe,
  140. .remove = zorro_device_remove,
  141. };
  142. EXPORT_SYMBOL(zorro_bus_type);
  143. static int __init zorro_driver_init(void)
  144. {
  145. return bus_register(&zorro_bus_type);
  146. }
  147. postcore_initcall(zorro_driver_init);