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