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