Home | History | Annotate | Line # | Download | only in pnpbios
pnpbios.c revision 1.14
      1 /* $NetBSD: pnpbios.c,v 1.14 2000/04/26 20:33:46 thorpej Exp $ */
      2 /*
      3  * Copyright (c) 2000 Christian E. Hopps.  All rights reserved.
      4  * Copyright (c) 1999
      5  * 	Matthias Drochner.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions, and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 /*
     30  * PnP BIOS documentation is available at the following locations.
     31  *
     32  * http://www.microsoft.com/hwdev/download/respec/pnpbios.zip
     33  * http://www.microsoft.com/hwdev/download/respec/biosclar.zip
     34  * http://www.microsoft.com/hwdev/download/respec/devids.txt
     35  *
     36  * PNPBIOSEVENTS is unfinished.  After coding what I did I discovered
     37  * I had no platforms to test on so someone else will need to finish
     38  * it.  I didn't want to toss the code though
     39  */
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/device.h>
     44 #include <sys/malloc.h>
     45 #include <sys/kernel.h>
     46 #include <sys/kthread.h>
     47 
     48 #include <vm/vm.h>
     49 #include <vm/vm_kern.h>
     50 
     51 #include <machine/isa_machdep.h>
     52 #include <machine/segments.h>
     53 
     54 #include <dev/isa/isareg.h>
     55 #include <dev/isapnp/isapnpreg.h>
     56 
     57 #include <arch/i386/pnpbios/pnpbiosvar.h>
     58 #include <arch/i386/pnpbios/pnpbiosreg.h>
     59 
     60 #include "opt_pnpbiosverbose.h"
     61 #include "isadma.h"
     62 #include "locators.h"
     63 
     64 #ifdef PNPBIOSDEBUG
     65 #define	DPRINTF(x) printf x
     66 #else
     67 #define	DPRINTF(x)
     68 #endif
     69 
     70 #ifdef PNPBIOSEVENTSDEBUG
     71 #define	EDPRINTF(x) printf x
     72 #else
     73 #define	EDPRINTF(x)
     74 #endif
     75 
     76 struct pnpbios_softc {
     77 	struct device		sc_dev;
     78 	isa_chipset_tag_t	sc_ic;
     79 	struct proc		*sc_evthread;
     80 
     81 	u_int8_t	*sc_evaddr;
     82 	int		sc_version;
     83 	int		sc_control;
     84 	int		sc_threadrun;
     85 };
     86 
     87 #define	PNPGET4(p)	((p)[0] + ((p)[1] << 8) + \
     88 			((p)[2] << 16) + ((p)[3] << 24))
     89 
     90 /* bios calls */
     91 int pnpbios_getapmtable __P((u_char *, size_t *));
     92 int pnpbios_getdockinfo __P((struct pnpdockinfo *));
     93 int pnpbios_getnode __P((int, int *, u_int8_t *, size_t));
     94 int pnpbios_getnumnodes __P((int *, size_t *));
     95 int pnpbios_getevent __P((u_int16_t *event));
     96 int pnpbios_sendmessage __P((int));
     97 int pnpbios_setnode __P((int, int, const u_int8_t *, size_t));
     98 
     99 void pnpbios_create_event_thread __P((void *));
    100 void pnpbios_event_thread __P((void *));
    101 
    102 /* configuration stuff */
    103 caddr_t pnpbios_mapit __P((u_long, u_long, int));
    104 caddr_t pnpbios_find __P((void));
    105 int pnpbios_match __P((struct device *, struct cfdata *, void *));
    106 void pnpbios_attach __P((struct device *, struct device *, void *));
    107 void pnpbios_printres __P((struct pnpresources *));
    108 int pnpbios_print __P((void *, const char *));
    109 void pnpbios_id_to_string __P((u_int32_t, char *));
    110 void pnpbios_attachnode __P((struct pnpbios_softc *, int, const u_int8_t *,
    111     size_t));
    112 int pnp_scan __P((const u_int8_t **, size_t,struct pnpresources *, int));
    113 int pnpbios_submatch __P((struct device *, struct cfdata *, void *));
    114 extern int pnpbioscall __P((int));
    115 
    116 /* scanning functions */
    117 int pnp_compatid __P((struct pnpresources *, const void *, size_t));
    118 int pnp_newirq __P((struct pnpresources *, const void *, size_t));
    119 int pnp_newdma __P((struct pnpresources *, const void *, size_t));
    120 int pnp_newioport __P((struct pnpresources *, const void *, size_t));
    121 int pnp_newfixedioport __P((struct pnpresources *, const void *, size_t));
    122 #ifdef PNPBIOSDEBUG
    123 int pnp_debugdump __P((struct pnpresources *, const void *, size_t));
    124 #endif
    125 
    126 /*
    127  * small ressource types (beginning with 1)
    128  */
    129 static struct{
    130 	int (*handler) __P((struct pnpresources *, const void *, size_t));
    131 	int minlen, maxlen;
    132 } smallrescs[] = {
    133 	{0, 2, 2}, /* PnP version number */
    134 	{0, 5, 6}, /* logical device id */
    135 	{pnp_compatid, 4, 4}, /* compatible device id */
    136 	{pnp_newirq, 2, 3}, /* irq  descriptor */
    137 	{pnp_newdma, 2, 2}, /* dma  descriptor */
    138 	{0, 0, 1}, /* start dep */
    139 	{0, 0, 0}, /* end dep */
    140 	{pnp_newioport, 7, 7}, /* io descriptor */
    141 	{pnp_newfixedioport, 3, 3}, /* fixed io descriptor */
    142 	{0, -1, -1}, /* reserved */
    143 	{0, -1, -1},
    144 	{0, -1, -1},
    145 	{0, -1, -1},
    146 	{0, 1, 7}, /* vendor defined */
    147 	{0, 1, 1} /* end */
    148 };
    149 
    150 
    151 struct cfattach pnpbios_ca = {
    152 	sizeof(struct pnpbios_softc), pnpbios_match, pnpbios_attach
    153 };
    154 
    155 /*
    156  * Private stack and return value buffer. Spec (1.0a, ch. 4.3) says that
    157  * 1024 bytes must be available to the BIOS function.
    158  */
    159 #define PNPBIOS_BUFSIZE 4096
    160 
    161 int pnpbios_enabled = 1;
    162 size_t pnpbios_entry;
    163 caddr_t pnpbios_scratchbuf;
    164 
    165 /*
    166  * There can be only one of these, and the i386 ISA code needs to
    167  * reference this.
    168  */
    169 struct pnpbios_softc *pnpbios_softc;
    170 
    171 #define PNPBIOS_SIGNATURE ('$' | ('P' << 8) | ('n' << 16) | ('P' << 24))
    172 
    173 caddr_t
    174 pnpbios_find()
    175 {
    176 	caddr_t p, c;
    177 	u_int8_t cksum;
    178 	size_t structlen;
    179 
    180 	for (p = (caddr_t)ISA_HOLE_VADDR(0xf0000);
    181 	     p <= (caddr_t)ISA_HOLE_VADDR(0xffff0);
    182 	     p += 16) {
    183 		if (*(int *)p != PNPBIOS_SIGNATURE)
    184 			continue;
    185 		structlen = *(u_int8_t *)(p + 5);
    186 		if ((structlen < 0x21) ||
    187 		    ((p + structlen - 1) > (caddr_t)ISA_HOLE_VADDR(0xfffff)))
    188 			continue;
    189 
    190 		cksum = 0;
    191 		for (c = p; c < p + structlen; c++)
    192 			cksum += *(u_int8_t *)c;
    193 		if (cksum != 0)
    194 			continue;
    195 
    196 		if (*(char *)(p + 4) != 0x10) {
    197 			printf("unknown version %x\n", *(char *)(p + 4));
    198 			continue;
    199 		}
    200 
    201 		return (p);
    202 	}
    203 
    204 	return (0);
    205 }
    206 
    207 int
    208 pnpbios_probe()
    209 {
    210 
    211 	return (pnpbios_find() != 0);
    212 }
    213 
    214 int
    215 pnpbios_match(parent, match, aux)
    216 	struct device *parent;
    217 	struct cfdata *match;
    218 	void *aux;
    219 {
    220 	struct pnpbios_attach_args *paa = aux;
    221 
    222 	/* These are not the droids you're looking for. */
    223 	if (strcmp(paa->paa_busname, "pnpbios") != 0)
    224 		return (0);
    225 
    226 	/* There can be only one! */
    227 	if (pnpbios_softc != NULL)
    228 		return (0);
    229 
    230 	return (pnpbios_enabled);
    231 }
    232 
    233 caddr_t
    234 pnpbios_mapit(addr, len, prot)
    235 	u_long addr, len;
    236 	int prot;
    237 {
    238 	u_long startpa, pa, endpa;
    239 	vaddr_t startva, va;
    240 
    241 	pa = startpa = i386_trunc_page(addr);
    242 	endpa = i386_round_page(addr + len);
    243 
    244 	va = startva = uvm_km_valloc(kernel_map, endpa - startpa);
    245 	if (!startva)
    246 		return (0);
    247 	for (; pa < endpa; pa += NBPG, va += NBPG)
    248 		pmap_kenter_pa(va, pa, prot);
    249 
    250 	return ((caddr_t)(startva + (addr - startpa)));
    251 }
    252 
    253 void
    254 pnpbios_attach(parent, self, aux)
    255 	struct device *parent, *self;
    256 	void *aux;
    257 {
    258 	struct pnpbios_softc *sc = (struct pnpbios_softc *)self;
    259 	struct pnpbios_attach_args *paa = aux;
    260 	struct pnpdevnode *dn;
    261 	caddr_t p;
    262 	unsigned int codepbase, datapbase, evaddrp;
    263 	caddr_t codeva, datava;
    264 	extern char pnpbiostramp[], epnpbiostramp[];
    265 	int res, num, i, size, idx;
    266 #ifdef PNPBIOSVERBOSE
    267 	struct pnpdockinfo di;
    268 #endif
    269 #ifdef PNPBIOSEVENTS
    270 	int evtype;
    271 #endif
    272 	u_int8_t *buf;
    273 
    274 	pnpbios_softc = sc;
    275 	sc->sc_ic = paa->paa_ic;
    276 
    277 #if NISADMA > 0
    278 	isa_dmainit(sc->sc_ic, I386_BUS_SPACE_IO, &isa_bus_dma_tag, self);
    279 #endif
    280 
    281 	p = pnpbios_find();
    282 	if (!p)
    283 		panic("pnpbios_attach: disappeared");
    284 
    285 	sc->sc_version = *(u_int8_t *)(p + 0x04);
    286 	sc->sc_control = *(u_int8_t *)(p + 0x06);
    287 	evaddrp = *(u_int32_t *)(p + 0x09);
    288 	codepbase = *(u_int32_t *)(p + 0x13);
    289 	datapbase = *(u_int32_t *)(p + 0x1d);
    290 	pnpbios_entry = *(u_int16_t *)(p + 0x11);
    291 
    292 #ifdef PNPBIOSEVENTS
    293 	/* if we have an event mechnism queue a thread to deal with them */
    294 	evtype = (sc->sc_control & PNP_IC_CONTORL_EVENT_MASK);
    295 	if (evtype == PNP_IC_CONTROL_EVENT_POLL) {
    296 		sc->sc_evaddr = pnpbios_mapit(evaddrp, NBPG,
    297 			VM_PROT_READ | VM_PROT_WRITE);
    298 		if (!sc->sc_evaddr)
    299 			printf("%s: couldn't map event flag 0x%08x\n",
    300 			    sc->sc_dev.dv_xname, evaddrp);
    301 	}
    302 #endif
    303 #ifdef PNPBIOSVERBOSE
    304 	printf(": code %x, data %x, entry %x, control %x eventp %x\n%s",
    305 	    codepbase, datapbase, pnpbios_entry, sc->sc_control,
    306 	    (int)evaddrp, self->dv_xname);
    307 #endif
    308 
    309 	codeva = pnpbios_mapit(codepbase, 0x10000,
    310 		VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    311 	datava = pnpbios_mapit(datapbase, 0x10000,
    312 		VM_PROT_READ | VM_PROT_WRITE);
    313 	if (codeva == 0 || datava == 0) {
    314 		printf("no vm for mapping\n");
    315 		return;
    316 	}
    317 	pnpbios_scratchbuf = malloc(PNPBIOS_BUFSIZE, M_DEVBUF, M_NOWAIT);
    318 
    319 	setsegment(&gdt[GPNPBIOSCODE_SEL].sd, codeva, 0xffff,
    320 		   SDT_MEMERA, SEL_KPL, 0, 0);
    321 	setsegment(&gdt[GPNPBIOSDATA_SEL].sd, datava, 0xffff,
    322 		   SDT_MEMRWA, SEL_KPL, 0, 0);
    323 	setsegment(&gdt[GPNPBIOSSCRATCH_SEL].sd,
    324 		   pnpbios_scratchbuf, PNPBIOS_BUFSIZE - 1,
    325 		   SDT_MEMRWA, SEL_KPL, 0, 0);
    326 	setsegment(&gdt[GPNPBIOSTRAMP_SEL].sd,
    327 		   pnpbiostramp, epnpbiostramp - pnpbiostramp - 1,
    328 		   SDT_MEMERA, SEL_KPL, 1, 0);
    329 
    330 	res = pnpbios_getnumnodes(&num, &size);
    331 	if (res) {
    332 		printf("pnpbios_getnumnodes: error %d\n", res);
    333 		return;
    334 	}
    335 
    336 	printf(": nodes %d, max len %d\n", num, size);
    337 	buf = malloc(size, M_DEVBUF, M_NOWAIT);
    338 
    339 #ifdef PNPBIOSEVENTS
    340 	EDPRINTF(("%s: event flag vaddr 0x%08x\n", sc->sc_dev.dv_xname,
    341 	    (int)sc->sc_evaddr));
    342 #endif
    343 
    344 	res = pnpbios_getdockinfo(&di);
    345 	if (res == PNP_RC_SYSTEM_NOT_DOCKED)
    346 		printf("%s: not docked\n", sc->sc_dev.dv_xname);
    347 	else if (res)
    348 		EDPRINTF(("%s: dockinfo fails 0x%02x\n",
    349 		    sc->sc_dev.dv_xname, res));
    350 	else {
    351 		char idstr[8];
    352 		pnpbios_id_to_string(di.di_id, idstr);
    353 		printf("%s: dock id %s serial number %d capabilities 0x%04x\n",
    354 		    sc->sc_dev.dv_xname, idstr, di.di_serial, di.di_cap);
    355 	}
    356 
    357 	idx = 0;
    358 	for (i = 0; i < num && idx != 0xff; i++) {
    359 		int node = idx;
    360 		res = pnpbios_getnode(1, &idx, buf, size);
    361 		if (res) {
    362 			printf("pnpbios_getnode: error %d\n", res);
    363 			continue;
    364 		}
    365 		dn = (struct pnpdevnode *)buf;
    366 		if (dn->dn_handle != node)
    367 			printf("node idx: called %d, got %d\n", node,
    368 			    dn->dn_handle);
    369 		pnpbios_attachnode(sc, node, buf, dn->dn_size);
    370 	}
    371 	if (i != num)
    372 		printf("got only %d nodes\n", i);
    373 	if (idx != 0xff)
    374 		printf("last idx=%x\n", idx);
    375 
    376 	free(buf, M_DEVBUF);
    377 
    378 #ifdef PNPBIOSEVENTS
    379 	/* if we have an event mechnism queue a thread to deal with them */
    380 	/* XXX need to update with irq if we do that */
    381 	if (evtype != PNP_IC_CONTROL_EVENT_NONE) {
    382 		if (evtype != PNP_IC_CONTROL_EVENT_POLL || sc->sc_evaddr) {
    383 			sc->sc_threadrun = 1;
    384 			config_pending_incr();
    385 			kthread_create(pnpbios_create_event_thread, sc);
    386 		}
    387 	}
    388 #endif
    389 }
    390 
    391 int
    392 pnpbios_getnumnodes(nump, sizep)
    393 	int *nump;
    394 	size_t *sizep;
    395 {
    396 	int res;
    397 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    398 
    399 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    400 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    401 	*--help = 2; /* buffer offset for node size */
    402 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    403 	*--help = 0; /* buffer offset for numnodes */
    404 	*--help = PNP_FC_GET_NUM_NODES;
    405 
    406 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    407 	if (res)
    408 		return (res);
    409 
    410 	*nump = *(short *)(pnpbios_scratchbuf + 0);
    411 	*sizep = *(short *)(pnpbios_scratchbuf + 2);
    412 	return (0);
    413 }
    414 
    415 int
    416 pnpbios_getnode(flags, idxp, buf, len)
    417 	int flags;
    418 	int *idxp;
    419 	u_int8_t *buf;
    420 	size_t len;
    421 {
    422 	int res;
    423 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    424 
    425 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    426 	*--help = flags;
    427 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    428 	*--help = 2; /* buffer offset for node data */
    429 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    430 	*--help = 0; /* buffer offset for index in/out */
    431 	*--help = PNP_FC_GET_DEVICE_NODE;
    432 
    433 	*(short *)(pnpbios_scratchbuf + 0) = *idxp;
    434 
    435 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    436 	if (res)
    437 		return (res);
    438 
    439 	*idxp = *(short *)(pnpbios_scratchbuf + 0);
    440 	bcopy(pnpbios_scratchbuf + 2, buf, len);
    441 	return (0);
    442 }
    443 
    444 int
    445 pnpbios_setnode(flags, idx, buf, len)
    446 	int flags, idx;
    447 	const u_int8_t *buf;
    448 	size_t len;
    449 {
    450 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    451 
    452 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    453 	*--help = flags;
    454 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    455 	*--help = 0; /* buffer offset for node data */
    456 	*--help = idx;
    457 	*--help = PNP_FC_SET_DEVICE_NODE;
    458 
    459 	memcpy(pnpbios_scratchbuf, buf, len);
    460 
    461 	return (pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf));
    462 }
    463 
    464 int
    465 pnpbios_getevent(event)
    466 	u_int16_t *event;
    467 {
    468 	int res;
    469 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    470 
    471 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    472 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    473 	*--help = 0; /* buffer offset for message data */
    474 	*--help = PNP_FC_GET_EVENT;
    475 
    476 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    477 	*event = pnpbios_scratchbuf[0] + (pnpbios_scratchbuf[1] << 8);
    478 	return (res);
    479 }
    480 
    481 int
    482 pnpbios_sendmessage(msg)
    483 	int msg;
    484 {
    485 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    486 
    487 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    488 	*--help = msg;
    489 	*--help = PNP_FC_SEND_MESSAGE;
    490 
    491 	return (pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf));
    492 }
    493 
    494 int
    495 pnpbios_getdockinfo(di)
    496 	struct pnpdockinfo *di;
    497 {
    498 	int res;
    499 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    500 
    501 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    502 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    503 	*--help = 0; /* buffer offset for dock info */
    504 	*--help = PNP_FC_GET_DOCK_INFO;
    505 
    506 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    507 	memcpy(di, pnpbios_scratchbuf, sizeof(*di));
    508 	return (res);
    509 }
    510 
    511 /* XXX we don't support more than PNPBIOS_BUFSIZE - (stacklen + 2) */
    512 int
    513 pnpbios_getapmtable(tab, len)
    514 	u_int8_t *tab;
    515 	size_t *len;
    516 {
    517 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    518 	size_t origlen, stacklen;
    519 	int res;
    520 
    521 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    522 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    523 	*--help = 2; /* buffer offset for table */
    524 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    525 	*--help = 0; /* buffer offset for length */
    526 	*--help = PNP_FC_GET_APM_TABLE;
    527 
    528 	origlen = *len;
    529 	stacklen = (caddr_t)help - pnpbios_scratchbuf;
    530 	if (origlen > PNPBIOS_BUFSIZE - stacklen - 2)
    531 		origlen = PNPBIOS_BUFSIZE - stacklen - 2;
    532 	*(u_int16_t *)(pnpbios_scratchbuf) = origlen;
    533 
    534 	res = pnpbioscall(((caddr_t)help) - pnpbios_scratchbuf);
    535 	*len = *(u_int16_t *)pnpbios_scratchbuf;
    536 	if (res)
    537 		return (res);
    538 	if (origlen && *len > origlen) {
    539 		printf("pnpbios: returned apm table exceed requested size\n");
    540 		return (PNP_RC_BUFFER_TOO_SMALL);
    541 	}
    542 	memcpy(tab, pnpbios_scratchbuf + 2, *len);
    543 	return (0);
    544 }
    545 
    546 void
    547 pnpbios_id_to_string(pnpid, s)
    548 	u_int32_t pnpid;
    549 	char *s;
    550 {
    551 	static char hex[] = "0123456789ABCDEF";
    552 	u_int8_t *id;
    553 
    554 	id = (u_int8_t *)&pnpid;
    555 	*s++ = 'A' + (id[0] >> 2) - 1;
    556 	*s++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
    557 	*s++ = 'A' + (id[1] & 0x1f) - 1;
    558 	*s++ = hex[id[2] >> 4];
    559 	*s++ = hex[id[2] & 0x0f];
    560 	*s++ = hex[id[3] >> 4];
    561 	*s++ = hex[id[3] & 0x0f];
    562 	*s = '\0';
    563 }
    564 
    565 void
    566 pnpbios_printres(r)
    567 	struct pnpresources *r;
    568 {
    569 	struct pnp_mem *mem;
    570 	struct pnp_io *io;
    571 	struct pnp_irq *irq;
    572 	struct pnp_dma *dma;
    573 	int p = 0;
    574 
    575 	mem = SIMPLEQ_FIRST(&r->mem);
    576 	if (mem) {
    577 		printf("mem");
    578 		do {
    579 			printf(" %x", mem->minbase);
    580 			if (mem->len > 1)
    581 				printf("-%x", mem->minbase + mem->len - 1);
    582 		} while ((mem = SIMPLEQ_NEXT(mem, next)));
    583 		p++;
    584 	}
    585 	io = SIMPLEQ_FIRST(&r->io);
    586 	if (io) {
    587 		if (p++)
    588 			printf(", ");
    589 		printf("io");
    590 		do {
    591 			printf(" %x", io->minbase);
    592 			if (io->len > 1)
    593 				printf("-%x", io->minbase + io->len - 1);
    594 		} while ((io = SIMPLEQ_NEXT(io, next)));
    595 	}
    596 	irq = SIMPLEQ_FIRST(&r->irq);
    597 	if (irq) {
    598 		if (p++)
    599 			printf(", ");
    600 		printf("irq");
    601 		do {
    602 			printf(" %d", ffs(irq->mask) - 1);
    603 		} while ((irq = SIMPLEQ_NEXT(irq, next)));
    604 	}
    605 	dma = SIMPLEQ_FIRST(&r->dma);
    606 	if (dma) {
    607 		if (p)
    608 			printf(", ");
    609 		printf("dma");
    610 		do {
    611 			printf(" %d", ffs(dma->mask) - 1);
    612 		} while ((dma = SIMPLEQ_NEXT(dma, next)));
    613 	}
    614 }
    615 
    616 int
    617 pnpbios_print(aux, pnp)
    618 	void *aux;
    619 	const char *pnp;
    620 {
    621 	struct pnpbiosdev_attach_args *aa = aux;
    622 
    623 	if (pnp)
    624 		return (QUIET);
    625 
    626 	printf(" index %d (%s", aa->idx, aa->primid);
    627 	if (aa->resc->longname)
    628 		printf(", %s", aa->resc->longname);
    629 	if (aa->idstr != aa->primid)
    630 		printf(", attached as %s", aa->idstr);
    631 	printf(")");
    632 
    633 	return (0);
    634 }
    635 
    636 void
    637 pnpbios_print_devres(dev, aa)
    638 	struct device *dev;
    639 	struct pnpbiosdev_attach_args *aa;
    640 {
    641 
    642 	printf("%s: ", dev->dv_xname);
    643 	pnpbios_printres(aa->resc);
    644 	printf("\n");
    645 }
    646 
    647 int
    648 pnpbios_submatch(parent, match, aux)
    649 	struct device *parent;
    650 	struct cfdata *match;
    651 	void *aux;
    652 {
    653 	struct pnpbiosdev_attach_args *aa = aux;
    654 
    655 	if (match->cf_loc[PNPBIOSCF_INDEX] != PNPBIOSCF_INDEX_DEFAULT &&
    656 	    match->cf_loc[PNPBIOSCF_INDEX] != aa->idx)
    657 		return (0);
    658 
    659 	return ((*match->cf_attach->ca_match)(parent, match, aux));
    660 }
    661 
    662 void
    663 pnpbios_attachnode(sc, idx, buf, len)
    664 	struct pnpbios_softc *sc;
    665 	int idx;
    666 	const u_int8_t *buf;
    667 	size_t len;
    668 {
    669 	struct pnpdevnode *dn;
    670 	const u_int8_t *p;
    671 	char idstr[8];
    672 	struct pnpresources r, s;
    673 	struct pnpbiosdev_attach_args aa;
    674 	struct pnp_compatid *compatid;
    675 	int res, i;
    676 
    677 	dn = (struct pnpdevnode *)buf;
    678 	pnpbios_id_to_string(dn->dn_product, idstr);
    679 	p = (u_char *)(dn + 1);
    680 
    681 	DPRINTF(("%s: type 0x%02x subtype 0x%02x dpi 0x%02x attr 0x%04x:\n",
    682 	    idstr, dn->dn_type, dn->dn_subtype, dn->dn_dpi, dn->dn_attr));
    683 	DPRINTF(("%s: allocated config scan:\n", idstr));
    684 	res = pnp_scan(&p, len - 12, &r, 0);
    685 	if (res < 0) {
    686 		printf("error in config data\n");
    687 		goto dump;
    688 	}
    689 
    690 	/*
    691 	 * the following is consistency check only for now
    692 	 */
    693 	DPRINTF(("\tpossible config scan:\n"));
    694 	res = pnp_scan(&p, len - (p - buf), &s, 0);
    695 	if (res < 0) {
    696 		printf("error in possible configuration\n");
    697 		goto dump;
    698 	}
    699 
    700 	DPRINTF(("\tcompat id scan:\n"));
    701 	res = pnp_scan(&p, len - (p - buf), &s, 0);
    702 	if (res < 0) {
    703 		printf("error in compatible ID\n");
    704 		goto dump;
    705 	}
    706 
    707 	if (p != buf + len) {
    708 		printf("%s: length mismatch in node %d: used %d of %d Bytes\n",
    709 		       sc->sc_dev.dv_xname, idx, p - buf, len);
    710 		if (p > buf + len) {
    711 			/* XXX shouldn't happen - pnp_scan should catch it */
    712 			goto dump;
    713 		}
    714 		/* Crappy BIOS: Buffer is not fully used. Be generous. */
    715 	}
    716 
    717 	if (r.nummem + r.numio + r.numirq + r.numdma == 0) {
    718 #ifdef PNPBIOSVERBOSE
    719 		printf("%s", idstr);
    720 		if (r.longname)
    721 			printf(", %s", r.longname);
    722 		compatid = s.compatids;
    723 		while (compatid) {
    724 			printf(", %s", compatid->idstr);
    725 			compatid = compatid->next;
    726 		}
    727 		printf(" at %s index %d disabled\n", sc->sc_dev.dv_xname, idx);
    728 #endif
    729 		return;
    730 	}
    731 
    732 	aa.pbt = 0; /* XXX placeholder */
    733 	aa.idx = idx;
    734 	aa.resc = &r;
    735 	aa.ic = sc->sc_ic;
    736 	aa.primid = idstr;
    737 
    738 	/* first try the specific device ID */
    739 	aa.idstr = idstr;
    740 	if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    741 	    pnpbios_submatch))
    742 		return;
    743 
    744 	/* if no driver was found, try compatible IDs */
    745 	compatid = s.compatids;
    746 	while (compatid) {
    747 		aa.idstr = compatid->idstr;
    748 		if (config_found_sm((struct device *)sc, &aa, pnpbios_print,
    749 		    pnpbios_submatch))
    750 			return;
    751 		compatid = compatid->next;
    752 	}
    753 
    754 #ifdef PNPBIOSVERBOSE
    755 	printf("%s", idstr);
    756 	if (r.longname)
    757 		printf(", %s", r.longname);
    758 	compatid = s.compatids;
    759 	while (compatid) {
    760 		printf(", %s", compatid->idstr);
    761 		compatid = compatid->next;
    762 	}
    763 	printf(" (");
    764 	pnpbios_printres(&r);
    765 	printf(") at %s index %d ignored\n", sc->sc_dev.dv_xname, idx);
    766 #endif
    767 
    768 	return;
    769 
    770 	/* XXX should free ressource lists */
    771 
    772 dump:
    773 	i = 0;
    774 #ifdef PNPBIOSDEBUG
    775 	/* print some useful info */
    776 	if (len >= sizeof(*dn)) {
    777 		printf("%s idx %d size %d type 0x%x:0x%x:0x%x attr 0x%x\n",
    778 		    idstr, dn->dn_handle, dn->dn_size, dn->dn_type,
    779 		    dn->dn_subtype, dn->dn_dpi, dn->dn_attr);
    780 		i += sizeof(*dn);
    781 	}
    782 #endif
    783 	for (; i < len; i++)
    784 		printf(" %02x", buf[i]);
    785 	printf("\n");
    786 }
    787 
    788 int
    789 pnp_scan(bufp, maxlen, r, in_depends)
    790 	const u_int8_t **bufp;
    791 	size_t maxlen;
    792 	struct pnpresources *r;
    793 	int in_depends;
    794 {
    795 	const void *start;
    796 	const u_int8_t *p;
    797 	struct pnp_mem *mem;
    798 	int tag, type, len;
    799 	char *idstr;
    800 	int i;
    801 
    802 	p = *bufp;
    803 
    804 	bzero(r, sizeof(*r));
    805 	SIMPLEQ_INIT(&r->mem);
    806 	SIMPLEQ_INIT(&r->io);
    807 	SIMPLEQ_INIT(&r->irq);
    808 	SIMPLEQ_INIT(&r->dma);
    809 
    810 	for (;;) {
    811 		if (p >= *bufp + maxlen) {
    812 			printf("pnp_scanresources: end of buffer\n");
    813 			return (-1);
    814 		}
    815 		start = p;
    816 		tag = *p;
    817 		if (tag & ISAPNP_LARGE_TAG) {
    818 			len = *(u_int16_t *)(p + 1);
    819 			p += sizeof(struct pnplargeres) + len;
    820 
    821 			switch (tag) {
    822 			case ISAPNP_TAG_MEM_RANGE_DESC: {
    823 				const struct pnpmem16rangeres *res = start;
    824 				if (len != sizeof(*res) - 3) {
    825 					printf("pnp_scan: bad mem desc\n");
    826 					return (-1);
    827 				}
    828 
    829 				mem = malloc(sizeof(struct pnp_mem),
    830 					     M_DEVBUF, M_WAITOK);
    831 				mem->flags = res->r_flags;
    832 				mem->minbase = res->r_minbase << 8;
    833 				mem->maxbase = res->r_maxbase << 8;
    834 				mem->align = res->r_align;
    835 				if (mem->align == 0)
    836 					mem->align = 0x10000;
    837 				mem->len = res->r_len << 8;
    838 				DPRINTF(("\ttag memrange "));
    839 				goto gotmem;
    840 			}
    841 			case ISAPNP_TAG_ANSI_IDENT_STRING: {
    842 				const struct pnpansiidentres *res = start;
    843 				if (in_depends)
    844 					printf("ID in dep?\n");
    845 				idstr = malloc(len + 1, M_DEVBUF, M_NOWAIT);
    846 				for (i = 0; i < len; i++)
    847 					idstr[i] = res->r_id[i];
    848 				idstr[len] = '\0';
    849 
    850 				DPRINTF(("\ttag ansiident %s\n", idstr));
    851 
    852 				if (idstr[0] == '\0') {
    853 					/* disabled device */
    854 					free(idstr, M_DEVBUF);
    855 					break;
    856 				}
    857 				r->longname = idstr;
    858 				break;
    859 			}
    860 			case ISAPNP_TAG_MEM32_RANGE_DESC: {
    861 				const struct pnpmem32rangeres *res = start;
    862 				if (len != sizeof(*res) - 3) {
    863 					printf("pnp_scan: bad mem32 desc\n");
    864 					return (-1);
    865 				}
    866 
    867 				mem = malloc(sizeof(struct pnp_mem),
    868 					     M_DEVBUF, M_WAITOK);
    869 				mem->flags = res->r_flags;
    870 				mem->minbase = res->r_minbase;
    871 				mem->maxbase = res->r_maxbase;
    872 				mem->align = res->r_align;
    873 				mem->len = res->r_len;
    874 				DPRINTF(("\ttag mem32range "));
    875 				goto gotmem;
    876 			}
    877 			case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC: {
    878 				const struct pnpfixedmem32rangeres *res = start;
    879 				if (len != sizeof(*res) - 3) {
    880 					printf("pnp_scan: bad mem32 desc\n");
    881 					return (-1);
    882 				}
    883 
    884 				mem = malloc(sizeof(struct pnp_mem),
    885 					     M_DEVBUF, M_WAITOK);
    886 				mem->flags = res->r_flags;
    887 				mem->minbase = res->r_base;
    888 				mem->maxbase = mem->minbase;
    889 				mem->align = 0;
    890 				mem->len = res->r_len;
    891 				DPRINTF(("\ttag fixedmem32range "));
    892 gotmem:
    893 				if (mem->len == 0) { /* disabled */
    894 					DPRINTF(("zeroed\n"));
    895 					free(mem, M_DEVBUF);
    896 					break;
    897 				}
    898 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
    899 				r->nummem++;
    900 
    901 				DPRINTF(("flags %02x min %08x max %08x "
    902 				    "align %08x len %08x\n", mem->flags,
    903 				    mem->minbase, mem->maxbase, mem->align,
    904 				    mem->len));
    905 
    906 				break;
    907 			}
    908 			case ISAPNP_TAG_UNICODE_IDENT_STRING:
    909 			case ISAPNP_TAG_VENDOR_DEFINED:
    910 			default:
    911 #ifdef PNPBIOSDEBUG
    912 				pnp_debugdump(r, start, len);
    913 #endif
    914 				break;
    915 			}
    916 		} else {
    917 			type = (tag >> 3) & 0x0f;
    918 			len = tag & 0x07;
    919 			p += 1 + len;
    920 
    921 			if (type == 0 ||
    922 			    len < smallrescs[type - 1].minlen ||
    923 			    len > smallrescs[type - 1].maxlen) {
    924 				printf("pnp_scan: bad small resource\n");
    925 				return (-1);
    926 			}
    927 			if (type == ISAPNP_TAG_END) {
    928 #ifdef PNPBIOSDEBUG
    929 				const struct pnpendres *res = start;
    930 #endif
    931 				if (in_depends) {
    932 					/*
    933 					 * this seems to occur and is
    934 					 * an optimization to not require
    935 					 * the end dep in a depend
    936 					 * that ends the section
    937 					 */
    938 					p -= 1 + len;
    939 				}
    940 				DPRINTF(("\ttag end cksum %02x\n",
    941 				    res->r_cksum));
    942 				break;
    943 			}
    944 			if (type == ISAPNP_TAG_DEP_START) {
    945 #ifdef PNPBIOSDEBUG
    946 				const struct pnpdepstartres *res = start;
    947 #endif
    948 				struct pnpresources *new, *last;
    949 				int rv;
    950 
    951 				DPRINTF(("\ttag startdep flags %02x\n",
    952 				    len ? res->r_pri : ISAPNP_DEP_ACCEPTABLE));
    953 
    954 				if (r->dependant_link) {
    955 					printf("second dep?\n");
    956 					return (-1);
    957 				}
    958 				/* XXX not sure about this */
    959 				if (in_depends) {
    960 					*bufp = p;
    961 					return (1);
    962 				}
    963 				last = r;
    964 				do {
    965 					new = malloc(sizeof(*new),
    966 						     M_DEVBUF, M_NOWAIT);
    967 
    968 					rv = pnp_scan(&p, maxlen - (p - *bufp),
    969 						       new, 1);
    970 					if (rv < 0) {
    971 						printf("error in dependant "
    972 						    "function\n");
    973 						free(new, M_DEVBUF);
    974 						return (-1);
    975 					}
    976 					last->dependant_link = new;
    977 					last = new;
    978 				} while (rv > 0);
    979 				continue;
    980 			}
    981 			if (type == ISAPNP_TAG_DEP_END) {
    982 				DPRINTF(("\ttag enddep\n"));
    983 				if (!in_depends) {
    984 					printf("tag %d end dep?\n", tag);
    985 					return (-1);
    986 				}
    987 				break;
    988 			}
    989 			if (!smallrescs[type - 1].handler) {
    990 #ifdef PNPBIOSDEBUG
    991 				pnp_debugdump(r, start, len);
    992 #endif
    993 			} else if (
    994 			    (*smallrescs[type - 1].handler)(r, start, len))
    995 				return (-1);
    996 		}
    997 	}
    998 	*bufp = p;
    999 	return (0);
   1000 }
   1001 
   1002 int
   1003 pnp_newirq(r, vres, len)
   1004 	struct pnpresources *r;
   1005 	const void *vres;
   1006 	size_t len;
   1007 {
   1008 	const struct pnpirqres *res;
   1009 	struct pnp_irq *irq;
   1010 
   1011 	res = vres;
   1012 	if (res->r_mask == 0) { /* disabled */
   1013 		DPRINTF(("\ttag irq zeroed\n"));
   1014 		return (0);
   1015 	}
   1016 	irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
   1017 	irq->mask = res->r_mask;
   1018 	if (len > 2)
   1019 		irq->flags = res->r_info;
   1020 	else
   1021 		irq->flags = 0x01;
   1022 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
   1023 	r->numirq++;
   1024 
   1025 	DPRINTF(("\ttag irq flags %02x mask %04x\n", irq->flags,irq->mask));
   1026 
   1027 	return (0);
   1028 }
   1029 
   1030 int
   1031 pnp_newdma(r, vres, len)
   1032 	struct pnpresources *r;
   1033 	const void *vres;
   1034 	size_t len;
   1035 {
   1036 	const struct pnpdmares *res;
   1037 	struct pnp_dma *dma;
   1038 
   1039 	res = vres;
   1040 	if (res->r_mask == 0) { /* disabled */
   1041 		DPRINTF(("\ttag dma zeroed\n"));
   1042 		return (0);
   1043 	}
   1044 	dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
   1045 	dma->mask = res->r_mask;
   1046 	dma->flags = res->r_flags;
   1047 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
   1048 	r->numdma++;
   1049 
   1050 	DPRINTF(("\ttag dma flags %02x mask %02x\n", dma->flags,dma->mask));
   1051 
   1052 	return (0);
   1053 }
   1054 
   1055 int
   1056 pnp_newioport(r, vres, len)
   1057 	struct pnpresources *r;
   1058 	const void *vres;
   1059 	size_t len;
   1060 {
   1061 	const struct pnpportres *res;
   1062 	struct pnp_io *io;
   1063 
   1064 	res = vres;
   1065 	if (res->r_len == 0) { /* disabled */
   1066 		DPRINTF(("\ttag io zeroed\n"));
   1067 		return (0);
   1068 	}
   1069 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
   1070 	io->flags = res->r_flags;
   1071 	io->minbase = res->r_minbase;
   1072 	io->maxbase = res->r_maxbase;
   1073 	io->align = res->r_align;
   1074 	io->len = res->r_len;
   1075 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
   1076 	r->numio++;
   1077 
   1078 	DPRINTF(("\ttag io flags %02x min %04x max %04x align "
   1079 	    "0x%02x len 0x%02x\n", io->flags, io->minbase, io->maxbase,
   1080 	    io->align, io->len));
   1081 
   1082 	return (0);
   1083 }
   1084 
   1085 int
   1086 pnp_newfixedioport(r, vres, len)
   1087 	struct pnpresources *r;
   1088 	const void *vres;
   1089 	size_t len;
   1090 {
   1091 	const struct pnpfixedportres *res;
   1092 	struct pnp_io *io;
   1093 
   1094 	res = vres;
   1095 	if (res->r_len == 0) { /* disabled */
   1096 		DPRINTF(("\ttag fixedio zeroed\n"));
   1097 		return (0);
   1098 	}
   1099 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
   1100 	io->flags = 1; /* 10 bit decoding */
   1101 	io->minbase = io->maxbase = res->r_base;
   1102 	io->align = 1;
   1103 	io->len = res->r_len;
   1104 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
   1105 	r->numio++;
   1106 
   1107 	DPRINTF(("\ttag fixedio flags %02x base %04x align %02x len %02x\n",
   1108 	    io->flags, io->minbase, io->align, io->len));
   1109 
   1110 	return (0);
   1111 }
   1112 
   1113 int
   1114 pnp_compatid(r, vres, len)
   1115 	struct pnpresources *r;
   1116 	const void *vres;
   1117 	size_t len;
   1118 {
   1119 	const struct pnpcompatres *res;
   1120 	struct pnp_compatid *id;
   1121 
   1122 	res = vres;
   1123 	id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
   1124 	pnpbios_id_to_string(res->r_id, id->idstr);
   1125 	id->next = r->compatids;
   1126 	r->compatids = id;
   1127 
   1128 	DPRINTF(("\ttag compatid %s\n", id->idstr));
   1129 
   1130 	return (0);
   1131 }
   1132 
   1133 #ifdef PNPBIOSDEBUG
   1134 int
   1135 pnp_debugdump(r, vres, len)
   1136 	struct pnpresources *r;
   1137 	const void *vres;
   1138 	size_t len;
   1139 {
   1140 	const u_int8_t *res;
   1141 	int type, i;
   1142 
   1143 	if (res[0] & ISAPNP_LARGE_TAG) {
   1144 		type = res[0] & 0x7f;
   1145 		printf("\tTAG %02x len %04x %s", type, len, len ? "data" : "");
   1146 		i = 3;
   1147 	} else {
   1148 		type = (res[0] >> 3) & 0x0f;
   1149 		printf("\tTAG %02x len %02x %s", type, len, len ? "data" : "");
   1150 		i = 1;
   1151 	}
   1152 	for (; i < len; i++)
   1153 		printf(" %02x", res[i]);
   1154 	printf("\n");
   1155 
   1156 	return (0);
   1157 }
   1158 #endif
   1159 
   1160 int
   1161 pnpbios_io_map(pbt, resc, idx, tagp, hdlp)
   1162 	pnpbios_tag_t pbt;
   1163 	struct pnpresources *resc;
   1164 	int idx;
   1165 	bus_space_tag_t *tagp;
   1166 	bus_space_handle_t *hdlp;
   1167 {
   1168 	struct pnp_io *io;
   1169 
   1170 	if (idx >= resc->numio)
   1171 		return (EINVAL);
   1172 
   1173 	io = SIMPLEQ_FIRST(&resc->io);
   1174 	while (idx--)
   1175 		io = SIMPLEQ_NEXT(io, next);
   1176 
   1177 	*tagp = I386_BUS_SPACE_IO;
   1178 	return (i386_memio_map(I386_BUS_SPACE_IO, io->minbase, io->len,
   1179 			       0, hdlp));
   1180 }
   1181 
   1182 void
   1183 pnpbios_io_unmap(pbt, resc, idx, tag, hdl)
   1184 	pnpbios_tag_t pbt;
   1185 	struct pnpresources *resc;
   1186 	int idx;
   1187 	bus_space_tag_t tag;
   1188 	bus_space_handle_t hdl;
   1189 {
   1190 	struct pnp_io *io;
   1191 
   1192 	if (idx >= resc->numio)
   1193 		return;
   1194 
   1195 	io = SIMPLEQ_FIRST(&resc->io);
   1196 	while (idx--)
   1197 		io = SIMPLEQ_NEXT(io, next);
   1198 
   1199 	i386_memio_unmap(tag, hdl, io->len);
   1200 }
   1201 
   1202 int
   1203 pnpbios_getiobase(pbt, resc, idx, tagp, basep)
   1204 	pnpbios_tag_t pbt;
   1205 	struct pnpresources *resc;
   1206 	int idx;
   1207 	bus_space_tag_t *tagp;
   1208 	int *basep;
   1209 {
   1210 	struct pnp_io *io;
   1211 
   1212 	if (idx >= resc->numio)
   1213 		return (EINVAL);
   1214 
   1215 	io = SIMPLEQ_FIRST(&resc->io);
   1216 	while (idx--)
   1217 		io = SIMPLEQ_NEXT(io, next);
   1218 
   1219 	if (tagp)
   1220 		*tagp = I386_BUS_SPACE_IO;
   1221 	if (basep)
   1222 		*basep = io->minbase;
   1223 	return (0);
   1224 }
   1225 
   1226 void *
   1227 pnpbios_intr_establish(pbt, resc, idx, level, fcn, arg)
   1228 	pnpbios_tag_t pbt;
   1229 	struct pnpresources *resc;
   1230 	int idx, level;
   1231 	int (*fcn) __P((void *));
   1232 	void *arg;
   1233 {
   1234 	struct pnp_irq *irq;
   1235 	int irqnum, type;
   1236 
   1237 	if (idx >= resc->numirq)
   1238 		return (0);
   1239 
   1240 	irq = SIMPLEQ_FIRST(&resc->irq);
   1241 	while (idx--)
   1242 		irq = SIMPLEQ_NEXT(irq, next);
   1243 
   1244 	irqnum = ffs(irq->mask) - 1;
   1245 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
   1246 
   1247 	return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
   1248 }
   1249 
   1250 int
   1251 pnpbios_getirqnum(pbt, resc, idx, irqp, istp)
   1252 	pnpbios_tag_t pbt;
   1253 	struct pnpresources *resc;
   1254 	int idx;
   1255 	int *irqp;
   1256 	int *istp;
   1257 {
   1258 	struct pnp_irq *irq;
   1259 
   1260 	if (idx >= resc->numirq)
   1261 		return (EINVAL);
   1262 
   1263 	irq = SIMPLEQ_FIRST(&resc->irq);
   1264 	while (idx--)
   1265 		irq = SIMPLEQ_NEXT(irq, next);
   1266 
   1267 	if (irqp != NULL)
   1268 		*irqp = ffs(irq->mask) - 1;
   1269 	if (istp != NULL)
   1270 		*istp = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
   1271 	return (0);
   1272 }
   1273 
   1274 int
   1275 pnpbios_getdmachan(pbt, resc, idx, chanp)
   1276 	pnpbios_tag_t pbt;
   1277 	struct pnpresources *resc;
   1278 	int idx;
   1279 	int *chanp;
   1280 {
   1281 	struct pnp_dma *dma;
   1282 
   1283 	if (idx >= resc->numdma)
   1284 		return (EINVAL);
   1285 
   1286 	dma = SIMPLEQ_FIRST(&resc->dma);
   1287 	while (idx--)
   1288 		dma = SIMPLEQ_NEXT(dma, next);
   1289 
   1290 	*chanp = ffs(dma->mask) - 1;
   1291 	return (0);
   1292 }
   1293 
   1294 #ifdef PNPBIOSEVENTS
   1295 void
   1296 pnpbios_create_event_thread(arg)
   1297 	void *arg;
   1298 {
   1299 	struct pnpbios_softc *sc;
   1300 
   1301 	sc = arg;
   1302 	if (kthread_create1(pnpbios_event_thread, sc, &sc->sc_evthread,
   1303 	    "%s", sc->sc_dev.dv_xname))
   1304 		panic("pnpbios_create_event_thread");
   1305 }
   1306 
   1307 void
   1308 pnpbios_event_thread(arg)
   1309 	void *arg;
   1310 {
   1311 	struct pnpbios_softc *sc;
   1312 	u_int16_t event;
   1313 	u_int evflag;
   1314 	int rv, poll;
   1315 
   1316 	sc = arg;
   1317 	if ((sc->sc_control & PNP_IC_CONTORL_EVENT_MASK)
   1318 	    != PNP_IC_CONTROL_EVENT_POLL)
   1319 		poll = 0;
   1320 	else {
   1321 		poll = hz;
   1322 		rv = pnpbios_sendmessage(PNP_CM_PNP_OS_ACTIVE);
   1323 		EDPRINTF(("pnpbios: os active returns 0x%02x\n", rv));
   1324 	}
   1325 
   1326 	config_pending_decr();
   1327 
   1328 	goto start;
   1329 	while (sc->sc_threadrun) {
   1330 		/* maybe we have an event */
   1331 		if (!poll)
   1332 			(void)tsleep(pnpbios_event_thread, PWAIT,
   1333 			    "pnpbiosevent", 0);
   1334 		else if (((evflag = *sc->sc_evaddr) & 0x01) == 0) {
   1335 			if (evflag)
   1336 				EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
   1337 			(void)tsleep(pnpbios_event_thread, PWAIT,
   1338 			    "pnpbiosevent", poll);
   1339 			continue;
   1340 		} else {
   1341 			EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
   1342 		}
   1343 start:
   1344 		if ((rv = pnpbios_getevent(&event))) {
   1345 			EDPRINTF(("pnpbios: getevent rc: 0x%02x\n", rv));
   1346 #ifdef DIAGNOSTIC
   1347 			if (rv != PNP_RC_EVENTS_NOT_PENDING)
   1348 				printf("%s: getevent failed: %d\n",
   1349 				    sc->sc_dev.dv_xname, rv);
   1350 #endif
   1351 			continue;
   1352 		}
   1353 		switch (event) {
   1354 		case PNP_EID_ABOUT_TO_CHANGE_CONFIG:
   1355 			EDPRINTF(("pnpbios: about to change event\n"));
   1356 			break;
   1357 		case PNP_EID_DOCK_CHANGED:
   1358 			EDPRINTF(("pnpbios: dock changed event\n"));
   1359 			break;
   1360 		case PNP_EID_SYSTEM_DEVICE_CHANGED:
   1361 			EDPRINTF(("pnpbios: system device changed event\n"));
   1362 			break;
   1363 		case PNP_EID_CONFIG_CHANGE_FAILED:
   1364 			EDPRINTF(("pnpbios: config changed event\n"));
   1365 			break;
   1366 		case PNP_EID_UNKNOWN_SYSTEM_EVENT:
   1367 #ifdef DIAGNOSTIC
   1368 			printf("%s: \"unknown system event\"\n",
   1369 			    sc->sc_dev.dv_xname);
   1370 #endif
   1371 			break;
   1372 		default:
   1373 #ifdef DIAGNOSTIC
   1374 #ifdef PNPBIOSVERBOSE
   1375 			if (event & PNP_EID_OEM_DEFINED_BIT)
   1376 				printf("%s: vendor defined event 0x%04x\n",
   1377 				    sc->sc_dev.dv_xname, event);
   1378 			else
   1379 #endif
   1380 				printf("%s: unkown event 0x%04x\n",
   1381 				    sc->sc_dev.dv_xname, event);
   1382 #endif
   1383 			break;
   1384 		}
   1385 	}
   1386 
   1387 	pnpbios_sendmessage(PNP_CM_PNP_OS_INACTIVE);
   1388 	kthread_exit(0);
   1389 }
   1390 #endif	/* PNPBIOSEVENTS */
   1391 
   1392