xref: /openbmc/linux/drivers/zorro/zorro-driver.c (revision fc7a6209)
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 
17 #include "zorro.h"
18 
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 
31 static const struct zorro_device_id *
32 zorro_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 
44 static 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 
65 static void zorro_device_remove(struct device *dev)
66 {
67 	struct zorro_dev *z = to_zorro_dev(dev);
68 	struct zorro_driver *drv = to_zorro_driver(dev->driver);
69 
70 	if (drv) {
71 		if (drv->remove)
72 			drv->remove(z);
73 		z->driver = NULL;
74 	}
75 }
76 
77 
78     /**
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
83      *  Returns zero or a negative error value.
84      */
85 
86 int zorro_register_driver(struct zorro_driver *drv)
87 {
88 	/* initialize common driver fields */
89 	drv->driver.name = drv->name;
90 	drv->driver.bus = &zorro_bus_type;
91 
92 	/* register with core */
93 	return driver_register(&drv->driver);
94 }
95 EXPORT_SYMBOL(zorro_register_driver);
96 
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 
108 void zorro_unregister_driver(struct zorro_driver *drv)
109 {
110 	driver_unregister(&drv->driver);
111 }
112 EXPORT_SYMBOL(zorro_unregister_driver);
113 
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 the driver core to check whether a Zorro device present in the
122      *  system is in a driver's list of supported devices.  Returns 1 if
123      *  supported, and 0 if there is no match.
124      */
125 
126 static 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 	return !!zorro_match_device(ids, z);
136 }
137 
138 static int zorro_uevent(struct device *dev, struct kobj_uevent_env *env)
139 {
140 	struct zorro_dev *z;
141 
142 	if (!dev)
143 		return -ENODEV;
144 
145 	z = to_zorro_dev(dev);
146 	if (!z)
147 		return -ENODEV;
148 
149 	if (add_uevent_var(env, "ZORRO_ID=%08X", z->id) ||
150 	    add_uevent_var(env, "ZORRO_SLOT_NAME=%s", dev_name(dev)) ||
151 	    add_uevent_var(env, "ZORRO_SLOT_ADDR=%04X", z->slotaddr) ||
152 	    add_uevent_var(env, "MODALIAS=" ZORRO_DEVICE_MODALIAS_FMT, z->id))
153 		return -ENOMEM;
154 
155 	return 0;
156 }
157 
158 struct bus_type zorro_bus_type = {
159 	.name		= "zorro",
160 	.dev_name	= "zorro",
161 	.dev_groups	= zorro_device_attribute_groups,
162 	.match		= zorro_bus_match,
163 	.uevent		= zorro_uevent,
164 	.probe		= zorro_device_probe,
165 	.remove		= zorro_device_remove,
166 };
167 EXPORT_SYMBOL(zorro_bus_type);
168 
169 
170 static int __init zorro_driver_init(void)
171 {
172 	return bus_register(&zorro_bus_type);
173 }
174 
175 postcore_initcall(zorro_driver_init);
176 
177