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