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