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