Home | History | Annotate | Line # | Download | only in pnpbios
pnpbios.c revision 1.5
      1 /* $NetBSD: pnpbios.c,v 1.5 1999/11/19 02:40:25 matt 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; 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 (idx != 0xff)
    246 		printf("last idx=%x\n", idx);
    247 
    248 	free(buf, M_DEVBUF);
    249 }
    250 
    251 static int
    252 pnpbios_getnumnodes(nump, sizep)
    253 	int *nump;
    254 	size_t *sizep;
    255 {
    256 	int res;
    257 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    258 
    259 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    260 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    261 	*--help = 2; /* buffer offset for node size */
    262 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    263 	*--help = 0; /* buffer offset for numnodes */
    264 	*--help = 0; /* GET_NUM_NODES */
    265 
    266 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    267 	if (res)
    268 		return (res);
    269 
    270 	*nump = *(short *)(pnpbios_scratchbuf + 0);
    271 	*sizep = *(short *)(pnpbios_scratchbuf + 2);
    272 	return (0);
    273 }
    274 
    275 static int
    276 pnpbios_getnode(flags, idxp, buf, len)
    277 	int flags;
    278 	int *idxp;
    279 	unsigned char *buf;
    280 	size_t len;
    281 {
    282 	int res;
    283 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    284 
    285 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    286 	*--help = flags;
    287 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    288 	*--help = 2; /* buffer offset for node data */
    289 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    290 	*--help = 0; /* buffer offset for index in/out */
    291 	*--help = 1; /* GET_DEVICE_NODE */
    292 
    293 	*(short *)(pnpbios_scratchbuf + 0) = *idxp;
    294 
    295 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    296 	if (res)
    297 		return (res);
    298 
    299 	*idxp = *(short *)(pnpbios_scratchbuf + 0);
    300 	bcopy(pnpbios_scratchbuf + 2, buf, len);
    301 	return (0);
    302 }
    303 
    304 static void
    305 eisaid_to_string(id, s)
    306 	unsigned char *id;
    307 	char *s;
    308 {
    309 	static char hex[] = "0123456789ABCDEF";
    310 
    311 	*s++ = 'A' + (id[0] >> 2) - 1;
    312 	*s++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
    313 	*s++ = 'A' + (id[1] & 0x1f) - 1;
    314 	*s++ = hex[id[2] >> 4];
    315 	*s++ = hex[id[2] & 0x0f];
    316 	*s++ = hex[id[3] >> 4];
    317 	*s++ = hex[id[3] & 0x0f];
    318 	*s = '\0';
    319 }
    320 
    321 static void
    322 pnpbios_printres(r)
    323 	struct pnpresources *r;
    324 {
    325 	struct pnp_mem *mem;
    326 	struct pnp_io *io;
    327 	struct pnp_irq *irq;
    328 	struct pnp_dma *dma;
    329 	int p = 0;
    330 
    331 	mem = SIMPLEQ_FIRST(&r->mem);
    332 	if (mem) {
    333 		printf("mem");
    334 		do {
    335 			printf(" %x", mem->minbase);
    336 			if (mem->len > 1)
    337 				printf("-%x", mem->minbase + mem->len - 1);
    338 		} while ((mem = SIMPLEQ_NEXT(mem, next)));
    339 		p++;
    340 	}
    341 	io = SIMPLEQ_FIRST(&r->io);
    342 	if (io) {
    343 		if (p++)
    344 			printf(", ");
    345 		printf("io");
    346 		do {
    347 			printf(" %x", io->minbase);
    348 			if (io->len > 1)
    349 				printf("-%x", io->minbase + io->len - 1);
    350 		} while ((io = SIMPLEQ_NEXT(io, next)));
    351 	}
    352 	irq = SIMPLEQ_FIRST(&r->irq);
    353 	if (irq) {
    354 		if (p++)
    355 			printf(", ");
    356 		printf("irq");
    357 		do {
    358 			printf(" %d", ffs(irq->mask) - 1);
    359 		} while ((irq = SIMPLEQ_NEXT(irq, next)));
    360 	}
    361 	dma = SIMPLEQ_FIRST(&r->dma);
    362 	if (dma) {
    363 		if (p)
    364 			printf(", ");
    365 		printf("dma");
    366 		do {
    367 			printf(" %d", ffs(dma->mask) - 1);
    368 		} while ((dma = SIMPLEQ_NEXT(dma, next)));
    369 	}
    370 }
    371 
    372 static int
    373 pnpbios_print(aux, pnp)
    374 	void *aux;
    375 	const char *pnp;
    376 {
    377 	struct pnpbiosdev_attach_args *aa = aux;
    378 
    379 	if (pnp)
    380 		return (QUIET);
    381 
    382 	printf(" index %d (%s", aa->idx, aa->primid);
    383 	if (aa->resc->longname)
    384 		printf(", %s", aa->resc->longname);
    385 	if (aa->idstr != aa->primid)
    386 		printf(", attached as %s", aa->idstr);
    387 	printf(")");
    388 
    389 	return (0);
    390 }
    391 
    392 void
    393 pnpbios_print_devres(dev, aa)
    394 	struct device *dev;
    395 	struct pnpbiosdev_attach_args *aa;
    396 {
    397 
    398 	printf("%s: ", dev->dv_xname);
    399 	pnpbios_printres(aa->resc);
    400 	printf("\n");
    401 }
    402 
    403 static int
    404 pnpbios_submatch(parent, match, aux)
    405 	struct device *parent;
    406 	struct cfdata *match;
    407 	void *aux;
    408 {
    409 	struct pnpbiosdev_attach_args *aa = aux;
    410 
    411 	if (match->cf_loc[PNPBIOSCF_INDEX] != PNPBIOSCF_INDEX_DEFAULT &&
    412 	    match->cf_loc[PNPBIOSCF_INDEX] != aa->idx)
    413 		return (0);
    414 
    415 	return ((*match->cf_attach->ca_match)(parent, match, aux));
    416 }
    417 
    418 static void
    419 pnpbios_attachnode(sc, idx, buf, len)
    420 	struct pnpbios_softc *sc;
    421 	int idx;
    422 	unsigned char *buf;
    423 	size_t len;
    424 {
    425 	char idstr[8];
    426 	unsigned char *p;
    427 	int res;
    428 	struct pnpresources r, s;
    429 	int i;
    430 	struct pnpbiosdev_attach_args aa;
    431 	struct pnp_compatid *compatid;
    432 
    433 	eisaid_to_string(buf + 3, idstr);
    434 	p = buf + 12;
    435 
    436 	res = pnp_scan(&p, len - 12, &r, 0);
    437 	if (res < 0) {
    438 		printf("error in config data\n");
    439 		goto dump;
    440 	}
    441 
    442 	/*
    443 	 * the following is consistency check only for now
    444 	 */
    445 	res = pnp_scan(&p, len - (p - buf), &s, 0);
    446 	if (res < 0) {
    447 		printf("error in possible configuration\n");
    448 		goto dump;
    449 	}
    450 
    451 	res = pnp_scan(&p, len - (p - buf), &s, 0);
    452 	if (res < 0) {
    453 		printf("error in compatible ID\n");
    454 		goto dump;
    455 	}
    456 
    457 	if (p != buf + len) {
    458 		printf("length mismatch\n");
    459 		goto dump;
    460 	}
    461 
    462 	aa.pbt = 0; /* XXX placeholder */
    463 	aa.idx = idx;
    464 	aa.resc = &r;
    465 	aa.ic = sc->sc_ic;
    466 	aa.primid = idstr;
    467 
    468 	/* first try the specific device ID */
    469 	aa.idstr = idstr;
    470 	if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    471 	    pnpbios_submatch))
    472 		return;
    473 
    474 	/* if no driver was found, try compatible IDs */
    475 	compatid = s.compatids;
    476 	while (compatid) {
    477 		aa.idstr = compatid->idstr;
    478 		if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    479 		    pnpbios_submatch))
    480 			return;
    481 		compatid = compatid->next;
    482 	}
    483 
    484 #ifdef PNPBIOSVERBOSE
    485 	printf("%s", idstr);
    486 	if (r.longname)
    487 		printf(", %s", r.longname);
    488 	compatid = s.compatids;
    489 	while (compatid) {
    490 		printf(", %s", compatid->idstr);
    491 		compatid = compatid->next;
    492 	}
    493 	printf(" (");
    494 	pnpbios_printres(&r);
    495 	printf(") at %s index %d ignored\n", sc->sc_dev.dv_xname, idx);
    496 #endif
    497 
    498 	return;
    499 
    500 	/* XXX should free ressource lists */
    501 
    502 dump:
    503 	for (i = 0; i < len; i++)
    504 		printf(" %02x", buf[i]);
    505 	printf("\n");
    506 }
    507 
    508 static int pnp_compatid __P((struct pnpresources *, unsigned char *, size_t));
    509 static int pnp_newirq __P((struct pnpresources *, unsigned char *, size_t));
    510 static int pnp_newdma __P((struct pnpresources *, unsigned char *, size_t));
    511 static int pnp_newioport __P((struct pnpresources *, unsigned char *, size_t));
    512 
    513 /*
    514  * small ressource types (beginning with 1)
    515  */
    516 static struct{
    517 	int (*handler) __P((struct pnpresources *, unsigned char *, size_t));
    518 	int minlen, maxlen;
    519 } smallrescs[] = {
    520 	{0, 2, 2}, /* PnP version number */
    521 	{0, 5, 6}, /* logical device id */
    522 	{pnp_compatid, 4, 4}, /* compatible device id */
    523 	{pnp_newirq, 2, 3}, /* irq  descriptor */
    524 	{pnp_newdma, 2, 2}, /* dma  descriptor */
    525 	{0, 0, 1}, /* start dep */
    526 	{0, 0, 0}, /* end dep */
    527 	{pnp_newioport, 7, 7}, /* io descriptor */
    528 	{0, 3, 3}, /* fixed io descriptor */
    529 	{0, -1, -1}, /* reserved */
    530 	{0, -1, -1},
    531 	{0, -1, -1},
    532 	{0, -1, -1},
    533 	{0, 1, 7}, /* vendor defined */
    534 	{0, 1, 1} /* end */
    535 };
    536 
    537 #define NEXTBYTE(p) (*(p)++)
    538 
    539 static int
    540 pnp_scan(bufp, maxlen, r, in_depends)
    541 	unsigned char **bufp;
    542 	size_t maxlen;
    543 	struct pnpresources *r;
    544 	int in_depends;
    545 {
    546 	unsigned char *p = *bufp;
    547 	int tag, type, len;
    548 	char *idstr;
    549 	int i;
    550 	struct pnp_mem *mem;
    551 
    552 	bzero(r, sizeof(*r));
    553 	SIMPLEQ_INIT(&r->mem);
    554 	SIMPLEQ_INIT(&r->io);
    555 	SIMPLEQ_INIT(&r->irq);
    556 	SIMPLEQ_INIT(&r->dma);
    557 
    558 	for (;;) {
    559 		if (p >= *bufp + maxlen) {
    560 			printf("pnp_scanresources: end of buffer\n");
    561 			return (-1);
    562 		}
    563 		tag = NEXTBYTE(p);
    564 
    565 		if (tag & 0x80) { /* long tag */
    566 			type = tag & 0x7f;
    567 			len = NEXTBYTE(p);
    568 			len |= NEXTBYTE(p) << 8;
    569 
    570 			switch (type) {
    571 			case 0x01: /* memory descriptor */
    572 				if (len != 9) {
    573 					printf("pnp_scan: bad mem desc\n");
    574 					return (-1);
    575 				}
    576 
    577 				mem = malloc(sizeof(struct pnp_mem),
    578 					     M_DEVBUF, M_WAITOK);
    579 				mem->flags = NEXTBYTE(p);
    580 				mem->minbase = NEXTBYTE(p) << 8;
    581 				mem->minbase |= NEXTBYTE(p) << 16;
    582 				mem->maxbase = NEXTBYTE(p) << 8;
    583 				mem->maxbase |= NEXTBYTE(p) << 16;
    584 				mem->align = NEXTBYTE(p);
    585 				mem->align |= NEXTBYTE(p) << 8;
    586 				if (mem->align == 0)
    587 					mem->align = 0x10000;
    588 				mem->len = NEXTBYTE(p) << 8;
    589 				mem->len |= NEXTBYTE(p) << 16;
    590 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    591 				r->nummem++;
    592 #ifdef PNPBIOSDEBUG
    593 				if (mem->len == 0)
    594 					printf("ZERO mem descriptor\n");
    595 #endif
    596 				break;
    597 			case 0x02:
    598 				if (in_depends)
    599 					printf("ID in dep?\n");
    600 				idstr = malloc(len + 1, M_DEVBUF, M_NOWAIT);
    601 				for (i = 0; i < len; i++)
    602 					idstr[i] = NEXTBYTE(p);
    603 				idstr[len] = '\0';
    604 				r->longname = idstr;
    605 				break;
    606 			case 0x06: /* 32bit fixed memory descriptor */
    607 				if (len != 9) {
    608 					printf("pnp_scan: bad mem32 desc\n");
    609 					return (-1);
    610 				}
    611 
    612 				mem = malloc(sizeof(struct pnp_mem),
    613 					     M_DEVBUF, M_WAITOK);
    614 				mem->flags = NEXTBYTE(p);
    615 				mem->minbase = NEXTBYTE(p);
    616 				mem->minbase |= NEXTBYTE(p) << 8;
    617 				mem->minbase |= NEXTBYTE(p) << 16;
    618 				mem->minbase |= NEXTBYTE(p) << 24;
    619 				mem->maxbase = mem->minbase;
    620 				mem->align = 0;
    621 				mem->len = NEXTBYTE(p);
    622 				mem->len |= NEXTBYTE(p) << 8;
    623 				mem->len |= NEXTBYTE(p) << 16;
    624 				mem->len |= NEXTBYTE(p) << 24;
    625 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    626 				r->nummem++;
    627 #ifdef PNPBIOSDEBUG
    628 				if (mem->len == 0)
    629 					printf("ZERO mem descriptor\n");
    630 #endif
    631 				break;
    632 			default:
    633 				printf("ignoring long tag %x\n", type);
    634 				while (len--)
    635 					(void) NEXTBYTE(p);
    636 			}
    637 		} else {
    638 			unsigned char tmpbuf[7];
    639 			int i;
    640 
    641 			type = (tag >> 3) & 0x0f;
    642 			len = tag & 0x07;
    643 
    644 			if (type == 0 ||
    645 			    len < smallrescs[type - 1].minlen ||
    646 			    len > smallrescs[type - 1].maxlen) {
    647 				printf("pnp_scan: bad small resource\n");
    648 				return (-1);
    649 			}
    650 			for (i = 0; i < len; i++)
    651 				tmpbuf[i] = NEXTBYTE(p);
    652 
    653 			if (type == 0x0f) { /* end mark */
    654 				if (in_depends) {
    655 					printf("end in dep?\n");
    656 					return (-1);
    657 				}
    658 				break;
    659 			}
    660 			if (type == 0x06) { /* start dep */
    661 				struct pnpresources *new, *last;
    662 				int res;
    663 
    664 				if (r->dependant_link) {
    665 					printf("second dep?\n");
    666 					return (-1);
    667 				}
    668 
    669 				if (in_depends) {
    670 					*bufp = p;
    671 					return (1);
    672 				}
    673 
    674 				last = r;
    675 				do {
    676 					new = malloc(sizeof(*new),
    677 						     M_DEVBUF, M_NOWAIT);
    678 
    679 					res = pnp_scan(&p, maxlen - (p - *bufp),
    680 						       new, 1);
    681 					if (res < 0) {
    682 				printf("error in dependant function\n");
    683 						free(new, M_DEVBUF);
    684 						return (-1);
    685 					}
    686 
    687 					last->dependant_link = new;
    688 					last = new;
    689 				} while (res > 0);
    690 				continue;
    691 			}
    692 			if (type == 0x07) { /* end dep */
    693 				if (!in_depends) {
    694 					printf("end dep?\n");
    695 					return (-1);
    696 				}
    697 				break;
    698 			}
    699 
    700 			if (!smallrescs[type - 1].handler)
    701 				printf("ignoring short tag %x\n", type);
    702 			else
    703 				if ((*smallrescs[type - 1].handler)(r, tmpbuf,
    704 								    len))
    705 					return (-1);
    706 		}
    707 	}
    708 	*bufp = p;
    709 	return (0);
    710 }
    711 
    712 static int
    713 pnp_newirq(r, buf, len)
    714 	struct pnpresources *r;
    715 	unsigned char *buf;
    716 	size_t len;
    717 {
    718 	struct pnp_irq *irq;
    719 
    720 	irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
    721 	irq->mask = buf[0] | (buf[1] << 8);
    722 	if (len > 2)
    723 		irq->flags = buf[2];
    724 	else
    725 		irq->flags = 0x01;
    726 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    727 	r->numirq++;
    728 	return (0);
    729 }
    730 
    731 static int
    732 pnp_newdma(r, buf, len)
    733 	struct pnpresources *r;
    734 	unsigned char *buf;
    735 	size_t len;
    736 {
    737 	struct pnp_dma *dma;
    738 
    739 	dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
    740 	dma->mask = buf[0];
    741 	dma->flags = buf[1];
    742 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    743 	r->numdma++;
    744 	return (0);
    745 }
    746 
    747 static int
    748 pnp_newioport(r, buf, len)
    749 	struct pnpresources *r;
    750 	unsigned char *buf;
    751 	size_t len;
    752 {
    753 	struct pnp_io *io;
    754 
    755 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
    756 	io->flags = buf[0];
    757 	io->minbase = buf[1] | (buf[2] << 8);
    758 	io->maxbase = buf[3] | (buf[4] << 8);
    759 	io->align = buf[5];
    760 	io->len = buf[6];
    761 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    762 	r->numio++;
    763 	return (0);
    764 }
    765 
    766 static int
    767 pnp_compatid(r, buf, len)
    768 	struct pnpresources *r;
    769 	unsigned char *buf;
    770 	size_t len;
    771 {
    772 	struct pnp_compatid *id;
    773 
    774 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    775 	eisaid_to_string(buf, id->idstr);
    776 	id->next = r->compatids;
    777 	r->compatids = id;
    778 	return (0);
    779 }
    780 
    781 int
    782 pnpbios_io_map(pbt, resc, idx, tagp, hdlp)
    783 	pnpbios_tag_t pbt;
    784 	struct pnpresources *resc;
    785 	int idx;
    786 	bus_space_tag_t *tagp;
    787 	bus_space_handle_t *hdlp;
    788 {
    789 	struct pnp_io *io;
    790 
    791 	if (idx >= resc->numio)
    792 		return (EINVAL);
    793 
    794 	io = SIMPLEQ_FIRST(&resc->io);
    795 	while (idx--)
    796 		io = SIMPLEQ_NEXT(io, next);
    797 
    798 	*tagp = I386_BUS_SPACE_IO;
    799 	return (i386_memio_map(I386_BUS_SPACE_IO, io->minbase, io->len,
    800 			       0, hdlp));
    801 }
    802 
    803 int
    804 pnpbios_getiobase(pbt, resc, idx, tagp, basep)
    805 	pnpbios_tag_t pbt;
    806 	struct pnpresources *resc;
    807 	int idx;
    808 	bus_space_tag_t *tagp;
    809 	int *basep;
    810 {
    811 	struct pnp_io *io;
    812 
    813 	if (idx >= resc->numio)
    814 		return (EINVAL);
    815 
    816 	io = SIMPLEQ_FIRST(&resc->io);
    817 	while (idx--)
    818 		io = SIMPLEQ_NEXT(io, next);
    819 
    820 	if (tagp)
    821 		*tagp = I386_BUS_SPACE_IO;
    822 	if (basep)
    823 		*basep = io->minbase;
    824 	return (0);
    825 }
    826 
    827 void *
    828 pnpbios_intr_establish(pbt, resc, idx, level, fcn, arg)
    829 	pnpbios_tag_t pbt;
    830 	struct pnpresources *resc;
    831 	int idx, level;
    832 	int (*fcn) __P((void *));
    833 	void *arg;
    834 {
    835 	struct pnp_irq *irq;
    836 	int irqnum, type;
    837 
    838 	if (idx >= resc->numirq)
    839 		return (0);
    840 
    841 	irq = SIMPLEQ_FIRST(&resc->irq);
    842 	while (idx--)
    843 		irq = SIMPLEQ_NEXT(irq, next);
    844 
    845 	irqnum = ffs(irq->mask) - 1;
    846 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    847 
    848 	return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
    849 }
    850 
    851 int
    852 pnpbios_getirqnum(pbt, resc, idx, irqp)
    853 	pnpbios_tag_t pbt;
    854 	struct pnpresources *resc;
    855 	int idx;
    856 	int *irqp;
    857 {
    858 	struct pnp_irq *irq;
    859 
    860 	if (idx >= resc->numirq)
    861 		return (EINVAL);
    862 
    863 	irq = SIMPLEQ_FIRST(&resc->irq);
    864 	while (idx--)
    865 		irq = SIMPLEQ_NEXT(irq, next);
    866 
    867 	*irqp = ffs(irq->mask) - 1;
    868 	return (0);
    869 }
    870 
    871 int
    872 pnpbios_getdmachan(pbt, resc, idx, chanp)
    873 	pnpbios_tag_t pbt;
    874 	struct pnpresources *resc;
    875 	int idx;
    876 	int *chanp;
    877 {
    878 	struct pnp_dma *dma;
    879 
    880 	if (idx >= resc->numdma)
    881 		return (EINVAL);
    882 
    883 	dma = SIMPLEQ_FIRST(&resc->dma);
    884 	while (idx--)
    885 		dma = SIMPLEQ_NEXT(dma, next);
    886 
    887 	*chanp = ffs(dma->mask) - 1;
    888 	return (0);
    889 }
    890