Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* -*- linux-c -*- ------------------------------------------------------- *
0003  *
0004  *   Copyright (C) 1991, 1992 Linus Torvalds
0005  *   Copyright 2007-2008 rPath, Inc. - All Rights Reserved
0006  *
0007  * ----------------------------------------------------------------------- */
0008 
0009 /*
0010  * arch/i386/boot/video-mode.c
0011  *
0012  * Set the video mode.  This is separated out into a different
0013  * file in order to be shared with the ACPI wakeup code.
0014  */
0015 
0016 #include "boot.h"
0017 #include "video.h"
0018 #include "vesa.h"
0019 
0020 #include <uapi/asm/boot.h>
0021 
0022 /*
0023  * Common variables
0024  */
0025 int adapter;        /* 0=CGA/MDA/HGC, 1=EGA, 2=VGA+ */
0026 int force_x, force_y;   /* Don't query the BIOS for cols/rows */
0027 int do_restore;     /* Screen contents changed during mode flip */
0028 int graphic_mode;   /* Graphic mode with linear frame buffer */
0029 
0030 /* Probe the video drivers and have them generate their mode lists. */
0031 void probe_cards(int unsafe)
0032 {
0033     struct card_info *card;
0034     static u8 probed[2];
0035 
0036     if (probed[unsafe])
0037         return;
0038 
0039     probed[unsafe] = 1;
0040 
0041     for (card = video_cards; card < video_cards_end; card++) {
0042         if (card->unsafe == unsafe) {
0043             if (card->probe)
0044                 card->nmodes = card->probe();
0045             else
0046                 card->nmodes = 0;
0047         }
0048     }
0049 }
0050 
0051 /* Test if a mode is defined */
0052 int mode_defined(u16 mode)
0053 {
0054     struct card_info *card;
0055     struct mode_info *mi;
0056     int i;
0057 
0058     for (card = video_cards; card < video_cards_end; card++) {
0059         mi = card->modes;
0060         for (i = 0; i < card->nmodes; i++, mi++) {
0061             if (mi->mode == mode)
0062                 return 1;
0063         }
0064     }
0065 
0066     return 0;
0067 }
0068 
0069 /* Set mode (without recalc) */
0070 static int raw_set_mode(u16 mode, u16 *real_mode)
0071 {
0072     int nmode, i;
0073     struct card_info *card;
0074     struct mode_info *mi;
0075 
0076     /* Drop the recalc bit if set */
0077     mode &= ~VIDEO_RECALC;
0078 
0079     /* Scan for mode based on fixed ID, position, or resolution */
0080     nmode = 0;
0081     for (card = video_cards; card < video_cards_end; card++) {
0082         mi = card->modes;
0083         for (i = 0; i < card->nmodes; i++, mi++) {
0084             int visible = mi->x || mi->y;
0085 
0086             if ((mode == nmode && visible) ||
0087                 mode == mi->mode ||
0088                 mode == (mi->y << 8)+mi->x) {
0089                 *real_mode = mi->mode;
0090                 return card->set_mode(mi);
0091             }
0092 
0093             if (visible)
0094                 nmode++;
0095         }
0096     }
0097 
0098     /* Nothing found?  Is it an "exceptional" (unprobed) mode? */
0099     for (card = video_cards; card < video_cards_end; card++) {
0100         if (mode >= card->xmode_first &&
0101             mode < card->xmode_first+card->xmode_n) {
0102             struct mode_info mix;
0103             *real_mode = mix.mode = mode;
0104             mix.x = mix.y = 0;
0105             return card->set_mode(&mix);
0106         }
0107     }
0108 
0109     /* Otherwise, failure... */
0110     return -1;
0111 }
0112 
0113 /*
0114  * Recalculate the vertical video cutoff (hack!)
0115  */
0116 static void vga_recalc_vertical(void)
0117 {
0118     unsigned int font_size, rows;
0119     u16 crtc;
0120     u8 pt, ov;
0121 
0122     set_fs(0);
0123     font_size = rdfs8(0x485); /* BIOS: font size (pixels) */
0124     rows = force_y ? force_y : rdfs8(0x484)+1; /* Text rows */
0125 
0126     rows *= font_size;  /* Visible scan lines */
0127     rows--;         /* ... minus one */
0128 
0129     crtc = vga_crtc();
0130 
0131     pt = in_idx(crtc, 0x11);
0132     pt &= ~0x80;        /* Unlock CR0-7 */
0133     out_idx(pt, crtc, 0x11);
0134 
0135     out_idx((u8)rows, crtc, 0x12); /* Lower height register */
0136 
0137     ov = in_idx(crtc, 0x07); /* Overflow register */
0138     ov &= 0xbd;
0139     ov |= (rows >> (8-1)) & 0x02;
0140     ov |= (rows >> (9-6)) & 0x40;
0141     out_idx(ov, crtc, 0x07);
0142 }
0143 
0144 /* Set mode (with recalc if specified) */
0145 int set_mode(u16 mode)
0146 {
0147     int rv;
0148     u16 real_mode;
0149 
0150     /* Very special mode numbers... */
0151     if (mode == VIDEO_CURRENT_MODE)
0152         return 0;   /* Nothing to do... */
0153     else if (mode == NORMAL_VGA)
0154         mode = VIDEO_80x25;
0155     else if (mode == EXTENDED_VGA)
0156         mode = VIDEO_8POINT;
0157 
0158     rv = raw_set_mode(mode, &real_mode);
0159     if (rv)
0160         return rv;
0161 
0162     if (mode & VIDEO_RECALC)
0163         vga_recalc_vertical();
0164 
0165     /* Save the canonical mode number for the kernel, not
0166        an alias, size specification or menu position */
0167 #ifndef _WAKEUP
0168     boot_params.hdr.vid_mode = real_mode;
0169 #endif
0170     return 0;
0171 }