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