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