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