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