zorro: Simplify remove callback
[linux-2.6-block.git] / drivers / zorro / zorro-driver.c
CommitLineData
1da177e4
LT
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
13#include <linux/init.h>
14#include <linux/module.h>
15#include <linux/zorro.h>
16
87e715de
DT
17#include "zorro.h"
18
1da177e4
LT
19
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
25 *
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.
29 */
30
0cfb07c5 31static const struct zorro_device_id *
1da177e4
LT
32zorro_match_device(const struct zorro_device_id *ids,
33 const struct zorro_dev *z)
34{
35 while (ids->id) {
36 if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
37 return ids;
38 ids++;
39 }
40 return NULL;
41}
42
43
44static int zorro_device_probe(struct device *dev)
45{
46 int error = 0;
47 struct zorro_driver *drv = to_zorro_driver(dev->driver);
48 struct zorro_dev *z = to_zorro_dev(dev);
49
50 if (!z->driver && drv->probe) {
51 const struct zorro_device_id *id;
52
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;
59 }
60 }
61 return error;
62}
63
64
fc7a6209 65static void zorro_device_remove(struct device *dev)
2f9b0b5e
GU
66{
67 struct zorro_dev *z = to_zorro_dev(dev);
68 struct zorro_driver *drv = to_zorro_driver(dev->driver);
69
18d214cc
UKK
70 if (drv->remove)
71 drv->remove(z);
72 z->driver = NULL;
2f9b0b5e
GU
73}
74
75
1da177e4
LT
76 /**
77 * zorro_register_driver - register a new Zorro driver
78 * @drv: the driver structure to register
79 *
80 * Adds the driver structure to the list of registered drivers
33d8675e 81 * Returns zero or a negative error value.
1da177e4
LT
82 */
83
84int zorro_register_driver(struct zorro_driver *drv)
85{
1da177e4
LT
86 /* initialize common driver fields */
87 drv->driver.name = drv->name;
88 drv->driver.bus = &zorro_bus_type;
1da177e4
LT
89
90 /* register with core */
33d8675e 91 return driver_register(&drv->driver);
1da177e4 92}
d996e9dc 93EXPORT_SYMBOL(zorro_register_driver);
1da177e4
LT
94
95
96 /**
97 * zorro_unregister_driver - unregister a zorro driver
98 * @drv: the driver structure to unregister
99 *
100 * Deletes the driver structure from the list of registered Zorro drivers,
101 * gives it a chance to clean up by calling its remove() function for
102 * each device it was responsible for, and marks those devices as
103 * driverless.
104 */
105
106void zorro_unregister_driver(struct zorro_driver *drv)
107{
108 driver_unregister(&drv->driver);
109}
d996e9dc 110EXPORT_SYMBOL(zorro_unregister_driver);
1da177e4
LT
111
112
113 /**
114 * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
115 * device id structure
116 * @ids: array of Zorro device id structures to search in
117 * @dev: the Zorro device structure to match against
118 *
aeee094d
GU
119 * Used by the driver core to check whether a Zorro device present in the
120 * system is in a driver's list of supported devices. Returns 1 if
121 * supported, and 0 if there is no match.
1da177e4
LT
122 */
123
124static int zorro_bus_match(struct device *dev, struct device_driver *drv)
125{
126 struct zorro_dev *z = to_zorro_dev(dev);
127 struct zorro_driver *zorro_drv = to_zorro_driver(drv);
128 const struct zorro_device_id *ids = zorro_drv->id_table;
129
130 if (!ids)
131 return 0;
132
3d52910e 133 return !!zorro_match_device(ids, z);
1da177e4
LT
134}
135
bf54a2b3
GU
136static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
137{
bf54a2b3
GU
138 struct zorro_dev *z;
139
140 if (!dev)
141 return -ENODEV;
142
143 z = to_zorro_dev(dev);
144 if (!z)
145 return -ENODEV;
146
147 if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
148 add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
149 add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
150 add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
151 return -ENOMEM;
152
153 return 0;
bf54a2b3 154}
1da177e4
LT
155
156struct bus_type zorro_bus_type = {
87e715de
DT
157 .name = "zorro",
158 .dev_name = "zorro",
159 .dev_groups = zorro_device_attribute_groups,
160 .match = zorro_bus_match,
161 .uevent = zorro_uevent,
162 .probe = zorro_device_probe,
163 .remove = zorro_device_remove,
1da177e4 164};
d996e9dc 165EXPORT_SYMBOL(zorro_bus_type);
1da177e4
LT
166
167
168static int __init zorro_driver_init(void)
169{
170 return bus_register(&zorro_bus_type);
171}
172
173postcore_initcall(zorro_driver_init);
174