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