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