xref: /freebsd/sys/dev/fb/vga.c (revision f7c32ed617858bcd22f8d1b03199099d50125721)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1999 Kazutaka YOKOTA <yokota@zodiac.mech.utsunomiya-u.ac.jp>
5  * Copyright (c) 1992-1998 Søren Schmidt
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer as
13  *    the first lines of this file unmodified.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32 
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35 
36 #include "opt_vga.h"
37 #include "opt_fb.h"
38 #ifndef FB_DEBUG
39 #define	FB_DEBUG	0
40 #endif
41 #include "opt_syscons.h"	/* should be removed in the future, XXX */
42 
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/kernel.h>
46 #include <sys/conf.h>
47 #include <sys/fcntl.h>
48 #include <sys/malloc.h>
49 #include <sys/fbio.h>
50 
51 #include <vm/vm.h>
52 #include <vm/vm_param.h>
53 #include <vm/pmap.h>
54 
55 #include <machine/md_var.h>
56 #if defined(__i386__) || defined(__amd64__)
57 #include <machine/pc/bios.h>
58 #endif
59 #include <machine/bus.h>
60 
61 #include <dev/fb/fbreg.h>
62 #include <dev/fb/vgareg.h>
63 
64 #include <isa/isareg.h>
65 
66 #ifndef VGA_DEBUG
67 #define VGA_DEBUG		0
68 #endif
69 
70 /* XXX machine/pc/bios.h has got too much i386-specific stuff in it */
71 #ifndef BIOS_PADDRTOVADDR
72 #define	BIOS_PADDRTOVADDR(x)	(x)
73 #endif
74 
75 int
76 vga_probe_unit(int unit, video_adapter_t *buf, int flags)
77 {
78 	video_adapter_t *adp;
79 	video_switch_t *sw;
80 	int error;
81 
82 	sw = vid_get_switch(VGA_DRIVER_NAME);
83 	if (sw == NULL)
84 		return 0;
85 	error = (*sw->probe)(unit, &adp, NULL, flags);
86 	if (error)
87 		return error;
88 	bcopy(adp, buf, sizeof(*buf));
89 	return 0;
90 }
91 
92 int
93 vga_attach_unit(int unit, vga_softc_t *sc, int flags)
94 {
95 	video_switch_t *sw;
96 	int error;
97 
98 	sw = vid_get_switch(VGA_DRIVER_NAME);
99 	if (sw == NULL)
100 		return ENXIO;
101 
102 	error = (*sw->probe)(unit, &sc->adp, NULL, flags);
103 	if (error)
104 		return error;
105 	return (*sw->init)(unit, sc->adp, flags);
106 }
107 
108 /* cdev driver functions */
109 
110 #ifdef FB_INSTALL_CDEV
111 
112 int
113 vga_open(struct cdev *dev, vga_softc_t *sc, int flag, int mode, struct thread *td)
114 {
115 	if (sc == NULL)
116 		return ENXIO;
117 	if (mode & (O_CREAT | O_APPEND | O_TRUNC))
118 		return ENODEV;
119 
120 	return genfbopen(&sc->gensc, sc->adp, flag, mode, td);
121 }
122 
123 int
124 vga_close(struct cdev *dev, vga_softc_t *sc, int flag, int mode, struct thread *td)
125 {
126 	return genfbclose(&sc->gensc, sc->adp, flag, mode, td);
127 }
128 
129 int
130 vga_read(struct cdev *dev, vga_softc_t *sc, struct uio *uio, int flag)
131 {
132 	return genfbread(&sc->gensc, sc->adp, uio, flag);
133 }
134 
135 int
136 vga_write(struct cdev *dev, vga_softc_t *sc, struct uio *uio, int flag)
137 {
138 	return genfbread(&sc->gensc, sc->adp, uio, flag);
139 }
140 
141 int
142 vga_ioctl(struct cdev *dev, vga_softc_t *sc, u_long cmd, caddr_t arg, int flag,
143 	  struct thread *td)
144 {
145 	return genfbioctl(&sc->gensc, sc->adp, cmd, arg, flag, td);
146 }
147 
148 int
149 vga_mmap(struct cdev *dev, vga_softc_t *sc, vm_ooffset_t offset,
150     vm_paddr_t *paddr, int prot, vm_memattr_t *memattr)
151 {
152 	return genfbmmap(&sc->gensc, sc->adp, offset, paddr, prot, memattr);
153 }
154 
155 #endif /* FB_INSTALL_CDEV */
156 
157 /* LOW-LEVEL */
158 
159 #include <isa/rtc.h>
160 #ifdef __i386__
161 #include <dev/fb/vesa.h>
162 #endif
163 
164 #define probe_done(adp)		((adp)->va_flags & V_ADP_PROBED)
165 #define init_done(adp)		((adp)->va_flags & V_ADP_INITIALIZED)
166 #define config_done(adp)	((adp)->va_flags & V_ADP_REGISTERED)
167 
168 /* for compatibility with old kernel options */
169 #ifdef SC_ALT_SEQACCESS
170 #undef SC_ALT_SEQACCESS
171 #undef VGA_ALT_SEQACCESS
172 #define VGA_ALT_SEQACCESS	1
173 #endif
174 
175 #ifdef SLOW_VGA
176 #undef SLOW_VGA
177 #undef VGA_SLOW_IOACCESS
178 #define VGA_SLOW_IOACCESS
179 #endif
180 
181 /* architecture dependent option */
182 #if !defined(__i386__) && !defined(__amd64__)
183 #define VGA_NO_BIOS		1
184 #endif
185 
186 /* this should really be in `rtc.h' */
187 #define RTC_EQUIPMENT           0x14
188 
189 /* various sizes */
190 #define V_MODE_MAP_SIZE		(M_VGA_CG320 + 1)
191 #define V_MODE_PARAM_SIZE	64
192 
193 /* video adapter state buffer */
194 struct adp_state {
195     int			sig;
196 #define V_STATE_SIG	0x736f6962
197     u_char		regs[V_MODE_PARAM_SIZE];
198 };
199 typedef struct adp_state adp_state_t;
200 
201 /* video adapter information */
202 #define DCC_MONO	0
203 #define DCC_CGA40	1
204 #define DCC_CGA80	2
205 #define DCC_EGAMONO	3
206 #define DCC_EGA40	4
207 #define DCC_EGA80	5
208 
209 /*
210  * NOTE: `va_window' should have a virtual address, but is initialized
211  * with a physical address in the following table, as verify_adapter()
212  * will perform address conversion at run-time.
213  */
214 static video_adapter_t adapter_init_value[] = {
215     /* DCC_MONO */
216     { 0, KD_MONO, "mda", 0, 0, 0, 	    IO_MDA, IO_MDASIZE, MONO_CRTC,
217       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
218       0, 0, 0, 0, 7, 0, },
219     /* DCC_CGA40 */
220     { 0, KD_CGA,  "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
221       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
222       0, 0, 0, 0, 3, 0, },
223     /* DCC_CGA80 */
224     { 0, KD_CGA,  "cga", 0, 0, V_ADP_COLOR, IO_CGA, IO_CGASIZE, COLOR_CRTC,
225       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
226       0, 0, 0, 0, 3, 0, },
227     /* DCC_EGAMONO */
228     { 0, KD_EGA,  "ega", 0, 0, 0,	    IO_MDA, 48,	  MONO_CRTC,
229       EGA_BUF_BASE, EGA_BUF_SIZE, MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE,
230       0, 0, 0, 0, 7, 0, },
231     /* DCC_EGA40 */
232     { 0, KD_EGA,  "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48,	  COLOR_CRTC,
233       EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
234       0, 0, 0, 0, 3, 0, },
235     /* DCC_EGA80 */
236     { 0, KD_EGA,  "ega", 0, 0, V_ADP_COLOR, IO_MDA, 48,	  COLOR_CRTC,
237       EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE,
238       0, 0, 0, 0, 3, 0, },
239 };
240 
241 static video_adapter_t	biosadapter[2];
242 static int		biosadapters = 0;
243 
244 /* video driver declarations */
245 static int			vga_configure(int flags);
246        int			(*vga_sub_configure)(int flags);
247 #if 0
248 static int			vga_nop(void);
249 #endif
250 static int			vga_error(void);
251 static vi_probe_t		vga_probe;
252 static vi_init_t		vga_init;
253 static vi_get_info_t		vga_get_info;
254 static vi_query_mode_t		vga_query_mode;
255 static vi_set_mode_t		vga_set_mode;
256 static vi_save_font_t		vga_save_font;
257 static vi_load_font_t		vga_load_font;
258 static vi_show_font_t		vga_show_font;
259 static vi_save_palette_t	vga_save_palette;
260 static vi_load_palette_t	vga_load_palette;
261 static vi_set_border_t		vga_set_border;
262 static vi_save_state_t		vga_save_state;
263 static vi_load_state_t		vga_load_state;
264 static vi_set_win_org_t		vga_set_origin;
265 static vi_read_hw_cursor_t	vga_read_hw_cursor;
266 static vi_set_hw_cursor_t	vga_set_hw_cursor;
267 static vi_set_hw_cursor_shape_t	vga_set_hw_cursor_shape;
268 static vi_blank_display_t	vga_blank_display;
269 static vi_mmap_t		vga_mmap_buf;
270 static vi_ioctl_t		vga_dev_ioctl;
271 #ifndef VGA_NO_MODE_CHANGE
272 static vi_clear_t		vga_clear;
273 static vi_fill_rect_t		vga_fill_rect;
274 static vi_bitblt_t		vga_bitblt;
275 #else /* VGA_NO_MODE_CHANGE */
276 #define vga_clear		(vi_clear_t *)vga_error
277 #define vga_fill_rect		(vi_fill_rect_t *)vga_error
278 #define vga_bitblt		(vi_bitblt_t *)vga_error
279 #endif
280 static vi_diag_t		vga_diag;
281 
282 static video_switch_t vgavidsw = {
283 	vga_probe,
284 	vga_init,
285 	vga_get_info,
286 	vga_query_mode,
287 	vga_set_mode,
288 	vga_save_font,
289 	vga_load_font,
290 	vga_show_font,
291 	vga_save_palette,
292 	vga_load_palette,
293 	vga_set_border,
294 	vga_save_state,
295 	vga_load_state,
296 	vga_set_origin,
297 	vga_read_hw_cursor,
298 	vga_set_hw_cursor,
299 	vga_set_hw_cursor_shape,
300 	vga_blank_display,
301 	vga_mmap_buf,
302 	vga_dev_ioctl,
303 	vga_clear,
304 	vga_fill_rect,
305 	vga_bitblt,
306 	vga_error,
307 	vga_error,
308 	vga_diag,
309 };
310 
311 VIDEO_DRIVER(mda, vgavidsw, NULL);
312 VIDEO_DRIVER(cga, vgavidsw, NULL);
313 VIDEO_DRIVER(ega, vgavidsw, NULL);
314 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
315 
316 /* VGA BIOS standard video modes */
317 #define EOT		(-1)
318 #define NA		(-2)
319 
320 static video_info_t bios_vmode[] = {
321     /* CGA */
322     { M_B40x25,     V_INFO_COLOR, 40, 25, 8,  8, 2, 1,
323       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
324     { M_C40x25,     V_INFO_COLOR, 40, 25, 8,  8, 4, 1,
325       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
326     { M_B80x25,     V_INFO_COLOR, 80, 25, 8,  8, 2, 1,
327       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
328     { M_C80x25,     V_INFO_COLOR, 80, 25, 8,  8, 4, 1,
329       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
330     /* EGA */
331     { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
332       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
333     { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
334       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
335     { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
336       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
337     { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
338       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
339     /* VGA */
340     { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
341       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
342     { M_VGA_M80x25, 0,            80, 25, 8, 16, 2, 1,
343       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
344     { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
345       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
346     /* MDA */
347     { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1,
348       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
349     /* EGA */
350     { M_ENH_B80x43, V_INFO_COLOR, 80, 43, 8,  8, 2, 1,
351       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
352     { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8,  8, 4, 1,
353       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
354     /* VGA */
355     { M_VGA_M80x30, 0,            80, 30, 8, 16, 2, 1,
356       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
357     { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
358       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
359     { M_VGA_M80x50, 0,            80, 50, 8,  8, 2, 1,
360       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
361     { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8,  8, 4, 1,
362       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
363     { M_VGA_M80x60, 0,            80, 60, 8,  8, 2, 1,
364       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
365     { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8,  8, 4, 1,
366       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
367 
368 #ifndef VGA_NO_MODE_CHANGE
369 
370 #ifdef VGA_WIDTH90
371     { M_VGA_M90x25, 0,            90, 25, 8, 16, 2, 1,
372       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
373     { M_VGA_C90x25, V_INFO_COLOR, 90, 25, 8, 16, 4, 1,
374       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
375     { M_VGA_M90x30, 0,            90, 30, 8, 16, 2, 1,
376       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
377     { M_VGA_C90x30, V_INFO_COLOR, 90, 30, 8, 16, 4, 1,
378       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
379     { M_VGA_M90x43, 0,            90, 43, 8,  8, 2, 1,
380       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
381     { M_VGA_C90x43, V_INFO_COLOR, 90, 43, 8,  8, 4, 1,
382       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
383     { M_VGA_M90x50, 0,            90, 50, 8,  8, 2, 1,
384       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
385     { M_VGA_C90x50, V_INFO_COLOR, 90, 50, 8,  8, 4, 1,
386       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
387     { M_VGA_M90x60, 0,            90, 60, 8,  8, 2, 1,
388       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
389     { M_VGA_C90x60, V_INFO_COLOR, 90, 60, 8,  8, 4, 1,
390       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
391 #endif /* VGA_WIDTH90 */
392 
393     /* CGA */
394     { M_BG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
395       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
396     { M_CG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
397       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
398     { M_BG640,      V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 1, 1,
399       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
400     /* EGA */
401     { M_CG320_D,    V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 4, 4,
402       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
403       V_INFO_MM_PLANAR },
404     { M_CG640_E,    V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 4, 4,
405       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
406       V_INFO_MM_PLANAR },
407     { M_EGAMONOAPA, V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
408       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 ,
409       V_INFO_MM_PLANAR },
410     { M_ENHMONOAPA2,V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
411       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
412       V_INFO_MM_PLANAR },
413     { M_CG640x350,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
414       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
415       V_INFO_MM_PLANAR },
416     { M_ENH_CG640,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
417       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
418       V_INFO_MM_PLANAR },
419     /* VGA */
420     { M_BG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
421       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
422       V_INFO_MM_PLANAR },
423     { M_CG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
424       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
425       V_INFO_MM_PLANAR },
426     { M_VGA_CG320,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 8, 1,
427       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
428       V_INFO_MM_PACKED, 1 },
429     { M_VGA_MODEX,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8,  8, 8, 4,
430       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
431       V_INFO_MM_VGAX, 1 },
432 #endif /* VGA_NO_MODE_CHANGE */
433 
434     { EOT },
435 };
436 
437 static int		vga_init_done = FALSE;
438 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
439 static u_char		*video_mode_ptr = NULL;		/* EGA/VGA */
440 static u_char		*video_mode_ptr2 = NULL;	/* CGA/MDA */
441 #endif
442 static u_char		*mode_map[V_MODE_MAP_SIZE];
443 static adp_state_t	adpstate;
444 static adp_state_t	adpstate2;
445 static int		rows_offset = 1;
446 
447 /* local macros and functions */
448 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
449 
450 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
451 static void map_mode_table(u_char *map[], u_char *table, int max);
452 #endif
453 static void clear_mode_map(video_adapter_t *adp, u_char *map[], int max,
454 			   int color);
455 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
456 static int map_mode_num(int mode);
457 #endif
458 static int map_gen_mode_num(int type, int color, int mode);
459 static int map_bios_mode_num(int type, int color, int bios_mode);
460 static u_char *get_mode_param(int mode);
461 #ifndef VGA_NO_BIOS
462 static void fill_adapter_param(int code, video_adapter_t *adp);
463 #endif
464 static int verify_adapter(video_adapter_t *adp);
465 static void update_adapter_info(video_adapter_t *adp, video_info_t *info);
466 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
467 #define COMP_IDENTICAL	0
468 #define COMP_SIMILAR	1
469 #define COMP_DIFFERENT	2
470 static int comp_adpregs(u_char *buf1, u_char *buf2);
471 #endif
472 static int probe_adapters(void);
473 static int set_line_length(video_adapter_t *adp, int pixel);
474 static int set_display_start(video_adapter_t *adp, int x, int y);
475 
476 #ifndef VGA_NO_MODE_CHANGE
477 #ifdef VGA_WIDTH90
478 static void set_width90(adp_state_t *params);
479 #endif
480 #endif /* !VGA_NO_MODE_CHANGE */
481 
482 #ifndef VGA_NO_FONT_LOADING
483 #define PARAM_BUFSIZE	6
484 static void set_font_mode(video_adapter_t *adp, u_char *buf);
485 static void set_normal_mode(video_adapter_t *adp, u_char *buf);
486 #endif
487 
488 #ifndef VGA_NO_MODE_CHANGE
489 static void filll_io(int val, vm_offset_t d, size_t size);
490 static void planar_fill(video_adapter_t *adp, int val);
491 static void packed_fill(video_adapter_t *adp, int val);
492 static void direct_fill(video_adapter_t *adp, int val);
493 #ifdef notyet
494 static void planar_fill_rect(video_adapter_t *adp, int val, int x, int y,
495 			     int cx, int cy);
496 static void packed_fill_rect(video_adapter_t *adp, int val, int x, int y,
497 			     int cx, int cy);
498 static void direct_fill_rect16(video_adapter_t *adp, int val, int x, int y,
499 			       int cx, int cy);
500 static void direct_fill_rect24(video_adapter_t *adp, int val, int x, int y,
501 			       int cx, int cy);
502 static void direct_fill_rect32(video_adapter_t *adp, int val, int x, int y,
503 			       int cx, int cy);
504 #endif /* notyet */
505 #endif /* !VGA_NO_MODE_CHANGE */
506 
507 static void dump_buffer(u_char *buf, size_t len);
508 
509 #define	ISMAPPED(pa, width)				\
510 	(((pa) <= (u_long)0x1000 - (width)) 		\
511 	 || ((pa) >= ISA_HOLE_START && (pa) <= 0x100000 - (width)))
512 
513 #define	prologue(adp, flag, err)			\
514 	if (!vga_init_done || !((adp)->va_flags & (flag)))	\
515 	    return (err)
516 
517 /* a backdoor for the console driver */
518 static int
519 vga_configure(int flags)
520 {
521     int i;
522 
523     probe_adapters();
524     for (i = 0; i < biosadapters; ++i) {
525 	if (!probe_done(&biosadapter[i]))
526 	    continue;
527 	biosadapter[i].va_flags |= V_ADP_INITIALIZED;
528 	if (!config_done(&biosadapter[i])) {
529 	    if (vid_register(&biosadapter[i]) < 0)
530 		continue;
531 	    biosadapter[i].va_flags |= V_ADP_REGISTERED;
532 	}
533     }
534     if (vga_sub_configure != NULL)
535 	(*vga_sub_configure)(flags);
536 
537     return biosadapters;
538 }
539 
540 /* local subroutines */
541 
542 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
543 /* construct the mode parameter map */
544 static void
545 map_mode_table(u_char *map[], u_char *table, int max)
546 {
547     int i;
548 
549     for(i = 0; i < max; ++i)
550 	map[i] = table + i*V_MODE_PARAM_SIZE;
551     for(; i < V_MODE_MAP_SIZE; ++i)
552 	map[i] = NULL;
553 }
554 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
555 
556 static void
557 clear_mode_map(video_adapter_t *adp, u_char *map[], int max, int color)
558 {
559     video_info_t info;
560     int i;
561 
562     /*
563      * NOTE: we don't touch `bios_vmode[]' because it is shared
564      * by all adapters.
565      */
566     for(i = 0; i < max; ++i) {
567 	if (vga_get_info(adp, i, &info))
568 	    continue;
569 	if ((info.vi_flags & V_INFO_COLOR) != color)
570 	    map[i] = NULL;
571     }
572 }
573 
574 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
575 /* map the non-standard video mode to a known mode number */
576 static int
577 map_mode_num(int mode)
578 {
579     static struct {
580         int from;
581         int to;
582     } mode_map[] = {
583         { M_ENH_B80x43, M_ENH_B80x25 },
584         { M_ENH_C80x43, M_ENH_C80x25 },
585         { M_VGA_M80x30, M_VGA_M80x25 },
586         { M_VGA_C80x30, M_VGA_C80x25 },
587         { M_VGA_M80x50, M_VGA_M80x25 },
588         { M_VGA_C80x50, M_VGA_C80x25 },
589         { M_VGA_M80x60, M_VGA_M80x25 },
590         { M_VGA_C80x60, M_VGA_C80x25 },
591 #ifdef VGA_WIDTH90
592         { M_VGA_M90x25, M_VGA_M80x25 },
593         { M_VGA_C90x25, M_VGA_C80x25 },
594         { M_VGA_M90x30, M_VGA_M80x25 },
595         { M_VGA_C90x30, M_VGA_C80x25 },
596         { M_VGA_M90x43, M_VGA_M80x25 },
597         { M_VGA_C90x43, M_ENH_C80x25 },
598         { M_VGA_M90x50, M_VGA_M80x25 },
599         { M_VGA_C90x50, M_VGA_C80x25 },
600         { M_VGA_M90x60, M_VGA_M80x25 },
601         { M_VGA_C90x60, M_VGA_C80x25 },
602 #endif
603         { M_VGA_MODEX,  M_VGA_CG320 },
604     };
605     int i;
606 
607     for (i = 0; i < nitems(mode_map); ++i) {
608         if (mode_map[i].from == mode)
609             return mode_map[i].to;
610     }
611     return mode;
612 }
613 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
614 
615 /* map a generic video mode to a known mode number */
616 static int
617 map_gen_mode_num(int type, int color, int mode)
618 {
619     static struct {
620 	int from;
621 	int to_color;
622 	int to_mono;
623     } mode_map[] = {
624 	{ M_TEXT_80x30,	M_VGA_C80x30, M_VGA_M80x30, },
625 	{ M_TEXT_80x43,	M_ENH_C80x43, M_ENH_B80x43, },
626 	{ M_TEXT_80x50,	M_VGA_C80x50, M_VGA_M80x50, },
627 	{ M_TEXT_80x60,	M_VGA_C80x60, M_VGA_M80x60, },
628     };
629     int i;
630 
631     if (mode == M_TEXT_80x25) {
632 	switch (type) {
633 
634 	case KD_VGA:
635 	    if (color)
636 		return M_VGA_C80x25;
637 	    else
638 		return M_VGA_M80x25;
639 	    break;
640 
641 	case KD_EGA:
642 	    if (color)
643 		return M_ENH_C80x25;
644 	    else
645 		return M_EGAMONO80x25;
646 	    break;
647 
648 	case KD_CGA:
649 	    return M_C80x25;
650 
651 	case KD_MONO:
652 	case KD_HERCULES:
653 	    return M_EGAMONO80x25;	/* XXX: this name is confusing */
654 
655  	default:
656 	    return -1;
657 	}
658     }
659 
660     for (i = 0; i < nitems(mode_map); ++i) {
661         if (mode_map[i].from == mode)
662             return ((color) ? mode_map[i].to_color : mode_map[i].to_mono);
663     }
664     return mode;
665 }
666 
667 /* turn the BIOS video number into our video mode number */
668 static int
669 map_bios_mode_num(int type, int color, int bios_mode)
670 {
671     static int cga_modes[7] = {
672 	M_B40x25, M_C40x25,		/* 0, 1 */
673 	M_B80x25, M_C80x25,		/* 2, 3 */
674 	M_BG320, M_CG320,
675 	M_BG640,
676     };
677     static int ega_modes[17] = {
678 	M_ENH_B40x25, M_ENH_C40x25,	/* 0, 1 */
679 	M_ENH_B80x25, M_ENH_C80x25,	/* 2, 3 */
680 	M_BG320, M_CG320,
681 	M_BG640,
682 	M_EGAMONO80x25,			/* 7 */
683 	8, 9, 10, 11, 12,
684 	M_CG320_D,
685 	M_CG640_E,
686 	M_ENHMONOAPA2,			/* XXX: video momery > 64K */
687 	M_ENH_CG640,			/* XXX: video momery > 64K */
688     };
689     static int vga_modes[20] = {
690 	M_VGA_C40x25, M_VGA_C40x25,	/* 0, 1 */
691 	M_VGA_C80x25, M_VGA_C80x25,	/* 2, 3 */
692 	M_BG320, M_CG320,
693 	M_BG640,
694 	M_VGA_M80x25,			/* 7 */
695 	8, 9, 10, 11, 12,
696 	M_CG320_D,
697 	M_CG640_E,
698 	M_ENHMONOAPA2,
699 	M_ENH_CG640,
700 	M_BG640x480, M_CG640x480,
701 	M_VGA_CG320,
702     };
703 
704     switch (type) {
705 
706     case KD_VGA:
707 	if (bios_mode < nitems(vga_modes))
708 	    return vga_modes[bios_mode];
709 	else if (color)
710 	    return M_VGA_C80x25;
711 	else
712 	    return M_VGA_M80x25;
713 	break;
714 
715     case KD_EGA:
716 	if (bios_mode < nitems(ega_modes))
717 	    return ega_modes[bios_mode];
718 	else if (color)
719 	    return M_ENH_C80x25;
720 	else
721 	    return M_EGAMONO80x25;
722 	break;
723 
724     case KD_CGA:
725 	if (bios_mode < nitems(cga_modes))
726 	    return cga_modes[bios_mode];
727 	else
728 	    return M_C80x25;
729 	break;
730 
731     case KD_MONO:
732     case KD_HERCULES:
733 	return M_EGAMONO80x25;		/* XXX: this name is confusing */
734 
735     default:
736 	break;
737     }
738     return -1;
739 }
740 
741 /* look up a parameter table entry */
742 static u_char
743 *get_mode_param(int mode)
744 {
745 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
746     if (mode >= V_MODE_MAP_SIZE)
747 	mode = map_mode_num(mode);
748 #endif
749     if ((mode >= 0) && (mode < V_MODE_MAP_SIZE))
750 	return mode_map[mode];
751     else
752 	return NULL;
753 }
754 
755 #ifndef VGA_NO_BIOS
756 static void
757 fill_adapter_param(int code, video_adapter_t *adp)
758 {
759     static struct {
760 	int primary;
761 	int secondary;
762     } dcc[] = {
763 	{ DCC_MONO, 			DCC_EGA40 /* CGA monitor */ },
764 	{ DCC_MONO, 			DCC_EGA80 /* CGA monitor */ },
765 	{ DCC_MONO, 			DCC_EGA80 },
766 	{ DCC_MONO, 			DCC_EGA80 },
767 	{ DCC_CGA40, 			DCC_EGAMONO },
768 	{ DCC_CGA80, 			DCC_EGAMONO },
769 	{ DCC_EGA40 /* CGA monitor */, 	DCC_MONO},
770 	{ DCC_EGA80 /* CGA monitor */, 	DCC_MONO},
771 	{ DCC_EGA80,			DCC_MONO },
772 	{ DCC_EGA80, 			DCC_MONO },
773 	{ DCC_EGAMONO, 			DCC_CGA40 },
774 	{ DCC_EGAMONO, 			DCC_CGA80 },
775     };
776 
777     if ((code < 0) || (code >= nitems(dcc))) {
778 	adp[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
779 	adp[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
780     } else {
781 	adp[V_ADP_PRIMARY] = adapter_init_value[dcc[code].primary];
782 	adp[V_ADP_SECONDARY] = adapter_init_value[dcc[code].secondary];
783     }
784 }
785 #endif /* VGA_NO_BIOS */
786 
787 static int
788 verify_adapter(video_adapter_t *adp)
789 {
790     vm_offset_t buf;
791     u_int16_t v;
792 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
793     u_int32_t p;
794 #endif
795 
796     buf = BIOS_PADDRTOVADDR(adp->va_window);
797     v = readw(buf);
798     writew(buf, 0xA55A);
799     if (readw(buf) != 0xA55A)
800 	return ENXIO;
801     writew(buf, v);
802 
803     switch (adp->va_type) {
804 
805     case KD_EGA:
806 	outb(adp->va_crtc_addr, 7);
807 	if (inb(adp->va_crtc_addr) == 7) {
808 	    adp->va_type = KD_VGA;
809 	    adp->va_name = "vga";
810 	    adp->va_flags |= V_ADP_STATESAVE | V_ADP_PALETTE;
811 	}
812 	adp->va_flags |= V_ADP_STATELOAD | V_ADP_BORDER;
813 	/* the color adapter may be in the 40x25 mode... XXX */
814 
815 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
816 	/* get the BIOS video mode pointer */
817 	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8);
818 	p = BIOS_SADDRTOLADDR(p);
819 	if (ISMAPPED(p, sizeof(u_int32_t))) {
820 	    p = *(u_int32_t *)BIOS_PADDRTOVADDR(p);
821 	    p = BIOS_SADDRTOLADDR(p);
822 	    if (ISMAPPED(p, V_MODE_PARAM_SIZE))
823 		video_mode_ptr = (u_char *)BIOS_PADDRTOVADDR(p);
824 	}
825 #endif
826 	break;
827 
828     case KD_CGA:
829 	adp->va_flags |= V_ADP_COLOR | V_ADP_BORDER;
830 	/* may be in the 40x25 mode... XXX */
831 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
832 	/* get the BIOS video mode pointer */
833 	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
834 	p = BIOS_SADDRTOLADDR(p);
835 	video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
836 #endif
837 	break;
838 
839     case KD_MONO:
840 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
841 	/* get the BIOS video mode pointer */
842 	p = *(u_int32_t *)BIOS_PADDRTOVADDR(0x1d*4);
843 	p = BIOS_SADDRTOLADDR(p);
844 	video_mode_ptr2 = (u_char *)BIOS_PADDRTOVADDR(p);
845 #endif
846 	break;
847     }
848 
849     return 0;
850 }
851 
852 static void
853 update_adapter_info(video_adapter_t *adp, video_info_t *info)
854 {
855     adp->va_flags &= ~V_ADP_COLOR;
856     adp->va_flags |=
857 	(info->vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
858     adp->va_crtc_addr =
859 	(adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
860     adp->va_window = BIOS_PADDRTOVADDR(info->vi_window);
861     adp->va_window_size = info->vi_window_size;
862     adp->va_window_gran = info->vi_window_gran;
863     adp->va_window_orig = 0;
864     /* XXX */
865     adp->va_buffer = info->vi_buffer;
866     adp->va_buffer_size = info->vi_buffer_size;
867     adp->va_flags &= ~V_ADP_CWIDTH9;
868     if (info->vi_flags & V_INFO_CWIDTH9)
869 	adp->va_flags |= V_ADP_CWIDTH9;
870     if (info->vi_mem_model == V_INFO_MM_VGAX) {
871 	adp->va_line_width = info->vi_width/2;
872     } else if (info->vi_flags & V_INFO_GRAPHICS) {
873 	switch (info->vi_depth/info->vi_planes) {
874 	case 1:
875 	    adp->va_line_width = info->vi_width/8;
876 	    break;
877 	case 2:
878 	    adp->va_line_width = info->vi_width/4;
879 	    break;
880 	case 4:
881 	    adp->va_line_width = info->vi_width/2;
882 	    break;
883 	case 8:
884 	default: /* shouldn't happen */
885 	    adp->va_line_width = info->vi_width;
886 	    break;
887 	}
888     } else {
889 	adp->va_line_width = info->vi_width;
890     }
891     adp->va_disp_start.x = 0;
892     adp->va_disp_start.y = 0;
893     bcopy(info, &adp->va_info, sizeof(adp->va_info));
894 }
895 
896 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
897 /* compare two parameter table entries */
898 static int
899 comp_adpregs(u_char *buf1, u_char *buf2)
900 {
901     static struct {
902         u_char mask;
903     } params[V_MODE_PARAM_SIZE] = {
904 	{0xff}, {0x00}, {0xff}, 		/* COLS}, ROWS}, POINTS */
905 	{0x00}, {0x00}, 			/* page length */
906 	{0xfe}, {0xff}, {0xff}, {0xff},		/* sequencer registers */
907 	{0xf3},					/* misc register */
908 	{0xff}, {0xff}, {0xff}, {0x7f}, {0xff},	/* CRTC */
909 	{0xff}, {0xff}, {0xff}, {0x7f}, {0xff},
910 	{0x00}, {0x00}, {0x00}, {0x00}, {0x00},
911 	{0x00}, {0xff}, {0x7f}, {0xff}, {0xff},
912 	{0x7f}, {0xff}, {0xff}, {0xef}, {0xff},
913 	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},	/* attribute controller regs */
914 	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},
915 	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},
916 	{0xff}, {0xff}, {0xff}, {0xff}, {0xf0},
917 	{0xff}, {0xff}, {0xff}, {0xff}, {0xff},	/* GDC register */
918 	{0xff}, {0xff}, {0xff}, {0xff},
919     };
920     int identical = TRUE;
921     int i;
922 
923     if ((buf1 == NULL) || (buf2 == NULL))
924 	return COMP_DIFFERENT;
925 
926     for (i = 0; i < nitems(params); ++i) {
927 	if (params[i].mask == 0)	/* don't care */
928 	    continue;
929 	if ((buf1[i] & params[i].mask) != (buf2[i] & params[i].mask))
930 	    return COMP_DIFFERENT;
931 	if (buf1[i] != buf2[i])
932 	    identical = FALSE;
933     }
934     return (identical) ? COMP_IDENTICAL : COMP_SIMILAR;
935 }
936 #endif /* !VGA_NO_BIOS && !VGA_NO_MODE_CHANGE */
937 
938 /* probe video adapters and return the number of detected adapters */
939 static int
940 probe_adapters(void)
941 {
942     video_adapter_t *adp;
943     video_info_t info;
944 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
945     u_char *mp;
946 #endif
947     int height, i, width;
948 
949     /* do this test only once */
950     if (vga_init_done)
951 	return biosadapters;
952     vga_init_done = TRUE;
953 
954     /*
955      * Locate display adapters.
956      * The AT architecture supports up to two adapters. `syscons' allows
957      * the following combinations of adapters:
958      *     1) MDA + CGA
959      *     2) MDA + EGA/VGA color
960      *     3) CGA + EGA/VGA mono
961      * Note that `syscons' doesn't bother with MCGA as it is only
962      * avaiable for low end PS/2 models which has 80286 or earlier CPUs,
963      * thus, they are not running FreeBSD!
964      * When there are two adapaters in the system, one becomes `primary'
965      * and the other `secondary'. The EGA adapter has a set of DIP
966      * switches on board for this information and the EGA BIOS copies
967      * it in the BIOS data area BIOSDATA_VIDEOSWITCH (40:88).
968      * The VGA BIOS has more sophisticated mechanism and has this
969      * information in BIOSDATA_DCCINDEX (40:8a), but it also maintains
970      * compatibility with the EGA BIOS by updating BIOSDATA_VIDEOSWITCH.
971      */
972 
973     /*
974      * Check rtc and BIOS data area.
975      * XXX: we don't use BIOSDATA_EQUIPMENT, since it is not a dead
976      * copy of RTC_EQUIPMENT.  Bits 4 and 5 of ETC_EQUIPMENT are
977      * zeros for EGA and VGA.  However, the EGA/VGA BIOS sets
978      * these bits in BIOSDATA_EQUIPMENT according to the monitor
979      * type detected.
980      */
981 #ifndef VGA_NO_BIOS
982     if (*(u_int32_t *)BIOS_PADDRTOVADDR(0x4a8)) {
983 	/* EGA/VGA BIOS is present */
984 	fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
985 			   biosadapter);
986     } else {
987 	switch ((rtcin(RTC_EQUIPMENT) >> 4) & 3) {	/* bit 4 and 5 */
988 	case 0:
989 	    /* EGA/VGA: shouldn't be happening */
990 	    fill_adapter_param(readb(BIOS_PADDRTOVADDR(0x488)) & 0x0f,
991 			       biosadapter);
992 	    break;
993 	case 1:
994 	    /* CGA 40x25 */
995 	    /* FIXME: switch to the 80x25 mode? XXX */
996 	    biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA40];
997 	    biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
998 	    break;
999 	case 2:
1000 	    /* CGA 80x25 */
1001 	    biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_CGA80];
1002 	    biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
1003 	    break;
1004 	case 3:
1005 	    /* MDA */
1006 	    biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_MONO];
1007 	    biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_CGA80];
1008 	    break;
1009 	}
1010     }
1011 #else
1012     /* assume EGA/VGA? XXX */
1013     biosadapter[V_ADP_PRIMARY] = adapter_init_value[DCC_EGA80];
1014     biosadapter[V_ADP_SECONDARY] = adapter_init_value[DCC_MONO];
1015 #endif /* VGA_NO_BIOS */
1016 
1017     biosadapters = 0;
1018     if (verify_adapter(&biosadapter[V_ADP_SECONDARY]) == 0) {
1019 	++biosadapters;
1020 	biosadapter[V_ADP_SECONDARY].va_flags |= V_ADP_PROBED;
1021 	biosadapter[V_ADP_SECONDARY].va_mode =
1022 	    biosadapter[V_ADP_SECONDARY].va_initial_mode =
1023 	    map_bios_mode_num(biosadapter[V_ADP_SECONDARY].va_type,
1024 			      biosadapter[V_ADP_SECONDARY].va_flags
1025 				  & V_ADP_COLOR,
1026 			      biosadapter[V_ADP_SECONDARY].va_initial_bios_mode);
1027     } else {
1028 	biosadapter[V_ADP_SECONDARY].va_type = -1;
1029     }
1030     if (verify_adapter(&biosadapter[V_ADP_PRIMARY]) == 0) {
1031 	++biosadapters;
1032 	biosadapter[V_ADP_PRIMARY].va_flags |= V_ADP_PROBED;
1033 #ifndef VGA_NO_BIOS
1034 	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode =
1035 	    readb(BIOS_PADDRTOVADDR(0x449));
1036 #else
1037 	biosadapter[V_ADP_PRIMARY].va_initial_bios_mode = 3;	/* XXX */
1038 #endif
1039 	biosadapter[V_ADP_PRIMARY].va_mode =
1040 	    biosadapter[V_ADP_PRIMARY].va_initial_mode =
1041 	    map_bios_mode_num(biosadapter[V_ADP_PRIMARY].va_type,
1042 			      biosadapter[V_ADP_PRIMARY].va_flags & V_ADP_COLOR,
1043 			      biosadapter[V_ADP_PRIMARY].va_initial_bios_mode);
1044     } else {
1045 	biosadapter[V_ADP_PRIMARY] = biosadapter[V_ADP_SECONDARY];
1046 	biosadapter[V_ADP_SECONDARY].va_type = -1;
1047     }
1048     if (biosadapters == 0)
1049 	return biosadapters;
1050     biosadapter[V_ADP_PRIMARY].va_unit = V_ADP_PRIMARY;
1051     biosadapter[V_ADP_SECONDARY].va_unit = V_ADP_SECONDARY;
1052 
1053 #if 0 /* we don't need these... */
1054     fb_init_struct(&biosadapter[V_ADP_PRIMARY], ...);
1055     fb_init_struct(&biosadapter[V_ADP_SECONDARY], ...);
1056 #endif
1057 
1058 #ifdef notyet
1059     /*
1060      * We cannot have two video adapter of the same type; there must be
1061      * only one of color or mono adapter, or one each of them.
1062      */
1063     if (biosadapters > 1) {
1064 	if (!((biosadapter[0].va_flags ^ biosadapter[1].va_flags)
1065 	      & V_ADP_COLOR))
1066 	    /* we have two mono or color adapters!! */
1067 	    return (biosadapters = 0);
1068     }
1069 #endif
1070 
1071     /*
1072      * Ensure a zero start address. The registers are w/o
1073      * for old hardware so it's too hard to relocate the active screen
1074      * memory.
1075      * This must be done before vga_save_state() for VGA.
1076      */
1077     outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 12);
1078     outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
1079     outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr, 13);
1080     outb(biosadapter[V_ADP_PRIMARY].va_crtc_addr + 1, 0);
1081 
1082     /* the video mode parameter table in EGA/VGA BIOS */
1083     /* NOTE: there can be only one EGA/VGA, wheather color or mono,
1084      * recognized by the video BIOS.
1085      */
1086     if ((biosadapter[V_ADP_PRIMARY].va_type == KD_EGA) ||
1087 	(biosadapter[V_ADP_PRIMARY].va_type == KD_VGA)) {
1088 	adp = &biosadapter[V_ADP_PRIMARY];
1089     } else if ((biosadapter[V_ADP_SECONDARY].va_type == KD_EGA) ||
1090 	       (biosadapter[V_ADP_SECONDARY].va_type == KD_VGA)) {
1091 	adp = &biosadapter[V_ADP_SECONDARY];
1092     } else {
1093 	adp = NULL;
1094     }
1095     bzero(mode_map, sizeof(mode_map));
1096     if (adp != NULL) {
1097 	if (adp->va_type == KD_VGA) {
1098 	    vga_save_state(adp, &adpstate, sizeof(adpstate));
1099 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1100 	    mode_map[adp->va_initial_mode] = adpstate.regs;
1101 	    rows_offset = 1;
1102 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1103 	    if (video_mode_ptr == NULL) {
1104 		mode_map[adp->va_initial_mode] = adpstate.regs;
1105 		rows_offset = 1;
1106 	    } else {
1107 		/* discard the table if we are not familiar with it... */
1108 		map_mode_table(mode_map, video_mode_ptr, M_VGA_CG320 + 1);
1109 		mp = get_mode_param(adp->va_initial_mode);
1110 		if (mp != NULL)
1111 		    bcopy(mp, adpstate2.regs, sizeof(adpstate2.regs));
1112 		switch (comp_adpregs(adpstate.regs, mp)) {
1113 		case COMP_IDENTICAL:
1114 		    /*
1115 		     * OK, this parameter table looks reasonably familiar
1116 		     * to us...
1117 		     */
1118 		    /*
1119 		     * This is a kludge for Toshiba DynaBook SS433
1120 		     * whose BIOS video mode table entry has the actual #
1121 		     * of rows at the offset 1; BIOSes from other
1122 		     * manufacturers store the # of rows - 1 there. XXX
1123 		     */
1124 		    rows_offset = adpstate.regs[1] + 1 - mp[1];
1125 		    break;
1126 
1127 		case COMP_SIMILAR:
1128 		    /*
1129 		     * Not exactly the same, but similar enough to be
1130 		     * trusted. However, use the saved register values
1131 		     * for the initial mode and other modes which are
1132 		     * based on the initial mode.
1133 		     */
1134 		    mode_map[adp->va_initial_mode] = adpstate.regs;
1135 		    rows_offset = adpstate.regs[1] + 1 - mp[1];
1136 		    adpstate.regs[1] -= rows_offset - 1;
1137 		    break;
1138 
1139 		case COMP_DIFFERENT:
1140 		default:
1141 		    /*
1142 		     * Don't use the parameter table in the BIOS, since
1143 		     * even the BIOS doesn't use it for the initial mode.
1144 		     * Restrict the tweaked modes to (in practice) 80x50
1145 		     * from 80x25 with 400 scan lines, since the only safe
1146 		     * tweak is changing the characters from 8x16 to 8x8.
1147 		     */
1148 		    video_mode_ptr = NULL;
1149 		    bzero(mode_map, sizeof(mode_map));
1150 		    mode_map[adp->va_initial_mode] = adpstate.regs;
1151 		    rows_offset = 1;
1152 
1153 		    width = height = -1;
1154 		    for (i = 0; i < nitems(bios_vmode); ++i) {
1155 			if (bios_vmode[i].vi_mode == adp->va_initial_mode) {
1156 			    width = bios_vmode[i].vi_width;
1157 			    height = bios_vmode[i].vi_height;
1158 			    break;
1159 			}
1160 		    }
1161 		    for (i = 0; i < nitems(bios_vmode); ++i) {
1162 			if (bios_vmode[i].vi_mode != adp->va_initial_mode &&
1163 			    map_mode_num(bios_vmode[i].vi_mode) ==
1164 			     adp->va_initial_mode &&
1165 			    (bios_vmode[i].vi_width != width ||
1166 			     bios_vmode[i].vi_height != 2 * height)) {
1167 			    bios_vmode[i].vi_mode = NA;
1168 			}
1169 		    }
1170 		    break;
1171 		}
1172 	    }
1173 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1174 
1175 #ifndef VGA_NO_MODE_CHANGE
1176 	    adp->va_flags |= V_ADP_MODECHANGE;
1177 #endif
1178 #ifndef VGA_NO_FONT_LOADING
1179 	    adp->va_flags |= V_ADP_FONT;
1180 #endif
1181 	} else if (adp->va_type == KD_EGA) {
1182 #if defined(VGA_NO_BIOS) || defined(VGA_NO_MODE_CHANGE)
1183 	    rows_offset = 1;
1184 #else /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1185 	    if (video_mode_ptr == NULL) {
1186 		rows_offset = 1;
1187 	    } else {
1188 		map_mode_table(mode_map, video_mode_ptr, M_ENH_C80x25 + 1);
1189 		/* XXX how can one validate the EGA table... */
1190 		mp = get_mode_param(adp->va_initial_mode);
1191 		if (mp != NULL) {
1192 		    adp->va_flags |= V_ADP_MODECHANGE;
1193 #ifndef VGA_NO_FONT_LOADING
1194 		    adp->va_flags |= V_ADP_FONT;
1195 #endif
1196 		    rows_offset = 1;
1197 		} else {
1198 		    /*
1199 		     * This is serious. We will not be able to switch video
1200 		     * modes at all...
1201 		     */
1202 		    video_mode_ptr = NULL;
1203 		    bzero(mode_map, sizeof(mode_map));
1204 		    rows_offset = 1;
1205                 }
1206 	    }
1207 #endif /* VGA_NO_BIOS || VGA_NO_MODE_CHANGE */
1208 	}
1209     }
1210 
1211     /* remove conflicting modes if we have more than one adapter */
1212     if (biosadapters > 0) {
1213 	for (i = 0; i < biosadapters; ++i) {
1214 	    if (!(biosadapter[i].va_flags & V_ADP_MODECHANGE))
1215 		continue;
1216 	    clear_mode_map(&biosadapter[i], mode_map, M_VGA_CG320 + 1,
1217 			   (biosadapter[i].va_flags & V_ADP_COLOR) ?
1218 			       V_INFO_COLOR : 0);
1219 	    if ((biosadapter[i].va_type == KD_VGA)
1220 		|| (biosadapter[i].va_type == KD_EGA)) {
1221 		biosadapter[i].va_io_base =
1222 		    (biosadapter[i].va_flags & V_ADP_COLOR) ?
1223 			IO_VGA : IO_MDA;
1224 		biosadapter[i].va_io_size = 32;
1225 	    }
1226 	}
1227     }
1228 
1229 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
1230     /*
1231      * Attempt to determine the real character width for each mode.  9 wide
1232      * is supposed to be standard for EGA mono mode and most VGA text modes,
1233      * but some hardware doesn't support it, so dynamic configuration is
1234      * needed.  Bit 0 in sequencer register 1 is supposed control the width
1235      * (set = 8), but this is unreliable too.  Trust that 0 in the sequencer
1236      * bit means 9 wide after verifying that 9 is consistent with some CRTC
1237      * timing. The ratio (Horizontal Total) / (Horizontal Displayed) is
1238      * about 1.2 in all standard 9-wide modes and should be about 9/8 larger
1239      * again  in similar 8-wide modes; in practice it is usually about 1.4
1240      * times larger.
1241      */
1242     for (i = 0; i < nitems(bios_vmode); ++i) {
1243 	if (bios_vmode[i].vi_mem_model == V_INFO_MM_TEXT &&
1244 	    bios_vmode[i].vi_width != 90) {
1245 	    mp = get_mode_param(map_mode_num(bios_vmode[i].vi_mode));
1246 	    if (mp != NULL && !(mp[5] & 1) && mp[10] <= mp[11] * 125 / 100)
1247 		bios_vmode[i].vi_flags |= V_INFO_CWIDTH9;
1248 	}
1249     }
1250 #endif
1251 
1252     /* buffer address */
1253     vga_get_info(&biosadapter[V_ADP_PRIMARY],
1254 		 biosadapter[V_ADP_PRIMARY].va_initial_mode, &info);
1255     info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1256     update_adapter_info(&biosadapter[V_ADP_PRIMARY], &info);
1257 
1258     if (biosadapters > 1) {
1259 	vga_get_info(&biosadapter[V_ADP_SECONDARY],
1260 		     biosadapter[V_ADP_SECONDARY].va_initial_mode, &info);
1261 	info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1262 	update_adapter_info(&biosadapter[V_ADP_SECONDARY], &info);
1263     }
1264 
1265     /*
1266      * XXX: we should verify the following values for the primary adapter...
1267      * crtc I/O port address: *(u_int16_t *)BIOS_PADDRTOVADDR(0x463);
1268      * color/mono display: (*(u_int8_t *)BIOS_PADDRTOVADDR(0x487) & 0x02)
1269      *                     ? 0 : V_ADP_COLOR;
1270      * columns: *(u_int8_t *)BIOS_PADDRTOVADDR(0x44a);
1271      * rows: *(u_int8_t *)BIOS_PADDRTOVADDR(0x484);
1272      * font size: *(u_int8_t *)BIOS_PADDRTOVADDR(0x485);
1273      * buffer size: *(u_int16_t *)BIOS_PADDRTOVADDR(0x44c);
1274      */
1275 
1276     return biosadapters;
1277 }
1278 
1279 /* set the scan line length in pixel */
1280 static int
1281 set_line_length(video_adapter_t *adp, int pixel)
1282 {
1283     u_char *mp;
1284     int ppw;	/* pixels per word */
1285     int bpl;	/* bytes per line */
1286     int count;
1287 
1288     if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1289 	return ENODEV;
1290     mp = get_mode_param(adp->va_mode);
1291     if (mp == NULL)
1292 	return EINVAL;
1293 
1294     switch (adp->va_info.vi_mem_model) {
1295     case V_INFO_MM_PLANAR:
1296 	ppw = 16/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1297 	count = howmany(pixel, ppw)/2;
1298 	bpl = (howmany(pixel, ppw)/2)*4;
1299 	break;
1300     case V_INFO_MM_PACKED:
1301 	count = (pixel + 7)/8;
1302 	bpl = rounddown(pixel + 7, 8);
1303 	break;
1304     case V_INFO_MM_TEXT:
1305 	count = (pixel + 7)/8;			/* columns */
1306 	bpl = (pixel + 7)/8;			/* columns */
1307 	break;
1308     default:
1309 	return ENODEV;
1310     }
1311 
1312     if (mp[10 + 0x17] & 0x40)			/* CRTC mode control reg */
1313 	count *= 2;				/* byte mode */
1314     outb(adp->va_crtc_addr, 0x13);
1315     outb(adp->va_crtc_addr + 1, count);
1316     adp->va_line_width = bpl;
1317 
1318     return 0;
1319 }
1320 
1321 static int
1322 set_display_start(video_adapter_t *adp, int x, int y)
1323 {
1324     int off;	/* byte offset (graphics mode)/word offset (text mode) */
1325     int poff;	/* pixel offset */
1326     int roff;	/* row offset */
1327     int ppb;	/* pixels per byte */
1328 
1329     if ((adp->va_type != KD_VGA) && (adp->va_type != KD_EGA))
1330 	x &= ~7;
1331     if (adp->va_info.vi_flags & V_INFO_GRAPHICS) {
1332 	ppb = 8/(adp->va_info.vi_depth/adp->va_info.vi_planes);
1333 	off = y*adp->va_line_width + x/ppb;
1334 	roff = 0;
1335 	poff = x%ppb;
1336     } else {
1337 	if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1338 	    outb(TSIDX, 1);
1339 	    if (inb(TSREG) & 1)
1340 		ppb = 9;
1341 	    else
1342 		ppb = 8;
1343 	} else {
1344 	    ppb = 8;
1345 	}
1346 	off = y/adp->va_info.vi_cheight*adp->va_line_width + x/ppb;
1347 	roff = y%adp->va_info.vi_cheight;
1348 	/* FIXME: is this correct? XXX */
1349 	if (ppb == 8)
1350 	    poff = x%ppb;
1351 	else
1352 	    poff = (x + 8)%ppb;
1353     }
1354 
1355     /* start address */
1356     outb(adp->va_crtc_addr, 0xc);		/* high */
1357     outb(adp->va_crtc_addr + 1, off >> 8);
1358     outb(adp->va_crtc_addr, 0xd);		/* low */
1359     outb(adp->va_crtc_addr + 1, off & 0xff);
1360 
1361     /* horizontal pel pan */
1362     if ((adp->va_type == KD_VGA) || (adp->va_type == KD_EGA)) {
1363 	inb(adp->va_crtc_addr + 6);
1364 	outb(ATC, 0x13 | 0x20);
1365 	outb(ATC, poff);
1366 	inb(adp->va_crtc_addr + 6);
1367 	outb(ATC, 0x20);
1368     }
1369 
1370     /* preset raw scan */
1371     outb(adp->va_crtc_addr, 8);
1372     outb(adp->va_crtc_addr + 1, roff);
1373 
1374     adp->va_disp_start.x = x;
1375     adp->va_disp_start.y = y;
1376     return 0;
1377 }
1378 
1379 #ifndef VGA_NO_MODE_CHANGE
1380 #if defined(__i386__) || defined(__amd64__)	/* XXX */
1381 static void
1382 fill(int val, void *d, size_t size)
1383 {
1384     u_char *p = d;
1385 
1386     while (size-- > 0)
1387 	*p++ = val;
1388 }
1389 #endif /* __i386__ */
1390 
1391 static void
1392 filll_io(int val, vm_offset_t d, size_t size)
1393 {
1394     while (size-- > 0) {
1395 	writel(d, val);
1396 	d += sizeof(u_int32_t);
1397     }
1398 }
1399 #endif /* !VGA_NO_MODE_CHANGE */
1400 
1401 /* entry points */
1402 
1403 #if 0
1404 static int
1405 vga_nop(void)
1406 {
1407     return 0;
1408 }
1409 #endif
1410 
1411 static int
1412 vga_error(void)
1413 {
1414     return ENODEV;
1415 }
1416 
1417 static int
1418 vga_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
1419 {
1420     probe_adapters();
1421     if (unit >= biosadapters)
1422 	return ENXIO;
1423 
1424     *adpp = &biosadapter[unit];
1425 
1426     return 0;
1427 }
1428 
1429 static int
1430 vga_init(int unit, video_adapter_t *adp, int flags)
1431 {
1432     if ((unit >= biosadapters) || (adp == NULL) || !probe_done(adp))
1433 	return ENXIO;
1434 
1435     if (!init_done(adp)) {
1436 	/* nothing to do really... */
1437 	adp->va_flags |= V_ADP_INITIALIZED;
1438     }
1439 
1440     if (!config_done(adp)) {
1441 	if (vid_register(adp) < 0)
1442 		return ENXIO;
1443 	adp->va_flags |= V_ADP_REGISTERED;
1444     }
1445     if (vga_sub_configure != NULL)
1446 	(*vga_sub_configure)(0);
1447 
1448     return 0;
1449 }
1450 
1451 /*
1452  * get_info():
1453  * Return the video_info structure of the requested video mode.
1454  *
1455  * all adapters
1456  */
1457 static int
1458 vga_get_info(video_adapter_t *adp, int mode, video_info_t *info)
1459 {
1460     int i;
1461 
1462     if (!vga_init_done)
1463 	return ENXIO;
1464 
1465     mode = map_gen_mode_num(adp->va_type, adp->va_flags & V_ADP_COLOR, mode);
1466 #ifndef VGA_NO_MODE_CHANGE
1467     if (adp->va_flags & V_ADP_MODECHANGE) {
1468 	/*
1469 	 * If the parameter table entry for this mode is not found,
1470 	 * the mode is not supported...
1471 	 */
1472 	if (get_mode_param(mode) == NULL)
1473 	    return EINVAL;
1474     } else
1475 #endif /* VGA_NO_MODE_CHANGE */
1476     {
1477 	/*
1478 	 * Even if we don't support video mode switching on this adapter,
1479 	 * the information on the initial (thus current) video mode
1480 	 * should be made available.
1481 	 */
1482 	if (mode != adp->va_initial_mode)
1483 	    return EINVAL;
1484     }
1485 
1486     for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1487 	if (bios_vmode[i].vi_mode == NA)
1488 	    continue;
1489 	if (mode == bios_vmode[i].vi_mode) {
1490 	    *info = bios_vmode[i];
1491 	    /* XXX */
1492 	    info->vi_buffer_size = info->vi_window_size*info->vi_planes;
1493 	    return 0;
1494 	}
1495     }
1496     return EINVAL;
1497 }
1498 
1499 /*
1500  * query_mode():
1501  * Find a video mode matching the requested parameters.
1502  * Fields filled with 0 are considered "don't care" fields and
1503  * match any modes.
1504  *
1505  * all adapters
1506  */
1507 static int
1508 vga_query_mode(video_adapter_t *adp, video_info_t *info)
1509 {
1510     int i;
1511 
1512     if (!vga_init_done)
1513 	return ENXIO;
1514 
1515     for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
1516 	if (bios_vmode[i].vi_mode == NA)
1517 	    continue;
1518 
1519 	if ((info->vi_width != 0)
1520 	    && (info->vi_width != bios_vmode[i].vi_width))
1521 		continue;
1522 	if ((info->vi_height != 0)
1523 	    && (info->vi_height != bios_vmode[i].vi_height))
1524 		continue;
1525 	if ((info->vi_cwidth != 0)
1526 	    && (info->vi_cwidth != bios_vmode[i].vi_cwidth))
1527 		continue;
1528 	if ((info->vi_cheight != 0)
1529 	    && (info->vi_cheight != bios_vmode[i].vi_cheight))
1530 		continue;
1531 	if ((info->vi_depth != 0)
1532 	    && (info->vi_depth != bios_vmode[i].vi_depth))
1533 		continue;
1534 	if ((info->vi_planes != 0)
1535 	    && (info->vi_planes != bios_vmode[i].vi_planes))
1536 		continue;
1537 	/* XXX: should check pixel format, memory model */
1538 	if ((info->vi_flags != 0)
1539 	    && (info->vi_flags != bios_vmode[i].vi_flags))
1540 		continue;
1541 
1542 	/* verify if this mode is supported on this adapter */
1543 	if (vga_get_info(adp, bios_vmode[i].vi_mode, info))
1544 		continue;
1545 	return 0;
1546     }
1547     return ENODEV;
1548 }
1549 
1550 /*
1551  * set_mode():
1552  * Change the video mode.
1553  *
1554  * EGA/VGA
1555  */
1556 
1557 #ifndef VGA_NO_MODE_CHANGE
1558 #ifdef VGA_WIDTH90
1559 static void
1560 set_width90(adp_state_t *params)
1561 {
1562     /*
1563      * Based on code submitted by Kelly Yancey (kbyanc@freedomnet.com)
1564      * and alexv@sui.gda.itesm.mx.
1565      */
1566     params->regs[5] |= 1;		/* toggle 8 pixel wide fonts */
1567     params->regs[10+0x0] = 0x6b;
1568     params->regs[10+0x1] = 0x59;
1569     params->regs[10+0x2] = 0x5a;
1570     params->regs[10+0x3] = 0x8e;
1571     params->regs[10+0x4] = 0x5e;
1572     params->regs[10+0x5] = 0x8a;
1573     params->regs[10+0x13] = 45;
1574     params->regs[35+0x13] = 0;
1575 }
1576 #endif /* VGA_WIDTH90 */
1577 #endif /* !VGA_NO_MODE_CHANGE */
1578 
1579 static int
1580 vga_set_mode(video_adapter_t *adp, int mode)
1581 {
1582 #ifndef VGA_NO_MODE_CHANGE
1583     video_info_t info;
1584     adp_state_t params;
1585 
1586     prologue(adp, V_ADP_MODECHANGE, ENODEV);
1587 
1588     mode = map_gen_mode_num(adp->va_type,
1589 			    adp->va_flags & V_ADP_COLOR, mode);
1590     if (vga_get_info(adp, mode, &info))
1591 	return EINVAL;
1592 
1593 #if VGA_DEBUG > 1
1594     printf("vga_set_mode(): setting mode %d\n", mode);
1595 #endif
1596 
1597     params.sig = V_STATE_SIG;
1598     bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1599 
1600     switch (mode) {
1601 #ifdef VGA_WIDTH90
1602     case M_VGA_C90x60: case M_VGA_M90x60:
1603 	set_width90(&params);
1604 	/* FALLTHROUGH */
1605 #endif
1606     case M_VGA_C80x60: case M_VGA_M80x60:
1607 	params.regs[2]  = 0x08;
1608 	params.regs[19] = 0x47;
1609 	goto special_480l;
1610 
1611 #ifdef VGA_WIDTH90
1612     case M_VGA_C90x30: case M_VGA_M90x30:
1613 	set_width90(&params);
1614 	/* FALLTHROUGH */
1615 #endif
1616     case M_VGA_C80x30: case M_VGA_M80x30:
1617 	params.regs[19] = 0x4f;
1618 special_480l:
1619 	params.regs[9] |= 0xc0;
1620 	params.regs[16] = 0x08;
1621 	params.regs[17] = 0x3e;
1622 	params.regs[26] = 0xea;
1623 	params.regs[28] = 0xdf;
1624 	params.regs[31] = 0xe7;
1625 	params.regs[32] = 0x04;
1626 	goto setup_mode;
1627 
1628 #ifdef VGA_WIDTH90
1629     case M_VGA_C90x43: case M_VGA_M90x43:
1630 	set_width90(&params);
1631 	/* FALLTHROUGH */
1632 #endif
1633     case M_ENH_C80x43: case M_ENH_B80x43:
1634 	params.regs[28] = 87;
1635 	goto special_80x50;
1636 
1637 #ifdef VGA_WIDTH90
1638     case M_VGA_C90x50: case M_VGA_M90x50:
1639 	set_width90(&params);
1640 	/* FALLTHROUGH */
1641 #endif
1642     case M_VGA_C80x50: case M_VGA_M80x50:
1643 special_80x50:
1644 	params.regs[2] = 8;
1645 	params.regs[19] = 7;
1646 	goto setup_mode;
1647 
1648 #ifdef VGA_WIDTH90
1649     case M_VGA_C90x25: case M_VGA_M90x25:
1650 	set_width90(&params);
1651 	/* FALLTHROUGH */
1652 #endif
1653     case M_VGA_C40x25: case M_VGA_C80x25:
1654     case M_VGA_M80x25:
1655     case M_B40x25:     case M_C40x25:
1656     case M_B80x25:     case M_C80x25:
1657     case M_ENH_B40x25: case M_ENH_C40x25:
1658     case M_ENH_B80x25: case M_ENH_C80x25:
1659     case M_EGAMONO80x25:
1660 
1661 setup_mode:
1662 	vga_load_state(adp, &params);
1663 	break;
1664 
1665     case M_VGA_MODEX:
1666 	/* "unchain" the VGA mode */
1667 	params.regs[5-1+0x04] &= 0xf7;
1668 	params.regs[5-1+0x04] |= 0x04;
1669 	/* turn off doubleword mode */
1670 	params.regs[10+0x14] &= 0xbf;
1671 	/* turn off word addressing */
1672 	params.regs[10+0x17] |= 0x40;
1673 	/* set logical screen width */
1674 	params.regs[10+0x13] = 80;
1675 	/* set 240 lines */
1676 	params.regs[10+0x11] = 0x2c;
1677 	params.regs[10+0x06] = 0x0d;
1678 	params.regs[10+0x07] = 0x3e;
1679 	params.regs[10+0x10] = 0xea;
1680 	params.regs[10+0x11] = 0xac;
1681 	params.regs[10+0x12] = 0xdf;
1682 	params.regs[10+0x15] = 0xe7;
1683 	params.regs[10+0x16] = 0x06;
1684 	/* set vertical sync polarity to reflect aspect ratio */
1685 	params.regs[9] = 0xe3;
1686 	goto setup_grmode;
1687 
1688     case M_BG320:     case M_CG320:     case M_BG640:
1689     case M_CG320_D:   case M_CG640_E:
1690     case M_CG640x350: case M_ENH_CG640:
1691     case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1692 
1693 setup_grmode:
1694 	vga_load_state(adp, &params);
1695 	break;
1696 
1697     default:
1698 	return EINVAL;
1699     }
1700 
1701     adp->va_mode = mode;
1702     info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1703     update_adapter_info(adp, &info);
1704 
1705     /* move hardware cursor out of the way */
1706     vidd_set_hw_cursor(adp, -1, -1);
1707 
1708     return 0;
1709 #else /* VGA_NO_MODE_CHANGE */
1710     return ENODEV;
1711 #endif /* VGA_NO_MODE_CHANGE */
1712 }
1713 
1714 #ifndef VGA_NO_FONT_LOADING
1715 
1716 static void
1717 set_font_mode(video_adapter_t *adp, u_char *buf)
1718 {
1719     u_char *mp;
1720     int s;
1721 
1722     s = splhigh();
1723 
1724     /* save register values */
1725     if (adp->va_type == KD_VGA) {
1726 	outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1727 	outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1728 	outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1729 	outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1730 	outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1731 	inb(adp->va_crtc_addr + 6);
1732 	outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1733     } else /* if (adp->va_type == KD_EGA) */ {
1734 	/*
1735 	 * EGA cannot be read; copy parameters from the mode parameter
1736 	 * table.
1737 	 */
1738 	mp = get_mode_param(adp->va_mode);
1739 	buf[0] = mp[5 + 0x02 - 1];
1740 	buf[1] = mp[5 + 0x04 - 1];
1741 	buf[2] = mp[55 + 0x04];
1742 	buf[3] = mp[55 + 0x05];
1743 	buf[4] = mp[55 + 0x06];
1744 	buf[5] = mp[35 + 0x10];
1745     }
1746 
1747     /* setup vga for loading fonts */
1748     inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1749     outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1750     inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1751     outb(ATC, 0x20);				/* enable palette */
1752 
1753 #ifdef VGA_SLOW_IOACCESS
1754 #ifdef VGA_ALT_SEQACCESS
1755     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1756 #endif
1757     outb(TSIDX, 0x02); outb(TSREG, 0x04);
1758     outb(TSIDX, 0x04); outb(TSREG, 0x07);
1759 #ifdef VGA_ALT_SEQACCESS
1760     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1761 #endif
1762     outb(GDCIDX, 0x04); outb(GDCREG, 0x02);
1763     outb(GDCIDX, 0x05); outb(GDCREG, 0x00);
1764     outb(GDCIDX, 0x06); outb(GDCREG, 0x04);
1765 #else /* VGA_SLOW_IOACCESS */
1766 #ifdef VGA_ALT_SEQACCESS
1767     outw(TSIDX, 0x0100);
1768 #endif
1769     outw(TSIDX, 0x0402);
1770     outw(TSIDX, 0x0704);
1771 #ifdef VGA_ALT_SEQACCESS
1772     outw(TSIDX, 0x0300);
1773 #endif
1774     outw(GDCIDX, 0x0204);
1775     outw(GDCIDX, 0x0005);
1776     outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
1777 #endif /* VGA_SLOW_IOACCESS */
1778 
1779     splx(s);
1780 }
1781 
1782 static void
1783 set_normal_mode(video_adapter_t *adp, u_char *buf)
1784 {
1785     int s;
1786 
1787     s = splhigh();
1788 
1789     /* setup vga for normal operation mode again */
1790     inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1791     outb(ATC, 0x10); outb(ATC, buf[5]);
1792     inb(adp->va_crtc_addr + 6);			/* reset flip-flop */
1793     outb(ATC, 0x20);				/* enable palette */
1794 
1795 #ifdef VGA_SLOW_IOACCESS
1796 #ifdef VGA_ALT_SEQACCESS
1797     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1798 #endif
1799     outb(TSIDX, 0x02); outb(TSREG, buf[0]);
1800     outb(TSIDX, 0x04); outb(TSREG, buf[1]);
1801 #ifdef VGA_ALT_SEQACCESS
1802     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1803 #endif
1804     outb(GDCIDX, 0x04); outb(GDCREG, buf[2]);
1805     outb(GDCIDX, 0x05); outb(GDCREG, buf[3]);
1806     if (adp->va_crtc_addr == MONO_CRTC) {
1807 	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x08);
1808     } else {
1809 	outb(GDCIDX, 0x06); outb(GDCREG,(buf[4] & 0x03) | 0x0c);
1810     }
1811 #else /* VGA_SLOW_IOACCESS */
1812 #ifdef VGA_ALT_SEQACCESS
1813     outw(TSIDX, 0x0100);
1814 #endif
1815     outw(TSIDX, 0x0002 | (buf[0] << 8));
1816     outw(TSIDX, 0x0004 | (buf[1] << 8));
1817 #ifdef VGA_ALT_SEQACCESS
1818     outw(TSIDX, 0x0300);
1819 #endif
1820     outw(GDCIDX, 0x0004 | (buf[2] << 8));
1821     outw(GDCIDX, 0x0005 | (buf[3] << 8));
1822     if (adp->va_crtc_addr == MONO_CRTC)
1823         outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x08)<<8));
1824     else
1825         outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1826 #endif /* VGA_SLOW_IOACCESS */
1827 
1828     splx(s);
1829 }
1830 
1831 #endif /* VGA_NO_FONT_LOADING */
1832 
1833 /*
1834  * save_font():
1835  * Read the font data in the requested font page from the video adapter.
1836  *
1837  * EGA/VGA
1838  */
1839 static int
1840 vga_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1841 	      u_char *data, int ch, int count)
1842 {
1843 #ifndef VGA_NO_FONT_LOADING
1844     u_char buf[PARAM_BUFSIZE];
1845     vm_offset_t segment;
1846     int c;
1847 #ifdef VGA_ALT_SEQACCESS
1848     int s;
1849     u_char val = 0;
1850 #endif
1851 
1852     prologue(adp, V_ADP_FONT, ENODEV);
1853 
1854     if (fontsize < 14) {
1855 	/* FONT_8 */
1856 	fontsize = 8;
1857     } else if (fontsize >= 32) {
1858 	fontsize = 32;
1859     } else if (fontsize >= 16) {
1860 	/* FONT_16 */
1861 	fontsize = 16;
1862     } else {
1863 	/* FONT_14 */
1864 	fontsize = 14;
1865     }
1866 
1867     if (page < 0 || page >= 8 || fontwidth != 8)
1868 	return EINVAL;
1869     segment = FONT_BUF + 0x4000*page;
1870     if (page > 3)
1871 	segment -= 0xe000;
1872 
1873 #ifdef VGA_ALT_SEQACCESS
1874     if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1875 	s = splhigh();
1876 	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1877 	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
1878 	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1879 	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1880 	splx(s);
1881     }
1882 #endif
1883 
1884     set_font_mode(adp, buf);
1885     if (fontsize == 32) {
1886 	bcopy_fromio((uintptr_t)segment + ch*32, data, fontsize*count);
1887     } else {
1888 	for (c = ch; count > 0; ++c, --count) {
1889 	    bcopy_fromio((uintptr_t)segment + c*32, data, fontsize);
1890 	    data += fontsize;
1891 	}
1892     }
1893     set_normal_mode(adp, buf);
1894 
1895 #ifdef VGA_ALT_SEQACCESS
1896     if (adp->va_type == KD_VGA) {
1897 	s = splhigh();
1898 	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1899 	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1900 	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1901 	splx(s);
1902     }
1903 #endif
1904 
1905     return 0;
1906 #else /* VGA_NO_FONT_LOADING */
1907     return ENODEV;
1908 #endif /* VGA_NO_FONT_LOADING */
1909 }
1910 
1911 /*
1912  * load_font():
1913  * Set the font data in the requested font page.
1914  * NOTE: it appears that some recent video adapters do not support
1915  * the font page other than 0... XXX
1916  *
1917  * EGA/VGA
1918  */
1919 static int
1920 vga_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1921 	      u_char *data, int ch, int count)
1922 {
1923 #ifndef VGA_NO_FONT_LOADING
1924     u_char buf[PARAM_BUFSIZE];
1925     vm_offset_t segment;
1926     int c;
1927 #ifdef VGA_ALT_SEQACCESS
1928     int s;
1929     u_char val = 0;
1930 #endif
1931 
1932     prologue(adp, V_ADP_FONT, ENODEV);
1933 
1934     if (fontsize < 14) {
1935 	/* FONT_8 */
1936 	fontsize = 8;
1937     } else if (fontsize >= 32) {
1938 	fontsize = 32;
1939     } else if (fontsize >= 16) {
1940 	/* FONT_16 */
1941 	fontsize = 16;
1942     } else {
1943 	/* FONT_14 */
1944 	fontsize = 14;
1945     }
1946 
1947     if (page < 0 || page >= 8 || fontwidth != 8)
1948 	return EINVAL;
1949     segment = FONT_BUF + 0x4000*page;
1950     if (page > 3)
1951 	segment -= 0xe000;
1952 
1953 #ifdef VGA_ALT_SEQACCESS
1954     if (adp->va_type == KD_VGA) {	/* what about EGA? XXX */
1955 	s = splhigh();
1956 	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1957 	outb(TSIDX, 0x01); val = inb(TSREG);	/* disable screen */
1958 	outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1959 	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1960 	splx(s);
1961     }
1962 #endif
1963 
1964     set_font_mode(adp, buf);
1965     if (fontsize == 32) {
1966 	bcopy_toio(data, (uintptr_t)segment + ch*32, fontsize*count);
1967     } else {
1968 	for (c = ch; count > 0; ++c, --count) {
1969 	    bcopy_toio(data, (uintptr_t)segment + c*32, fontsize);
1970 	    data += fontsize;
1971 	}
1972     }
1973     set_normal_mode(adp, buf);
1974 
1975 #ifdef VGA_ALT_SEQACCESS
1976     if (adp->va_type == KD_VGA) {
1977 	s = splhigh();
1978 	outb(TSIDX, 0x00); outb(TSREG, 0x01);
1979 	outb(TSIDX, 0x01); outb(TSREG, val & 0xdf);	/* enable screen */
1980 	outb(TSIDX, 0x00); outb(TSREG, 0x03);
1981 	splx(s);
1982     }
1983 #endif
1984 
1985     return 0;
1986 #else /* VGA_NO_FONT_LOADING */
1987     return ENODEV;
1988 #endif /* VGA_NO_FONT_LOADING */
1989 }
1990 
1991 /*
1992  * show_font():
1993  * Activate the requested font page.
1994  * NOTE: it appears that some recent video adapters do not support
1995  * the font page other than 0... XXX
1996  *
1997  * EGA/VGA
1998  */
1999 static int
2000 vga_show_font(video_adapter_t *adp, int page)
2001 {
2002 #ifndef VGA_NO_FONT_LOADING
2003     static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
2004     int s;
2005 
2006     prologue(adp, V_ADP_FONT, ENODEV);
2007     if (page < 0 || page >= 8)
2008 	return EINVAL;
2009 
2010     s = splhigh();
2011     outb(TSIDX, 0x03); outb(TSREG, cg[page]);
2012     splx(s);
2013 
2014     return 0;
2015 #else /* VGA_NO_FONT_LOADING */
2016     return ENODEV;
2017 #endif /* VGA_NO_FONT_LOADING */
2018 }
2019 
2020 /*
2021  * save_palette():
2022  * Read DAC values. The values have expressed in 8 bits.
2023  *
2024  * VGA
2025  */
2026 static int
2027 vga_save_palette(video_adapter_t *adp, u_char *palette)
2028 {
2029     int bits;
2030     int i;
2031 
2032     prologue(adp, V_ADP_PALETTE, ENODEV);
2033 
2034     /*
2035      * We store 8 bit values in the palette buffer, while the standard
2036      * VGA has 6 bit DAC .
2037      */
2038     outb(PALRADR, 0x00);
2039     bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 0 : 2;
2040     for (i = 0; i < 256*3; ++i)
2041 	palette[i] = inb(PALDATA) << bits;
2042     inb(adp->va_crtc_addr + 6);	/* reset flip/flop */
2043     return 0;
2044 }
2045 
2046 static int
2047 vga_save_palette2(video_adapter_t *adp, int base, int count,
2048 		  u_char *r, u_char *g, u_char *b)
2049 {
2050     int bits;
2051     int i;
2052 
2053     prologue(adp, V_ADP_PALETTE, ENODEV);
2054 
2055     outb(PALRADR, base);
2056     bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 0 : 2;
2057     for (i = 0; i < count; ++i) {
2058 	r[i] = inb(PALDATA) << bits;
2059 	g[i] = inb(PALDATA) << bits;
2060 	b[i] = inb(PALDATA) << bits;
2061     }
2062     inb(adp->va_crtc_addr + 6);		/* reset flip/flop */
2063     return 0;
2064 }
2065 
2066 /*
2067  * load_palette():
2068  * Set DAC values.
2069  *
2070  * VGA
2071  */
2072 static int
2073 vga_load_palette(video_adapter_t *adp, u_char *palette)
2074 {
2075     int bits;
2076     int i;
2077 
2078     prologue(adp, V_ADP_PALETTE, ENODEV);
2079 
2080     outb(PIXMASK, 0xff);		/* no pixelmask */
2081     outb(PALWADR, 0x00);
2082     bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 0 : 2;
2083     for (i = 0; i < 256*3; ++i)
2084 	outb(PALDATA, palette[i] >> bits);
2085     inb(adp->va_crtc_addr + 6);	/* reset flip/flop */
2086     outb(ATC, 0x20);			/* enable palette */
2087     return 0;
2088 }
2089 
2090 static int
2091 vga_load_palette2(video_adapter_t *adp, int base, int count,
2092 		  u_char *r, u_char *g, u_char *b)
2093 {
2094     int bits;
2095     int i;
2096 
2097     prologue(adp, V_ADP_PALETTE, ENODEV);
2098 
2099     outb(PIXMASK, 0xff);		/* no pixelmask */
2100     outb(PALWADR, base);
2101     bits = (adp->va_flags & V_ADP_DAC8) != 0 ? 0 : 2;
2102     for (i = 0; i < count; ++i) {
2103 	outb(PALDATA, r[i] >> bits);
2104 	outb(PALDATA, g[i] >> bits);
2105 	outb(PALDATA, b[i] >> bits);
2106     }
2107     inb(adp->va_crtc_addr + 6);		/* reset flip/flop */
2108     outb(ATC, 0x20);			/* enable palette */
2109     return 0;
2110 }
2111 
2112 /*
2113  * set_border():
2114  * Change the border color.
2115  *
2116  * CGA/EGA/VGA
2117  */
2118 static int
2119 vga_set_border(video_adapter_t *adp, int color)
2120 {
2121     prologue(adp, V_ADP_BORDER, ENODEV);
2122 
2123     switch (adp->va_type) {
2124     case KD_EGA:
2125     case KD_VGA:
2126 	inb(adp->va_crtc_addr + 6);	/* reset flip-flop */
2127 	outb(ATC, 0x31); outb(ATC, color & 0xff);
2128 	break;
2129     case KD_CGA:
2130 	outb(adp->va_crtc_addr + 5, color & 0x0f); /* color select register */
2131 	break;
2132     case KD_MONO:
2133     case KD_HERCULES:
2134     default:
2135 	break;
2136     }
2137     return 0;
2138 }
2139 
2140 /*
2141  * save_state():
2142  * Read video register values.
2143  * NOTE: this function only reads the standard EGA/VGA registers.
2144  * any extra/extended registers of SVGA adapters are not saved.
2145  *
2146  * VGA
2147  */
2148 static int
2149 vga_save_state(video_adapter_t *adp, void *p, size_t size)
2150 {
2151     video_info_t info;
2152     u_char *buf;
2153     int crtc_addr;
2154     int i, j;
2155     int s;
2156 
2157     if (size == 0) {
2158 	/* return the required buffer size */
2159 	prologue(adp, V_ADP_STATESAVE, 0);
2160 	return sizeof(adp_state_t);
2161     } else {
2162 	prologue(adp, V_ADP_STATESAVE, ENODEV);
2163 	if (size < sizeof(adp_state_t))
2164 	    return EINVAL;
2165     }
2166 
2167     ((adp_state_t *)p)->sig = V_STATE_SIG;
2168     buf = ((adp_state_t *)p)->regs;
2169     bzero(buf, V_MODE_PARAM_SIZE);
2170     crtc_addr = adp->va_crtc_addr;
2171 
2172     s = splhigh();
2173 
2174     outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
2175     for (i = 0, j = 5; i < 4; i++) {
2176 	outb(TSIDX, i + 1);
2177 	buf[j++]  =  inb(TSREG);
2178     }
2179     buf[9]  =  inb(MISC + 10);			/* dot-clock */
2180     outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
2181 
2182     for (i = 0, j = 10; i < 25; i++) {		/* crtc */
2183 	outb(crtc_addr, i);
2184 	buf[j++]  =  inb(crtc_addr + 1);
2185     }
2186     for (i = 0, j = 35; i < 20; i++) {		/* attribute ctrl */
2187         inb(crtc_addr + 6);			/* reset flip-flop */
2188 	outb(ATC, i);
2189 	buf[j++]  =  inb(ATC + 1);
2190     }
2191     for (i = 0, j = 55; i < 9; i++) {		/* graph data ctrl */
2192 	outb(GDCIDX, i);
2193 	buf[j++]  =  inb(GDCREG);
2194     }
2195     inb(crtc_addr + 6);				/* reset flip-flop */
2196     outb(ATC, 0x20);				/* enable palette */
2197 
2198     splx(s);
2199 
2200 #if 1
2201     if (vga_get_info(adp, adp->va_mode, &info) == 0) {
2202 	if (info.vi_flags & V_INFO_GRAPHICS) {
2203 	    buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
2204 	    buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
2205 	} else {
2206 	    buf[0] = info.vi_width;		/* COLS */
2207 	    buf[1] = info.vi_height - 1;	/* ROWS */
2208 	}
2209 	buf[2] = info.vi_cheight;		/* POINTS */
2210     }
2211 #else
2212     buf[0] = readb(BIOS_PADDRTOVADDR(0x44a));	/* COLS */
2213     buf[1] = readb(BIOS_PADDRTOVADDR(0x484));	/* ROWS */
2214     buf[2] = readb(BIOS_PADDRTOVADDR(0x485));	/* POINTS */
2215     buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
2216     buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
2217 #endif
2218 
2219     return 0;
2220 }
2221 
2222 /*
2223  * load_state():
2224  * Set video registers at once.
2225  * NOTE: this function only updates the standard EGA/VGA registers.
2226  * any extra/extended registers of SVGA adapters are not changed.
2227  *
2228  * EGA/VGA
2229  */
2230 static int
2231 vga_load_state(video_adapter_t *adp, void *p)
2232 {
2233     u_char *buf;
2234     int crtc_addr;
2235     int s;
2236     int i;
2237 
2238     prologue(adp, V_ADP_STATELOAD, ENODEV);
2239     if (((adp_state_t *)p)->sig != V_STATE_SIG)
2240 	return EINVAL;
2241 
2242     buf = ((adp_state_t *)p)->regs;
2243     crtc_addr = adp->va_crtc_addr;
2244 
2245 #if VGA_DEBUG > 1
2246     dump_buffer(buf, V_MODE_PARAM_SIZE);
2247 #endif
2248 
2249     s = splhigh();
2250 
2251     outb(TSIDX, 0x00); outb(TSREG, 0x01);	/* stop sequencer */
2252     for (i = 0; i < 4; ++i) {			/* program sequencer */
2253 	outb(TSIDX, i + 1);
2254 	outb(TSREG, buf[i + 5]);
2255     }
2256     outb(MISC, buf[9]);				/* set dot-clock */
2257     outb(TSIDX, 0x00); outb(TSREG, 0x03);	/* start sequencer */
2258     outb(crtc_addr, 0x11);
2259     outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
2260     for (i = 0; i < 25; ++i) {			/* program crtc */
2261 	outb(crtc_addr, i);
2262 	outb(crtc_addr + 1, buf[i + 10]);
2263     }
2264     inb(crtc_addr+6);				/* reset flip-flop */
2265     for (i = 0; i < 20; ++i) {			/* program attribute ctrl */
2266 	outb(ATC, i);
2267 	outb(ATC, buf[i + 35]);
2268     }
2269     for (i = 0; i < 9; ++i) {			/* program graph data ctrl */
2270 	outb(GDCIDX, i);
2271 	outb(GDCREG, buf[i + 55]);
2272     }
2273     inb(crtc_addr + 6);				/* reset flip-flop */
2274     outb(ATC, 0x20);				/* enable palette */
2275 
2276 #ifdef notyet /* a temporary workaround for kernel panic, XXX */
2277 #ifndef VGA_NO_BIOS
2278     if (adp->va_unit == V_ADP_PRIMARY) {
2279 	writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]);	/* COLS */
2280 	writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
2281 	writeb(BIOS_PADDRTOVADDR(0x485), buf[2]);	/* POINTS */
2282 #if 0
2283 	writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
2284 	writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
2285 #endif
2286     }
2287 #endif /* VGA_NO_BIOS */
2288 #endif /* notyet */
2289 
2290     splx(s);
2291     return 0;
2292 }
2293 
2294 /*
2295  * set_origin():
2296  * Change the origin (window mapping) of the banked frame buffer.
2297  */
2298 static int
2299 vga_set_origin(video_adapter_t *adp, off_t offset)
2300 {
2301     /*
2302      * The standard video modes do not require window mapping;
2303      * always return error.
2304      */
2305     return ENODEV;
2306 }
2307 
2308 /*
2309  * read_hw_cursor():
2310  * Read the position of the hardware text cursor.
2311  *
2312  * all adapters
2313  */
2314 static int
2315 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
2316 {
2317     u_int16_t off;
2318     int s;
2319 
2320     if (!vga_init_done)
2321 	return ENXIO;
2322 
2323     if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2324 	return ENODEV;
2325 
2326     s = spltty();
2327     outb(adp->va_crtc_addr, 14);
2328     off = inb(adp->va_crtc_addr + 1);
2329     outb(adp->va_crtc_addr, 15);
2330     off = (off << 8) | inb(adp->va_crtc_addr + 1);
2331     splx(s);
2332 
2333     *row = off / adp->va_info.vi_width;
2334     *col = off % adp->va_info.vi_width;
2335 
2336     return 0;
2337 }
2338 
2339 /*
2340  * set_hw_cursor():
2341  * Move the hardware text cursor.  If col and row are both -1,
2342  * the cursor won't be shown.
2343  *
2344  * all adapters
2345  */
2346 static int
2347 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
2348 {
2349     u_int16_t off;
2350     int s;
2351 
2352     if (!vga_init_done)
2353 	return ENXIO;
2354 
2355     if ((col == -1) && (row == -1)) {
2356 	off = -1;
2357     } else {
2358 	if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
2359 	    return ENODEV;
2360 	off = row*adp->va_info.vi_width + col;
2361     }
2362 
2363     s = spltty();
2364     outb(adp->va_crtc_addr, 14);
2365     outb(adp->va_crtc_addr + 1, off >> 8);
2366     outb(adp->va_crtc_addr, 15);
2367     outb(adp->va_crtc_addr + 1, off & 0x00ff);
2368     splx(s);
2369 
2370     return 0;
2371 }
2372 
2373 /*
2374  * set_hw_cursor_shape():
2375  * Change the shape of the hardware text cursor. If the height is
2376  * zero or negative, the cursor won't be shown.
2377  *
2378  * all adapters
2379  */
2380 static int
2381 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
2382 			int celsize, int blink)
2383 {
2384     int s;
2385 
2386     if (!vga_init_done)
2387 	return ENXIO;
2388 
2389     s = spltty();
2390     switch (adp->va_type) {
2391     case KD_VGA:
2392     case KD_CGA:
2393     case KD_MONO:
2394     case KD_HERCULES:
2395     default:
2396 	if (height <= 0) {
2397 	    /* make the cursor invisible */
2398 	    outb(adp->va_crtc_addr, 10);
2399 	    outb(adp->va_crtc_addr + 1, 32);
2400 	    outb(adp->va_crtc_addr, 11);
2401 	    outb(adp->va_crtc_addr + 1, 0);
2402 	} else {
2403 	    outb(adp->va_crtc_addr, 10);
2404 	    outb(adp->va_crtc_addr + 1, celsize - base - height);
2405 	    outb(adp->va_crtc_addr, 11);
2406 	    outb(adp->va_crtc_addr + 1, celsize - base - 1);
2407 	}
2408 	break;
2409     case KD_EGA:
2410 	if (height <= 0) {
2411 	    /* make the cursor invisible */
2412 	    outb(adp->va_crtc_addr, 10);
2413 	    outb(adp->va_crtc_addr + 1, celsize);
2414 	    outb(adp->va_crtc_addr, 11);
2415 	    outb(adp->va_crtc_addr + 1, 0);
2416 	} else {
2417 	    outb(adp->va_crtc_addr, 10);
2418 	    outb(adp->va_crtc_addr + 1, celsize - base - height);
2419 	    outb(adp->va_crtc_addr, 11);
2420 	    outb(adp->va_crtc_addr + 1, celsize - base);
2421 	}
2422 	break;
2423     }
2424     splx(s);
2425 
2426     return 0;
2427 }
2428 
2429 /*
2430  * blank_display()
2431  * Put the display in power save/power off mode.
2432  *
2433  * all adapters
2434  */
2435 static int
2436 vga_blank_display(video_adapter_t *adp, int mode)
2437 {
2438     u_char val;
2439     int s;
2440 
2441     s = splhigh();
2442     switch (adp->va_type) {
2443     case KD_VGA:
2444 	switch (mode) {
2445 	case V_DISPLAY_SUSPEND:
2446 	case V_DISPLAY_STAND_BY:
2447 	    outb(TSIDX, 0x01);
2448 	    val = inb(TSREG);
2449 	    outb(TSIDX, 0x01);
2450 	    outb(TSREG, val | 0x20);
2451 	    outb(adp->va_crtc_addr, 0x17);
2452 	    val = inb(adp->va_crtc_addr + 1);
2453 	    outb(adp->va_crtc_addr + 1, val & ~0x80);
2454 	    break;
2455 	case V_DISPLAY_BLANK:
2456 	    outb(TSIDX, 0x01);
2457 	    val = inb(TSREG);
2458 	    outb(TSIDX, 0x01);
2459 	    outb(TSREG, val | 0x20);
2460 	    break;
2461 	case V_DISPLAY_ON:
2462 	    outb(TSIDX, 0x01);
2463 	    val = inb(TSREG);
2464 	    outb(TSIDX, 0x01);
2465 	    outb(TSREG, val & 0xDF);
2466 	    outb(adp->va_crtc_addr, 0x17);
2467 	    val = inb(adp->va_crtc_addr + 1);
2468 	    outb(adp->va_crtc_addr + 1, val | 0x80);
2469 	    break;
2470 	}
2471 	break;
2472 
2473     case KD_EGA:
2474 	/* no support yet */
2475 	splx(s);
2476 	return ENODEV;
2477 
2478     case KD_CGA:
2479 	switch (mode) {
2480 	case V_DISPLAY_SUSPEND:
2481 	case V_DISPLAY_STAND_BY:
2482 	case V_DISPLAY_BLANK:
2483 	    outb(adp->va_crtc_addr + 4, 0x25);
2484 	    break;
2485 	case V_DISPLAY_ON:
2486 	    outb(adp->va_crtc_addr + 4, 0x2d);
2487 	    break;
2488 	}
2489 	break;
2490 
2491     case KD_MONO:
2492     case KD_HERCULES:
2493 	switch (mode) {
2494 	case V_DISPLAY_SUSPEND:
2495 	case V_DISPLAY_STAND_BY:
2496 	case V_DISPLAY_BLANK:
2497 	    outb(adp->va_crtc_addr + 4, 0x21);
2498 	    break;
2499 	case V_DISPLAY_ON:
2500 	    outb(adp->va_crtc_addr + 4, 0x29);
2501 	    break;
2502 	}
2503 	break;
2504     default:
2505 	break;
2506     }
2507     splx(s);
2508 
2509     return 0;
2510 }
2511 
2512 /*
2513  * mmap():
2514  * Mmap frame buffer.
2515  *
2516  * all adapters
2517  */
2518 static int
2519 vga_mmap_buf(video_adapter_t *adp, vm_ooffset_t offset, vm_paddr_t *paddr,
2520    	     int prot, vm_memattr_t *memattr)
2521 {
2522     if (adp->va_info.vi_flags & V_INFO_LINEAR)
2523 	return -1;
2524 
2525 #if VGA_DEBUG > 0
2526     printf("vga_mmap_buf(): window:0x%jx, offset:0x%jx\n",
2527 	   (uintmax_t)adp->va_info.vi_window, (uintmax_t)offset);
2528 #endif
2529 
2530     /* XXX: is this correct? */
2531     if (offset > adp->va_window_size - PAGE_SIZE)
2532 	return -1;
2533 
2534     *paddr = adp->va_info.vi_window + offset;
2535     return 0;
2536 }
2537 
2538 #ifndef VGA_NO_MODE_CHANGE
2539 
2540 static void
2541 planar_fill(video_adapter_t *adp, int val)
2542 {
2543     int length;
2544     int at;			/* position in the frame buffer */
2545     int l;
2546 
2547     outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
2548     outw(GDCIDX, 0x0003);		/* data rotate/function select */
2549     outw(GDCIDX, 0x0f01);		/* set/reset enable */
2550     outw(GDCIDX, 0xff08);		/* bit mask */
2551     outw(GDCIDX, (val << 8) | 0x00);	/* set/reset */
2552     at = 0;
2553     length = adp->va_line_width*adp->va_info.vi_height;
2554     while (length > 0) {
2555 	l = imin(length, adp->va_window_size);
2556 	vidd_set_win_org(adp, at);
2557 	bzero_io(adp->va_window, l);
2558 	length -= l;
2559 	at += l;
2560     }
2561     outw(GDCIDX, 0x0000);		/* set/reset */
2562     outw(GDCIDX, 0x0001);		/* set/reset enable */
2563 }
2564 
2565 static void
2566 packed_fill(video_adapter_t *adp, int val)
2567 {
2568     int length;
2569     int at;			/* position in the frame buffer */
2570     int l;
2571 
2572     at = 0;
2573     length = adp->va_line_width*adp->va_info.vi_height;
2574     while (length > 0) {
2575 	l = imin(length, adp->va_window_size);
2576 	vidd_set_win_org(adp, at);
2577 	fill_io(val, adp->va_window, l);
2578 	length -= l;
2579 	at += l;
2580     }
2581 }
2582 
2583 static void
2584 direct_fill(video_adapter_t *adp, int val)
2585 {
2586     int length;
2587     int at;			/* position in the frame buffer */
2588     int l;
2589 
2590     at = 0;
2591     length = adp->va_line_width*adp->va_info.vi_height;
2592     while (length > 0) {
2593 	l = imin(length, adp->va_window_size);
2594 	vidd_set_win_org(adp, at);
2595 	switch (adp->va_info.vi_pixel_size) {
2596 	case sizeof(u_int16_t):
2597 	    fillw_io(val, adp->va_window, l/sizeof(u_int16_t));
2598 	    break;
2599 	case 3:
2600 	    /* FIXME */
2601 	    break;
2602 	case sizeof(u_int32_t):
2603 	    filll_io(val, adp->va_window, l/sizeof(u_int32_t));
2604 	    break;
2605 	}
2606 	length -= l;
2607 	at += l;
2608     }
2609 }
2610 
2611 static int
2612 vga_clear(video_adapter_t *adp)
2613 {
2614     switch (adp->va_info.vi_mem_model) {
2615     case V_INFO_MM_TEXT:
2616 	/* do nothing? XXX */
2617 	break;
2618     case V_INFO_MM_PLANAR:
2619 	planar_fill(adp, 0);
2620 	break;
2621     case V_INFO_MM_PACKED:
2622 	packed_fill(adp, 0);
2623 	break;
2624     case V_INFO_MM_DIRECT:
2625 	direct_fill(adp, 0);
2626 	break;
2627     }
2628     return 0;
2629 }
2630 
2631 #ifdef notyet
2632 static void
2633 planar_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2634 {
2635     int banksize;
2636     int bank;
2637     int pos;
2638     int offset;			/* offset within window */
2639     int bx;
2640     int l;
2641 
2642     outw(GDCIDX, 0x0005);		/* read mode 0, write mode 0 */
2643     outw(GDCIDX, 0x0003);		/* data rotate/function select */
2644     outw(GDCIDX, 0x0f01);		/* set/reset enable */
2645     outw(GDCIDX, 0xff08);		/* bit mask */
2646     outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
2647 
2648     banksize = adp->va_window_size;
2649     bank = -1;
2650     while (cy > 0) {
2651 	pos = adp->va_line_width*y + x/8;
2652 	if (bank != pos/banksize) {
2653 	    vidd_set_win_org(adp, pos);
2654 	    bank = pos/banksize;
2655 	}
2656 	offset = pos%banksize;
2657 	bx = (x + cx)/8 - x/8;
2658 	if (x % 8) {
2659 	    outw(GDCIDX, ((0xff00 >> (x % 8)) & 0xff00) | 0x08);
2660 	    writeb(adp->va_window + offset, 0);
2661 	    ++offset;
2662 	    --bx;
2663 	    if (offset >= banksize) {
2664 		offset = 0;
2665 		++bank;		/* next bank */
2666 		vidd_set_win_org(adp, bank*banksize);
2667 	    }
2668 	    outw(GDCIDX, 0xff08);	/* bit mask */
2669 	}
2670 	while (bx > 0) {
2671 	    l = imin(bx, banksize);
2672 	    bzero_io(adp->va_window + offset, l);
2673 	    offset += l;
2674 	    bx -= l;
2675 	    if (offset >= banksize) {
2676 		offset = 0;
2677 		++bank;		/* next bank */
2678 		vidd_set_win_org(adp, bank*banksize);
2679 	    }
2680 	}
2681 	if ((x + cx) % 8) {
2682 	    outw(GDCIDX, (~(0xff00 >> ((x + cx) % 8)) & 0xff00) | 0x08);
2683 	    writeb(adp->va_window + offset, 0);
2684 	    ++offset;
2685 	    if (offset >= banksize) {
2686 		offset = 0;
2687 		++bank;		/* next bank */
2688 		vidd_set_win_org(adp, bank*banksize);
2689 	    }
2690 	    outw(GDCIDX, 0xff08);	/* bit mask */
2691 	}
2692 	++y;
2693 	--cy;
2694     }
2695 
2696     outw(GDCIDX, 0xff08);		/* bit mask */
2697     outw(GDCIDX, 0x0000);		/* set/reset */
2698     outw(GDCIDX, 0x0001);		/* set/reset enable */
2699 }
2700 
2701 static void
2702 packed_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2703 {
2704     int banksize;
2705     int bank;
2706     int pos;
2707     int offset;			/* offset within window */
2708     int end;
2709 
2710     banksize = adp->va_window_size;
2711     bank = -1;
2712     cx *= adp->va_info.vi_pixel_size;
2713     while (cy > 0) {
2714 	pos = adp->va_line_width*y + x*adp->va_info.vi_pixel_size;
2715 	if (bank != pos/banksize) {
2716 	    vidd_set_win_org(adp, pos);
2717 	    bank = pos/banksize;
2718 	}
2719 	offset = pos%banksize;
2720 	end = imin(offset + cx, banksize);
2721 	fill_io(val, adp->va_window + offset,
2722 		(end - offset)/adp->va_info.vi_pixel_size);
2723 	/* the line may cross the window boundary */
2724 	if (offset + cx > banksize) {
2725 	    ++bank;		/* next bank */
2726 	    vidd_set_win_org(adp, bank*banksize);
2727 	    end = offset + cx - banksize;
2728 	    fill_io(val, adp->va_window, end/adp->va_info.vi_pixel_size);
2729 	}
2730 	++y;
2731 	--cy;
2732     }
2733 }
2734 
2735 static void
2736 direct_fill_rect16(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2737 {
2738     int banksize;
2739     int bank;
2740     int pos;
2741     int offset;			/* offset within window */
2742     int end;
2743 
2744     /*
2745      * XXX: the function assumes that banksize is a muliple of
2746      * sizeof(u_int16_t).
2747      */
2748     banksize = adp->va_window_size;
2749     bank = -1;
2750     cx *= sizeof(u_int16_t);
2751     while (cy > 0) {
2752 	pos = adp->va_line_width*y + x*sizeof(u_int16_t);
2753 	if (bank != pos/banksize) {
2754 	    vidd_set_win_org(adp, pos);
2755 	    bank = pos/banksize;
2756 	}
2757 	offset = pos%banksize;
2758 	end = imin(offset + cx, banksize);
2759 	fillw_io(val, adp->va_window + offset,
2760 		 (end - offset)/sizeof(u_int16_t));
2761 	/* the line may cross the window boundary */
2762 	if (offset + cx > banksize) {
2763 	    ++bank;		/* next bank */
2764 	    vidd_set_win_org(adp, bank*banksize);
2765 	    end = offset + cx - banksize;
2766 	    fillw_io(val, adp->va_window, end/sizeof(u_int16_t));
2767 	}
2768 	++y;
2769 	--cy;
2770     }
2771 }
2772 
2773 static void
2774 direct_fill_rect24(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2775 {
2776     int banksize;
2777     int bank;
2778     int pos;
2779     int offset;			/* offset within window */
2780     int end;
2781     int i;
2782     int j;
2783     u_int8_t b[3];
2784 
2785     b[0] = val & 0x0000ff;
2786     b[1] = (val >> 8) & 0x0000ff;
2787     b[2] = (val >> 16) & 0x0000ff;
2788     banksize = adp->va_window_size;
2789     bank = -1;
2790     cx *= 3;
2791     while (cy > 0) {
2792 	pos = adp->va_line_width*y + x*3;
2793 	if (bank != pos/banksize) {
2794 	    vidd_set_win_org(adp, pos);
2795 	    bank = pos/banksize;
2796 	}
2797 	offset = pos%banksize;
2798 	end = imin(offset + cx, banksize);
2799 	for (i = 0, j = offset; j < end; i = (++i)%3, ++j) {
2800 	    writeb(adp->va_window + j, b[i]);
2801 	}
2802 	/* the line may cross the window boundary */
2803 	if (offset + cx >= banksize) {
2804 	    ++bank;		/* next bank */
2805 	    vidd_set_win_org(adp, bank*banksize);
2806 	    j = 0;
2807 	    end = offset + cx - banksize;
2808 	    for (; j < end; i = (++i)%3, ++j) {
2809 		writeb(adp->va_window + j, b[i]);
2810 	    }
2811 	}
2812 	++y;
2813 	--cy;
2814     }
2815 }
2816 
2817 static void
2818 direct_fill_rect32(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2819 {
2820     int banksize;
2821     int bank;
2822     int pos;
2823     int offset;			/* offset within window */
2824     int end;
2825 
2826     /*
2827      * XXX: the function assumes that banksize is a muliple of
2828      * sizeof(u_int32_t).
2829      */
2830     banksize = adp->va_window_size;
2831     bank = -1;
2832     cx *= sizeof(u_int32_t);
2833     while (cy > 0) {
2834 	pos = adp->va_line_width*y + x*sizeof(u_int32_t);
2835 	if (bank != pos/banksize) {
2836 	    vidd_set_win_org(adp, pos);
2837 	    bank = pos/banksize;
2838 	}
2839 	offset = pos%banksize;
2840 	end = imin(offset + cx, banksize);
2841 	filll_io(val, adp->va_window + offset,
2842 		 (end - offset)/sizeof(u_int32_t));
2843 	/* the line may cross the window boundary */
2844 	if (offset + cx > banksize) {
2845 	    ++bank;		/* next bank */
2846 	    vidd_set_win_org(adp, bank*banksize);
2847 	    end = offset + cx - banksize;
2848 	    filll_io(val, adp->va_window, end/sizeof(u_int32_t));
2849 	}
2850 	++y;
2851 	--cy;
2852     }
2853 }
2854 
2855 static int
2856 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2857 {
2858     switch (adp->va_info.vi_mem_model) {
2859     case V_INFO_MM_TEXT:
2860 	/* do nothing? XXX */
2861 	break;
2862     case V_INFO_MM_PLANAR:
2863 	planar_fill_rect(adp, val, x, y, cx, cy);
2864 	break;
2865     case V_INFO_MM_PACKED:
2866 	packed_fill_rect(adp, val, x, y, cx, cy);
2867 	break;
2868     case V_INFO_MM_DIRECT:
2869 	switch (adp->va_info.vi_pixel_size) {
2870 	case sizeof(u_int16_t):
2871 	    direct_fill_rect16(adp, val, x, y, cx, cy);
2872 	    break;
2873 	case 3:
2874 	    direct_fill_rect24(adp, val, x, y, cx, cy);
2875 	    break;
2876 	case sizeof(u_int32_t):
2877 	    direct_fill_rect32(adp, val, x, y, cx, cy);
2878 	    break;
2879 	}
2880 	break;
2881     }
2882     return 0;
2883 }
2884 #else /* !notyet */
2885 static int
2886 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2887 {
2888     return ENODEV;
2889 }
2890 #endif /* notyet */
2891 
2892 static int
2893 vga_bitblt(video_adapter_t *adp,...)
2894 {
2895     /* FIXME */
2896     return ENODEV;
2897 }
2898 
2899 #endif /* !VGA_NO_MODE_CHANGE */
2900 
2901 static int
2902 get_palette(video_adapter_t *adp, int base, int count,
2903 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
2904 {
2905     u_char *r;
2906     u_char *g;
2907     u_char *b;
2908 
2909     if (count < 0 || base < 0 || count > 256 || base > 256 ||
2910 	base + count > 256)
2911 	return EINVAL;
2912 
2913     r = malloc(count*3, M_DEVBUF, M_WAITOK);
2914     g = r + count;
2915     b = g + count;
2916     if (vga_save_palette2(adp, base, count, r, g, b)) {
2917 	free(r, M_DEVBUF);
2918 	return ENODEV;
2919     }
2920     copyout(r, red, count);
2921     copyout(g, green, count);
2922     copyout(b, blue, count);
2923     if (trans != NULL) {
2924 	bzero(r, count);
2925 	copyout(r, trans, count);
2926     }
2927     free(r, M_DEVBUF);
2928 
2929     return 0;
2930 }
2931 
2932 static int
2933 set_palette(video_adapter_t *adp, int base, int count,
2934 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
2935 {
2936     u_char *r;
2937     u_char *g;
2938     u_char *b;
2939     int err;
2940 
2941     if (count < 0 || base < 0 || count > 256 || base > 256 ||
2942 	base + count > 256)
2943 	return EINVAL;
2944 
2945     r = malloc(count*3, M_DEVBUF, M_WAITOK);
2946     g = r + count;
2947     b = g + count;
2948     err = copyin(red, r, count);
2949     if (!err)
2950         err = copyin(green, g, count);
2951     if (!err)
2952         err = copyin(blue, b, count);
2953     if (!err)
2954         err = vga_load_palette2(adp, base, count, r, g, b);
2955     free(r, M_DEVBUF);
2956 
2957     return (err ? ENODEV : 0);
2958 }
2959 
2960 static int
2961 vga_dev_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
2962 {
2963     switch (cmd) {
2964     case FBIO_GETWINORG:	/* get frame buffer window origin */
2965 	*(u_int *)arg = 0;
2966 	return 0;
2967 
2968     case FBIO_SETWINORG:	/* set frame buffer window origin */
2969 	return ENODEV;
2970 
2971     case FBIO_SETDISPSTART:	/* set display start address */
2972 	return (set_display_start(adp,
2973 				  ((video_display_start_t *)arg)->x,
2974 			  	  ((video_display_start_t *)arg)->y)
2975 		? ENODEV : 0);
2976 
2977     case FBIO_SETLINEWIDTH:	/* set scan line length in pixel */
2978 	return (set_line_length(adp, *(u_int *)arg) ? ENODEV : 0);
2979 
2980     case FBIO_GETPALETTE:	/* get color palette */
2981 	return get_palette(adp, ((video_color_palette_t *)arg)->index,
2982 			   ((video_color_palette_t *)arg)->count,
2983 			   ((video_color_palette_t *)arg)->red,
2984 			   ((video_color_palette_t *)arg)->green,
2985 			   ((video_color_palette_t *)arg)->blue,
2986 			   ((video_color_palette_t *)arg)->transparent);
2987 
2988     case FBIO_SETPALETTE:	/* set color palette */
2989 	return set_palette(adp, ((video_color_palette_t *)arg)->index,
2990 			   ((video_color_palette_t *)arg)->count,
2991 			   ((video_color_palette_t *)arg)->red,
2992 			   ((video_color_palette_t *)arg)->green,
2993 			   ((video_color_palette_t *)arg)->blue,
2994 			   ((video_color_palette_t *)arg)->transparent);
2995 
2996     case FBIOGTYPE:		/* get frame buffer type info. */
2997 	((struct fbtype *)arg)->fb_type = fb_type(adp->va_type);
2998 	((struct fbtype *)arg)->fb_height = adp->va_info.vi_height;
2999 	((struct fbtype *)arg)->fb_width = adp->va_info.vi_width;
3000 	((struct fbtype *)arg)->fb_depth = adp->va_info.vi_depth;
3001 	if ((adp->va_info.vi_depth <= 1) || (adp->va_info.vi_depth > 8))
3002 	    ((struct fbtype *)arg)->fb_cmsize = 0;
3003 	else
3004 	    ((struct fbtype *)arg)->fb_cmsize = 1 << adp->va_info.vi_depth;
3005 	((struct fbtype *)arg)->fb_size = adp->va_buffer_size;
3006 	return 0;
3007 
3008     case FBIOGETCMAP:		/* get color palette */
3009 	return get_palette(adp, ((struct fbcmap *)arg)->index,
3010 			   ((struct fbcmap *)arg)->count,
3011 			   ((struct fbcmap *)arg)->red,
3012 			   ((struct fbcmap *)arg)->green,
3013 			   ((struct fbcmap *)arg)->blue, NULL);
3014 
3015     case FBIOPUTCMAP:		/* set color palette */
3016 	return set_palette(adp, ((struct fbcmap *)arg)->index,
3017 			   ((struct fbcmap *)arg)->count,
3018 			   ((struct fbcmap *)arg)->red,
3019 			   ((struct fbcmap *)arg)->green,
3020 			   ((struct fbcmap *)arg)->blue, NULL);
3021 
3022     default:
3023 	return fb_commonioctl(adp, cmd, arg);
3024     }
3025 }
3026 
3027 static void
3028 dump_buffer(u_char *buf, size_t len)
3029 {
3030     int i;
3031 
3032     for(i = 0; i < len;) {
3033 	printf("%02x ", buf[i]);
3034 	if ((++i % 16) == 0)
3035 	    printf("\n");
3036     }
3037 }
3038 
3039 /*
3040  * diag():
3041  * Print some information about the video adapter and video modes,
3042  * with requested level of details.
3043  *
3044  * all adapters
3045  */
3046 static int
3047 vga_diag(video_adapter_t *adp, int level)
3048 {
3049     u_char *mp;
3050 #if FB_DEBUG > 1
3051     video_info_t info;
3052     int i;
3053 #endif
3054 
3055     if (!vga_init_done)
3056 	return ENXIO;
3057 
3058 #if FB_DEBUG > 1
3059 #ifndef VGA_NO_BIOS
3060     printf("vga: RTC equip. code:0x%02x, DCC code:0x%02x\n",
3061 	   rtcin(RTC_EQUIPMENT), readb(BIOS_PADDRTOVADDR(0x488)));
3062     printf("vga: CRTC:0x%x, video option:0x%02x, ",
3063 	   readw(BIOS_PADDRTOVADDR(0x463)),
3064 	   readb(BIOS_PADDRTOVADDR(0x487)));
3065     printf("rows:%d, cols:%d, font height:%d\n",
3066 	   readb(BIOS_PADDRTOVADDR(0x44a)),
3067 	   readb(BIOS_PADDRTOVADDR(0x484)) + 1,
3068 	   readb(BIOS_PADDRTOVADDR(0x485)));
3069 #endif /* VGA_NO_BIOS */
3070 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
3071     printf("vga: param table EGA/VGA:%p", video_mode_ptr);
3072     printf(", CGA/MDA:%p\n", video_mode_ptr2);
3073     printf("vga: rows_offset:%d\n", rows_offset);
3074 #endif
3075 #endif /* FB_DEBUG > 1 */
3076 
3077     fb_dump_adp_info(VGA_DRIVER_NAME, adp, level);
3078 
3079 #if FB_DEBUG > 1
3080     if (adp->va_flags & V_ADP_MODECHANGE) {
3081 	for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
3082 	    if (bios_vmode[i].vi_mode == NA)
3083 		continue;
3084 	    if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
3085 		continue;
3086 	    fb_dump_mode_info(VGA_DRIVER_NAME, adp, &bios_vmode[i], level);
3087 	}
3088     } else {
3089 	vga_get_info(adp, adp->va_initial_mode, &info);	/* shouldn't fail */
3090 	fb_dump_mode_info(VGA_DRIVER_NAME, adp, &info, level);
3091     }
3092 #endif /* FB_DEBUG > 1 */
3093 
3094     if ((adp->va_type != KD_EGA) && (adp->va_type != KD_VGA))
3095 	return 0;
3096 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
3097     if (video_mode_ptr == NULL)
3098 	printf("vga%d: %s: WARNING: video mode switching is not "
3099 	       "fully supported on this adapter\n",
3100 	       adp->va_unit, adp->va_name);
3101 #endif
3102     if (level <= 0)
3103 	return 0;
3104 
3105     if (adp->va_type == KD_VGA) {
3106 	printf("VGA parameters upon power-up\n");
3107 	dump_buffer(adpstate.regs, sizeof(adpstate.regs));
3108 	printf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
3109 	dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
3110     }
3111 
3112     mp = get_mode_param(adp->va_initial_mode);
3113     if (mp == NULL)	/* this shouldn't be happening */
3114 	return 0;
3115     printf("EGA/VGA parameters to be used for mode %d\n", adp->va_initial_mode);
3116     dump_buffer(mp, V_MODE_PARAM_SIZE);
3117 
3118     return 0;
3119 }
3120