vgacon.c 41.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/*
 *  linux/drivers/video/vgacon.c -- Low level VGA based console driver
 *
 *	Created 28 Sep 1997 by Geert Uytterhoeven
 *
 *	Rewritten by Martin Mares <mj@ucw.cz>, July 1998
 *
 *  This file is based on the old console.c, vga.c and vesa_blank.c drivers.
 *
 *	Copyright (C) 1991, 1992  Linus Torvalds
 *			    1995  Jay Estabrook
 *
 *	User definable mapping table and font loading by Eugene G. Crosser,
 *	<crosser@average.org>
 *
 *	Improved loadable font/UTF-8 support by H. Peter Anvin
 *	Feb-Sep 1995 <peter.anvin@linux.org>
 *
 *	Colour palette handling, by Simon Tatham
 *	17-Jun-95 <sgt20@cam.ac.uk>
 *
 *	if 512 char mode is already enabled don't re-enable it,
 *	because it causes screen to flicker, by Mitja Horvat
 *	5-May-96 <mitja.horvat@guest.arnes.si>
 *
 *	Use 2 outw instead of 4 outb_p to reduce erroneous text
 *	flashing on RHS of screen during heavy console scrolling .
 *	Oct 1996, Paul Gortmaker.
 *
 *
 *  This file is subject to the terms and conditions of the GNU General Public
 *  License.  See the file COPYING in the main directory of this archive for
 *  more details.
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/kernel.h>
#include <linux/console.h>
#include <linux/string.h>
#include <linux/kd.h>
#include <linux/slab.h>
#include <linux/vt_kern.h>
D
Dave Airlie 已提交
45
#include <linux/sched.h>
L
Linus Torvalds 已提交
46 47 48 49
#include <linux/selection.h>
#include <linux/spinlock.h>
#include <linux/ioport.h>
#include <linux/init.h>
50
#include <linux/screen_info.h>
L
Linus Torvalds 已提交
51 52 53
#include <video/vga.h>
#include <asm/io.h>

54
static DEFINE_RAW_SPINLOCK(vga_lock);
L
Linus Torvalds 已提交
55 56
static int cursor_size_lastfrom;
static int cursor_size_lastto;
57 58
static u32 vgacon_xres;
static u32 vgacon_yres;
59
static struct vgastate vgastate;
L
Linus Torvalds 已提交
60 61 62

#define BLANK 0x0020

63
#define VGA_FONTWIDTH       8   /* VGA does not support fontwidths != 8 */
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73
/*
 *  Interface used by the world
 */

static const char *vgacon_startup(void);
static void vgacon_init(struct vc_data *c, int init);
static void vgacon_deinit(struct vc_data *c);
static void vgacon_cursor(struct vc_data *c, int mode);
static int vgacon_switch(struct vc_data *c);
static int vgacon_blank(struct vc_data *c, int blank, int mode_switch);
74
static void vgacon_scrolldelta(struct vc_data *c, int lines);
L
Linus Torvalds 已提交
75 76 77
static int vgacon_set_origin(struct vc_data *c);
static void vgacon_save_screen(struct vc_data *c);
static void vgacon_invert_region(struct vc_data *c, u16 * p, int count);
78
static struct uni_pagedir *vgacon_uni_pagedir;
T
Takashi Iwai 已提交
79
static int vgacon_refcount;
L
Linus Torvalds 已提交
80 81

/* Description of the hardware situation */
82
static bool		vga_init_done;
83 84 85 86 87 88 89
static unsigned long	vga_vram_base		__read_mostly;	/* Base of video memory */
static unsigned long	vga_vram_end		__read_mostly;	/* End of video memory */
static unsigned int	vga_vram_size		__read_mostly;	/* Size of video memory */
static u16		vga_video_port_reg	__read_mostly;	/* Video register select port */
static u16		vga_video_port_val	__read_mostly;	/* Video register value port */
static unsigned int	vga_video_num_columns;			/* Number of text columns */
static unsigned int	vga_video_num_lines;			/* Number of text lines */
90
static bool		vga_can_do_color;			/* Do we support colors? */
91 92
static unsigned int	vga_default_font_height __read_mostly;	/* Height of default screen font */
static unsigned char	vga_video_type		__read_mostly;	/* Card type */
93
static bool		vga_font_is_default = true;
L
Linus Torvalds 已提交
94
static int		vga_vesa_blanked;
95 96 97
static bool 		vga_palette_blanked;
static bool 		vga_is_gfx;
static bool 		vga_512_chars;
L
Linus Torvalds 已提交
98
static int 		vga_video_font_height;
99 100
static int 		vga_scan_lines		__read_mostly;
static unsigned int 	vga_rolled_over;
L
Linus Torvalds 已提交
101

102 103 104
static bool vgacon_text_mode_force;
static bool vga_hardscroll_enabled;
static bool vga_hardscroll_user_enable = true;
D
Dave Airlie 已提交
105 106 107

bool vgacon_text_force(void)
{
108
	return vgacon_text_mode_force;
D
Dave Airlie 已提交
109 110 111 112 113
}
EXPORT_SYMBOL(vgacon_text_force);

static int __init text_mode(char *str)
{
114
	vgacon_text_mode_force = true;
115 116 117 118 119

	pr_warning("You have booted with nomodeset. This means your GPU drivers are DISABLED\n");
	pr_warning("Any video related functionality will be severely degraded, and you may not even be able to suspend the system properly\n");
	pr_warning("Unless you actually understand what nomodeset does, you should reboot without enabling it\n");

D
Dave Airlie 已提交
120 121 122 123 124 125
	return 1;
}

/* force text mode - used by kernel modesetting */
__setup("nomodeset", text_mode);

L
Linus Torvalds 已提交
126 127 128 129 130 131 132
static int __init no_scroll(char *str)
{
	/*
	 * Disabling scrollback is required for the Braillex ib80-piezo
	 * Braille reader made by F.H. Papenmeier (Germany).
	 * Use the "no-scroll" bootflag.
	 */
133
	vga_hardscroll_user_enable = vga_hardscroll_enabled = false;
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
	return 1;
}

__setup("no-scroll", no_scroll);

/*
 * By replacing the four outb_p with two back to back outw, we can reduce
 * the window of opportunity to see text mislocated to the RHS of the
 * console during heavy scrolling activity. However there is the remote
 * possibility that some pre-dinosaur hardware won't like the back to back
 * I/O. Since the Xservers get away with it, we should be able to as well.
 */
static inline void write_vga(unsigned char reg, unsigned int val)
{
	unsigned int v1, v2;
	unsigned long flags;

	/*
	 * ddprintk might set the console position from interrupt
	 * handlers, thus the write has to be IRQ-atomic.
	 */
155
	raw_spin_lock_irqsave(&vga_lock, flags);
L
Linus Torvalds 已提交
156 157 158 159
	v1 = reg + (val & 0xff00);
	v2 = reg + 1 + ((val << 8) & 0xff00);
	outw(v1, vga_video_port_reg);
	outw(v2, vga_video_port_reg);
160
	raw_spin_unlock_irqrestore(&vga_lock, flags);
L
Linus Torvalds 已提交
161 162
}

163 164 165 166 167 168 169
static inline void vga_set_mem_top(struct vc_data *c)
{
	write_vga(12, (c->vc_visible_origin - vga_vram_base) / 2);
}

#ifdef CONFIG_VGACON_SOFT_SCROLLBACK
/* software scrollback */
170
struct vgacon_scrollback_info {
171 172 173 174 175 176 177 178
	void *data;
	int tail;
	int size;
	int rows;
	int cnt;
	int cur;
	int save;
	int restore;
179
};
180

181 182
static struct vgacon_scrollback_info *vgacon_scrollback_cur;
static struct vgacon_scrollback_info vgacon_scrollbacks[MAX_NR_CONSOLES];
183 184 185 186
static bool scrollback_persistent = \
	IS_ENABLED(CONFIG_VGACON_SOFT_SCROLLBACK_PERSISTENT_ENABLE_BY_DEFAULT);
module_param_named(scrollback_persistent, scrollback_persistent, bool, 0000);
MODULE_PARM_DESC(scrollback_persistent, "Enable persistent scrollback for all vga consoles");
187 188 189 190 191 192 193 194 195 196 197 198 199 200

static void vgacon_scrollback_reset(int vc_num, size_t reset_size)
{
	struct vgacon_scrollback_info *scrollback = &vgacon_scrollbacks[vc_num];

	if (scrollback->data && reset_size > 0)
		memset(scrollback->data, 0, reset_size);

	scrollback->cnt  = 0;
	scrollback->tail = 0;
	scrollback->cur  = 0;
}

static void vgacon_scrollback_init(int vc_num)
201
{
202 203 204 205
	int pitch = vga_video_num_columns * 2;
	size_t size = CONFIG_VGACON_SOFT_SCROLLBACK_SIZE * 1024;
	int rows = size / pitch;
	void *data;
206

207 208 209 210 211 212 213 214 215 216
	data = kmalloc_array(CONFIG_VGACON_SOFT_SCROLLBACK_SIZE, 1024,
			     GFP_NOWAIT);

	vgacon_scrollbacks[vc_num].data = data;
	vgacon_scrollback_cur = &vgacon_scrollbacks[vc_num];

	vgacon_scrollback_cur->rows = rows - 1;
	vgacon_scrollback_cur->size = rows * pitch;

	vgacon_scrollback_reset(vc_num, size);
217 218
}

219
static void vgacon_scrollback_switch(int vc_num)
220
{
221 222
	if (!scrollback_persistent)
		vc_num = 0;
223 224 225 226

	if (!vgacon_scrollbacks[vc_num].data) {
		vgacon_scrollback_init(vc_num);
	} else {
227 228 229 230
		if (scrollback_persistent) {
			vgacon_scrollback_cur = &vgacon_scrollbacks[vc_num];
		} else {
			size_t size = CONFIG_VGACON_SOFT_SCROLLBACK_SIZE * 1024;
231

232 233
			vgacon_scrollback_reset(vc_num, size);
		}
234 235 236
	}
}

237
static void vgacon_scrollback_startup(void)
238
{
239 240
	vgacon_scrollback_cur = &vgacon_scrollbacks[0];
	vgacon_scrollback_init(0);
241 242 243 244 245 246
}

static void vgacon_scrollback_update(struct vc_data *c, int t, int count)
{
	void *p;

247 248
	if (!vgacon_scrollback_cur->data || !vgacon_scrollback_cur->size ||
	    c->vc_num != fg_console)
249 250 251 252 253
		return;

	p = (void *) (c->vc_origin + t * c->vc_size_row);

	while (count--) {
254 255
		scr_memcpyw(vgacon_scrollback_cur->data +
			    vgacon_scrollback_cur->tail,
256
			    p, c->vc_size_row);
257 258

		vgacon_scrollback_cur->cnt++;
259
		p += c->vc_size_row;
260
		vgacon_scrollback_cur->tail += c->vc_size_row;
261

262 263
		if (vgacon_scrollback_cur->tail >= vgacon_scrollback_cur->size)
			vgacon_scrollback_cur->tail = 0;
264

265 266
		if (vgacon_scrollback_cur->cnt > vgacon_scrollback_cur->rows)
			vgacon_scrollback_cur->cnt = vgacon_scrollback_cur->rows;
267

268
		vgacon_scrollback_cur->cur = vgacon_scrollback_cur->cnt;
269 270 271 272 273
	}
}

static void vgacon_restore_screen(struct vc_data *c)
{
274
	c->vc_origin = c->vc_visible_origin;
275
	vgacon_scrollback_cur->save = 0;
276

277
	if (!vga_is_gfx && !vgacon_scrollback_cur->restore) {
278 279 280
		scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
			    c->vc_screenbuf_size > vga_vram_size ?
			    vga_vram_size : c->vc_screenbuf_size);
281 282
		vgacon_scrollback_cur->restore = 1;
		vgacon_scrollback_cur->cur = vgacon_scrollback_cur->cnt;
283 284 285
	}
}

286
static void vgacon_scrolldelta(struct vc_data *c, int lines)
287
{
288
	int start, end, count, soff;
289 290

	if (!lines) {
291
		vgacon_restore_screen(c);
292
		return;
293 294
	}

295
	if (!vgacon_scrollback_cur->data)
296
		return;
297

298
	if (!vgacon_scrollback_cur->save) {
299 300
		vgacon_cursor(c, CM_ERASE);
		vgacon_save_screen(c);
301
		c->vc_origin = (unsigned long)c->vc_screenbuf;
302
		vgacon_scrollback_cur->save = 1;
303 304
	}

305 306
	vgacon_scrollback_cur->restore = 0;
	start = vgacon_scrollback_cur->cur + lines;
307 308 309 310 311
	end = start + abs(lines);

	if (start < 0)
		start = 0;

312 313
	if (start > vgacon_scrollback_cur->cnt)
		start = vgacon_scrollback_cur->cnt;
314 315 316 317

	if (end < 0)
		end = 0;

318 319
	if (end > vgacon_scrollback_cur->cnt)
		end = vgacon_scrollback_cur->cnt;
320

321
	vgacon_scrollback_cur->cur = start;
322
	count = end - start;
323 324
	soff = vgacon_scrollback_cur->tail -
		((vgacon_scrollback_cur->cnt - end) * c->vc_size_row);
325 326 327
	soff -= count * c->vc_size_row;

	if (soff < 0)
328
		soff += vgacon_scrollback_cur->size;
329

330
	count = vgacon_scrollback_cur->cnt - start;
331 332 333 334

	if (count > c->vc_rows)
		count = c->vc_rows;

M
Marcin Slusarz 已提交
335 336
	if (count) {
		int copysize;
337 338

		int diff = c->vc_rows - count;
339
		void *d = (void *) c->vc_visible_origin;
340 341
		void *s = (void *) c->vc_screenbuf;

M
Marcin Slusarz 已提交
342 343
		count *= c->vc_size_row;
		/* how much memory to end of buffer left? */
344 345
		copysize = min(count, vgacon_scrollback_cur->size - soff);
		scr_memcpyw(d, vgacon_scrollback_cur->data + soff, copysize);
M
Marcin Slusarz 已提交
346 347 348 349
		d += copysize;
		count -= copysize;

		if (count) {
350
			scr_memcpyw(d, vgacon_scrollback_cur->data, count);
M
Marcin Slusarz 已提交
351 352
			d += count;
		}
353

M
Marcin Slusarz 已提交
354 355
		if (diff)
			scr_memcpyw(d, s, diff * c->vc_size_row);
356 357
	} else
		vgacon_cursor(c, CM_MOVE);
358
}
359 360 361 362 363

static void vgacon_flush_scrollback(struct vc_data *c)
{
	size_t size = CONFIG_VGACON_SOFT_SCROLLBACK_SIZE * 1024;

364
	vgacon_scrollback_reset(c->vc_num, size);
365
}
366 367 368 369
#else
#define vgacon_scrollback_startup(...) do { } while (0)
#define vgacon_scrollback_init(...)    do { } while (0)
#define vgacon_scrollback_update(...)  do { } while (0)
370
#define vgacon_scrollback_switch(...)  do { } while (0)
371 372 373 374 375 376 377

static void vgacon_restore_screen(struct vc_data *c)
{
	if (c->vc_origin != c->vc_visible_origin)
		vgacon_scrolldelta(c, 0);
}

378
static void vgacon_scrolldelta(struct vc_data *c, int lines)
379
{
380 381
	vc_scrolldelta_helper(c, lines, vga_rolled_over, (void *)vga_vram_base,
			vga_vram_size);
382 383
	vga_set_mem_top(c);
}
384 385 386 387

static void vgacon_flush_scrollback(struct vc_data *c)
{
}
388 389
#endif /* CONFIG_VGACON_SOFT_SCROLLBACK */

390
static const char *vgacon_startup(void)
L
Linus Torvalds 已提交
391 392 393 394 395
{
	const char *display_desc = NULL;
	u16 saved1, saved2;
	volatile u16 *p;

Y
Yannick Heneault 已提交
396 397
	if (screen_info.orig_video_isVGA == VIDEO_TYPE_VLFB ||
	    screen_info.orig_video_isVGA == VIDEO_TYPE_EFI) {
L
Linus Torvalds 已提交
398 399 400 401 402 403 404 405 406
	      no_vga:
#ifdef CONFIG_DUMMY_CONSOLE
		conswitchp = &dummy_con;
		return conswitchp->con_startup();
#else
		return NULL;
#endif
	}

407 408
	/* boot_params.screen_info reasonably initialized? */
	if ((screen_info.orig_video_lines == 0) ||
409
	    (screen_info.orig_video_cols  == 0))
410 411
		goto no_vga;

L
Linus Torvalds 已提交
412
	/* VGA16 modes are not handled by VGACON */
413 414 415 416 417
	if ((screen_info.orig_video_mode == 0x0D) ||	/* 320x200/4 */
	    (screen_info.orig_video_mode == 0x0E) ||	/* 640x200/4 */
	    (screen_info.orig_video_mode == 0x10) ||	/* 640x350/4 */
	    (screen_info.orig_video_mode == 0x12) ||	/* 640x480/4 */
	    (screen_info.orig_video_mode == 0x6A))	/* 800x600/4 (VESA) */
L
Linus Torvalds 已提交
418 419
		goto no_vga;

420 421
	vga_video_num_lines = screen_info.orig_video_lines;
	vga_video_num_columns = screen_info.orig_video_cols;
422
	vgastate.vgabase = NULL;
L
Linus Torvalds 已提交
423

424 425
	if (screen_info.orig_video_mode == 7) {
		/* Monochrome display */
L
Linus Torvalds 已提交
426 427 428
		vga_vram_base = 0xb0000;
		vga_video_port_reg = VGA_CRT_IM;
		vga_video_port_val = VGA_CRT_DM;
429
		if ((screen_info.orig_video_ega_bx & 0xff) != 0x10) {
L
Linus Torvalds 已提交
430
			static struct resource ega_console_resource =
431 432 433 434
			    { .name	= "ega",
			      .flags	= IORESOURCE_IO,
			      .start	= 0x3B0,
			      .end	= 0x3BF };
L
Linus Torvalds 已提交
435
			vga_video_type = VIDEO_TYPE_EGAM;
436
			vga_vram_size = 0x8000;
L
Linus Torvalds 已提交
437 438 439 440 441
			display_desc = "EGA+";
			request_resource(&ioport_resource,
					 &ega_console_resource);
		} else {
			static struct resource mda1_console_resource =
442 443 444 445
			    { .name	= "mda",
			      .flags	= IORESOURCE_IO,
			      .start	= 0x3B0,
			      .end	= 0x3BB };
L
Linus Torvalds 已提交
446
			static struct resource mda2_console_resource =
447 448 449 450
			    { .name	= "mda",
			      .flags	= IORESOURCE_IO,
			      .start	= 0x3BF,
			      .end	= 0x3BF };
L
Linus Torvalds 已提交
451
			vga_video_type = VIDEO_TYPE_MDA;
452
			vga_vram_size = 0x2000;
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460 461
			display_desc = "*MDA";
			request_resource(&ioport_resource,
					 &mda1_console_resource);
			request_resource(&ioport_resource,
					 &mda2_console_resource);
			vga_video_font_height = 14;
		}
	} else {
		/* If not, it is color. */
462
		vga_can_do_color = true;
L
Linus Torvalds 已提交
463 464 465
		vga_vram_base = 0xb8000;
		vga_video_port_reg = VGA_CRT_IC;
		vga_video_port_val = VGA_CRT_DC;
466
		if ((screen_info.orig_video_ega_bx & 0xff) != 0x10) {
L
Linus Torvalds 已提交
467 468
			int i;

469
			vga_vram_size = 0x8000;
L
Linus Torvalds 已提交
470

471
			if (!screen_info.orig_video_isVGA) {
472 473 474 475 476
				static struct resource ega_console_resource =
				    { .name	= "ega",
				      .flags	= IORESOURCE_IO,
				      .start	= 0x3C0,
				      .end	= 0x3DF };
L
Linus Torvalds 已提交
477 478 479 480 481
				vga_video_type = VIDEO_TYPE_EGAC;
				display_desc = "EGA";
				request_resource(&ioport_resource,
						 &ega_console_resource);
			} else {
482 483 484 485 486
				static struct resource vga_console_resource =
				    { .name	= "vga+",
				      .flags	= IORESOURCE_IO,
				      .start	= 0x3C0,
				      .end	= 0x3DF };
L
Linus Torvalds 已提交
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
				vga_video_type = VIDEO_TYPE_VGAC;
				display_desc = "VGA+";
				request_resource(&ioport_resource,
						 &vga_console_resource);

				/*
				 * Normalise the palette registers, to point
				 * the 16 screen colours to the first 16
				 * DAC entries.
				 */

				for (i = 0; i < 16; i++) {
					inb_p(VGA_IS1_RC);
					outb_p(i, VGA_ATT_W);
					outb_p(i, VGA_ATT_W);
				}
				outb_p(0x20, VGA_ATT_W);

				/*
				 * Now set the DAC registers back to their
				 * default values
				 */
				for (i = 0; i < 16; i++) {
					outb_p(color_table[i], VGA_PEL_IW);
					outb_p(default_red[i], VGA_PEL_D);
					outb_p(default_grn[i], VGA_PEL_D);
					outb_p(default_blu[i], VGA_PEL_D);
				}
			}
		} else {
			static struct resource cga_console_resource =
518 519 520 521
			    { .name	= "cga",
			      .flags	= IORESOURCE_IO,
			      .start	= 0x3D4,
			      .end	= 0x3D5 };
L
Linus Torvalds 已提交
522
			vga_video_type = VIDEO_TYPE_CGA;
523
			vga_vram_size = 0x2000;
L
Linus Torvalds 已提交
524 525 526 527 528 529 530
			display_desc = "*CGA";
			request_resource(&ioport_resource,
					 &cga_console_resource);
			vga_video_font_height = 8;
		}
	}

531 532
	vga_vram_base = VGA_MAP_MEM(vga_vram_base, vga_vram_size);
	vga_vram_end = vga_vram_base + vga_vram_size;
L
Linus Torvalds 已提交
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561

	/*
	 *      Find out if there is a graphics card present.
	 *      Are there smarter methods around?
	 */
	p = (volatile u16 *) vga_vram_base;
	saved1 = scr_readw(p);
	saved2 = scr_readw(p + 1);
	scr_writew(0xAA55, p);
	scr_writew(0x55AA, p + 1);
	if (scr_readw(p) != 0xAA55 || scr_readw(p + 1) != 0x55AA) {
		scr_writew(saved1, p);
		scr_writew(saved2, p + 1);
		goto no_vga;
	}
	scr_writew(0x55AA, p);
	scr_writew(0xAA55, p + 1);
	if (scr_readw(p) != 0x55AA || scr_readw(p + 1) != 0xAA55) {
		scr_writew(saved1, p);
		scr_writew(saved2, p + 1);
		goto no_vga;
	}
	scr_writew(saved1, p);
	scr_writew(saved2, p + 1);

	if (vga_video_type == VIDEO_TYPE_EGAC
	    || vga_video_type == VIDEO_TYPE_VGAC
	    || vga_video_type == VIDEO_TYPE_EGAM) {
		vga_hardscroll_enabled = vga_hardscroll_user_enable;
562 563
		vga_default_font_height = screen_info.orig_video_points;
		vga_video_font_height = screen_info.orig_video_points;
L
Linus Torvalds 已提交
564 565 566 567
		/* This may be suboptimal but is a safe bet - go with it */
		vga_scan_lines =
		    vga_video_font_height * vga_video_num_lines;
	}
568

569
	vgacon_xres = screen_info.orig_video_cols * VGA_FONTWIDTH;
570
	vgacon_yres = vga_scan_lines;
571 572 573

	if (!vga_init_done) {
		vgacon_scrollback_startup();
574
		vga_init_done = true;
575 576
	}

L
Linus Torvalds 已提交
577 578 579 580 581
	return display_desc;
}

static void vgacon_init(struct vc_data *c, int init)
{
582
	struct uni_pagedir *p;
L
Linus Torvalds 已提交
583

584 585 586 587 588
	/*
	 * We cannot be loaded as a module, therefore init is always 1,
	 * but vgacon_init can be called more than once, and init will
	 * not be 1.
	 */
L
Linus Torvalds 已提交
589
	c->vc_can_do_color = vga_can_do_color;
590 591 592 593 594 595 596 597

	/* set dimensions manually if init != 0 since vc_resize() will fail */
	if (init) {
		c->vc_cols = vga_video_num_columns;
		c->vc_rows = vga_video_num_lines;
	} else
		vc_resize(c, vga_video_num_columns, vga_video_num_lines);

L
Linus Torvalds 已提交
598 599 600
	c->vc_scan_lines = vga_scan_lines;
	c->vc_font.height = vga_video_font_height;
	c->vc_complement_mask = 0x7700;
601 602
	if (vga_512_chars)
		c->vc_hi_font_mask = 0x0800;
L
Linus Torvalds 已提交
603
	p = *c->vc_uni_pagedir_loc;
T
Takashi Iwai 已提交
604
	if (c->vc_uni_pagedir_loc != &vgacon_uni_pagedir) {
L
Linus Torvalds 已提交
605
		con_free_unimap(c);
T
Takashi Iwai 已提交
606 607 608 609
		c->vc_uni_pagedir_loc = &vgacon_uni_pagedir;
		vgacon_refcount++;
	}
	if (!vgacon_uni_pagedir && p)
L
Linus Torvalds 已提交
610
		con_set_default_unimap(c);
611

612 613 614 615
	/* Only set the default if the user didn't deliberately override it */
	if (global_cursor_default == -1)
		global_cursor_default =
			!(screen_info.flags & VIDEO_FLAGS_NOCURSOR);
L
Linus Torvalds 已提交
616 617 618 619
}

static void vgacon_deinit(struct vc_data *c)
{
620
	/* When closing the active console, reset video origin */
621
	if (con_is_visible(c)) {
L
Linus Torvalds 已提交
622 623 624
		c->vc_visible_origin = vga_vram_base;
		vga_set_mem_top(c);
	}
625

T
Takashi Iwai 已提交
626
	if (!--vgacon_refcount)
627
		con_free_unimap(c);
L
Linus Torvalds 已提交
628 629 630 631 632
	c->vc_uni_pagedir_loc = &c->vc_uni_pagedir;
	con_set_default_unimap(c);
}

static u8 vgacon_build_attr(struct vc_data *c, u8 color, u8 intensity,
633
			    u8 blink, u8 underline, u8 reverse, u8 italic)
L
Linus Torvalds 已提交
634 635 636 637
{
	u8 attr = color;

	if (vga_can_do_color) {
638 639 640
		if (italic)
			attr = (attr & 0xF0) | c->vc_itcolor;
		else if (underline)
L
Linus Torvalds 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653
			attr = (attr & 0xf0) | c->vc_ulcolor;
		else if (intensity == 0)
			attr = (attr & 0xf0) | c->vc_halfcolor;
	}
	if (reverse)
		attr =
		    ((attr) & 0x88) | ((((attr) >> 4) | ((attr) << 4)) &
				       0x77);
	if (blink)
		attr ^= 0x80;
	if (intensity == 2)
		attr ^= 0x08;
	if (!vga_can_do_color) {
654 655 656
		if (italic)
			attr = (attr & 0xF8) | 0x02;
		else if (underline)
L
Linus Torvalds 已提交
657 658 659 660 661 662 663 664 665
			attr = (attr & 0xf8) | 0x01;
		else if (intensity == 0)
			attr = (attr & 0xf0) | 0x08;
	}
	return attr;
}

static void vgacon_invert_region(struct vc_data *c, u16 * p, int count)
{
666
	const bool col = vga_can_do_color;
L
Linus Torvalds 已提交
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688

	while (count--) {
		u16 a = scr_readw(p);
		if (col)
			a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) |
			    (((a) & 0x0700) << 4);
		else
			a ^= ((a & 0x0700) == 0x0100) ? 0x7000 : 0x7700;
		scr_writew(a, p++);
	}
}

static void vgacon_set_cursor_size(int xpos, int from, int to)
{
	unsigned long flags;
	int curs, cure;

	if ((from == cursor_size_lastfrom) && (to == cursor_size_lastto))
		return;
	cursor_size_lastfrom = from;
	cursor_size_lastto = to;

689
	raw_spin_lock_irqsave(&vga_lock, flags);
690 691 692 693 694 695 696 697 698
	if (vga_video_type >= VIDEO_TYPE_VGAC) {
		outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
		curs = inb_p(vga_video_port_val);
		outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
		cure = inb_p(vga_video_port_val);
	} else {
		curs = 0;
		cure = 0;
	}
L
Linus Torvalds 已提交
699 700 701 702

	curs = (curs & 0xc0) | from;
	cure = (cure & 0xe0) | to;

703
	outb_p(VGA_CRTC_CURSOR_START, vga_video_port_reg);
L
Linus Torvalds 已提交
704
	outb_p(curs, vga_video_port_val);
705
	outb_p(VGA_CRTC_CURSOR_END, vga_video_port_reg);
L
Linus Torvalds 已提交
706
	outb_p(cure, vga_video_port_val);
707
	raw_spin_unlock_irqrestore(&vga_lock, flags);
L
Linus Torvalds 已提交
708 709 710 711
}

static void vgacon_cursor(struct vc_data *c, int mode)
{
712 713 714
	if (c->vc_mode != KD_TEXT)
		return;

715 716
	vgacon_restore_screen(c);

L
Linus Torvalds 已提交
717 718
	switch (mode) {
	case CM_ERASE:
719
		write_vga(14, (c->vc_pos - vga_vram_base) / 2);
720 721 722 723
	        if (vga_video_type >= VIDEO_TYPE_VGAC)
			vgacon_set_cursor_size(c->vc_x, 31, 30);
		else
			vgacon_set_cursor_size(c->vc_x, 31, 31);
L
Linus Torvalds 已提交
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760
		break;

	case CM_MOVE:
	case CM_DRAW:
		write_vga(14, (c->vc_pos - vga_vram_base) / 2);
		switch (c->vc_cursor_type & 0x0f) {
		case CUR_UNDERLINE:
			vgacon_set_cursor_size(c->vc_x,
					       c->vc_font.height -
					       (c->vc_font.height <
						10 ? 2 : 3),
					       c->vc_font.height -
					       (c->vc_font.height <
						10 ? 1 : 2));
			break;
		case CUR_TWO_THIRDS:
			vgacon_set_cursor_size(c->vc_x,
					       c->vc_font.height / 3,
					       c->vc_font.height -
					       (c->vc_font.height <
						10 ? 1 : 2));
			break;
		case CUR_LOWER_THIRD:
			vgacon_set_cursor_size(c->vc_x,
					       (c->vc_font.height * 2) / 3,
					       c->vc_font.height -
					       (c->vc_font.height <
						10 ? 1 : 2));
			break;
		case CUR_LOWER_HALF:
			vgacon_set_cursor_size(c->vc_x,
					       c->vc_font.height / 2,
					       c->vc_font.height -
					       (c->vc_font.height <
						10 ? 1 : 2));
			break;
		case CUR_NONE:
761 762 763 764
			if (vga_video_type >= VIDEO_TYPE_VGAC)
				vgacon_set_cursor_size(c->vc_x, 31, 30);
			else
				vgacon_set_cursor_size(c->vc_x, 31, 31);
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772 773 774
			break;
		default:
			vgacon_set_cursor_size(c->vc_x, 1,
					       c->vc_font.height);
			break;
		}
		break;
	}
}

775 776 777 778 779
static int vgacon_doresize(struct vc_data *c,
		unsigned int width, unsigned int height)
{
	unsigned long flags;
	unsigned int scanlines = height * c->vc_font.height;
780
	u8 scanlines_lo = 0, r7 = 0, vsync_end = 0, mode, max_scan;
781

782
	raw_spin_lock_irqsave(&vga_lock, flags);
783

784 785
	vgacon_xres = width * VGA_FONTWIDTH;
	vgacon_yres = height * c->vc_font.height;
786 787 788
	if (vga_video_type >= VIDEO_TYPE_VGAC) {
		outb_p(VGA_CRTC_MAX_SCAN, vga_video_port_reg);
		max_scan = inb_p(vga_video_port_val);
789

790 791
		if (max_scan & 0x80)
			scanlines <<= 1;
792

793 794
		outb_p(VGA_CRTC_MODE, vga_video_port_reg);
		mode = inb_p(vga_video_port_val);
795

796 797
		if (mode & 0x04)
			scanlines >>= 1;
798

799 800
		scanlines -= 1;
		scanlines_lo = scanlines & 0xff;
801

802 803 804 805 806 807 808 809 810 811 812 813 814 815
		outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
		r7 = inb_p(vga_video_port_val) & ~0x42;

		if (scanlines & 0x100)
			r7 |= 0x02;
		if (scanlines & 0x200)
			r7 |= 0x40;

		/* deprotect registers */
		outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
		vsync_end = inb_p(vga_video_port_val);
		outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
		outb_p(vsync_end & ~0x80, vga_video_port_val);
	}
816 817 818 819 820 821

	outb_p(VGA_CRTC_H_DISP, vga_video_port_reg);
	outb_p(width - 1, vga_video_port_val);
	outb_p(VGA_CRTC_OFFSET, vga_video_port_reg);
	outb_p(width >> 1, vga_video_port_val);

822 823 824 825 826
	if (vga_video_type >= VIDEO_TYPE_VGAC) {
		outb_p(VGA_CRTC_V_DISP_END, vga_video_port_reg);
		outb_p(scanlines_lo, vga_video_port_val);
		outb_p(VGA_CRTC_OVERFLOW, vga_video_port_reg);
		outb_p(r7,vga_video_port_val);
827

828 829 830 831
		/* reprotect registers */
		outb_p(VGA_CRTC_V_SYNC_END, vga_video_port_reg);
		outb_p(vsync_end, vga_video_port_val);
	}
832

833
	raw_spin_unlock_irqrestore(&vga_lock, flags);
834 835 836
	return 0;
}

L
Linus Torvalds 已提交
837 838
static int vgacon_switch(struct vc_data *c)
{
839 840
	int x = c->vc_cols * VGA_FONTWIDTH;
	int y = c->vc_rows * c->vc_font.height;
841
	int rows = screen_info.orig_video_lines * vga_default_font_height/
842
		c->vc_font.height;
L
Linus Torvalds 已提交
843 844 845 846 847 848 849
	/*
	 * We need to save screen size here as it's the only way
	 * we can spot the screen has been resized and we need to
	 * set size of freshly allocated screens ourselves.
	 */
	vga_video_num_columns = c->vc_cols;
	vga_video_num_lines = c->vc_rows;
J
James Simmons 已提交
850 851 852 853

	/* We can only copy out the size of the video buffer here,
	 * otherwise we get into VGA BIOS */

854
	if (!vga_is_gfx) {
L
Linus Torvalds 已提交
855
		scr_memcpyw((u16 *) c->vc_origin, (u16 *) c->vc_screenbuf,
856 857
			    c->vc_screenbuf_size > vga_vram_size ?
				vga_vram_size : c->vc_screenbuf_size);
858 859 860

		if ((vgacon_xres != x || vgacon_yres != y) &&
		    (!(vga_video_num_columns % 2) &&
861
		     vga_video_num_columns <= screen_info.orig_video_cols &&
862
		     vga_video_num_lines <= rows))
S
Samuel Thibault 已提交
863
			vgacon_doresize(c, c->vc_cols, c->vc_rows);
864 865
	}

866
	vgacon_scrollback_switch(c->vc_num);
L
Linus Torvalds 已提交
867 868 869
	return 0;		/* Redrawing not needed */
}

J
Jiri Slaby 已提交
870
static void vga_set_palette(struct vc_data *vc, const unsigned char *table)
L
Linus Torvalds 已提交
871 872 873
{
	int i, j;

874
	vga_w(vgastate.vgabase, VGA_PEL_MSK, 0xff);
L
Linus Torvalds 已提交
875
	for (i = j = 0; i < 16; i++) {
876 877 878 879
		vga_w(vgastate.vgabase, VGA_PEL_IW, table[i]);
		vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
		vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
		vga_w(vgastate.vgabase, VGA_PEL_D, vc->vc_palette[j++] >> 2);
L
Linus Torvalds 已提交
880 881 882
	}
}

883
static void vgacon_set_palette(struct vc_data *vc, const unsigned char *table)
L
Linus Torvalds 已提交
884 885
{
	if (vga_video_type != VIDEO_TYPE_VGAC || vga_palette_blanked
886
	    || !con_is_visible(vc))
887
		return;
L
Linus Torvalds 已提交
888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910
	vga_set_palette(vc, table);
}

/* structure holding original VGA register settings */
static struct {
	unsigned char SeqCtrlIndex;	/* Sequencer Index reg.   */
	unsigned char CrtCtrlIndex;	/* CRT-Contr. Index reg.  */
	unsigned char CrtMiscIO;	/* Miscellaneous register */
	unsigned char HorizontalTotal;	/* CRT-Controller:00h */
	unsigned char HorizDisplayEnd;	/* CRT-Controller:01h */
	unsigned char StartHorizRetrace;	/* CRT-Controller:04h */
	unsigned char EndHorizRetrace;	/* CRT-Controller:05h */
	unsigned char Overflow;	/* CRT-Controller:07h */
	unsigned char StartVertRetrace;	/* CRT-Controller:10h */
	unsigned char EndVertRetrace;	/* CRT-Controller:11h */
	unsigned char ModeControl;	/* CRT-Controller:17h */
	unsigned char ClockingMode;	/* Seq-Controller:01h */
} vga_state;

static void vga_vesa_blank(struct vgastate *state, int mode)
{
	/* save original values of VGA controller registers */
	if (!vga_vesa_blanked) {
911
		raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
912 913 914
		vga_state.SeqCtrlIndex = vga_r(state->vgabase, VGA_SEQ_I);
		vga_state.CrtCtrlIndex = inb_p(vga_video_port_reg);
		vga_state.CrtMiscIO = vga_r(state->vgabase, VGA_MIS_R);
915
		raw_spin_unlock_irq(&vga_lock);
L
Linus Torvalds 已提交
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937

		outb_p(0x00, vga_video_port_reg);	/* HorizontalTotal */
		vga_state.HorizontalTotal = inb_p(vga_video_port_val);
		outb_p(0x01, vga_video_port_reg);	/* HorizDisplayEnd */
		vga_state.HorizDisplayEnd = inb_p(vga_video_port_val);
		outb_p(0x04, vga_video_port_reg);	/* StartHorizRetrace */
		vga_state.StartHorizRetrace = inb_p(vga_video_port_val);
		outb_p(0x05, vga_video_port_reg);	/* EndHorizRetrace */
		vga_state.EndHorizRetrace = inb_p(vga_video_port_val);
		outb_p(0x07, vga_video_port_reg);	/* Overflow */
		vga_state.Overflow = inb_p(vga_video_port_val);
		outb_p(0x10, vga_video_port_reg);	/* StartVertRetrace */
		vga_state.StartVertRetrace = inb_p(vga_video_port_val);
		outb_p(0x11, vga_video_port_reg);	/* EndVertRetrace */
		vga_state.EndVertRetrace = inb_p(vga_video_port_val);
		outb_p(0x17, vga_video_port_reg);	/* ModeControl */
		vga_state.ModeControl = inb_p(vga_video_port_val);
		vga_state.ClockingMode = vga_rseq(state->vgabase, VGA_SEQ_CLOCK_MODE);
	}

	/* assure that video is enabled */
	/* "0x20" is VIDEO_ENABLE_bit in register 01 of sequencer */
938
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973
	vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, vga_state.ClockingMode | 0x20);

	/* test for vertical retrace in process.... */
	if ((vga_state.CrtMiscIO & 0x80) == 0x80)
		vga_w(state->vgabase, VGA_MIS_W, vga_state.CrtMiscIO & 0xEF);

	/*
	 * Set <End of vertical retrace> to minimum (0) and
	 * <Start of vertical Retrace> to maximum (incl. overflow)
	 * Result: turn off vertical sync (VSync) pulse.
	 */
	if (mode & VESA_VSYNC_SUSPEND) {
		outb_p(0x10, vga_video_port_reg);	/* StartVertRetrace */
		outb_p(0xff, vga_video_port_val);	/* maximum value */
		outb_p(0x11, vga_video_port_reg);	/* EndVertRetrace */
		outb_p(0x40, vga_video_port_val);	/* minimum (bits 0..3)  */
		outb_p(0x07, vga_video_port_reg);	/* Overflow */
		outb_p(vga_state.Overflow | 0x84, vga_video_port_val);	/* bits 9,10 of vert. retrace */
	}

	if (mode & VESA_HSYNC_SUSPEND) {
		/*
		 * Set <End of horizontal retrace> to minimum (0) and
		 *  <Start of horizontal Retrace> to maximum
		 * Result: turn off horizontal sync (HSync) pulse.
		 */
		outb_p(0x04, vga_video_port_reg);	/* StartHorizRetrace */
		outb_p(0xff, vga_video_port_val);	/* maximum */
		outb_p(0x05, vga_video_port_reg);	/* EndHorizRetrace */
		outb_p(0x00, vga_video_port_val);	/* minimum (0) */
	}

	/* restore both index registers */
	vga_w(state->vgabase, VGA_SEQ_I, vga_state.SeqCtrlIndex);
	outb_p(vga_state.CrtCtrlIndex, vga_video_port_reg);
974
	raw_spin_unlock_irq(&vga_lock);
L
Linus Torvalds 已提交
975 976 977 978 979
}

static void vga_vesa_unblank(struct vgastate *state)
{
	/* restore original values of VGA controller registers */
980
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
	vga_w(state->vgabase, VGA_MIS_W, vga_state.CrtMiscIO);

	outb_p(0x00, vga_video_port_reg);	/* HorizontalTotal */
	outb_p(vga_state.HorizontalTotal, vga_video_port_val);
	outb_p(0x01, vga_video_port_reg);	/* HorizDisplayEnd */
	outb_p(vga_state.HorizDisplayEnd, vga_video_port_val);
	outb_p(0x04, vga_video_port_reg);	/* StartHorizRetrace */
	outb_p(vga_state.StartHorizRetrace, vga_video_port_val);
	outb_p(0x05, vga_video_port_reg);	/* EndHorizRetrace */
	outb_p(vga_state.EndHorizRetrace, vga_video_port_val);
	outb_p(0x07, vga_video_port_reg);	/* Overflow */
	outb_p(vga_state.Overflow, vga_video_port_val);
	outb_p(0x10, vga_video_port_reg);	/* StartVertRetrace */
	outb_p(vga_state.StartVertRetrace, vga_video_port_val);
	outb_p(0x11, vga_video_port_reg);	/* EndVertRetrace */
	outb_p(vga_state.EndVertRetrace, vga_video_port_val);
	outb_p(0x17, vga_video_port_reg);	/* ModeControl */
	outb_p(vga_state.ModeControl, vga_video_port_val);
	/* ClockingMode */
	vga_wseq(state->vgabase, VGA_SEQ_CLOCK_MODE, vga_state.ClockingMode);

	/* restore index/control registers */
	vga_w(state->vgabase, VGA_SEQ_I, vga_state.SeqCtrlIndex);
	outb_p(vga_state.CrtCtrlIndex, vga_video_port_reg);
1005
	raw_spin_unlock_irq(&vga_lock);
L
Linus Torvalds 已提交
1006 1007 1008 1009 1010 1011
}

static void vga_pal_blank(struct vgastate *state)
{
	int i;

P
Pozsar Balazs 已提交
1012
	vga_w(state->vgabase, VGA_PEL_MSK, 0xff);
L
Linus Torvalds 已提交
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
	for (i = 0; i < 16; i++) {
		vga_w(state->vgabase, VGA_PEL_IW, i);
		vga_w(state->vgabase, VGA_PEL_D, 0);
		vga_w(state->vgabase, VGA_PEL_D, 0);
		vga_w(state->vgabase, VGA_PEL_D, 0);
	}
}

static int vgacon_blank(struct vc_data *c, int blank, int mode_switch)
{
	switch (blank) {
	case 0:		/* Unblank */
		if (vga_vesa_blanked) {
1026
			vga_vesa_unblank(&vgastate);
L
Linus Torvalds 已提交
1027 1028 1029 1030
			vga_vesa_blanked = 0;
		}
		if (vga_palette_blanked) {
			vga_set_palette(c, color_table);
1031
			vga_palette_blanked = false;
L
Linus Torvalds 已提交
1032 1033
			return 0;
		}
1034
		vga_is_gfx = false;
L
Linus Torvalds 已提交
1035 1036 1037 1038 1039
		/* Tell console.c that it has to restore the screen itself */
		return 1;
	case 1:		/* Normal blanking */
	case -1:	/* Obsolete */
		if (!mode_switch && vga_video_type == VIDEO_TYPE_VGAC) {
1040
			vga_pal_blank(&vgastate);
1041
			vga_palette_blanked = true;
L
Linus Torvalds 已提交
1042 1043 1044 1045 1046 1047
			return 0;
		}
		vgacon_set_origin(c);
		scr_memsetw((void *) vga_vram_base, BLANK,
			    c->vc_screenbuf_size);
		if (mode_switch)
1048
			vga_is_gfx = true;
L
Linus Torvalds 已提交
1049 1050 1051
		return 1;
	default:		/* VESA blanking */
		if (vga_video_type == VIDEO_TYPE_VGAC) {
1052
			vga_vesa_blank(&vgastate, blank - 1);
L
Linus Torvalds 已提交
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076
			vga_vesa_blanked = blank;
		}
		return 0;
	}
}

/*
 * PIO_FONT support.
 *
 * The font loading code goes back to the codepage package by
 * Joel Hoffman (joel@wam.umd.edu). (He reports that the original
 * reference is: "From: p. 307 of _Programmer's Guide to PC & PS/2
 * Video Systems_ by Richard Wilton. 1987.  Microsoft Press".)
 *
 * Change for certain monochrome monitors by Yury Shevchuck
 * (sizif@botik.yaroslavl.su).
 */

#define colourmap 0xa0000
/* Pauline Middelink <middelin@polyware.iaf.nl> reports that we
   should use 0xA0000 for the bwmap as well.. */
#define blackwmap 0xa0000
#define cmapsz 8192

1077 1078
static int vgacon_do_font_op(struct vgastate *state, char *arg, int set,
		bool ch512)
L
Linus Torvalds 已提交
1079 1080 1081 1082
{
	unsigned short video_port_status = vga_video_port_reg + 6;
	int font_select = 0x00, beg, i;
	char *charmap;
1083
	bool clear_attribs = false;
L
Linus Torvalds 已提交
1084
	if (vga_video_type != VIDEO_TYPE_EGAM) {
1085
		charmap = (char *) VGA_MAP_MEM(colourmap, 0);
L
Linus Torvalds 已提交
1086 1087
		beg = 0x0e;
	} else {
1088
		charmap = (char *) VGA_MAP_MEM(blackwmap, 0);
L
Linus Torvalds 已提交
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
		beg = 0x0a;
	}

#ifdef BROKEN_GRAPHICS_PROGRAMS
	/*
	 * All fonts are loaded in slot 0 (0:1 for 512 ch)
	 */

	if (!arg)
		return -EINVAL;	/* Return to default font not supported */

1100
	vga_font_is_default = false;
L
Linus Torvalds 已提交
1101 1102 1103 1104 1105 1106 1107 1108 1109 1110
	font_select = ch512 ? 0x04 : 0x00;
#else
	/*
	 * The default font is kept in slot 0 and is never touched.
	 * A custom font is loaded in slot 2 (256 ch) or 2:3 (512 ch)
	 */

	if (set) {
		vga_font_is_default = !arg;
		if (!arg)
1111
			ch512 = false;	/* Default font is always 256 */
L
Linus Torvalds 已提交
1112 1113 1114 1115 1116 1117 1118
		font_select = arg ? (ch512 ? 0x0e : 0x0a) : 0x00;
	}

	if (!vga_font_is_default)
		charmap += 4 * cmapsz;
#endif

1119
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
	/* First, the Sequencer */
	vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x1);
	/* CPU writes only to map 2 */
	vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x04);	
	/* Sequential addressing */
	vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x07);	
	/* Clear synchronous reset */
	vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x03);

	/* Now, the graphics controller, select map 2 */
	vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x02);		
	/* disable odd-even addressing */
	vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x00);
	/* map start at A000:0000 */
	vga_wgfx(state->vgabase, VGA_GFX_MISC, 0x00);
1135
	raw_spin_unlock_irq(&vga_lock);
L
Linus Torvalds 已提交
1136 1137 1138

	if (arg) {
		if (set)
1139
			for (i = 0; i < cmapsz; i++) {
L
Linus Torvalds 已提交
1140
				vga_writeb(arg[i], charmap + i);
1141 1142
				cond_resched();
			}
L
Linus Torvalds 已提交
1143
		else
1144
			for (i = 0; i < cmapsz; i++) {
L
Linus Torvalds 已提交
1145
				arg[i] = vga_readb(charmap + i);
1146 1147
				cond_resched();
			}
L
Linus Torvalds 已提交
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157

		/*
		 * In 512-character mode, the character map is not contiguous if
		 * we want to remain EGA compatible -- which we do
		 */

		if (ch512) {
			charmap += 2 * cmapsz;
			arg += cmapsz;
			if (set)
1158
				for (i = 0; i < cmapsz; i++) {
L
Linus Torvalds 已提交
1159
					vga_writeb(arg[i], charmap + i);
1160 1161
					cond_resched();
				}
L
Linus Torvalds 已提交
1162
			else
1163
				for (i = 0; i < cmapsz; i++) {
L
Linus Torvalds 已提交
1164
					arg[i] = vga_readb(charmap + i);
1165 1166
					cond_resched();
				}
L
Linus Torvalds 已提交
1167 1168 1169
		}
	}

1170
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
	/* First, the sequencer, Synchronous reset */
	vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x01);	
	/* CPU writes to maps 0 and 1 */
	vga_wseq(state->vgabase, VGA_SEQ_PLANE_WRITE, 0x03);
	/* odd-even addressing */
	vga_wseq(state->vgabase, VGA_SEQ_MEMORY_MODE, 0x03);
	/* Character Map Select */
	if (set)
		vga_wseq(state->vgabase, VGA_SEQ_CHARACTER_MAP, font_select);
	/* clear synchronous reset */
	vga_wseq(state->vgabase, VGA_SEQ_RESET, 0x03);

	/* Now, the graphics controller, select map 0 for CPU */
	vga_wgfx(state->vgabase, VGA_GFX_PLANE_READ, 0x00);
	/* enable even-odd addressing */
	vga_wgfx(state->vgabase, VGA_GFX_MODE, 0x10);
	/* map starts at b800:0 or b000:0 */
	vga_wgfx(state->vgabase, VGA_GFX_MISC, beg);

	/* if 512 char mode is already enabled don't re-enable it. */
	if ((set) && (ch512 != vga_512_chars)) {
		vga_512_chars = ch512;
		/* 256-char: enable intensity bit
		   512-char: disable intensity bit */
		inb_p(video_port_status);	/* clear address flip-flop */
		/* color plane enable register */
		vga_wattr(state->vgabase, VGA_ATC_PLANE_ENABLE, ch512 ? 0x07 : 0x0f);
		/* Wilton (1987) mentions the following; I don't know what
		   it means, but it works, and it appears necessary */
		inb_p(video_port_status);
		vga_wattr(state->vgabase, VGA_AR_ENABLE_DISPLAY, 0);	
1202
		clear_attribs = true;
L
Linus Torvalds 已提交
1203
	}
1204
	raw_spin_unlock_irq(&vga_lock);
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	if (clear_attribs) {
		for (i = 0; i < MAX_NR_CONSOLES; i++) {
			struct vc_data *c = vc_cons[i].d;
			if (c && c->vc_sw == &vga_con) {
				/* force hi font mask to 0, so we always clear
				   the bit on either transition */
				c->vc_hi_font_mask = 0x00;
				clear_buffer_attributes(c);
				c->vc_hi_font_mask = ch512 ? 0x0800 : 0;
			}
		}
	}
L
Linus Torvalds 已提交
1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241
	return 0;
}

/*
 * Adjust the screen to fit a font of a certain height
 */
static int vgacon_adjust_height(struct vc_data *vc, unsigned fontheight)
{
	unsigned char ovr, vde, fsr;
	int rows, maxscan, i;

	rows = vc->vc_scan_lines / fontheight;	/* Number of video rows we end up with */
	maxscan = rows * fontheight - 1;	/* Scan lines to actually display-1 */

	/* Reprogram the CRTC for the new font size
	   Note: the attempt to read the overflow register will fail
	   on an EGA, but using 0xff for the previous value appears to
	   be OK for EGA text modes in the range 257-512 scan lines, so I
	   guess we don't need to worry about it.

	   The same applies for the spill bits in the font size and cursor
	   registers; they are write-only on EGA, but it appears that they
	   are all don't care bits on EGA, so I guess it doesn't matter. */

1242
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
1243 1244 1245 1246
	outb_p(0x07, vga_video_port_reg);	/* CRTC overflow register */
	ovr = inb_p(vga_video_port_val);
	outb_p(0x09, vga_video_port_reg);	/* Font size register */
	fsr = inb_p(vga_video_port_val);
1247
	raw_spin_unlock_irq(&vga_lock);
L
Linus Torvalds 已提交
1248 1249 1250 1251 1252 1253

	vde = maxscan & 0xff;	/* Vertical display end reg */
	ovr = (ovr & 0xbd) +	/* Overflow register */
	    ((maxscan & 0x100) >> 7) + ((maxscan & 0x200) >> 3);
	fsr = (fsr & 0xe0) + (fontheight - 1);	/*  Font size register */

1254
	raw_spin_lock_irq(&vga_lock);
L
Linus Torvalds 已提交
1255 1256 1257 1258 1259 1260
	outb_p(0x07, vga_video_port_reg);	/* CRTC overflow register */
	outb_p(ovr, vga_video_port_val);
	outb_p(0x09, vga_video_port_reg);	/* Font size */
	outb_p(fsr, vga_video_port_val);
	outb_p(0x12, vga_video_port_reg);	/* Vertical display limit */
	outb_p(vde, vga_video_port_val);
1261
	raw_spin_unlock_irq(&vga_lock);
1262
	vga_video_font_height = fontheight;
L
Linus Torvalds 已提交
1263 1264 1265 1266 1267

	for (i = 0; i < MAX_NR_CONSOLES; i++) {
		struct vc_data *c = vc_cons[i].d;

		if (c && c->vc_sw == &vga_con) {
1268
			if (con_is_visible(c)) {
L
Linus Torvalds 已提交
1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
			        /* void size to cause regs to be rewritten */
				cursor_size_lastfrom = 0;
				cursor_size_lastto = 0;
				c->vc_sw->con_cursor(c, CM_DRAW);
			}
			c->vc_font.height = fontheight;
			vc_resize(c, 0, rows);	/* Adjust console size */
		}
	}
	return 0;
}

1281 1282
static int vgacon_font_set(struct vc_data *c, struct console_font *font,
			   unsigned int flags)
L
Linus Torvalds 已提交
1283 1284 1285 1286 1287 1288 1289
{
	unsigned charcount = font->charcount;
	int rc;

	if (vga_video_type < VIDEO_TYPE_EGAM)
		return -EINVAL;

1290 1291
	if (font->width != VGA_FONTWIDTH ||
	    (charcount != 256 && charcount != 512))
L
Linus Torvalds 已提交
1292 1293
		return -EINVAL;

1294
	rc = vgacon_do_font_op(&vgastate, font->data, 1, charcount == 512);
L
Linus Torvalds 已提交
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
	if (rc)
		return rc;

	if (!(flags & KD_FONT_FLAG_DONT_RECALC))
		rc = vgacon_adjust_height(c, font->height);
	return rc;
}

static int vgacon_font_get(struct vc_data *c, struct console_font *font)
{
	if (vga_video_type < VIDEO_TYPE_EGAM)
		return -EINVAL;

1308
	font->width = VGA_FONTWIDTH;
L
Linus Torvalds 已提交
1309 1310 1311 1312
	font->height = c->vc_font.height;
	font->charcount = vga_512_chars ? 512 : 256;
	if (!font->data)
		return 0;
1313
	return vgacon_do_font_op(&vgastate, font->data, 0, vga_512_chars);
L
Linus Torvalds 已提交
1314 1315
}

1316
static int vgacon_resize(struct vc_data *c, unsigned int width,
1317
			 unsigned int height, unsigned int user)
1318
{
1319 1320 1321
	if ((width << 1) * height > vga_vram_size)
		return -EINVAL;

1322 1323
	if (width % 2 || width > screen_info.orig_video_cols ||
	    height > (screen_info.orig_video_lines * vga_default_font_height)/
1324
	    c->vc_font.height)
1325 1326 1327
		/* let svgatextmode tinker with video timings and
		   return success */
		return (user) ? 0 : -EINVAL;
1328

1329
	if (con_is_visible(c) && !vga_is_gfx) /* who knows */
1330 1331 1332 1333
		vgacon_doresize(c, width, height);
	return 0;
}

L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
static int vgacon_set_origin(struct vc_data *c)
{
	if (vga_is_gfx ||	/* We don't play origin tricks in graphic modes */
	    (console_blanked && !vga_palette_blanked))	/* Nor we write to blanked screens */
		return 0;
	c->vc_origin = c->vc_visible_origin = vga_vram_base;
	vga_set_mem_top(c);
	vga_rolled_over = 0;
	return 1;
}

static void vgacon_save_screen(struct vc_data *c)
{
	static int vga_bootup_console = 0;

	if (!vga_bootup_console) {
		/* This is a gross hack, but here is the only place we can
		 * set bootup console parameters without messing up generic
		 * console initialization routines.
		 */
		vga_bootup_console = 1;
1355 1356
		c->vc_x = screen_info.orig_x;
		c->vc_y = screen_info.orig_y;
L
Linus Torvalds 已提交
1357
	}
J
James Simmons 已提交
1358

1359
	/* We can't copy in more than the size of the video buffer,
J
James Simmons 已提交
1360 1361
	 * or we'll be copying in VGA BIOS */

L
Linus Torvalds 已提交
1362 1363
	if (!vga_is_gfx)
		scr_memcpyw((u16 *) c->vc_screenbuf, (u16 *) c->vc_origin,
J
James Simmons 已提交
1364
			    c->vc_screenbuf_size > vga_vram_size ? vga_vram_size : c->vc_screenbuf_size);
L
Linus Torvalds 已提交
1365 1366
}

1367 1368
static bool vgacon_scroll(struct vc_data *c, unsigned int t, unsigned int b,
		enum con_scroll dir, unsigned int lines)
L
Linus Torvalds 已提交
1369 1370 1371 1372
{
	unsigned long oldo;
	unsigned int delta;

1373
	if (t || b != c->vc_rows || vga_is_gfx || c->vc_mode != KD_TEXT)
1374
		return false;
L
Linus Torvalds 已提交
1375 1376

	if (!vga_hardscroll_enabled || lines >= c->vc_rows / 2)
1377
		return false;
L
Linus Torvalds 已提交
1378

1379
	vgacon_restore_screen(c);
L
Linus Torvalds 已提交
1380 1381 1382
	oldo = c->vc_origin;
	delta = lines * c->vc_size_row;
	if (dir == SM_UP) {
1383
		vgacon_scrollback_update(c, t, lines);
L
Linus Torvalds 已提交
1384 1385 1386 1387 1388 1389 1390 1391 1392
		if (c->vc_scr_end + delta >= vga_vram_end) {
			scr_memcpyw((u16 *) vga_vram_base,
				    (u16 *) (oldo + delta),
				    c->vc_screenbuf_size - delta);
			c->vc_origin = vga_vram_base;
			vga_rolled_over = oldo - vga_vram_base;
		} else
			c->vc_origin += delta;
		scr_memsetw((u16 *) (c->vc_origin + c->vc_screenbuf_size -
1393
				     delta), c->vc_video_erase_char,
L
Linus Torvalds 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405
			    delta);
	} else {
		if (oldo - delta < vga_vram_base) {
			scr_memmovew((u16 *) (vga_vram_end -
					      c->vc_screenbuf_size +
					      delta), (u16 *) oldo,
				     c->vc_screenbuf_size - delta);
			c->vc_origin = vga_vram_end - c->vc_screenbuf_size;
			vga_rolled_over = 0;
		} else
			c->vc_origin -= delta;
		c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
1406
		scr_memsetw((u16 *) (c->vc_origin), c->vc_video_erase_char,
L
Linus Torvalds 已提交
1407 1408 1409 1410 1411 1412
			    delta);
	}
	c->vc_scr_end = c->vc_origin + c->vc_screenbuf_size;
	c->vc_visible_origin = c->vc_origin;
	vga_set_mem_top(c);
	c->vc_pos = (c->vc_pos - oldo) + c->vc_origin;
1413
	return true;
L
Linus Torvalds 已提交
1414 1415 1416 1417 1418 1419
}

/*
 *  The console `switch' structure for the VGA based console
 */

1420 1421 1422 1423 1424
static void vgacon_clear(struct vc_data *vc, int sy, int sx, int height,
			 int width) { }
static void vgacon_putc(struct vc_data *vc, int c, int ypos, int xpos) { }
static void vgacon_putcs(struct vc_data *vc, const unsigned short *s,
			 int count, int ypos, int xpos) { }
L
Linus Torvalds 已提交
1425 1426 1427 1428 1429 1430

const struct consw vga_con = {
	.owner = THIS_MODULE,
	.con_startup = vgacon_startup,
	.con_init = vgacon_init,
	.con_deinit = vgacon_deinit,
1431 1432 1433
	.con_clear = vgacon_clear,
	.con_putc = vgacon_putc,
	.con_putcs = vgacon_putcs,
L
Linus Torvalds 已提交
1434 1435 1436 1437 1438 1439
	.con_cursor = vgacon_cursor,
	.con_scroll = vgacon_scroll,
	.con_switch = vgacon_switch,
	.con_blank = vgacon_blank,
	.con_font_set = vgacon_font_set,
	.con_font_get = vgacon_font_get,
1440
	.con_resize = vgacon_resize,
L
Linus Torvalds 已提交
1441 1442 1443 1444 1445 1446
	.con_set_palette = vgacon_set_palette,
	.con_scrolldelta = vgacon_scrolldelta,
	.con_set_origin = vgacon_set_origin,
	.con_save_screen = vgacon_save_screen,
	.con_build_attr = vgacon_build_attr,
	.con_invert_region = vgacon_invert_region,
1447
	.con_flush_scrollback = vgacon_flush_scrollback,
L
Linus Torvalds 已提交
1448
};
D
Daniel Vetter 已提交
1449
EXPORT_SYMBOL(vga_con);
L
Linus Torvalds 已提交
1450 1451

MODULE_LICENSE("GPL");