1 /*- 2 * Copyright (c) 2003 Matthew N. Dodd <winter@jurai.net> 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$ 27 */ 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/kernel.h> 32 33 #include <sys/module.h> 34 #include <sys/bus.h> 35 #include <sys/conf.h> 36 37 #include <machine/bus.h> 38 #include <machine/resource.h> 39 #include <sys/rman.h> 40 41 /* And all this for BIOS_PADDRTOVADDR() */ 42 #include <vm/vm.h> 43 #include <vm/pmap.h> 44 #include <machine/md_var.h> 45 #include <machine/pc/bios.h> 46 47 #include <machine/smapi.h> 48 49 #define SMAPI_START 0xf0000 50 #define SMAPI_STEP 0x10 51 #define SMAPI_OFF 0 52 #define SMAPI_LEN 4 53 #define SMAPI_SIG "$SMB" 54 55 #define RES2HDR(res) ((struct smapi_bios_header *)rman_get_virtual(res)) 56 #define ADDR2HDR(addr) ((struct smapi_bios_header *)BIOS_PADDRTOVADDR(addr)) 57 58 struct smapi_softc { 59 dev_t cdev; 60 device_t dev; 61 struct resource * res; 62 int rid; 63 64 u_int32_t smapi32_entry; 65 66 struct smapi_bios_header *header; 67 }; 68 69 u_long smapi32_offset; 70 u_short smapi32_segment; 71 #define SMAPI32_SEGMENT 0x18 72 73 devclass_t smapi_devclass; 74 75 static d_ioctl_t smapi_ioctl; 76 77 static struct cdevsw smapi_cdevsw = { 78 .d_open = nullopen, 79 .d_close = nullclose, 80 .d_ioctl = smapi_ioctl, 81 .d_name = "smapi", 82 .d_maj = MAJOR_AUTO, 83 .d_flags = D_MEM, 84 }; 85 86 static void smapi_identify (driver_t *, device_t); 87 static int smapi_probe (device_t); 88 static int smapi_attach (device_t); 89 static int smapi_detach (device_t); 90 static int smapi_modevent (module_t, int, void *); 91 92 static int smapi_header_cksum (struct smapi_bios_header *); 93 94 extern int smapi32 (struct smapi_bios_parameter *, 95 struct smapi_bios_parameter *); 96 extern int smapi32_new (u_long, u_short, 97 struct smapi_bios_parameter *, 98 struct smapi_bios_parameter *); 99 100 static int 101 smapi_ioctl (dev, cmd, data, fflag, td) 102 dev_t dev; 103 u_long cmd; 104 caddr_t data; 105 int fflag; 106 d_thread_t * td; 107 { 108 struct smapi_softc *sc; 109 int error; 110 111 error = 0; 112 sc = devclass_get_softc(smapi_devclass, minor(dev)); 113 if (sc == NULL) { 114 error = ENXIO; 115 goto fail; 116 } 117 118 switch (cmd) { 119 case SMAPIOGHEADER: 120 bcopy((caddr_t)sc->header, data, 121 sizeof(struct smapi_bios_header)); 122 error = 0; 123 break; 124 case SMAPIOCGFUNCTION: 125 #if 1 126 smapi32_segment = SMAPI32_SEGMENT; 127 smapi32_offset = sc->smapi32_entry; 128 error = smapi32( 129 #else 130 error = smapi32_new(sc->smapi32_entry, SMAPI32_SEGMENT, 131 #endif 132 (struct smapi_bios_parameter *)data, 133 (struct smapi_bios_parameter *)data); 134 break; 135 default: 136 error = ENOTTY; 137 } 138 139 fail: 140 return (error); 141 } 142 143 static int 144 smapi_header_cksum (struct smapi_bios_header *header) 145 { 146 u_int8_t *ptr; 147 u_int8_t cksum; 148 int i; 149 150 ptr = (u_int8_t *)header; 151 cksum = 0; 152 for (i = 0; i < header->length; i++) { 153 cksum += ptr[i]; 154 } 155 156 return (cksum); 157 } 158 159 static void 160 smapi_identify (driver_t *driver, device_t parent) 161 { 162 device_t child; 163 u_int32_t addr; 164 int length; 165 int rid; 166 167 if (!device_is_alive(parent)) 168 return; 169 170 addr = bios_sigsearch(SMAPI_START, SMAPI_SIG, SMAPI_LEN, 171 SMAPI_STEP, SMAPI_OFF); 172 if (addr != 0) { 173 rid = 0; 174 length = ADDR2HDR(addr)->length; 175 176 child = BUS_ADD_CHILD(parent, 0, "smapi", -1); 177 device_set_driver(child, driver); 178 bus_set_resource(child, SYS_RES_MEMORY, rid, addr, length); 179 device_set_desc(child, "SMAPI BIOS"); 180 } 181 182 return; 183 } 184 185 static int 186 smapi_probe (device_t dev) 187 { 188 struct resource *res; 189 int rid; 190 int error; 191 192 error = 0; 193 rid = 0; 194 res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid, 195 0ul, ~0ul, 1, RF_ACTIVE); 196 if (res == NULL) { 197 device_printf(dev, "Unable to allocate memory resource.\n"); 198 error = ENOMEM; 199 goto bad; 200 } 201 202 if (smapi_header_cksum(RES2HDR(res))) { 203 device_printf(dev, "SMAPI header checksum failed.\n"); 204 error = ENXIO; 205 goto bad; 206 } 207 208 bad: 209 if (res) 210 bus_release_resource(dev, SYS_RES_MEMORY, rid, res); 211 return (error); 212 } 213 214 static int 215 smapi_attach (device_t dev) 216 { 217 struct smapi_softc *sc; 218 int error; 219 220 sc = device_get_softc(dev); 221 error = 0; 222 223 sc->dev = dev; 224 sc->rid = 0; 225 sc->res = bus_alloc_resource(dev, SYS_RES_MEMORY, &sc->rid, 226 0ul, ~0ul, 1, RF_ACTIVE); 227 if (sc->res == NULL) { 228 device_printf(dev, "Unable to allocate memory resource.\n"); 229 error = ENOMEM; 230 goto bad; 231 } 232 sc->header = (struct smapi_bios_header *)rman_get_virtual(sc->res); 233 sc->smapi32_entry = (u_int32_t)BIOS_PADDRTOVADDR( 234 sc->header->prot32_segment + 235 sc->header->prot32_offset); 236 237 sc->cdev = make_dev(&smapi_cdevsw, 238 device_get_unit(sc->dev), 239 UID_ROOT, GID_WHEEL, 0600, 240 "%s%d", 241 smapi_cdevsw.d_name, 242 device_get_unit(sc->dev)); 243 244 device_printf(dev, "Version: %d.%02d, Length: %d, Checksum: 0x%02x\n", 245 bcd2bin(sc->header->version_major), 246 bcd2bin(sc->header->version_minor), 247 sc->header->length, 248 sc->header->checksum); 249 device_printf(dev, "Information=0x%b\n", 250 sc->header->information, 251 "\020" 252 "\001REAL_VM86" 253 "\002PROTECTED_16" 254 "\003PROTECTED_32"); 255 256 if (bootverbose) { 257 if (sc->header->information & SMAPI_REAL_VM86) 258 device_printf(dev, "Real/VM86 mode: Segment 0x%04x, Offset 0x%04x\n", 259 sc->header->real16_segment, 260 sc->header->real16_offset); 261 if (sc->header->information & SMAPI_PROT_16BIT) 262 device_printf(dev, "16-bit Protected mode: Segment 0x%08x, Offset 0x%04x\n", 263 sc->header->prot16_segment, 264 sc->header->prot16_offset); 265 if (sc->header->information & SMAPI_PROT_32BIT) 266 device_printf(dev, "32-bit Protected mode: Segment 0x%08x, Offset 0x%08x\n", 267 sc->header->prot32_segment, 268 sc->header->prot32_offset); 269 } 270 271 return (0); 272 bad: 273 if (sc->res) 274 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 275 return (error); 276 } 277 278 static int 279 smapi_detach (device_t dev) 280 { 281 struct smapi_softc *sc; 282 283 sc = device_get_softc(dev); 284 285 destroy_dev(sc->cdev); 286 287 if (sc->res) 288 bus_release_resource(dev, SYS_RES_MEMORY, sc->rid, sc->res); 289 290 return (0); 291 } 292 293 static int 294 smapi_modevent (mod, what, arg) 295 module_t mod; 296 int what; 297 void * arg; 298 { 299 device_t * devs; 300 int count; 301 int i; 302 303 switch (what) { 304 case MOD_LOAD: 305 break; 306 case MOD_UNLOAD: 307 devclass_get_devices(smapi_devclass, &devs, &count); 308 for (i = 0; i < count; i++) { 309 device_delete_child(device_get_parent(devs[i]), devs[i]); 310 } 311 break; 312 default: 313 break; 314 } 315 316 return (0); 317 } 318 319 static device_method_t smapi_methods[] = { 320 /* Device interface */ 321 DEVMETHOD(device_identify, smapi_identify), 322 DEVMETHOD(device_probe, smapi_probe), 323 DEVMETHOD(device_attach, smapi_attach), 324 DEVMETHOD(device_detach, smapi_detach), 325 { 0, 0 } 326 }; 327 328 static driver_t smapi_driver = { 329 "smapi", 330 smapi_methods, 331 sizeof(struct smapi_softc), 332 }; 333 334 DRIVER_MODULE(smapi, nexus, smapi_driver, smapi_devclass, smapi_modevent, 0); 335 MODULE_VERSION(smapi, 1); 336