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