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