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