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