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