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