]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/isa/vga_isa.c
sys/*/conf/*, docs: fix links to handbook
[FreeBSD/FreeBSD.git] / sys / isa / vga_isa.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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 #include "opt_vga.h"
31 #include "opt_fb.h"
32 #include "opt_syscons.h"        /* should be removed in the future, XXX */
33
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/module.h>
39 #include <sys/conf.h>
40 #include <sys/bus.h>
41 #include <sys/fbio.h>
42
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45
46 #include <sys/rman.h>
47
48 #include <vm/vm.h>
49 #include <vm/pmap.h>
50
51 #include <machine/md_var.h>
52 #ifdef __i386__
53 #include <machine/pc/bios.h>
54 #endif
55
56 #include <dev/fb/fbreg.h>
57 #include <dev/fb/vgareg.h>
58
59 #include <isa/isareg.h>
60 #include <isa/isavar.h>
61
62 #define VGA_ID          0x0009d041      /* PNP0900 */
63
64 static struct isa_pnp_id vga_ids[] = {
65         { VGA_ID,       NULL },         /* PNP0900 */
66         { 0,            NULL },
67 };
68
69 static void
70 vga_suspend(device_t dev)
71 {
72         vga_softc_t *sc;
73         int nbytes;
74
75         sc = device_get_softc(dev);
76
77         /* Save the video state across the suspend. */
78         if (sc->state_buf != NULL)
79                 goto save_palette;
80         nbytes = vidd_save_state(sc->adp, NULL, 0);
81         if (nbytes <= 0)
82                 goto save_palette;
83         sc->state_buf = malloc(nbytes, M_TEMP, M_NOWAIT);
84         if (sc->state_buf == NULL)
85                 goto save_palette;
86         if (bootverbose)
87                 device_printf(dev, "saving %d bytes of video state\n", nbytes);
88         if (vidd_save_state(sc->adp, sc->state_buf, nbytes) != 0) {
89                 device_printf(dev, "failed to save state (nbytes=%d)\n",
90                     nbytes);
91                 free(sc->state_buf, M_TEMP);
92                 sc->state_buf = NULL;
93         }
94
95 save_palette:
96         /* Save the color palette across the suspend. */
97         if (sc->pal_buf != NULL)
98                 return;
99         sc->pal_buf = malloc(256 * 3, M_TEMP, M_NOWAIT);
100         if (sc->pal_buf == NULL)
101                 return;
102         if (bootverbose)
103                 device_printf(dev, "saving color palette\n");
104         if (vidd_save_palette(sc->adp, sc->pal_buf) != 0) {
105                 device_printf(dev, "failed to save palette\n");
106                 free(sc->pal_buf, M_TEMP);
107                 sc->pal_buf = NULL;
108         }
109 }
110
111 static void
112 vga_resume(device_t dev)
113 {
114         vga_softc_t *sc;
115
116         sc = device_get_softc(dev);
117
118         if (sc->state_buf != NULL) {
119                 if (vidd_load_state(sc->adp, sc->state_buf) != 0)
120                         device_printf(dev, "failed to reload state\n");
121                 free(sc->state_buf, M_TEMP);
122                 sc->state_buf = NULL;
123         }
124         if (sc->pal_buf != NULL) {
125                 if (vidd_load_palette(sc->adp, sc->pal_buf) != 0)
126                         device_printf(dev, "failed to reload palette\n");
127                 free(sc->pal_buf, M_TEMP);
128                 sc->pal_buf = NULL;
129         }
130 }
131
132 #define VGA_SOFTC(unit)         \
133         ((vga_softc_t *)devclass_get_softc(isavga_devclass, unit))
134
135 static devclass_t       isavga_devclass;
136
137 #ifdef FB_INSTALL_CDEV
138
139 static d_open_t         isavga_open;
140 static d_close_t        isavga_close;
141 static d_read_t         isavga_read;
142 static d_write_t        isavga_write;
143 static d_ioctl_t        isavga_ioctl;
144 static d_mmap_t         isavga_mmap;
145
146 static struct cdevsw isavga_cdevsw = {
147         .d_version =    D_VERSION,
148         .d_flags =      D_NEEDGIANT,
149         .d_open =       isavga_open,
150         .d_close =      isavga_close,
151         .d_read =       isavga_read,
152         .d_write =      isavga_write,
153         .d_ioctl =      isavga_ioctl,
154         .d_mmap =       isavga_mmap,
155         .d_name =       VGA_DRIVER_NAME,
156 };
157
158 #endif /* FB_INSTALL_CDEV */
159
160 static void
161 isavga_identify(driver_t *driver, device_t parent)
162 {
163         BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, VGA_DRIVER_NAME, 0);
164 }
165
166 static int
167 isavga_probe(device_t dev)
168 {
169         video_adapter_t adp;
170         int error;
171
172         /* No pnp support */
173         if (isa_get_vendorid(dev))
174                 return (ENXIO);
175
176         error = vga_probe_unit(device_get_unit(dev), &adp, device_get_flags(dev));
177         if (error == 0) {
178                 device_set_desc(dev, "Generic ISA VGA");
179                 bus_set_resource(dev, SYS_RES_IOPORT, 0,
180                                  adp.va_io_base, adp.va_io_size);
181                 bus_set_resource(dev, SYS_RES_MEMORY, 0,
182                                  adp.va_mem_base, adp.va_mem_size);
183                 isa_set_vendorid(dev, VGA_ID);
184                 isa_set_logicalid(dev, VGA_ID);
185 #if 0
186                 isa_set_port(dev, adp.va_io_base);
187                 isa_set_portsize(dev, adp.va_io_size);
188                 isa_set_maddr(dev, adp.va_mem_base);
189                 isa_set_msize(dev, adp.va_mem_size);
190 #endif
191         }
192         return (error);
193 }
194
195 static int
196 isavga_attach(device_t dev)
197 {
198         vga_softc_t *sc;
199         int unit;
200         int rid;
201         int error;
202
203         unit = device_get_unit(dev);
204         sc = device_get_softc(dev);
205
206         rid = 0;
207         bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid,
208                                   RF_ACTIVE | RF_SHAREABLE);
209         rid = 0;
210         bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
211                                  RF_ACTIVE | RF_SHAREABLE);
212
213         error = vga_attach_unit(unit, sc, device_get_flags(dev));
214         if (error)
215                 return (error);
216
217 #ifdef FB_INSTALL_CDEV
218         /* attach a virtual frame buffer device */
219         error = fb_attach(VGA_MKMINOR(unit), sc->adp, &isavga_cdevsw);
220         if (error)
221                 return (error);
222 #endif /* FB_INSTALL_CDEV */
223
224         if (0 && bootverbose)
225                 vidd_diag(sc->adp, bootverbose);
226
227 #if 0 /* experimental */
228         device_add_child(dev, "fb", -1);
229         bus_generic_attach(dev);
230 #endif
231
232         return (0);
233 }
234
235 static int
236 isavga_suspend(device_t dev)
237 {
238         int error;
239
240         error = bus_generic_suspend(dev);
241         if (error != 0)
242                 return (error);
243         vga_suspend(dev);
244
245         return (error);
246 }
247
248 static int
249 isavga_resume(device_t dev)
250 {
251
252         vga_resume(dev);
253
254         return (bus_generic_resume(dev));
255 }
256
257 #ifdef FB_INSTALL_CDEV
258
259 static int
260 isavga_open(struct cdev *dev, int flag, int mode, struct thread *td)
261 {
262         return (vga_open(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
263 }
264
265 static int
266 isavga_close(struct cdev *dev, int flag, int mode, struct thread *td)
267 {
268         return (vga_close(dev, VGA_SOFTC(VGA_UNIT(dev)), flag, mode, td));
269 }
270
271 static int
272 isavga_read(struct cdev *dev, struct uio *uio, int flag)
273 {
274         return (vga_read(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
275 }
276
277 static int
278 isavga_write(struct cdev *dev, struct uio *uio, int flag)
279 {
280         return (vga_write(dev, VGA_SOFTC(VGA_UNIT(dev)), uio, flag));
281 }
282
283 static int
284 isavga_ioctl(struct cdev *dev, u_long cmd, caddr_t arg, int flag, struct thread *td)
285 {
286         return (vga_ioctl(dev, VGA_SOFTC(VGA_UNIT(dev)), cmd, arg, flag, td));
287 }
288
289 static int
290 isavga_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
291     int prot, vm_memattr_t *memattr)
292 {
293         return (vga_mmap(dev, VGA_SOFTC(VGA_UNIT(dev)), offset, paddr, prot,
294             memattr));
295 }
296
297 #endif /* FB_INSTALL_CDEV */
298
299 static device_method_t isavga_methods[] = {
300         DEVMETHOD(device_identify,      isavga_identify),
301         DEVMETHOD(device_probe,         isavga_probe),
302         DEVMETHOD(device_attach,        isavga_attach),
303         DEVMETHOD(device_suspend,       isavga_suspend),
304         DEVMETHOD(device_resume,        isavga_resume),
305
306         DEVMETHOD_END
307 };
308
309 static driver_t isavga_driver = {
310         VGA_DRIVER_NAME,
311         isavga_methods,
312         sizeof(vga_softc_t),
313 };
314
315 DRIVER_MODULE(vga, isa, isavga_driver, isavga_devclass, 0, 0);
316
317 static devclass_t       vgapm_devclass;
318
319 static void
320 vgapm_identify(driver_t *driver, device_t parent)
321 {
322
323         if (device_get_flags(parent) != 0)
324                 device_add_child(parent, "vgapm", 0);
325 }
326
327 static int
328 vgapm_probe(device_t dev)
329 {
330
331         device_set_desc(dev, "VGA suspend/resume");
332         device_quiet(dev);
333
334         return (BUS_PROBE_DEFAULT);
335 }
336
337 static int
338 vgapm_attach(device_t dev)
339 {
340
341         bus_generic_probe(dev);
342         bus_generic_attach(dev);
343
344         return (0);
345 }
346
347 static int
348 vgapm_suspend(device_t dev)
349 {
350         device_t vga_dev;
351         int error;
352
353         error = bus_generic_suspend(dev);
354         if (error != 0)
355                 return (error);
356         vga_dev = devclass_get_device(isavga_devclass, 0);
357         if (vga_dev == NULL)
358                 return (0);
359         vga_suspend(vga_dev);
360
361         return (0);
362 }
363
364 static int
365 vgapm_resume(device_t dev)
366 {
367         device_t vga_dev;
368
369         vga_dev = devclass_get_device(isavga_devclass, 0);
370         if (vga_dev != NULL)
371                 vga_resume(vga_dev);
372
373         return (bus_generic_resume(dev));
374 }
375
376 static device_method_t vgapm_methods[] = {
377         DEVMETHOD(device_identify,      vgapm_identify),
378         DEVMETHOD(device_probe,         vgapm_probe),
379         DEVMETHOD(device_attach,        vgapm_attach),
380         DEVMETHOD(device_suspend,       vgapm_suspend),
381         DEVMETHOD(device_resume,        vgapm_resume),
382         { 0, 0 }
383 };
384
385 static driver_t vgapm_driver = {
386         "vgapm",
387         vgapm_methods,
388         0
389 };
390
391 DRIVER_MODULE(vgapm, vgapci, vgapm_driver, vgapm_devclass, 0, 0);
392 ISA_PNP_INFO(vga_ids);