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