1 /*-
2 * Copyright (c) 1998 Doug Rabson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 */
26
27 #ifndef BURN_BRIDGES
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/5.2/sys/i386/isa/isa_compat.c 122063 2003-11-04 19:04:54Z jhb $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <machine/bus.h>
39 #include <sys/rman.h>
40
41 #include <machine/vmparam.h>
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44 #include <machine/pmap.h>
45 #include <machine/md_var.h>
46
47 #include <machine/resource.h>
48 #include <isa/isavar.h>
49 #include <i386/isa/isa_device.h>
50
51 /*
52 * The 'priv' field has been removed from 'struct driver' since the
53 * only remaining user of that field was this compatibility layer. We
54 * use this field to map from the newbus driver stub to the underlying
55 * old-style isa driver.
56 */
57 struct isa_compat_driver {
58 KOBJ_CLASS_FIELDS;
59 void *priv;
60 };
61
62 struct isa_compat_resources {
63 struct resource *ports;
64 struct resource *memory;
65 struct resource *drq;
66 struct resource *irq;
67 };
68
69 static void
70 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
71 {
72 int rid;
73 u_long start, count;
74
75 if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
76 &start, &count) == 0) {
77 rid = 0;
78 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
79 &rid, 0ul, ~0ul, 1,
80 RF_ACTIVE);
81 if (res->ports == NULL && bootverbose)
82 printf("isa_compat: didn't get ports for %s\n",
83 device_get_name(dev));
84 } else
85 res->ports = 0;
86
87 if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
88 &start, &count) == 0
89 && start != 0) {
90 rid = 0;
91 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
92 &rid, 0ul, ~0ul, 1,
93 RF_ACTIVE);
94 if (res->memory == NULL && bootverbose)
95 printf("isa_compat: didn't get memory for %s\n",
96 device_get_name(dev));
97 } else
98 res->memory = 0;
99
100 if (bus_get_resource(dev, SYS_RES_DRQ, 0,
101 &start, &count) == 0) {
102 rid = 0;
103 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
104 &rid, 0ul, ~0ul, 1,
105 RF_ACTIVE);
106 if (res->drq == NULL && bootverbose)
107 printf("isa_compat: didn't get drq for %s\n",
108 device_get_name(dev));
109 } else
110 res->drq = 0;
111
112 if (bus_get_resource(dev, SYS_RES_IRQ, 0,
113 &start, &count) == 0) {
114 rid = 0;
115 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
116 &rid, 0ul, ~0ul, 1,
117 RF_SHAREABLE | RF_ACTIVE);
118 if (res->irq == NULL && bootverbose)
119 printf("isa_compat: didn't get irq for %s\n",
120 device_get_name(dev));
121 } else
122 res->irq = 0;
123 }
124
125 static void
126 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
127 {
128 if (res->ports) {
129 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
130 res->ports = 0;
131 }
132 if (res->memory) {
133 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
134 res->memory = 0;
135 }
136 if (res->drq) {
137 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
138 res->drq = 0;
139 }
140 if (res->irq) {
141 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
142 res->irq = 0;
143 }
144 }
145
146 #define irqmask(x) ((x) < 0 ? 0 : (1 << (x)))
147
148 static int
149 isa_compat_probe(device_t dev)
150 {
151 struct isa_compat_driver *drv;
152 struct isa_device *dvp = device_get_softc(dev);
153 struct isa_compat_resources res;
154 u_long start, count;
155
156 /* No pnp support */
157 if (isa_get_vendorid(dev))
158 return (ENXIO);
159
160 bzero(&res, sizeof(res));
161 /*
162 * Fill in the isa_device fields.
163 */
164 drv = (struct isa_compat_driver *) device_get_driver(dev);
165 dvp->id_driver = drv->priv;
166 if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
167 &start, &count) == 0)
168 dvp->id_iobase = start;
169 else
170 dvp->id_iobase = -1;
171 if (bus_get_resource(dev, SYS_RES_IRQ, 0,
172 &start, &count) == 0)
173 dvp->id_irq = irqmask(start);
174 else
175 dvp->id_irq = 0;
176 if (bus_get_resource(dev, SYS_RES_DRQ, 0,
177 &start, &count) == 0)
178 dvp->id_drq = start;
179 else
180 dvp->id_drq = -1;
181 if (bus_get_resource(dev, SYS_RES_MEMORY,
182 0, &start, &count) == 0) {
183 dvp->id_maddr = (void *)(uintptr_t)start;
184 dvp->id_msize = count;
185 } else {
186 dvp->id_maddr = NULL;
187 dvp->id_msize = 0;
188 }
189 dvp->id_unit = device_get_unit(dev);
190 dvp->id_flags = device_get_flags(dev);
191 dvp->id_enabled = device_is_enabled(dev); /* XXX unused */
192 dvp->id_device = dev;
193
194 /*
195 * Do the wrapped probe.
196 */
197 if (dvp->id_driver->probe) {
198 int portsize;
199 void *maddr;
200 struct isa_device old;
201
202 isa_compat_alloc_resources(dev, &res);
203 if (res.memory)
204 maddr = rman_get_virtual(res.memory);
205 else
206 maddr = 0;
207 dvp->id_maddr = maddr;
208 old = *dvp;
209 portsize = dvp->id_driver->probe(dvp);
210 isa_compat_release_resources(dev, &res);
211 if (portsize != 0) {
212 if (portsize > 0 || dvp->id_iobase != old.id_iobase)
213 bus_set_resource(dev, SYS_RES_IOPORT,
214 0, dvp->id_iobase, portsize);
215 if (dvp->id_irq != old.id_irq)
216 bus_set_resource(dev, SYS_RES_IRQ, 0,
217 ffs(dvp->id_irq) - 1, 1);
218 if (dvp->id_drq != old.id_drq)
219 bus_set_resource(dev, SYS_RES_DRQ, 0,
220 dvp->id_drq, 1);
221 if (dvp->id_maddr != old.id_maddr
222 || dvp->id_msize != old.id_msize) {
223 maddr = dvp->id_maddr;
224 if (maddr != NULL)
225 bus_set_resource(dev,
226 SYS_RES_MEMORY,
227 0,
228 kvtop(maddr),
229 dvp->id_msize);
230 else
231 bus_delete_resource(dev,
232 SYS_RES_MEMORY,
233 0);
234 }
235 return 0;
236 }
237 }
238 return ENXIO;
239 }
240
241 static void
242 isa_compat_intr(void *arg)
243 {
244 struct isa_device *dvp;
245
246 dvp = (struct isa_device *)arg;
247 dvp->id_ointr(dvp->id_unit);
248 }
249
250 static int
251 isa_compat_attach(device_t dev)
252 {
253 struct isa_device *dvp = device_get_softc(dev);
254 struct isa_compat_resources res;
255 int error;
256
257 bzero(&res, sizeof(res));
258 isa_compat_alloc_resources(dev, &res);
259 if (dvp->id_driver->attach)
260 dvp->id_driver->attach(dvp);
261 if (res.irq && dvp->id_irq && dvp->id_intr) {
262 void *ih;
263
264 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
265 res.irq, dvp->id_driver->intrflags,
266 isa_compat_intr, dvp, &ih);
267 if (error)
268 printf("isa_compat_attach: failed to setup intr: %d\n",
269 error);
270 }
271 device_printf(dev, "driver is using old-style compatibility shims\n");
272 return 0;
273 }
274
275 static device_method_t isa_compat_methods[] = {
276 /* Device interface */
277 DEVMETHOD(device_probe, isa_compat_probe),
278 DEVMETHOD(device_attach, isa_compat_attach),
279
280 { 0, 0 }
281 };
282
283 /*
284 * Create a new style driver around each old isa driver.
285 */
286 int
287 compat_isa_handler(module_t mod, int type, void *data)
288 {
289 struct isa_driver *id = (struct isa_driver *)data;
290 struct isa_compat_driver *driver;
291 devclass_t isa_devclass = devclass_find("isa");
292
293 switch (type) {
294 case MOD_LOAD:
295 driver = malloc(sizeof(struct isa_compat_driver),
296 M_DEVBUF, M_NOWAIT | M_ZERO);
297 if (!driver)
298 return ENOMEM;
299 driver->name = id->name;
300 driver->methods = isa_compat_methods;
301 driver->size = sizeof(struct isa_device);
302 driver->priv = id;
303 if (id->sensitive_hw) {
304 #if 0
305 resource_set_int(id->name, -1, "sensitive", 1);
306 #else
307 printf("WARNING: isa driver %s is sensitive, but cannot set it!\n",
308 driver->name);
309 #endif
310 }
311 devclass_add_driver(isa_devclass, (kobj_class_t) driver);
312 break;
313 case MOD_UNLOAD:
314 printf("%s: module unload not supported!\n", id->name);
315 return EOPNOTSUPP;
316 default:
317 break;
318 }
319 return 0;
320 }
321
322 #else
323 #error "cvs rm sys/i386/isa/isa_compat.c"
324 #endif
Cache object: 63140f9c6e0ed203b521801cc19e05a2
|