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