Home | History | Annotate | Line # | Download | only in pnpbios
pnpbios.c revision 1.6
      1 /* $NetBSD: pnpbios.c,v 1.6 1999/11/30 15:54:55 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("length mismatch\n");
    461 		goto dump;
    462 	}
    463 
    464 	aa.pbt = 0; /* XXX placeholder */
    465 	aa.idx = idx;
    466 	aa.resc = &r;
    467 	aa.ic = sc->sc_ic;
    468 	aa.primid = idstr;
    469 
    470 	/* first try the specific device ID */
    471 	aa.idstr = idstr;
    472 	if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    473 	    pnpbios_submatch))
    474 		return;
    475 
    476 	/* if no driver was found, try compatible IDs */
    477 	compatid = s.compatids;
    478 	while (compatid) {
    479 		aa.idstr = compatid->idstr;
    480 		if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    481 		    pnpbios_submatch))
    482 			return;
    483 		compatid = compatid->next;
    484 	}
    485 
    486 #ifdef PNPBIOSVERBOSE
    487 	printf("%s", idstr);
    488 	if (r.longname)
    489 		printf(", %s", r.longname);
    490 	compatid = s.compatids;
    491 	while (compatid) {
    492 		printf(", %s", compatid->idstr);
    493 		compatid = compatid->next;
    494 	}
    495 	printf(" (");
    496 	pnpbios_printres(&r);
    497 	printf(") at %s index %d ignored\n", sc->sc_dev.dv_xname, idx);
    498 #endif
    499 
    500 	return;
    501 
    502 	/* XXX should free ressource lists */
    503 
    504 dump:
    505 	for (i = 0; i < len; i++)
    506 		printf(" %02x", buf[i]);
    507 	printf("\n");
    508 }
    509 
    510 static int pnp_compatid __P((struct pnpresources *, unsigned char *, size_t));
    511 static int pnp_newirq __P((struct pnpresources *, unsigned char *, size_t));
    512 static int pnp_newdma __P((struct pnpresources *, unsigned char *, size_t));
    513 static int pnp_newioport __P((struct pnpresources *, unsigned char *, size_t));
    514 
    515 /*
    516  * small ressource types (beginning with 1)
    517  */
    518 static struct{
    519 	int (*handler) __P((struct pnpresources *, unsigned char *, size_t));
    520 	int minlen, maxlen;
    521 } smallrescs[] = {
    522 	{0, 2, 2}, /* PnP version number */
    523 	{0, 5, 6}, /* logical device id */
    524 	{pnp_compatid, 4, 4}, /* compatible device id */
    525 	{pnp_newirq, 2, 3}, /* irq  descriptor */
    526 	{pnp_newdma, 2, 2}, /* dma  descriptor */
    527 	{0, 0, 1}, /* start dep */
    528 	{0, 0, 0}, /* end dep */
    529 	{pnp_newioport, 7, 7}, /* io descriptor */
    530 	{0, 3, 3}, /* fixed io descriptor */
    531 	{0, -1, -1}, /* reserved */
    532 	{0, -1, -1},
    533 	{0, -1, -1},
    534 	{0, -1, -1},
    535 	{0, 1, 7}, /* vendor defined */
    536 	{0, 1, 1} /* end */
    537 };
    538 
    539 #define NEXTBYTE(p) (*(p)++)
    540 
    541 static int
    542 pnp_scan(bufp, maxlen, r, in_depends)
    543 	unsigned char **bufp;
    544 	size_t maxlen;
    545 	struct pnpresources *r;
    546 	int in_depends;
    547 {
    548 	unsigned char *p = *bufp;
    549 	int tag, type, len;
    550 	char *idstr;
    551 	int i;
    552 	struct pnp_mem *mem;
    553 
    554 	bzero(r, sizeof(*r));
    555 	SIMPLEQ_INIT(&r->mem);
    556 	SIMPLEQ_INIT(&r->io);
    557 	SIMPLEQ_INIT(&r->irq);
    558 	SIMPLEQ_INIT(&r->dma);
    559 
    560 	for (;;) {
    561 		if (p >= *bufp + maxlen) {
    562 			printf("pnp_scanresources: end of buffer\n");
    563 			return (-1);
    564 		}
    565 		tag = NEXTBYTE(p);
    566 
    567 		if (tag & 0x80) { /* long tag */
    568 			type = tag & 0x7f;
    569 			len = NEXTBYTE(p);
    570 			len |= NEXTBYTE(p) << 8;
    571 
    572 			switch (type) {
    573 			case 0x01: /* memory descriptor */
    574 				if (len != 9) {
    575 					printf("pnp_scan: bad mem desc\n");
    576 					return (-1);
    577 				}
    578 
    579 				mem = malloc(sizeof(struct pnp_mem),
    580 					     M_DEVBUF, M_WAITOK);
    581 				mem->flags = NEXTBYTE(p);
    582 				mem->minbase = NEXTBYTE(p) << 8;
    583 				mem->minbase |= NEXTBYTE(p) << 16;
    584 				mem->maxbase = NEXTBYTE(p) << 8;
    585 				mem->maxbase |= NEXTBYTE(p) << 16;
    586 				mem->align = NEXTBYTE(p);
    587 				mem->align |= NEXTBYTE(p) << 8;
    588 				if (mem->align == 0)
    589 					mem->align = 0x10000;
    590 				mem->len = NEXTBYTE(p) << 8;
    591 				mem->len |= NEXTBYTE(p) << 16;
    592 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    593 				r->nummem++;
    594 #ifdef PNPBIOSDEBUG
    595 				if (mem->len == 0)
    596 					printf("ZERO mem descriptor\n");
    597 #endif
    598 				break;
    599 			case 0x02:
    600 				if (in_depends)
    601 					printf("ID in dep?\n");
    602 				idstr = malloc(len + 1, M_DEVBUF, M_NOWAIT);
    603 				for (i = 0; i < len; i++)
    604 					idstr[i] = NEXTBYTE(p);
    605 				idstr[len] = '\0';
    606 				r->longname = idstr;
    607 				break;
    608 			case 0x06: /* 32bit fixed memory descriptor */
    609 				if (len != 9) {
    610 					printf("pnp_scan: bad mem32 desc\n");
    611 					return (-1);
    612 				}
    613 
    614 				mem = malloc(sizeof(struct pnp_mem),
    615 					     M_DEVBUF, M_WAITOK);
    616 				mem->flags = NEXTBYTE(p);
    617 				mem->minbase = NEXTBYTE(p);
    618 				mem->minbase |= NEXTBYTE(p) << 8;
    619 				mem->minbase |= NEXTBYTE(p) << 16;
    620 				mem->minbase |= NEXTBYTE(p) << 24;
    621 				mem->maxbase = mem->minbase;
    622 				mem->align = 0;
    623 				mem->len = NEXTBYTE(p);
    624 				mem->len |= NEXTBYTE(p) << 8;
    625 				mem->len |= NEXTBYTE(p) << 16;
    626 				mem->len |= NEXTBYTE(p) << 24;
    627 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    628 				r->nummem++;
    629 #ifdef PNPBIOSDEBUG
    630 				if (mem->len == 0)
    631 					printf("ZERO mem descriptor\n");
    632 #endif
    633 				break;
    634 			default:
    635 				printf("ignoring long tag %x\n", type);
    636 				while (len--)
    637 					(void) NEXTBYTE(p);
    638 			}
    639 		} else {
    640 			unsigned char tmpbuf[7];
    641 			int i;
    642 
    643 			type = (tag >> 3) & 0x0f;
    644 			len = tag & 0x07;
    645 
    646 			if (type == 0 ||
    647 			    len < smallrescs[type - 1].minlen ||
    648 			    len > smallrescs[type - 1].maxlen) {
    649 				printf("pnp_scan: bad small resource\n");
    650 				return (-1);
    651 			}
    652 			for (i = 0; i < len; i++)
    653 				tmpbuf[i] = NEXTBYTE(p);
    654 
    655 			if (type == 0x0f) { /* end mark */
    656 				if (in_depends) {
    657 					printf("end in dep?\n");
    658 					return (-1);
    659 				}
    660 				break;
    661 			}
    662 			if (type == 0x06) { /* start dep */
    663 				struct pnpresources *new, *last;
    664 				int res;
    665 
    666 				if (r->dependant_link) {
    667 					printf("second dep?\n");
    668 					return (-1);
    669 				}
    670 
    671 				if (in_depends) {
    672 					*bufp = p;
    673 					return (1);
    674 				}
    675 
    676 				last = r;
    677 				do {
    678 					new = malloc(sizeof(*new),
    679 						     M_DEVBUF, M_NOWAIT);
    680 
    681 					res = pnp_scan(&p, maxlen - (p - *bufp),
    682 						       new, 1);
    683 					if (res < 0) {
    684 				printf("error in dependant function\n");
    685 						free(new, M_DEVBUF);
    686 						return (-1);
    687 					}
    688 
    689 					last->dependant_link = new;
    690 					last = new;
    691 				} while (res > 0);
    692 				continue;
    693 			}
    694 			if (type == 0x07) { /* end dep */
    695 				if (!in_depends) {
    696 					printf("end dep?\n");
    697 					return (-1);
    698 				}
    699 				break;
    700 			}
    701 
    702 			if (!smallrescs[type - 1].handler)
    703 				printf("ignoring short tag %x\n", type);
    704 			else
    705 				if ((*smallrescs[type - 1].handler)(r, tmpbuf,
    706 								    len))
    707 					return (-1);
    708 		}
    709 	}
    710 	*bufp = p;
    711 	return (0);
    712 }
    713 
    714 static int
    715 pnp_newirq(r, buf, len)
    716 	struct pnpresources *r;
    717 	unsigned char *buf;
    718 	size_t len;
    719 {
    720 	struct pnp_irq *irq;
    721 
    722 	irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
    723 	irq->mask = buf[0] | (buf[1] << 8);
    724 	if (len > 2)
    725 		irq->flags = buf[2];
    726 	else
    727 		irq->flags = 0x01;
    728 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
    729 	r->numirq++;
    730 	return (0);
    731 }
    732 
    733 static int
    734 pnp_newdma(r, buf, len)
    735 	struct pnpresources *r;
    736 	unsigned char *buf;
    737 	size_t len;
    738 {
    739 	struct pnp_dma *dma;
    740 
    741 	dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
    742 	dma->mask = buf[0];
    743 	dma->flags = buf[1];
    744 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
    745 	r->numdma++;
    746 	return (0);
    747 }
    748 
    749 static int
    750 pnp_newioport(r, buf, len)
    751 	struct pnpresources *r;
    752 	unsigned char *buf;
    753 	size_t len;
    754 {
    755 	struct pnp_io *io;
    756 
    757 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
    758 	io->flags = buf[0];
    759 	io->minbase = buf[1] | (buf[2] << 8);
    760 	io->maxbase = buf[3] | (buf[4] << 8);
    761 	io->align = buf[5];
    762 	io->len = buf[6];
    763 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
    764 	r->numio++;
    765 	return (0);
    766 }
    767 
    768 static int
    769 pnp_compatid(r, buf, len)
    770 	struct pnpresources *r;
    771 	unsigned char *buf;
    772 	size_t len;
    773 {
    774 	struct pnp_compatid *id;
    775 
    776 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
    777 	eisaid_to_string(buf, id->idstr);
    778 	id->next = r->compatids;
    779 	r->compatids = id;
    780 	return (0);
    781 }
    782 
    783 int
    784 pnpbios_io_map(pbt, resc, idx, tagp, hdlp)
    785 	pnpbios_tag_t pbt;
    786 	struct pnpresources *resc;
    787 	int idx;
    788 	bus_space_tag_t *tagp;
    789 	bus_space_handle_t *hdlp;
    790 {
    791 	struct pnp_io *io;
    792 
    793 	if (idx >= resc->numio)
    794 		return (EINVAL);
    795 
    796 	io = SIMPLEQ_FIRST(&resc->io);
    797 	while (idx--)
    798 		io = SIMPLEQ_NEXT(io, next);
    799 
    800 	*tagp = I386_BUS_SPACE_IO;
    801 	return (i386_memio_map(I386_BUS_SPACE_IO, io->minbase, io->len,
    802 			       0, hdlp));
    803 }
    804 
    805 int
    806 pnpbios_getiobase(pbt, resc, idx, tagp, basep)
    807 	pnpbios_tag_t pbt;
    808 	struct pnpresources *resc;
    809 	int idx;
    810 	bus_space_tag_t *tagp;
    811 	int *basep;
    812 {
    813 	struct pnp_io *io;
    814 
    815 	if (idx >= resc->numio)
    816 		return (EINVAL);
    817 
    818 	io = SIMPLEQ_FIRST(&resc->io);
    819 	while (idx--)
    820 		io = SIMPLEQ_NEXT(io, next);
    821 
    822 	if (tagp)
    823 		*tagp = I386_BUS_SPACE_IO;
    824 	if (basep)
    825 		*basep = io->minbase;
    826 	return (0);
    827 }
    828 
    829 void *
    830 pnpbios_intr_establish(pbt, resc, idx, level, fcn, arg)
    831 	pnpbios_tag_t pbt;
    832 	struct pnpresources *resc;
    833 	int idx, level;
    834 	int (*fcn) __P((void *));
    835 	void *arg;
    836 {
    837 	struct pnp_irq *irq;
    838 	int irqnum, type;
    839 
    840 	if (idx >= resc->numirq)
    841 		return (0);
    842 
    843 	irq = SIMPLEQ_FIRST(&resc->irq);
    844 	while (idx--)
    845 		irq = SIMPLEQ_NEXT(irq, next);
    846 
    847 	irqnum = ffs(irq->mask) - 1;
    848 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
    849 
    850 	return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
    851 }
    852 
    853 int
    854 pnpbios_getirqnum(pbt, resc, idx, irqp)
    855 	pnpbios_tag_t pbt;
    856 	struct pnpresources *resc;
    857 	int idx;
    858 	int *irqp;
    859 {
    860 	struct pnp_irq *irq;
    861 
    862 	if (idx >= resc->numirq)
    863 		return (EINVAL);
    864 
    865 	irq = SIMPLEQ_FIRST(&resc->irq);
    866 	while (idx--)
    867 		irq = SIMPLEQ_NEXT(irq, next);
    868 
    869 	*irqp = ffs(irq->mask) - 1;
    870 	return (0);
    871 }
    872 
    873 int
    874 pnpbios_getdmachan(pbt, resc, idx, chanp)
    875 	pnpbios_tag_t pbt;
    876 	struct pnpresources *resc;
    877 	int idx;
    878 	int *chanp;
    879 {
    880 	struct pnp_dma *dma;
    881 
    882 	if (idx >= resc->numdma)
    883 		return (EINVAL);
    884 
    885 	dma = SIMPLEQ_FIRST(&resc->dma);
    886 	while (idx--)
    887 		dma = SIMPLEQ_NEXT(dma, next);
    888 
    889 	*chanp = ffs(dma->mask) - 1;
    890 	return (0);
    891 }
    892