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