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