blob: ac1db7f1bcab7731e9366cfc4bce832c4258cd35 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
17
18 /**
19 * zorro_match_device - Tell if a Zorro device structure has a matching
20 * Zorro device id structure
21 * @ids: array of Zorro device id structures to search in
22 * @dev: the Zorro device structure to match against
23 *
24 * Used by a driver to check whether a Zorro device present in the
25 * system is in its list of supported devices. Returns the matching
26 * zorro_device_id structure or %NULL if there is no match.
27 */
28
29const struct zorro_device_id *
30zorro_match_device(const struct zorro_device_id *ids,
31 const struct zorro_dev *z)
32{
33 while (ids->id) {
34 if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
35 return ids;
36 ids++;
37 }
38 return NULL;
39}
Jim Rotmalmd996e9d2011-07-17 23:12:30 +020040EXPORT_SYMBOL(zorro_match_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43static int zorro_device_probe(struct device *dev)
44{
45 int error = 0;
46 struct zorro_driver *drv = to_zorro_driver(dev->driver);
47 struct zorro_dev *z = to_zorro_dev(dev);
48
49 if (!z->driver && drv->probe) {
50 const struct zorro_device_id *id;
51
52 id = zorro_match_device(drv->id_table, z);
53 if (id)
54 error = drv->probe(z, id);
55 if (error >= 0) {
56 z->driver = drv;
57 error = 0;
58 }
59 }
60 return error;
61}
62
63
Geert Uytterhoeven2f9b0b52007-11-18 11:10:04 +010064static int zorro_device_remove(struct device *dev)
65{
66 struct zorro_dev *z = to_zorro_dev(dev);
67 struct zorro_driver *drv = to_zorro_driver(dev->driver);
68
69 if (drv) {
70 if (drv->remove)
71 drv->remove(z);
72 z->driver = NULL;
73 }
74 return 0;
75}
76
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 /**
79 * zorro_register_driver - register a new Zorro driver
80 * @drv: the driver structure to register
81 *
82 * Adds the driver structure to the list of registered drivers
Bjorn Helgaas33d86752006-03-25 03:07:20 -080083 * Returns zero or a negative error value.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85
86int zorro_register_driver(struct zorro_driver *drv)
87{
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 /* initialize common driver fields */
89 drv->driver.name = drv->name;
90 drv->driver.bus = &zorro_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 /* register with core */
Bjorn Helgaas33d86752006-03-25 03:07:20 -080093 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Jim Rotmalmd996e9d2011-07-17 23:12:30 +020095EXPORT_SYMBOL(zorro_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97
98 /**
99 * zorro_unregister_driver - unregister a zorro driver
100 * @drv: the driver structure to unregister
101 *
102 * Deletes the driver structure from the list of registered Zorro drivers,
103 * gives it a chance to clean up by calling its remove() function for
104 * each device it was responsible for, and marks those devices as
105 * driverless.
106 */
107
108void zorro_unregister_driver(struct zorro_driver *drv)
109{
110 driver_unregister(&drv->driver);
111}
Jim Rotmalmd996e9d2011-07-17 23:12:30 +0200112EXPORT_SYMBOL(zorro_unregister_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114
115 /**
116 * zorro_bus_match - Tell if a Zorro device structure has a matching Zorro
117 * device id structure
118 * @ids: array of Zorro device id structures to search in
119 * @dev: the Zorro device structure to match against
120 *
121 * Used by a driver to check whether a Zorro device present in the
122 * system is in its list of supported devices.Returns the matching
123 * zorro_device_id structure or %NULL if there is no match.
124 */
125
126static int zorro_bus_match(struct device *dev, struct device_driver *drv)
127{
128 struct zorro_dev *z = to_zorro_dev(dev);
129 struct zorro_driver *zorro_drv = to_zorro_driver(drv);
130 const struct zorro_device_id *ids = zorro_drv->id_table;
131
132 if (!ids)
133 return 0;
134
135 while (ids->id) {
136 if (ids->id == ZORRO_WILDCARD || ids->id == z->id)
137 return 1;
138 ids++;
139 }
140 return 0;
141}
142
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100143static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
144{
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100145 struct zorro_dev *z;
146
147 if (!dev)
148 return -ENODEV;
149
150 z = to_zorro_dev(dev);
151 if (!z)
152 return -ENODEV;
153
154 if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
155 add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
156 add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
157 add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
158 return -ENOMEM;
159
160 return 0;
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100161}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163struct bus_type zorro_bus_type = {
164 .name = "zorro",
Russell Kingb6a01e92006-01-05 14:43:43 +0000165 .match = zorro_bus_match,
Geert Uytterhoevenbf54a2b2008-11-18 21:13:53 +0100166 .uevent = zorro_uevent,
Russell Kingb6a01e92006-01-05 14:43:43 +0000167 .probe = zorro_device_probe,
Geert Uytterhoeven2f9b0b52007-11-18 11:10:04 +0100168 .remove = zorro_device_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169};
Jim Rotmalmd996e9d2011-07-17 23:12:30 +0200170EXPORT_SYMBOL(zorro_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172
173static int __init zorro_driver_init(void)
174{
175 return bus_register(&zorro_bus_type);
176}
177
178postcore_initcall(zorro_driver_init);
179