1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp> 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer as 12 * the first lines of this file unmodified. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include "opt_vga.h" 33 #include "opt_fb.h" 34 #include "opt_syscons.h" /* should be removed in the future, XXX */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/malloc.h> 40 #include <sys/module.h> 41 #include <sys/conf.h> 42 #include <sys/bus.h> 43 #include <sys/fbio.h> 44 45 #include <machine/bus.h> 46 #include <machine/resource.h> 47 48 #include <sys/rman.h> 49 50 #include <vm/vm.h> 51 #include <vm/pmap.h> 52 53 #include <machine/md_var.h> 54 #ifdef __i386__ 55 #include <machine/pc/bios.h> 56 #endif 57 58 #include <dev/fb/fbreg.h> 59 #include <dev/fb/vgareg.h> 60 61 #include <isa/isareg.h> 62 #include <isa/isavar.h> 63 64 static void 65 vga_suspend(device_t dev) 66 { 67 vga_softc_t *sc; 68 int nbytes; 69 70 sc = device_get_softc(dev); 71 72 /* Save the video state across the suspend. */ 73 if (sc->state_buf != NULL) 74 goto save_palette; 75 nbytes = vidd_save_state(sc->adp, NULL, 0); 76 if (nbytes <= 0) 77 goto save_palette; 78 sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT); 79 if (sc->state_buf == NULL) 80 goto save_palette; 81 if (bootverbose) 82 device_printf(dev, "saving %d bytes of video state\n", nbytes); 83 if (vidd_save_state(sc->adp, sc->state_buf, nbytes) != 0) { 84 device_printf(dev, "failed to save state (nbytes=%d)\n", 85 nbytes); 86 free(sc->state_buf, M_TEMP); 87 sc->state_buf = NULL; 88 } 89 90 save_palette: 91 /* Save the color palette across the suspend. */ 92 if (sc->pal_buf != NULL) 93 return; 94 sc->pal_buf = malloc(256 * 3, M_TEMP, M_NOWAIT); 95 if (sc->pal_buf == NULL) 96 return; 97 if (bootverbose) 98 device_printf(dev, "saving color palette\n"); 99 if (vidd_save_palette(sc->adp, sc->pal_buf) != 0) { 100 device_printf(dev, "failed to save palette\n"); 101 free(sc->pal_buf, M_TEMP); 102 sc->pal_buf = NULL; 103 } 104 } 105 106 static void 107 vga_resume(device_t dev) 108 { 109 vga_softc_t *sc; 110 111 sc = device_get_softc(dev); 112 113 if (sc->state_buf != NULL) { 114 if (vidd_load_state(sc->adp, sc->state_buf) != 0) 115 device_printf(dev, "failed to reload state\n"); 116 free(sc->state_buf, M_TEMP); 117 sc->state_buf = NULL; 118 } 119 if (sc->pal_buf != NULL) { 120 if (vidd_load_palette(sc->adp, sc->pal_buf) != 0) 121 device_printf(dev, "failed to reload palette\n"); 122 free(sc->pal_buf, M_TEMP); 123 sc->pal_buf = NULL; 124 } 125 } 126 127 #define VGA_SOFTC(unit) \ 128 ((vga_softc_t *)devclass_get_softc(isavga_devclass, unit)) 129 130 static devclass_t isavga_devclass; 131 132 #ifdef FB_INSTALL_CDEV 133 134 static d_open_t isavga_open; 135 static d_close_t isavga_close; 136 static d_read_t isavga_read; 137 static d_write_t isavga_write; 138 static d_ioctl_t isavga_ioctl; 139 static d_mmap_t isavga_mmap; 140 141 static struct cdevsw isavga_cdevsw = { 142 .d_version = D_VERSION, 143 .d_flags = D_NEEDGIANT, 144 .d_open = isavga_open, 145 .d_close = isavga_close, 146 .d_read = isavga_read, 147 .d_write = isavga_write, 148 .d_ioctl = isavga_ioctl, 149 .d_mmap = isavga_mmap, 150 .d_name = VGA_DRIVER_NAME, 151 }; 152 153 #endif /* FB_INSTALL_CDEV */ 154 155 static void 156 isavga_identify(driver_t *driver, device_t parent) 157 { 158 BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0); 159 } 160 161 static int 162 isavga_probe(device_t dev) 163 { 164 video_adapter_t adp; 165 int error; 166 167 /* No pnp support */ 168 if (isa_get_vendorid(dev)) 169 return (ENXIO); 170 171 error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev)); 172 if (error == 0) { 173 device_set_desc(dev, "Generic ISA VGA"); 174 bus_set_resource(dev, SYS_RES_IOPORT, 0, 175 adp.va_io_base, adp.va_io_size); 176 bus_set_resource(dev, SYS_RES_MEMORY, 0, 177 adp.va_mem_base, adp.va_mem_size); 178 isa_set_vendorid(dev, PNP_EISAID("PNP0900")); 179 #if 0 180 isa_set_port(dev, adp.va_io_base); 181 isa_set_portsize(dev, adp.va_io_size); 182 isa_set_maddr(dev, adp.va_mem_base); 183 isa_set_msize(dev, adp.va_mem_size); 184 #endif 185 } 186 return (error); 187 } 188 189 static int 190 isavga_attach(device_t dev) 191 { 192 vga_softc_t *sc; 193 int unit; 194 int rid; 195 int error; 196 197 unit = device_get_unit(dev); 198 sc = device_get_softc(dev); 199 200 rid = 0; 201 bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, 202 RF_ACTIVE | RF_SHAREABLE); 203 rid = 0; 204 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 205 RF_ACTIVE | RF_SHAREABLE); 206 207 error = vga_attach_unit(unit, sc, device_get_flags(dev)); 208 if (error) 209 return (error); 210 211 #ifdef FB_INSTALL_CDEV 212 /* attach a virtual frame buffer device */ 213 error = fb_attach(VGA_MKMINOR(unit), sc->adp, &isavga_cdevsw); 214 if (error) 215 return (error); 216 #endif /* FB_INSTALL_CDEV */ 217 218 if (0 && bootverbose) 219 vidd_diag(sc->adp, bootverbose); 220 221 #if 0 /* experimental */ 222 device_add_child(dev, "fb", -1); 223 bus_generic_attach(dev); 224 #endif 225 226 return (0); 227 } 228 229 static int 230 isavga_suspend(device_t dev) 231 { 232 int error; 233 234 error = bus_generic_suspend(dev); 235 if (error != 0) 236 return (error); 237 vga_suspend(dev); 238 239 return (error); 240 } 241 242 static int 243 isavga_resume(device_t dev) 244 { 245 246 vga_resume(dev); 247 248 return (bus_generic_resume(dev)); 249 } 250 251 #ifdef FB_INSTALL_CDEV 252 253 static int 254 isavga_open(struct cdev *dev, int flag, int mode, struct thread *td) 255 { 256 return (vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td)); 257 } 258 259 static int 260 isavga_close(struct cdev *dev, int flag, int mode, struct thread *td) 261 { 262 return (vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td)); 263 } 264 265 static int 266 isavga_read(struct cdev *dev, struct uio *uio, int flag) 267 { 268 return (vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag)); 269 } 270 271 static int 272 isavga_write(struct cdev *dev, struct uio *uio, int flag) 273 { 274 return (vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag)); 275 } 276 277 static int 278 isavga_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td) 279 { 280 return (vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td)); 281 } 282 283 static int 284 isavga_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr, 285 int prot, vm_memattr_t *memattr) 286 { 287 return (vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, paddr, prot, 288 memattr)); 289 } 290 291 #endif /* FB_INSTALL_CDEV */ 292 293 static device_method_t isavga_methods[] = { 294 DEVMETHOD(device_identify, isavga_identify), 295 DEVMETHOD(device_probe, isavga_probe), 296 DEVMETHOD(device_attach, isavga_attach), 297 DEVMETHOD(device_suspend, isavga_suspend), 298 DEVMETHOD(device_resume, isavga_resume), 299 300 DEVMETHOD_END 301 }; 302 303 static driver_t isavga_driver = { 304 VGA_DRIVER_NAME, 305 isavga_methods, 306 sizeof(vga_softc_t), 307 }; 308 309 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0); 310 311 static devclass_t vgapm_devclass; 312 313 static void 314 vgapm_identify(driver_t *driver, device_t parent) 315 { 316 317 if (device_get_flags(parent) != 0) 318 device_add_child(parent, "vgapm", 0); 319 } 320 321 static int 322 vgapm_probe(device_t dev) 323 { 324 325 device_set_desc(dev, "VGA suspend/resume"); 326 device_quiet(dev); 327 328 return (BUS_PROBE_DEFAULT); 329 } 330 331 static int 332 vgapm_attach(device_t dev) 333 { 334 335 bus_generic_probe(dev); 336 bus_generic_attach(dev); 337 338 return (0); 339 } 340 341 static int 342 vgapm_suspend(device_t dev) 343 { 344 device_t vga_dev; 345 int error; 346 347 error = bus_generic_suspend(dev); 348 if (error != 0) 349 return (error); 350 vga_dev = devclass_get_device(isavga_devclass, 0); 351 if (vga_dev == NULL) 352 return (0); 353 vga_suspend(vga_dev); 354 355 return (0); 356 } 357 358 static int 359 vgapm_resume(device_t dev) 360 { 361 device_t vga_dev; 362 363 vga_dev = devclass_get_device(isavga_devclass, 0); 364 if (vga_dev != NULL) 365 vga_resume(vga_dev); 366 367 return (bus_generic_resume(dev)); 368 } 369 370 static device_method_t vgapm_methods[] = { 371 DEVMETHOD(device_identify, vgapm_identify), 372 DEVMETHOD(device_probe, vgapm_probe), 373 DEVMETHOD(device_attach, vgapm_attach), 374 DEVMETHOD(device_suspend, vgapm_suspend), 375 DEVMETHOD(device_resume, vgapm_resume), 376 { 0, 0 } 377 }; 378 379 static driver_t vgapm_driver = { 380 "vgapm", 381 vgapm_methods, 382 0 383 }; 384 385 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0); 386