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