Merge branch 'vendor/SENDMAIL'
[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  */
31
32 #include "opt_vga.h"
33 #include "opt_fb.h"
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/conf.h>
39 #include <sys/fcntl.h>
40 #include <sys/malloc.h>
41 #include <sys/fbio.h>
42 #include <sys/thread2.h>
43
44 #include <bus/isa/isareg.h>
45
46 #include <machine/clock.h>
47 #include <machine/md_var.h>
48 #ifdef __i386__
49 #include <machine/pc/bios.h>
50 #endif
51
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/pmap.h>
55
56 #include "fbreg.h"
57 #include "vgareg.h"
58
59 #ifndef VGA_DEBUG
60 #define VGA_DEBUG               0
61 #endif
62
63 /* machine/pc/bios.h has got too much i386-specific stuff in it */
64 #ifndef BIOS_PADDRTOVADDR
65 #define BIOS_PADDRTOVADDR(x) (((x) - ISA_HOLE_START) + atdevbase)
66 #endif
67 int
68 vga_probe_unit(int unit, video_adapter_t *buf, int flags)
69 {
70         video_adapter_t *adp;
71         video_switch_t *sw;
72         int error;
73
74         sw = vid_get_switch(VGA_DRIVER_NAME);
75         if (sw == NULL)
76                 return 0;
77         error = (*sw->probe)(unit, &adp, NULL, flags);
78         if (error)
79                 return error;
80         bcopy(adp, buf, sizeof(*buf));
81         return 0;
82 }
83
84 int
85 vga_attach_unit(int unit, vga_softc_t *sc, int flags)
86 {
87         video_switch_t *sw;
88         int error;
89
90         sw = vid_get_switch(VGA_DRIVER_NAME);
91         if (sw == NULL)
92                 return ENXIO;
93
94         error = (*sw->probe)(unit, &sc->adp, NULL, flags);
95         if (error)
96                 return error;
97         return (*sw->init)(unit, sc->adp, flags);
98 }
99
100 /* cdev driver functions */
101
102 #ifdef FB_INSTALL_CDEV
103
104 struct ucred;
105
106 int
107 vga_open(cdev_t dev, vga_softc_t *sc, int flag, int mode, struct ucred *cred)
108 {
109         if (sc == NULL)
110                 return ENXIO;
111         if (mode & (O_CREAT | O_APPEND | O_TRUNC))
112                 return ENODEV;
113
114         return genfbopen(&sc->gensc, sc->adp, flag, mode, cred);
115 }
116
117 int
118 vga_close(cdev_t dev, vga_softc_t *sc, int flag, int mode)
119 {
120         return genfbclose(&sc->gensc, sc->adp, flag, mode);
121 }
122
123 int
124 vga_read(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
125 {
126         return genfbread(&sc->gensc, sc->adp, uio, flag);
127 }
128
129 int
130 vga_write(cdev_t dev, vga_softc_t *sc, struct uio *uio, int flag)
131 {
132         return genfbread(&sc->gensc, sc->adp, uio, flag);
133 }
134
135 int
136 vga_ioctl(cdev_t dev, vga_softc_t *sc, u_long cmd, caddr_t arg, int flag,
137           struct ucred *cred)
138 {
139         return genfbioctl(&sc->gensc, sc->adp, cmd, arg, flag, cred);
140 }
141
142 int
143 vga_mmap(cdev_t dev, vga_softc_t *sc, vm_offset_t offset, int prot)
144 {
145         return genfbmmap(&sc->gensc, sc->adp, offset, prot);
146 }
147
148 #endif /* FB_INSTALL_CDEV */
149
150 /* LOW-LEVEL */
151
152 #define probe_done(adp)         ((adp)->va_flags & V_ADP_PROBED)
153 #define init_done(adp)          ((adp)->va_flags & V_ADP_INITIALIZED)
154 #define config_done(adp)        ((adp)->va_flags & V_ADP_REGISTERED)
155
156 /* various sizes */
157 #define V_MODE_MAP_SIZE         (M_VGA_CG320 + 1)
158 #define V_MODE_PARAM_SIZE       64
159
160 /* video adapter state buffer */
161 struct adp_state {
162     int                 sig;
163 #define V_STATE_SIG     0x736f6962
164     u_char              regs[V_MODE_PARAM_SIZE];
165 };
166 typedef struct adp_state adp_state_t;
167
168 /* 
169  * NOTE: `va_window' should have a virtual address, but is initialized
170  * with a physical address in the following table, as verify_adapter()
171  * will perform address conversion at run-time.
172  */
173 static video_adapter_t biosadapter = {
174     0, KD_VGA, VGA_DRIVER_NAME, 0, 0, V_ADP_COLOR, IO_VGA, 32,
175     EGA_BUF_BASE, EGA_BUF_SIZE, CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 
176     0, 0, 0, M_VGA_C80x25, M_C80x25, M_VGA_C80x25
177 };
178
179 /* video driver declarations */
180 static int                      vga_configure(int flags);
181        int                      (*vga_sub_configure)(int flags);
182 #if 0
183 static int                      vga_nop(void);
184 #endif
185 static int                      vga_error(void);
186 static vi_probe_t               vga_probe;
187 static vi_init_t                vga_init;
188 static vi_get_info_t            vga_get_info;
189 static vi_query_mode_t          vga_query_mode;
190 static vi_set_mode_t            vga_set_mode;
191 static vi_save_font_t           vga_save_font;
192 static vi_load_font_t           vga_load_font;
193 static vi_show_font_t           vga_show_font;
194 static vi_save_palette_t        vga_save_palette;
195 static vi_load_palette_t        vga_load_palette;
196 static vi_set_border_t          vga_set_border;
197 static vi_save_state_t          vga_save_state;
198 static vi_load_state_t          vga_load_state;
199 static vi_set_win_org_t         vga_set_origin;
200 static vi_read_hw_cursor_t      vga_read_hw_cursor;
201 static vi_set_hw_cursor_t       vga_set_hw_cursor;
202 static vi_set_hw_cursor_shape_t vga_set_hw_cursor_shape;
203 static vi_blank_display_t       vga_blank_display;
204 static vi_mmap_t                vga_mmap_buf;
205 static vi_ioctl_t               vga_dev_ioctl;
206 #ifndef VGA_NO_MODE_CHANGE
207 static vi_clear_t               vga_clear;
208 static vi_fill_rect_t           vga_fill_rect;
209 static vi_bitblt_t              vga_bitblt;
210 #else /* VGA_NO_MODE_CHANGE */
211 #define vga_clear               (vi_clear_t *)vga_error
212 #define vga_fill_rect           (vi_fill_rect_t *)vga_error
213 #define vga_bitblt              (vi_bitblt_t *)vga_error
214 #endif
215 static vi_diag_t                vga_diag;
216
217 static video_switch_t vgavidsw = {
218         vga_probe,
219         vga_init,
220         vga_get_info,
221         vga_query_mode, 
222         vga_set_mode,
223         vga_save_font,
224         vga_load_font,
225         vga_show_font,
226         vga_save_palette,
227         vga_load_palette,
228         vga_set_border,
229         vga_save_state,
230         vga_load_state,
231         vga_set_origin,
232         vga_read_hw_cursor,
233         vga_set_hw_cursor,
234         vga_set_hw_cursor_shape,
235         vga_blank_display,
236         vga_mmap_buf,
237         vga_dev_ioctl,
238         vga_clear,
239         vga_fill_rect,
240         vga_bitblt,
241         vga_error,
242         vga_error,
243         vga_diag,
244 };
245
246 VIDEO_DRIVER(vga, vgavidsw, vga_configure);
247
248 /* VGA BIOS standard video modes */
249 #define EOT             (-1)
250 #define NA              (-2)
251
252 static video_info_t bios_vmode[] = {
253     /* CGA */
254     { M_B40x25,     V_INFO_COLOR, 40, 25, 8,  8, 2, 1,
255       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
256     { M_C40x25,     V_INFO_COLOR, 40, 25, 8,  8, 4, 1,
257       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
258     { M_B80x25,     V_INFO_COLOR, 80, 25, 8,  8, 2, 1,
259       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
260     { M_C80x25,     V_INFO_COLOR, 80, 25, 8,  8, 4, 1,
261       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
262     /* EGA */
263     { M_ENH_B40x25, V_INFO_COLOR, 40, 25, 8, 14, 2, 1,
264       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
265     { M_ENH_C40x25, V_INFO_COLOR, 40, 25, 8, 14, 4, 1,
266       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
267     { M_ENH_B80x25, V_INFO_COLOR, 80, 25, 8, 14, 2, 1,
268       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
269     { M_ENH_C80x25, V_INFO_COLOR, 80, 25, 8, 14, 4, 1,
270       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
271     /* VGA */
272     { M_VGA_C40x25, V_INFO_COLOR, 40, 25, 8, 16, 4, 1,
273       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
274     { M_VGA_M80x25, 0,            80, 25, 8, 16, 2, 1,
275       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
276     { M_VGA_C80x25, V_INFO_COLOR, 80, 25, 8, 16, 4, 1,
277       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
278     /* MDA */
279     { M_EGAMONO80x25, 0,          80, 25, 8, 14, 2, 1,
280       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
281     /* EGA */
282     { M_ENH_B80x43, 0,            80, 43, 8,  8, 2, 1,
283       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
284     { M_ENH_C80x43, V_INFO_COLOR, 80, 43, 8,  8, 4, 1,
285       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
286     /* VGA */
287     { M_VGA_M80x30, 0,            80, 30, 8, 16, 2, 1,
288       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
289     { M_VGA_C80x30, V_INFO_COLOR, 80, 30, 8, 16, 4, 1,
290       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
291     { M_VGA_M80x50, 0,            80, 50, 8,  8, 2, 1,
292       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
293     { M_VGA_C80x50, V_INFO_COLOR, 80, 50, 8,  8, 4, 1,
294       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
295     { M_VGA_M80x60, 0,            80, 60, 8,  8, 2, 1,
296       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
297     { M_VGA_C80x60, V_INFO_COLOR, 80, 60, 8,  8, 4, 1,
298       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
299
300 #ifndef VGA_NO_MODE_CHANGE
301
302 #ifdef VGA_WIDTH90
303     { M_VGA_M90x25, 0,            90, 25, 8, 16, 2, 1,
304       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
305     { M_VGA_C90x25, V_INFO_COLOR, 90, 25, 8, 16, 4, 1,
306       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
307     { M_VGA_M90x30, 0,            90, 30, 8, 16, 2, 1,
308       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
309     { M_VGA_C90x30, V_INFO_COLOR, 90, 30, 8, 16, 4, 1,
310       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
311     { M_VGA_M90x43, 0,            90, 43, 8,  8, 2, 1,
312       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
313     { M_VGA_C90x43, V_INFO_COLOR, 90, 43, 8,  8, 4, 1,
314       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
315     { M_VGA_M90x50, 0,            90, 50, 8,  8, 2, 1,
316       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
317     { M_VGA_C90x50, V_INFO_COLOR, 90, 50, 8,  8, 4, 1,
318       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
319     { M_VGA_M90x60, 0,            90, 60, 8,  8, 2, 1,
320       MDA_BUF_BASE, MDA_BUF_SIZE, MDA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
321     { M_VGA_C90x60, V_INFO_COLOR, 90, 60, 8,  8, 4, 1,
322       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_TEXT },
323 #endif /* VGA_WIDTH90 */
324
325     /* CGA */
326     { M_BG320,      V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 2, 1,
327       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
328     { M_CG320,      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_BG640,      V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 1, 1,
331       CGA_BUF_BASE, CGA_BUF_SIZE, CGA_BUF_SIZE, 0, 0, V_INFO_MM_CGA },
332     /* EGA */
333     { M_CG320_D,    V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 4, 4,
334       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
335       V_INFO_MM_PLANAR },
336     { M_CG640_E,    V_INFO_COLOR | V_INFO_GRAPHICS, 640, 200, 8,  8, 4, 4,
337       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
338       V_INFO_MM_PLANAR },
339     { M_EGAMONOAPA, V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
340       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, 64*1024, 0, 0 ,
341       V_INFO_MM_PLANAR },
342     { M_ENHMONOAPA2,V_INFO_GRAPHICS,                640, 350, 8, 14, 4, 4,
343       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
344       V_INFO_MM_PLANAR },
345     { M_CG640x350,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 2, 2,
346       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
347       V_INFO_MM_PLANAR },
348     { M_ENH_CG640,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 350, 8, 14, 4, 4,
349       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
350       V_INFO_MM_PLANAR },
351     /* VGA */
352     { M_BG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
353       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
354       V_INFO_MM_PLANAR },
355     { M_CG640x480,  V_INFO_COLOR | V_INFO_GRAPHICS, 640, 480, 8, 16, 4, 4,
356       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0 ,
357       V_INFO_MM_PLANAR },
358     { M_VGA_CG320,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 200, 8,  8, 8, 1,
359       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
360       V_INFO_MM_PACKED, 1 },
361     { M_VGA_MODEX,  V_INFO_COLOR | V_INFO_GRAPHICS, 320, 240, 8,  8, 8, 4,
362       GRAPHICS_BUF_BASE, GRAPHICS_BUF_SIZE, GRAPHICS_BUF_SIZE, 0, 0,
363       V_INFO_MM_VGAX, 1 },
364 #endif /* VGA_NO_MODE_CHANGE */
365
366     { EOT },
367 };
368
369 static int              vga_init_done = FALSE;
370 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
371 static u_char           *video_mode_ptr = NULL;
372 #endif
373 static u_char           *mode_map[V_MODE_MAP_SIZE];
374 static adp_state_t      adpstate;
375 static adp_state_t      adpstate2;
376 static int              rows_offset = 1;
377
378 /* local macros and functions */
379 #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
380
381 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
382 static void map_mode_table(u_char **, u_char *);
383 static int map_mode_num(int);
384 #endif
385 static int map_bios_mode_num(int);
386 static u_char *get_mode_param(int);
387 static int verify_adapter(video_adapter_t *);
388 static void update_adapter_info(video_adapter_t *, video_info_t *);
389 static int probe_adapters(void);
390 static int set_line_length(video_adapter_t *, int);
391 static int set_display_start(video_adapter_t *, int, int);
392
393 #ifndef VGA_NO_MODE_CHANGE
394 #ifdef VGA_WIDTH90
395 static void set_width90(adp_state_t *);
396 #endif
397 #endif /* !VGA_NO_MODE_CHANGE */
398
399 #ifndef VGA_NO_FONT_LOADING
400 #define PARAM_BUFSIZE   6
401 static void set_font_mode(video_adapter_t *, u_char *);
402 static void set_normal_mode(video_adapter_t *, u_char *);
403 #endif
404
405 #ifndef VGA_NO_MODE_CHANGE
406 static void filll_io(int, vm_offset_t, size_t);
407 static void planar_fill(video_adapter_t *, int);
408 static void packed_fill(video_adapter_t *, int);
409 static void direct_fill(video_adapter_t *, int);
410 #ifdef notyet
411 static void planar_fill_rect(video_adapter_t *, int, int, int, int, int);
412 static void packed_fill_rect(video_adapter_t *, int, int, int, int, int);
413 static void direct_fill_rect16(video_adapter_t *, int, int, int, int, int);
414 static void direct_fill_rect24(video_adapter_t *, int, int, int, int, int);
415 static void direct_fill_rect32(video_adapter_t *, int, int, int, int, int);
416 #endif /* notyet */
417 #endif /* !VGA_NO_MODE_CHANGE */
418
419 static void dump_buffer(u_char *, size_t);
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 < sizeof(mode_map)/sizeof(mode_map[0]); ++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 < sizeof(vga_modes)/sizeof(vga_modes[0]))
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 #if VGA_DEBUG > 1
1039     kprintf("vga_set_mode(): setting mode %d\n", mode);
1040 #endif
1041
1042     params.sig = V_STATE_SIG;
1043     bcopy(get_mode_param(mode), params.regs, sizeof(params.regs));
1044
1045     switch (mode) {
1046 #ifdef VGA_WIDTH90
1047     case M_VGA_C90x60: case M_VGA_M90x60:
1048         set_width90(&params);
1049         /* FALLTHROUGH */
1050 #endif
1051     case M_VGA_C80x60: case M_VGA_M80x60:
1052         params.regs[2]  = 0x08;
1053         params.regs[19] = 0x47;
1054         goto special_480l;
1055
1056 #ifdef VGA_WIDTH90
1057     case M_VGA_C90x30: case M_VGA_M90x30:
1058         set_width90(&params);
1059         /* FALLTHROUGH */
1060 #endif
1061     case M_VGA_C80x30: case M_VGA_M80x30:
1062         params.regs[19] = 0x4f;
1063 special_480l:
1064         params.regs[9] |= 0xc0;
1065         params.regs[16] = 0x08;
1066         params.regs[17] = 0x3e;
1067         params.regs[26] = 0xea;
1068         params.regs[28] = 0xdf;
1069         params.regs[31] = 0xe7;
1070         params.regs[32] = 0x04;
1071         goto setup_mode;
1072
1073 #ifdef VGA_WIDTH90
1074     case M_VGA_C90x43: case M_VGA_M90x43:
1075         set_width90(&params);
1076         /* FALLTHROUGH */
1077 #endif
1078     case M_ENH_C80x43: case M_ENH_B80x43:
1079         params.regs[28] = 87;
1080         goto special_80x50;
1081
1082 #ifdef VGA_WIDTH90
1083     case M_VGA_C90x50: case M_VGA_M90x50:
1084         set_width90(&params);
1085         /* FALLTHROUGH */
1086 #endif
1087     case M_VGA_C80x50: case M_VGA_M80x50:
1088 special_80x50:
1089         params.regs[2] = 8;
1090         params.regs[19] = 7;
1091         goto setup_mode;
1092
1093 #ifdef VGA_WIDTH90
1094     case M_VGA_C90x25: case M_VGA_M90x25:
1095         set_width90(&params);
1096         /* FALLTHROUGH */
1097 #endif
1098     case M_VGA_C40x25: case M_VGA_C80x25:
1099     case M_VGA_M80x25:
1100     case M_B40x25:     case M_C40x25:
1101     case M_B80x25:     case M_C80x25:
1102     case M_ENH_B40x25: case M_ENH_C40x25:
1103     case M_ENH_B80x25: case M_ENH_C80x25:
1104     case M_EGAMONO80x25:
1105
1106 setup_mode:
1107         vga_load_state(adp, &params);
1108         break;
1109
1110     case M_VGA_MODEX:
1111         /* "unchain" the VGA mode */
1112         params.regs[5-1+0x04] &= 0xf7;
1113         params.regs[5-1+0x04] |= 0x04;
1114         /* turn off doubleword mode */
1115         params.regs[10+0x14] &= 0xbf;
1116         /* turn off word addressing */
1117         params.regs[10+0x17] |= 0x40;
1118         /* set logical screen width */
1119         params.regs[10+0x13] = 80;
1120         /* set 240 lines */
1121         params.regs[10+0x11] = 0x2c;
1122         params.regs[10+0x06] = 0x0d;
1123         params.regs[10+0x07] = 0x3e;
1124         params.regs[10+0x10] = 0xea;
1125         params.regs[10+0x11] = 0xac;
1126         params.regs[10+0x12] = 0xdf;
1127         params.regs[10+0x15] = 0xe7;
1128         params.regs[10+0x16] = 0x06;
1129         /* set vertical sync polarity to reflect aspect ratio */
1130         params.regs[9] = 0xe3;
1131         goto setup_grmode;
1132
1133     case M_BG320:     case M_CG320:     case M_BG640:
1134     case M_CG320_D:   case M_CG640_E:
1135     case M_CG640x350: case M_ENH_CG640:
1136     case M_BG640x480: case M_CG640x480: case M_VGA_CG320:
1137
1138 setup_grmode:
1139         vga_load_state(adp, &params);
1140         break;
1141
1142     default:
1143         return EINVAL;
1144     }
1145
1146     adp->va_mode = mode;
1147     info.vi_flags &= ~V_INFO_LINEAR; /* XXX */
1148     update_adapter_info(adp, &info);
1149
1150     /* move hardware cursor out of the way */
1151     (*vidsw[adp->va_index]->set_hw_cursor)(adp, -1, -1);
1152
1153     return 0;
1154 #else /* VGA_NO_MODE_CHANGE */
1155     return ENODEV;
1156 #endif /* VGA_NO_MODE_CHANGE */
1157 }
1158
1159 #ifndef VGA_NO_FONT_LOADING
1160
1161 static void
1162 set_font_mode(video_adapter_t *adp, u_char *buf)
1163 {
1164     crit_enter();
1165
1166     /* save register values */
1167     outb(TSIDX, 0x02); buf[0] = inb(TSREG);
1168     outb(TSIDX, 0x04); buf[1] = inb(TSREG);
1169     outb(GDCIDX, 0x04); buf[2] = inb(GDCREG);
1170     outb(GDCIDX, 0x05); buf[3] = inb(GDCREG);
1171     outb(GDCIDX, 0x06); buf[4] = inb(GDCREG);
1172     inb(CRTC + 6);
1173     outb(ATC, 0x10); buf[5] = inb(ATC + 1);
1174
1175     /* setup vga for loading fonts */
1176     inb(CRTC + 6);                              /* reset flip-flop */
1177     outb(ATC, 0x10); outb(ATC, buf[5] & ~0x01);
1178     inb(CRTC + 6);                              /* reset flip-flop */
1179     outb(ATC, 0x20);                            /* enable palette */
1180
1181 #ifdef VGA_ALT_SEQACCESS
1182     outw(TSIDX, 0x0100);
1183 #endif
1184     outw(TSIDX, 0x0402);
1185     outw(TSIDX, 0x0704);
1186 #ifdef VGA_ALT_SEQACCESS
1187     outw(TSIDX, 0x0300);
1188 #endif
1189     outw(GDCIDX, 0x0204);
1190     outw(GDCIDX, 0x0005);
1191     outw(GDCIDX, 0x0406);               /* addr = a0000, 64kb */
1192
1193     crit_exit();
1194 }
1195
1196 static void
1197 set_normal_mode(video_adapter_t *adp, u_char *buf)
1198 {
1199     crit_enter();
1200
1201     /* setup vga for normal operation mode again */
1202     inb(CRTC + 6);                              /* reset flip-flop */
1203     outb(ATC, 0x10); outb(ATC, buf[5]);
1204     inb(CRTC + 6);                              /* reset flip-flop */
1205     outb(ATC, 0x20);                            /* enable palette */
1206
1207 #ifdef VGA_ALT_SEQACCESS
1208     outw(TSIDX, 0x0100);
1209 #endif
1210     outw(TSIDX, 0x0002 | (buf[0] << 8));
1211     outw(TSIDX, 0x0004 | (buf[1] << 8));
1212 #ifdef VGA_ALT_SEQACCESS
1213     outw(TSIDX, 0x0300);
1214 #endif
1215     outw(GDCIDX, 0x0004 | (buf[2] << 8));
1216     outw(GDCIDX, 0x0005 | (buf[3] << 8));
1217     outw(GDCIDX, 0x0006 | (((buf[4] & 0x03) | 0x0c)<<8));
1218
1219     crit_exit();
1220 }
1221
1222 #endif /* VGA_NO_FONT_LOADING */
1223
1224 /*
1225  * save_font():
1226  * Read the font data in the requested font page from the video adapter.
1227  */
1228 static int
1229 vga_save_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1230               int ch, int count)
1231 {
1232 #ifndef VGA_NO_FONT_LOADING
1233     u_char buf[PARAM_BUFSIZE];
1234     vm_offset_t segment;
1235     int c;
1236 #ifdef VGA_ALT_SEQACCESS
1237     u_char val = 0;
1238 #endif
1239
1240     prologue(adp, V_ADP_FONT, ENODEV);
1241
1242     if (fontsize < 14) {
1243         /* FONT_8 */
1244         fontsize = 8;
1245     } else if (fontsize >= 32) {
1246         fontsize = 32;
1247     } else if (fontsize >= 16) {
1248         /* FONT_16 */
1249         fontsize = 16;
1250     } else {
1251         /* FONT_14 */
1252         fontsize = 14;
1253     }
1254
1255     if (page < 0 || page >= 8)
1256         return EINVAL;
1257     segment = FONT_BUF + 0x4000*page;
1258     if (page > 3)
1259         segment -= 0xe000;
1260
1261 #ifdef VGA_ALT_SEQACCESS
1262     crit_enter();
1263     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1264     outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
1265     outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1266     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1267     crit_exit();
1268 #endif
1269
1270     set_font_mode(adp, buf);
1271     if (fontsize == 32) {
1272         bcopy_fromio((uintptr_t)segment + ch*32, data, fontsize*count);
1273     } else {
1274         for (c = ch; count > 0; ++c, --count) {
1275             bcopy_fromio((uintptr_t)segment + c*32, data, fontsize);
1276             data += fontsize;
1277         }
1278     }
1279     set_normal_mode(adp, buf);
1280
1281 #ifdef VGA_ALT_SEQACCESS
1282     crit_enter();
1283     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1284     outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1285     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1286     crit_exit();
1287 #endif
1288
1289     return 0;
1290 #else /* VGA_NO_FONT_LOADING */
1291     return ENODEV;
1292 #endif /* VGA_NO_FONT_LOADING */
1293 }
1294
1295 /*
1296  * load_font():
1297  * Set the font data in the requested font page.
1298  * NOTE: it appears that some recent video adapters do not support
1299  * the font page other than 0... XXX
1300  */
1301 static int
1302 vga_load_font(video_adapter_t *adp, int page, int fontsize, u_char *data,
1303               int ch, int count)
1304 {
1305 #ifndef VGA_NO_FONT_LOADING
1306     u_char buf[PARAM_BUFSIZE];
1307     vm_offset_t segment;
1308     int c;
1309 #ifdef VGA_ALT_SEQACCESS
1310     u_char val = 0;
1311 #endif
1312
1313     prologue(adp, V_ADP_FONT, ENODEV);
1314
1315     if (fontsize < 14) {
1316         /* FONT_8 */
1317         fontsize = 8;
1318     } else if (fontsize >= 32) {
1319         fontsize = 32;
1320     } else if (fontsize >= 16) {
1321         /* FONT_16 */
1322         fontsize = 16;
1323     } else {
1324         /* FONT_14 */
1325         fontsize = 14;
1326     }
1327
1328     if (page < 0 || page >= 8)
1329         return EINVAL;
1330     segment = FONT_BUF + 0x4000*page;
1331     if (page > 3)
1332         segment -= 0xe000;
1333
1334 #ifdef VGA_ALT_SEQACCESS
1335     crit_enter();
1336     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1337     outb(TSIDX, 0x01); val = inb(TSREG);        /* disable screen */
1338     outb(TSIDX, 0x01); outb(TSREG, val | 0x20);
1339     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1340     crit_exit();
1341 #endif
1342
1343     set_font_mode(adp, buf);
1344     if (fontsize == 32) {
1345         bcopy_toio(data, (uintptr_t)segment + ch*32, fontsize*count);
1346     } else {
1347         for (c = ch; count > 0; ++c, --count) {
1348             bcopy_toio(data, (uintptr_t)segment + c*32, fontsize);
1349             data += fontsize;
1350         }
1351     }
1352     set_normal_mode(adp, buf);
1353
1354 #ifdef VGA_ALT_SEQACCESS
1355     crit_enter();
1356     outb(TSIDX, 0x00); outb(TSREG, 0x01);
1357     outb(TSIDX, 0x01); outb(TSREG, val & 0xdf); /* enable screen */
1358     outb(TSIDX, 0x00); outb(TSREG, 0x03);
1359     crit_exit();
1360 #endif
1361
1362     return 0;
1363 #else /* VGA_NO_FONT_LOADING */
1364     return ENODEV;
1365 #endif /* VGA_NO_FONT_LOADING */
1366 }
1367
1368 /*
1369  * show_font():
1370  * Activate the requested font page.
1371  * NOTE: it appears that some recent video adapters do not support
1372  * the font page other than 0... XXX
1373  */
1374 static int
1375 vga_show_font(video_adapter_t *adp, int page)
1376 {
1377 #ifndef VGA_NO_FONT_LOADING
1378     static u_char cg[] = { 0x00, 0x05, 0x0a, 0x0f, 0x30, 0x35, 0x3a, 0x3f };
1379
1380     prologue(adp, V_ADP_FONT, ENODEV);
1381     if (page < 0 || page >= 8)
1382         return EINVAL;
1383
1384     crit_enter();
1385     outb(TSIDX, 0x03); outb(TSREG, cg[page]);
1386     crit_exit();
1387
1388     return 0;
1389 #else /* VGA_NO_FONT_LOADING */
1390     return ENODEV;
1391 #endif /* VGA_NO_FONT_LOADING */
1392 }
1393
1394 /*
1395  * save_palette():
1396  * Read DAC values. The values have expressed in 8 bits.
1397  *
1398  * VGA
1399  */
1400 static int
1401 vga_save_palette(video_adapter_t *adp, u_char *palette)
1402 {
1403     int i;
1404
1405     prologue(adp, V_ADP_PALETTE, ENODEV);
1406
1407     /* 
1408      * We store 8 bit values in the palette buffer, while the standard
1409      * VGA has 6 bit DAC .
1410      */
1411     outb(PALRADR, 0x00);
1412     for (i = 0; i < 256*3; ++i)
1413         palette[i] = inb(PALDATA) << 2; 
1414     inb(CRTC + 6);              /* reset flip/flop */
1415     return 0;
1416 }
1417
1418 static int
1419 vga_save_palette2(video_adapter_t *adp, int base, int count,
1420                   u_char *r, u_char *g, u_char *b)
1421 {
1422     int i;
1423
1424     prologue(adp, V_ADP_PALETTE, ENODEV);
1425
1426     outb(PALRADR, base);
1427     for (i = 0; i < count; ++i) {
1428         r[i] = inb(PALDATA) << 2; 
1429         g[i] = inb(PALDATA) << 2; 
1430         b[i] = inb(PALDATA) << 2; 
1431     }
1432     inb(CRTC + 6);              /* reset flip/flop */
1433     return 0;
1434 }
1435
1436 /*
1437  * load_palette():
1438  * Set DAC values.
1439  *
1440  * VGA
1441  */
1442 static int
1443 vga_load_palette(video_adapter_t *adp, const u_char *palette)
1444 {
1445     int i;
1446
1447     prologue(adp, V_ADP_PALETTE, ENODEV);
1448
1449     outb(PIXMASK, 0xff);                /* no pixelmask */
1450     outb(PALWADR, 0x00);
1451     for (i = 0; i < 256*3; ++i)
1452         outb(PALDATA, palette[i] >> 2);
1453     inb(CRTC + 6);                      /* reset flip/flop */
1454     outb(ATC, 0x20);                    /* enable palette */
1455     return 0;
1456 }
1457
1458 static int
1459 vga_load_palette2(video_adapter_t *adp, int base, int count,
1460                   u_char *r, u_char *g, u_char *b)
1461 {
1462     int i;
1463
1464     prologue(adp, V_ADP_PALETTE, ENODEV);
1465
1466     outb(PIXMASK, 0xff);                /* no pixelmask */
1467     outb(PALWADR, base);
1468     for (i = 0; i < count; ++i) {
1469         outb(PALDATA, r[i] >> 2);
1470         outb(PALDATA, g[i] >> 2);
1471         outb(PALDATA, b[i] >> 2);
1472     }
1473     inb(CRTC + 6);                      /* reset flip/flop */
1474     outb(ATC, 0x20);                    /* enable palette */
1475     return 0;
1476 }
1477
1478 /*
1479  * set_border():
1480  * Change the border color.
1481  */
1482 static int
1483 vga_set_border(video_adapter_t *adp, int color)
1484 {
1485     prologue(adp, V_ADP_BORDER, ENODEV);
1486
1487     inb(CRTC + 6);      /* reset flip-flop */
1488     outb(ATC, 0x31); outb(ATC, color & 0xff); 
1489
1490     return 0;
1491 }
1492
1493 /*
1494  * save_state():
1495  * Read video register values.
1496  * NOTE: this function only reads the standard VGA registers.
1497  * any extra/extended registers of SVGA adapters are not saved.
1498  */
1499 static int
1500 vga_save_state(video_adapter_t *adp, void *p, size_t size)
1501 {
1502     video_info_t info;
1503     u_char *buf;
1504     int crtc_addr;
1505     int i, j;
1506
1507     if (size == 0) {
1508         /* return the required buffer size */
1509         prologue(adp, V_ADP_STATESAVE, 0);
1510         return sizeof(adp_state_t);
1511     } else {
1512         prologue(adp, V_ADP_STATESAVE, ENODEV);
1513         if (size < sizeof(adp_state_t))
1514             return EINVAL;
1515     }
1516     ((adp_state_t *)p)->sig = V_STATE_SIG;
1517     buf = ((adp_state_t *)p)->regs;
1518     bzero(buf, V_MODE_PARAM_SIZE);
1519     crtc_addr = CRTC;
1520
1521     crit_enter();
1522
1523     outb(TSIDX, 0x00); outb(TSREG, 0x01);       /* stop sequencer */
1524     for (i = 0, j = 5; i < 4; i++) {           
1525         outb(TSIDX, i + 1);
1526         buf[j++]  =  inb(TSREG);
1527     }
1528     buf[9]  =  inb(MISC + 10);                  /* dot-clock */
1529     outb(TSIDX, 0x00); outb(TSREG, 0x03);       /* start sequencer */
1530
1531     for (i = 0, j = 10; i < 25; i++) {          /* crtc */
1532         outb(crtc_addr, i);
1533         buf[j++]  =  inb(crtc_addr + 1);
1534     }
1535     for (i = 0, j = 35; i < 20; i++) {          /* attribute ctrl */
1536         inb(crtc_addr + 6);                     /* reset flip-flop */
1537         outb(ATC, i);
1538         buf[j++]  =  inb(ATC + 1);
1539     }
1540     for (i = 0, j = 55; i < 9; i++) {           /* graph data ctrl */
1541         outb(GDCIDX, i);
1542         buf[j++]  =  inb(GDCREG);
1543     }
1544     inb(crtc_addr + 6);                         /* reset flip-flop */
1545     outb(ATC, 0x20);                            /* enable palette */
1546
1547     crit_exit();
1548
1549 #if 1
1550     if (vga_get_info(adp, adp->va_mode, &info) == 0) {
1551         if (info.vi_flags & V_INFO_GRAPHICS) {
1552             buf[0] = info.vi_width/info.vi_cwidth; /* COLS */
1553             buf[1] = info.vi_height/info.vi_cheight - 1; /* ROWS */
1554         } else {
1555             buf[0] = info.vi_width;             /* COLS */
1556             buf[1] = info.vi_height - 1;        /* ROWS */
1557         }
1558         buf[2] = info.vi_cheight;               /* POINTS */
1559     } else {
1560         /* XXX: shouldn't be happening... */
1561         kprintf("vga%d: %s: failed to obtain mode info. (vga_save_state())\n",
1562                adp->va_unit, adp->va_name);
1563     }
1564 #else
1565     buf[0] = readb(BIOS_PADDRTOVADDR(0x44a));   /* COLS */
1566     buf[1] = readb(BIOS_PADDRTOVADDR(0x484));   /* ROWS */
1567     buf[2] = readb(BIOS_PADDRTOVADDR(0x485));   /* POINTS */
1568     buf[3] = readb(BIOS_PADDRTOVADDR(0x44c));
1569     buf[4] = readb(BIOS_PADDRTOVADDR(0x44d));
1570 #endif
1571
1572     return 0;
1573 }
1574
1575 /*
1576  * load_state():
1577  * Set video registers at once.
1578  * NOTE: this function only updates the standard VGA registers.
1579  * any extra/extended registers of SVGA adapters are not changed.
1580  */
1581 static int
1582 vga_load_state(video_adapter_t *adp, void *p)
1583 {
1584     u_char *buf;
1585     int crtc_addr;
1586     int i;
1587
1588     prologue(adp, V_ADP_STATELOAD, ENODEV);
1589     if (((adp_state_t *)p)->sig != V_STATE_SIG)
1590         return EINVAL;
1591
1592     buf = ((adp_state_t *)p)->regs;
1593     crtc_addr = CRTC;
1594
1595 #if VGA_DEBUG > 1
1596     dump_buffer(buf, V_MODE_PARAM_SIZE);
1597 #endif
1598
1599     crit_enter();
1600
1601     outb(TSIDX, 0x00); outb(TSREG, 0x01);       /* stop sequencer */
1602     for (i = 0; i < 4; ++i) {                   /* program sequencer */
1603         outb(TSIDX, i + 1);
1604         outb(TSREG, buf[i + 5]);
1605     }
1606     outb(MISC, buf[9]);                         /* set dot-clock */
1607     outb(TSIDX, 0x00); outb(TSREG, 0x03);       /* start sequencer */
1608     outb(crtc_addr, 0x11);
1609     outb(crtc_addr + 1, inb(crtc_addr + 1) & 0x7F);
1610     for (i = 0; i < 25; ++i) {                  /* program crtc */
1611         outb(crtc_addr, i);
1612         outb(crtc_addr + 1, buf[i + 10]);
1613     }
1614     inb(crtc_addr+6);                           /* reset flip-flop */
1615     for (i = 0; i < 20; ++i) {                  /* program attribute ctrl */
1616         outb(ATC, i);
1617         outb(ATC, buf[i + 35]);
1618     }
1619     for (i = 0; i < 9; ++i) {                   /* program graph data ctrl */
1620         outb(GDCIDX, i);
1621         outb(GDCREG, buf[i + 55]);
1622     }
1623     inb(crtc_addr + 6);                         /* reset flip-flop */
1624     outb(ATC, 0x20);                            /* enable palette */
1625
1626 #if notyet /* a temporary workaround for kernel panic, XXX */
1627 #ifndef VGA_NO_BIOS
1628     if (adp->va_unit == V_ADP_PRIMARY) {
1629         writeb(BIOS_PADDRTOVADDR(0x44a), buf[0]);       /* COLS */
1630         writeb(BIOS_PADDRTOVADDR(0x484), buf[1] + rows_offset - 1); /* ROWS */
1631         writeb(BIOS_PADDRTOVADDR(0x485), buf[2]);       /* POINTS */
1632 #if 0
1633         writeb(BIOS_PADDRTOVADDR(0x44c), buf[3]);
1634         writeb(BIOS_PADDRTOVADDR(0x44d), buf[4]);
1635 #endif
1636     }
1637 #endif /* VGA_NO_BIOS */
1638 #endif /* notyet */
1639
1640     crit_exit();
1641     return 0;
1642 }
1643
1644 /*
1645  * set_origin():
1646  * Change the origin (window mapping) of the banked frame buffer.
1647  */
1648 static int
1649 vga_set_origin(video_adapter_t *adp, off_t offset)
1650 {
1651     /* 
1652      * The standard video modes do not require window mapping; 
1653      * always return error.
1654      */
1655     return ENODEV;
1656 }
1657
1658 /*
1659  * read_hw_cursor():
1660  * Read the position of the hardware text cursor.
1661  */
1662 static int
1663 vga_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1664 {
1665     u_int16_t off;
1666
1667     if (!vga_init_done)
1668         return ENXIO;
1669
1670     if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1671         return ENODEV;
1672
1673     crit_enter();
1674     outb(CRTC, 14);
1675     off = inb(CRTC + 1);
1676     outb(CRTC, 15);
1677     off = (off << 8) | inb(CRTC + 1);
1678     crit_exit();
1679
1680     *row = off / adp->va_info.vi_width;
1681     *col = off % adp->va_info.vi_width;
1682
1683     return 0;
1684 }
1685
1686 /*
1687  * set_hw_cursor():
1688  * Move the hardware text cursor.  If col and row are both -1, 
1689  * the cursor won't be shown.
1690  */
1691 static int
1692 vga_set_hw_cursor(video_adapter_t *adp, int col, int row)
1693 {
1694     u_int16_t off;
1695
1696     if (!vga_init_done)
1697         return ENXIO;
1698
1699     if ((col == -1) && (row == -1)) {
1700         off = -1;
1701     } else {
1702         if (adp->va_info.vi_flags & V_INFO_GRAPHICS)
1703             return ENODEV;
1704         off = row*adp->va_info.vi_width + col;
1705     }
1706
1707     crit_enter();
1708     outb(CRTC, 14);
1709     outb(CRTC + 1, off >> 8);
1710     outb(CRTC, 15);
1711     outb(CRTC + 1, off & 0x00ff);
1712     crit_exit();
1713
1714     return 0;
1715 }
1716
1717 /*
1718  * set_hw_cursor_shape():
1719  * Change the shape of the hardware text cursor. If the height is
1720  * zero or negative, the cursor won't be shown.
1721  */
1722 static int
1723 vga_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1724                         int celsize, int blink)
1725 {
1726     if (!vga_init_done)
1727         return ENXIO;
1728
1729     crit_enter();
1730     if (height <= 0) {
1731         /* make the cursor invisible */
1732         outb(CRTC, 10);
1733         outb(CRTC + 1, 32);
1734         outb(CRTC, 11);
1735         outb(CRTC + 1, 0);
1736     } else {
1737         outb(CRTC, 10);
1738         outb(CRTC + 1, celsize - base - height);
1739         outb(CRTC, 11);
1740         outb(CRTC + 1, celsize - base - 1);
1741     }
1742     crit_exit();
1743
1744     return 0;
1745 }
1746
1747 /*
1748  * blank_display()
1749  * Put the display in power save/power off mode.
1750  */
1751 static int
1752 vga_blank_display(video_adapter_t *adp, int mode)
1753 {
1754     u_char val;
1755
1756     crit_enter();
1757     switch (mode) {
1758     case V_DISPLAY_SUSPEND:
1759     case V_DISPLAY_STAND_BY:
1760         outb(TSIDX, 0x01);
1761         val = inb(TSREG);
1762         outb(TSIDX, 0x01);
1763         outb(TSREG, val | 0x20);
1764         outb(CRTC, 0x17);
1765         val = inb(CRTC + 1);
1766         outb(CRTC + 1, val & ~0x80);
1767         break;
1768     case V_DISPLAY_OFF:
1769         outb(TSIDX, 0x01);
1770         val = inb(TSREG);
1771         outb(TSIDX, 0x01);
1772         outb(TSREG, val | 0x20);
1773         break;
1774     case V_DISPLAY_ON:
1775         outb(TSIDX, 0x01);
1776         val = inb(TSREG);
1777         outb(TSIDX, 0x01);
1778         outb(TSREG, val & 0xDF);
1779         outb(CRTC, 0x17);
1780         val = inb(CRTC + 1);
1781         outb(CRTC + 1, val | 0x80);
1782         break;
1783     }
1784     crit_exit();
1785
1786     return 0;
1787 }
1788
1789 /*
1790  * mmap():
1791  * Mmap frame buffer.
1792  */
1793 static int
1794 vga_mmap_buf(video_adapter_t *adp, vm_offset_t offset, int prot)
1795 {
1796     if (adp->va_info.vi_flags & V_INFO_LINEAR)
1797         return -1;
1798
1799 #if VGA_DEBUG > 0
1800     kprintf("vga_mmap_buf(): window:0x%x, offset:%p\n",
1801            adp->va_info.vi_window, (void *)offset);
1802 #endif
1803
1804     /* XXX: is this correct? */
1805     if (offset > adp->va_window_size - PAGE_SIZE)
1806         return -1;
1807
1808 #if defined(__i386__)
1809     return i386_btop(adp->va_info.vi_window + offset);
1810 #elif defined(__x86_64__)
1811     return x86_64_btop(adp->va_info.vi_window + offset);
1812 #else
1813 #error "vga_mmap_buf needs to return something"
1814 #endif
1815 }
1816
1817 #ifndef VGA_NO_MODE_CHANGE
1818
1819 static void
1820 planar_fill(video_adapter_t *adp, int val)
1821 {
1822     int length;
1823     int at;                     /* position in the frame buffer */
1824     int l;
1825
1826     outw(GDCIDX, 0x0005);               /* read mode 0, write mode 0 */
1827     outw(GDCIDX, 0x0003);               /* data rotate/function select */
1828     outw(GDCIDX, 0x0f01);               /* set/reset enable */
1829     outw(GDCIDX, 0xff08);               /* bit mask */
1830     outw(GDCIDX, (val << 8) | 0x00);    /* set/reset */
1831     at = 0;
1832     length = adp->va_line_width*adp->va_info.vi_height;
1833     while (length > 0) {
1834         l = imin(length, adp->va_window_size);
1835         (*vidsw[adp->va_index]->set_win_org)(adp, at);
1836         bzero_io(adp->va_window, l);
1837         length -= l;
1838         at += l;
1839     }
1840     outw(GDCIDX, 0x0000);               /* set/reset */
1841     outw(GDCIDX, 0x0001);               /* set/reset enable */
1842 }
1843
1844 static void
1845 packed_fill(video_adapter_t *adp, int val)
1846 {
1847     int length;
1848     int at;                     /* position in the frame buffer */
1849     int l;
1850
1851     at = 0;
1852     length = adp->va_line_width*adp->va_info.vi_height;
1853     while (length > 0) {
1854         l = imin(length, adp->va_window_size);
1855         (*vidsw[adp->va_index]->set_win_org)(adp, at);
1856         fill_io(val, adp->va_window, l);
1857         length -= l;
1858         at += l;
1859     }
1860 }
1861
1862 static void
1863 direct_fill(video_adapter_t *adp, int val)
1864 {
1865     int length;
1866     int at;                     /* position in the frame buffer */
1867     int l;
1868
1869     at = 0;
1870     length = adp->va_line_width*adp->va_info.vi_height;
1871     while (length > 0) {
1872         l = imin(length, adp->va_window_size);
1873         (*vidsw[adp->va_index]->set_win_org)(adp, at);
1874         switch (adp->va_info.vi_pixel_size) {
1875         case sizeof(u_int16_t):
1876             fillw_io(val, adp->va_window, l/sizeof(u_int16_t));
1877             break;
1878         case 3:
1879             /* FIXME */
1880             break;
1881         case sizeof(u_int32_t):
1882             filll_io(val, adp->va_window, l/sizeof(u_int32_t));
1883             break;
1884         }
1885         length -= l;
1886         at += l;
1887     }
1888 }
1889
1890 static int
1891 vga_clear(video_adapter_t *adp)
1892 {
1893     switch (adp->va_info.vi_mem_model) {
1894     case V_INFO_MM_TEXT:
1895         /* do nothing? XXX */
1896         break;
1897     case V_INFO_MM_PLANAR:
1898         planar_fill(adp, 0);
1899         break;
1900     case V_INFO_MM_PACKED:
1901         packed_fill(adp, 0);
1902         break;
1903     case V_INFO_MM_DIRECT:
1904         direct_fill(adp, 0);
1905         break;
1906     }
1907     return 0;
1908 }
1909
1910 #ifdef notyet
1911 static void
1912 planar_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1913 {
1914     int banksize;
1915     int bank;
1916     int pos;
1917     int offset;                 /* offset within window */
1918     int bx;
1919     int l;
1920
1921     outw(GDCIDX, 0x0005);               /* read mode 0, write mode 0 */
1922     outw(GDCIDX, 0x0003);               /* data rotate/function select */
1923     outw(GDCIDX, 0x0f01);               /* set/reset enable */
1924     outw(GDCIDX, 0xff08);               /* bit mask */
1925     outw(GDCIDX, (val << 8) | 0x00); /* set/reset */
1926
1927     banksize = adp->va_window_size;
1928     bank = -1;
1929     while (cy > 0) {
1930         pos = adp->va_line_width*y + x/8;
1931         if (bank != pos/banksize) {
1932             (*vidsw[adp->va_index]->set_win_org)(adp, pos);
1933             bank = pos/banksize;
1934         }
1935         offset = pos%banksize;
1936         bx = (x + cx)/8 - x/8;
1937         if (x % 8) {
1938             outw(GDCIDX, ((0xff00 >> (x % 8)) & 0xff00) | 0x08);
1939             writeb(adp->va_window + offset, 0);
1940             ++offset;
1941             --bx;
1942             if (offset >= banksize) {
1943                 offset = 0;
1944                 ++bank;         /* next bank */
1945                 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
1946             }
1947             outw(GDCIDX, 0xff08);       /* bit mask */
1948         }
1949         while (bx > 0) {
1950             l = imin(bx, banksize);
1951             bzero_io(adp->va_window + offset, l);
1952             offset += l;
1953             bx -= l;
1954             if (offset >= banksize) {
1955                 offset = 0;
1956                 ++bank;         /* next bank */
1957                 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
1958             }
1959         }
1960         if ((x + cx) % 8) {
1961             outw(GDCIDX, (~(0xff00 >> ((x + cx) % 8)) & 0xff00) | 0x08);
1962             writeb(adp->va_window + offset, 0);
1963             ++offset;
1964             if (offset >= banksize) {
1965                 offset = 0;
1966                 ++bank;         /* next bank */
1967                 (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
1968             }
1969             outw(GDCIDX, 0xff08);       /* bit mask */
1970         }
1971         ++y;
1972         --cy;
1973     }
1974
1975     outw(GDCIDX, 0xff08);               /* bit mask */
1976     outw(GDCIDX, 0x0000);               /* set/reset */
1977     outw(GDCIDX, 0x0001);               /* set/reset enable */
1978 }
1979
1980 static void
1981 packed_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1982 {
1983     int banksize;
1984     int bank;
1985     int pos;
1986     int offset;                 /* offset within window */
1987     int end;
1988
1989     banksize = adp->va_window_size;
1990     bank = -1;
1991     cx *= adp->va_info.vi_pixel_size;
1992     while (cy > 0) {
1993         pos = adp->va_line_width*y + x*adp->va_info.vi_pixel_size;
1994         if (bank != pos/banksize) {
1995             (*vidsw[adp->va_index]->set_win_org)(adp, pos);
1996             bank = pos/banksize;
1997         }
1998         offset = pos%banksize;
1999         end = imin(offset + cx, banksize);
2000         fill_io(val, adp->va_window + offset,
2001                 (end - offset)/adp->va_info.vi_pixel_size);
2002         /* the line may cross the window boundary */
2003         if (offset + cx > banksize) {
2004             ++bank;             /* next bank */
2005             (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2006             end = offset + cx - banksize;
2007             fill_io(val, adp->va_window, end/adp->va_info.vi_pixel_size);
2008         }
2009         ++y;
2010         --cy;
2011     }
2012 }
2013
2014 static void
2015 direct_fill_rect16(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 end;
2022
2023     /*
2024      * XXX: the function assumes that banksize is a muliple of
2025      * sizeof(u_int16_t).
2026      */
2027     banksize = adp->va_window_size;
2028     bank = -1;
2029     cx *= sizeof(u_int16_t);
2030     while (cy > 0) {
2031         pos = adp->va_line_width*y + x*sizeof(u_int16_t);
2032         if (bank != pos/banksize) {
2033             (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2034             bank = pos/banksize;
2035         }
2036         offset = pos%banksize;
2037         end = imin(offset + cx, banksize);
2038         fillw_io(val, adp->va_window + offset,
2039                  (end - offset)/sizeof(u_int16_t));
2040         /* the line may cross the window boundary */
2041         if (offset + cx > banksize) {
2042             ++bank;             /* next bank */
2043             (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2044             end = offset + cx - banksize;
2045             fillw_io(val, adp->va_window, end/sizeof(u_int16_t));
2046         }
2047         ++y;
2048         --cy;
2049     }
2050 }
2051
2052 static void
2053 direct_fill_rect24(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2054 {
2055     int banksize;
2056     int bank;
2057     int pos;
2058     int offset;                 /* offset within window */
2059     int end;
2060     int i;
2061     int j;
2062     u_int8_t b[3];
2063
2064     b[0] = val & 0x0000ff;
2065     b[1] = (val >> 8) & 0x0000ff;
2066     b[2] = (val >> 16) & 0x0000ff;
2067     banksize = adp->va_window_size;
2068     bank = -1;
2069     cx *= 3;
2070     while (cy > 0) {
2071         pos = adp->va_line_width*y + x*3;
2072         if (bank != pos/banksize) {
2073             (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2074             bank = pos/banksize;
2075         }
2076         offset = pos%banksize;
2077         end = imin(offset + cx, banksize);
2078         for (i = 0, j = offset; j < end; i = (++i)%3, ++j) {
2079             writeb(adp->va_window + j, b[i]);
2080         }
2081         /* the line may cross the window boundary */
2082         if (offset + cx >= banksize) {
2083             ++bank;             /* next bank */
2084             (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2085             j = 0;
2086             end = offset + cx - banksize;
2087             for (; j < end; i = (++i)%3, ++j) {
2088                 writeb(adp->va_window + j, b[i]);
2089             }
2090         }
2091         ++y;
2092         --cy;
2093     }
2094 }
2095
2096 static void
2097 direct_fill_rect32(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2098 {
2099     int banksize;
2100     int bank;
2101     int pos;
2102     int offset;                 /* offset within window */
2103     int end;
2104
2105     /*
2106      * XXX: the function assumes that banksize is a muliple of
2107      * sizeof(u_int32_t).
2108      */
2109     banksize = adp->va_window_size;
2110     bank = -1;
2111     cx *= sizeof(u_int32_t);
2112     while (cy > 0) {
2113         pos = adp->va_line_width*y + x*sizeof(u_int32_t);
2114         if (bank != pos/banksize) {
2115             (*vidsw[adp->va_index]->set_win_org)(adp, pos);
2116             bank = pos/banksize;
2117         }
2118         offset = pos%banksize;
2119         end = imin(offset + cx, banksize);
2120         filll_io(val, adp->va_window + offset,
2121                  (end - offset)/sizeof(u_int32_t));
2122         /* the line may cross the window boundary */
2123         if (offset + cx > banksize) {
2124             ++bank;             /* next bank */
2125             (*vidsw[adp->va_index]->set_win_org)(adp, bank*banksize);
2126             end = offset + cx - banksize;
2127             filll_io(val, adp->va_window, end/sizeof(u_int32_t));
2128         }
2129         ++y;
2130         --cy;
2131     }
2132 }
2133
2134 static int
2135 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2136 {
2137     switch (adp->va_info.vi_mem_model) {
2138     case V_INFO_MM_TEXT:
2139         /* do nothing? XXX */
2140         break;
2141     case V_INFO_MM_PLANAR:
2142         planar_fill_rect(adp, val, x, y, cx, cy);
2143         break;
2144     case V_INFO_MM_PACKED:
2145         packed_fill_rect(adp, val, x, y, cx, cy);
2146         break;
2147     case V_INFO_MM_DIRECT:
2148         switch (adp->va_info.vi_pixel_size) {
2149         case sizeof(u_int16_t):
2150             direct_fill_rect16(adp, val, x, y, cx, cy);
2151             break;
2152         case 3:
2153             direct_fill_rect24(adp, val, x, y, cx, cy);
2154             break;
2155         case sizeof(u_int32_t):
2156             direct_fill_rect32(adp, val, x, y, cx, cy);
2157             break;
2158         }
2159         break;
2160     }
2161     return 0;
2162 }
2163 #else /* !notyet */
2164 static int
2165 vga_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
2166 {
2167     return ENODEV;
2168 }
2169 #endif /* notyet */
2170
2171 static int
2172 vga_bitblt(video_adapter_t *adp,...)
2173 {
2174     /* FIXME */
2175     return ENODEV;
2176 }
2177
2178 #endif /* !VGA_NO_MODE_CHANGE */
2179
2180 static int
2181 get_palette(video_adapter_t *adp, int base, int count,
2182             u_char *red, u_char *green, u_char *blue, u_char *trans)
2183 {
2184     u_char *r;
2185     u_char *g;
2186     u_char *b;
2187
2188     if (count < 0 || base < 0 || count > 256 || base > 256 ||
2189         base + count > 256)
2190         return EINVAL;
2191
2192     r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2193     g = r + count;
2194     b = g + count;
2195     if (vga_save_palette2(adp, base, count, r, g, b)) {
2196         kfree(r, M_DEVBUF);
2197         return ENODEV;
2198     }
2199     copyout(r, red, count);
2200     copyout(g, green, count);
2201     copyout(b, blue, count);
2202     if (trans != NULL) {
2203         bzero(r, count);
2204         copyout(r, trans, count);
2205     }
2206     kfree(r, M_DEVBUF);
2207
2208     return 0;
2209 }
2210
2211 static int
2212 set_palette(video_adapter_t *adp, int base, int count,
2213             u_char *red, u_char *green, u_char *blue, u_char *trans)
2214 {
2215     u_char *r;
2216     u_char *g;
2217     u_char *b;
2218     int err;
2219
2220     if (count < 0 || base < 0 || count > 256 || base > 256 ||
2221         base + count > 256)
2222         return EINVAL;
2223
2224     r = kmalloc(count*3, M_DEVBUF, M_WAITOK);
2225     g = r + count;
2226     b = g + count;
2227     err = copyin(red, r, count);
2228     if (!err)
2229         err = copyin(green, g, count);
2230     if (!err)
2231         err = copyin(blue, b, count);
2232     if (!err)
2233         err = vga_load_palette2(adp, base, count, r, g, b);
2234     kfree(r, M_DEVBUF);
2235
2236     return (err ? ENODEV : 0);
2237 }
2238
2239 static int
2240 vga_dev_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
2241 {
2242     switch (cmd) {
2243     case FBIO_GETWINORG:        /* get frame buffer window origin */
2244         *(u_int *)arg = 0;
2245         return 0;
2246
2247     case FBIO_SETWINORG:        /* set frame buffer window origin */
2248         return ENODEV;
2249
2250     case FBIO_SETDISPSTART:     /* set display start address */
2251         return (set_display_start(adp, 
2252                                   ((video_display_start_t *)arg)->x,
2253                                   ((video_display_start_t *)arg)->y)
2254                 ? ENODEV : 0);
2255
2256     case FBIO_SETLINEWIDTH:     /* set scan line length in pixel */
2257         return (set_line_length(adp, *(u_int *)arg) ? ENODEV : 0);
2258
2259     case FBIO_GETPALETTE:       /* get color palette */
2260         return get_palette(adp, ((video_color_palette_t *)arg)->index,
2261                            ((video_color_palette_t *)arg)->count,
2262                            ((video_color_palette_t *)arg)->red,
2263                            ((video_color_palette_t *)arg)->green,
2264                            ((video_color_palette_t *)arg)->blue,
2265                            ((video_color_palette_t *)arg)->transparent);
2266
2267     case FBIO_SETPALETTE:       /* set color palette */
2268         return set_palette(adp, ((video_color_palette_t *)arg)->index,
2269                            ((video_color_palette_t *)arg)->count,
2270                            ((video_color_palette_t *)arg)->red,
2271                            ((video_color_palette_t *)arg)->green,
2272                            ((video_color_palette_t *)arg)->blue,
2273                            ((video_color_palette_t *)arg)->transparent);
2274
2275     case FBIOGTYPE:             /* get frame buffer type info. */
2276         ((struct fbtype *)arg)->fb_type = fb_type(adp->va_type);
2277         ((struct fbtype *)arg)->fb_height = adp->va_info.vi_height;
2278         ((struct fbtype *)arg)->fb_width = adp->va_info.vi_width;
2279         ((struct fbtype *)arg)->fb_depth = adp->va_info.vi_depth;
2280         if ((adp->va_info.vi_depth <= 1) || (adp->va_info.vi_depth > 8))
2281             ((struct fbtype *)arg)->fb_cmsize = 0;
2282         else
2283             ((struct fbtype *)arg)->fb_cmsize = 1 << adp->va_info.vi_depth;
2284         ((struct fbtype *)arg)->fb_size = adp->va_buffer_size;
2285         return 0;
2286
2287     case FBIOGETCMAP:           /* get color palette */
2288         return get_palette(adp, ((struct fbcmap *)arg)->index,
2289                            ((struct fbcmap *)arg)->count,
2290                            ((struct fbcmap *)arg)->red,
2291                            ((struct fbcmap *)arg)->green,
2292                            ((struct fbcmap *)arg)->blue, NULL);
2293
2294     case FBIOPUTCMAP:           /* set color palette */
2295         return set_palette(adp, ((struct fbcmap *)arg)->index,
2296                            ((struct fbcmap *)arg)->count,
2297                            ((struct fbcmap *)arg)->red,
2298                            ((struct fbcmap *)arg)->green,
2299                            ((struct fbcmap *)arg)->blue, NULL);
2300
2301     default:
2302         return fb_commonioctl(adp, cmd, arg);
2303     }
2304 }
2305
2306 static void
2307 dump_buffer(u_char *buf, size_t len)
2308 {
2309     int i;
2310
2311     for(i = 0; i < len;) {
2312         kprintf("%02x ", buf[i]);
2313         if ((++i % 16) == 0)
2314             kprintf("\n");
2315     }
2316 }
2317
2318 /*
2319  * diag():
2320  * Print some information about the video adapter and video modes,
2321  * with requested level of details.
2322  */
2323 static int
2324 vga_diag(video_adapter_t *adp, int level)
2325 {
2326     u_char *mp;
2327 #if FB_DEBUG > 1
2328     video_info_t info;
2329     int i;
2330 #endif
2331
2332     if (!vga_init_done)
2333         return ENXIO;
2334
2335 #if FB_DEBUG > 1
2336 #ifndef VGA_NO_BIOS
2337     kprintf("vga: DCC code:0x%02x\n",
2338         readb(BIOS_PADDRTOVADDR(0x488)));
2339     kprintf("vga: CRTC:0x%x, video option:0x%02x, ",
2340         readw(BIOS_PADDRTOVADDR(0x463)),
2341         readb(BIOS_PADDRTOVADDR(0x487)));
2342     kprintf("rows:%d, cols:%d, font height:%d\n",
2343         readb(BIOS_PADDRTOVADDR(0x44a)),
2344         readb(BIOS_PADDRTOVADDR(0x484)) + 1,
2345         readb(BIOS_PADDRTOVADDR(0x485)));
2346 #endif /* VGA_NO_BIOS */
2347 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2348     kprintf("vga: param table:%p\n", video_mode_ptr);
2349     kprintf("vga: rows_offset:%d\n", rows_offset);
2350 #endif
2351 #endif /* FB_DEBUG > 1 */
2352
2353     fb_dump_adp_info(VGA_DRIVER_NAME, adp, level);
2354
2355 #if FB_DEBUG > 1
2356     if (adp->va_flags & V_ADP_MODECHANGE) {
2357         for (i = 0; bios_vmode[i].vi_mode != EOT; ++i) {
2358             if (bios_vmode[i].vi_mode == NA)
2359                 continue;
2360             if (get_mode_param(bios_vmode[i].vi_mode) == NULL)
2361                 continue;
2362             fb_dump_mode_info(VGA_DRIVER_NAME, adp, &bios_vmode[i], level);
2363         }
2364     } else {
2365         vga_get_info(adp, adp->va_initial_mode, &info); /* shouldn't fail */
2366         fb_dump_mode_info(VGA_DRIVER_NAME, adp, &info, level);
2367     }
2368 #endif /* FB_DEBUG > 1 */
2369
2370 #if !defined(VGA_NO_BIOS) && !defined(VGA_NO_MODE_CHANGE)
2371     if (video_mode_ptr == NULL)
2372         kprintf("vga%d: %s: WARNING: video mode switching is not "
2373                "fully supported on this adapter\n",
2374                adp->va_unit, adp->va_name);
2375 #endif
2376     if (level <= 0)
2377         return 0;
2378
2379     kprintf("VGA parameters upon power-up\n");
2380     dump_buffer(adpstate.regs, sizeof(adpstate.regs));
2381
2382     mp = get_mode_param(adp->va_initial_mode);
2383     if (mp == NULL)     /* this shouldn't be happening */
2384         return 0;
2385     kprintf("VGA parameters in BIOS for mode %d\n", adp->va_initial_mode);
2386     dump_buffer(adpstate2.regs, sizeof(adpstate2.regs));
2387     kprintf("VGA parameters to be used for mode %d\n", adp->va_initial_mode);
2388     dump_buffer(mp, V_MODE_PARAM_SIZE);
2389
2390     return 0;
2391 }