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