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