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