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