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