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