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