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