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