pnpbios.c revision 1.9 1 /* $NetBSD: pnpbios.c,v 1.9 2000/01/16 03:30:23 drochner Exp $ */
2 /*
3 * Copyright (c) 1999
4 * Matthias Drochner. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/param.h>
29 #include <sys/systm.h>
30 #include <sys/device.h>
31 #include <sys/malloc.h>
32 #include <dev/isa/isareg.h>
33 #include <machine/isa_machdep.h>
34 #include <machine/segments.h>
35 #include <vm/vm.h>
36 #include <vm/vm_kern.h>
37
38 #include <arch/i386/pnpbios/pnpbiosvar.h>
39
40 #include "opt_pnpbiosverbose.h"
41 #include "isadma.h"
42 #include "locators.h"
43
44 struct pnpbios_softc {
45 struct device sc_dev;
46 isa_chipset_tag_t sc_ic;
47 };
48
49 static caddr_t pnpbios_find __P((void));
50 static int pnpbios_match __P((struct device *, struct cfdata *, void *));
51 static void pnpbios_attach __P((struct device *, struct device *, void *));
52 static void pnpbios_printres __P((struct pnpresources *));
53 static int pnpbios_print __P((void *, const char *));
54 static int pnpbios_getnumnodes __P((int *, size_t *));
55 static int pnpbios_getnode __P((int, int *, unsigned char *, size_t));
56 static void eisaid_to_string __P((unsigned char *, char *));
57 static void pnpbios_attachnode __P((struct pnpbios_softc *, int,
58 unsigned char *, size_t));
59 static int pnp_scan __P((unsigned char **, size_t, struct pnpresources *, int));
60
61 static int pnpbios_submatch __P((struct device *, struct cfdata *, void *));
62
63 extern int pnpbioscall __P((int));
64
65 struct cfattach pnpbios_ca = {
66 sizeof(struct pnpbios_softc), pnpbios_match, pnpbios_attach
67 };
68
69 /*
70 * Private stack and return value buffer. Spec (1.0a, ch. 4.3) says that
71 * 1024 bytes must be available to the BIOS function.
72 */
73 #define PNPBIOS_BUFSIZE 4096
74
75 int pnpbios_enabled = 1;
76 size_t pnpbios_entry;
77 caddr_t pnpbios_scratchbuf;
78
79 /*
80 * There can be only one of these, and the i386 ISA code needs to
81 * reference this.
82 */
83 struct pnpbios_softc *pnpbios_softc;
84
85 #define PNPBIOS_SIGNATURE ('$' | ('P' << 8) | ('n' << 16) | ('P' << 24))
86
87 static caddr_t
88 pnpbios_find()
89 {
90 caddr_t p, c;
91 unsigned char cksum;
92 size_t structlen;
93
94 for (p = (caddr_t)ISA_HOLE_VADDR(0xf0000);
95 p <= (caddr_t)ISA_HOLE_VADDR(0xffff0);
96 p += 16) {
97 if (*(int *)p != PNPBIOS_SIGNATURE)
98 continue;
99 structlen = *(unsigned char *)(p + 5);
100 if ((structlen < 0x21) ||
101 ((p + structlen - 1) > (caddr_t)ISA_HOLE_VADDR(0xfffff)))
102 continue;
103
104 cksum = 0;
105 for (c = p; c < p + structlen; c++)
106 cksum += *(unsigned char *)c;
107 if (cksum != 0)
108 continue;
109
110 if (*(char *)(p + 4) != 0x10) {
111 printf("unknown version %x\n", *(char *)(p + 4));
112 continue;
113 }
114
115 return (p);
116 }
117
118 return (0);
119 }
120
121 int
122 pnpbios_probe()
123 {
124
125 return (pnpbios_find() != 0);
126 }
127
128 static int
129 pnpbios_match(parent, match, aux)
130 struct device *parent;
131 struct cfdata *match;
132 void *aux;
133 {
134 struct pnpbios_attach_args *paa = aux;
135
136 /* These are not the droids you're looking for. */
137 if (strcmp(paa->paa_busname, "pnpbios") != 0)
138 return (0);
139
140 /* There can be only one! */
141 if (pnpbios_softc != NULL)
142 return (0);
143
144 return (pnpbios_enabled);
145 }
146
147 static caddr_t mapit __P((u_long, u_long, int));
148
149 static caddr_t
150 mapit(addr, len, prot)
151 u_long addr, len;
152 int prot;
153 {
154 u_long startpa, pa, endpa;
155 vaddr_t startva, va;
156
157 pa = startpa = i386_trunc_page(addr);
158 endpa = i386_round_page(addr + len);
159
160 va = startva = uvm_km_valloc(kernel_map, endpa - startpa);
161 if (!startva)
162 return (0);
163 for (; pa < endpa; pa += NBPG, va += NBPG)
164 pmap_kenter_pa(va, pa, prot);
165
166 return ((caddr_t)(startva + (addr - startpa)));
167 }
168
169 static void
170 pnpbios_attach(parent, self, aux)
171 struct device *parent, *self;
172 void *aux;
173 {
174 struct pnpbios_softc *sc = (struct pnpbios_softc *)self;
175 struct pnpbios_attach_args *paa = aux;
176 caddr_t p;
177 unsigned int codepbase, datapbase;
178 caddr_t codeva, datava;
179 extern char pnpbiostramp[], epnpbiostramp[];
180 int res, num, i, size, idx;
181 unsigned char *buf;
182
183 pnpbios_softc = sc;
184 sc->sc_ic = paa->paa_ic;
185
186 #if NISADMA > 0
187 isa_dmainit(sc->sc_ic, I386_BUS_SPACE_IO, &isa_bus_dma_tag, self);
188 #endif
189
190 p = pnpbios_find();
191 if (!p)
192 panic("pnpbios_attach: disappeared");
193
194 codepbase = *(unsigned int *)(p + 0x13);
195 datapbase = *(unsigned int *)(p + 0x1d);
196 pnpbios_entry = *(unsigned short *)(p + 0x11);
197
198 #ifdef PNPBIOSVERBOSE
199 printf(": code %x, data %x, entry %x\n%s",
200 codepbase, datapbase, pnpbios_entry, self->dv_xname);
201 #endif
202
203 codeva = mapit(codepbase, 0x10000,
204 VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
205 datava = mapit(datapbase, 0x10000,
206 VM_PROT_READ | VM_PROT_WRITE);
207 if (codeva == 0 || datava == 0) {
208 printf("no vm for mapping\n");
209 return;
210 }
211 pnpbios_scratchbuf = malloc(PNPBIOS_BUFSIZE, M_DEVBUF, M_NOWAIT);
212
213 setsegment(&gdt[GPNPBIOSCODE_SEL].sd, codeva, 0xffff,
214 SDT_MEMERA, SEL_KPL, 0, 0);
215 setsegment(&gdt[GPNPBIOSDATA_SEL].sd, datava, 0xffff,
216 SDT_MEMRWA, SEL_KPL, 0, 0);
217 setsegment(&gdt[GPNPBIOSSCRATCH_SEL].sd,
218 pnpbios_scratchbuf, PNPBIOS_BUFSIZE - 1,
219 SDT_MEMRWA, SEL_KPL, 0, 0);
220 setsegment(&gdt[GPNPBIOSTRAMP_SEL].sd,
221 pnpbiostramp, epnpbiostramp - pnpbiostramp - 1,
222 SDT_MEMERA, SEL_KPL, 1, 0);
223
224 res = pnpbios_getnumnodes(&num, &size);
225 if (res) {
226 printf("pnpbios_getnumnodes: error %d\n", res);
227 return;
228 }
229
230 printf(": %d nodes, max len %d\n", num, size);
231 buf = malloc(size, M_DEVBUF, M_NOWAIT);
232
233 idx = 0;
234 for (i = 0; i < num && idx != 0xff; i++) {
235 int node = idx;
236 res = pnpbios_getnode(1, &idx, buf, size);
237 if (res) {
238 printf("pnpbios_getnode: error %d\n", res);
239 continue;
240 }
241 if (buf[2] != node)
242 printf("node idx: called %d, got %d\n", node, buf[2]);
243 pnpbios_attachnode(sc, node, buf, buf[0] + (buf[1] << 8));
244 }
245 if (i != num)
246 printf("got only %d nodes\n", i);
247 if (idx != 0xff)
248 printf("last idx=%x\n", idx);
249
250 free(buf, M_DEVBUF);
251 }
252
253 static int
254 pnpbios_getnumnodes(nump, sizep)
255 int *nump;
256 size_t *sizep;
257 {
258 int res;
259 short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
260
261 *--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
262 *--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
263 *--help = 2; /* buffer offset for node size */
264 *--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
265 *--help = 0; /* buffer offset for numnodes */
266 *--help = 0; /* GET_NUM_NODES */
267
268 res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
269 if (res)
270 return (res);
271
272 *nump = *(short *)(pnpbios_scratchbuf + 0);
273 *sizep = *(short *)(pnpbios_scratchbuf + 2);
274 return (0);
275 }
276
277 static int
278 pnpbios_getnode(flags, idxp, buf, len)
279 int flags;
280 int *idxp;
281 unsigned char *buf;
282 size_t len;
283 {
284 int res;
285 short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
286
287 *--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
288 *--help = flags;
289 *--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
290 *--help = 2; /* buffer offset for node data */
291 *--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
292 *--help = 0; /* buffer offset for index in/out */
293 *--help = 1; /* GET_DEVICE_NODE */
294
295 *(short *)(pnpbios_scratchbuf + 0) = *idxp;
296
297 res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
298 if (res)
299 return (res);
300
301 *idxp = *(short *)(pnpbios_scratchbuf + 0);
302 bcopy(pnpbios_scratchbuf + 2, buf, len);
303 return (0);
304 }
305
306 static void
307 eisaid_to_string(id, s)
308 unsigned char *id;
309 char *s;
310 {
311 static char hex[] = "0123456789ABCDEF";
312
313 *s++ = 'A' + (id[0] >> 2) - 1;
314 *s++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
315 *s++ = 'A' + (id[1] & 0x1f) - 1;
316 *s++ = hex[id[2] >> 4];
317 *s++ = hex[id[2] & 0x0f];
318 *s++ = hex[id[3] >> 4];
319 *s++ = hex[id[3] & 0x0f];
320 *s = '\0';
321 }
322
323 static void
324 pnpbios_printres(r)
325 struct pnpresources *r;
326 {
327 struct pnp_mem *mem;
328 struct pnp_io *io;
329 struct pnp_irq *irq;
330 struct pnp_dma *dma;
331 int p = 0;
332
333 mem = SIMPLEQ_FIRST(&r->mem);
334 if (mem) {
335 printf("mem");
336 do {
337 printf(" %x", mem->minbase);
338 if (mem->len > 1)
339 printf("-%x", mem->minbase + mem->len - 1);
340 } while ((mem = SIMPLEQ_NEXT(mem, next)));
341 p++;
342 }
343 io = SIMPLEQ_FIRST(&r->io);
344 if (io) {
345 if (p++)
346 printf(", ");
347 printf("io");
348 do {
349 printf(" %x", io->minbase);
350 if (io->len > 1)
351 printf("-%x", io->minbase + io->len - 1);
352 } while ((io = SIMPLEQ_NEXT(io, next)));
353 }
354 irq = SIMPLEQ_FIRST(&r->irq);
355 if (irq) {
356 if (p++)
357 printf(", ");
358 printf("irq");
359 do {
360 printf(" %d", ffs(irq->mask) - 1);
361 } while ((irq = SIMPLEQ_NEXT(irq, next)));
362 }
363 dma = SIMPLEQ_FIRST(&r->dma);
364 if (dma) {
365 if (p)
366 printf(", ");
367 printf("dma");
368 do {
369 printf(" %d", ffs(dma->mask) - 1);
370 } while ((dma = SIMPLEQ_NEXT(dma, next)));
371 }
372 }
373
374 static int
375 pnpbios_print(aux, pnp)
376 void *aux;
377 const char *pnp;
378 {
379 struct pnpbiosdev_attach_args *aa = aux;
380
381 if (pnp)
382 return (QUIET);
383
384 printf(" index %d (%s", aa->idx, aa->primid);
385 if (aa->resc->longname)
386 printf(", %s", aa->resc->longname);
387 if (aa->idstr != aa->primid)
388 printf(", attached as %s", aa->idstr);
389 printf(")");
390
391 return (0);
392 }
393
394 void
395 pnpbios_print_devres(dev, aa)
396 struct device *dev;
397 struct pnpbiosdev_attach_args *aa;
398 {
399
400 printf("%s: ", dev->dv_xname);
401 pnpbios_printres(aa->resc);
402 printf("\n");
403 }
404
405 static int
406 pnpbios_submatch(parent, match, aux)
407 struct device *parent;
408 struct cfdata *match;
409 void *aux;
410 {
411 struct pnpbiosdev_attach_args *aa = aux;
412
413 if (match->cf_loc[PNPBIOSCF_INDEX] != PNPBIOSCF_INDEX_DEFAULT &&
414 match->cf_loc[PNPBIOSCF_INDEX] != aa->idx)
415 return (0);
416
417 return ((*match->cf_attach->ca_match)(parent, match, aux));
418 }
419
420 static void
421 pnpbios_attachnode(sc, idx, buf, len)
422 struct pnpbios_softc *sc;
423 int idx;
424 unsigned char *buf;
425 size_t len;
426 {
427 char idstr[8];
428 unsigned char *p;
429 int res;
430 struct pnpresources r, s;
431 int i;
432 struct pnpbiosdev_attach_args aa;
433 struct pnp_compatid *compatid;
434
435 eisaid_to_string(buf + 3, idstr);
436 p = buf + 12;
437
438 res = pnp_scan(&p, len - 12, &r, 0);
439 if (res < 0) {
440 printf("error in config data\n");
441 goto dump;
442 }
443
444 /*
445 * the following is consistency check only for now
446 */
447 res = pnp_scan(&p, len - (p - buf), &s, 0);
448 if (res < 0) {
449 printf("error in possible configuration\n");
450 goto dump;
451 }
452
453 res = pnp_scan(&p, len - (p - buf), &s, 0);
454 if (res < 0) {
455 printf("error in compatible ID\n");
456 goto dump;
457 }
458
459 if (p != buf + len) {
460 printf("%s: length mismatch in node %d: used %d of %d Bytes\n",
461 sc->sc_dev.dv_xname, idx, p - buf, len);
462 if (p > buf + len) {
463 /* XXX shouldn't happen - pnp_scan should catch it */
464 goto dump;
465 }
466 /* Crappy BIOS: Buffer is not fully used. Be generous. */
467 }
468
469 if (r.nummem + r.numio + r.numirq + r.numdma == 0) {
470 #ifdef PNPBIOSVERBOSE
471 printf("%s", idstr);
472 if (r.longname)
473 printf(", %s", r.longname);
474 compatid = s.compatids;
475 while (compatid) {
476 printf(", %s", compatid->idstr);
477 compatid = compatid->next;
478 }
479 printf(" at %s index %d disabled\n", sc->sc_dev.dv_xname, idx);
480 #endif
481 return;
482 }
483
484 aa.pbt = 0; /* XXX placeholder */
485 aa.idx = idx;
486 aa.resc = &r;
487 aa.ic = sc->sc_ic;
488 aa.primid = idstr;
489
490 /* first try the specific device ID */
491 aa.idstr = idstr;
492 if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
493 pnpbios_submatch))
494 return;
495
496 /* if no driver was found, try compatible IDs */
497 compatid = s.compatids;
498 while (compatid) {
499 aa.idstr = compatid->idstr;
500 if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
501 pnpbios_submatch))
502 return;
503 compatid = compatid->next;
504 }
505
506 #ifdef PNPBIOSVERBOSE
507 printf("%s", idstr);
508 if (r.longname)
509 printf(", %s", r.longname);
510 compatid = s.compatids;
511 while (compatid) {
512 printf(", %s", compatid->idstr);
513 compatid = compatid->next;
514 }
515 printf(" (");
516 pnpbios_printres(&r);
517 printf(") at %s index %d ignored\n", sc->sc_dev.dv_xname, idx);
518 #endif
519
520 return;
521
522 /* XXX should free ressource lists */
523
524 dump:
525 for (i = 0; i < len; i++)
526 printf(" %02x", buf[i]);
527 printf("\n");
528 }
529
530 static int pnp_compatid __P((struct pnpresources *, unsigned char *, size_t));
531 static int pnp_newirq __P((struct pnpresources *, unsigned char *, size_t));
532 static int pnp_newdma __P((struct pnpresources *, unsigned char *, size_t));
533 static int pnp_newioport __P((struct pnpresources *, unsigned char *, size_t));
534 static int pnp_newfixedioport __P((struct pnpresources *, unsigned char *, size_t));
535
536 /*
537 * small ressource types (beginning with 1)
538 */
539 static struct{
540 int (*handler) __P((struct pnpresources *, unsigned char *, size_t));
541 int minlen, maxlen;
542 } smallrescs[] = {
543 {0, 2, 2}, /* PnP version number */
544 {0, 5, 6}, /* logical device id */
545 {pnp_compatid, 4, 4}, /* compatible device id */
546 {pnp_newirq, 2, 3}, /* irq descriptor */
547 {pnp_newdma, 2, 2}, /* dma descriptor */
548 {0, 0, 1}, /* start dep */
549 {0, 0, 0}, /* end dep */
550 {pnp_newioport, 7, 7}, /* io descriptor */
551 {pnp_newfixedioport, 3, 3}, /* fixed io descriptor */
552 {0, -1, -1}, /* reserved */
553 {0, -1, -1},
554 {0, -1, -1},
555 {0, -1, -1},
556 {0, 1, 7}, /* vendor defined */
557 {0, 1, 1} /* end */
558 };
559
560 #define NEXTBYTE(p) (*(p)++)
561
562 static int
563 pnp_scan(bufp, maxlen, r, in_depends)
564 unsigned char **bufp;
565 size_t maxlen;
566 struct pnpresources *r;
567 int in_depends;
568 {
569 unsigned char *p = *bufp;
570 int tag, type, len;
571 char *idstr;
572 int i;
573 struct pnp_mem *mem;
574
575 bzero(r, sizeof(*r));
576 SIMPLEQ_INIT(&r->mem);
577 SIMPLEQ_INIT(&r->io);
578 SIMPLEQ_INIT(&r->irq);
579 SIMPLEQ_INIT(&r->dma);
580
581 for (;;) {
582 if (p >= *bufp + maxlen) {
583 printf("pnp_scanresources: end of buffer\n");
584 return (-1);
585 }
586 tag = NEXTBYTE(p);
587
588 if (tag & 0x80) { /* long tag */
589 type = tag & 0x7f;
590 len = NEXTBYTE(p);
591 len |= NEXTBYTE(p) << 8;
592
593 switch (type) {
594 case 0x01: /* memory descriptor */
595 if (len != 9) {
596 printf("pnp_scan: bad mem desc\n");
597 return (-1);
598 }
599
600 mem = malloc(sizeof(struct pnp_mem),
601 M_DEVBUF, M_WAITOK);
602 mem->flags = NEXTBYTE(p);
603 mem->minbase = NEXTBYTE(p) << 8;
604 mem->minbase |= NEXTBYTE(p) << 16;
605 mem->maxbase = NEXTBYTE(p) << 8;
606 mem->maxbase |= NEXTBYTE(p) << 16;
607 mem->align = NEXTBYTE(p);
608 mem->align |= NEXTBYTE(p) << 8;
609 if (mem->align == 0)
610 mem->align = 0x10000;
611 mem->len = NEXTBYTE(p) << 8;
612 mem->len |= NEXTBYTE(p) << 16;
613 goto gotmem;
614 case 0x02:
615 if (in_depends)
616 printf("ID in dep?\n");
617 idstr = malloc(len + 1, M_DEVBUF, M_NOWAIT);
618 for (i = 0; i < len; i++)
619 idstr[i] = NEXTBYTE(p);
620 idstr[len] = '\0';
621 if (idstr[0] == '\0') {
622 /* disabled device */
623 free(idstr, M_DEVBUF);
624 break;
625 }
626 r->longname = idstr;
627 break;
628 case 0x05: /* 32bit memory descriptor */
629 if (len != 17) {
630 printf("pnp_scan: bad mem32 desc\n");
631 return (-1);
632 }
633
634 mem = malloc(sizeof(struct pnp_mem),
635 M_DEVBUF, M_WAITOK);
636 mem->flags = NEXTBYTE(p);
637 mem->minbase = NEXTBYTE(p);
638 mem->minbase |= NEXTBYTE(p) << 8;
639 mem->minbase |= NEXTBYTE(p) << 16;
640 mem->minbase |= NEXTBYTE(p) << 24;
641 mem->maxbase = NEXTBYTE(p);
642 mem->maxbase |= NEXTBYTE(p) << 8;
643 mem->maxbase |= NEXTBYTE(p) << 16;
644 mem->maxbase |= NEXTBYTE(p) << 24;
645 mem->align = NEXTBYTE(p);
646 mem->align |= NEXTBYTE(p) << 8;
647 mem->align |= NEXTBYTE(p) << 16;
648 mem->align |= NEXTBYTE(p) << 24;
649 mem->len = NEXTBYTE(p);
650 mem->len |= NEXTBYTE(p) << 8;
651 mem->len |= NEXTBYTE(p) << 16;
652 mem->len |= NEXTBYTE(p) << 24;
653 goto gotmem;
654 case 0x06: /* 32bit fixed memory descriptor */
655 if (len != 9) {
656 printf("pnp_scan: bad mem32 desc\n");
657 return (-1);
658 }
659
660 mem = malloc(sizeof(struct pnp_mem),
661 M_DEVBUF, M_WAITOK);
662 mem->flags = NEXTBYTE(p);
663 mem->minbase = NEXTBYTE(p);
664 mem->minbase |= NEXTBYTE(p) << 8;
665 mem->minbase |= NEXTBYTE(p) << 16;
666 mem->minbase |= NEXTBYTE(p) << 24;
667 mem->maxbase = mem->minbase;
668 mem->align = 0;
669 mem->len = NEXTBYTE(p);
670 mem->len |= NEXTBYTE(p) << 8;
671 mem->len |= NEXTBYTE(p) << 16;
672 mem->len |= NEXTBYTE(p) << 24;
673 gotmem:
674 if (mem->len == 0) { /* disabled */
675 #ifdef PNPBIOSDEBUG
676 printf("ZERO mem descriptor\n");
677 #endif
678 free(mem, M_DEVBUF);
679 break;
680 }
681 SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
682 r->nummem++;
683 break;
684 default:
685 printf("ignoring long tag %x\n", type);
686 while (len--)
687 (void) NEXTBYTE(p);
688 }
689 } else {
690 unsigned char tmpbuf[7];
691 int i;
692
693 type = (tag >> 3) & 0x0f;
694 len = tag & 0x07;
695
696 if (type == 0 ||
697 len < smallrescs[type - 1].minlen ||
698 len > smallrescs[type - 1].maxlen) {
699 printf("pnp_scan: bad small resource\n");
700 return (-1);
701 }
702 for (i = 0; i < len; i++)
703 tmpbuf[i] = NEXTBYTE(p);
704
705 if (type == 0x0f) { /* end mark */
706 if (in_depends) {
707 printf("end in dep?\n");
708 return (-1);
709 }
710 break;
711 }
712 if (type == 0x06) { /* start dep */
713 struct pnpresources *new, *last;
714 int res;
715
716 if (r->dependant_link) {
717 printf("second dep?\n");
718 return (-1);
719 }
720
721 if (in_depends) {
722 *bufp = p;
723 return (1);
724 }
725
726 last = r;
727 do {
728 new = malloc(sizeof(*new),
729 M_DEVBUF, M_NOWAIT);
730
731 res = pnp_scan(&p, maxlen - (p - *bufp),
732 new, 1);
733 if (res < 0) {
734 printf("error in dependant function\n");
735 free(new, M_DEVBUF);
736 return (-1);
737 }
738
739 last->dependant_link = new;
740 last = new;
741 } while (res > 0);
742 continue;
743 }
744 if (type == 0x07) { /* end dep */
745 if (!in_depends) {
746 printf("end dep?\n");
747 return (-1);
748 }
749 break;
750 }
751
752 if (!smallrescs[type - 1].handler)
753 printf("ignoring short tag %x\n", type);
754 else
755 if ((*smallrescs[type - 1].handler)(r, tmpbuf,
756 len))
757 return (-1);
758 }
759 }
760 *bufp = p;
761 return (0);
762 }
763
764 static int
765 pnp_newirq(r, buf, len)
766 struct pnpresources *r;
767 unsigned char *buf;
768 size_t len;
769 {
770 struct pnp_irq *irq;
771
772 if (buf[0] == 0 && buf[1] == 0) { /* disabled */
773 #ifdef PNPBIOSDEBUG
774 printf("ZERO irq descriptor\n");
775 #endif
776 return (0);
777 }
778 irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
779 irq->mask = buf[0] | (buf[1] << 8);
780 if (len > 2)
781 irq->flags = buf[2];
782 else
783 irq->flags = 0x01;
784 SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
785 r->numirq++;
786 return (0);
787 }
788
789 static int
790 pnp_newdma(r, buf, len)
791 struct pnpresources *r;
792 unsigned char *buf;
793 size_t len;
794 {
795 struct pnp_dma *dma;
796
797 if (buf[0] == 0) { /* disabled */
798 #ifdef PNPBIOSDEBUG
799 printf("ZERO dma descriptor\n");
800 #endif
801 return (0);
802 }
803 dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
804 dma->mask = buf[0];
805 dma->flags = buf[1];
806 SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
807 r->numdma++;
808 return (0);
809 }
810
811 static int
812 pnp_newioport(r, buf, len)
813 struct pnpresources *r;
814 unsigned char *buf;
815 size_t len;
816 {
817 struct pnp_io *io;
818
819 if (buf[6] == 0) { /* disabled */
820 #ifdef PNPBIOSDEBUG
821 printf("ZERO io descriptor\n");
822 #endif
823 return (0);
824 }
825 io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
826 io->flags = buf[0];
827 io->minbase = buf[1] | (buf[2] << 8);
828 io->maxbase = buf[3] | (buf[4] << 8);
829 io->align = buf[5];
830 io->len = buf[6];
831 SIMPLEQ_INSERT_TAIL(&r->io, io, next);
832 r->numio++;
833 return (0);
834 }
835
836 static int
837 pnp_newfixedioport(r, buf, len)
838 struct pnpresources *r;
839 unsigned char *buf;
840 size_t len;
841 {
842 struct pnp_io *io;
843
844 if (buf[2] == 0) { /* disabled */
845 #ifdef PNPBIOSDEBUG
846 printf("ZERO fixed io descriptor\n");
847 #endif
848 return (0);
849 }
850 io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
851 io->flags = 1; /* 10 bit decoding */
852 io->minbase = io->maxbase = buf[0] | (buf[1] << 8);
853 io->align = 1;
854 io->len = buf[2];
855 SIMPLEQ_INSERT_TAIL(&r->io, io, next);
856 r->numio++;
857 return (0);
858 }
859
860 static int
861 pnp_compatid(r, buf, len)
862 struct pnpresources *r;
863 unsigned char *buf;
864 size_t len;
865 {
866 struct pnp_compatid *id;
867
868 id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
869 eisaid_to_string(buf, id->idstr);
870 id->next = r->compatids;
871 r->compatids = id;
872 return (0);
873 }
874
875 int
876 pnpbios_io_map(pbt, resc, idx, tagp, hdlp)
877 pnpbios_tag_t pbt;
878 struct pnpresources *resc;
879 int idx;
880 bus_space_tag_t *tagp;
881 bus_space_handle_t *hdlp;
882 {
883 struct pnp_io *io;
884
885 if (idx >= resc->numio)
886 return (EINVAL);
887
888 io = SIMPLEQ_FIRST(&resc->io);
889 while (idx--)
890 io = SIMPLEQ_NEXT(io, next);
891
892 *tagp = I386_BUS_SPACE_IO;
893 return (i386_memio_map(I386_BUS_SPACE_IO, io->minbase, io->len,
894 0, hdlp));
895 }
896
897 int
898 pnpbios_getiobase(pbt, resc, idx, tagp, basep)
899 pnpbios_tag_t pbt;
900 struct pnpresources *resc;
901 int idx;
902 bus_space_tag_t *tagp;
903 int *basep;
904 {
905 struct pnp_io *io;
906
907 if (idx >= resc->numio)
908 return (EINVAL);
909
910 io = SIMPLEQ_FIRST(&resc->io);
911 while (idx--)
912 io = SIMPLEQ_NEXT(io, next);
913
914 if (tagp)
915 *tagp = I386_BUS_SPACE_IO;
916 if (basep)
917 *basep = io->minbase;
918 return (0);
919 }
920
921 void *
922 pnpbios_intr_establish(pbt, resc, idx, level, fcn, arg)
923 pnpbios_tag_t pbt;
924 struct pnpresources *resc;
925 int idx, level;
926 int (*fcn) __P((void *));
927 void *arg;
928 {
929 struct pnp_irq *irq;
930 int irqnum, type;
931
932 if (idx >= resc->numirq)
933 return (0);
934
935 irq = SIMPLEQ_FIRST(&resc->irq);
936 while (idx--)
937 irq = SIMPLEQ_NEXT(irq, next);
938
939 irqnum = ffs(irq->mask) - 1;
940 type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
941
942 return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
943 }
944
945 int
946 pnpbios_getirqnum(pbt, resc, idx, irqp)
947 pnpbios_tag_t pbt;
948 struct pnpresources *resc;
949 int idx;
950 int *irqp;
951 {
952 struct pnp_irq *irq;
953
954 if (idx >= resc->numirq)
955 return (EINVAL);
956
957 irq = SIMPLEQ_FIRST(&resc->irq);
958 while (idx--)
959 irq = SIMPLEQ_NEXT(irq, next);
960
961 *irqp = ffs(irq->mask) - 1;
962 return (0);
963 }
964
965 int
966 pnpbios_getdmachan(pbt, resc, idx, chanp)
967 pnpbios_tag_t pbt;
968 struct pnpresources *resc;
969 int idx;
970 int *chanp;
971 {
972 struct pnp_dma *dma;
973
974 if (idx >= resc->numdma)
975 return (EINVAL);
976
977 dma = SIMPLEQ_FIRST(&resc->dma);
978 while (idx--)
979 dma = SIMPLEQ_NEXT(dma, next);
980
981 *chanp = ffs(dma->mask) - 1;
982 return (0);
983 }
984