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
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
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
41static int	biospnp_init(void);
42static void	biospnp_enumerate(void);
43
44struct pnphandler biospnphandler =
45{
46    "PnP BIOS",
47    biospnp_enumerate
48};
49
50struct pnp_ICstructure
51{
52    u_int8_t	pnp_signature[4];
53    u_int8_t	pnp_version;
54    u_int8_t	pnp_length;
55    u_int16_t	pnp_BIOScontrol;
56    u_int8_t	pnp_checksum;
57    u_int32_t	pnp_eventflag;
58    u_int16_t	pnp_rmip;
59    u_int16_t	pnp_rmcs;
60    u_int16_t	pnp_pmip;
61    u_int32_t	pnp_pmcs;
62    u_int8_t	pnp_OEMdev[4];
63    u_int16_t	pnp_rmds;
64    u_int32_t	pnp_pmds;
65} __packed;
66
67struct pnp_devNode
68{
69    u_int16_t	dn_size;
70    u_int8_t	dn_handle;
71    u_int8_t	dn_id[4];
72    u_int8_t	dn_type[3];
73    u_int16_t	dn_attrib;
74    u_int8_t	dn_data[1];
75} __packed;
76
77struct pnp_isaConfiguration
78{
79    u_int8_t	ic_revision;
80    u_int8_t	ic_nCSN;
81    u_int16_t	ic_rdport;
82    u_int16_t	ic_reserved;
83} __packed;
84
85static struct pnp_ICstructure	*pnp_Icheck = NULL;
86static u_int16_t		pnp_NumNodes;
87static u_int16_t		pnp_NodeSize;
88
89static void	biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
90static int	biospnp_call(int func, const char *fmt, ...);
91
92#define vsegofs(vptr)	(((u_int32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
93
94typedef void    v86bios_t(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
95v86bios_t	*v86bios = (v86bios_t *)v86int;
96
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)
100
101/* PnP BIOS return codes */
102#define PNP_SUCCESS			0x00
103#define PNP_FUNCTION_NOT_SUPPORTED	0x80
104
105/*
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.
108 */
109static int
110biospnp_init(void)
111{
112    struct pnp_isaConfiguration	icfg;
113    char			*sigptr;
114    int				result;
115
116    /* Search for the $PnP signature */
117    pnp_Icheck = NULL;
118    for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
119	if (!bcmp(sigptr, "$PnP", 4)) {
120	    pnp_Icheck = (struct pnp_ICstructure *)sigptr;
121	    break;
122	}
123
124    /* No signature, no BIOS */
125    if (pnp_Icheck == NULL)
126	return(1);
127
128    /*
129     * Fetch the system table parameters as a test of the BIOS
130     */
131    result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
132    if (result != PNP_SUCCESS) {
133	return(1);
134    }
135
136    /*
137     * Look for the PnP ISA configuration table
138     */
139    result = biospnp_f40(vsegofs(&icfg));
140    switch (result) {
141    case PNP_SUCCESS:
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;
145	break;
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;
150	break;
151    }
152    return(0);
153}
154
155static void
156biospnp_enumerate(void)
157{
158    u_int8_t		Node;
159    struct pnp_devNode	*devNodeBuffer;
160    int			result;
161    struct pnpinfo	*pi;
162    int			count;
163
164    /* Init/check state */
165    if (biospnp_init())
166	return;
167
168    devNodeBuffer = (struct pnp_devNode *)alloca(pnp_NodeSize);
169    Node = 0;
170    count = 1000;
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);
175	} else {
176	    pi = pnp_allocinfo();
177	    pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
178	    biospnp_scanresdata(pi, devNodeBuffer);
179	    pnp_addinfo(pi);
180	}
181    }
182}
183
184/*
185 * Scan the resource data in the node's data area for compatible device IDs
186 * and descriptions.
187 */
188static void
189biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
190{
191    u_int	tag, i, rlen, dlen;
192    u_int8_t	*p;
193    char	*str;
194
195    p = dn->dn_data;			/* point to resource data */
196    dlen = dn->dn_size - (p - (u_int8_t *)dn);	/* length of resource data */
197
198    for (i = 0; i < dlen; i+= rlen) {
199	tag = p[i];
200	i++;
201	if (PNP_RES_TYPE(tag) == 0) {
202	    rlen = PNP_SRES_LEN(tag);
203	    /* small resource */
204	    switch (PNP_SRES_NUM(tag)) {
205
206	    case COMP_DEVICE_ID:
207		/* got a compatible device ID */
208		pnp_addident(pi, pnp_eisaformat(p + i));
209		break;
210
211	    case END_TAG:
212		return;
213	    }
214	} else {
215	    /* large resource */
216	    rlen = *(u_int16_t *)(p + i);
217	    i += sizeof(u_int16_t);
218
219	    switch(PNP_LRES_NUM(tag)) {
220
221	    case ID_STRING_ANSI:
222		str = malloc(rlen + 1);
223		bcopy(p + i, str, rlen);
224		str[rlen] = 0;
225		if (pi->pi_desc == NULL) {
226		    pi->pi_desc = str;
227		} else {
228		    free(str);
229		}
230		break;
231	    }
232	}
233    }
234}
235
236
237/*
238 * Make a 16-bit realmode PnP BIOS call.
239 *
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.
243 *
244 * Arguments to the BIOS functions must be packed on the stack, hence
245 * this evil.
246 */
247static int
248biospnp_call(int func, const char *fmt, ...)
249{
250    va_list	ap;
251    const char	*p;
252    u_int8_t	*argp;
253    u_int32_t	args[4];
254    u_int32_t	i;
255
256    /* function number first */
257    argp = (u_int8_t *)args;
258    *(u_int16_t *)argp = func;
259    argp += sizeof(u_int16_t);
260
261    /* take args according to format */
262    va_start(ap, fmt);
263    for (p = fmt; *p != 0; p++) {
264	switch(*p) {
265
266	case 'w':
267	    i = va_arg(ap, u_int);
268	    *(u_int16_t *)argp = i;
269	    argp += sizeof(u_int16_t);
270	    break;
271
272	case 'l':
273	    i = va_arg(ap, u_int32_t);
274	    *(u_int32_t *)argp = i;
275	    argp += sizeof(u_int32_t);
276	    break;
277	}
278    }
279    va_end(ap);
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}
293