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