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