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