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