2 * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * PnP BIOS enumerator.
35 #include <machine/stdarg.h>
36 #include <bootstrap.h>
41 static int biospnp_init(void);
42 static void biospnp_enumerate(void);
44 struct pnphandler biospnphandler =
50 struct pnp_ICstructure
52 uint8_t pnp_signature[4];
55 uint16_t pnp_BIOScontrol;
57 uint32_t pnp_eventflag;
62 uint8_t pnp_OEMdev[4];
77 struct pnp_isaConfiguration
85 static struct pnp_ICstructure *pnp_Icheck = NULL;
86 static uint16_t pnp_NumNodes;
87 static uint16_t pnp_NodeSize;
89 static void biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
90 static int biospnp_call(int func, const char *fmt, ...);
92 #define vsegofs(vptr) (((uint32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
94 typedef void v86bios_t(uint32_t, uint32_t, uint32_t, uint32_t);
95 v86bios_t *v86bios = (v86bios_t *)v86int;
97 #define biospnp_f00(NumNodes, NodeSize) biospnp_call(0x00, "ll", NumNodes, NodeSize)
98 #define biospnp_f01(Node, devNodeBuffer, Control) biospnp_call(0x01, "llw", Node, devNodeBuffer, Control)
99 #define biospnp_f40(Configuration) biospnp_call(0x40, "l", Configuration)
101 /* PnP BIOS return codes */
102 #define PNP_SUCCESS 0x00
103 #define PNP_FUNCTION_NOT_SUPPORTED 0x80
106 * Initialisation: locate the PnP BIOS, test that we can call it.
107 * Returns nonzero if the PnP BIOS is not usable on this system.
112 struct pnp_isaConfiguration icfg;
116 /* Search for the $PnP signature */
118 for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
119 if (!bcmp(sigptr, "$PnP", 4)) {
120 pnp_Icheck = (struct pnp_ICstructure *)sigptr;
124 /* No signature, no BIOS */
125 if (pnp_Icheck == NULL)
129 * Fetch the system table parameters as a test of the BIOS
131 result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
132 if (result != PNP_SUCCESS) {
137 * Look for the PnP ISA configuration table
139 result = biospnp_f40(vsegofs(&icfg));
142 /* If the BIOS found some PnP devices, take its hint for the read port */
143 if ((icfg.ic_revision == 1) && (icfg.ic_nCSN > 0))
144 isapnp_readport = icfg.ic_rdport;
146 case PNP_FUNCTION_NOT_SUPPORTED:
147 /* The BIOS says there is no ISA bus (should we trust that this works?) */
148 printf("PnP BIOS claims no ISA bus\n");
149 isapnp_readport = -1;
156 biospnp_enumerate(void)
159 struct pnp_devNode *devNodeBuffer;
164 /* Init/check state */
168 devNodeBuffer = (struct pnp_devNode *)alloca(pnp_NodeSize);
171 while((Node != 0xff) && (count-- > 0)) {
172 result = biospnp_f01(vsegofs(&Node), vsegofs(devNodeBuffer), 0x1);
173 if (result != PNP_SUCCESS) {
174 printf("PnP BIOS node %d: error 0x%x\n", Node, result);
176 pi = pnp_allocinfo();
177 pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
178 biospnp_scanresdata(pi, devNodeBuffer);
185 * Scan the resource data in the node's data area for compatible device IDs
189 biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
191 u_int tag, i, rlen, dlen;
195 p = dn->dn_data; /* point to resource data */
196 dlen = dn->dn_size - (p - (uint8_t *)dn); /* length of resource data */
198 for (i = 0; i < dlen; i+= rlen) {
201 if (PNP_RES_TYPE(tag) == 0) {
202 rlen = PNP_SRES_LEN(tag);
204 switch (PNP_SRES_NUM(tag)) {
207 /* got a compatible device ID */
208 pnp_addident(pi, pnp_eisaformat(p + i));
216 rlen = *(uint16_t *)(p + i);
217 i += sizeof(uint16_t);
219 switch(PNP_LRES_NUM(tag)) {
222 str = malloc(rlen + 1);
223 bcopy(p + i, str, rlen);
225 if (pi->pi_desc == NULL) {
238 * Make a 16-bit realmode PnP BIOS call.
240 * The first argument passed is the function number, the last is the
241 * BIOS data segment selector. Intermediate arguments may be 16 or
242 * 32 bytes in length, and are described by the format string.
244 * Arguments to the BIOS functions must be packed on the stack, hence
248 biospnp_call(int func, const char *fmt, ...)
256 /* function number first */
257 argp = (uint8_t *)args;
258 *(uint16_t *)argp = func;
259 argp += sizeof(uint16_t);
261 /* take args according to format */
263 for (p = fmt; *p != 0; p++) {
267 i = va_arg(ap, u_int);
268 *(uint16_t *)argp = i;
269 argp += sizeof(uint16_t);
273 i = va_arg(ap, uint32_t);
274 *(uint32_t *)argp = i;
275 argp += sizeof(uint32_t);
281 /* BIOS segment last */
282 *(uint16_t *)argp = pnp_Icheck->pnp_rmds;
283 argp += sizeof(uint16_t);
285 /* prepare for call */
286 v86.ctl = V86_ADDR | V86_CALLF;
287 v86.addr = ((uint32_t)pnp_Icheck->pnp_rmcs << 16) + pnp_Icheck->pnp_rmip;
289 /* call with packed stack and return */
290 v86bios(args[0], args[1], args[2], args[3]);
291 return(v86.eax & 0xffff);