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