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