2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
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 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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
34 #include "opt_syscons.h" /* should be removed in the future, XXX */
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>
45 #include <machine/bus.h>
46 #include <machine/resource.h>
53 #include <machine/md_var.h>
55 #include <machine/pc/bios.h>
58 #include <dev/fb/fbreg.h>
59 #include <dev/fb/vgareg.h>
61 #include <isa/isareg.h>
62 #include <isa/isavar.h>
65 vga_suspend(device_t dev)
70 sc = device_get_softc(dev);
72 /* Save the video state across the suspend. */
73 if (sc->state_buf != NULL)
75 nbytes = vidd_save_state(sc->adp, NULL, 0);
78 sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT);
79 if (sc->state_buf == NULL)
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",
86 free(sc->state_buf, M_TEMP);
91 /* Save the color palette across the suspend. */
92 if (sc->pal_buf != NULL)
94 sc->pal_buf = malloc(256 * 3, M_TEMP, M_NOWAIT);
95 if (sc->pal_buf == NULL)
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);
107 vga_resume(device_t dev)
111 sc = device_get_softc(dev);
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;
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);
127 #define VGA_SOFTC(unit) \
128 ((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
130 static devclass_t isavga_devclass;
132 #ifdef FB_INSTALL_CDEV
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;
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,
153 #endif /* FB_INSTALL_CDEV */
156 isavga_identify(driver_t *driver, device_t parent)
158 BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
162 isavga_probe(device_t dev)
168 if (isa_get_vendorid(dev))
171 error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
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);
179 isa_set_port(dev, adp.va_io_base);
180 isa_set_portsize(dev, adp.va_io_size);
181 isa_set_maddr(dev, adp.va_mem_base);
182 isa_set_msize(dev, adp.va_mem_size);
189 isavga_attach(device_t dev)
196 unit = device_get_unit(dev);
197 sc = device_get_softc(dev);
200 bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
201 RF_ACTIVE | RF_SHAREABLE);
203 bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
204 RF_ACTIVE | RF_SHAREABLE);
206 error = vga_attach_unit(unit, sc, device_get_flags(dev));
210 #ifdef FB_INSTALL_CDEV
211 /* attach a virtual frame buffer device */
212 error = fb_attach(VGA_MKMINOR(unit), sc->adp, &isavga_cdevsw);
215 #endif /* FB_INSTALL_CDEV */
217 if (0 && bootverbose)
218 vidd_diag(sc->adp, bootverbose);
220 #if 0 /* experimental */
221 device_add_child(dev, "fb", -1);
222 bus_generic_attach(dev);
229 isavga_suspend(device_t dev)
233 error = bus_generic_suspend(dev);
242 isavga_resume(device_t dev)
247 return (bus_generic_resume(dev));
250 #ifdef FB_INSTALL_CDEV
253 isavga_open(struct cdev *dev, int flag, int mode, struct thread *td)
255 return (vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
259 isavga_close(struct cdev *dev, int flag, int mode, struct thread *td)
261 return (vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
265 isavga_read(struct cdev *dev, struct uio *uio, int flag)
267 return (vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
271 isavga_write(struct cdev *dev, struct uio *uio, int flag)
273 return (vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
277 isavga_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
279 return (vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td));
283 isavga_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
284 int prot, vm_memattr_t *memattr)
286 return (vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, paddr, prot,
290 #endif /* FB_INSTALL_CDEV */
292 static device_method_t isavga_methods[] = {
293 DEVMETHOD(device_identify, isavga_identify),
294 DEVMETHOD(device_probe, isavga_probe),
295 DEVMETHOD(device_attach, isavga_attach),
296 DEVMETHOD(device_suspend, isavga_suspend),
297 DEVMETHOD(device_resume, isavga_resume),
302 static driver_t isavga_driver = {
308 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
310 static devclass_t vgapm_devclass;
313 vgapm_identify(driver_t *driver, device_t parent)
316 if (device_get_flags(parent) != 0)
317 device_add_child(parent, "vgapm", 0);
321 vgapm_probe(device_t dev)
324 device_set_desc(dev, "VGA suspend/resume");
327 return (BUS_PROBE_DEFAULT);
331 vgapm_attach(device_t dev)
334 bus_generic_probe(dev);
335 bus_generic_attach(dev);
341 vgapm_suspend(device_t dev)
346 error = bus_generic_suspend(dev);
349 vga_dev = devclass_get_device(isavga_devclass, 0);
352 vga_suspend(vga_dev);
358 vgapm_resume(device_t dev)
362 vga_dev = devclass_get_device(isavga_devclass, 0);
366 return (bus_generic_resume(dev));
369 static device_method_t vgapm_methods[] = {
370 DEVMETHOD(device_identify, vgapm_identify),
371 DEVMETHOD(device_probe, vgapm_probe),
372 DEVMETHOD(device_attach, vgapm_attach),
373 DEVMETHOD(device_suspend, vgapm_suspend),
374 DEVMETHOD(device_resume, vgapm_resume),
378 static driver_t vgapm_driver = {
384 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0);