1 /* $NetBSD: pnpbios.c,v 1.67 2009/11/03 13:14:50 cegger Exp $ */
4 * Copyright (c) 2000 Jason R. Thorpe. All rights reserved.
5 * Copyright (c) 2000 Christian E. Hopps. All rights reserved.
7 * Matthias Drochner. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
32 * PnP BIOS documentation is available at the following locations.
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
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
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: pnpbios.c,v 1.67 2009/11/03 13:14:50 cegger Exp $");
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>
53 #include <uvm/uvm_extern.h>
55 #include <machine/isa_machdep.h>
56 #include <machine/segments.h>
58 #include <dev/isa/isareg.h>
59 #include <dev/isapnp/isapnpreg.h>
61 #include <arch/i386/pnpbios/pnpbiosvar.h>
62 #include <arch/i386/pnpbios/pnpbiosreg.h>
64 #include "opt_pnpbios.h"
68 int pnpbiosverbose
= 1;
70 int pnpbiosverbose
= 0;
74 #ifdef PNPBIOSDEBUG_VALUE
75 int pnpbiosdebug
= PNPBIOSDEBUG_VALUE
;
79 #define DPRINTF(x) if (pnpbiosdebug) aprint_normal x
84 #ifdef PNPBIOSEVENTSDEBUG
85 #define EDPRINTF(x) aprint_normal x
90 struct pnpbios_softc
{
92 isa_chipset_tag_t sc_ic
;
103 #define PNPGET4(p) ((p)[0] + ((p)[1] << 8) + \
104 ((p)[2] << 16) + ((p)[3] << 24))
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);
114 static int pnpbios_getnode(int, int *,
116 static int pnpbios_getnumnodes(int *, size_t *);
119 static int pnpbios_getdockinfo(struct pnpdockinfo
*);
121 static int pnpbios_getevent(uint16_t *);
122 static void pnpbios_event_thread(void *);
123 static int pnpbios_sendmessage(int);
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 *,
138 static int pnp_scan(const uint8_t **, size_t,
139 struct pnpresources
*, int);
140 extern int pnpbioscall(int);
142 static void pnpbios_enumerate(struct pnpbios_softc
*);
144 static int pnpbios_update_dock_status(struct pnpbios_softc
*);
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);
154 static int pnp_debugdump(struct pnpresources
*, const void *, size_t);
158 * small ressource types (beginning with 1)
161 int (*handler
)(struct pnpresources
*, const void *, size_t);
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 */
177 {0, 1, 7}, /* vendor defined */
182 CFATTACH_DECL_NEW(pnpbios
, sizeof(struct pnpbios_softc
),
183 pnpbios_match
, pnpbios_attach
, NULL
, NULL
);
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.
189 #define PNPBIOS_BUFSIZE 4096
191 int pnpbios_enabled
= 1;
192 size_t pnpbios_entry
;
193 char *pnpbios_scratchbuf
;
196 * There can be only one of these, and the i386 ISA code needs to
199 struct pnpbios_softc
*pnpbios_softc
;
201 #define PNPBIOS_SIGNATURE ('$' | ('P' << 8) | ('n' << 16) | ('P' << 24))
210 for (p
= (char *)ISA_HOLE_VADDR(0xf0000);
211 p
<= (char *)ISA_HOLE_VADDR(0xffff0);
213 if (*(int *)p
!= PNPBIOS_SIGNATURE
)
215 structlen
= *(uint8_t *)(p
+ 5);
216 if ((structlen
< 0x21) ||
217 ((p
+ structlen
- 1) > (char *)ISA_HOLE_VADDR(0xfffff)))
221 for (c
= p
; c
< p
+ structlen
; c
++)
222 cksum
+= *(uint8_t *)c
;
226 if (*(char *)(p
+ 4) != 0x10) {
227 printf("unknown version %x\n", *(char *)(p
+ 4));
241 return (pnpbios_find() != 0);
245 pnpbios_match(device_t parent
, cfdata_t match
, void *aux
)
248 /* There can be only one! */
249 if (pnpbios_softc
!= NULL
)
252 return (pnpbios_enabled
);
256 pnpbios_mapit(paddr_t addr
, u_long len
, vm_prot_t prot
)
258 paddr_t startpa
, pa
, endpa
;
261 pa
= startpa
= x86_trunc_page(addr
);
262 endpa
= x86_round_page(addr
+ len
);
264 va
= startva
= uvm_km_alloc(kernel_map
, endpa
- startpa
, 0,
268 for (; pa
< endpa
; pa
+= PAGE_SIZE
, va
+= PAGE_SIZE
)
269 pmap_kenter_pa(va
, pa
, prot
, 0);
270 pmap_update(pmap_kernel());
272 return ((void *)(startva
+ (addr
- startpa
)));
276 pnpbios_attach(device_t parent
, device_t self
, void *aux
)
278 struct pnpbios_softc
*sc
= device_private(self
);
279 struct pnpbios_attach_args
*paa
= aux
;
281 unsigned int codepbase
, datapbase
, evaddrp
;
282 void *codeva
, *datava
;
283 extern char pnpbiostramp
[], epnpbiostramp
[];
293 sc
->sc_ic
= paa
->paa_ic
;
297 panic("pnpbios_attach: disappeared");
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);
306 if (pnpbiosverbose
) {
307 aprint_normal(": code %x, data %x, entry %x, control %x,"
309 codepbase
, datapbase
, pnpbios_entry
, sc
->sc_control
,
310 (unsigned int)evaddrp
, device_xname(self
));
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
);
320 aprint_error_dev(self
, "couldn't map event flag 0x%08x\n",
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");
333 pnpbios_scratchbuf
= malloc(PNPBIOS_BUFSIZE
, M_DEVBUF
, M_NOWAIT
);
335 setsegment(&gdt
[GPNPBIOSCODE_SEL
].sd
, codeva
, 0xffff,
336 SDT_MEMERA
, SEL_KPL
, 0, 0);
337 setsegment(&gdt
[GPNPBIOSDATA_SEL
].sd
, datava
, 0xffff,
338 SDT_MEMRWA
, SEL_KPL
, 0, 0);
339 setsegment(&gdt
[GPNPBIOSSCRATCH_SEL
].sd
,
340 pnpbios_scratchbuf
, PNPBIOS_BUFSIZE
- 1,
341 SDT_MEMRWA
, SEL_KPL
, 0, 0);
342 setsegment(&gdt
[GPNPBIOSTRAMP_SEL
].sd
,
343 pnpbiostramp
, epnpbiostramp
- pnpbiostramp
- 1,
344 SDT_MEMERA
, SEL_KPL
, 1, 0);
346 res
= pnpbios_getnumnodes(&num
, &size
);
348 aprint_error(": pnpbios_getnumnodes: error %d\n", res
);
352 aprint_normal(": nodes %d, max len %d\n", num
, size
);
355 EDPRINTF(("%s: event flag vaddr 0x%08x\n", device_xname(self
),
356 (int)sc
->sc_evaddr
));
358 /* Set initial dock status. */
360 (void) pnpbios_update_dock_status(sc
);
363 /* Enumerate the device nodes. */
364 pnpbios_enumerate(sc
);
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();
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");
383 pnpbios_enumerate(struct pnpbios_softc
*sc
)
385 int res
, num
, i
, size
, idx
, dynidx
;
386 struct pnpdevnode
*dn
;
389 res
= pnpbios_getnumnodes(&num
, &size
);
391 aprint_error_dev(sc
->sc_dev
, "pnpbios_getnumnodes: error %d\n",
396 buf
= malloc(size
, M_DEVBUF
, M_NOWAIT
);
398 aprint_error_dev(sc
->sc_dev
, "unable to allocate node buffer\n");
403 * Loop through the list of indices getting data and match/attaching
404 * each as appropriate.
406 * Unfortunately, some BIOSes seem to have fatal bugs getting the
407 * dynamic (i.e. currently active) configuration, for instance some
408 * Sony VAIO laptops, including the PCG-Z505HE. They don't have such a
409 * problem with that static (i.e. next boot time) configuration,
410 * however. The workaround is to get the static configuration for all
411 * indices, and only get dynamic configuration for devices where the
414 * This seems to work conveniently as the indices that cause
415 * crashes (and it seems to vary from machine to machine) do not
416 * seem to be for devices that NetBSD's pnpbios supports.
420 for (i
= 0; i
< num
&& idx
!= 0xff; i
++) {
421 DPRINTF(("%s: getting info for index %d\n",
422 device_xname(sc
->sc_dev
), idx
));
426 res
= pnpbios_getnode(PNP_CF_DEVCONF_STATIC
, &idx
, buf
, size
);
428 aprint_error_dev(sc
->sc_dev
, "index %d error %d "
429 "getting static configuration\n", idx
, res
);
432 dn
= (struct pnpdevnode
*)buf
;
433 if (!pnpbios_attachnode(sc
, dn
->dn_handle
, buf
, dn
->dn_size
, 1)) {
434 DPRINTF(("%s handle %d: no match from static config\n",
435 device_xname(sc
->sc_dev
), dn
->dn_handle
));
439 res
= pnpbios_getnode(PNP_CF_DEVCONF_DYNAMIC
, &dynidx
, buf
, size
);
441 aprint_error_dev(sc
->sc_dev
, "index %d error %d "
442 "getting dynamic configuration\n", dynidx
, res
);
445 dn
= (struct pnpdevnode
*)buf
;
446 if (!pnpbios_attachnode(sc
, dn
->dn_handle
, buf
, dn
->dn_size
, 0)) {
447 DPRINTF(("%s handle %d: no match from dynamic config\n",
448 device_xname(sc
->sc_dev
), dn
->dn_handle
));
453 aprint_error_dev(sc
->sc_dev
, "got only %d nodes\n", i
);
455 aprint_error_dev(sc
->sc_dev
, "last index %d\n", idx
);
462 pnpbios_update_dock_status(struct pnpbios_softc
*sc
)
464 struct pnpdockinfo di
;
465 const char *when
, *style
;
466 int res
, odocked
= sc
->sc_docked
;
468 res
= pnpbios_getdockinfo(&di
);
469 if (res
== PNP_RC_SYSTEM_NOT_DOCKED
) {
471 if (odocked
!= sc
->sc_docked
)
472 printf("%s: not docked\n", device_xname(sc
->sc_dev
));
474 EDPRINTF(("%s: dockinfo failed 0x%02x\n",
475 device_xname(sc
->sc_dev
), res
));
478 if (odocked
!= sc
->sc_docked
) {
480 pnpbios_id_to_string(di
.di_id
, idstr
);
481 printf("%s: dock id %s", device_xname(sc
->sc_dev
), idstr
);
482 if (pnpbiosverbose
) {
483 if (di
.di_serial
!= -1)
484 printf(", serial number %d",
487 switch (di
.di_cap
& PNP_DI_DOCK_STYLE_MASK
) {
488 case PNP_DI_DOCK_STYLE_SUPRISE
:
491 case PNP_DI_DOCK_STYLE_VCR
:
492 style
= "controlled";
495 style
= "<style unknown>";
498 switch (di
.di_cap
& PNP_DI_DOCK_WHEN_MASK
) {
499 case PNP_DI_DOCK_WHEN_NO_POWER
:
502 case PNP_DI_DOCK_WHEN_SUSPENDED
:
505 case PNP_DI_DOCK_WHEN_RUNNING
:
508 case PNP_DI_DOCK_WHEN_RESERVED
:
512 when
= "<dock type unknown>";
515 printf(", %s %s docking\n", style
, when
);
524 pnpbios_getnumnodes(int *nump
, size_t *sizep
)
527 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
529 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
530 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
531 *--help
= 2; /* buffer offset for node size */
532 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
533 *--help
= 0; /* buffer offset for numnodes */
534 *--help
= PNP_FC_GET_NUM_NODES
;
536 res
= pnpbioscall(((char *)help
) - pnpbios_scratchbuf
);
540 *nump
= *(short *)(pnpbios_scratchbuf
+ 0);
541 *sizep
= *(short *)(pnpbios_scratchbuf
+ 2);
546 pnpbios_getnode(int flags
, int *idxp
, uint8_t *buf
, size_t len
)
549 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
551 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
553 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
554 *--help
= 2; /* buffer offset for node data */
555 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
556 *--help
= 0; /* buffer offset for index in/out */
557 *--help
= PNP_FC_GET_DEVICE_NODE
;
559 *(short *)(pnpbios_scratchbuf
+ 0) = *idxp
;
561 res
= pnpbioscall(((char *)help
) - pnpbios_scratchbuf
);
565 *idxp
= *(short *)(pnpbios_scratchbuf
+ 0);
566 memcpy(buf
, pnpbios_scratchbuf
+ 2, len
);
572 /* XXX - pnpbios_setnode() is never called. */
575 pnpbios_setnode(int flags
, int idx
, const uint8_t *buf
, size_t len
)
577 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
579 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
581 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
582 *--help
= 0; /* buffer offset for node data */
584 *--help
= PNP_FC_SET_DEVICE_NODE
;
586 memcpy(pnpbios_scratchbuf
, buf
, len
);
588 return (pnpbioscall(((void *)help
) - pnpbios_scratchbuf
));
594 pnpbios_getevent(uint16_t *event
)
597 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
599 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
600 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
601 *--help
= 0; /* buffer offset for message data */
602 *--help
= PNP_FC_GET_EVENT
;
604 res
= pnpbioscall(((void *)help
) - pnpbios_scratchbuf
);
605 *event
= pnpbios_scratchbuf
[0] + (pnpbios_scratchbuf
[1] << 8);
610 pnpbios_sendmessage(int msg
)
612 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
614 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
616 *--help
= PNP_FC_SEND_MESSAGE
;
618 return (pnpbioscall(((void *)help
) - pnpbios_scratchbuf
));
622 pnpbios_getdockinfo(struct pnpdockinfo
*di
)
625 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
627 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
628 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
629 *--help
= 0; /* buffer offset for dock info */
630 *--help
= PNP_FC_GET_DOCK_INFO
;
632 res
= pnpbioscall(((void *)help
) - pnpbios_scratchbuf
);
633 memcpy(di
, pnpbios_scratchbuf
, sizeof(*di
));
636 #endif /* PNPBIOSEVENTS */
639 /* XXX - pnpbios_getapmtable() is not called. */
641 /* XXX we don't support more than PNPBIOS_BUFSIZE - (stacklen + 2) */
643 pnpbios_getapmtable(uint8_t *tab
, size_t *len
)
645 short *help
= (short *)(pnpbios_scratchbuf
+ PNPBIOS_BUFSIZE
);
646 size_t origlen
, stacklen
;
649 *--help
= GSEL(GPNPBIOSDATA_SEL
, SEL_KPL
);
650 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
651 *--help
= 2; /* buffer offset for table */
652 *--help
= GSEL(GPNPBIOSSCRATCH_SEL
, SEL_KPL
);
653 *--help
= 0; /* buffer offset for length */
654 *--help
= PNP_FC_GET_APM_TABLE
;
657 stacklen
= (void *)help
- pnpbios_scratchbuf
;
658 if (origlen
> PNPBIOS_BUFSIZE
- stacklen
- 2)
659 origlen
= PNPBIOS_BUFSIZE
- stacklen
- 2;
660 *(uint16_t *)(pnpbios_scratchbuf
) = origlen
;
662 res
= pnpbioscall(((void *)help
) - pnpbios_scratchbuf
);
663 *len
= *(uint16_t *)pnpbios_scratchbuf
;
666 if (origlen
&& *len
> origlen
) {
667 printf("pnpbios: returned apm table exceed requested size\n");
668 return (PNP_RC_BUFFER_TOO_SMALL
);
670 memcpy(tab
, pnpbios_scratchbuf
+ 2, *len
);
676 pnpbios_id_to_string(uint32_t pnpid
, char *s
)
680 id
= (uint8_t *)&pnpid
;
681 *s
++ = 'A' + (id
[0] >> 2) - 1;
682 *s
++ = 'A' + ((id
[0] & 3) << 3) + (id
[1] >> 5) - 1;
683 *s
++ = 'A' + (id
[1] & 0x1f) - 1;
684 *s
++ = HEXDIGITS
[id
[2] >> 4];
685 *s
++ = HEXDIGITS
[id
[2] & 0x0f];
686 *s
++ = HEXDIGITS
[id
[3] >> 4];
687 *s
++ = HEXDIGITS
[id
[3] & 0x0f];
692 pnpbios_printres(struct pnpresources
*r
)
700 mem
= SIMPLEQ_FIRST(&r
->mem
);
702 aprint_normal("mem");
704 aprint_normal(" %x", mem
->minbase
);
707 mem
->minbase
+ mem
->len
- 1);
708 } while ((mem
= SIMPLEQ_NEXT(mem
, next
)));
711 io
= SIMPLEQ_FIRST(&r
->io
);
717 aprint_normal(" %x", io
->minbase
);
720 io
->minbase
+ io
->len
- 1);
721 } while ((io
= SIMPLEQ_NEXT(io
, next
)));
723 irq
= SIMPLEQ_FIRST(&r
->irq
);
727 aprint_normal("irq");
729 aprint_normal(" %d", ffs(irq
->mask
) - 1);
730 } while ((irq
= SIMPLEQ_NEXT(irq
, next
)));
732 dma
= SIMPLEQ_FIRST(&r
->dma
);
736 aprint_normal("DMA");
738 aprint_normal(" %d", ffs(dma
->mask
) - 1);
739 } while ((dma
= SIMPLEQ_NEXT(dma
, next
)));
744 pnpbios_print(void *aux
, const char *pnp
)
746 struct pnpbiosdev_attach_args
*aa
= aux
;
751 aprint_normal(" index %d (%s", aa
->idx
, aa
->primid
);
752 if (aa
->resc
->longname
)
753 aprint_normal(", %s", aa
->resc
->longname
);
754 if (aa
->idstr
!= aa
->primid
)
755 aprint_normal(", attached as %s", aa
->idstr
);
762 pnpbios_print_devres(device_t dev
, struct pnpbiosdev_attach_args
*aa
)
765 aprint_normal_dev(dev
, "");
766 pnpbios_printres(aa
->resc
);
771 pnpbios_attachchild(struct pnpbios_softc
*sc
,
772 struct pnpbiosdev_attach_args
*aa
, int matchonly
)
774 int locs
[PNPBIOSCF_NLOCS
];
776 locs
[PNPBIOSCF_INDEX
] = aa
->idx
;
779 return (config_search_loc(config_stdsubmatch
, sc
->sc_dev
,
780 "pnpbios", locs
, aa
) != NULL
);
782 return (config_found_sm_loc(sc
->sc_dev
, "pnpbios",
783 locs
, aa
, pnpbios_print
, config_stdsubmatch
)
788 pnpbios_attachnode(struct pnpbios_softc
*sc
, int idx
, const uint8_t *buf
,
789 size_t len
, int matchonly
)
791 const struct pnpdevnode
*dn
;
794 struct pnpresources r
, s
;
795 struct pnpbiosdev_attach_args aa
;
796 struct pnp_compatid
*compatid
;
799 dn
= (const struct pnpdevnode
*)buf
;
800 pnpbios_id_to_string(dn
->dn_product
, idstr
);
801 p
= (const u_char
*)(dn
+ 1);
803 DPRINTF(("%s (%s): type 0x%02x subtype "
804 "0x%02x dpi 0x%02x attr 0x%04x:\n",
805 idstr
, matchonly
? "static" : "dynamic", dn
->dn_type
,
806 dn
->dn_subtype
, dn
->dn_dpi
, dn
->dn_attr
));
807 DPRINTF(("%s: allocated config scan:\n", idstr
));
808 res
= pnp_scan(&p
, len
- 12, &r
, 0);
810 aprint_error("error in config data\n");
815 * the following is consistency check only for now
817 DPRINTF(("\tpossible config scan:\n"));
818 res
= pnp_scan(&p
, len
- (p
- buf
), &s
, 0);
820 aprint_error("error in possible configuration\n");
824 DPRINTF(("\tcompat id scan:\n"));
825 res
= pnp_scan(&p
, len
- (p
- buf
), &s
, 0);
827 aprint_error("error in compatible ID\n");
831 if (p
!= buf
+ len
) {
832 aprint_error_dev(sc
->sc_dev
, "length mismatch in node %d:"
833 " used %d of %d Bytes\n",
836 /* XXX shouldn't happen - pnp_scan should catch it */
839 /* Crappy BIOS: Buffer is not fully used. Be generous. */
842 if (r
.nummem
+ r
.numio
+ r
.numirq
+ r
.numdma
== 0) {
843 if (pnpbiosverbose
) {
844 aprint_normal("%s", idstr
);
846 aprint_normal(", %s", r
.longname
);
847 compatid
= s
.compatids
;
849 aprint_normal(", %s", compatid
->idstr
);
850 compatid
= compatid
->next
;
852 aprint_normal(" at %s index %d disabled\n",
853 device_xname(sc
->sc_dev
), idx
);
858 aa
.pbt
= 0; /* XXX placeholder */
864 /* first try the specific device ID */
866 if (pnpbios_attachchild(sc
, &aa
, matchonly
))
869 /* if no driver was found, try compatible IDs */
870 compatid
= s
.compatids
;
872 aa
.idstr
= compatid
->idstr
;
873 if (pnpbios_attachchild(sc
, &aa
, matchonly
))
875 compatid
= compatid
->next
;
878 if (pnpbiosverbose
) {
879 aprint_normal("%s", idstr
);
881 aprint_normal(", %s", r
.longname
);
882 compatid
= s
.compatids
;
884 aprint_normal(", %s", compatid
->idstr
);
885 compatid
= compatid
->next
;
888 pnpbios_printres(&r
);
889 aprint_normal(") at %s index %d ignored\n",
890 device_xname(sc
->sc_dev
), idx
);
895 /* XXX should free resource lists */
900 /* print some useful info */
901 if (len
>= sizeof(*dn
)) {
902 aprint_normal("%s idx %d size %d type 0x%x:0x%x:0x%x attr 0x%x\n",
903 idstr
, dn
->dn_handle
, dn
->dn_size
, dn
->dn_type
,
904 dn
->dn_subtype
, dn
->dn_dpi
, dn
->dn_attr
);
909 aprint_normal(" %02x", buf
[i
]);
915 pnp_scan(const uint8_t **bufp
, size_t maxlen
,
916 struct pnpresources
*r
, int in_depends
)
927 memset(r
, 0, sizeof(*r
));
928 SIMPLEQ_INIT(&r
->mem
);
929 SIMPLEQ_INIT(&r
->io
);
930 SIMPLEQ_INIT(&r
->irq
);
931 SIMPLEQ_INIT(&r
->dma
);
934 if (p
>= *bufp
+ maxlen
) {
935 aprint_normal("pnp_scanresources: end of buffer\n");
940 if (tag
& ISAPNP_LARGE_TAG
) {
941 len
= *(const uint16_t *)(p
+ 1);
942 p
+= sizeof(struct pnplargeres
) + len
;
945 case ISAPNP_TAG_MEM_RANGE_DESC
: {
946 const struct pnpmem16rangeres
*res
= start
;
947 if (len
!= sizeof(*res
) - 3) {
948 aprint_normal("pnp_scan: bad mem desc\n");
952 mem
= malloc(sizeof(struct pnp_mem
),
954 mem
->flags
= res
->r_flags
;
955 mem
->minbase
= res
->r_minbase
<< 8;
956 mem
->maxbase
= res
->r_maxbase
<< 8;
957 mem
->align
= res
->r_align
;
959 mem
->align
= 0x10000;
960 mem
->len
= res
->r_len
<< 8;
961 DPRINTF(("\ttag memrange "));
964 case ISAPNP_TAG_ANSI_IDENT_STRING
: {
965 const struct pnpansiidentres
*res
= start
;
967 aprint_normal("ID in dep?\n");
968 idstr
= malloc(len
+ 1, M_DEVBUF
, M_NOWAIT
);
969 for (i
= 0; i
< len
; i
++)
970 idstr
[i
] = res
->r_id
[i
];
973 DPRINTF(("\ttag ansiident %s\n", idstr
));
975 if (idstr
[0] == '\0') {
976 /* disabled device */
977 free(idstr
, M_DEVBUF
);
983 case ISAPNP_TAG_MEM32_RANGE_DESC
: {
984 const struct pnpmem32rangeres
*res
= start
;
985 if (len
!= sizeof(*res
) - 3) {
986 aprint_normal("pnp_scan: bad mem32 desc\n");
990 mem
= malloc(sizeof(struct pnp_mem
),
992 mem
->flags
= res
->r_flags
;
993 mem
->minbase
= res
->r_minbase
;
994 mem
->maxbase
= res
->r_maxbase
;
995 mem
->align
= res
->r_align
;
996 mem
->len
= res
->r_len
;
997 DPRINTF(("\ttag mem32range "));
1000 case ISAPNP_TAG_FIXED_MEM32_RANGE_DESC
: {
1001 const struct pnpfixedmem32rangeres
*res
= start
;
1002 if (len
!= sizeof(*res
) - 3) {
1003 aprint_normal("pnp_scan: bad mem32 desc\n");
1007 mem
= malloc(sizeof(struct pnp_mem
),
1008 M_DEVBUF
, M_WAITOK
);
1009 mem
->flags
= res
->r_flags
;
1010 mem
->minbase
= res
->r_base
;
1011 mem
->maxbase
= mem
->minbase
;
1013 mem
->len
= res
->r_len
;
1014 DPRINTF(("\ttag fixedmem32range "));
1016 if (mem
->len
== 0) { /* disabled */
1017 DPRINTF(("zeroed\n"));
1018 free(mem
, M_DEVBUF
);
1021 SIMPLEQ_INSERT_TAIL(&r
->mem
, mem
, next
);
1024 DPRINTF(("flags %02x min %08x max %08x "
1025 "align %08x len %08x\n", mem
->flags
,
1026 mem
->minbase
, mem
->maxbase
, mem
->align
,
1031 case ISAPNP_TAG_UNICODE_IDENT_STRING
:
1032 case ISAPNP_TAG_VENDOR_DEFINED
:
1035 pnp_debugdump(r
, start
, len
);
1040 type
= (tag
>> 3) & 0x0f;
1045 len
< smallrescs
[type
- 1].minlen
||
1046 len
> smallrescs
[type
- 1].maxlen
) {
1047 aprint_normal("pnp_scan: bad small resource\n");
1050 if (type
== ISAPNP_TAG_END
) {
1052 const struct pnpendres
*res
= start
;
1056 * this seems to occur and is
1057 * an optimization to not require
1058 * the end dep in a depend
1059 * that ends the section
1063 DPRINTF(("\ttag end cksum %02x\n",
1067 if (type
== ISAPNP_TAG_DEP_START
) {
1069 const struct pnpdepstartres
*res
= start
;
1071 struct pnpresources
*new, *last
;
1074 DPRINTF(("\ttag startdep flags %02x\n",
1075 len
? res
->r_pri
: ISAPNP_DEP_ACCEPTABLE
));
1077 if (r
->dependant_link
) {
1078 aprint_normal("second dep?\n");
1081 /* XXX not sure about this */
1088 new = malloc(sizeof(*new),
1089 M_DEVBUF
, M_NOWAIT
);
1091 rv
= pnp_scan(&p
, maxlen
- (p
- *bufp
),
1094 aprint_normal("error in"
1095 " dependant function\n");
1096 free(new, M_DEVBUF
);
1099 last
->dependant_link
= new;
1104 if (type
== ISAPNP_TAG_DEP_END
) {
1105 DPRINTF(("\ttag enddep\n"));
1107 aprint_normal("tag %d end dep?\n", tag
);
1112 if (!smallrescs
[type
- 1].handler
) {
1114 pnp_debugdump(r
, start
, len
);
1117 (*smallrescs
[type
- 1].handler
)(r
, start
, len
))
1126 pnp_newirq(struct pnpresources
*r
, const void *vres
, size_t len
)
1128 const struct pnpirqres
*res
;
1129 struct pnp_irq
*irq
;
1132 if (res
->r_mask
== 0) { /* disabled */
1133 DPRINTF(("\ttag irq zeroed\n"));
1136 irq
= malloc(sizeof(struct pnp_irq
), M_DEVBUF
, M_NOWAIT
);
1137 irq
->mask
= res
->r_mask
;
1139 irq
->flags
= res
->r_info
;
1142 SIMPLEQ_INSERT_TAIL(&r
->irq
, irq
, next
);
1145 DPRINTF(("\ttag irq flags %02x mask %04x\n", irq
->flags
,irq
->mask
));
1151 pnp_newdma(struct pnpresources
*r
, const void *vres
, size_t len
)
1153 const struct pnpdmares
*res
;
1154 struct pnp_dma
*dma
;
1157 if (res
->r_mask
== 0) { /* disabled */
1158 DPRINTF(("\ttag DMA zeroed\n"));
1161 dma
= malloc(sizeof(struct pnp_dma
), M_DEVBUF
, M_NOWAIT
);
1162 dma
->mask
= res
->r_mask
;
1163 dma
->flags
= res
->r_flags
;
1164 SIMPLEQ_INSERT_TAIL(&r
->dma
, dma
, next
);
1167 DPRINTF(("\ttag DMA flags %02x mask %02x\n", dma
->flags
,dma
->mask
));
1173 pnp_newioport(struct pnpresources
*r
, const void *vres
, size_t len
)
1175 const struct pnpportres
*res
;
1179 if (res
->r_len
== 0) { /* disabled */
1180 DPRINTF(("\ttag io zeroed\n"));
1183 io
= malloc(sizeof(struct pnp_io
), M_DEVBUF
, M_NOWAIT
);
1184 io
->flags
= res
->r_flags
;
1185 io
->minbase
= res
->r_minbase
;
1186 io
->maxbase
= res
->r_maxbase
;
1187 io
->align
= res
->r_align
;
1188 io
->len
= res
->r_len
;
1189 SIMPLEQ_INSERT_TAIL(&r
->io
, io
, next
);
1192 DPRINTF(("\ttag io flags %02x min %04x max %04x align "
1193 "0x%02x len 0x%02x\n", io
->flags
, io
->minbase
, io
->maxbase
,
1194 io
->align
, io
->len
));
1200 pnp_newfixedioport(struct pnpresources
*r
, const void *vres
,
1203 const struct pnpfixedportres
*res
;
1207 if (res
->r_len
== 0) { /* disabled */
1208 DPRINTF(("\ttag fixedio zeroed\n"));
1211 io
= malloc(sizeof(struct pnp_io
), M_DEVBUF
, M_NOWAIT
);
1212 io
->flags
= 1; /* 10 bit decoding */
1213 io
->minbase
= io
->maxbase
= res
->r_base
;
1215 io
->len
= res
->r_len
;
1216 SIMPLEQ_INSERT_TAIL(&r
->io
, io
, next
);
1219 DPRINTF(("\ttag fixedio flags %02x base %04x align %02x len %02x\n",
1220 io
->flags
, io
->minbase
, io
->align
, io
->len
));
1226 pnp_compatid(struct pnpresources
*r
, const void *vres
, size_t len
)
1228 const struct pnpcompatres
*res
;
1229 struct pnp_compatid
*id
;
1232 id
= malloc(sizeof(*id
), M_DEVBUF
, M_NOWAIT
);
1233 pnpbios_id_to_string(res
->r_id
, id
->idstr
);
1234 id
->next
= r
->compatids
;
1237 DPRINTF(("\ttag compatid %s\n", id
->idstr
));
1244 pnp_debugdump(struct pnpresources
*r
, const void *vres
, size_t len
)
1246 const uint8_t *res
= vres
;
1249 if (res
[0] & ISAPNP_LARGE_TAG
) {
1250 type
= res
[0] & 0x7f;
1251 aprint_normal("\tTAG %02x len %04x %s",
1252 type
, len
, len
? "data" : "");
1255 type
= (res
[0] >> 3) & 0x0f;
1256 aprint_normal("\tTAG %02x len %02x %s",
1257 type
, len
, len
? "data" : "");
1260 for (; i
< len
; i
++)
1261 aprint_normal(" %02x", res
[i
]);
1262 aprint_normal("\n");
1269 pnpbios_io_map(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1270 int idx
, bus_space_tag_t
*tagp
, bus_space_handle_t
*hdlp
)
1274 if (idx
>= resc
->numio
)
1277 io
= SIMPLEQ_FIRST(&resc
->io
);
1279 io
= SIMPLEQ_NEXT(io
, next
);
1281 *tagp
= X86_BUS_SPACE_IO
;
1282 return (bus_space_map(X86_BUS_SPACE_IO
, io
->minbase
, io
->len
,
1287 pnpbios_io_unmap(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1288 int idx
, bus_space_tag_t tag
, bus_space_handle_t hdl
)
1292 if (idx
>= resc
->numio
)
1295 io
= SIMPLEQ_FIRST(&resc
->io
);
1297 io
= SIMPLEQ_NEXT(io
, next
);
1299 bus_space_unmap(tag
, hdl
, io
->len
);
1303 pnpbios_getiobase(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1304 int idx
, bus_space_tag_t
*tagp
, int *basep
)
1308 if (idx
>= resc
->numio
)
1311 io
= SIMPLEQ_FIRST(&resc
->io
);
1313 io
= SIMPLEQ_NEXT(io
, next
);
1316 *tagp
= X86_BUS_SPACE_IO
;
1318 *basep
= io
->minbase
;
1323 pnpbios_getiosize(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1324 int idx
, int *sizep
)
1328 if (idx
>= resc
->numio
)
1331 io
= SIMPLEQ_FIRST(&resc
->io
);
1333 io
= SIMPLEQ_NEXT(io
, next
);
1340 pnpbios_intr_establish(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1341 int idx
, int level
, int (*fcn
)(void *), void *arg
)
1343 struct pnp_irq
*irq
;
1346 if (idx
>= resc
->numirq
)
1349 irq
= SIMPLEQ_FIRST(&resc
->irq
);
1351 irq
= SIMPLEQ_NEXT(irq
, next
);
1353 irqnum
= ffs(irq
->mask
) - 1;
1354 type
= (irq
->flags
& 0x0c) ? IST_LEVEL
: IST_EDGE
;
1356 return (isa_intr_establish(0, irqnum
, type
, level
, fcn
, arg
));
1360 pnpbios_getirqnum(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1361 int idx
, int *irqp
, int *istp
)
1363 struct pnp_irq
*irq
;
1365 if (idx
>= resc
->numirq
)
1368 irq
= SIMPLEQ_FIRST(&resc
->irq
);
1370 irq
= SIMPLEQ_NEXT(irq
, next
);
1373 *irqp
= ffs(irq
->mask
) - 1;
1375 *istp
= (irq
->flags
& 0x0c) ? IST_LEVEL
: IST_EDGE
;
1380 pnpbios_getdmachan(pnpbios_tag_t pbt
, struct pnpresources
*resc
,
1381 int idx
, int *chanp
)
1383 struct pnp_dma
*dma
;
1385 if (idx
>= resc
->numdma
)
1388 dma
= SIMPLEQ_FIRST(&resc
->dma
);
1390 dma
= SIMPLEQ_NEXT(dma
, next
);
1392 *chanp
= ffs(dma
->mask
) - 1;
1396 #ifdef PNPBIOSEVENTS
1398 pnpbios_event_thread(void *arg
)
1400 struct pnpbios_softc
*sc
;
1406 if ((sc
->sc_control
& PNP_IC_CONTORL_EVENT_MASK
)
1407 != PNP_IC_CONTROL_EVENT_POLL
)
1411 rv
= pnpbios_sendmessage(PNP_CM_PNP_OS_ACTIVE
);
1412 EDPRINTF(("pnpbios: os active returns 0x%02x\n", rv
));
1415 config_pending_decr();
1418 while (sc
->sc_threadrun
) {
1419 /* maybe we have an event */
1421 (void)tsleep(pnpbios_event_thread
, PWAIT
,
1423 else if (((evflag
= *sc
->sc_evaddr
) & 0x01) == 0) {
1425 EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag
));
1426 (void)tsleep(pnpbios_event_thread
, PWAIT
,
1427 "pnpbiosevent", poll
);
1430 EDPRINTF(("pnpbios: evflags 0x%02x\n", evflag
));
1433 if ((rv
= pnpbios_getevent(&event
))) {
1434 EDPRINTF(("pnpbios: getevent rc: 0x%02x\n", rv
));
1436 if (rv
!= PNP_RC_EVENTS_NOT_PENDING
)
1437 printf("%s: getevent failed: %d\n",
1438 device_xname(sc
->sc_dev
), rv
);
1443 case PNP_EID_ABOUT_TO_CHANGE_CONFIG
:
1444 EDPRINTF(("pnpbios: about to change event\n"));
1446 * The system is about to be docked or undocked.
1447 * Acknowledge the event, so that the procedure
1449 * XXX When should we ever send an ABORT?
1451 pnpbios_sendmessage(PNP_RM_OK
);
1453 case PNP_EID_DOCK_CHANGED
:
1457 EDPRINTF(("pnpbios: dock changed event\n"));
1459 odocked
= pnpbios_update_dock_status(sc
);
1460 if (odocked
== sc
->sc_docked
)
1462 switch (sc
->sc_docked
) {
1464 /* We have been undocked. */
1465 /* XXX detach devices XXX */
1469 /* We have been docked. */
1470 /* XXX attach devices XXX */
1474 /* getdockinfo failed! */
1475 printf("%s: dock changed event, but unable "
1476 "to get dock info; event ignored\n",
1477 device_xname(sc
->sc_dev
));
1481 case PNP_EID_SYSTEM_DEVICE_CHANGED
:
1482 EDPRINTF(("pnpbios: system device changed event\n"));
1484 case PNP_EID_CONFIG_CHANGE_FAILED
:
1485 EDPRINTF(("pnpbios: config changed event\n"));
1487 case PNP_EID_UNKNOWN_SYSTEM_EVENT
:
1489 printf("%s: \"unknown system event\"\n",
1490 device_xname(sc
->sc_dev
));
1495 if (event
& PNP_EID_OEM_DEFINED_BIT
)
1496 printf("%s: vendor defined event 0x%04x\n",
1497 device_xname(sc
->sc_dev
), event
);
1499 printf("%s: unknown event 0x%04x\n",
1500 device_xname(sc
->sc_dev
), event
);
1506 pnpbios_sendmessage(PNP_CM_PNP_OS_INACTIVE
);
1509 #endif /* PNPBIOSEVENTS */