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