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