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