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