Home | History | Annotate | Line # | Download | only in pnpbios
pnpbios.c revision 1.8
      1 /* $NetBSD: pnpbios.c,v 1.8 2000/01/12 19:24:02 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 				r->longname = idstr;
    622 				break;
    623 			case 0x05:  /* 32bit memory descriptor */
    624 				if (len != 17) {
    625 					printf("pnp_scan: bad mem32 desc\n");
    626 					return (-1);
    627 				}
    628 
    629 				mem = malloc(sizeof(struct pnp_mem),
    630 					     M_DEVBUF, M_WAITOK);
    631 				mem->flags = NEXTBYTE(p);
    632 				mem->minbase = NEXTBYTE(p);
    633 				mem->minbase |= NEXTBYTE(p) << 8;
    634 				mem->minbase |= NEXTBYTE(p) << 16;
    635 				mem->minbase |= NEXTBYTE(p) << 24;
    636 				mem->maxbase = NEXTBYTE(p);
    637 				mem->maxbase |= NEXTBYTE(p) << 8;
    638 				mem->maxbase |= NEXTBYTE(p) << 16;
    639 				mem->maxbase |= NEXTBYTE(p) << 24;
    640 				mem->align = NEXTBYTE(p);
    641 				mem->align |= NEXTBYTE(p) << 8;
    642 				mem->align |= NEXTBYTE(p) << 16;
    643 				mem->align |= NEXTBYTE(p) << 24;
    644 				mem->len = NEXTBYTE(p);
    645 				mem->len |= NEXTBYTE(p) << 8;
    646 				mem->len |= NEXTBYTE(p) << 16;
    647 				mem->len |= NEXTBYTE(p) << 24;
    648 				goto gotmem;
    649 			case 0x06: /* 32bit fixed memory descriptor */
    650 				if (len != 9) {
    651 					printf("pnp_scan: bad mem32 desc\n");
    652 					return (-1);
    653 				}
    654 
    655 				mem = malloc(sizeof(struct pnp_mem),
    656 					     M_DEVBUF, M_WAITOK);
    657 				mem->flags = NEXTBYTE(p);
    658 				mem->minbase = NEXTBYTE(p);
    659 				mem->minbase |= NEXTBYTE(p) << 8;
    660 				mem->minbase |= NEXTBYTE(p) << 16;
    661 				mem->minbase |= NEXTBYTE(p) << 24;
    662 				mem->maxbase = mem->minbase;
    663 				mem->align = 0;
    664 				mem->len = NEXTBYTE(p);
    665 				mem->len |= NEXTBYTE(p) << 8;
    666 				mem->len |= NEXTBYTE(p) << 16;
    667 				mem->len |= NEXTBYTE(p) << 24;
    668 gotmem:
    669 				if (mem->len == 0) { /* disabled */
    670 #ifdef PNPBIOSDEBUG
    671 					printf("ZERO mem descriptor\n");
    672 #endif
    673 					free(mem, M_DEVBUF);
    674 					break;
    675 				}
    676 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    677 				r->nummem++;
    678 				break;
    679 			default:
    680 				printf("ignoring long tag %x\n", type);
    681 				while (len--)
    682 					(void) NEXTBYTE(p);
    683 			}
    684 		} else {
    685 			unsigned char tmpbuf[7];
    686 			int i;
    687 
    688 			type = (tag >> 3) & 0x0f;
    689 			len = tag & 0x07;
    690 
    691 			if (type == 0 ||
    692 			    len < smallrescs[type - 1].minlen ||
    693 			    len > smallrescs[type - 1].maxlen) {
    694 				printf("pnp_scan: bad small resource\n");
    695 				return (-1);
    696 			}
    697 			for (i = 0; i < len; i++)
    698 				tmpbuf[i] = NEXTBYTE(p);
    699 
    700 			if (type == 0x0f) { /* end mark */
    701 				if (in_depends) {
    702 					printf("end in dep?\n");
    703 					return (-1);
    704 				}
    705 				break;
    706 			}
    707 			if (type == 0x06) { /* start dep */
    708 				struct pnpresources *new, *last;
    709 				int res;
    710 
    711 				if (r->dependant_link) {
    712 					printf("second dep?\n");
    713 					return (-1);
    714 				}
    715 
    716 				if (in_depends) {
    717 					*bufp = p;
    718 					return (1);
    719 				}
    720 
    721 				last = r;
    722 				do {
    723 					new = malloc(sizeof(*new),
    724 						     M_DEVBUF, M_NOWAIT);
    725 
    726 					res = pnp_scan(&p, maxlen - (p - *bufp),
    727 						       new, 1);
    728 					if (res < 0) {
    729 				printf("error in dependant function\n");
    730 						free(new, M_DEVBUF);
    731 						return (-1);
    732 					}
    733 
    734 					last->dependant_link = new;
    735 					last = new;
    736 				} while (res > 0);
    737 				continue;
    738 			}
    739 			if (type == 0x07) { /* end dep */
    740 				if (!in_depends) {
    741 					printf("end dep?\n");
    742 					return (-1);
    743 				}
    744 				break;
    745 			}
    746 
    747 			if (!smallrescs[type - 1].handler)
    748 				printf("ignoring short tag %x\n", type);
    749 			else
    750 				if ((*smallrescs[type - 1].handler)(r, tmpbuf,
    751 								    len))
    752 					return (-1);
    753 		}
    754 	}
    755 	*bufp = p;
    756 	return (0);
    757 }
    758 
    759 static int
    760 pnp_newirq(r, buf, len)
    761 	struct pnpresources *r;
    762 	unsigned char *buf;
    763 	size_t len;
    764 {
    765 	struct pnp_irq *irq;
    766 
    767 	if (buf[0] == 0 && buf[1] == 0) { /* disabled */
    768 #ifdef PNPBIOSDEBUG
    769 		printf("ZERO irq descriptor\n");
    770 #endif
    771 		return (0);
    772 	}
    773 	irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
    774 	irq->mask = buf[0] | (buf[1] << 8);
    775 	if (len > 2)
    776 		irq->flags = buf[2];
    777 	else
    778 		irq->flags = 0x01;
    779 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    780 	r->numirq++;
    781 	return (0);
    782 }
    783 
    784 static int
    785 pnp_newdma(r, buf, len)
    786 	struct pnpresources *r;
    787 	unsigned char *buf;
    788 	size_t len;
    789 {
    790 	struct pnp_dma *dma;
    791 
    792 	if (buf[0] == 0) { /* disabled */
    793 #ifdef PNPBIOSDEBUG
    794 		printf("ZERO dma descriptor\n");
    795 #endif
    796 		return (0);
    797 	}
    798 	dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
    799 	dma->mask = buf[0];
    800 	dma->flags = buf[1];
    801 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    802 	r->numdma++;
    803 	return (0);
    804 }
    805 
    806 static int
    807 pnp_newioport(r, buf, len)
    808 	struct pnpresources *r;
    809 	unsigned char *buf;
    810 	size_t len;
    811 {
    812 	struct pnp_io *io;
    813 
    814 	if (buf[6] == 0) { /* disabled */
    815 #ifdef PNPBIOSDEBUG
    816 		printf("ZERO io descriptor\n");
    817 #endif
    818 		return (0);
    819 	}
    820 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
    821 	io->flags = buf[0];
    822 	io->minbase = buf[1] | (buf[2] << 8);
    823 	io->maxbase = buf[3] | (buf[4] << 8);
    824 	io->align = buf[5];
    825 	io->len = buf[6];
    826 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    827 	r->numio++;
    828 	return (0);
    829 }
    830 
    831 static int
    832 pnp_newfixedioport(r, buf, len)
    833 	struct pnpresources *r;
    834 	unsigned char *buf;
    835 	size_t len;
    836 {
    837 	struct pnp_io *io;
    838 
    839 	if (buf[2] == 0) { /* disabled */
    840 #ifdef PNPBIOSDEBUG
    841 		printf("ZERO fixed io descriptor\n");
    842 #endif
    843 		return (0);
    844 	}
    845 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
    846 	io->flags = 1; /* 10 bit decoding */
    847 	io->minbase = io->maxbase = buf[0] | (buf[1] << 8);
    848 	io->align = 1;
    849 	io->len = buf[2];
    850 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    851 	r->numio++;
    852 	return (0);
    853 }
    854 
    855 static int
    856 pnp_compatid(r, buf, len)
    857 	struct pnpresources *r;
    858 	unsigned char *buf;
    859 	size_t len;
    860 {
    861 	struct pnp_compatid *id;
    862 
    863 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    864 	eisaid_to_string(buf, id->idstr);
    865 	id->next = r->compatids;
    866 	r->compatids = id;
    867 	return (0);
    868 }
    869 
    870 int
    871 pnpbios_io_map(pbt, resc, idx, tagp, hdlp)
    872 	pnpbios_tag_t pbt;
    873 	struct pnpresources *resc;
    874 	int idx;
    875 	bus_space_tag_t *tagp;
    876 	bus_space_handle_t *hdlp;
    877 {
    878 	struct pnp_io *io;
    879 
    880 	if (idx >= resc->numio)
    881 		return (EINVAL);
    882 
    883 	io = SIMPLEQ_FIRST(&resc->io);
    884 	while (idx--)
    885 		io = SIMPLEQ_NEXT(io, next);
    886 
    887 	*tagp = I386_BUS_SPACE_IO;
    888 	return (i386_memio_map(I386_BUS_SPACE_IO, io->minbase, io->len,
    889 			       0, hdlp));
    890 }
    891 
    892 int
    893 pnpbios_getiobase(pbt, resc, idx, tagp, basep)
    894 	pnpbios_tag_t pbt;
    895 	struct pnpresources *resc;
    896 	int idx;
    897 	bus_space_tag_t *tagp;
    898 	int *basep;
    899 {
    900 	struct pnp_io *io;
    901 
    902 	if (idx >= resc->numio)
    903 		return (EINVAL);
    904 
    905 	io = SIMPLEQ_FIRST(&resc->io);
    906 	while (idx--)
    907 		io = SIMPLEQ_NEXT(io, next);
    908 
    909 	if (tagp)
    910 		*tagp = I386_BUS_SPACE_IO;
    911 	if (basep)
    912 		*basep = io->minbase;
    913 	return (0);
    914 }
    915 
    916 void *
    917 pnpbios_intr_establish(pbt, resc, idx, level, fcn, arg)
    918 	pnpbios_tag_t pbt;
    919 	struct pnpresources *resc;
    920 	int idx, level;
    921 	int (*fcn) __P((void *));
    922 	void *arg;
    923 {
    924 	struct pnp_irq *irq;
    925 	int irqnum, type;
    926 
    927 	if (idx >= resc->numirq)
    928 		return (0);
    929 
    930 	irq = SIMPLEQ_FIRST(&resc->irq);
    931 	while (idx--)
    932 		irq = SIMPLEQ_NEXT(irq, next);
    933 
    934 	irqnum = ffs(irq->mask) - 1;
    935 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    936 
    937 	return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
    938 }
    939 
    940 int
    941 pnpbios_getirqnum(pbt, resc, idx, irqp)
    942 	pnpbios_tag_t pbt;
    943 	struct pnpresources *resc;
    944 	int idx;
    945 	int *irqp;
    946 {
    947 	struct pnp_irq *irq;
    948 
    949 	if (idx >= resc->numirq)
    950 		return (EINVAL);
    951 
    952 	irq = SIMPLEQ_FIRST(&resc->irq);
    953 	while (idx--)
    954 		irq = SIMPLEQ_NEXT(irq, next);
    955 
    956 	*irqp = ffs(irq->mask) - 1;
    957 	return (0);
    958 }
    959 
    960 int
    961 pnpbios_getdmachan(pbt, resc, idx, chanp)
    962 	pnpbios_tag_t pbt;
    963 	struct pnpresources *resc;
    964 	int idx;
    965 	int *chanp;
    966 {
    967 	struct pnp_dma *dma;
    968 
    969 	if (idx >= resc->numdma)
    970 		return (EINVAL);
    971 
    972 	dma = SIMPLEQ_FIRST(&resc->dma);
    973 	while (idx--)
    974 		dma = SIMPLEQ_NEXT(dma, next);
    975 
    976 	*chanp = ffs(dma->mask) - 1;
    977 	return (0);
    978 }
    979