2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/module.h>
41 #include <machine/bus.h>
42 #include <machine/resource.h>
45 /* And all this for BIOS_PADDRTOVADDR() */
47 #include <vm/vm_param.h>
49 #include <machine/md_var.h>
50 #include <machine/pc/bios.h>
52 #include <machine/smapi.h>
54 #define SMAPI_START 0xf0000
55 #define SMAPI_STEP 0x10
58 #define SMAPI_SIG "$SMB"
60 #define RES2HDR(res) ((struct smapi_bios_header *)rman_get_virtual(res))
61 #define ADDR2HDR(addr) ((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr))
66 struct resource * res;
69 u_int32_t smapi32_entry;
71 struct smapi_bios_header *header;
74 extern u_long smapi32_offset;
75 extern u_short smapi32_segment;
77 devclass_t smapi_devclass;
79 static d_ioctl_t smapi_ioctl;
81 static struct cdevsw smapi_cdevsw = {
82 .d_version = D_VERSION,
83 .d_ioctl = smapi_ioctl,
85 .d_flags = D_NEEDGIANT,
88 static void smapi_identify(driver_t *, device_t);
89 static int smapi_probe(device_t);
90 static int smapi_attach(device_t);
91 static int smapi_detach(device_t);
92 static int smapi_modevent(module_t, int, void *);
94 static int smapi_header_cksum(struct smapi_bios_header *);
96 extern int smapi32(struct smapi_bios_parameter *,
97 struct smapi_bios_parameter *);
98 extern int smapi32_new(u_long, u_short,
99 struct smapi_bios_parameter *,
100 struct smapi_bios_parameter *);
103 smapi_ioctl (struct cdev *dev, u_long cmd, caddr_t data, int fflag, struct thread *td)
105 struct smapi_softc *sc;
109 sc = devclass_get_softc(smapi_devclass, dev2unit(dev));
117 bcopy((caddr_t)sc->header, data,
118 sizeof(struct smapi_bios_header));
121 case SMAPIOCGFUNCTION:
122 smapi32_offset = sc->smapi32_entry;
123 error = smapi32((struct smapi_bios_parameter *)data,
124 (struct smapi_bios_parameter *)data);
135 smapi_header_cksum (struct smapi_bios_header *header)
141 ptr = (u_int8_t *)header;
143 for (i = 0; i < header->length; i++) {
151 smapi_identify (driver_t *driver, device_t parent)
158 if (!device_is_alive(parent))
161 addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN,
162 SMAPI_STEP, SMAPI_OFF);
165 length = ADDR2HDR(addr)->length;
167 child = BUS_ADD_CHILD(parent, 5, "smapi", -1);
168 device_set_driver(child, driver);
169 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length);
170 device_set_desc(child, "SMAPI BIOS");
177 smapi_probe (device_t dev)
179 struct resource *res;
185 res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
187 device_printf(dev, "Unable to allocate memory resource.\n");
192 if (smapi_header_cksum(RES2HDR(res))) {
193 device_printf(dev, "SMAPI header checksum failed.\n");
200 bus_release_resource(dev, SYS_RES_MEMORY, rid, res);
205 smapi_attach (device_t dev)
207 struct smapi_softc *sc;
210 sc = device_get_softc(dev);
215 sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->rid,
217 if (sc->res == NULL) {
218 device_printf(dev, "Unable to allocate memory resource.\n");
222 sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res);
223 sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR(
224 sc->header->prot32_segment +
225 sc->header->prot32_offset);
227 sc->cdev = make_dev(&smapi_cdevsw,
228 device_get_unit(sc->dev),
229 UID_ROOT, GID_WHEEL, 0600,
232 device_get_unit(sc->dev));
234 device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n",
235 bcd2bin(sc->header->version_major),
236 bcd2bin(sc->header->version_minor),
238 sc->header->checksum);
239 device_printf(dev, "Information=0x%b\n",
240 sc->header->information,
247 if (sc->header->information & SMAPI_REAL_VM86)
248 device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n",
249 sc->header->real16_segment,
250 sc->header->real16_offset);
251 if (sc->header->information & SMAPI_PROT_16BIT)
252 device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n",
253 sc->header->prot16_segment,
254 sc->header->prot16_offset);
255 if (sc->header->information & SMAPI_PROT_32BIT)
256 device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n",
257 sc->header->prot32_segment,
258 sc->header->prot32_offset);
264 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
269 smapi_detach (device_t dev)
271 struct smapi_softc *sc;
273 sc = device_get_softc(dev);
275 destroy_dev(sc->cdev);
278 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res);
284 smapi_modevent (module_t mod, int what, void *arg)
294 devclass_get_devices(smapi_devclass, &devs, &count);
295 for (i = 0; i < count; i++) {
296 device_delete_child(device_get_parent(devs[i]), devs[i]);
307 static device_method_t smapi_methods[] = {
308 /* Device interface */
309 DEVMETHOD(device_identify, smapi_identify),
310 DEVMETHOD(device_probe, smapi_probe),
311 DEVMETHOD(device_attach, smapi_attach),
312 DEVMETHOD(device_detach, smapi_detach),
316 static driver_t smapi_driver = {
319 sizeof(struct smapi_softc),
322 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0);
323 MODULE_VERSION(smapi, 1);