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