The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/fb/vesa.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: c6b1c199cd35b8d683d4c721b102bdbb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.