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