Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[linux/fpc-iii.git] / drivers / zorro / zorro-driver.c
blob0dd7cbcec2b0d478b229602280b6cb77f5b372b7
1 /*
2 * Zorro Driver Services
4 * Copyright (C) 2003 Geert Uytterhoeven
6 * Loosely based on drivers/pci/pci-driver.c
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.
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/zorro.h>
17 #include "zorro.h"
20 /**
21 * zorro_match_device - Tell if a Zorro device structure has a matching
22 * Zorro device id structure
23 * @ids: array of Zorro device id structures to search in
24 * @dev: the Zorro device structure to match against
26 * Used by a driver to check whether a Zorro device present in the
27 * system is in its list of supported devices. Returns the matching
28 * zorro_device_id structure or %NULL if there is no match.
31 static const struct zorro_device_id *
32 zorro_match_device(const struct zorro_device_id *ids,
33 const struct zorro_dev *z)
35 while (ids->id) {
36 if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
37 return ids;
38 ids++;
40 return NULL;
44 static int zorro_device_probe(struct device *dev)
46 int error = 0;
47 struct zorro_driver *drv = to_zorro_driver(dev->driver);
48 struct zorro_dev *z = to_zorro_dev(dev);
50 if (!z->driver && drv->probe) {
51 const struct zorro_device_id *id;
53 id = zorro_match_device(drv->id_table, z);
54 if (id)
55 error = drv->probe(z, id);
56 if (error >= 0) {
57 z->driver = drv;
58 error = 0;
61 return error;
65 static int zorro_device_remove(struct device *dev)
67 struct zorro_dev *z = to_zorro_dev(dev);
68 struct zorro_driver *drv = to_zorro_driver(dev->driver);
70 if (drv) {
71 if (drv->remove)
72 drv->remove(z);
73 z->driver = NULL;
75 return 0;
79 /**
80 * zorro_register_driver - register a new Zorro driver
81 * @drv: the driver structure to register
83 * Adds the driver structure to the list of registered drivers
84 * Returns zero or a negative error value.
87 int zorro_register_driver(struct zorro_driver *drv)
89 /* initialize common driver fields */
90 drv->driver.name = drv->name;
91 drv->driver.bus = &zorro_bus_type;
93 /* register with core */
94 return driver_register(&drv->driver);
96 EXPORT_SYMBOL(zorro_register_driver);
99 /**
100 * zorro_unregister_driver - unregister a zorro driver
101 * @drv: the driver structure to unregister
103 * Deletes the driver structure from the list of registered Zorro drivers,
104 * gives it a chance to clean up by calling its remove() function for
105 * each device it was responsible for, and marks those devices as
106 * driverless.
109 void zorro_unregister_driver(struct zorro_driver *drv)
111 driver_unregister(&drv->driver);
113 EXPORT_SYMBOL(zorro_unregister_driver);
117 * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
118 * device id structure
119 * @ids: array of Zorro device id structures to search in
120 * @dev: the Zorro device structure to match against
122 * Used by the driver core to check whether a Zorro device present in the
123 * system is in a driver's list of supported devices. Returns 1 if
124 * supported, and 0 if there is no match.
127 static int zorro_bus_match(struct device *dev, struct device_driver *drv)
129 struct zorro_dev *z = to_zorro_dev(dev);
130 struct zorro_driver *zorro_drv = to_zorro_driver(drv);
131 const struct zorro_device_id *ids = zorro_drv->id_table;
133 if (!ids)
134 return 0;
136 return !!zorro_match_device(ids, z);
139 static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
141 struct zorro_dev *z;
143 if (!dev)
144 return -ENODEV;
146 z = to_zorro_dev(dev);
147 if (!z)
148 return -ENODEV;
150 if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
151 add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
152 add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
153 add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
154 return -ENOMEM;
156 return 0;
159 struct bus_type zorro_bus_type = {
160 .name = "zorro",
161 .dev_name = "zorro",
162 .dev_groups = zorro_device_attribute_groups,
163 .match = zorro_bus_match,
164 .uevent = zorro_uevent,
165 .probe = zorro_device_probe,
166 .remove = zorro_device_remove,
168 EXPORT_SYMBOL(zorro_bus_type);
171 static int __init zorro_driver_init(void)
173 return bus_register(&zorro_bus_type);
176 postcore_initcall(zorro_driver_init);