f78fa69de57dea48fa696c5605e807a38236d6cc
[dragonfly.git] / sys / boot / pc32 / libi386 / biospnp.c
1 /*-
2  * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/boot/i386/libi386/biospnp.c,v 1.9 2003/08/25 23:28:31 obrien Exp $
27  * $DragonFly: src/sys/boot/pc32/libi386/biospnp.c,v 1.4 2003/11/10 06:08:36 dillon Exp $
28  */
29
30 /*
31  * PnP BIOS enumerator.
32  */
33
34 #include <stand.h>
35 #include <machine/stdarg.h>
36 #include <bootstrap.h>
37 #include <isapnp.h>
38 #include <btxv86.h>
39
40 #define __packed __attribute__((__packed__))
41
42 static int      biospnp_init(void);
43 static void     biospnp_enumerate(void);
44
45 struct pnphandler biospnphandler =
46 {
47     "PnP BIOS",
48     biospnp_enumerate
49 };
50
51 struct pnp_ICstructure
52 {
53     u_int8_t    pnp_signature[4]         __packed;
54     u_int8_t    pnp_version              __packed;
55     u_int8_t    pnp_length               __packed;
56     u_int16_t   pnp_BIOScontrol          __packed;
57     u_int8_t    pnp_checksum             __packed;
58     u_int32_t   pnp_eventflag            __packed;
59     u_int16_t   pnp_rmip                 __packed;
60     u_int16_t   pnp_rmcs                 __packed;
61     u_int16_t   pnp_pmip                 __packed;
62     u_int32_t   pnp_pmcs                 __packed;
63     u_int8_t    pnp_OEMdev[4]            __packed;
64     u_int16_t   pnp_rmds                 __packed;
65     u_int32_t   pnp_pmds                 __packed;
66 };
67
68 struct pnp_devNode 
69 {
70     u_int16_t   dn_size         __packed;
71     u_int8_t    dn_handle       __packed;
72     u_int8_t    dn_id[4]        __packed;
73     u_int8_t    dn_type[3]      __packed;
74     u_int16_t   dn_attrib       __packed;
75     u_int8_t    dn_data[1]      __packed;
76 };
77
78 struct pnp_isaConfiguration
79 {
80     u_int8_t    ic_revision     __packed;
81     u_int8_t    ic_nCSN         __packed;
82     u_int16_t   ic_rdport       __packed;
83     u_int16_t   ic_reserved     __packed;
84 };
85
86 static struct pnp_ICstructure   *pnp_Icheck = NULL;
87 static u_int16_t                pnp_NumNodes;
88 static u_int16_t                pnp_NodeSize;
89
90 static void     biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
91 static int      biospnp_call(int func, const char *fmt, ...);
92
93 #define vsegofs(vptr)   (((u_int32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
94
95 typedef void    v86bios_t(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
96 v86bios_t       *v86bios = (v86bios_t *)v86int;
97
98 #define biospnp_f00(NumNodes, NodeSize)                 biospnp_call(0x00, "ll", NumNodes, NodeSize)
99 #define biospnp_f01(Node, devNodeBuffer, Control)       biospnp_call(0x01, "llw", Node, devNodeBuffer, Control)
100 #define biospnp_f40(Configuration)                      biospnp_call(0x40, "l", Configuration)
101
102 /* PnP BIOS return codes */
103 #define PNP_SUCCESS                     0x00
104 #define PNP_FUNCTION_NOT_SUPPORTED      0x80
105
106 /*
107  * Initialisation: locate the PnP BIOS, test that we can call it.
108  * Returns nonzero if the PnP BIOS is not usable on this system.
109  */
110 static int
111 biospnp_init(void)
112 {
113     struct pnp_isaConfiguration icfg;
114     char                        *sigptr;
115     int                         result;
116     
117     /* Search for the $PnP signature */
118     pnp_Icheck = NULL;
119     for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
120         if (!bcmp(sigptr, "$PnP", 4)) {
121             pnp_Icheck = (struct pnp_ICstructure *)sigptr;
122             break;
123         }
124         
125     /* No signature, no BIOS */
126     if (pnp_Icheck == NULL)
127         return(1);
128
129     /*
130      * Fetch the system table parameters as a test of the BIOS
131      */
132     result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
133     if (result != PNP_SUCCESS) {
134         return(1);
135     }
136
137     /*
138      * Look for the PnP ISA configuration table 
139      */
140     result = biospnp_f40(vsegofs(&icfg));
141     switch (result) {
142     case PNP_SUCCESS:
143         /* If the BIOS found some PnP devices, take its hint for the read port */
144         if ((icfg.ic_revision == 1) && (icfg.ic_nCSN > 0))
145             isapnp_readport = icfg.ic_rdport;
146         break;
147     case PNP_FUNCTION_NOT_SUPPORTED:
148         /* The BIOS says there is no ISA bus (should we trust that this works?) */
149         printf("PnP BIOS claims no ISA bus\n");
150         isapnp_readport = -1;
151         break;
152     }
153     return(0);
154 }
155
156 static void
157 biospnp_enumerate(void)
158 {
159     u_int8_t            Node;
160     struct pnp_devNode  *devNodeBuffer;
161     int                 result;
162     struct pnpinfo      *pi;
163     int                 count;
164
165     /* Init/check state */
166     if (biospnp_init())
167         return;
168
169     devNodeBuffer = (struct pnp_devNode *)malloc(pnp_NodeSize);
170     Node = 0;
171     count = 1000;
172     while((Node != 0xff) && (count-- > 0)) {
173         result = biospnp_f01(vsegofs(&Node), vsegofs(devNodeBuffer), 0x1);
174         if (result != PNP_SUCCESS) {
175             printf("PnP BIOS node %d: error 0x%x\n", Node, result);
176         } else {
177             pi = pnp_allocinfo();
178             pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
179             biospnp_scanresdata(pi, devNodeBuffer);
180             pnp_addinfo(pi);
181         }
182     }
183 }
184
185 /*
186  * Scan the resource data in the node's data area for compatible device IDs
187  * and descriptions.
188  */
189 static void
190 biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
191 {
192     u_int       tag, i, rlen, dlen;
193     u_int8_t    *p;
194     char        *str;
195
196     p = dn->dn_data;                    /* point to resource data */
197     dlen = dn->dn_size - (p - (u_int8_t *)dn);  /* length of resource data */
198
199     for (i = 0; i < dlen; i+= rlen) {
200         tag = p[i];
201         i++;
202         if (PNP_RES_TYPE(tag) == 0) {
203             rlen = PNP_SRES_LEN(tag);
204             /* small resource */
205             switch (PNP_SRES_NUM(tag)) {
206
207             case COMP_DEVICE_ID:
208                 /* got a compatible device ID */
209                 pnp_addident(pi, pnp_eisaformat(p + i));
210                 break;
211                 
212             case END_TAG:
213                 return;
214             }
215         } else {
216             /* large resource */
217             rlen = *(u_int16_t *)(p + i);
218             i += sizeof(u_int16_t);
219             
220             switch(PNP_LRES_NUM(tag)) {
221
222             case ID_STRING_ANSI:
223                 str = malloc(rlen + 1);
224                 bcopy(p + i, str, rlen);
225                 str[rlen] = 0;
226                 if (pi->pi_desc == NULL) {
227                     pi->pi_desc = str;
228                 } else {
229                     free(str);
230                 }
231                 break;
232             }
233         }
234     }
235 }
236
237
238 /*
239  * Make a 16-bit realmode PnP BIOS call.
240  *
241  * The first argument passed is the function number, the last is the
242  * BIOS data segment selector.  Intermediate arguments may be 16 or 
243  * 32 bytes in length, and are described by the format string.
244  *
245  * Arguments to the BIOS functions must be packed on the stack, hence
246  * this evil.
247  */
248 static int
249 biospnp_call(int func, const char *fmt, ...)
250 {
251     __va_list   ap;
252     const char  *p;
253     u_int8_t    *argp;
254     u_int32_t   args[4];
255     u_int32_t   i;
256
257     /* function number first */
258     argp = (u_int8_t *)args;
259     *(u_int16_t *)argp = func;
260     argp += sizeof(u_int16_t);
261
262     /* take args according to format */
263     __va_start(ap, fmt);
264     for (p = fmt; *p != 0; p++) {
265         switch(*p) {
266
267         case 'w':
268             i = __va_arg(ap, u_int);
269             *(u_int16_t *)argp = i;
270             argp += sizeof(u_int16_t);
271             break;
272             
273         case 'l':
274             i = __va_arg(ap, u_int32_t);
275             *(u_int32_t *)argp = i;
276             argp += sizeof(u_int32_t);
277             break;
278         }
279     }
280
281     /* BIOS segment last */
282     *(u_int16_t *)argp = pnp_Icheck->pnp_rmds;
283     argp += sizeof(u_int16_t);
284
285     /* prepare for call */
286     v86.ctl = V86_ADDR | V86_CALLF; 
287     v86.addr = ((u_int32_t)pnp_Icheck->pnp_rmcs << 16) + pnp_Icheck->pnp_rmip;
288     
289     /* call with packed stack and return */
290     v86bios(args[0], args[1], args[2], args[3]);
291     return(v86.eax & 0xffff);
292 }