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