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