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