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