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