5112 lines · 121.3 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* *  Copyright (C) 1991, 1992  Linus Torvalds */ /* * Hopefully this will be a rather complete VT102 implementation. * * Beeping thanks to John T Kohl. * * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics *   Chars, and VT100 enhancements by Peter MacDonald. * * Copy and paste function by Andrew Haylett, *   some enhancements by Alessandro Rubini. * * Code to check for different video-cards mostly by Galen Hunt, * <g-hunt@ee.utah.edu> * * Rudimentary ISO 10646/Unicode/UTF-8 character set support by * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>. * * Dynamic allocation of consoles, aeb@cwi.nl, May 1994 * Resizing of consoles, aeb, 940926 * * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94 * <poe@daimi.aau.dk> * * User-defined bell sound, new setterm control sequences and printk * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95 * * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp> * * Merge with the abstract console driver by Geert Uytterhoeven * <geert@linux-m68k.org>, Jan 1997. * *   Original m68k console driver modifications by * *     - Arno Griffioen <arno@usn.nl> *     - David Carter <carter@cs.bris.ac.uk> * *   The abstract console driver provides a generic interface for a text *   console. It supports VGA text mode, frame buffer based graphical consoles *   and special graphics processors that are only accessible through some *   registers (e.g. a TMS340x0 GSP). * *   The interface to the hardware is specified using a special structure *   (struct consw) which contains function pointers to console operations *   (see <linux/console.h> for more information). * * Support for changeable cursor shape * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997 * * Ported to i386 and con_scrolldelta fixed * by Emmanuel Marty <core@ggi-project.org>, April 1998 * * Resurrected character buffers in videoram plus lots of other trickery * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998 * * Removed old-style timers, introduced console_timer, made timer * deletion SMP-safe.  17Jun00, Andrew Morton * * Removed console_lock, enabled interrupts across all console operations * 13 March 2001, Andrew Morton * * Fixed UTF-8 mode so alternate charset modes always work according * to control sequences interpreted in do_con_trol function * preserving backward VT100 semigraphics compatibility, * malformed UTF sequences represented as sequences of replacement glyphs, * original codes or '?' as a last resort if replacement glyph is undefined * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006 */ #include <linux/module.h>#include <linux/types.h>#include <linux/sched/signal.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/kernel.h>#include <linux/string.h>#include <linux/errno.h>#include <linux/hex.h>#include <linux/kd.h>#include <linux/slab.h>#include <linux/vmalloc.h>#include <linux/major.h>#include <linux/mm.h>#include <linux/console.h>#include <linux/init.h>#include <linux/mutex.h>#include <linux/vt_kern.h>#include <linux/selection.h>#include <linux/tiocl.h>#include <linux/kbd_kern.h>#include <linux/consolemap.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/workqueue.h>#include <linux/pm.h>#include <linux/font.h>#include <linux/bitops.h>#include <linux/notifier.h>#include <linux/device.h>#include <linux/io.h>#include <linux/uaccess.h>#include <linux/kdb.h>#include <linux/ctype.h>#include <linux/gcd.h> #define MAX_NR_CON_DRIVER 16 #define CON_DRIVER_FLAG_MODULE 1#define CON_DRIVER_FLAG_INIT   2#define CON_DRIVER_FLAG_ATTR   4#define CON_DRIVER_FLAG_ZOMBIE 8 struct con_driver {	const struct consw *con;	const char *desc;	struct device *dev;	int node;	int first;	int last;	int flag;}; static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];const struct consw *conswitchp; /* * Here is the default bell parameters: 750HZ, 1/8th of a second */#define DEFAULT_BELL_PITCH	750#define DEFAULT_BELL_DURATION	(HZ/8)#define DEFAULT_CURSOR_BLINK_MS	200 struct vc vc_cons [MAX_NR_CONSOLES];EXPORT_SYMBOL(vc_cons); static const struct consw *con_driver_map[MAX_NR_CONSOLES]; static int con_open(struct tty_struct *, struct file *);static void vc_init(struct vc_data *vc, int do_clear);static void gotoxy(struct vc_data *vc, int new_x, int new_y);static void restore_cur(struct vc_data *vc);static void save_cur(struct vc_data *vc);static void reset_terminal(struct vc_data *vc, int do_clear);static void con_flush_chars(struct tty_struct *tty);static int set_vesa_blanking(u8 __user *mode);static void set_cursor(struct vc_data *vc);static void hide_cursor(struct vc_data *vc);static void console_callback(struct work_struct *ignored);static void con_driver_unregister_callback(struct work_struct *ignored);static void blank_screen_t(struct timer_list *unused);static void set_palette(struct vc_data *vc);static void unblank_screen(void); #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1) int default_utf8 = true;module_param(default_utf8, int, S_IRUGO | S_IWUSR);int global_cursor_default = -1;module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);EXPORT_SYMBOL(global_cursor_default); static int cur_default = CUR_UNDERLINE;module_param(cur_default, int, S_IRUGO | S_IWUSR); /* * ignore_poke: don't unblank the screen when things are typed.  This is * mainly for the privacy of braille terminal users. */static int ignore_poke; int do_poke_blanked_console;int console_blanked;EXPORT_SYMBOL(console_blanked); static enum vesa_blank_mode vesa_blank_mode;static int vesa_off_interval;static int blankinterval;core_param(consoleblank, blankinterval, int, 0444); static DECLARE_WORK(console_work, console_callback);static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback); /* * fg_console is the current virtual console, * last_console is the last used one, * want_console is the console we want to switch to, */int fg_console;EXPORT_SYMBOL(fg_console);int last_console;int want_console = -1; /* * For each existing display, we have a pointer to console currently visible * on that display, allowing consoles other than fg_console to be refreshed * appropriately. Unless the low-level driver supplies its own display_fg * variable, we use this one for the "master display". */static struct vc_data *master_display_fg; /* * Unfortunately, we need to delay tty echo when we're currently writing to the * console since the code is (and always was) not re-entrant, so we schedule * all flip requests to process context with schedule-task() and run it from * console_callback(). */ /* * For the same reason, we defer scrollback to the console callback. */static int scrollback_delta; /* * Hook so that the power management routines can (un)blank * the console on our behalf. */int (*console_blank_hook)(int);EXPORT_SYMBOL(console_blank_hook); static DEFINE_TIMER(console_timer, blank_screen_t);static int blank_state;static int blank_timer_expired;enum {	blank_off = 0,	blank_normal_wait,	blank_vesa_wait,}; /* * struct vc_font */ /** * vc_font_pitch - Calculates the number of bytes between two adjacent scanlines * @font: The VC font * * Returns: * The number of bytes between two adjacent scanlines in the font data */unsigned int vc_font_pitch(const struct vc_font *font){	return font_glyph_pitch(font->width);}EXPORT_SYMBOL_GPL(vc_font_pitch); /** * vc_font_size - Calculates the size of the font data in bytes * @font: The VC font * * vc_font_size() calculates the number of bytes of font data in the * font specified by @font. The function calculates the size from the * font parameters. * * Returns: * The size of the font data in bytes. */unsigned int vc_font_size(const struct vc_font *font){	return font_glyph_size(font->width, font->height) * font->charcount;}EXPORT_SYMBOL_GPL(vc_font_size); /* * /sys/class/tty/tty0/ * * the attribute 'active' contains the name of the current vc * console and it supports poll() to detect vc switches */static struct device *tty0dev; /* * Notifier list for console events. */static ATOMIC_NOTIFIER_HEAD(vt_notifier_list); int register_vt_notifier(struct notifier_block *nb){	return atomic_notifier_chain_register(&vt_notifier_list, nb);}EXPORT_SYMBOL_GPL(register_vt_notifier); int unregister_vt_notifier(struct notifier_block *nb){	return atomic_notifier_chain_unregister(&vt_notifier_list, nb);}EXPORT_SYMBOL_GPL(unregister_vt_notifier); static void notify_write(struct vc_data *vc, unsigned int unicode){	struct vt_notifier_param param = { .vc = vc, .c = unicode };	atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);} static void notify_update(struct vc_data *vc){	struct vt_notifier_param param = { .vc = vc };	atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);}/* *	Low-Level Functions */ static inline bool con_is_fg(const struct vc_data *vc){	return vc->vc_num == fg_console;} static inline bool con_should_update(const struct vc_data *vc){	return con_is_visible(vc) && !console_blanked;} static inline u16 *screenpos(const struct vc_data *vc, unsigned int offset,			     bool viewed){	unsigned long origin = viewed ? vc->vc_visible_origin : vc->vc_origin; 	return (u16 *)(origin + offset);} static void con_putc(struct vc_data *vc, u16 ca, unsigned int y, unsigned int x){	if (vc->vc_sw->con_putc)		vc->vc_sw->con_putc(vc, ca, y, x);	else		vc->vc_sw->con_putcs(vc, &ca, 1, y, x);} /* Called  from the keyboard irq path.. */static inline void scrolldelta(int lines){	/* FIXME */	/* scrolldelta needs some kind of consistency lock, but the BKL was	   and still is not protecting versus the scheduled back end */	scrollback_delta += lines;	schedule_console_callback();} void schedule_console_callback(void){	schedule_work(&console_work);} /* * Code to manage unicode-based screen buffers */ /* * Our screen buffer is preceded by an array of line pointers so that * scrolling only implies some pointer shuffling. */ static u32 **vc_uniscr_alloc(unsigned int cols, unsigned int rows){	u32 **uni_lines;	void *p;	unsigned int memsize, i, col_size = cols * sizeof(**uni_lines); 	/* allocate everything in one go */	memsize = col_size * rows;	memsize += rows * sizeof(*uni_lines);	uni_lines = vzalloc(memsize);	if (!uni_lines)		return NULL; 	/* initial line pointers */	p = uni_lines + rows;	for (i = 0; i < rows; i++) {		uni_lines[i] = p;		p += col_size;	} 	return uni_lines;} static void vc_uniscr_free(u32 **uni_lines){	vfree(uni_lines);} static void vc_uniscr_set(struct vc_data *vc, u32 **new_uni_lines){	vc_uniscr_free(vc->vc_uni_lines);	vc->vc_uni_lines = new_uni_lines;} static void vc_uniscr_putc(struct vc_data *vc, u32 uc){	if (vc->vc_uni_lines)		vc->vc_uni_lines[vc->state.y][vc->state.x] = uc;} static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr){	if (vc->vc_uni_lines) {		u32 *ln = vc->vc_uni_lines[vc->state.y];		unsigned int x = vc->state.x, cols = vc->vc_cols; 		memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));		memset32(&ln[x], ' ', nr);	}} static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr){	if (vc->vc_uni_lines) {		u32 *ln = vc->vc_uni_lines[vc->state.y];		unsigned int x = vc->state.x, cols = vc->vc_cols; 		memmove(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));		memset32(&ln[cols - nr], ' ', nr);	}} static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,				 unsigned int nr){	if (vc->vc_uni_lines)		memset32(&vc->vc_uni_lines[vc->state.y][x], ' ', nr);} static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,				  unsigned int nr){	if (vc->vc_uni_lines)		while (nr--)			memset32(vc->vc_uni_lines[y++], ' ', vc->vc_cols);} /* juggling array rotation algorithm (complexity O(N), size complexity O(1)) */static void juggle_array(u32 **array, unsigned int size, unsigned int nr){	unsigned int gcd_idx; 	for (gcd_idx = 0; gcd_idx < gcd(nr, size); gcd_idx++) {		u32 *gcd_idx_val = array[gcd_idx];		unsigned int dst_idx = gcd_idx; 		while (1) {			unsigned int src_idx = (dst_idx + nr) % size;			if (src_idx == gcd_idx)				break; 			array[dst_idx] = array[src_idx];			dst_idx = src_idx;		} 		array[dst_idx] = gcd_idx_val;	}} static void vc_uniscr_scroll(struct vc_data *vc, unsigned int top,			     unsigned int bottom, enum con_scroll dir,			     unsigned int nr){	u32 **uni_lines = vc->vc_uni_lines;	unsigned int size = bottom - top; 	if (!uni_lines)		return; 	if (dir == SM_DOWN) {		juggle_array(&uni_lines[top], size, size - nr);		vc_uniscr_clear_lines(vc, top, nr);	} else {		juggle_array(&uni_lines[top], size, nr);		vc_uniscr_clear_lines(vc, bottom - nr, nr);	}} static u32 vc_uniscr_getc(struct vc_data *vc, int relative_pos){	int pos = vc->state.x + vc->vc_need_wrap + relative_pos; 	if (vc->vc_uni_lines && in_range(pos, 0, vc->vc_cols))		return vc->vc_uni_lines[vc->state.y][pos];	return 0;} static void vc_uniscr_copy_area(u32 **dst_lines,				unsigned int dst_cols,				unsigned int dst_rows,				u32 **src_lines,				unsigned int src_cols,				unsigned int src_top_row,				unsigned int src_bot_row){	unsigned int dst_row = 0; 	if (!dst_lines)		return; 	while (src_top_row < src_bot_row) {		u32 *src_line = src_lines[src_top_row];		u32 *dst_line = dst_lines[dst_row]; 

Lines 1–500 of 5112.