Home | History | Annotate | Line # | Download | only in pnpbios
      1 /* $NetBSD: pnpbios.c,v 1.77 2021/08/07 16:18:55 thorpej Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2000 Jason R. Thorpe.  All rights reserved.
      5  * Copyright (c) 2000 Christian E. Hopps.  All rights reserved.
      6  * Copyright (c) 1999
      7  * 	Matthias Drochner.  All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions, and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     28  * SUCH DAMAGE.
     29  */
     30 
     31 /*
     32  * PnP BIOS documentation is available at the following locations.
     33  *
     34  * http://www.microsoft.com/hwdev/download/respec/pnpbios.zip
     35  * http://www.microsoft.com/hwdev/download/respec/biosclar.zip
     36  * http://www.microsoft.com/hwdev/download/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.77 2021/08/07 16:18:55 thorpej 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 resource 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 	sc->sc_dev = self;
    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 = *(uint8_t *)(p + 0x04);
    300 	sc->sc_control = *(uint8_t *)(p + 0x06);
    301 	evaddrp = *(uint32_t *)(p + 0x09);
    302 	codepbase = *(uint32_t *)(p + 0x13);
    303 	datapbase = *(uint32_t *)(p + 0x1d);
    304 	pnpbios_entry = *(uint16_t *)(p + 0x11);
    305 
    306 	if (pnpbiosverbose) {
    307 		aprint_normal(": code %x, data %x, entry %x, control %x,"
    308 			      " eventp %x\n%s",
    309 		    codepbase, datapbase, pnpbios_entry, sc->sc_control,
    310 		    (unsigned int)evaddrp, device_xname(self));
    311 	}
    312 
    313 #ifdef PNPBIOSEVENTS
    314 	/* if we have an event mechnism queue a thread to deal with them */
    315 	evtype = (sc->sc_control & PNP_IC_CONTORL_EVENT_MASK);
    316 	if (evtype == PNP_IC_CONTROL_EVENT_POLL) {
    317 		sc->sc_evaddr = pnpbios_mapit(evaddrp, PAGE_SIZE,
    318 			VM_PROT_READ | VM_PROT_WRITE);
    319 		if (!sc->sc_evaddr)
    320 			aprint_error_dev(self, "couldn't map event flag 0x%08x\n",
    321 			    evaddrp);
    322 	}
    323 #endif
    324 
    325 	codeva = pnpbios_mapit(codepbase, 0x10000,
    326 		VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE);
    327 	datava = pnpbios_mapit(datapbase, 0x10000,
    328 		VM_PROT_READ | VM_PROT_WRITE);
    329 	if (codeva == 0 || datava == 0) {
    330 		aprint_error(": no vm for mapping\n");
    331 		return;
    332 	}
    333 	pnpbios_scratchbuf = malloc(PNPBIOS_BUFSIZE, M_DEVBUF, M_WAITOK);
    334 
    335 	setsegment(&gdtstore[GPNPBIOSCODE_SEL].sd, codeva, 0xffff,
    336 		   SDT_MEMERA, SEL_KPL, 0, 0);
    337 	setsegment(&gdtstore[GPNPBIOSDATA_SEL].sd, datava, 0xffff,
    338 		   SDT_MEMRWA, SEL_KPL, 0, 0);
    339 	setsegment(&gdtstore[GPNPBIOSSCRATCH_SEL].sd,
    340 		   pnpbios_scratchbuf, PNPBIOS_BUFSIZE - 1,
    341 		   SDT_MEMRWA, SEL_KPL, 0, 0);
    342 	setsegment(&gdtstore[GPNPBIOSTRAMP_SEL].sd,
    343 		   pnpbiostramp, epnpbiostramp - pnpbiostramp - 1,
    344 		   SDT_MEMERA, SEL_KPL, 1, 0);
    345 
    346 	res = pnpbios_getnumnodes(&num, &size);
    347 	if (res) {
    348 		aprint_error(": pnpbios_getnumnodes: error %d\n", res);
    349 		return;
    350 	}
    351 
    352 	aprint_normal(": nodes %d, max len %d\n", num, size);
    353 
    354 #ifdef PNPBIOSEVENTS
    355 	EDPRINTF(("%s: event flag vaddr 0x%08x\n", device_xname(self),
    356 	    (int)sc->sc_evaddr));
    357 
    358 	/* Set initial dock status. */
    359 	sc->sc_docked = -1;
    360 	(void) pnpbios_update_dock_status(sc);
    361 #endif
    362 
    363 	/* Enumerate the device nodes. */
    364 	pnpbios_enumerate(sc);
    365 
    366 #ifdef PNPBIOSEVENTS
    367 	/* if we have an event mechnism queue a thread to deal with them */
    368 	/* XXX need to update with irq if we do that */
    369 	if (evtype != PNP_IC_CONTROL_EVENT_NONE) {
    370 		if (evtype != PNP_IC_CONTROL_EVENT_POLL || sc->sc_evaddr) {
    371 			sc->sc_threadrun = 1;
    372 			config_pending_incr(sc->sc_dev);
    373 			if (kthread_create(PRI_NONE, 0, NULL,
    374 			    pnpbios_event_thread, sc, &sc->sc_evthread,
    375 			    "%s", device_xname(self)))
    376 			    	panic("pnpbios: create event thread");
    377 		}
    378 	}
    379 #endif
    380 }
    381 
    382 static void
    383 pnpbios_enumerate(struct pnpbios_softc *sc)
    384 {
    385 	int res, num, i, size, idx, dynidx;
    386 	struct pnpdevnode *dn;
    387 	uint8_t *buf;
    388 
    389 	res = pnpbios_getnumnodes(&num, &size);
    390 	if (res) {
    391 		aprint_error_dev(sc->sc_dev, "pnpbios_getnumnodes: error %d\n",
    392 		    res);
    393 		return;
    394 	}
    395 
    396 	buf = malloc(size, M_DEVBUF, M_WAITOK);
    397 
    398 	/*
    399 	 * Loop through the list of indices getting data and match/attaching
    400 	 * each as appropriate.
    401 	 *
    402 	 * Unfortunately, some BIOSes seem to have fatal bugs getting the
    403 	 * dynamic (i.e. currently active) configuration, for instance some
    404 	 * Sony VAIO laptops, including the PCG-Z505HE.  They don't have such a
    405 	 * problem with that static (i.e. next boot time) configuration,
    406 	 * however.  The workaround is to get the static configuration for all
    407 	 * indices, and only get dynamic configuration for devices where the
    408 	 * match is positive.
    409 	 *
    410 	 * This seems to work conveniently as the indices that cause
    411 	 * crashes (and it seems to vary from machine to machine) do not
    412 	 * seem to be for devices that NetBSD's pnpbios supports.
    413 	 */
    414 
    415 	idx = 0;
    416 	for (i = 0; i < num && idx != 0xff; i++) {
    417 		DPRINTF(("%s: getting info for index %d\n",
    418 		    device_xname(sc->sc_dev), idx));
    419 
    420 		dynidx = idx;
    421 
    422 		res = pnpbios_getnode(PNP_CF_DEVCONF_STATIC, &idx, buf, size);
    423 		if (res) {
    424 			aprint_error_dev(sc->sc_dev, "index %d error %d "
    425 			    "getting static configuration\n", idx, res);
    426 			continue;
    427 		}
    428 		dn = (struct pnpdevnode *)buf;
    429 		if (!pnpbios_attachnode(sc, dn->dn_handle, buf, dn->dn_size, 1)) {
    430 			DPRINTF(("%s handle %d: no match from static config\n",
    431 			    device_xname(sc->sc_dev), dn->dn_handle));
    432 			continue;
    433 		}
    434 
    435 		res = pnpbios_getnode(PNP_CF_DEVCONF_DYNAMIC, &dynidx, buf, size);
    436 		if (res) {
    437 			aprint_error_dev(sc->sc_dev, "index %d error %d "
    438 			    "getting dynamic configuration\n", dynidx, res);
    439 			continue;
    440 		}
    441 		dn = (struct pnpdevnode *)buf;
    442 		if (!pnpbios_attachnode(sc, dn->dn_handle, buf, dn->dn_size, 0)) {
    443 			DPRINTF(("%s handle %d: no match from dynamic config\n",
    444 			    device_xname(sc->sc_dev), dn->dn_handle));
    445 			continue;
    446 		}
    447 	}
    448 	if (i != num)
    449 		aprint_error_dev(sc->sc_dev, "got only %d nodes\n", i);
    450 	if (idx != 0xff)
    451 		aprint_error_dev(sc->sc_dev, "last index %d\n", idx);
    452 
    453 	free(buf, M_DEVBUF);
    454 }
    455 
    456 #ifdef PNPBIOSEVENTS
    457 static int
    458 pnpbios_update_dock_status(struct pnpbios_softc *sc)
    459 {
    460 	struct pnpdockinfo di;
    461 	const char *when, *style;
    462 	int res, odocked = sc->sc_docked;
    463 
    464 	res = pnpbios_getdockinfo(&di);
    465 	if (res == PNP_RC_SYSTEM_NOT_DOCKED) {
    466 		sc->sc_docked = 0;
    467 		if (odocked != sc->sc_docked)
    468 			printf("%s: not docked\n", device_xname(sc->sc_dev));
    469 	} else if (res) {
    470 		EDPRINTF(("%s: dockinfo failed 0x%02x\n",
    471 		    device_xname(sc->sc_dev), res));
    472 	} else {
    473 		sc->sc_docked = 1;
    474 		if (odocked != sc->sc_docked) {
    475 			char idstr[8];
    476 			pnpbios_id_to_string(di.di_id, idstr);
    477 			printf("%s: dock id %s", device_xname(sc->sc_dev), idstr);
    478 			if (pnpbiosverbose) {
    479 				if (di.di_serial != -1)
    480 					printf(", serial number %d",
    481 					    di.di_serial);
    482 			}
    483 			switch (di.di_cap & PNP_DI_DOCK_STYLE_MASK) {
    484 			case PNP_DI_DOCK_STYLE_SUPRISE:
    485 				style = "surprise";
    486 				break;
    487 			case PNP_DI_DOCK_STYLE_VCR:
    488 				style = "controlled";
    489 				break;
    490 			default:
    491 				style = "<style unknown>";
    492 				break;
    493 			}
    494 			switch (di.di_cap & PNP_DI_DOCK_WHEN_MASK) {
    495 			case PNP_DI_DOCK_WHEN_NO_POWER:
    496 				when = "cold";
    497 				break;
    498 			case PNP_DI_DOCK_WHEN_SUSPENDED:
    499 				when = "warm";
    500 				break;
    501 			case PNP_DI_DOCK_WHEN_RUNNING:
    502 				when = "hot";
    503 				break;
    504 			case PNP_DI_DOCK_WHEN_RESERVED:
    505 				when = "<reserved>";
    506 				break;
    507 			default:
    508 				when = "<dock type unknown>";
    509 					break;
    510 			}
    511 			printf(", %s %s docking\n", style, when);
    512 		}
    513 	}
    514 
    515 	return (odocked);
    516 }
    517 #endif
    518 
    519 static int
    520 pnpbios_getnumnodes(int *nump, size_t *sizep)
    521 {
    522 	int res;
    523 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    524 
    525 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    526 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    527 	*--help = 2; /* buffer offset for node size */
    528 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    529 	*--help = 0; /* buffer offset for numnodes */
    530 	*--help = PNP_FC_GET_NUM_NODES;
    531 
    532 	res = pnpbioscall(((char *)help) - pnpbios_scratchbuf);
    533 	if (res)
    534 		return (res);
    535 
    536 	*nump = *(short *)(pnpbios_scratchbuf + 0);
    537 	*sizep = *(short *)(pnpbios_scratchbuf + 2);
    538 	return (0);
    539 }
    540 
    541 static int
    542 pnpbios_getnode(int flags, int *idxp, uint8_t *buf, size_t len)
    543 {
    544 	int res;
    545 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    546 
    547 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    548 	*--help = flags;
    549 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    550 	*--help = 2; /* buffer offset for node data */
    551 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    552 	*--help = 0; /* buffer offset for index in/out */
    553 	*--help = PNP_FC_GET_DEVICE_NODE;
    554 
    555 	*(short *)(pnpbios_scratchbuf + 0) = *idxp;
    556 
    557 	res = pnpbioscall(((char *)help) - pnpbios_scratchbuf);
    558 	if (res)
    559 		return (res);
    560 
    561 	*idxp = *(short *)(pnpbios_scratchbuf + 0);
    562 	memcpy(buf, pnpbios_scratchbuf + 2, len);
    563 	return (0);
    564 }
    565 
    566 
    567 #if 0
    568 /* XXX - pnpbios_setnode() is never called. */
    569 
    570 static int
    571 pnpbios_setnode(int flags, int idx, const uint8_t *buf, size_t len)
    572 {
    573 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    574 
    575 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    576 	*--help = flags;
    577 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    578 	*--help = 0; /* buffer offset for node data */
    579 	*--help = idx;
    580 	*--help = PNP_FC_SET_DEVICE_NODE;
    581 
    582 	memcpy(pnpbios_scratchbuf, buf, len);
    583 
    584 	return (pnpbioscall(((void *)help) - pnpbios_scratchbuf));
    585 }
    586 #endif /* 0 */
    587 
    588 #ifdef PNPBIOSEVENTS
    589 static int
    590 pnpbios_getevent(uint16_t *event)
    591 {
    592 	int res;
    593 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    594 
    595 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    596 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    597 	*--help = 0; /* buffer offset for message data */
    598 	*--help = PNP_FC_GET_EVENT;
    599 
    600 	res = pnpbioscall(((void *)help) - pnpbios_scratchbuf);
    601 	*event = pnpbios_scratchbuf[0] + (pnpbios_scratchbuf[1] << 8);
    602 	return (res);
    603 }
    604 
    605 static int
    606 pnpbios_sendmessage(int msg)
    607 {
    608 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    609 
    610 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    611 	*--help = msg;
    612 	*--help = PNP_FC_SEND_MESSAGE;
    613 
    614 	return (pnpbioscall(((void *)help) - pnpbios_scratchbuf));
    615 }
    616 
    617 static int
    618 pnpbios_getdockinfo(struct pnpdockinfo *di)
    619 {
    620 	int res;
    621 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    622 
    623 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    624 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    625 	*--help = 0; /* buffer offset for dock info */
    626 	*--help = PNP_FC_GET_DOCK_INFO;
    627 
    628 	res = pnpbioscall(((void *)help) - pnpbios_scratchbuf);
    629 	memcpy(di, pnpbios_scratchbuf, sizeof(*di));
    630 	return (res);
    631 }
    632 #endif /* PNPBIOSEVENTS */
    633 
    634 #if 0
    635 /* XXX - pnpbios_getapmtable() is not called. */
    636 
    637 /* XXX we don't support more than PNPBIOS_BUFSIZE - (stacklen + 2) */
    638 static int
    639 pnpbios_getapmtable(uint8_t *tab, size_t *len)
    640 {
    641 	short *help = (short *)(pnpbios_scratchbuf + PNPBIOS_BUFSIZE);
    642 	size_t origlen, stacklen;
    643 	int res;
    644 
    645 	*--help = GSEL(GPNPBIOSDATA_SEL, SEL_KPL);
    646 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    647 	*--help = 2; /* buffer offset for table */
    648 	*--help = GSEL(GPNPBIOSSCRATCH_SEL, SEL_KPL);
    649 	*--help = 0; /* buffer offset for length */
    650 	*--help = PNP_FC_GET_APM_TABLE;
    651 
    652 	origlen = *len;
    653 	stacklen = (void *)help - pnpbios_scratchbuf;
    654 	if (origlen > PNPBIOS_BUFSIZE - stacklen - 2)
    655 		origlen = PNPBIOS_BUFSIZE - stacklen - 2;
    656 	*(uint16_t *)(pnpbios_scratchbuf) = origlen;
    657 
    658 	res = pnpbioscall(((void *)help) - pnpbios_scratchbuf);
    659 	*len = *(uint16_t *)pnpbios_scratchbuf;
    660 	if (res)
    661 		return (res);
    662 	if (origlen && *len > origlen) {
    663 		printf("pnpbios: returned apm table exceed requested size\n");
    664 		return (PNP_RC_BUFFER_TOO_SMALL);
    665 	}
    666 	memcpy(tab, pnpbios_scratchbuf + 2, *len);
    667 	return (0);
    668 }
    669 #endif
    670 
    671 static void
    672 pnpbios_id_to_string(uint32_t pnpid, char *s)
    673 {
    674 	uint8_t *id;
    675 
    676 	id = (uint8_t *)&pnpid;
    677 	*s++ = 'A' + (id[0] >> 2) - 1;
    678 	*s++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
    679 	*s++ = 'A' + (id[1] & 0x1f) - 1;
    680 	*s++ = HEXDIGITS[id[2] >> 4];
    681 	*s++ = HEXDIGITS[id[2] & 0x0f];
    682 	*s++ = HEXDIGITS[id[3] >> 4];
    683 	*s++ = HEXDIGITS[id[3] & 0x0f];
    684 	*s = '\0';
    685 }
    686 
    687 static void
    688 pnpbios_printres(struct pnpresources *r)
    689 {
    690 	struct pnp_mem *mem;
    691 	struct pnp_io *io;
    692 	struct pnp_irq *irq;
    693 	struct pnp_dma *dma;
    694 	int p = 0;
    695 
    696 	mem = SIMPLEQ_FIRST(&r->mem);
    697 	if (mem) {
    698 		aprint_normal("mem");
    699 		do {
    700 			aprint_normal(" %x", mem->minbase);
    701 			if (mem->len > 1)
    702 				aprint_normal("-%x",
    703 					      mem->minbase + mem->len - 1);
    704 		} while ((mem = SIMPLEQ_NEXT(mem, next)));
    705 		p++;
    706 	}
    707 	io = SIMPLEQ_FIRST(&r->io);
    708 	if (io) {
    709 		if (p++)
    710 			aprint_normal(", ");
    711 		aprint_normal("io");
    712 		do {
    713 			aprint_normal(" %x", io->minbase);
    714 			if (io->len > 1)
    715 				aprint_normal("-%x",
    716 					      io->minbase + io->len - 1);
    717 		} while ((io = SIMPLEQ_NEXT(io, next)));
    718 	}
    719 	irq = SIMPLEQ_FIRST(&r->irq);
    720 	if (irq) {
    721 		if (p++)
    722 			aprint_normal(", ");
    723 		aprint_normal("irq");
    724 		do {
    725 			aprint_normal(" %d", ffs(irq->mask) - 1);
    726 		} while ((irq = SIMPLEQ_NEXT(irq, next)));
    727 	}
    728 	dma = SIMPLEQ_FIRST(&r->dma);
    729 	if (dma) {
    730 		if (p)
    731 			aprint_normal(", ");
    732 		aprint_normal("DMA");
    733 		do {
    734 			aprint_normal(" %d", ffs(dma->mask) - 1);
    735 		} while ((dma = SIMPLEQ_NEXT(dma, next)));
    736 	}
    737 }
    738 
    739 static int
    740 pnpbios_print(void *aux, const char *pnp)
    741 {
    742 	struct pnpbiosdev_attach_args *aa = aux;
    743 
    744 	if (pnp)
    745 		return (QUIET);
    746 
    747 	aprint_normal(" index %d (%s", aa->idx, aa->primid);
    748 	if (aa->resc->longname)
    749 		aprint_normal(", %s", aa->resc->longname);
    750 	if (aa->idstr != aa->primid)
    751 		aprint_normal(", attached as %s", aa->idstr);
    752 	aprint_normal(")");
    753 
    754 	return (0);
    755 }
    756 
    757 void
    758 pnpbios_print_devres(device_t dev, struct pnpbiosdev_attach_args *aa)
    759 {
    760 
    761 	aprint_normal_dev(dev, "");
    762 	pnpbios_printres(aa->resc);
    763 	aprint_normal("\n");
    764 }
    765 
    766 static int
    767 pnpbios_attachchild(struct pnpbios_softc *sc,
    768 		    struct pnpbiosdev_attach_args *aa, int matchonly)
    769 {
    770 	int locs[PNPBIOSCF_NLOCS];
    771 
    772 	locs[PNPBIOSCF_INDEX] = aa->idx;
    773 
    774 	if (matchonly)
    775 		return (config_search(sc->sc_dev, aa,
    776 				      CFARGS(.submatch = config_stdsubmatch,
    777 					     .locators = locs)) != NULL);
    778 	else
    779 		return (config_found(sc->sc_dev, aa, pnpbios_print,
    780 				     CFARGS(.submatch = config_stdsubmatch,
    781 					    .locators = locs)) != NULL);
    782 }
    783 
    784 static int
    785 pnpbios_attachnode(struct pnpbios_softc *sc, int idx, const uint8_t *buf,
    786     size_t len, int matchonly)
    787 {
    788 	const struct pnpdevnode *dn;
    789 	const uint8_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 		aprint_error("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 		aprint_error("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 		aprint_error("error in compatible ID\n");
    825 		goto dump;
    826 	}
    827 
    828 	if (p != buf + len) {
    829 		aprint_error_dev(sc->sc_dev, "length mismatch in node %d:"
    830 			     " used %d of %d Bytes\n",
    831 		       idx, p - buf, len);
    832 		if (p > buf + len) {
    833 			/* XXX shouldn't happen - pnp_scan should catch it */
    834 			goto dump;
    835 		}
    836 		/* Crappy BIOS: Buffer is not fully used. Be generous. */
    837 	}
    838 
    839 	if (r.nummem + r.numio + r.numirq + r.numdma == 0) {
    840 		if (pnpbiosverbose) {
    841 			aprint_normal("%s", idstr);
    842 			if (r.longname)
    843 				aprint_normal(", %s", r.longname);
    844 			compatid = s.compatids;
    845 			while (compatid) {
    846 				aprint_normal(", %s", compatid->idstr);
    847 				compatid = compatid->next;
    848 			}
    849 			aprint_normal(" at %s index %d disabled\n",
    850 			    device_xname(sc->sc_dev), idx);
    851 		}
    852 		return 0;
    853 	}
    854 
    855 	aa.pbt = 0; /* XXX placeholder */
    856 	aa.idx = idx;
    857 	aa.resc = &r;
    858 	aa.ic = sc->sc_ic;
    859 	aa.primid = idstr;
    860 
    861 	/* first try the specific device ID */
    862 	aa.idstr = idstr;
    863 	if (pnpbios_attachchild(sc, &aa, matchonly))
    864 		return -1;
    865 
    866 	/* if no driver was found, try compatible IDs */
    867 	compatid = s.compatids;
    868 	while (compatid) {
    869 		aa.idstr = compatid->idstr;
    870 		if (pnpbios_attachchild(sc, &aa, matchonly))
    871 			return -1;
    872 		compatid = compatid->next;
    873 	}
    874 
    875 	if (pnpbiosverbose) {
    876 		aprint_normal("%s", idstr);
    877 		if (r.longname)
    878 			aprint_normal(", %s", r.longname);
    879 		compatid = s.compatids;
    880 		while (compatid) {
    881 			aprint_normal(", %s", compatid->idstr);
    882 			compatid = compatid->next;
    883 		}
    884 		aprint_normal(" (");
    885 		pnpbios_printres(&r);
    886 		aprint_normal(") at %s index %d ignored\n",
    887 			      device_xname(sc->sc_dev), idx);
    888 	}
    889 
    890 	return 0;
    891 
    892 	/* XXX should free resource lists */
    893 
    894 dump:
    895 	i = 0;
    896 #ifdef PNPBIOSDEBUG
    897 	/* print some useful info */
    898 	if (len >= sizeof(*dn)) {
    899 		aprint_normal("%s idx %d size %d type 0x%x:0x%x:0x%x attr 0x%x\n",
    900 		    idstr, dn->dn_handle, dn->dn_size, dn->dn_type,
    901 		    dn->dn_subtype, dn->dn_dpi, dn->dn_attr);
    902 		i += sizeof(*dn);
    903 	}
    904 #endif
    905 	for (; i < len; i++)
    906 		aprint_normal(" %02x", buf[i]);
    907 	aprint_normal("\n");
    908 	return 0;
    909 }
    910 
    911 static int
    912 pnp_scan(const uint8_t **bufp, size_t maxlen,
    913     struct pnpresources *r, int in_depends)
    914 {
    915 	const void *start;
    916 	const uint8_t *p;
    917 	struct pnp_mem *mem;
    918 	int tag, type, len;
    919 	char *idstr;
    920 	int i;
    921 
    922 	p = *bufp;
    923 
    924 	memset(r, 0, sizeof(*r));
    925 	SIMPLEQ_INIT(&r->mem);
    926 	SIMPLEQ_INIT(&r->io);
    927 	SIMPLEQ_INIT(&r->irq);
    928 	SIMPLEQ_INIT(&r->dma);
    929 
    930 	for (;;) {
    931 		if (p >= *bufp + maxlen) {
    932 			aprint_normal("pnp_scanresources: end of buffer\n");
    933 			return (-1);
    934 		}
    935 		start = p;
    936 		tag = *p;
    937 		if (tag & ISAPNP_LARGE_TAG) {
    938 			len = *(const uint16_t *)(p + 1);
    939 			p += sizeof(struct pnplargeres) + len;
    940 
    941 			switch (tag) {
    942 			case ISAPNP_TAG_MEM_RANGE_DESC: {
    943 				const struct pnpmem16rangeres *res = start;
    944 				if (len != sizeof(*res) - 3) {
    945 					aprint_normal("pnp_scan: bad mem desc\n");
    946 					return (-1);
    947 				}
    948 
    949 				mem = malloc(sizeof(struct pnp_mem),
    950 					     M_DEVBUF, M_WAITOK);
    951 				mem->flags = res->r_flags;
    952 				mem->minbase = res->r_minbase << 8;
    953 				mem->maxbase = res->r_maxbase << 8;
    954 				mem->align = res->r_align;
    955 				if (mem->align == 0)
    956 					mem->align = 0x10000;
    957 				mem->len = res->r_len << 8;
    958 				DPRINTF(("\ttag memrange "));
    959 				goto gotmem;
    960 			}
    961 			case ISAPNP_TAG_ANSI_IDENT_STRING: {
    962 				const struct pnpansiidentres *res = start;
    963 				if (in_depends)
    964 					aprint_normal("ID in dep?\n");
    965 				idstr = malloc(len + 1, M_DEVBUF, M_WAITOK);
    966 				for (i = 0; i < len; i++)
    967 					idstr[i] = res->r_id[i];
    968 				idstr[len] = '\0';
    969 
    970 				DPRINTF(("\ttag ansiident %s\n", idstr));
    971 
    972 				if (idstr[0] == '\0') {
    973 					/* disabled device */
    974 					free(idstr, M_DEVBUF);
    975 					break;
    976 				}
    977 				r->longname = idstr;
    978 				break;
    979 			}
    980 			case ISAPNP_TAG_MEM32_RANGE_DESC: {
    981 				const struct pnpmem32rangeres *res = start;
    982 				if (len != sizeof(*res) - 3) {
    983 					aprint_normal("pnp_scan: bad mem32 desc\n");
    984 					return (-1);
    985 				}
    986 
    987 				mem = malloc(sizeof(struct pnp_mem),
    988 					     M_DEVBUF, M_WAITOK);
    989 				mem->flags = res->r_flags;
    990 				mem->minbase = res->r_minbase;
    991 				mem->maxbase = res->r_maxbase;
    992 				mem->align = res->r_align;
    993 				mem->len = res->r_len;
    994 				DPRINTF(("\ttag mem32range "));
    995 				goto gotmem;
    996 			}
    997 			case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC: {
    998 				const struct pnpfixedmem32rangeres *res = start;
    999 				if (len != sizeof(*res) - 3) {
   1000 					aprint_normal("pnp_scan: bad mem32 desc\n");
   1001 					return (-1);
   1002 				}
   1003 
   1004 				mem = malloc(sizeof(struct pnp_mem),
   1005 					     M_DEVBUF, M_WAITOK);
   1006 				mem->flags = res->r_flags;
   1007 				mem->minbase = res->r_base;
   1008 				mem->maxbase = mem->minbase;
   1009 				mem->align = 0;
   1010 				mem->len = res->r_len;
   1011 				DPRINTF(("\ttag fixedmem32range "));
   1012 			gotmem:
   1013 				if (mem->len == 0) { /* disabled */
   1014 					DPRINTF(("zeroed\n"));
   1015 					free(mem, M_DEVBUF);
   1016 					break;
   1017 				}
   1018 				SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
   1019 				r->nummem++;
   1020 
   1021 				DPRINTF(("flags %02x min %08x max %08x "
   1022 				    "align %08x len %08x\n", mem->flags,
   1023 				    mem->minbase, mem->maxbase, mem->align,
   1024 				    mem->len));
   1025 
   1026 				break;
   1027 			}
   1028 			case ISAPNP_TAG_UNICODE_IDENT_STRING:
   1029 			case ISAPNP_TAG_VENDOR_DEFINED:
   1030 			default:
   1031 #ifdef PNPBIOSDEBUG
   1032 				pnp_debugdump(r, start, len);
   1033 #endif
   1034 				break;
   1035 			}
   1036 		} else {
   1037 			type = (tag >> 3) & 0x0f;
   1038 			len = tag & 0x07;
   1039 			p += 1 + len;
   1040 
   1041 			if (type == 0 ||
   1042 			    len < smallrescs[type - 1].minlen ||
   1043 			    len > smallrescs[type - 1].maxlen) {
   1044 				aprint_normal("pnp_scan: bad small resource\n");
   1045 				return (-1);
   1046 			}
   1047 			if (type == ISAPNP_TAG_END) {
   1048 #ifdef PNPBIOSDEBUG
   1049 				const struct pnpendres *res = start;
   1050 #endif
   1051 				if (in_depends) {
   1052 					/*
   1053 					 * this seems to occur and is
   1054 					 * an optimization to not require
   1055 					 * the end dep in a depend
   1056 					 * that ends the section
   1057 					 */
   1058 					p -= 1 + len;
   1059 				}
   1060 				DPRINTF(("\ttag end cksum %02x\n",
   1061 				    res->r_cksum));
   1062 				break;
   1063 			}
   1064 			if (type == ISAPNP_TAG_DEP_START) {
   1065 #ifdef PNPBIOSDEBUG
   1066 				const struct pnpdepstartres *res = start;
   1067 #endif
   1068 				struct pnpresources *new, *last;
   1069 				int rv;
   1070 
   1071 				DPRINTF(("\ttag startdep flags %02x\n",
   1072 				    len ? res->r_pri : ISAPNP_DEP_ACCEPTABLE));
   1073 
   1074 				if (r->dependent_link) {
   1075 					aprint_normal("second dep?\n");
   1076 					return (-1);
   1077 				}
   1078 				/* XXX not sure about this */
   1079 				if (in_depends) {
   1080 					*bufp = p;
   1081 					return (1);
   1082 				}
   1083 				last = r;
   1084 				do {
   1085 					new = malloc(sizeof(*new),
   1086 						     M_DEVBUF, M_WAITOK);
   1087 
   1088 					rv = pnp_scan(&p, maxlen - (p - *bufp),
   1089 						       new, 1);
   1090 					if (rv < 0) {
   1091 						aprint_normal("error in"
   1092 						    " dependent function\n");
   1093 						free(new, M_DEVBUF);
   1094 						return (-1);
   1095 					}
   1096 					last->dependent_link = new;
   1097 					last = new;
   1098 				} while (rv > 0);
   1099 				continue;
   1100 			}
   1101 			if (type == ISAPNP_TAG_DEP_END) {
   1102 				DPRINTF(("\ttag enddep\n"));
   1103 				if (!in_depends) {
   1104 					aprint_normal("tag %d end dep?\n", tag);
   1105 					return (-1);
   1106 				}
   1107 				break;
   1108 			}
   1109 			if (!smallrescs[type - 1].handler) {
   1110 #ifdef PNPBIOSDEBUG
   1111 				pnp_debugdump(r, start, len);
   1112 #endif
   1113 			} else if (
   1114 			    (*smallrescs[type - 1].handler)(r, start, len))
   1115 				return (-1);
   1116 		}
   1117 	}
   1118 	*bufp = p;
   1119 	return (0);
   1120 }
   1121 
   1122 static int
   1123 pnp_newirq(struct pnpresources *r, const void *vres, size_t len)
   1124 {
   1125 	const struct pnpirqres *res;
   1126 	struct pnp_irq *irq;
   1127 
   1128 	res = vres;
   1129 	if (res->r_mask == 0) { /* disabled */
   1130 		DPRINTF(("\ttag irq zeroed\n"));
   1131 		return (0);
   1132 	}
   1133 	irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_WAITOK);
   1134 	irq->mask = res->r_mask;
   1135 	if (len > 2)
   1136 		irq->flags = res->r_info;
   1137 	else
   1138 		irq->flags = 0x01;
   1139 	SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
   1140 	r->numirq++;
   1141 
   1142 	DPRINTF(("\ttag irq flags %02x mask %04x\n", irq->flags,irq->mask));
   1143 
   1144 	return (0);
   1145 }
   1146 
   1147 static int
   1148 pnp_newdma(struct pnpresources *r, const void *vres, size_t len)
   1149 {
   1150 	const struct pnpdmares *res;
   1151 	struct pnp_dma *dma;
   1152 
   1153 	res = vres;
   1154 	if (res->r_mask == 0) { /* disabled */
   1155 		DPRINTF(("\ttag DMA zeroed\n"));
   1156 		return (0);
   1157 	}
   1158 	dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_WAITOK);
   1159 	dma->mask = res->r_mask;
   1160 	dma->flags = res->r_flags;
   1161 	SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
   1162 	r->numdma++;
   1163 
   1164 	DPRINTF(("\ttag DMA flags %02x mask %02x\n", dma->flags,dma->mask));
   1165 
   1166 	return (0);
   1167 }
   1168 
   1169 static int
   1170 pnp_newioport(struct pnpresources *r, const void *vres, size_t len)
   1171 {
   1172 	const struct pnpportres *res;
   1173 	struct pnp_io *io;
   1174 
   1175 	res = vres;
   1176 	if (res->r_len == 0) { /* disabled */
   1177 		DPRINTF(("\ttag io zeroed\n"));
   1178 		return (0);
   1179 	}
   1180 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_WAITOK);
   1181 	io->flags = res->r_flags;
   1182 	io->minbase = res->r_minbase;
   1183 	io->maxbase = res->r_maxbase;
   1184 	io->align = res->r_align;
   1185 	io->len = res->r_len;
   1186 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
   1187 	r->numio++;
   1188 
   1189 	DPRINTF(("\ttag io flags %02x min %04x max %04x align "
   1190 	    "0x%02x len 0x%02x\n", io->flags, io->minbase, io->maxbase,
   1191 	    io->align, io->len));
   1192 
   1193 	return (0);
   1194 }
   1195 
   1196 static int
   1197 pnp_newfixedioport(struct pnpresources *r, const void *vres,
   1198     size_t len)
   1199 {
   1200 	const struct pnpfixedportres *res;
   1201 	struct pnp_io *io;
   1202 
   1203 	res = vres;
   1204 	if (res->r_len == 0) { /* disabled */
   1205 		DPRINTF(("\ttag fixedio zeroed\n"));
   1206 		return (0);
   1207 	}
   1208 	io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_WAITOK);
   1209 	io->flags = 1; /* 10 bit decoding */
   1210 	io->minbase = io->maxbase = res->r_base;
   1211 	io->align = 1;
   1212 	io->len = res->r_len;
   1213 	SIMPLEQ_INSERT_TAIL(&r->io, io, next);
   1214 	r->numio++;
   1215 
   1216 	DPRINTF(("\ttag fixedio flags %02x base %04x align %02x len %02x\n",
   1217 	    io->flags, io->minbase, io->align, io->len));
   1218 
   1219 	return (0);
   1220 }
   1221 
   1222 static int
   1223 pnp_compatid(struct pnpresources *r, const void *vres, size_t len)
   1224 {
   1225 	const struct pnpcompatres *res;
   1226 	struct pnp_compatid *id;
   1227 
   1228 	res = vres;
   1229 	id = malloc(sizeof(*id), M_DEVBUF, M_WAITOK);
   1230 	pnpbios_id_to_string(res->r_id, id->idstr);
   1231 	id->next = r->compatids;
   1232 	r->compatids = id;
   1233 
   1234 	DPRINTF(("\ttag compatid %s\n", id->idstr));
   1235 
   1236 	return (0);
   1237 }
   1238 
   1239 #ifdef PNPBIOSDEBUG
   1240 static int
   1241 pnp_debugdump(struct pnpresources *r, const void *vres, size_t len)
   1242 {
   1243 	const uint8_t *res = vres;
   1244 	int type, i;
   1245 
   1246 	if (res[0] & ISAPNP_LARGE_TAG) {
   1247 		type = res[0] & 0x7f;
   1248 		aprint_normal("\tTAG %02x len %04x %s",
   1249 			      type, len, len ? "data" : "");
   1250 		i = 3;
   1251 	} else {
   1252 		type = (res[0] >> 3) & 0x0f;
   1253 		aprint_normal("\tTAG %02x len %02x %s",
   1254 			      type, len, len ? "data" : "");
   1255 		i = 1;
   1256 	}
   1257 	for (; i < len; i++)
   1258 		aprint_normal(" %02x", res[i]);
   1259 	aprint_normal("\n");
   1260 
   1261 	return (0);
   1262 }
   1263 #endif
   1264 
   1265 int
   1266 pnpbios_io_map(pnpbios_tag_t pbt, struct pnpresources *resc,
   1267     int idx, bus_space_tag_t *tagp, bus_space_handle_t *hdlp)
   1268 {
   1269 	struct pnp_io *io;
   1270 
   1271 	if (idx >= resc->numio)
   1272 		return (EINVAL);
   1273 
   1274 	io = SIMPLEQ_FIRST(&resc->io);
   1275 	while (idx--)
   1276 		io = SIMPLEQ_NEXT(io, next);
   1277 
   1278 	*tagp = x86_bus_space_io;
   1279 	return (bus_space_map(x86_bus_space_io, io->minbase, io->len,
   1280 			       0, hdlp));
   1281 }
   1282 
   1283 void
   1284 pnpbios_io_unmap(pnpbios_tag_t pbt, struct pnpresources *resc,
   1285     int idx, bus_space_tag_t tag, bus_space_handle_t hdl)
   1286 {
   1287 	struct pnp_io *io;
   1288 
   1289 	if (idx >= resc->numio)
   1290 		return;
   1291 
   1292 	io = SIMPLEQ_FIRST(&resc->io);
   1293 	while (idx--)
   1294 		io = SIMPLEQ_NEXT(io, next);
   1295 
   1296 	bus_space_unmap(tag, hdl, io->len);
   1297 }
   1298 
   1299 int
   1300 pnpbios_getiobase(pnpbios_tag_t pbt, struct pnpresources *resc,
   1301     int idx, bus_space_tag_t *tagp, int *basep)
   1302 {
   1303 	struct pnp_io *io;
   1304 
   1305 	if (idx >= resc->numio)
   1306 		return (EINVAL);
   1307 
   1308 	io = SIMPLEQ_FIRST(&resc->io);
   1309 	while (idx--)
   1310 		io = SIMPLEQ_NEXT(io, next);
   1311 
   1312 	if (tagp)
   1313 		*tagp = x86_bus_space_io;
   1314 	if (basep)
   1315 		*basep = io->minbase;
   1316 	return (0);
   1317 }
   1318 
   1319 int
   1320 pnpbios_getiosize(pnpbios_tag_t pbt, struct pnpresources *resc,
   1321     int idx, int *sizep)
   1322 {
   1323         struct pnp_io *io;
   1324 
   1325         if (idx >= resc->numio)
   1326             return (EINVAL);
   1327 
   1328         io = SIMPLEQ_FIRST(&resc->io);
   1329         while (idx--)
   1330                 io = SIMPLEQ_NEXT(io, next);
   1331         if (sizep)
   1332                 *sizep = io->len;
   1333         return (0);
   1334 }
   1335 
   1336 void *
   1337 pnpbios_intr_establish(pnpbios_tag_t pbt, struct pnpresources *resc,
   1338     int idx, int level, int (*fcn)(void *), void *arg)
   1339 {
   1340 	struct pnp_irq *irq;
   1341 	int irqnum, type;
   1342 
   1343 	if (idx >= resc->numirq)
   1344 		return (0);
   1345 
   1346 	irq = SIMPLEQ_FIRST(&resc->irq);
   1347 	while (idx--)
   1348 		irq = SIMPLEQ_NEXT(irq, next);
   1349 
   1350 	irqnum = ffs(irq->mask) - 1;
   1351 	type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
   1352 
   1353 	return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
   1354 }
   1355 
   1356 int
   1357 pnpbios_getirqnum(pnpbios_tag_t pbt, struct pnpresources *resc,
   1358     int idx, int *irqp, int *istp)
   1359 {
   1360 	struct pnp_irq *irq;
   1361 
   1362 	if (idx >= resc->numirq)
   1363 		return (EINVAL);
   1364 
   1365 	irq = SIMPLEQ_FIRST(&resc->irq);
   1366 	while (idx--)
   1367 		irq = SIMPLEQ_NEXT(irq, next);
   1368 
   1369 	if (irqp != NULL)
   1370 		*irqp = ffs(irq->mask) - 1;
   1371 	if (istp != NULL)
   1372 		*istp = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
   1373 	return (0);
   1374 }
   1375 
   1376 int
   1377 pnpbios_getdmachan(pnpbios_tag_t pbt, struct pnpresources *resc,
   1378     int idx, int *chanp)
   1379 {
   1380 	struct pnp_dma *dma;
   1381 
   1382 	if (idx >= resc->numdma)
   1383 		return (EINVAL);
   1384 
   1385 	dma = SIMPLEQ_FIRST(&resc->dma);
   1386 	while (idx--)
   1387 		dma = SIMPLEQ_NEXT(dma, next);
   1388 
   1389 	*chanp = ffs(dma->mask) - 1;
   1390 	return (0);
   1391 }
   1392 
   1393 #ifdef PNPBIOSEVENTS
   1394 static void
   1395 pnpbios_event_thread(void *arg)
   1396 {
   1397 	struct pnpbios_softc *sc;
   1398 	uint16_t event;
   1399 	u_int evflag;
   1400 	int rv, poll;
   1401 
   1402 	sc = arg;
   1403 	if ((sc->sc_control & PNP_IC_CONTORL_EVENT_MASK)
   1404 	    != PNP_IC_CONTROL_EVENT_POLL)
   1405 		poll = 0;
   1406 	else {
   1407 		poll = hz;
   1408 		rv = pnpbios_sendmessage(PNP_CM_PNP_OS_ACTIVE);
   1409 		EDPRINTF(("pnpbios: os active returns 0x%02x\n", rv));
   1410 	}
   1411 
   1412 	config_pending_decr(sc->sc_dev);
   1413 
   1414 	goto start;
   1415 	while (sc->sc_threadrun) {
   1416 		/* maybe we have an event */
   1417 		if (!poll)
   1418 			(void)tsleep(pnpbios_event_thread, PWAIT,
   1419 			    "pnpbiosevent", 0);
   1420 		else if (((evflag = *sc->sc_evaddr) & 0x01) == 0) {
   1421 			if (evflag)
   1422 				EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
   1423 			(void)tsleep(pnpbios_event_thread, PWAIT,
   1424 			    "pnpbiosevent", poll);
   1425 			continue;
   1426 		} else {
   1427 			EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
   1428 		}
   1429 start:
   1430 		if ((rv = pnpbios_getevent(&event))) {
   1431 			EDPRINTF(("pnpbios: getevent rc: 0x%02x\n", rv));
   1432 #ifdef DIAGNOSTIC
   1433 			if (rv != PNP_RC_EVENTS_NOT_PENDING)
   1434 				printf("%s: getevent failed: %d\n",
   1435 				    device_xname(sc->sc_dev), rv);
   1436 #endif
   1437 			continue;
   1438 		}
   1439 		switch (event) {
   1440 		case PNP_EID_ABOUT_TO_CHANGE_CONFIG:
   1441 			EDPRINTF(("pnpbios: about to change event\n"));
   1442 			/*
   1443 			 * The system is about to be docked or undocked.
   1444 			 * Acknowledge the event, so that the procedure
   1445 			 * can continue.
   1446 			 * XXX When should we ever send an ABORT?
   1447 			 */
   1448 			pnpbios_sendmessage(PNP_RM_OK);
   1449 			break;
   1450 		case PNP_EID_DOCK_CHANGED:
   1451 		    {
   1452 			int odocked;
   1453 
   1454 			EDPRINTF(("pnpbios: dock changed event\n"));
   1455 
   1456 			odocked = pnpbios_update_dock_status(sc);
   1457 			if (odocked == sc->sc_docked)
   1458 				break;
   1459 			switch (sc->sc_docked) {
   1460 			case 0:
   1461 				/* We have been undocked. */
   1462 				/* XXX detach devices XXX */
   1463 				break;
   1464 
   1465 			case 1:
   1466 				/* We have been docked. */
   1467 				/* XXX attach devices XXX */
   1468 				break;
   1469 
   1470 			default:
   1471 				/* getdockinfo failed! */
   1472 				printf("%s: dock changed event, but unable "
   1473 				    "to get dock info; event ignored\n",
   1474 				    device_xname(sc->sc_dev));
   1475 			}
   1476 			break;
   1477 		    }
   1478 		case PNP_EID_SYSTEM_DEVICE_CHANGED:
   1479 			EDPRINTF(("pnpbios: system device changed event\n"));
   1480 			break;
   1481 		case PNP_EID_CONFIG_CHANGE_FAILED:
   1482 			EDPRINTF(("pnpbios: config changed event\n"));
   1483 			break;
   1484 		case PNP_EID_UNKNOWN_SYSTEM_EVENT:
   1485 #ifdef DIAGNOSTIC
   1486 			printf("%s: \"unknown system event\"\n",
   1487 			    device_xname(sc->sc_dev));
   1488 #endif
   1489 			break;
   1490 		default:
   1491 #ifdef DIAGNOSTIC
   1492 			if (event & PNP_EID_OEM_DEFINED_BIT)
   1493 				printf("%s: vendor defined event 0x%04x\n",
   1494 				    device_xname(sc->sc_dev), event);
   1495 			else
   1496 				printf("%s: unknown event 0x%04x\n",
   1497 				    device_xname(sc->sc_dev), event);
   1498 #endif
   1499 			break;
   1500 		}
   1501 	}
   1502 
   1503 	pnpbios_sendmessage(PNP_CM_PNP_OS_INACTIVE);
   1504 	kthread_exit(0);
   1505 }
   1506 #endif	/* PNPBIOSEVENTS */
   1507