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