xref: /freebsd/sys/dev/fb/vesa.c (revision f45c063aa733372110aea20ec8185bb8b5c4438e)
1f45c063aSXin LI /*-
2f45c063aSXin LI  * Copyright (c) 1998 Kazutaka YOKOTA and Michael Smith
3f45c063aSXin LI  * All rights reserved.
4f45c063aSXin LI  *
5f45c063aSXin LI  * Redistribution and use in source and binary forms, with or without
6f45c063aSXin LI  * modification, are permitted provided that the following conditions
7f45c063aSXin LI  * are met:
8f45c063aSXin LI  * 1. Redistributions of source code must retain the above copyright
9f45c063aSXin LI  *    notice, this list of conditions and the following disclaimer as
10f45c063aSXin LI  *    the first lines of this file unmodified.
11f45c063aSXin LI  * 2. Redistributions in binary form must reproduce the above copyright
12f45c063aSXin LI  *    notice, this list of conditions and the following disclaimer in the
13f45c063aSXin LI  *    documentation and/or other materials provided with the distribution.
14f45c063aSXin LI  *
15f45c063aSXin LI  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16f45c063aSXin LI  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17f45c063aSXin LI  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18f45c063aSXin LI  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19f45c063aSXin LI  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20f45c063aSXin LI  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21f45c063aSXin LI  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22f45c063aSXin LI  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23f45c063aSXin LI  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24f45c063aSXin LI  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25f45c063aSXin LI  */
26f45c063aSXin LI 
27f45c063aSXin LI #include <sys/cdefs.h>
28f45c063aSXin LI __FBSDID("$FreeBSD$");
29f45c063aSXin LI 
30f45c063aSXin LI #include "opt_vga.h"
31f45c063aSXin LI #include "opt_vesa.h"
32f45c063aSXin LI 
33f45c063aSXin LI #ifndef VGA_NO_MODE_CHANGE
34f45c063aSXin LI 
35f45c063aSXin LI #include <sys/param.h>
36f45c063aSXin LI #include <sys/systm.h>
37f45c063aSXin LI #include <sys/kernel.h>
38f45c063aSXin LI #include <sys/module.h>
39f45c063aSXin LI #include <sys/malloc.h>
40f45c063aSXin LI #include <sys/fbio.h>
41f45c063aSXin LI 
42f45c063aSXin LI #include <vm/vm.h>
43f45c063aSXin LI #include <vm/vm_extern.h>
44f45c063aSXin LI #include <vm/vm_kern.h>
45f45c063aSXin LI #include <vm/vm_param.h>
46f45c063aSXin LI #include <vm/pmap.h>
47f45c063aSXin LI 
48f45c063aSXin LI #include <machine/md_var.h>
49f45c063aSXin LI #include <machine/vm86.h>
50f45c063aSXin LI #include <machine/pc/bios.h>
51f45c063aSXin LI #include <machine/pc/vesa.h>
52f45c063aSXin LI 
53f45c063aSXin LI #include <dev/fb/fbreg.h>
54f45c063aSXin LI #include <dev/fb/vgareg.h>
55f45c063aSXin LI 
56f45c063aSXin LI #ifndef __i386__
57f45c063aSXin LI #include <isa/isareg.h>
58f45c063aSXin LI #else
59f45c063aSXin LI #include <i386/isa/isa.h>
60f45c063aSXin LI #endif
61f45c063aSXin LI 
62f45c063aSXin LI #define	VESA_VIA_CLE266		"VIA CLE266\r\n"
63f45c063aSXin LI 
64f45c063aSXin LI #ifndef VESA_DEBUG
65f45c063aSXin LI #define VESA_DEBUG	0
66f45c063aSXin LI #endif
67f45c063aSXin LI 
68f45c063aSXin LI /* VESA video adapter state buffer stub */
69f45c063aSXin LI struct adp_state {
70f45c063aSXin LI 	int		sig;
71f45c063aSXin LI #define V_STATE_SIG	0x61736576
72f45c063aSXin LI 	u_char		regs[1];
73f45c063aSXin LI };
74f45c063aSXin LI typedef struct adp_state adp_state_t;
75f45c063aSXin LI 
76f45c063aSXin LI /* VESA video adapter */
77f45c063aSXin LI static video_adapter_t *vesa_adp = NULL;
78f45c063aSXin LI static int vesa_state_buf_size = 0;
79f45c063aSXin LI #define VESA_VM86_BUFSIZE	(3 * PAGE_SIZE)
80f45c063aSXin LI static void *vesa_vm86_buf;
81f45c063aSXin LI 
82f45c063aSXin LI /* VESA functions */
83f45c063aSXin LI #if 0
84f45c063aSXin LI static int			vesa_nop(void);
85f45c063aSXin LI #endif
86f45c063aSXin LI static int			vesa_error(void);
87f45c063aSXin LI static vi_probe_t		vesa_probe;
88f45c063aSXin LI static vi_init_t		vesa_init;
89f45c063aSXin LI static vi_get_info_t		vesa_get_info;
90f45c063aSXin LI static vi_query_mode_t		vesa_query_mode;
91f45c063aSXin LI static vi_set_mode_t		vesa_set_mode;
92f45c063aSXin LI static vi_save_font_t		vesa_save_font;
93f45c063aSXin LI static vi_load_font_t		vesa_load_font;
94f45c063aSXin LI static vi_show_font_t		vesa_show_font;
95f45c063aSXin LI static vi_save_palette_t	vesa_save_palette;
96f45c063aSXin LI static vi_load_palette_t	vesa_load_palette;
97f45c063aSXin LI static vi_set_border_t		vesa_set_border;
98f45c063aSXin LI static vi_save_state_t		vesa_save_state;
99f45c063aSXin LI static vi_load_state_t		vesa_load_state;
100f45c063aSXin LI static vi_set_win_org_t		vesa_set_origin;
101f45c063aSXin LI static vi_read_hw_cursor_t	vesa_read_hw_cursor;
102f45c063aSXin LI static vi_set_hw_cursor_t	vesa_set_hw_cursor;
103f45c063aSXin LI static vi_set_hw_cursor_shape_t	vesa_set_hw_cursor_shape;
104f45c063aSXin LI static vi_blank_display_t	vesa_blank_display;
105f45c063aSXin LI static vi_mmap_t		vesa_mmap;
106f45c063aSXin LI static vi_ioctl_t		vesa_ioctl;
107f45c063aSXin LI static vi_clear_t		vesa_clear;
108f45c063aSXin LI static vi_fill_rect_t		vesa_fill_rect;
109f45c063aSXin LI static vi_bitblt_t		vesa_bitblt;
110f45c063aSXin LI static vi_diag_t		vesa_diag;
111f45c063aSXin LI static int			vesa_bios_info(int level);
112f45c063aSXin LI static struct vm86context	vesa_vmcontext;
113f45c063aSXin LI 
114f45c063aSXin LI static video_switch_t vesavidsw = {
115f45c063aSXin LI 	vesa_probe,
116f45c063aSXin LI 	vesa_init,
117f45c063aSXin LI 	vesa_get_info,
118f45c063aSXin LI 	vesa_query_mode,
119f45c063aSXin LI 	vesa_set_mode,
120f45c063aSXin LI 	vesa_save_font,
121f45c063aSXin LI 	vesa_load_font,
122f45c063aSXin LI 	vesa_show_font,
123f45c063aSXin LI 	vesa_save_palette,
124f45c063aSXin LI 	vesa_load_palette,
125f45c063aSXin LI 	vesa_set_border,
126f45c063aSXin LI 	vesa_save_state,
127f45c063aSXin LI 	vesa_load_state,
128f45c063aSXin LI 	vesa_set_origin,
129f45c063aSXin LI 	vesa_read_hw_cursor,
130f45c063aSXin LI 	vesa_set_hw_cursor,
131f45c063aSXin LI 	vesa_set_hw_cursor_shape,
132f45c063aSXin LI 	vesa_blank_display,
133f45c063aSXin LI 	vesa_mmap,
134f45c063aSXin LI 	vesa_ioctl,
135f45c063aSXin LI 	vesa_clear,
136f45c063aSXin LI 	vesa_fill_rect,
137f45c063aSXin LI 	vesa_bitblt,
138f45c063aSXin LI 	vesa_error,
139f45c063aSXin LI 	vesa_error,
140f45c063aSXin LI 	vesa_diag,
141f45c063aSXin LI };
142f45c063aSXin LI 
143f45c063aSXin LI static video_switch_t *prevvidsw;
144f45c063aSXin LI 
145f45c063aSXin LI /* VESA BIOS video modes */
146f45c063aSXin LI #define VESA_MAXMODES	64
147f45c063aSXin LI #define EOT		(-1)
148f45c063aSXin LI #define NA		(-2)
149f45c063aSXin LI 
150f45c063aSXin LI #define MODE_TABLE_DELTA 8
151f45c063aSXin LI 
152f45c063aSXin LI static int vesa_vmode_max = 0;
153f45c063aSXin LI static video_info_t vesa_vmode_empty = { EOT };
154f45c063aSXin LI static video_info_t *vesa_vmode = &vesa_vmode_empty;
155f45c063aSXin LI 
156f45c063aSXin LI static int vesa_init_done = FALSE;
157f45c063aSXin LI static int has_vesa_bios = FALSE;
158f45c063aSXin LI static struct vesa_info *vesa_adp_info = NULL;
159f45c063aSXin LI static u_int16_t *vesa_vmodetab = NULL;
160f45c063aSXin LI static char *vesa_oemstr = NULL;
161f45c063aSXin LI static char *vesa_venderstr = NULL;
162f45c063aSXin LI static char *vesa_prodstr = NULL;
163f45c063aSXin LI static char *vesa_revstr = NULL;
164f45c063aSXin LI 
165f45c063aSXin LI /* local macros and functions */
166f45c063aSXin LI #define BIOS_SADDRTOLADDR(p) ((((p) & 0xffff0000) >> 12) + ((p) & 0x0000ffff))
167f45c063aSXin LI 
168f45c063aSXin LI static int int10_set_mode(int mode);
169f45c063aSXin LI static int vesa_bios_get_mode(int mode, struct vesa_mode *vmode);
170f45c063aSXin LI static int vesa_bios_set_mode(int mode);
171f45c063aSXin LI static int vesa_bios_get_dac(void);
172f45c063aSXin LI static int vesa_bios_set_dac(int bits);
173f45c063aSXin LI static int vesa_bios_save_palette(int start, int colors, u_char *palette,
174f45c063aSXin LI 				  int bits);
175f45c063aSXin LI static int vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g,
176f45c063aSXin LI 				   u_char *b, int bits);
177f45c063aSXin LI static int vesa_bios_load_palette(int start, int colors, u_char *palette,
178f45c063aSXin LI 				  int bits);
179f45c063aSXin LI #ifdef notyet
180f45c063aSXin LI static int vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g,
181f45c063aSXin LI 				   u_char *b, int bits);
182f45c063aSXin LI #endif
183f45c063aSXin LI #define STATE_SIZE	0
184f45c063aSXin LI #define STATE_SAVE	1
185f45c063aSXin LI #define STATE_LOAD	2
186f45c063aSXin LI #define STATE_HW	(1<<0)
187f45c063aSXin LI #define STATE_DATA	(1<<1)
188f45c063aSXin LI #define STATE_DAC	(1<<2)
189f45c063aSXin LI #define STATE_REG	(1<<3)
190f45c063aSXin LI #define STATE_MOST	(STATE_HW | STATE_DATA | STATE_REG)
191f45c063aSXin LI #define STATE_ALL	(STATE_HW | STATE_DATA | STATE_DAC | STATE_REG)
192f45c063aSXin LI static int vesa_bios_state_buf_size(void);
193f45c063aSXin LI static int vesa_bios_save_restore(int code, void *p, size_t size);
194f45c063aSXin LI static int vesa_bios_get_line_length(void);
195f45c063aSXin LI static int vesa_bios_set_line_length(int pixel, int *bytes, int *lines);
196f45c063aSXin LI #if 0
197f45c063aSXin LI static int vesa_bios_get_start(int *x, int *y);
198f45c063aSXin LI #endif
199f45c063aSXin LI static int vesa_bios_set_start(int x, int y);
200f45c063aSXin LI static int vesa_map_gen_mode_num(int type, int color, int mode);
201f45c063aSXin LI static int vesa_translate_flags(u_int16_t vflags);
202f45c063aSXin LI static int vesa_translate_mmodel(u_int8_t vmodel);
203f45c063aSXin LI static void *vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off,
204f45c063aSXin LI 			  u_char *buf);
205f45c063aSXin LI static int vesa_bios_init(void);
206f45c063aSXin LI static void vesa_clear_modes(video_info_t *info, int color);
207f45c063aSXin LI static vm_offset_t vesa_map_buffer(u_int paddr, size_t size);
208f45c063aSXin LI static void vesa_unmap_buffer(vm_offset_t vaddr, size_t size);
209f45c063aSXin LI 
210f45c063aSXin LI #if 0
211f45c063aSXin LI static int vesa_get_origin(video_adapter_t *adp, off_t *offset);
212f45c063aSXin LI #endif
213f45c063aSXin LI 
214f45c063aSXin LI static void
215f45c063aSXin LI dump_buffer(u_char *buf, size_t len)
216f45c063aSXin LI {
217f45c063aSXin LI     int i;
218f45c063aSXin LI 
219f45c063aSXin LI     for(i = 0; i < len;) {
220f45c063aSXin LI 	printf("%02x ", buf[i]);
221f45c063aSXin LI 	if ((++i % 16) == 0)
222f45c063aSXin LI 	    printf("\n");
223f45c063aSXin LI     }
224f45c063aSXin LI }
225f45c063aSXin LI 
226f45c063aSXin LI /* INT 10 BIOS calls */
227f45c063aSXin LI static int
228f45c063aSXin LI int10_set_mode(int mode)
229f45c063aSXin LI {
230f45c063aSXin LI 	struct vm86frame vmf;
231f45c063aSXin LI 
232f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
233f45c063aSXin LI 	vmf.vmf_eax = 0x0000 | mode;
234f45c063aSXin LI 	vm86_intcall(0x10, &vmf);
235f45c063aSXin LI 	return 0;
236f45c063aSXin LI }
237f45c063aSXin LI 
238f45c063aSXin LI /* VESA BIOS calls */
239f45c063aSXin LI static int
240f45c063aSXin LI vesa_bios_get_mode(int mode, struct vesa_mode *vmode)
241f45c063aSXin LI {
242f45c063aSXin LI 	struct vm86frame vmf;
243f45c063aSXin LI 	u_char *buf;
244f45c063aSXin LI 	int err;
245f45c063aSXin LI 
246f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
247f45c063aSXin LI 	vmf.vmf_eax = 0x4f01;
248f45c063aSXin LI 	vmf.vmf_ecx = mode;
249f45c063aSXin LI 	buf = vesa_vm86_buf;
250f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_di);
251f45c063aSXin LI 
252f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
253f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
254f45c063aSXin LI 		return 1;
255f45c063aSXin LI 	bcopy(buf, vmode, sizeof(*vmode));
256f45c063aSXin LI 	return 0;
257f45c063aSXin LI }
258f45c063aSXin LI 
259f45c063aSXin LI static int
260f45c063aSXin LI vesa_bios_set_mode(int mode)
261f45c063aSXin LI {
262f45c063aSXin LI 	struct vm86frame vmf;
263f45c063aSXin LI 	int err;
264f45c063aSXin LI 
265f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
266f45c063aSXin LI 	vmf.vmf_eax = 0x4f02;
267f45c063aSXin LI 	vmf.vmf_ebx = mode;
268f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
269f45c063aSXin LI 	return ((err != 0) || (vmf.vmf_ax != 0x4f));
270f45c063aSXin LI }
271f45c063aSXin LI 
272f45c063aSXin LI static int
273f45c063aSXin LI vesa_bios_get_dac(void)
274f45c063aSXin LI {
275f45c063aSXin LI 	struct vm86frame vmf;
276f45c063aSXin LI 	int err;
277f45c063aSXin LI 
278f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
279f45c063aSXin LI 	vmf.vmf_eax = 0x4f08;
280f45c063aSXin LI 	vmf.vmf_ebx = 1;	/* get DAC width */
281f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
282f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
283f45c063aSXin LI 		return 6;	/* XXX */
284f45c063aSXin LI 	return ((vmf.vmf_ebx >> 8) & 0x00ff);
285f45c063aSXin LI }
286f45c063aSXin LI 
287f45c063aSXin LI static int
288f45c063aSXin LI vesa_bios_set_dac(int bits)
289f45c063aSXin LI {
290f45c063aSXin LI 	struct vm86frame vmf;
291f45c063aSXin LI 	int err;
292f45c063aSXin LI 
293f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
294f45c063aSXin LI 	vmf.vmf_eax = 0x4f08;
295f45c063aSXin LI 	vmf.vmf_ebx = (bits << 8);
296f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
297f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
298f45c063aSXin LI 		return 6;	/* XXX */
299f45c063aSXin LI 	return ((vmf.vmf_ebx >> 8) & 0x00ff);
300f45c063aSXin LI }
301f45c063aSXin LI 
302f45c063aSXin LI static int
303f45c063aSXin LI vesa_bios_save_palette(int start, int colors, u_char *palette, int bits)
304f45c063aSXin LI {
305f45c063aSXin LI 	struct vm86frame vmf;
306f45c063aSXin LI 	u_char *p;
307f45c063aSXin LI 	int err;
308f45c063aSXin LI 	int i;
309f45c063aSXin LI 
310f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
311f45c063aSXin LI 	vmf.vmf_eax = 0x4f09;
312f45c063aSXin LI 	vmf.vmf_ebx = 1;	/* get primary palette data */
313f45c063aSXin LI 	vmf.vmf_ecx = colors;
314f45c063aSXin LI 	vmf.vmf_edx = start;
315f45c063aSXin LI 	p = vesa_vm86_buf;
316f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
317f45c063aSXin LI 
318f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
319f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
320f45c063aSXin LI 		return 1;
321f45c063aSXin LI 
322f45c063aSXin LI 	bits = 8 - bits;
323f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
324f45c063aSXin LI 		palette[i*3]     = p[i*4 + 2] << bits;
325f45c063aSXin LI 		palette[i*3 + 1] = p[i*4 + 1] << bits;
326f45c063aSXin LI 		palette[i*3 + 2] = p[i*4] << bits;
327f45c063aSXin LI 	}
328f45c063aSXin LI 	return 0;
329f45c063aSXin LI }
330f45c063aSXin LI 
331f45c063aSXin LI static int
332f45c063aSXin LI vesa_bios_save_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
333f45c063aSXin LI 			int bits)
334f45c063aSXin LI {
335f45c063aSXin LI 	struct vm86frame vmf;
336f45c063aSXin LI 	u_char *p;
337f45c063aSXin LI 	int err;
338f45c063aSXin LI 	int i;
339f45c063aSXin LI 
340f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
341f45c063aSXin LI 	vmf.vmf_eax = 0x4f09;
342f45c063aSXin LI 	vmf.vmf_ebx = 1;	/* get primary palette data */
343f45c063aSXin LI 	vmf.vmf_ecx = colors;
344f45c063aSXin LI 	vmf.vmf_edx = start;
345f45c063aSXin LI 	p = vesa_vm86_buf;
346f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
347f45c063aSXin LI 
348f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
349f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
350f45c063aSXin LI 		return 1;
351f45c063aSXin LI 
352f45c063aSXin LI 	bits = 8 - bits;
353f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
354f45c063aSXin LI 		r[i] = p[i*4 + 2] << bits;
355f45c063aSXin LI 		g[i] = p[i*4 + 1] << bits;
356f45c063aSXin LI 		b[i] = p[i*4] << bits;
357f45c063aSXin LI 	}
358f45c063aSXin LI 	return 0;
359f45c063aSXin LI }
360f45c063aSXin LI 
361f45c063aSXin LI static int
362f45c063aSXin LI vesa_bios_load_palette(int start, int colors, u_char *palette, int bits)
363f45c063aSXin LI {
364f45c063aSXin LI 	struct vm86frame vmf;
365f45c063aSXin LI 	u_char *p;
366f45c063aSXin LI 	int err;
367f45c063aSXin LI 	int i;
368f45c063aSXin LI 
369f45c063aSXin LI 	p = vesa_vm86_buf;
370f45c063aSXin LI 	bits = 8 - bits;
371f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
372f45c063aSXin LI 		p[i*4]	   = palette[i*3 + 2] >> bits;
373f45c063aSXin LI 		p[i*4 + 1] = palette[i*3 + 1] >> bits;
374f45c063aSXin LI 		p[i*4 + 2] = palette[i*3] >> bits;
375f45c063aSXin LI 		p[i*4 + 3] = 0;
376f45c063aSXin LI 	}
377f45c063aSXin LI 
378f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
379f45c063aSXin LI 	vmf.vmf_eax = 0x4f09;
380f45c063aSXin LI 	vmf.vmf_ebx = 0;	/* set primary palette data */
381f45c063aSXin LI 	vmf.vmf_ecx = colors;
382f45c063aSXin LI 	vmf.vmf_edx = start;
383f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
384f45c063aSXin LI 
385f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
386f45c063aSXin LI 	return ((err != 0) || (vmf.vmf_ax != 0x4f));
387f45c063aSXin LI }
388f45c063aSXin LI 
389f45c063aSXin LI #ifdef notyet
390f45c063aSXin LI static int
391f45c063aSXin LI vesa_bios_load_palette2(int start, int colors, u_char *r, u_char *g, u_char *b,
392f45c063aSXin LI 			int bits)
393f45c063aSXin LI {
394f45c063aSXin LI 	struct vm86frame vmf;
395f45c063aSXin LI 	u_char *p;
396f45c063aSXin LI 	int err;
397f45c063aSXin LI 	int i;
398f45c063aSXin LI 
399f45c063aSXin LI 	p = vesa_vm86_buf;
400f45c063aSXin LI 	bits = 8 - bits;
401f45c063aSXin LI 	for (i = 0; i < colors; ++i) {
402f45c063aSXin LI 		p[i*4]	   = b[i] >> bits;
403f45c063aSXin LI 		p[i*4 + 1] = g[i] >> bits;
404f45c063aSXin LI 		p[i*4 + 2] = r[i] >> bits;
405f45c063aSXin LI 		p[i*4 + 3] = 0;
406f45c063aSXin LI 	}
407f45c063aSXin LI 
408f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
409f45c063aSXin LI 	vmf.vmf_eax = 0x4f09;
410f45c063aSXin LI 	vmf.vmf_ebx = 0;	/* set primary palette data */
411f45c063aSXin LI 	vmf.vmf_ecx = colors;
412f45c063aSXin LI 	vmf.vmf_edx = start;
413f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)p, &vmf.vmf_es, &vmf.vmf_di);
414f45c063aSXin LI 
415f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
416f45c063aSXin LI 	return ((err != 0) || (vmf.vmf_ax != 0x4f));
417f45c063aSXin LI }
418f45c063aSXin LI #endif
419f45c063aSXin LI 
420f45c063aSXin LI static int
421f45c063aSXin LI vesa_bios_state_buf_size(void)
422f45c063aSXin LI {
423f45c063aSXin LI 	struct vm86frame vmf;
424f45c063aSXin LI 	int err;
425f45c063aSXin LI 
426f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
427f45c063aSXin LI 	vmf.vmf_eax = 0x4f04;
428f45c063aSXin LI 	vmf.vmf_ecx = STATE_ALL;
429f45c063aSXin LI 	vmf.vmf_edx = STATE_SIZE;
430f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
431f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
432f45c063aSXin LI 		return 0;
433f45c063aSXin LI 	return vmf.vmf_bx*64;
434f45c063aSXin LI }
435f45c063aSXin LI 
436f45c063aSXin LI static int
437f45c063aSXin LI vesa_bios_save_restore(int code, void *p, size_t size)
438f45c063aSXin LI {
439f45c063aSXin LI 	struct vm86frame vmf;
440f45c063aSXin LI 	u_char *buf;
441f45c063aSXin LI 	int err;
442f45c063aSXin LI 
443f45c063aSXin LI 	if (size > VESA_VM86_BUFSIZE)
444f45c063aSXin LI 		return (1);
445f45c063aSXin LI 
446f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
447f45c063aSXin LI 	vmf.vmf_eax = 0x4f04;
448f45c063aSXin LI 	vmf.vmf_ecx = STATE_ALL;
449f45c063aSXin LI 	vmf.vmf_edx = code;	/* STATE_SAVE/STATE_LOAD */
450f45c063aSXin LI 	buf = vesa_vm86_buf;
451f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)buf, &vmf.vmf_es, &vmf.vmf_bx);
452f45c063aSXin LI 	bcopy(p, buf, size);
453f45c063aSXin LI 
454f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
455f45c063aSXin LI 	bcopy(buf, p, size);
456f45c063aSXin LI 	return ((err != 0) || (vmf.vmf_ax != 0x4f));
457f45c063aSXin LI }
458f45c063aSXin LI 
459f45c063aSXin LI static int
460f45c063aSXin LI vesa_bios_get_line_length(void)
461f45c063aSXin LI {
462f45c063aSXin LI 	struct vm86frame vmf;
463f45c063aSXin LI 	int err;
464f45c063aSXin LI 
465f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
466f45c063aSXin LI 	vmf.vmf_eax = 0x4f06;
467f45c063aSXin LI 	vmf.vmf_ebx = 1;	/* get scan line length */
468f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
469f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
470f45c063aSXin LI 		return -1;
471f45c063aSXin LI 	return vmf.vmf_bx;	/* line length in bytes */
472f45c063aSXin LI }
473f45c063aSXin LI 
474f45c063aSXin LI static int
475f45c063aSXin LI vesa_bios_set_line_length(int pixel, int *bytes, int *lines)
476f45c063aSXin LI {
477f45c063aSXin LI 	struct vm86frame vmf;
478f45c063aSXin LI 	int err;
479f45c063aSXin LI 
480f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
481f45c063aSXin LI 	vmf.vmf_eax = 0x4f06;
482f45c063aSXin LI 	vmf.vmf_ebx = 0;	/* set scan line length in pixel */
483f45c063aSXin LI 	vmf.vmf_ecx = pixel;
484f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
485f45c063aSXin LI #if VESA_DEBUG > 1
486f45c063aSXin LI 	printf("bx:%d, cx:%d, dx:%d\n", vmf.vmf_bx, vmf.vmf_cx, vmf.vmf_dx);
487f45c063aSXin LI #endif
488f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
489f45c063aSXin LI 		return 1;
490f45c063aSXin LI 	if (bytes)
491f45c063aSXin LI 		*bytes = vmf.vmf_bx;
492f45c063aSXin LI 	if (lines)
493f45c063aSXin LI 		*lines = vmf.vmf_dx;
494f45c063aSXin LI 	return 0;
495f45c063aSXin LI }
496f45c063aSXin LI 
497f45c063aSXin LI #if 0
498f45c063aSXin LI static int
499f45c063aSXin LI vesa_bios_get_start(int *x, int *y)
500f45c063aSXin LI {
501f45c063aSXin LI 	struct vm86frame vmf;
502f45c063aSXin LI 	int err;
503f45c063aSXin LI 
504f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
505f45c063aSXin LI 	vmf.vmf_eax = 0x4f07;
506f45c063aSXin LI 	vmf.vmf_ebx = 1;	/* get display start */
507f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
508f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
509f45c063aSXin LI 		return 1;
510f45c063aSXin LI 	*x = vmf.vmf_cx;
511f45c063aSXin LI 	*y = vmf.vmf_dx;
512f45c063aSXin LI 	return 0;
513f45c063aSXin LI }
514f45c063aSXin LI #endif
515f45c063aSXin LI 
516f45c063aSXin LI static int
517f45c063aSXin LI vesa_bios_set_start(int x, int y)
518f45c063aSXin LI {
519f45c063aSXin LI 	struct vm86frame vmf;
520f45c063aSXin LI 	int err;
521f45c063aSXin LI 
522f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
523f45c063aSXin LI 	vmf.vmf_eax = 0x4f07;
524f45c063aSXin LI 	vmf.vmf_ebx = 0x80;	/* set display start */
525f45c063aSXin LI 	vmf.vmf_edx = y;
526f45c063aSXin LI 	vmf.vmf_ecx = x;
527f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
528f45c063aSXin LI 	return ((err != 0) || (vmf.vmf_ax != 0x4f));
529f45c063aSXin LI }
530f45c063aSXin LI 
531f45c063aSXin LI /* map a generic video mode to a known mode */
532f45c063aSXin LI static int
533f45c063aSXin LI vesa_map_gen_mode_num(int type, int color, int mode)
534f45c063aSXin LI {
535f45c063aSXin LI     static struct {
536f45c063aSXin LI 	int from;
537f45c063aSXin LI 	int to;
538f45c063aSXin LI     } mode_map[] = {
539f45c063aSXin LI 	{ M_TEXT_132x25, M_VESA_C132x25 },
540f45c063aSXin LI 	{ M_TEXT_132x43, M_VESA_C132x43 },
541f45c063aSXin LI 	{ M_TEXT_132x50, M_VESA_C132x50 },
542f45c063aSXin LI 	{ M_TEXT_132x60, M_VESA_C132x60 },
543f45c063aSXin LI     };
544f45c063aSXin LI     int i;
545f45c063aSXin LI 
546f45c063aSXin LI     for (i = 0; i < sizeof(mode_map)/sizeof(mode_map[0]); ++i) {
547f45c063aSXin LI         if (mode_map[i].from == mode)
548f45c063aSXin LI             return mode_map[i].to;
549f45c063aSXin LI     }
550f45c063aSXin LI     return mode;
551f45c063aSXin LI }
552f45c063aSXin LI 
553f45c063aSXin LI static int
554f45c063aSXin LI vesa_translate_flags(u_int16_t vflags)
555f45c063aSXin LI {
556f45c063aSXin LI 	static struct {
557f45c063aSXin LI 		u_int16_t mask;
558f45c063aSXin LI 		int set;
559f45c063aSXin LI 		int reset;
560f45c063aSXin LI 	} ftable[] = {
561f45c063aSXin LI 		{ V_MODECOLOR, V_INFO_COLOR, 0 },
562f45c063aSXin LI 		{ V_MODEGRAPHICS, V_INFO_GRAPHICS, 0 },
563f45c063aSXin LI 		{ V_MODELFB, V_INFO_LINEAR, 0 },
564f45c063aSXin LI 	};
565f45c063aSXin LI 	int flags;
566f45c063aSXin LI 	int i;
567f45c063aSXin LI 
568f45c063aSXin LI 	for (flags = 0, i = 0; i < sizeof(ftable)/sizeof(ftable[0]); ++i) {
569f45c063aSXin LI 		flags |= (vflags & ftable[i].mask) ?
570f45c063aSXin LI 			 ftable[i].set : ftable[i].reset;
571f45c063aSXin LI 	}
572f45c063aSXin LI 	return flags;
573f45c063aSXin LI }
574f45c063aSXin LI 
575f45c063aSXin LI static int
576f45c063aSXin LI vesa_translate_mmodel(u_int8_t vmodel)
577f45c063aSXin LI {
578f45c063aSXin LI 	static struct {
579f45c063aSXin LI 		u_int8_t vmodel;
580f45c063aSXin LI 		int mmodel;
581f45c063aSXin LI 	} mtable[] = {
582f45c063aSXin LI 		{ V_MMTEXT,	V_INFO_MM_TEXT },
583f45c063aSXin LI 		{ V_MMCGA,	V_INFO_MM_CGA },
584f45c063aSXin LI 		{ V_MMHGC,	V_INFO_MM_HGC },
585f45c063aSXin LI 		{ V_MMEGA,	V_INFO_MM_PLANAR },
586f45c063aSXin LI 		{ V_MMPACKED,	V_INFO_MM_PACKED },
587f45c063aSXin LI 		{ V_MMDIRCOLOR,	V_INFO_MM_DIRECT },
588f45c063aSXin LI 	};
589f45c063aSXin LI 	int i;
590f45c063aSXin LI 
591f45c063aSXin LI 	for (i = 0; mtable[i].mmodel >= 0; ++i) {
592f45c063aSXin LI 		if (mtable[i].vmodel == vmodel)
593f45c063aSXin LI 			return mtable[i].mmodel;
594f45c063aSXin LI 	}
595f45c063aSXin LI 	return V_INFO_MM_OTHER;
596f45c063aSXin LI }
597f45c063aSXin LI 
598f45c063aSXin LI static void
599f45c063aSXin LI *vesa_fix_ptr(u_int32_t p, u_int16_t seg, u_int16_t off, u_char *buf)
600f45c063aSXin LI {
601f45c063aSXin LI 	if (p == 0)
602f45c063aSXin LI 		return NULL;
603f45c063aSXin LI 	if (((p >> 16) == seg) && ((p & 0xffff) >= off))
604f45c063aSXin LI 		return (void *)(buf + ((p & 0xffff) - off));
605f45c063aSXin LI 	else {
606f45c063aSXin LI 		p = BIOS_SADDRTOLADDR(p);
607f45c063aSXin LI 		return (void *)BIOS_PADDRTOVADDR(p);
608f45c063aSXin LI 	}
609f45c063aSXin LI }
610f45c063aSXin LI 
611f45c063aSXin LI static int
612f45c063aSXin LI vesa_bios_init(void)
613f45c063aSXin LI {
614f45c063aSXin LI 	static u_char buf[512];
615f45c063aSXin LI 	struct vm86frame vmf;
616f45c063aSXin LI 	struct vesa_mode vmode;
617f45c063aSXin LI 	video_info_t *p;
618f45c063aSXin LI 	u_char *vmbuf;
619f45c063aSXin LI 	int is_via_cle266;
620f45c063aSXin LI 	int modes;
621f45c063aSXin LI 	int err;
622f45c063aSXin LI 	int i;
623f45c063aSXin LI 
624f45c063aSXin LI 	if (vesa_init_done)
625f45c063aSXin LI 		return 0;
626f45c063aSXin LI 
627f45c063aSXin LI 	has_vesa_bios = FALSE;
628f45c063aSXin LI 	vesa_adp_info = NULL;
629f45c063aSXin LI 	vesa_vmode_max = 0;
630f45c063aSXin LI 	vesa_vmode[0].vi_mode = EOT;
631f45c063aSXin LI 
632f45c063aSXin LI 	/* Allocate a buffer and add each page to the vm86 context. */
633f45c063aSXin LI 	vesa_vm86_buf = malloc(VESA_VM86_BUFSIZE, M_DEVBUF, M_WAITOK | M_ZERO);
634f45c063aSXin LI 	KASSERT(((vm_offset_t)vesa_vm86_buf & PAGE_MASK) == 0,
635f45c063aSXin LI 	    ("bad vesa_vm86_buf alignment"));
636f45c063aSXin LI 	for (i = 0; i < howmany(VESA_VM86_BUFSIZE, PAGE_SIZE); i++)
637f45c063aSXin LI 		vm86_addpage(&vesa_vmcontext, i + 1,
638f45c063aSXin LI 		    (vm_offset_t)vesa_vm86_buf + PAGE_SIZE * i);
639f45c063aSXin LI 
640f45c063aSXin LI 	vmbuf = vesa_vm86_buf;
641f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));	/* paranoia */
642f45c063aSXin LI 	bcopy("VBE2", vmbuf, 4);	/* try for VBE2 data */
643f45c063aSXin LI 	vmf.vmf_eax = 0x4f00;
644f45c063aSXin LI 	vm86_getptr(&vesa_vmcontext, (vm_offset_t)vmbuf, &vmf.vmf_es, &vmf.vmf_di);
645f45c063aSXin LI 
646f45c063aSXin LI 	err = vm86_datacall(0x10, &vmf, &vesa_vmcontext);
647f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f) || bcmp("VESA", vmbuf, 4))
648f45c063aSXin LI 		return 1;
649f45c063aSXin LI 	bcopy(vmbuf, buf, sizeof(buf));
650f45c063aSXin LI 	vesa_adp_info = (struct vesa_info *)buf;
651f45c063aSXin LI 	if (bootverbose) {
652f45c063aSXin LI 		printf("VESA: information block\n");
653f45c063aSXin LI 		dump_buffer(buf, 64);
654f45c063aSXin LI 	}
655f45c063aSXin LI 	if (vesa_adp_info->v_version < 0x0102) {
656f45c063aSXin LI 		printf("VESA: VBE version %d.%d is not supported; "
657f45c063aSXin LI 		       "version 1.2 or later is required.\n",
658f45c063aSXin LI 		       ((vesa_adp_info->v_version & 0xf000) >> 12) * 10
659f45c063aSXin LI 			   + ((vesa_adp_info->v_version & 0x0f00) >> 8),
660f45c063aSXin LI 		       ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10
661f45c063aSXin LI 			   + (vesa_adp_info->v_version & 0x000f));
662f45c063aSXin LI 		return 1;
663f45c063aSXin LI 	}
664f45c063aSXin LI 
665f45c063aSXin LI 	/* fix string ptrs */
666f45c063aSXin LI 	vesa_oemstr = (char *)vesa_fix_ptr(vesa_adp_info->v_oemstr,
667f45c063aSXin LI 					   vmf.vmf_es, vmf.vmf_di, buf);
668f45c063aSXin LI 	is_via_cle266 = strcmp(vesa_oemstr, VESA_VIA_CLE266) == 0;
669f45c063aSXin LI 
670f45c063aSXin LI 	if (vesa_adp_info->v_version >= 0x0200) {
671f45c063aSXin LI 		vesa_venderstr =
672f45c063aSXin LI 		    (char *)vesa_fix_ptr(vesa_adp_info->v_venderstr,
673f45c063aSXin LI 					 vmf.vmf_es, vmf.vmf_di, buf);
674f45c063aSXin LI 		vesa_prodstr =
675f45c063aSXin LI 		    (char *)vesa_fix_ptr(vesa_adp_info->v_prodstr,
676f45c063aSXin LI 					 vmf.vmf_es, vmf.vmf_di, buf);
677f45c063aSXin LI 		vesa_revstr =
678f45c063aSXin LI 		    (char *)vesa_fix_ptr(vesa_adp_info->v_revstr,
679f45c063aSXin LI 					 vmf.vmf_es, vmf.vmf_di, buf);
680f45c063aSXin LI 	}
681f45c063aSXin LI 
682f45c063aSXin LI 	/* obtain video mode information */
683f45c063aSXin LI 	vesa_vmodetab = (u_int16_t *)vesa_fix_ptr(vesa_adp_info->v_modetable,
684f45c063aSXin LI 						  vmf.vmf_es, vmf.vmf_di, buf);
685f45c063aSXin LI 	if (vesa_vmodetab == NULL)
686f45c063aSXin LI 		return 1;
687f45c063aSXin LI 	for (i = 0, modes = 0;
688f45c063aSXin LI 		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
689f45c063aSXin LI 		&& (vesa_vmodetab[i] != 0xffff); ++i) {
690f45c063aSXin LI 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
691f45c063aSXin LI 			continue;
692f45c063aSXin LI 
693f45c063aSXin LI 		/* reject unsupported modes */
694f45c063aSXin LI #if 0
695f45c063aSXin LI 		if ((vmode.v_modeattr & (V_MODESUPP | V_MODEOPTINFO
696f45c063aSXin LI 					| V_MODENONVGA))
697f45c063aSXin LI 		    != (V_MODESUPP | V_MODEOPTINFO))
698f45c063aSXin LI 			continue;
699f45c063aSXin LI #else
700f45c063aSXin LI 		if ((vmode.v_modeattr & V_MODEOPTINFO) == 0) {
701f45c063aSXin LI #if VESA_DEBUG > 1
702f45c063aSXin LI 			printf(
703f45c063aSXin LI 		"Rejecting VESA %s mode: %d x %d x %d bpp  attr = %x\n",
704f45c063aSXin LI 			    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
705f45c063aSXin LI 			    vmode.v_width, vmode.v_height, vmode.v_bpp,
706f45c063aSXin LI 			    vmode.v_modeattr);
707f45c063aSXin LI #endif
708f45c063aSXin LI 			continue;
709f45c063aSXin LI 		}
710f45c063aSXin LI #endif
711f45c063aSXin LI 
712f45c063aSXin LI 		/* expand the array if necessary */
713f45c063aSXin LI 		if (modes >= vesa_vmode_max) {
714f45c063aSXin LI 			vesa_vmode_max += MODE_TABLE_DELTA;
715f45c063aSXin LI 			p = malloc(sizeof(*vesa_vmode)*(vesa_vmode_max + 1),
716f45c063aSXin LI 				   M_DEVBUF, M_WAITOK);
717f45c063aSXin LI #if VESA_DEBUG > 1
718f45c063aSXin LI 			printf("vesa_bios_init(): modes:%d, vesa_mode_max:%d\n",
719f45c063aSXin LI 			       modes, vesa_vmode_max);
720f45c063aSXin LI #endif
721f45c063aSXin LI 			if (modes > 0) {
722f45c063aSXin LI 				bcopy(vesa_vmode, p, sizeof(*vesa_vmode)*modes);
723f45c063aSXin LI 				free(vesa_vmode, M_DEVBUF);
724f45c063aSXin LI 			}
725f45c063aSXin LI 			vesa_vmode = p;
726f45c063aSXin LI 		}
727f45c063aSXin LI 
728f45c063aSXin LI #if VESA_DEBUG > 1
729f45c063aSXin LI 		printf("Found VESA %s mode: %d x %d x %d bpp\n",
730f45c063aSXin LI 		    vmode.v_modeattr & V_MODEGRAPHICS ? "graphics" : "text",
731f45c063aSXin LI 		    vmode.v_width, vmode.v_height, vmode.v_bpp);
732f45c063aSXin LI #endif
733f45c063aSXin LI 		if (is_via_cle266) {
734f45c063aSXin LI 		    if ((vmode.v_width & 0xff00) >> 8 == vmode.v_height - 1) {
735f45c063aSXin LI 			vmode.v_width &= 0xff;
736f45c063aSXin LI 			vmode.v_waseg = 0xb8000 >> 4;
737f45c063aSXin LI 		    }
738f45c063aSXin LI 		}
739f45c063aSXin LI 
740f45c063aSXin LI 		/* copy some fields */
741f45c063aSXin LI 		bzero(&vesa_vmode[modes], sizeof(vesa_vmode[modes]));
742f45c063aSXin LI 		vesa_vmode[modes].vi_mode = vesa_vmodetab[i];
743f45c063aSXin LI 		vesa_vmode[modes].vi_width = vmode.v_width;
744f45c063aSXin LI 		vesa_vmode[modes].vi_height = vmode.v_height;
745f45c063aSXin LI 		vesa_vmode[modes].vi_depth = vmode.v_bpp;
746f45c063aSXin LI 		vesa_vmode[modes].vi_planes = vmode.v_planes;
747f45c063aSXin LI 		vesa_vmode[modes].vi_cwidth = vmode.v_cwidth;
748f45c063aSXin LI 		vesa_vmode[modes].vi_cheight = vmode.v_cheight;
749f45c063aSXin LI 		vesa_vmode[modes].vi_window = (u_int)vmode.v_waseg << 4;
750f45c063aSXin LI 		/* XXX window B */
751f45c063aSXin LI 		vesa_vmode[modes].vi_window_size = vmode.v_wsize*1024;
752f45c063aSXin LI 		vesa_vmode[modes].vi_window_gran = vmode.v_wgran*1024;
753f45c063aSXin LI 		if (vmode.v_modeattr & V_MODELFB)
754f45c063aSXin LI 			vesa_vmode[modes].vi_buffer = vmode.v_lfb;
755f45c063aSXin LI 		else
756f45c063aSXin LI 			vesa_vmode[modes].vi_buffer = 0;
757f45c063aSXin LI 		/* XXX */
758f45c063aSXin LI 		vesa_vmode[modes].vi_buffer_size
759f45c063aSXin LI 			= vesa_adp_info->v_memsize*64*1024;
760f45c063aSXin LI #if 0
761f45c063aSXin LI 		if (vmode.v_offscreen > vmode.v_lfb)
762f45c063aSXin LI 			vesa_vmode[modes].vi_buffer_size
763f45c063aSXin LI 				= vmode.v_offscreen + vmode.v_offscreensize*1024
764f45c063aSXin LI 				      - vmode.v_lfb;
765f45c063aSXin LI 		else
766f45c063aSXin LI 			vesa_vmode[modes].vi_buffer_size
767f45c063aSXin LI 				= vmode.v_offscreen + vmode.v_offscreensize*1024
768f45c063aSXin LI #endif
769f45c063aSXin LI 		vesa_vmode[modes].vi_mem_model
770f45c063aSXin LI 			= vesa_translate_mmodel(vmode.v_memmodel);
771f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[0] = 0;
772f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[1] = 0;
773f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[2] = 0;
774f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fields[3] = 0;
775f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[0] = 0;
776f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[1] = 0;
777f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[2] = 0;
778f45c063aSXin LI 		vesa_vmode[modes].vi_pixel_fsizes[3] = 0;
779f45c063aSXin LI 		if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_PACKED) {
780f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
781f45c063aSXin LI 		} else if (vesa_vmode[modes].vi_mem_model == V_INFO_MM_DIRECT) {
782f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = (vmode.v_bpp + 7)/8;
783f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[0]
784f45c063aSXin LI 			    = vmode.v_redfieldpos;
785f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[1]
786f45c063aSXin LI 			    = vmode.v_greenfieldpos;
787f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[2]
788f45c063aSXin LI 			    = vmode.v_bluefieldpos;
789f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fields[3]
790f45c063aSXin LI 			    = vmode.v_resfieldpos;
791f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[0]
792f45c063aSXin LI 			    = vmode.v_redmasksize;
793f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[1]
794f45c063aSXin LI 			    = vmode.v_greenmasksize;
795f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[2]
796f45c063aSXin LI 			    = vmode.v_bluemasksize;
797f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_fsizes[3]
798f45c063aSXin LI 			    = vmode.v_resmasksize;
799f45c063aSXin LI 		} else {
800f45c063aSXin LI 			vesa_vmode[modes].vi_pixel_size = 0;
801f45c063aSXin LI 		}
802f45c063aSXin LI 
803f45c063aSXin LI 		vesa_vmode[modes].vi_flags
804f45c063aSXin LI 			= vesa_translate_flags(vmode.v_modeattr) | V_INFO_VESA;
805f45c063aSXin LI 		++modes;
806f45c063aSXin LI 	}
807f45c063aSXin LI 	vesa_vmode[modes].vi_mode = EOT;
808f45c063aSXin LI 	if (bootverbose)
809f45c063aSXin LI 		printf("VESA: %d mode(s) found\n", modes);
810f45c063aSXin LI 
811f45c063aSXin LI 	has_vesa_bios = (modes > 0);
812f45c063aSXin LI 	if (!has_vesa_bios)
813f45c063aSXin LI 		return (1);
814f45c063aSXin LI 
815f45c063aSXin LI 	return (0);
816f45c063aSXin LI }
817f45c063aSXin LI 
818f45c063aSXin LI static void
819f45c063aSXin LI vesa_clear_modes(video_info_t *info, int color)
820f45c063aSXin LI {
821f45c063aSXin LI 	while (info->vi_mode != EOT) {
822f45c063aSXin LI 		if ((info->vi_flags & V_INFO_COLOR) != color)
823f45c063aSXin LI 			info->vi_mode = NA;
824f45c063aSXin LI 		++info;
825f45c063aSXin LI 	}
826f45c063aSXin LI }
827f45c063aSXin LI 
828f45c063aSXin LI static vm_offset_t
829f45c063aSXin LI vesa_map_buffer(u_int paddr, size_t size)
830f45c063aSXin LI {
831f45c063aSXin LI 	vm_offset_t vaddr;
832f45c063aSXin LI 	u_int off;
833f45c063aSXin LI 
834f45c063aSXin LI 	off = paddr - trunc_page(paddr);
835f45c063aSXin LI 	vaddr = (vm_offset_t)pmap_mapdev(paddr - off, size + off);
836f45c063aSXin LI #if VESA_DEBUG > 1
837f45c063aSXin LI 	printf("vesa_map_buffer: paddr:%x vaddr:%x size:%x off:%x\n",
838f45c063aSXin LI 	       paddr, vaddr, size, off);
839f45c063aSXin LI #endif
840f45c063aSXin LI 	return (vaddr + off);
841f45c063aSXin LI }
842f45c063aSXin LI 
843f45c063aSXin LI static void
844f45c063aSXin LI vesa_unmap_buffer(vm_offset_t vaddr, size_t size)
845f45c063aSXin LI {
846f45c063aSXin LI #if VESA_DEBUG > 1
847f45c063aSXin LI 	printf("vesa_unmap_buffer: vaddr:%x size:%x\n", vaddr, size);
848f45c063aSXin LI #endif
849f45c063aSXin LI 	kmem_free(kernel_map, vaddr, size);
850f45c063aSXin LI }
851f45c063aSXin LI 
852f45c063aSXin LI /* entry points */
853f45c063aSXin LI 
854f45c063aSXin LI static int
855f45c063aSXin LI vesa_configure(int flags)
856f45c063aSXin LI {
857f45c063aSXin LI 	video_adapter_t *adp;
858f45c063aSXin LI 	int adapters;
859f45c063aSXin LI 	int error;
860f45c063aSXin LI 	int i;
861f45c063aSXin LI 
862f45c063aSXin LI 	if (vesa_init_done)
863f45c063aSXin LI 		return 0;
864f45c063aSXin LI 	if (flags & VIO_PROBE_ONLY)
865f45c063aSXin LI 		return 0;		/* XXX */
866f45c063aSXin LI 
867f45c063aSXin LI 	/*
868f45c063aSXin LI 	 * If the VESA module has already been loaded, abort loading
869f45c063aSXin LI 	 * the module this time.
870f45c063aSXin LI 	 */
871f45c063aSXin LI 	for (i = 0; (adp = vid_get_adapter(i)) != NULL; ++i) {
872f45c063aSXin LI 		if (adp->va_flags & V_ADP_VESA)
873f45c063aSXin LI 			return ENXIO;
874f45c063aSXin LI 		if (adp->va_type == KD_VGA)
875f45c063aSXin LI 			break;
876f45c063aSXin LI 	}
877f45c063aSXin LI 	/*
878f45c063aSXin LI 	 * The VGA adapter is not found.  This is because either
879f45c063aSXin LI 	 * 1) the VGA driver has not been initialized, or 2) the VGA card
880f45c063aSXin LI 	 * is not present.  If 1) is the case, we shall defer
881f45c063aSXin LI 	 * initialization for now and try again later.
882f45c063aSXin LI 	 */
883f45c063aSXin LI 	if (adp == NULL) {
884f45c063aSXin LI 		vga_sub_configure = vesa_configure;
885f45c063aSXin LI 		return ENODEV;
886f45c063aSXin LI 	}
887f45c063aSXin LI 
888f45c063aSXin LI 	/* count number of registered adapters */
889f45c063aSXin LI 	for (++i; vid_get_adapter(i) != NULL; ++i)
890f45c063aSXin LI 		;
891f45c063aSXin LI 	adapters = i;
892f45c063aSXin LI 
893f45c063aSXin LI 	/* call VESA BIOS */
894f45c063aSXin LI 	vesa_adp = adp;
895f45c063aSXin LI 	if (vesa_bios_init()) {
896f45c063aSXin LI 		vesa_adp = NULL;
897f45c063aSXin LI 		return ENXIO;
898f45c063aSXin LI 	}
899f45c063aSXin LI 	vesa_adp->va_flags |= V_ADP_VESA;
900f45c063aSXin LI 
901f45c063aSXin LI 	/* remove conflicting modes if we have more than one adapter */
902f45c063aSXin LI 	if (adapters > 1) {
903f45c063aSXin LI 		vesa_clear_modes(vesa_vmode,
904f45c063aSXin LI 				 (vesa_adp->va_flags & V_ADP_COLOR) ?
905f45c063aSXin LI 				     V_INFO_COLOR : 0);
906f45c063aSXin LI 	}
907f45c063aSXin LI 
908f45c063aSXin LI 	if ((error = vesa_load_ioctl()) == 0) {
909f45c063aSXin LI 		prevvidsw = vidsw[vesa_adp->va_index];
910f45c063aSXin LI 		vidsw[vesa_adp->va_index] = &vesavidsw;
911f45c063aSXin LI 		vesa_init_done = TRUE;
912f45c063aSXin LI 	} else {
913f45c063aSXin LI 		vesa_adp = NULL;
914f45c063aSXin LI 		return error;
915f45c063aSXin LI 	}
916f45c063aSXin LI 
917f45c063aSXin LI 	return 0;
918f45c063aSXin LI }
919f45c063aSXin LI 
920f45c063aSXin LI #if 0
921f45c063aSXin LI static int
922f45c063aSXin LI vesa_nop(void)
923f45c063aSXin LI {
924f45c063aSXin LI 	return 0;
925f45c063aSXin LI }
926f45c063aSXin LI #endif
927f45c063aSXin LI 
928f45c063aSXin LI static int
929f45c063aSXin LI vesa_error(void)
930f45c063aSXin LI {
931f45c063aSXin LI 	return 1;
932f45c063aSXin LI }
933f45c063aSXin LI 
934f45c063aSXin LI static int
935f45c063aSXin LI vesa_probe(int unit, video_adapter_t **adpp, void *arg, int flags)
936f45c063aSXin LI {
937f45c063aSXin LI 	return (*prevvidsw->probe)(unit, adpp, arg, flags);
938f45c063aSXin LI }
939f45c063aSXin LI 
940f45c063aSXin LI static int
941f45c063aSXin LI vesa_init(int unit, video_adapter_t *adp, int flags)
942f45c063aSXin LI {
943f45c063aSXin LI 	return (*prevvidsw->init)(unit, adp, flags);
944f45c063aSXin LI }
945f45c063aSXin LI 
946f45c063aSXin LI static int
947f45c063aSXin LI vesa_get_info(video_adapter_t *adp, int mode, video_info_t *info)
948f45c063aSXin LI {
949f45c063aSXin LI 	int i;
950f45c063aSXin LI 
951f45c063aSXin LI 	if ((*prevvidsw->get_info)(adp, mode, info) == 0)
952f45c063aSXin LI 		return 0;
953f45c063aSXin LI 
954f45c063aSXin LI 	if (adp != vesa_adp)
955f45c063aSXin LI 		return 1;
956f45c063aSXin LI 
957f45c063aSXin LI 	mode = vesa_map_gen_mode_num(vesa_adp->va_type,
958f45c063aSXin LI 				     vesa_adp->va_flags & V_ADP_COLOR, mode);
959f45c063aSXin LI 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
960f45c063aSXin LI 		if (vesa_vmode[i].vi_mode == NA)
961f45c063aSXin LI 			continue;
962f45c063aSXin LI 		if (vesa_vmode[i].vi_mode == mode) {
963f45c063aSXin LI 			*info = vesa_vmode[i];
964f45c063aSXin LI 			return 0;
965f45c063aSXin LI 		}
966f45c063aSXin LI 	}
967f45c063aSXin LI 	return 1;
968f45c063aSXin LI }
969f45c063aSXin LI 
970f45c063aSXin LI static int
971f45c063aSXin LI vesa_query_mode(video_adapter_t *adp, video_info_t *info)
972f45c063aSXin LI {
973f45c063aSXin LI 	int i;
974f45c063aSXin LI 
975f45c063aSXin LI 	if ((*prevvidsw->query_mode)(adp, info) == 0)
976f45c063aSXin LI 		return 0;
977f45c063aSXin LI 	if (adp != vesa_adp)
978f45c063aSXin LI 		return ENODEV;
979f45c063aSXin LI 
980f45c063aSXin LI 	for (i = 0; vesa_vmode[i].vi_mode != EOT; ++i) {
981f45c063aSXin LI 		if ((info->vi_width != 0)
982f45c063aSXin LI 		    && (info->vi_width != vesa_vmode[i].vi_width))
983f45c063aSXin LI 			continue;
984f45c063aSXin LI 		if ((info->vi_height != 0)
985f45c063aSXin LI 		    && (info->vi_height != vesa_vmode[i].vi_height))
986f45c063aSXin LI 			continue;
987f45c063aSXin LI 		if ((info->vi_cwidth != 0)
988f45c063aSXin LI 		    && (info->vi_cwidth != vesa_vmode[i].vi_cwidth))
989f45c063aSXin LI 			continue;
990f45c063aSXin LI 		if ((info->vi_cheight != 0)
991f45c063aSXin LI 		    && (info->vi_cheight != vesa_vmode[i].vi_cheight))
992f45c063aSXin LI 			continue;
993f45c063aSXin LI 		if ((info->vi_depth != 0)
994f45c063aSXin LI 		    && (info->vi_depth != vesa_vmode[i].vi_depth))
995f45c063aSXin LI 			continue;
996f45c063aSXin LI 		if ((info->vi_planes != 0)
997f45c063aSXin LI 		    && (info->vi_planes != vesa_vmode[i].vi_planes))
998f45c063aSXin LI 			continue;
999f45c063aSXin LI 		/* pixel format, memory model */
1000f45c063aSXin LI 		if ((info->vi_flags != 0)
1001f45c063aSXin LI 		    && (info->vi_flags != vesa_vmode[i].vi_flags))
1002f45c063aSXin LI 			continue;
1003f45c063aSXin LI 		*info = vesa_vmode[i];
1004f45c063aSXin LI 		return 0;
1005f45c063aSXin LI 	}
1006f45c063aSXin LI 	return ENODEV;
1007f45c063aSXin LI }
1008f45c063aSXin LI 
1009f45c063aSXin LI static int
1010f45c063aSXin LI vesa_set_mode(video_adapter_t *adp, int mode)
1011f45c063aSXin LI {
1012f45c063aSXin LI 	video_info_t info;
1013f45c063aSXin LI 	int len;
1014f45c063aSXin LI 
1015f45c063aSXin LI 	if (adp != vesa_adp)
1016f45c063aSXin LI 		return (*prevvidsw->set_mode)(adp, mode);
1017f45c063aSXin LI 
1018f45c063aSXin LI 	mode = vesa_map_gen_mode_num(adp->va_type,
1019f45c063aSXin LI 				     adp->va_flags & V_ADP_COLOR, mode);
1020f45c063aSXin LI #if VESA_DEBUG > 0
1021f45c063aSXin LI 	printf("VESA: set_mode(): %d(%x) -> %d(%x)\n",
1022f45c063aSXin LI 		adp->va_mode, adp->va_mode, mode, mode);
1023f45c063aSXin LI #endif
1024f45c063aSXin LI 	/*
1025f45c063aSXin LI 	 * If the current mode is a VESA mode and the new mode is not,
1026f45c063aSXin LI 	 * restore the state of the adapter first by setting one of the
1027f45c063aSXin LI 	 * standard VGA mode, so that non-standard, extended SVGA registers
1028f45c063aSXin LI 	 * are set to the state compatible with the standard VGA modes.
1029f45c063aSXin LI 	 * Otherwise (*prevvidsw->set_mode)() may not be able to set up
1030f45c063aSXin LI 	 * the new mode correctly.
1031f45c063aSXin LI 	 */
1032f45c063aSXin LI 	if (VESA_MODE(adp->va_mode)) {
1033f45c063aSXin LI 		if ((*prevvidsw->get_info)(adp, mode, &info) == 0) {
1034f45c063aSXin LI 			int10_set_mode(adp->va_initial_bios_mode);
1035f45c063aSXin LI 			if (adp->va_info.vi_flags & V_INFO_LINEAR)
1036f45c063aSXin LI 				vesa_unmap_buffer(adp->va_buffer,
1037f45c063aSXin LI 						  vesa_adp_info->v_memsize*64*1024);
1038f45c063aSXin LI 			/*
1039f45c063aSXin LI 			 * Once (*prevvidsw->get_info)() succeeded,
1040f45c063aSXin LI 			 * (*prevvidsw->set_mode)() below won't fail...
1041f45c063aSXin LI 			 */
1042f45c063aSXin LI 		}
1043f45c063aSXin LI 	}
1044f45c063aSXin LI 
1045f45c063aSXin LI 	/* we may not need to handle this mode after all... */
1046f45c063aSXin LI 	if ((*prevvidsw->set_mode)(adp, mode) == 0)
1047f45c063aSXin LI 		return 0;
1048f45c063aSXin LI 
1049f45c063aSXin LI 	/* is the new mode supported? */
1050f45c063aSXin LI 	if (vesa_get_info(adp, mode, &info))
1051f45c063aSXin LI 		return 1;
1052f45c063aSXin LI 	/* assert(VESA_MODE(mode)); */
1053f45c063aSXin LI 
1054f45c063aSXin LI #if VESA_DEBUG > 0
1055f45c063aSXin LI 	printf("VESA: about to set a VESA mode...\n");
1056f45c063aSXin LI #endif
1057f45c063aSXin LI 	/* don't use the linear frame buffer for text modes. XXX */
1058f45c063aSXin LI 	if (!(info.vi_flags & V_INFO_GRAPHICS))
1059f45c063aSXin LI 		info.vi_flags &= ~V_INFO_LINEAR;
1060f45c063aSXin LI 
1061f45c063aSXin LI 	if (vesa_bios_set_mode(mode | ((info.vi_flags & V_INFO_LINEAR) ? 0x4000 : 0)))
1062f45c063aSXin LI 		return 1;
1063f45c063aSXin LI 
1064f45c063aSXin LI 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1065f45c063aSXin LI 		vesa_unmap_buffer(adp->va_buffer,
1066f45c063aSXin LI 				  vesa_adp_info->v_memsize*64*1024);
1067f45c063aSXin LI 
1068f45c063aSXin LI #if VESA_DEBUG > 0
1069f45c063aSXin LI 	printf("VESA: mode set!\n");
1070f45c063aSXin LI #endif
1071f45c063aSXin LI 	vesa_adp->va_mode = mode;
1072f45c063aSXin LI 	vesa_adp->va_flags &= ~V_ADP_COLOR;
1073f45c063aSXin LI 	vesa_adp->va_flags |=
1074f45c063aSXin LI 		(info.vi_flags & V_INFO_COLOR) ? V_ADP_COLOR : 0;
1075f45c063aSXin LI 	vesa_adp->va_crtc_addr =
1076f45c063aSXin LI 		(vesa_adp->va_flags & V_ADP_COLOR) ? COLOR_CRTC : MONO_CRTC;
1077f45c063aSXin LI 	if (info.vi_flags & V_INFO_LINEAR) {
1078f45c063aSXin LI #if VESA_DEBUG > 1
1079f45c063aSXin LI 		printf("VESA: setting up LFB\n");
1080f45c063aSXin LI #endif
1081f45c063aSXin LI 		vesa_adp->va_buffer =
1082f45c063aSXin LI 			vesa_map_buffer(info.vi_buffer,
1083f45c063aSXin LI 					vesa_adp_info->v_memsize*64*1024);
1084f45c063aSXin LI 		vesa_adp->va_buffer_size = info.vi_buffer_size;
1085f45c063aSXin LI 		vesa_adp->va_window = vesa_adp->va_buffer;
1086f45c063aSXin LI 		vesa_adp->va_window_size = info.vi_buffer_size/info.vi_planes;
1087f45c063aSXin LI 		vesa_adp->va_window_gran = info.vi_buffer_size/info.vi_planes;
1088f45c063aSXin LI 	} else {
1089f45c063aSXin LI 		vesa_adp->va_buffer = 0;
1090f45c063aSXin LI 		vesa_adp->va_buffer_size = info.vi_buffer_size;
1091f45c063aSXin LI 		vesa_adp->va_window = BIOS_PADDRTOVADDR(info.vi_window);
1092f45c063aSXin LI 		vesa_adp->va_window_size = info.vi_window_size;
1093f45c063aSXin LI 		vesa_adp->va_window_gran = info.vi_window_gran;
1094f45c063aSXin LI 	}
1095f45c063aSXin LI 	vesa_adp->va_window_orig = 0;
1096f45c063aSXin LI 	len = vesa_bios_get_line_length();
1097f45c063aSXin LI 	if (len > 0) {
1098f45c063aSXin LI 		vesa_adp->va_line_width = len;
1099f45c063aSXin LI 	} else if (info.vi_flags & V_INFO_GRAPHICS) {
1100f45c063aSXin LI 		switch (info.vi_depth/info.vi_planes) {
1101f45c063aSXin LI 		case 1:
1102f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/8;
1103f45c063aSXin LI 			break;
1104f45c063aSXin LI 		case 2:
1105f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/4;
1106f45c063aSXin LI 			break;
1107f45c063aSXin LI 		case 4:
1108f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width/2;
1109f45c063aSXin LI 			break;
1110f45c063aSXin LI 		case 8:
1111f45c063aSXin LI 		default: /* shouldn't happen */
1112f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width;
1113f45c063aSXin LI 			break;
1114f45c063aSXin LI 		case 15:
1115f45c063aSXin LI 		case 16:
1116f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width*2;
1117f45c063aSXin LI 			break;
1118f45c063aSXin LI 		case 24:
1119f45c063aSXin LI 		case 32:
1120f45c063aSXin LI 			vesa_adp->va_line_width = info.vi_width*4;
1121f45c063aSXin LI 			break;
1122f45c063aSXin LI 		}
1123f45c063aSXin LI 	} else {
1124f45c063aSXin LI 		vesa_adp->va_line_width = info.vi_width;
1125f45c063aSXin LI 	}
1126f45c063aSXin LI 	vesa_adp->va_disp_start.x = 0;
1127f45c063aSXin LI 	vesa_adp->va_disp_start.y = 0;
1128f45c063aSXin LI #if VESA_DEBUG > 0
1129f45c063aSXin LI 	printf("vesa_set_mode(): vi_width:%d, len:%d, line_width:%d\n",
1130f45c063aSXin LI 	       info.vi_width, len, vesa_adp->va_line_width);
1131f45c063aSXin LI #endif
1132f45c063aSXin LI 	bcopy(&info, &vesa_adp->va_info, sizeof(vesa_adp->va_info));
1133f45c063aSXin LI 
1134f45c063aSXin LI 	/* move hardware cursor out of the way */
1135f45c063aSXin LI 	(*vidsw[vesa_adp->va_index]->set_hw_cursor)(vesa_adp, -1, -1);
1136f45c063aSXin LI 
1137f45c063aSXin LI 	return 0;
1138f45c063aSXin LI }
1139f45c063aSXin LI 
1140f45c063aSXin LI static int
1141f45c063aSXin LI vesa_save_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1142f45c063aSXin LI 	       u_char *data, int ch, int count)
1143f45c063aSXin LI {
1144f45c063aSXin LI 	return (*prevvidsw->save_font)(adp, page, fontsize, fontwidth, data,
1145f45c063aSXin LI 		ch, count);
1146f45c063aSXin LI }
1147f45c063aSXin LI 
1148f45c063aSXin LI static int
1149f45c063aSXin LI vesa_load_font(video_adapter_t *adp, int page, int fontsize, int fontwidth,
1150f45c063aSXin LI 	       u_char *data, int ch, int count)
1151f45c063aSXin LI {
1152f45c063aSXin LI 	return (*prevvidsw->load_font)(adp, page, fontsize, fontwidth, data,
1153f45c063aSXin LI 		ch, count);
1154f45c063aSXin LI }
1155f45c063aSXin LI 
1156f45c063aSXin LI static int
1157f45c063aSXin LI vesa_show_font(video_adapter_t *adp, int page)
1158f45c063aSXin LI {
1159f45c063aSXin LI 	return (*prevvidsw->show_font)(adp, page);
1160f45c063aSXin LI }
1161f45c063aSXin LI 
1162f45c063aSXin LI static int
1163f45c063aSXin LI vesa_save_palette(video_adapter_t *adp, u_char *palette)
1164f45c063aSXin LI {
1165f45c063aSXin LI 	int bits;
1166f45c063aSXin LI 	int error;
1167f45c063aSXin LI 
1168f45c063aSXin LI 	if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1169f45c063aSXin LI 	    && VESA_MODE(adp->va_mode)) {
1170f45c063aSXin LI 		bits = vesa_bios_get_dac();
1171f45c063aSXin LI 		error = vesa_bios_save_palette(0, 256, palette, bits);
1172f45c063aSXin LI 		if (error == 0)
1173f45c063aSXin LI 			return 0;
1174f45c063aSXin LI 		if (bits != 6)
1175f45c063aSXin LI 			return error;
1176f45c063aSXin LI 	}
1177f45c063aSXin LI 
1178f45c063aSXin LI 	return (*prevvidsw->save_palette)(adp, palette);
1179f45c063aSXin LI }
1180f45c063aSXin LI 
1181f45c063aSXin LI static int
1182f45c063aSXin LI vesa_load_palette(video_adapter_t *adp, u_char *palette)
1183f45c063aSXin LI {
1184f45c063aSXin LI #ifdef notyet
1185f45c063aSXin LI 	int bits;
1186f45c063aSXin LI 	int error;
1187f45c063aSXin LI 
1188f45c063aSXin LI 	if ((adp == vesa_adp) && (vesa_adp_info->v_flags & V_DAC8)
1189f45c063aSXin LI 	    && VESA_MODE(adp->va_mode) && ((bits = vesa_bios_set_dac(8)) > 6)) {
1190f45c063aSXin LI 		error = vesa_bios_load_palette(0, 256, palette, bits);
1191f45c063aSXin LI 		if (error == 0)
1192f45c063aSXin LI 			return 0;
1193f45c063aSXin LI 		if (vesa_bios_set_dac(6) != 6)
1194f45c063aSXin LI 			return 1;
1195f45c063aSXin LI 	}
1196f45c063aSXin LI #endif /* notyet */
1197f45c063aSXin LI 
1198f45c063aSXin LI 	return (*prevvidsw->load_palette)(adp, palette);
1199f45c063aSXin LI }
1200f45c063aSXin LI 
1201f45c063aSXin LI static int
1202f45c063aSXin LI vesa_set_border(video_adapter_t *adp, int color)
1203f45c063aSXin LI {
1204f45c063aSXin LI 	return (*prevvidsw->set_border)(adp, color);
1205f45c063aSXin LI }
1206f45c063aSXin LI 
1207f45c063aSXin LI static int
1208f45c063aSXin LI vesa_save_state(video_adapter_t *adp, void *p, size_t size)
1209f45c063aSXin LI {
1210f45c063aSXin LI 	if (adp != vesa_adp)
1211f45c063aSXin LI 		return (*prevvidsw->save_state)(adp, p, size);
1212f45c063aSXin LI 
1213f45c063aSXin LI 	if (vesa_state_buf_size == 0)
1214f45c063aSXin LI 		vesa_state_buf_size = vesa_bios_state_buf_size();
1215f45c063aSXin LI 	if (size == 0)
1216f45c063aSXin LI 		return (sizeof(int) + vesa_state_buf_size);
1217f45c063aSXin LI 	else if (size < (sizeof(int) + vesa_state_buf_size))
1218f45c063aSXin LI 		return 1;
1219f45c063aSXin LI 
1220f45c063aSXin LI 	((adp_state_t *)p)->sig = V_STATE_SIG;
1221f45c063aSXin LI 	bzero(((adp_state_t *)p)->regs, vesa_state_buf_size);
1222f45c063aSXin LI 	return vesa_bios_save_restore(STATE_SAVE, ((adp_state_t *)p)->regs,
1223f45c063aSXin LI 				      vesa_state_buf_size);
1224f45c063aSXin LI }
1225f45c063aSXin LI 
1226f45c063aSXin LI static int
1227f45c063aSXin LI vesa_load_state(video_adapter_t *adp, void *p)
1228f45c063aSXin LI {
1229f45c063aSXin LI 	if ((adp != vesa_adp) || (((adp_state_t *)p)->sig != V_STATE_SIG))
1230f45c063aSXin LI 		return (*prevvidsw->load_state)(adp, p);
1231f45c063aSXin LI 
1232f45c063aSXin LI 	return vesa_bios_save_restore(STATE_LOAD, ((adp_state_t *)p)->regs,
1233f45c063aSXin LI 				      vesa_state_buf_size);
1234f45c063aSXin LI }
1235f45c063aSXin LI 
1236f45c063aSXin LI #if 0
1237f45c063aSXin LI static int
1238f45c063aSXin LI vesa_get_origin(video_adapter_t *adp, off_t *offset)
1239f45c063aSXin LI {
1240f45c063aSXin LI 	struct vm86frame vmf;
1241f45c063aSXin LI 	int err;
1242f45c063aSXin LI 
1243f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
1244f45c063aSXin LI 	vmf.vmf_eax = 0x4f05;
1245f45c063aSXin LI 	vmf.vmf_ebx = 0x10;		/* WINDOW_A, XXX */
1246f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
1247f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
1248f45c063aSXin LI 		return 1;
1249f45c063aSXin LI 	*offset = vmf.vmf_dx*adp->va_window_gran;
1250f45c063aSXin LI 	return 0;
1251f45c063aSXin LI }
1252f45c063aSXin LI #endif
1253f45c063aSXin LI 
1254f45c063aSXin LI static int
1255f45c063aSXin LI vesa_set_origin(video_adapter_t *adp, off_t offset)
1256f45c063aSXin LI {
1257f45c063aSXin LI 	struct vm86frame vmf;
1258f45c063aSXin LI 	int err;
1259f45c063aSXin LI 
1260f45c063aSXin LI 	/*
1261f45c063aSXin LI 	 * This function should return as quickly as possible to
1262f45c063aSXin LI 	 * maintain good performance of the system. For this reason,
1263f45c063aSXin LI 	 * error checking is kept minimal and let the VESA BIOS to
1264f45c063aSXin LI 	 * detect error.
1265f45c063aSXin LI 	 */
1266f45c063aSXin LI 	if (adp != vesa_adp)
1267f45c063aSXin LI 		return (*prevvidsw->set_win_org)(adp, offset);
1268f45c063aSXin LI 
1269f45c063aSXin LI 	/* if this is a linear frame buffer, do nothing */
1270f45c063aSXin LI 	if (adp->va_info.vi_flags & V_INFO_LINEAR)
1271f45c063aSXin LI 		return 0;
1272f45c063aSXin LI 	/* XXX */
1273f45c063aSXin LI 	if (adp->va_window_gran == 0)
1274f45c063aSXin LI 		return 1;
1275f45c063aSXin LI 
1276f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
1277f45c063aSXin LI 	vmf.vmf_eax = 0x4f05;
1278f45c063aSXin LI 	vmf.vmf_ebx = 0;		/* WINDOW_A, XXX */
1279f45c063aSXin LI 	vmf.vmf_edx = offset/adp->va_window_gran;
1280f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
1281f45c063aSXin LI 	if ((err != 0) || (vmf.vmf_ax != 0x4f))
1282f45c063aSXin LI 		return 1;
1283f45c063aSXin LI 	bzero(&vmf, sizeof(vmf));
1284f45c063aSXin LI 	vmf.vmf_eax = 0x4f05;
1285f45c063aSXin LI 	vmf.vmf_ebx = 1;		/* WINDOW_B, XXX */
1286f45c063aSXin LI 	vmf.vmf_edx = offset/adp->va_window_gran;
1287f45c063aSXin LI 	err = vm86_intcall(0x10, &vmf);
1288f45c063aSXin LI 	adp->va_window_orig = (offset/adp->va_window_gran)*adp->va_window_gran;
1289f45c063aSXin LI 	return 0;			/* XXX */
1290f45c063aSXin LI }
1291f45c063aSXin LI 
1292f45c063aSXin LI static int
1293f45c063aSXin LI vesa_read_hw_cursor(video_adapter_t *adp, int *col, int *row)
1294f45c063aSXin LI {
1295f45c063aSXin LI 	return (*prevvidsw->read_hw_cursor)(adp, col, row);
1296f45c063aSXin LI }
1297f45c063aSXin LI 
1298f45c063aSXin LI static int
1299f45c063aSXin LI vesa_set_hw_cursor(video_adapter_t *adp, int col, int row)
1300f45c063aSXin LI {
1301f45c063aSXin LI 	return (*prevvidsw->set_hw_cursor)(adp, col, row);
1302f45c063aSXin LI }
1303f45c063aSXin LI 
1304f45c063aSXin LI static int
1305f45c063aSXin LI vesa_set_hw_cursor_shape(video_adapter_t *adp, int base, int height,
1306f45c063aSXin LI 			 int celsize, int blink)
1307f45c063aSXin LI {
1308f45c063aSXin LI 	return (*prevvidsw->set_hw_cursor_shape)(adp, base, height, celsize,
1309f45c063aSXin LI 						 blink);
1310f45c063aSXin LI }
1311f45c063aSXin LI 
1312f45c063aSXin LI static int
1313f45c063aSXin LI vesa_blank_display(video_adapter_t *adp, int mode)
1314f45c063aSXin LI {
1315f45c063aSXin LI 	/* XXX: use VESA DPMS */
1316f45c063aSXin LI 	return (*prevvidsw->blank_display)(adp, mode);
1317f45c063aSXin LI }
1318f45c063aSXin LI 
1319f45c063aSXin LI static int
1320f45c063aSXin LI vesa_mmap(video_adapter_t *adp, vm_offset_t offset, vm_paddr_t *paddr,
1321f45c063aSXin LI 	  int prot)
1322f45c063aSXin LI {
1323f45c063aSXin LI #if VESA_DEBUG > 0
1324f45c063aSXin LI 	printf("vesa_mmap(): window:0x%x, buffer:0x%x, offset:0x%x\n",
1325f45c063aSXin LI 	       adp->va_info.vi_window, adp->va_info.vi_buffer, offset);
1326f45c063aSXin LI #endif
1327f45c063aSXin LI 
1328f45c063aSXin LI 	if ((adp == vesa_adp) && (adp->va_info.vi_flags & V_INFO_LINEAR)) {
1329f45c063aSXin LI 		/* va_window_size == va_buffer_size/vi_planes */
1330f45c063aSXin LI 		/* XXX: is this correct? */
1331f45c063aSXin LI 		if (offset > adp->va_window_size - PAGE_SIZE)
1332f45c063aSXin LI 			return -1;
1333f45c063aSXin LI 		*paddr = adp->va_info.vi_buffer + offset;
1334f45c063aSXin LI 		return 0;
1335f45c063aSXin LI 	} else {
1336f45c063aSXin LI 		return (*prevvidsw->mmap)(adp, offset, paddr, prot);
1337f45c063aSXin LI 	}
1338f45c063aSXin LI }
1339f45c063aSXin LI 
1340f45c063aSXin LI static int
1341f45c063aSXin LI vesa_clear(video_adapter_t *adp)
1342f45c063aSXin LI {
1343f45c063aSXin LI 	return (*prevvidsw->clear)(adp);
1344f45c063aSXin LI }
1345f45c063aSXin LI 
1346f45c063aSXin LI static int
1347f45c063aSXin LI vesa_fill_rect(video_adapter_t *adp, int val, int x, int y, int cx, int cy)
1348f45c063aSXin LI {
1349f45c063aSXin LI 	return (*prevvidsw->fill_rect)(adp, val, x, y, cx, cy);
1350f45c063aSXin LI }
1351f45c063aSXin LI 
1352f45c063aSXin LI static int
1353f45c063aSXin LI vesa_bitblt(video_adapter_t *adp,...)
1354f45c063aSXin LI {
1355f45c063aSXin LI 	/* FIXME */
1356f45c063aSXin LI 	return 1;
1357f45c063aSXin LI }
1358f45c063aSXin LI 
1359f45c063aSXin LI static int
1360f45c063aSXin LI get_palette(video_adapter_t *adp, int base, int count,
1361f45c063aSXin LI 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1362f45c063aSXin LI {
1363f45c063aSXin LI 	u_char *r;
1364f45c063aSXin LI 	u_char *g;
1365f45c063aSXin LI 	u_char *b;
1366f45c063aSXin LI 	int bits;
1367f45c063aSXin LI 	int error;
1368f45c063aSXin LI 
1369f45c063aSXin LI 	if ((base < 0) || (base >= 256) || (count < 0) || (count > 256))
1370f45c063aSXin LI 		return 1;
1371f45c063aSXin LI 	if ((base + count) > 256)
1372f45c063aSXin LI 		return 1;
1373f45c063aSXin LI 	if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode))
1374f45c063aSXin LI 		return 1;
1375f45c063aSXin LI 
1376f45c063aSXin LI 	bits = vesa_bios_get_dac();
1377f45c063aSXin LI 	if (bits <= 6)
1378f45c063aSXin LI 		return 1;
1379f45c063aSXin LI 
1380f45c063aSXin LI 	r = malloc(count*3, M_DEVBUF, M_WAITOK);
1381f45c063aSXin LI 	g = r + count;
1382f45c063aSXin LI 	b = g + count;
1383f45c063aSXin LI 	error = vesa_bios_save_palette2(base, count, r, g, b, bits);
1384f45c063aSXin LI 	if (error == 0) {
1385f45c063aSXin LI 		copyout(r, red, count);
1386f45c063aSXin LI 		copyout(g, green, count);
1387f45c063aSXin LI 		copyout(b, blue, count);
1388f45c063aSXin LI 		if (trans != NULL) {
1389f45c063aSXin LI 			bzero(r, count);
1390f45c063aSXin LI 			copyout(r, trans, count);
1391f45c063aSXin LI 		}
1392f45c063aSXin LI 	}
1393f45c063aSXin LI 	free(r, M_DEVBUF);
1394f45c063aSXin LI 
1395f45c063aSXin LI 	/* if error && bits != 6 at this point, we are in trouble... XXX */
1396f45c063aSXin LI 	return error;
1397f45c063aSXin LI }
1398f45c063aSXin LI 
1399f45c063aSXin LI static int
1400f45c063aSXin LI set_palette(video_adapter_t *adp, int base, int count,
1401f45c063aSXin LI 	    u_char *red, u_char *green, u_char *blue, u_char *trans)
1402f45c063aSXin LI {
1403f45c063aSXin LI 	return 1;
1404f45c063aSXin LI #ifdef notyet
1405f45c063aSXin LI 	u_char *r;
1406f45c063aSXin LI 	u_char *g;
1407f45c063aSXin LI 	u_char *b;
1408f45c063aSXin LI 	int bits;
1409f45c063aSXin LI 	int error;
1410f45c063aSXin LI 
1411f45c063aSXin LI 	if ((base < 0) || (base >= 256) || (base + count > 256))
1412f45c063aSXin LI 		return 1;
1413f45c063aSXin LI 	if (!(vesa_adp_info->v_flags & V_DAC8) || !VESA_MODE(adp->va_mode)
1414f45c063aSXin LI 		|| ((bits = vesa_bios_set_dac(8)) <= 6))
1415f45c063aSXin LI 		return 1;
1416f45c063aSXin LI 
1417f45c063aSXin LI 	r = malloc(count*3, M_DEVBUF, M_WAITOK);
1418f45c063aSXin LI 	g = r + count;
1419f45c063aSXin LI 	b = g + count;
1420f45c063aSXin LI 	copyin(red, r, count);
1421f45c063aSXin LI 	copyin(green, g, count);
1422f45c063aSXin LI 	copyin(blue, b, count);
1423f45c063aSXin LI 
1424f45c063aSXin LI 	error = vesa_bios_load_palette2(base, count, r, g, b, bits);
1425f45c063aSXin LI 	free(r, M_DEVBUF);
1426f45c063aSXin LI 	if (error == 0)
1427f45c063aSXin LI 		return 0;
1428f45c063aSXin LI 
1429f45c063aSXin LI 	/* if the following call fails, we are in trouble... XXX */
1430f45c063aSXin LI 	vesa_bios_set_dac(6);
1431f45c063aSXin LI 	return 1;
1432f45c063aSXin LI #endif /* notyet */
1433f45c063aSXin LI }
1434f45c063aSXin LI 
1435f45c063aSXin LI static int
1436f45c063aSXin LI vesa_ioctl(video_adapter_t *adp, u_long cmd, caddr_t arg)
1437f45c063aSXin LI {
1438f45c063aSXin LI 	int bytes;
1439f45c063aSXin LI 
1440f45c063aSXin LI 	if (adp != vesa_adp)
1441f45c063aSXin LI 		return (*prevvidsw->ioctl)(adp, cmd, arg);
1442f45c063aSXin LI 
1443f45c063aSXin LI 	switch (cmd) {
1444f45c063aSXin LI 	case FBIO_SETWINORG:	/* set frame buffer window origin */
1445f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1446f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1447f45c063aSXin LI 		return (vesa_set_origin(adp, *(off_t *)arg) ? ENODEV : 0);
1448f45c063aSXin LI 
1449f45c063aSXin LI 	case FBIO_SETDISPSTART:	/* set display start address */
1450f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1451f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1452f45c063aSXin LI 		if (vesa_bios_set_start(((video_display_start_t *)arg)->x,
1453f45c063aSXin LI 					((video_display_start_t *)arg)->y))
1454f45c063aSXin LI 			return ENODEV;
1455f45c063aSXin LI 		adp->va_disp_start.x = ((video_display_start_t *)arg)->x;
1456f45c063aSXin LI 		adp->va_disp_start.y = ((video_display_start_t *)arg)->y;
1457f45c063aSXin LI 		return 0;
1458f45c063aSXin LI 
1459f45c063aSXin LI 	case FBIO_SETLINEWIDTH:	/* set line length in pixel */
1460f45c063aSXin LI 		if (!VESA_MODE(adp->va_mode))
1461f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1462f45c063aSXin LI 		if (vesa_bios_set_line_length(*(u_int *)arg, &bytes, NULL))
1463f45c063aSXin LI 			return ENODEV;
1464f45c063aSXin LI 		adp->va_line_width = bytes;
1465f45c063aSXin LI #if VESA_DEBUG > 1
1466f45c063aSXin LI 		printf("new line width:%d\n", adp->va_line_width);
1467f45c063aSXin LI #endif
1468f45c063aSXin LI 		return 0;
1469f45c063aSXin LI 
1470f45c063aSXin LI 	case FBIO_GETPALETTE:	/* get color palette */
1471f45c063aSXin LI 		if (get_palette(adp, ((video_color_palette_t *)arg)->index,
1472f45c063aSXin LI 				((video_color_palette_t *)arg)->count,
1473f45c063aSXin LI 				((video_color_palette_t *)arg)->red,
1474f45c063aSXin LI 				((video_color_palette_t *)arg)->green,
1475f45c063aSXin LI 				((video_color_palette_t *)arg)->blue,
1476f45c063aSXin LI 				((video_color_palette_t *)arg)->transparent))
1477f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1478f45c063aSXin LI 		return 0;
1479f45c063aSXin LI 
1480f45c063aSXin LI 
1481f45c063aSXin LI 	case FBIO_SETPALETTE:	/* set color palette */
1482f45c063aSXin LI 		if (set_palette(adp, ((video_color_palette_t *)arg)->index,
1483f45c063aSXin LI 				((video_color_palette_t *)arg)->count,
1484f45c063aSXin LI 				((video_color_palette_t *)arg)->red,
1485f45c063aSXin LI 				((video_color_palette_t *)arg)->green,
1486f45c063aSXin LI 				((video_color_palette_t *)arg)->blue,
1487f45c063aSXin LI 				((video_color_palette_t *)arg)->transparent))
1488f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1489f45c063aSXin LI 		return 0;
1490f45c063aSXin LI 
1491f45c063aSXin LI 	case FBIOGETCMAP:	/* get color palette */
1492f45c063aSXin LI 		if (get_palette(adp, ((struct fbcmap *)arg)->index,
1493f45c063aSXin LI 				((struct fbcmap *)arg)->count,
1494f45c063aSXin LI 				((struct fbcmap *)arg)->red,
1495f45c063aSXin LI 				((struct fbcmap *)arg)->green,
1496f45c063aSXin LI 				((struct fbcmap *)arg)->blue, NULL))
1497f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1498f45c063aSXin LI 		return 0;
1499f45c063aSXin LI 
1500f45c063aSXin LI 	case FBIOPUTCMAP:	/* set color palette */
1501f45c063aSXin LI 		if (set_palette(adp, ((struct fbcmap *)arg)->index,
1502f45c063aSXin LI 				((struct fbcmap *)arg)->count,
1503f45c063aSXin LI 				((struct fbcmap *)arg)->red,
1504f45c063aSXin LI 				((struct fbcmap *)arg)->green,
1505f45c063aSXin LI 				((struct fbcmap *)arg)->blue, NULL))
1506f45c063aSXin LI 			return (*prevvidsw->ioctl)(adp, cmd, arg);
1507f45c063aSXin LI 		return 0;
1508f45c063aSXin LI 
1509f45c063aSXin LI 	default:
1510f45c063aSXin LI 		return (*prevvidsw->ioctl)(adp, cmd, arg);
1511f45c063aSXin LI 	}
1512f45c063aSXin LI }
1513f45c063aSXin LI 
1514f45c063aSXin LI static int
1515f45c063aSXin LI vesa_diag(video_adapter_t *adp, int level)
1516f45c063aSXin LI {
1517f45c063aSXin LI 	int error;
1518f45c063aSXin LI 
1519f45c063aSXin LI 	/* call the previous handler first */
1520f45c063aSXin LI 	error = (*prevvidsw->diag)(adp, level);
1521f45c063aSXin LI 	if (error)
1522f45c063aSXin LI 		return error;
1523f45c063aSXin LI 
1524f45c063aSXin LI 	if (adp != vesa_adp)
1525f45c063aSXin LI 		return 1;
1526f45c063aSXin LI 
1527f45c063aSXin LI 	if (level <= 0)
1528f45c063aSXin LI 		return 0;
1529f45c063aSXin LI 
1530f45c063aSXin LI 	return 0;
1531f45c063aSXin LI }
1532f45c063aSXin LI 
1533f45c063aSXin LI static int
1534f45c063aSXin LI vesa_bios_info(int level)
1535f45c063aSXin LI {
1536f45c063aSXin LI #if VESA_DEBUG > 1
1537f45c063aSXin LI 	struct vesa_mode vmode;
1538f45c063aSXin LI 	int i;
1539f45c063aSXin LI #endif
1540f45c063aSXin LI 
1541f45c063aSXin LI 	if (bootverbose) {
1542f45c063aSXin LI 		/* general adapter information */
1543f45c063aSXin LI 		printf(
1544f45c063aSXin LI 	"VESA: v%d.%d, %dk memory, flags:0x%x, mode table:%p (%x)\n",
1545f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0xf000) >> 12) * 10 +
1546f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0x0f00) >> 8),
1547f45c063aSXin LI 		    ((vesa_adp_info->v_version & 0x00f0) >> 4) * 10 +
1548f45c063aSXin LI 		    (vesa_adp_info->v_version & 0x000f),
1549f45c063aSXin LI 		    vesa_adp_info->v_memsize * 64, vesa_adp_info->v_flags,
1550f45c063aSXin LI 		    vesa_vmodetab, vesa_adp_info->v_modetable);
1551f45c063aSXin LI 
1552f45c063aSXin LI 		/* OEM string */
1553f45c063aSXin LI 		if (vesa_oemstr != NULL)
1554f45c063aSXin LI 			printf("VESA: %s\n", vesa_oemstr);
1555f45c063aSXin LI 	}
1556f45c063aSXin LI 
1557f45c063aSXin LI 	if (level <= 0)
1558f45c063aSXin LI 		return 0;
1559f45c063aSXin LI 
1560f45c063aSXin LI 	if (vesa_adp_info->v_version >= 0x0200 && bootverbose) {
1561f45c063aSXin LI 		/* vender name, product name, product revision */
1562f45c063aSXin LI 		printf("VESA: %s %s %s\n",
1563f45c063aSXin LI 			(vesa_venderstr != NULL) ? vesa_venderstr : "unknown",
1564f45c063aSXin LI 			(vesa_prodstr != NULL) ? vesa_prodstr : "unknown",
1565f45c063aSXin LI 			(vesa_revstr != NULL) ? vesa_revstr : "?");
1566f45c063aSXin LI 	}
1567f45c063aSXin LI 
1568f45c063aSXin LI #if VESA_DEBUG > 1
1569f45c063aSXin LI 	/* mode information */
1570f45c063aSXin LI 	for (i = 0;
1571f45c063aSXin LI 		(i < (M_VESA_MODE_MAX - M_VESA_BASE + 1))
1572f45c063aSXin LI 		&& (vesa_vmodetab[i] != 0xffff); ++i) {
1573f45c063aSXin LI 		if (vesa_bios_get_mode(vesa_vmodetab[i], &vmode))
1574f45c063aSXin LI 			continue;
1575f45c063aSXin LI 
1576f45c063aSXin LI 		/* print something for diagnostic purpose */
1577f45c063aSXin LI 		printf("VESA: mode:0x%03x, flags:0x%04x",
1578f45c063aSXin LI 		       vesa_vmodetab[i], vmode.v_modeattr);
1579f45c063aSXin LI 		if (vmode.v_modeattr & V_MODEOPTINFO) {
1580f45c063aSXin LI 			if (vmode.v_modeattr & V_MODEGRAPHICS) {
1581f45c063aSXin LI 				printf(", G %dx%dx%d %d, ",
1582f45c063aSXin LI 				       vmode.v_width, vmode.v_height,
1583f45c063aSXin LI 				       vmode.v_bpp, vmode.v_planes);
1584f45c063aSXin LI 			} else {
1585f45c063aSXin LI 				printf(", T %dx%d, ",
1586f45c063aSXin LI 				       vmode.v_width, vmode.v_height);
1587f45c063aSXin LI 			}
1588f45c063aSXin LI 			printf("font:%dx%d, ",
1589f45c063aSXin LI 			       vmode.v_cwidth, vmode.v_cheight);
1590f45c063aSXin LI 			printf("pages:%d, mem:%d",
1591f45c063aSXin LI 			       vmode.v_ipages + 1, vmode.v_memmodel);
1592f45c063aSXin LI 		}
1593f45c063aSXin LI 		if (vmode.v_modeattr & V_MODELFB) {
1594f45c063aSXin LI 			printf("\nVESA: LFB:0x%x, off:0x%x, off_size:0x%x",
1595f45c063aSXin LI 			       vmode.v_lfb, vmode.v_offscreen,
1596f45c063aSXin LI 			       vmode.v_offscreensize*1024);
1597f45c063aSXin LI 		}
1598f45c063aSXin LI 		printf("\n");
1599f45c063aSXin LI 		printf("VESA: window A:0x%x (%x), window B:0x%x (%x), ",
1600f45c063aSXin LI 		       vmode.v_waseg, vmode.v_waattr,
1601f45c063aSXin LI 		       vmode.v_wbseg, vmode.v_wbattr);
1602f45c063aSXin LI 		printf("size:%dk, gran:%dk\n",
1603f45c063aSXin LI 		       vmode.v_wsize, vmode.v_wgran);
1604f45c063aSXin LI 	}
1605f45c063aSXin LI #endif /* VESA_DEBUG > 1 */
1606f45c063aSXin LI 
1607f45c063aSXin LI 	return 0;
1608f45c063aSXin LI }
1609f45c063aSXin LI 
1610f45c063aSXin LI /* module loading */
1611f45c063aSXin LI 
1612f45c063aSXin LI static int
1613f45c063aSXin LI vesa_load(void)
1614f45c063aSXin LI {
1615f45c063aSXin LI 	int error;
1616f45c063aSXin LI 	int s;
1617f45c063aSXin LI 
1618f45c063aSXin LI 	if (vesa_init_done)
1619f45c063aSXin LI 		return 0;
1620f45c063aSXin LI 
1621f45c063aSXin LI 	/* locate a VGA adapter */
1622f45c063aSXin LI 	s = spltty();
1623f45c063aSXin LI 	vesa_adp = NULL;
1624f45c063aSXin LI 	error = vesa_configure(0);
1625f45c063aSXin LI 	splx(s);
1626f45c063aSXin LI 
1627f45c063aSXin LI 	if (error == 0)
1628f45c063aSXin LI 		vesa_bios_info(bootverbose);
1629f45c063aSXin LI 
1630f45c063aSXin LI 	return error;
1631f45c063aSXin LI }
1632f45c063aSXin LI 
1633f45c063aSXin LI static int
1634f45c063aSXin LI vesa_unload(void)
1635f45c063aSXin LI {
1636f45c063aSXin LI 	u_char palette[256*3];
1637f45c063aSXin LI 	int error;
1638f45c063aSXin LI 	int bits;
1639f45c063aSXin LI 	int s;
1640f45c063aSXin LI 
1641f45c063aSXin LI 	/* if the adapter is currently in a VESA mode, don't unload */
1642f45c063aSXin LI 	if ((vesa_adp != NULL) && VESA_MODE(vesa_adp->va_mode))
1643f45c063aSXin LI 		return EBUSY;
1644f45c063aSXin LI 	/*
1645f45c063aSXin LI 	 * FIXME: if there is at least one vty which is in a VESA mode,
1646f45c063aSXin LI 	 * we shouldn't be unloading! XXX
1647f45c063aSXin LI 	 */
1648f45c063aSXin LI 
1649f45c063aSXin LI 	s = spltty();
1650f45c063aSXin LI 	if ((error = vesa_unload_ioctl()) == 0) {
1651f45c063aSXin LI 		if (vesa_adp != NULL) {
1652f45c063aSXin LI 			if (vesa_adp_info->v_flags & V_DAC8)  {
1653f45c063aSXin LI 				bits = vesa_bios_get_dac();
1654f45c063aSXin LI 				if (bits > 6) {
1655f45c063aSXin LI 					vesa_bios_save_palette(0, 256,
1656f45c063aSXin LI 							       palette, bits);
1657f45c063aSXin LI 					vesa_bios_set_dac(6);
1658f45c063aSXin LI 					vesa_bios_load_palette(0, 256,
1659f45c063aSXin LI 							       palette, 6);
1660f45c063aSXin LI 				}
1661f45c063aSXin LI 			}
1662f45c063aSXin LI 			vesa_adp->va_flags &= ~V_ADP_VESA;
1663f45c063aSXin LI 			vidsw[vesa_adp->va_index] = prevvidsw;
1664f45c063aSXin LI 		}
1665f45c063aSXin LI 	}
1666f45c063aSXin LI 	splx(s);
1667f45c063aSXin LI 
1668f45c063aSXin LI 	if (vesa_vm86_buf != NULL)
1669f45c063aSXin LI 		free(vesa_vm86_buf, M_DEVBUF);
1670f45c063aSXin LI 
1671f45c063aSXin LI 	return error;
1672f45c063aSXin LI }
1673f45c063aSXin LI 
1674f45c063aSXin LI static int
1675f45c063aSXin LI vesa_mod_event(module_t mod, int type, void *data)
1676f45c063aSXin LI {
1677f45c063aSXin LI 	switch (type) {
1678f45c063aSXin LI 	case MOD_LOAD:
1679f45c063aSXin LI 		return vesa_load();
1680f45c063aSXin LI 	case MOD_UNLOAD:
1681f45c063aSXin LI 		return vesa_unload();
1682f45c063aSXin LI 	default:
1683f45c063aSXin LI 		return EOPNOTSUPP;
1684f45c063aSXin LI 	}
1685f45c063aSXin LI 	return 0;
1686f45c063aSXin LI }
1687f45c063aSXin LI 
1688f45c063aSXin LI static moduledata_t vesa_mod = {
1689f45c063aSXin LI 	"vesa",
1690f45c063aSXin LI 	vesa_mod_event,
1691f45c063aSXin LI 	NULL,
1692f45c063aSXin LI };
1693f45c063aSXin LI 
1694f45c063aSXin LI DECLARE_MODULE(vesa, vesa_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1695f45c063aSXin LI 
1696f45c063aSXin LI #endif	/* VGA_NO_MODE_CHANGE */
1697