pnpbios.c revision 1.46 1 /* $NetBSD: pnpbios.c,v 1.46 2005/04/01 11:59:31 yamt 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.46 2005/04/01 11:59:31 yamt 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_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 static char hex[] = "0123456789ABCDEF";
680 u_int8_t *id;
681
682 id = (u_int8_t *)&pnpid;
683 *s++ = 'A' + (id[0] >> 2) - 1;
684 *s++ = 'A' + ((id[0] & 3) << 3) + (id[1] >> 5) - 1;
685 *s++ = 'A' + (id[1] & 0x1f) - 1;
686 *s++ = hex[id[2] >> 4];
687 *s++ = hex[id[2] & 0x0f];
688 *s++ = hex[id[3] >> 4];
689 *s++ = hex[id[3] & 0x0f];
690 *s = '\0';
691 }
692
693 static void
694 pnpbios_printres(struct pnpresources *r)
695 {
696 struct pnp_mem *mem;
697 struct pnp_io *io;
698 struct pnp_irq *irq;
699 struct pnp_dma *dma;
700 int p = 0;
701
702 mem = SIMPLEQ_FIRST(&r->mem);
703 if (mem) {
704 printf("mem");
705 do {
706 printf(" %x", mem->minbase);
707 if (mem->len > 1)
708 printf("-%x", mem->minbase + mem->len - 1);
709 } while ((mem = SIMPLEQ_NEXT(mem, next)));
710 p++;
711 }
712 io = SIMPLEQ_FIRST(&r->io);
713 if (io) {
714 if (p++)
715 printf(", ");
716 printf("io");
717 do {
718 printf(" %x", io->minbase);
719 if (io->len > 1)
720 printf("-%x", io->minbase + io->len - 1);
721 } while ((io = SIMPLEQ_NEXT(io, next)));
722 }
723 irq = SIMPLEQ_FIRST(&r->irq);
724 if (irq) {
725 if (p++)
726 printf(", ");
727 printf("irq");
728 do {
729 printf(" %d", ffs(irq->mask) - 1);
730 } while ((irq = SIMPLEQ_NEXT(irq, next)));
731 }
732 dma = SIMPLEQ_FIRST(&r->dma);
733 if (dma) {
734 if (p)
735 printf(", ");
736 printf("DMA");
737 do {
738 printf(" %d", ffs(dma->mask) - 1);
739 } while ((dma = SIMPLEQ_NEXT(dma, next)));
740 }
741 }
742
743 static int
744 pnpbios_print(void *aux, const char *pnp)
745 {
746 struct pnpbiosdev_attach_args *aa = aux;
747
748 if (pnp)
749 return (QUIET);
750
751 aprint_normal(" index %d (%s", aa->idx, aa->primid);
752 if (aa->resc->longname)
753 aprint_normal(", %s", aa->resc->longname);
754 if (aa->idstr != aa->primid)
755 aprint_normal(", attached as %s", aa->idstr);
756 aprint_normal(")");
757
758 return (0);
759 }
760
761 void
762 pnpbios_print_devres(struct device *dev, struct pnpbiosdev_attach_args *aa)
763 {
764
765 printf("%s: ", dev->dv_xname);
766 pnpbios_printres(aa->resc);
767 printf("\n");
768 }
769
770 static int
771 pnpbios_submatch(struct device *parent, struct cfdata *match,
772 const locdesc_t *ldesc, void *aux)
773 {
774
775 if (match->cf_loc[PNPBIOSCF_INDEX] != PNPBIOSCF_INDEX_DEFAULT &&
776 match->cf_loc[PNPBIOSCF_INDEX] != ldesc->locs[PNPBIOSCF_INDEX])
777 return (0);
778
779 return (config_match(parent, match, aux));
780 }
781
782 static int
783 pnpbios_attachchild(struct pnpbios_softc *sc,
784 struct pnpbiosdev_attach_args *aa, int matchonly)
785 {
786 int help[2];
787 locdesc_t *ldesc = (void *)help; /* XXX */
788
789 ldesc->len = 1;
790 ldesc->locs[PNPBIOSCF_INDEX] = aa->idx;
791
792 if (matchonly)
793 return (config_search_loc(pnpbios_submatch, (struct device *)sc,
794 "pnpbios", ldesc, aa) != NULL);
795 else
796 return (config_found_sm_loc((struct device *)sc, "pnpbios",
797 ldesc, aa, pnpbios_print, pnpbios_submatch)
798 != NULL);
799 }
800
801 static int
802 pnpbios_attachnode(struct pnpbios_softc *sc, int idx, const u_int8_t *buf,
803 size_t len, int matchonly)
804 {
805 struct pnpdevnode *dn;
806 const u_int8_t *p;
807 char idstr[8];
808 struct pnpresources r, s;
809 struct pnpbiosdev_attach_args aa;
810 struct pnp_compatid *compatid;
811 int res, i;
812
813 dn = (struct pnpdevnode *)buf;
814 pnpbios_id_to_string(dn->dn_product, idstr);
815 p = (u_char *)(dn + 1);
816
817 DPRINTF(("%s (%s): type 0x%02x subtype "
818 "0x%02x dpi 0x%02x attr 0x%04x:\n",
819 idstr, matchonly ? "static" : "dynamic", dn->dn_type,
820 dn->dn_subtype, dn->dn_dpi, dn->dn_attr));
821 DPRINTF(("%s: allocated config scan:\n", idstr));
822 res = pnp_scan(&p, len - 12, &r, 0);
823 if (res < 0) {
824 printf("error in config data\n");
825 goto dump;
826 }
827
828 /*
829 * the following is consistency check only for now
830 */
831 DPRINTF(("\tpossible config scan:\n"));
832 res = pnp_scan(&p, len - (p - buf), &s, 0);
833 if (res < 0) {
834 printf("error in possible configuration\n");
835 goto dump;
836 }
837
838 DPRINTF(("\tcompat id scan:\n"));
839 res = pnp_scan(&p, len - (p - buf), &s, 0);
840 if (res < 0) {
841 printf("error in compatible ID\n");
842 goto dump;
843 }
844
845 if (p != buf + len) {
846 printf("%s: length mismatch in node %d: used %d of %d Bytes\n",
847 sc->sc_dev.dv_xname, idx, p - buf, len);
848 if (p > buf + len) {
849 /* XXX shouldn't happen - pnp_scan should catch it */
850 goto dump;
851 }
852 /* Crappy BIOS: Buffer is not fully used. Be generous. */
853 }
854
855 if (r.nummem + r.numio + r.numirq + r.numdma == 0) {
856 if (pnpbiosverbose) {
857 printf("%s", idstr);
858 if (r.longname)
859 printf(", %s", r.longname);
860 compatid = s.compatids;
861 while (compatid) {
862 printf(", %s", compatid->idstr);
863 compatid = compatid->next;
864 }
865 printf(" at %s index %d disabled\n",
866 sc->sc_dev.dv_xname, idx);
867 }
868 return 0;
869 }
870
871 aa.pbt = 0; /* XXX placeholder */
872 aa.idx = idx;
873 aa.resc = &r;
874 aa.ic = sc->sc_ic;
875 aa.primid = idstr;
876
877 /* first try the specific device ID */
878 aa.idstr = idstr;
879 if (pnpbios_attachchild(sc, &aa, matchonly))
880 return -1;
881
882 /* if no driver was found, try compatible IDs */
883 compatid = s.compatids;
884 while (compatid) {
885 aa.idstr = compatid->idstr;
886 if (pnpbios_attachchild(sc, &aa, matchonly))
887 return -1;
888 compatid = compatid->next;
889 }
890
891 if (pnpbiosverbose) {
892 printf("%s", idstr);
893 if (r.longname)
894 printf(", %s", r.longname);
895 compatid = s.compatids;
896 while (compatid) {
897 printf(", %s", compatid->idstr);
898 compatid = compatid->next;
899 }
900 printf(" (");
901 pnpbios_printres(&r);
902 printf(") at %s index %d ignored\n", sc->sc_dev.dv_xname, idx);
903 }
904
905 return 0;
906
907 /* XXX should free resource lists */
908
909 dump:
910 i = 0;
911 #ifdef PNPBIOSDEBUG
912 /* print some useful info */
913 if (len >= sizeof(*dn)) {
914 printf("%s idx %d size %d type 0x%x:0x%x:0x%x attr 0x%x\n",
915 idstr, dn->dn_handle, dn->dn_size, dn->dn_type,
916 dn->dn_subtype, dn->dn_dpi, dn->dn_attr);
917 i += sizeof(*dn);
918 }
919 #endif
920 for (; i < len; i++)
921 printf(" %02x", buf[i]);
922 printf("\n");
923 return 0;
924 }
925
926 static int
927 pnp_scan(const u_int8_t **bufp, size_t maxlen,
928 struct pnpresources *r, int in_depends)
929 {
930 const void *start;
931 const u_int8_t *p;
932 struct pnp_mem *mem;
933 int tag, type, len;
934 char *idstr;
935 int i;
936
937 p = *bufp;
938
939 memset(r, 0, sizeof(*r));
940 SIMPLEQ_INIT(&r->mem);
941 SIMPLEQ_INIT(&r->io);
942 SIMPLEQ_INIT(&r->irq);
943 SIMPLEQ_INIT(&r->dma);
944
945 for (;;) {
946 if (p >= *bufp + maxlen) {
947 printf("pnp_scanresources: end of buffer\n");
948 return (-1);
949 }
950 start = p;
951 tag = *p;
952 if (tag & ISAPNP_LARGE_TAG) {
953 len = *(u_int16_t *)(p + 1);
954 p += sizeof(struct pnplargeres) + len;
955
956 switch (tag) {
957 case ISAPNP_TAG_MEM_RANGE_DESC: {
958 const struct pnpmem16rangeres *res = start;
959 if (len != sizeof(*res) - 3) {
960 printf("pnp_scan: bad mem desc\n");
961 return (-1);
962 }
963
964 mem = malloc(sizeof(struct pnp_mem),
965 M_DEVBUF, M_WAITOK);
966 mem->flags = res->r_flags;
967 mem->minbase = res->r_minbase << 8;
968 mem->maxbase = res->r_maxbase << 8;
969 mem->align = res->r_align;
970 if (mem->align == 0)
971 mem->align = 0x10000;
972 mem->len = res->r_len << 8;
973 DPRINTF(("\ttag memrange "));
974 goto gotmem;
975 }
976 case ISAPNP_TAG_ANSI_IDENT_STRING: {
977 const struct pnpansiidentres *res = start;
978 if (in_depends)
979 printf("ID in dep?\n");
980 idstr = malloc(len + 1, M_DEVBUF, M_NOWAIT);
981 for (i = 0; i < len; i++)
982 idstr[i] = res->r_id[i];
983 idstr[len] = '\0';
984
985 DPRINTF(("\ttag ansiident %s\n", idstr));
986
987 if (idstr[0] == '\0') {
988 /* disabled device */
989 free(idstr, M_DEVBUF);
990 break;
991 }
992 r->longname = idstr;
993 break;
994 }
995 case ISAPNP_TAG_MEM32_RANGE_DESC: {
996 const struct pnpmem32rangeres *res = start;
997 if (len != sizeof(*res) - 3) {
998 printf("pnp_scan: bad mem32 desc\n");
999 return (-1);
1000 }
1001
1002 mem = malloc(sizeof(struct pnp_mem),
1003 M_DEVBUF, M_WAITOK);
1004 mem->flags = res->r_flags;
1005 mem->minbase = res->r_minbase;
1006 mem->maxbase = res->r_maxbase;
1007 mem->align = res->r_align;
1008 mem->len = res->r_len;
1009 DPRINTF(("\ttag mem32range "));
1010 goto gotmem;
1011 }
1012 case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC: {
1013 const struct pnpfixedmem32rangeres *res = start;
1014 if (len != sizeof(*res) - 3) {
1015 printf("pnp_scan: bad mem32 desc\n");
1016 return (-1);
1017 }
1018
1019 mem = malloc(sizeof(struct pnp_mem),
1020 M_DEVBUF, M_WAITOK);
1021 mem->flags = res->r_flags;
1022 mem->minbase = res->r_base;
1023 mem->maxbase = mem->minbase;
1024 mem->align = 0;
1025 mem->len = res->r_len;
1026 DPRINTF(("\ttag fixedmem32range "));
1027 gotmem:
1028 if (mem->len == 0) { /* disabled */
1029 DPRINTF(("zeroed\n"));
1030 free(mem, M_DEVBUF);
1031 break;
1032 }
1033 SIMPLEQ_INSERT_TAIL(&r->mem, mem, next);
1034 r->nummem++;
1035
1036 DPRINTF(("flags %02x min %08x max %08x "
1037 "align %08x len %08x\n", mem->flags,
1038 mem->minbase, mem->maxbase, mem->align,
1039 mem->len));
1040
1041 break;
1042 }
1043 case ISAPNP_TAG_UNICODE_IDENT_STRING:
1044 case ISAPNP_TAG_VENDOR_DEFINED:
1045 default:
1046 #ifdef PNPBIOSDEBUG
1047 pnp_debugdump(r, start, len);
1048 #endif
1049 break;
1050 }
1051 } else {
1052 type = (tag >> 3) & 0x0f;
1053 len = tag & 0x07;
1054 p += 1 + len;
1055
1056 if (type == 0 ||
1057 len < smallrescs[type - 1].minlen ||
1058 len > smallrescs[type - 1].maxlen) {
1059 printf("pnp_scan: bad small resource\n");
1060 return (-1);
1061 }
1062 if (type == ISAPNP_TAG_END) {
1063 #ifdef PNPBIOSDEBUG
1064 const struct pnpendres *res = start;
1065 #endif
1066 if (in_depends) {
1067 /*
1068 * this seems to occur and is
1069 * an optimization to not require
1070 * the end dep in a depend
1071 * that ends the section
1072 */
1073 p -= 1 + len;
1074 }
1075 DPRINTF(("\ttag end cksum %02x\n",
1076 res->r_cksum));
1077 break;
1078 }
1079 if (type == ISAPNP_TAG_DEP_START) {
1080 #ifdef PNPBIOSDEBUG
1081 const struct pnpdepstartres *res = start;
1082 #endif
1083 struct pnpresources *new, *last;
1084 int rv;
1085
1086 DPRINTF(("\ttag startdep flags %02x\n",
1087 len ? res->r_pri : ISAPNP_DEP_ACCEPTABLE));
1088
1089 if (r->dependant_link) {
1090 printf("second dep?\n");
1091 return (-1);
1092 }
1093 /* XXX not sure about this */
1094 if (in_depends) {
1095 *bufp = p;
1096 return (1);
1097 }
1098 last = r;
1099 do {
1100 new = malloc(sizeof(*new),
1101 M_DEVBUF, M_NOWAIT);
1102
1103 rv = pnp_scan(&p, maxlen - (p - *bufp),
1104 new, 1);
1105 if (rv < 0) {
1106 printf("error in dependant "
1107 "function\n");
1108 free(new, M_DEVBUF);
1109 return (-1);
1110 }
1111 last->dependant_link = new;
1112 last = new;
1113 } while (rv > 0);
1114 continue;
1115 }
1116 if (type == ISAPNP_TAG_DEP_END) {
1117 DPRINTF(("\ttag enddep\n"));
1118 if (!in_depends) {
1119 printf("tag %d end dep?\n", tag);
1120 return (-1);
1121 }
1122 break;
1123 }
1124 if (!smallrescs[type - 1].handler) {
1125 #ifdef PNPBIOSDEBUG
1126 pnp_debugdump(r, start, len);
1127 #endif
1128 } else if (
1129 (*smallrescs[type - 1].handler)(r, start, len))
1130 return (-1);
1131 }
1132 }
1133 *bufp = p;
1134 return (0);
1135 }
1136
1137 static int
1138 pnp_newirq(struct pnpresources *r, const void *vres, size_t len)
1139 {
1140 const struct pnpirqres *res;
1141 struct pnp_irq *irq;
1142
1143 res = vres;
1144 if (res->r_mask == 0) { /* disabled */
1145 DPRINTF(("\ttag irq zeroed\n"));
1146 return (0);
1147 }
1148 irq = malloc(sizeof(struct pnp_irq), M_DEVBUF, M_NOWAIT);
1149 irq->mask = res->r_mask;
1150 if (len > 2)
1151 irq->flags = res->r_info;
1152 else
1153 irq->flags = 0x01;
1154 SIMPLEQ_INSERT_TAIL(&r->irq, irq, next);
1155 r->numirq++;
1156
1157 DPRINTF(("\ttag irq flags %02x mask %04x\n", irq->flags,irq->mask));
1158
1159 return (0);
1160 }
1161
1162 static int
1163 pnp_newdma(struct pnpresources *r, const void *vres, size_t len)
1164 {
1165 const struct pnpdmares *res;
1166 struct pnp_dma *dma;
1167
1168 res = vres;
1169 if (res->r_mask == 0) { /* disabled */
1170 DPRINTF(("\ttag DMA zeroed\n"));
1171 return (0);
1172 }
1173 dma = malloc(sizeof(struct pnp_dma), M_DEVBUF, M_NOWAIT);
1174 dma->mask = res->r_mask;
1175 dma->flags = res->r_flags;
1176 SIMPLEQ_INSERT_TAIL(&r->dma, dma, next);
1177 r->numdma++;
1178
1179 DPRINTF(("\ttag DMA flags %02x mask %02x\n", dma->flags,dma->mask));
1180
1181 return (0);
1182 }
1183
1184 static int
1185 pnp_newioport(struct pnpresources *r, const void *vres, size_t len)
1186 {
1187 const struct pnpportres *res;
1188 struct pnp_io *io;
1189
1190 res = vres;
1191 if (res->r_len == 0) { /* disabled */
1192 DPRINTF(("\ttag io zeroed\n"));
1193 return (0);
1194 }
1195 io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
1196 io->flags = res->r_flags;
1197 io->minbase = res->r_minbase;
1198 io->maxbase = res->r_maxbase;
1199 io->align = res->r_align;
1200 io->len = res->r_len;
1201 SIMPLEQ_INSERT_TAIL(&r->io, io, next);
1202 r->numio++;
1203
1204 DPRINTF(("\ttag io flags %02x min %04x max %04x align "
1205 "0x%02x len 0x%02x\n", io->flags, io->minbase, io->maxbase,
1206 io->align, io->len));
1207
1208 return (0);
1209 }
1210
1211 static int
1212 pnp_newfixedioport(struct pnpresources *r, const void *vres, size_t len)
1213 {
1214 const struct pnpfixedportres *res;
1215 struct pnp_io *io;
1216
1217 res = vres;
1218 if (res->r_len == 0) { /* disabled */
1219 DPRINTF(("\ttag fixedio zeroed\n"));
1220 return (0);
1221 }
1222 io = malloc(sizeof(struct pnp_io), M_DEVBUF, M_NOWAIT);
1223 io->flags = 1; /* 10 bit decoding */
1224 io->minbase = io->maxbase = res->r_base;
1225 io->align = 1;
1226 io->len = res->r_len;
1227 SIMPLEQ_INSERT_TAIL(&r->io, io, next);
1228 r->numio++;
1229
1230 DPRINTF(("\ttag fixedio flags %02x base %04x align %02x len %02x\n",
1231 io->flags, io->minbase, io->align, io->len));
1232
1233 return (0);
1234 }
1235
1236 static int
1237 pnp_compatid(struct pnpresources *r, const void *vres, size_t len)
1238 {
1239 const struct pnpcompatres *res;
1240 struct pnp_compatid *id;
1241
1242 res = vres;
1243 id = malloc(sizeof(*id), M_DEVBUF, M_NOWAIT);
1244 pnpbios_id_to_string(res->r_id, id->idstr);
1245 id->next = r->compatids;
1246 r->compatids = id;
1247
1248 DPRINTF(("\ttag compatid %s\n", id->idstr));
1249
1250 return (0);
1251 }
1252
1253 #ifdef PNPBIOSDEBUG
1254 static int
1255 pnp_debugdump(struct pnpresources *r, const void *vres, size_t len)
1256 {
1257 const u_int8_t *res = vres;
1258 int type, i;
1259
1260 if (res[0] & ISAPNP_LARGE_TAG) {
1261 type = res[0] & 0x7f;
1262 printf("\tTAG %02x len %04x %s", type, len, len ? "data" : "");
1263 i = 3;
1264 } else {
1265 type = (res[0] >> 3) & 0x0f;
1266 printf("\tTAG %02x len %02x %s", type, len, len ? "data" : "");
1267 i = 1;
1268 }
1269 for (; i < len; i++)
1270 printf(" %02x", res[i]);
1271 printf("\n");
1272
1273 return (0);
1274 }
1275 #endif
1276
1277 int
1278 pnpbios_io_map(pnpbios_tag_t pbt, struct pnpresources *resc,
1279 int idx, bus_space_tag_t *tagp, bus_space_handle_t *hdlp)
1280 {
1281 struct pnp_io *io;
1282
1283 if (idx >= resc->numio)
1284 return (EINVAL);
1285
1286 io = SIMPLEQ_FIRST(&resc->io);
1287 while (idx--)
1288 io = SIMPLEQ_NEXT(io, next);
1289
1290 *tagp = X86_BUS_SPACE_IO;
1291 return (x86_memio_map(X86_BUS_SPACE_IO, io->minbase, io->len,
1292 0, hdlp));
1293 }
1294
1295 void
1296 pnpbios_io_unmap(pnpbios_tag_t pbt, struct pnpresources *resc,
1297 int idx, bus_space_tag_t tag, bus_space_handle_t hdl)
1298 {
1299 struct pnp_io *io;
1300
1301 if (idx >= resc->numio)
1302 return;
1303
1304 io = SIMPLEQ_FIRST(&resc->io);
1305 while (idx--)
1306 io = SIMPLEQ_NEXT(io, next);
1307
1308 x86_memio_unmap(tag, hdl, io->len);
1309 }
1310
1311 int
1312 pnpbios_getiobase(pnpbios_tag_t pbt, struct pnpresources *resc,
1313 int idx, bus_space_tag_t *tagp, int *basep)
1314 {
1315 struct pnp_io *io;
1316
1317 if (idx >= resc->numio)
1318 return (EINVAL);
1319
1320 io = SIMPLEQ_FIRST(&resc->io);
1321 while (idx--)
1322 io = SIMPLEQ_NEXT(io, next);
1323
1324 if (tagp)
1325 *tagp = X86_BUS_SPACE_IO;
1326 if (basep)
1327 *basep = io->minbase;
1328 return (0);
1329 }
1330
1331 int
1332 pnpbios_getiosize(pnpbios_tag_t pbt, struct pnpresources *resc,
1333 int idx, int *sizep)
1334 {
1335 struct pnp_io *io;
1336
1337 if (idx >= resc->numio)
1338 return (EINVAL);
1339
1340 io = SIMPLEQ_FIRST(&resc->io);
1341 while (idx--)
1342 io = SIMPLEQ_NEXT(io, next);
1343 if (sizep)
1344 *sizep = io->len;
1345 return (0);
1346 }
1347
1348 void *
1349 pnpbios_intr_establish(pnpbios_tag_t pbt, struct pnpresources *resc,
1350 int idx, int level, int (*fcn)(void *), void *arg)
1351 {
1352 struct pnp_irq *irq;
1353 int irqnum, type;
1354
1355 if (idx >= resc->numirq)
1356 return (0);
1357
1358 irq = SIMPLEQ_FIRST(&resc->irq);
1359 while (idx--)
1360 irq = SIMPLEQ_NEXT(irq, next);
1361
1362 irqnum = ffs(irq->mask) - 1;
1363 type = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
1364
1365 return (isa_intr_establish(0, irqnum, type, level, fcn, arg));
1366 }
1367
1368 int
1369 pnpbios_getirqnum(pnpbios_tag_t pbt, struct pnpresources *resc,
1370 int idx, int *irqp, int *istp)
1371 {
1372 struct pnp_irq *irq;
1373
1374 if (idx >= resc->numirq)
1375 return (EINVAL);
1376
1377 irq = SIMPLEQ_FIRST(&resc->irq);
1378 while (idx--)
1379 irq = SIMPLEQ_NEXT(irq, next);
1380
1381 if (irqp != NULL)
1382 *irqp = ffs(irq->mask) - 1;
1383 if (istp != NULL)
1384 *istp = (irq->flags & 0x0c) ? IST_LEVEL : IST_EDGE;
1385 return (0);
1386 }
1387
1388 int
1389 pnpbios_getdmachan(pnpbios_tag_t pbt, struct pnpresources *resc,
1390 int idx, int *chanp)
1391 {
1392 struct pnp_dma *dma;
1393
1394 if (idx >= resc->numdma)
1395 return (EINVAL);
1396
1397 dma = SIMPLEQ_FIRST(&resc->dma);
1398 while (idx--)
1399 dma = SIMPLEQ_NEXT(dma, next);
1400
1401 *chanp = ffs(dma->mask) - 1;
1402 return (0);
1403 }
1404
1405 #ifdef PNPBIOSEVENTS
1406 static void
1407 pnpbios_create_event_thread(void *arg)
1408 {
1409 struct pnpbios_softc *sc;
1410
1411 sc = arg;
1412 if (kthread_create1(pnpbios_event_thread, sc, &sc->sc_evthread,
1413 "%s", sc->sc_dev.dv_xname))
1414 panic("pnpbios_create_event_thread");
1415 }
1416
1417 static void
1418 pnpbios_event_thread(void *arg)
1419 {
1420 struct pnpbios_softc *sc;
1421 u_int16_t event;
1422 u_int evflag;
1423 int rv, poll;
1424
1425 sc = arg;
1426 if ((sc->sc_control & PNP_IC_CONTORL_EVENT_MASK)
1427 != PNP_IC_CONTROL_EVENT_POLL)
1428 poll = 0;
1429 else {
1430 poll = hz;
1431 rv = pnpbios_sendmessage(PNP_CM_PNP_OS_ACTIVE);
1432 EDPRINTF(("pnpbios: os active returns 0x%02x\n", rv));
1433 }
1434
1435 config_pending_decr();
1436
1437 goto start;
1438 while (sc->sc_threadrun) {
1439 /* maybe we have an event */
1440 if (!poll)
1441 (void)tsleep(pnpbios_event_thread, PWAIT,
1442 "pnpbiosevent", 0);
1443 else if (((evflag = *sc->sc_evaddr) & 0x01) == 0) {
1444 if (evflag)
1445 EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
1446 (void)tsleep(pnpbios_event_thread, PWAIT,
1447 "pnpbiosevent", poll);
1448 continue;
1449 } else {
1450 EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag));
1451 }
1452 start:
1453 if ((rv = pnpbios_getevent(&event))) {
1454 EDPRINTF(("pnpbios: getevent rc: 0x%02x\n", rv));
1455 #ifdef DIAGNOSTIC
1456 if (rv != PNP_RC_EVENTS_NOT_PENDING)
1457 printf("%s: getevent failed: %d\n",
1458 sc->sc_dev.dv_xname, rv);
1459 #endif
1460 continue;
1461 }
1462 switch (event) {
1463 case PNP_EID_ABOUT_TO_CHANGE_CONFIG:
1464 EDPRINTF(("pnpbios: about to change event\n"));
1465 /*
1466 * The system is about to be docked or undocked.
1467 * Acknowledge the event, so that the procedure
1468 * can continue.
1469 * XXX When should we ever send an ABORT?
1470 */
1471 pnpbios_sendmessage(PNP_RM_OK);
1472 break;
1473 case PNP_EID_DOCK_CHANGED:
1474 {
1475 int odocked;
1476
1477 EDPRINTF(("pnpbios: dock changed event\n"));
1478
1479 odocked = pnpbios_update_dock_status(sc);
1480 if (odocked == sc->sc_docked)
1481 break;
1482 switch (sc->sc_docked) {
1483 case 0:
1484 /* We have been undocked. */
1485 /* XXX detach devices XXX */
1486 break;
1487
1488 case 1:
1489 /* We have been docked. */
1490 /* XXX attach devices XXX */
1491 break;
1492
1493 default:
1494 /* getdockinfo failed! */
1495 printf("%s: dock changed event, but unable "
1496 "to get dock info; event ignored\n",
1497 sc->sc_dev.dv_xname);
1498 }
1499 break;
1500 }
1501 case PNP_EID_SYSTEM_DEVICE_CHANGED:
1502 EDPRINTF(("pnpbios: system device changed event\n"));
1503 break;
1504 case PNP_EID_CONFIG_CHANGE_FAILED:
1505 EDPRINTF(("pnpbios: config changed event\n"));
1506 break;
1507 case PNP_EID_UNKNOWN_SYSTEM_EVENT:
1508 #ifdef DIAGNOSTIC
1509 printf("%s: \"unknown system event\"\n",
1510 sc->sc_dev.dv_xname);
1511 #endif
1512 break;
1513 default:
1514 #ifdef DIAGNOSTIC
1515 if (event & PNP_EID_OEM_DEFINED_BIT)
1516 printf("%s: vendor defined event 0x%04x\n",
1517 sc->sc_dev.dv_xname, event);
1518 else
1519 printf("%s: unknown event 0x%04x\n",
1520 sc->sc_dev.dv_xname, event);
1521 #endif
1522 break;
1523 }
1524 }
1525
1526 pnpbios_sendmessage(PNP_CM_PNP_OS_INACTIVE);
1527 kthread_exit(0);
1528 }
1529 #endif /* PNPBIOSEVENTS */
1530