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