^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * Zorro Driver Services
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2003 Geert Uytterhoeven
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Loosely based on drivers/pci/pci-driver.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * License. See the file COPYING in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/zorro.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include "zorro.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * zorro_match_device - Tell if a Zorro device structure has a matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * Zorro device id structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * @ids: array of Zorro device id structures to search in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * @dev: the Zorro device structure to match against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * Used by a driver to check whether a Zorro device present in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * system is in its list of supported devices. Returns the matching
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * zorro_device_id structure or %NULL if there is no match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static const struct zorro_device_id *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) zorro_match_device(const struct zorro_device_id *ids,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) const struct zorro_dev *z)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) while (ids->id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return ids;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) ids++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) static int zorro_device_probe(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct zorro_driver *drv = to_zorro_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct zorro_dev *z = to_zorro_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (!z->driver && drv->probe) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) const struct zorro_device_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) id = zorro_match_device(drv->id_table, z);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) error = drv->probe(z, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) if (error >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) z->driver = drv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static int zorro_device_remove(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct zorro_dev *z = to_zorro_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) struct zorro_driver *drv = to_zorro_driver(dev->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (drv) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (drv->remove)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) drv->remove(z);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) z->driver = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * zorro_register_driver - register a new Zorro driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * @drv: the driver structure to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Adds the driver structure to the list of registered drivers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * Returns zero or a negative error value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) int zorro_register_driver(struct zorro_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* initialize common driver fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) drv->driver.name = drv->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) drv->driver.bus = &zorro_bus_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* register with core */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return driver_register(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) EXPORT_SYMBOL(zorro_register_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * zorro_unregister_driver - unregister a zorro driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) * @drv: the driver structure to unregister
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) * Deletes the driver structure from the list of registered Zorro drivers,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) * gives it a chance to clean up by calling its remove() function for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) * each device it was responsible for, and marks those devices as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) * driverless.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) void zorro_unregister_driver(struct zorro_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) driver_unregister(&drv->driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) EXPORT_SYMBOL(zorro_unregister_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) * device id structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) * @ids: array of Zorro device id structures to search in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) * @dev: the Zorro device structure to match against
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) * Used by the driver core to check whether a Zorro device present in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) * system is in a driver's list of supported devices. Returns 1 if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) * supported, and 0 if there is no match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) static int zorro_bus_match(struct device *dev, struct device_driver *drv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) struct zorro_dev *z = to_zorro_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct zorro_driver *zorro_drv = to_zorro_driver(drv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) const struct zorro_device_id *ids = zorro_drv->id_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (!ids)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) return !!zorro_match_device(ids, z);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) struct zorro_dev *z;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) z = to_zorro_dev(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (!z)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) struct bus_type zorro_bus_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) .name = "zorro",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) .dev_name = "zorro",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) .dev_groups = zorro_device_attribute_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) .match = zorro_bus_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) .uevent = zorro_uevent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) .probe = zorro_device_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) .remove = zorro_device_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) EXPORT_SYMBOL(zorro_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) static int __init zorro_driver_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return bus_register(&zorro_bus_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) postcore_initcall(zorro_driver_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)