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