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