Merge from vendor branch READLINE:
[dragonfly.git] / sys / bus / isa / i386 / isa_compat.c
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  * $FreeBSD: src/sys/i386/isa/isa_compat.c,v 1.18.2.1 2001/05/17 23:05:06 imp Exp $
27  * $DragonFly: src/sys/bus/isa/i386/isa_compat.c,v 1.8 2004/04/01 07:33:18 joerg Exp $
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/bus.h>
34 #include <sys/malloc.h>
35 #include <sys/module.h>
36 #include <machine/bus.h>
37 #include <sys/rman.h>
38
39 #include <machine/vmparam.h>
40 #include <vm/vm.h>
41 #include <vm/pmap.h>
42 #include <machine/pmap.h>
43 #include <machine/md_var.h>
44
45 #include <machine/resource.h>
46 #include "../isavar.h"
47 #include "isa_compat.h"
48 #include "isa_device.h"
49
50 struct isa_compat_resources {
51     struct resource *ports;
52     struct resource *memory;
53     struct resource *drq;
54     struct resource *irq;
55 };
56
57 struct isa_compat_driver {
58         driver_t driver;
59         struct old_isa_driver *op;
60 };
61
62 int
63 isa_compat_nextid(void)
64 {
65         static int id = 2;      /* id_id of -1, 0 and 1 are "used" */
66
67         return id++;
68 }
69
70 static void
71 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
72 {
73         int rid;
74         u_long start, count;
75
76         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
77                              &start, &count) == 0) {
78                 rid = 0;
79                 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
80                                                 &rid, 0ul, ~0ul, 1,
81                                                 RF_ACTIVE);
82                 if (res->ports == NULL && bootverbose)
83                         printf("isa_compat: didn't get ports for %s\n",
84                                device_get_name(dev));
85         } else
86                 res->ports = 0;
87
88         if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
89                              &start, &count) == 0
90             && start != 0) {
91                 rid = 0;
92                 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
93                                                  &rid, 0ul, ~0ul, 1,
94                                                  RF_ACTIVE);
95                 if (res->memory == NULL && bootverbose)
96                         printf("isa_compat: didn't get memory for %s\n",
97                                device_get_name(dev));
98         } else
99                 res->memory = 0;
100
101         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
102                              &start, &count) == 0) {
103                 rid = 0;
104                 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
105                                               &rid, 0ul, ~0ul, 1,
106                                               RF_ACTIVE);
107                 if (res->drq == NULL && bootverbose)
108                         printf("isa_compat: didn't get drq for %s\n",
109                                device_get_name(dev));
110         } else
111                 res->drq = 0;
112
113         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
114                              &start, &count) == 0) {
115                 rid = 0;
116                 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
117                                               &rid, 0ul, ~0ul, 1,
118                                               RF_SHAREABLE | RF_ACTIVE);
119                 if (res->irq == NULL && bootverbose)
120                         printf("isa_compat: didn't get irq for %s\n",
121                                device_get_name(dev));
122         } else
123                 res->irq = 0;
124 }
125
126 static void
127 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
128 {
129         if (res->ports) {
130                 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
131                 res->ports = 0;
132         }
133         if (res->memory) {
134                 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
135                 res->memory = 0;
136         }
137         if (res->drq) {
138                 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
139                 res->drq = 0;
140         }
141         if (res->irq) {
142                 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
143                 res->irq = 0;
144         }
145 }
146
147 #define irqmask(x)      ((x) < 0 ? 0 : (1 << (x)))
148
149 static int
150 isa_compat_probe(device_t dev)
151 {
152         struct isa_device *dvp = device_get_softc(dev);
153         struct isa_compat_resources res;
154         struct old_isa_driver *op;
155         u_long start, count;
156
157         /* No pnp support */
158         if (isa_get_vendorid(dev))
159                 return (ENXIO);
160
161         bzero(&res, sizeof(res));
162         /*
163          * Fill in the isa_device fields.
164          */
165         op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
166         dvp->id_id = isa_compat_nextid();
167         dvp->id_driver = op->driver;
168         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
169                              &start, &count) == 0)
170                 dvp->id_iobase = start;
171         else
172                 dvp->id_iobase = -1;
173         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
174                              &start, &count) == 0)
175                 dvp->id_irq = irqmask(start);
176         else
177                 dvp->id_irq = 0;
178         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
179                              &start, &count) == 0)
180                 dvp->id_drq = start;
181         else
182                 dvp->id_drq = -1;
183         if (bus_get_resource(dev, SYS_RES_MEMORY,
184                              0, &start, &count) == 0) {
185                 dvp->id_maddr = (void *)(uintptr_t)start;
186                 dvp->id_msize = count;
187         } else {
188                 dvp->id_maddr = NULL;
189                 dvp->id_msize = 0;
190         }
191         dvp->id_unit = device_get_unit(dev);
192         dvp->id_flags = device_get_flags(dev);
193         dvp->id_enabled = device_is_enabled(dev);       /* XXX unused */
194         dvp->id_device = dev;
195
196         /*
197          * Do the wrapped probe.
198          */
199         if (dvp->id_driver->probe) {
200                 int portsize;
201                 void *maddr;
202                 struct isa_device old;
203
204                 isa_compat_alloc_resources(dev, &res);
205                 if (res.memory)
206                         maddr = rman_get_virtual(res.memory);
207                 else
208                         maddr = 0;
209                 dvp->id_maddr = maddr;
210                 old = *dvp;
211                 portsize = dvp->id_driver->probe(dvp);
212                 isa_compat_release_resources(dev, &res);
213                 if (portsize != 0) {
214                         if (portsize > 0 || dvp->id_iobase != old.id_iobase)
215                                 bus_set_resource(dev, SYS_RES_IOPORT,
216                                                  0, dvp->id_iobase, portsize);
217                         if (dvp->id_irq != old.id_irq)
218                                 bus_set_resource(dev, SYS_RES_IRQ, 0,
219                                                  ffs(dvp->id_irq) - 1, 1);
220                         if (dvp->id_drq != old.id_drq)
221                                 bus_set_resource(dev, SYS_RES_DRQ, 0,
222                                                  dvp->id_drq, 1);
223                         if (dvp->id_maddr != old.id_maddr
224                             || dvp->id_msize != old.id_msize) {
225                                 maddr = dvp->id_maddr;
226                                 if (maddr != NULL)
227                                         bus_set_resource(dev,
228                                                          SYS_RES_MEMORY,
229                                                          0,
230                                                          kvtop(maddr),
231                                                          dvp->id_msize);
232                                 else
233                                         bus_delete_resource(dev,
234                                                             SYS_RES_MEMORY,
235                                                             0);
236                         }
237                         return 0;
238                 }
239         }
240         return ENXIO;
241 }
242
243 static int
244 isa_compat_attach(device_t dev)
245 {
246         struct isa_device *dvp = device_get_softc(dev);
247         struct isa_compat_resources res;
248         int error;
249
250         bzero(&res, sizeof(res));
251         isa_compat_alloc_resources(dev, &res);
252         if (dvp->id_driver->attach)
253                 dvp->id_driver->attach(dvp);
254         if (res.irq && dvp->id_irq && dvp->id_intr) {
255                 struct old_isa_driver *op;
256                 void *ih;
257
258                 op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
259                 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
260                                        res.irq, op->type,
261                                        dvp->id_intr,
262                                        (void *)(uintptr_t)dvp->id_unit,
263                                        &ih);
264                 if (error)
265                         printf("isa_compat_attach: failed to setup intr: %d\n",
266                                error);
267         }
268         device_printf(dev, "driver is using old-style compatibility shims\n");
269         return 0;
270 }
271
272 static device_method_t isa_compat_methods[] = {
273         /* Device interface */
274         DEVMETHOD(device_probe,         isa_compat_probe),
275         DEVMETHOD(device_attach,        isa_compat_attach),
276
277         { 0, 0 }
278 };
279
280 /*
281  * Create a new style driver around each old isa driver.
282  */
283 void
284 isa_wrap_old_drivers(void)
285 {
286         int i;
287         struct old_isa_driver *op;
288         devclass_t isa_devclass = devclass_find("isa");
289         struct isa_compat_driver *driver;
290
291         for (i = 0, op = &old_drivers[0]; i < old_drivers_count; i++, op++) {
292                 driver = malloc(sizeof(struct isa_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
293                 driver->driver.name = op->driver->name;
294                 driver->driver.methods = isa_compat_methods;
295                 driver->driver.size = sizeof(struct isa_device);
296                 driver->op = op;
297                 if (op->driver->sensitive_hw)
298                         resource_set_int(op->driver->name, -1, "sensitive", 1);
299                 devclass_add_driver(isa_devclass, (driver_t *)driver);
300         }
301 }