Merge branch 'vendor/BZIP'
[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.12 2006/12/22 23:12:16 swildner 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 <sys/rman.h>
37
38 #include <machine/vmparam.h>
39 #include <vm/vm.h>
40 #include <vm/pmap.h>
41 #include <machine/pmap.h>
42 #include <machine/md_var.h>
43
44 #include "../isavar.h"
45 #include "isa_compat.h"
46 #include "isa_device.h"
47
48 struct isa_compat_resources {
49     struct resource *ports;
50     struct resource *memory;
51     struct resource *drq;
52     struct resource *irq;
53 };
54
55 struct isa_compat_driver {
56         driver_t driver;
57         struct old_isa_driver *op;
58 };
59
60 int
61 isa_compat_nextid(void)
62 {
63         static int id = 2;      /* id_id of -1, 0 and 1 are "used" */
64
65         return id++;
66 }
67
68 static void
69 isa_compat_alloc_resources(device_t dev, struct isa_compat_resources *res)
70 {
71         int rid;
72         u_long start, count;
73
74         if (bus_get_resource(dev, SYS_RES_IOPORT, 0,
75                              &start, &count) == 0) {
76                 rid = 0;
77                 res->ports = bus_alloc_resource(dev, SYS_RES_IOPORT,
78                                                 &rid, 0ul, ~0ul, 1,
79                                                 RF_ACTIVE);
80                 if (res->ports == NULL && bootverbose)
81                         kprintf("isa_compat: didn't get ports for %s\n",
82                                device_get_name(dev));
83         } else
84                 res->ports = 0;
85
86         if (bus_get_resource(dev, SYS_RES_MEMORY, 0,
87                              &start, &count) == 0
88             && start != 0) {
89                 rid = 0;
90                 res->memory = bus_alloc_resource(dev, SYS_RES_MEMORY,
91                                                  &rid, 0ul, ~0ul, 1,
92                                                  RF_ACTIVE);
93                 if (res->memory == NULL && bootverbose)
94                         kprintf("isa_compat: didn't get memory for %s\n",
95                                device_get_name(dev));
96         } else
97                 res->memory = 0;
98
99         if (bus_get_resource(dev, SYS_RES_DRQ, 0,
100                              &start, &count) == 0) {
101                 rid = 0;
102                 res->drq = bus_alloc_resource(dev, SYS_RES_DRQ,
103                                               &rid, 0ul, ~0ul, 1,
104                                               RF_ACTIVE);
105                 if (res->drq == NULL && bootverbose)
106                         kprintf("isa_compat: didn't get drq for %s\n",
107                                device_get_name(dev));
108         } else
109                 res->drq = 0;
110
111         if (bus_get_resource(dev, SYS_RES_IRQ, 0,
112                              &start, &count) == 0) {
113                 rid = 0;
114                 res->irq = bus_alloc_resource(dev, SYS_RES_IRQ,
115                                               &rid, 0ul, ~0ul, 1,
116                                               RF_SHAREABLE | RF_ACTIVE);
117                 if (res->irq == NULL && bootverbose)
118                         kprintf("isa_compat: didn't get irq for %s\n",
119                                device_get_name(dev));
120         } else
121                 res->irq = 0;
122 }
123
124 static void
125 isa_compat_release_resources(device_t dev, struct isa_compat_resources *res)
126 {
127         if (res->ports) {
128                 bus_release_resource(dev, SYS_RES_IOPORT, 0, res->ports);
129                 res->ports = 0;
130         }
131         if (res->memory) {
132                 bus_release_resource(dev, SYS_RES_MEMORY, 0, res->memory);
133                 res->memory = 0;
134         }
135         if (res->drq) {
136                 bus_release_resource(dev, SYS_RES_DRQ, 0, res->drq);
137                 res->drq = 0;
138         }
139         if (res->irq) {
140                 bus_release_resource(dev, SYS_RES_IRQ, 0, res->irq);
141                 res->irq = 0;
142         }
143 }
144
145 #define irqmask(x)      ((x) < 0 ? 0 : (1 << (x)))
146
147 static int
148 isa_compat_probe(device_t dev)
149 {
150         struct isa_device *dvp = device_get_softc(dev);
151         struct isa_compat_resources res;
152         struct old_isa_driver *op;
153         u_long start, count;
154
155         /* No pnp support */
156         if (isa_get_vendorid(dev))
157                 return (ENXIO);
158
159         bzero(&res, sizeof(res));
160         /*
161          * Fill in the isa_device fields.
162          */
163         op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
164         dvp->id_id = isa_compat_nextid();
165         dvp->id_driver = op->driver;
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 int
242 isa_compat_attach(device_t dev)
243 {
244         struct isa_device *dvp = device_get_softc(dev);
245         struct isa_compat_resources res;
246         int error;
247
248         bzero(&res, sizeof(res));
249         isa_compat_alloc_resources(dev, &res);
250         if (dvp->id_driver->attach)
251                 dvp->id_driver->attach(dvp);
252         if (res.irq && dvp->id_irq && dvp->id_intr) {
253                 struct old_isa_driver *op;
254                 void *ih;
255
256                 op = ((struct isa_compat_driver *)device_get_driver(dev))->op;
257                 error = BUS_SETUP_INTR(device_get_parent(dev), dev,
258                                        res.irq, op->type,
259                                        dvp->id_intr,
260                                        (void *)(uintptr_t)dvp->id_unit,
261                                        &ih, NULL);
262                 if (error)
263                         kprintf("isa_compat_attach: failed to setup intr: %d\n",
264                                error);
265         }
266         device_printf(dev, "driver is using old-style compatibility shims\n");
267         return 0;
268 }
269
270 static device_method_t isa_compat_methods[] = {
271         /* Device interface */
272         DEVMETHOD(device_probe,         isa_compat_probe),
273         DEVMETHOD(device_attach,        isa_compat_attach),
274
275         { 0, 0 }
276 };
277
278 /*
279  * Create a new style driver around each old isa driver.
280  */
281 void
282 isa_wrap_old_drivers(void)
283 {
284         int i;
285         struct old_isa_driver *op;
286         devclass_t isa_devclass = devclass_find("isa");
287         struct isa_compat_driver *driver;
288
289         for (i = 0, op = &old_drivers[0]; i < old_drivers_count; i++, op++) {
290                 driver = kmalloc(sizeof(struct isa_compat_driver), M_DEVBUF, M_WAITOK | M_ZERO);
291                 driver->driver.name = op->driver->name;
292                 driver->driver.methods = isa_compat_methods;
293                 driver->driver.size = sizeof(struct isa_device);
294                 driver->op = op;
295                 if (op->driver->sensitive_hw)
296                         resource_set_int(op->driver->name, -1, "sensitive", 1);
297                 devclass_add_driver(isa_devclass, (driver_t *)driver);
298         }
299 }