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