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