1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 1991, 1992 Linus Torvalds
7 * Hopefully this will be a rather complete VT102 implementation.
9 * Beeping thanks to John T Kohl.
11 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
12 * Chars, and VT100 enhancements by Peter MacDonald.
14 * Copy and paste function by Andrew Haylett,
15 * some enhancements by Alessandro Rubini.
17 * Code to check for different video-cards mostly by Galen Hunt,
18 * <g-hunt@ee.utah.edu>
20 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
21 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
23 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
24 * Resizing of consoles, aeb, 940926
26 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
29 * User-defined bell sound, new setterm control sequences and printk
30 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
32 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
34 * Merge with the abstract console driver by Geert Uytterhoeven
35 * <geert@linux-m68k.org>, Jan 1997.
37 * Original m68k console driver modifications by
39 * - Arno Griffioen <arno@usn.nl>
40 * - David Carter <carter@cs.bris.ac.uk>
42 * The abstract console driver provides a generic interface for a text
43 * console. It supports VGA text mode, frame buffer based graphical consoles
44 * and special graphics processors that are only accessible through some
45 * registers (e.g. a TMS340x0 GSP).
47 * The interface to the hardware is specified using a special structure
48 * (struct consw) which contains function pointers to console operations
49 * (see <linux/console.h> for more information).
51 * Support for changeable cursor shape
52 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
54 * Ported to i386 and con_scrolldelta fixed
55 * by Emmanuel Marty <core@ggi-project.org>, April 1998
57 * Resurrected character buffers in videoram plus lots of other trickery
58 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
60 * Removed old-style timers, introduced console_timer, made timer
61 * deletion SMP-safe. 17Jun00, Andrew Morton
63 * Removed console_lock, enabled interrupts across all console operations
64 * 13 March 2001, Andrew Morton
66 * Fixed UTF-8 mode so alternate charset modes always work according
67 * to control sequences interpreted in do_con_trol function
68 * preserving backward VT100 semigraphics compatibility,
69 * malformed UTF sequences represented as sequences of replacement glyphs,
70 * original codes or '?' as a last resort if replacement glyph is undefined
71 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
74 #include <linux/module.h>
75 #include <linux/types.h>
76 #include <linux/sched/signal.h>
77 #include <linux/tty.h>
78 #include <linux/tty_flip.h>
79 #include <linux/kernel.h>
80 #include <linux/string.h>
81 #include <linux/errno.h>
83 #include <linux/slab.h>
84 #include <linux/vmalloc.h>
85 #include <linux/major.h>
87 #include <linux/console.h>
88 #include <linux/init.h>
89 #include <linux/mutex.h>
90 #include <linux/vt_kern.h>
91 #include <linux/selection.h>
92 #include <linux/tiocl.h>
93 #include <linux/kbd_kern.h>
94 #include <linux/consolemap.h>
95 #include <linux/timer.h>
96 #include <linux/interrupt.h>
97 #include <linux/workqueue.h>
99 #include <linux/font.h>
100 #include <linux/bitops.h>
101 #include <linux/notifier.h>
102 #include <linux/device.h>
103 #include <linux/io.h>
104 #include <linux/uaccess.h>
105 #include <linux/kdb.h>
106 #include <linux/ctype.h>
107 #include <linux/bsearch.h>
108 #include <linux/gcd.h>
110 #define MAX_NR_CON_DRIVER 16
112 #define CON_DRIVER_FLAG_MODULE 1
113 #define CON_DRIVER_FLAG_INIT 2
114 #define CON_DRIVER_FLAG_ATTR 4
115 #define CON_DRIVER_FLAG_ZOMBIE 8
118 const struct consw *con;
127 static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
128 const struct consw *conswitchp;
130 /* A bitmap for codes <32. A bit of 1 indicates that the code
131 * corresponding to that bit number invokes some special action
132 * (such as cursor movement) and should not be displayed as a
133 * glyph unless the disp_ctrl mode is explicitly enabled.
135 #define CTRL_ACTION 0x0d00ff81
136 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
139 * Here is the default bell parameters: 750HZ, 1/8th of a second
141 #define DEFAULT_BELL_PITCH 750
142 #define DEFAULT_BELL_DURATION (HZ/8)
143 #define DEFAULT_CURSOR_BLINK_MS 200
145 struct vc vc_cons [MAX_NR_CONSOLES];
147 #ifndef VT_SINGLE_DRIVER
148 static const struct consw *con_driver_map[MAX_NR_CONSOLES];
151 static int con_open(struct tty_struct *, struct file *);
152 static void vc_init(struct vc_data *vc, unsigned int rows,
153 unsigned int cols, int do_clear);
154 static void gotoxy(struct vc_data *vc, int new_x, int new_y);
155 static void save_cur(struct vc_data *vc);
156 static void reset_terminal(struct vc_data *vc, int do_clear);
157 static void con_flush_chars(struct tty_struct *tty);
158 static int set_vesa_blanking(char __user *p);
159 static void set_cursor(struct vc_data *vc);
160 static void hide_cursor(struct vc_data *vc);
161 static void console_callback(struct work_struct *ignored);
162 static void con_driver_unregister_callback(struct work_struct *ignored);
163 static void blank_screen_t(struct timer_list *unused);
164 static void set_palette(struct vc_data *vc);
166 #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
168 static int printable; /* Is console ready for printing? */
169 int default_utf8 = true;
170 module_param(default_utf8, int, S_IRUGO | S_IWUSR);
171 int global_cursor_default = -1;
172 module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
174 static int cur_default = CUR_DEFAULT;
175 module_param(cur_default, int, S_IRUGO | S_IWUSR);
178 * ignore_poke: don't unblank the screen when things are typed. This is
179 * mainly for the privacy of braille terminal users.
181 static int ignore_poke;
183 int do_poke_blanked_console;
186 static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
187 static int vesa_off_interval;
188 static int blankinterval;
189 core_param(consoleblank, blankinterval, int, 0444);
191 static DECLARE_WORK(console_work, console_callback);
192 static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
195 * fg_console is the current virtual console,
196 * last_console is the last used one,
197 * want_console is the console we want to switch to,
198 * saved_* variants are for save/restore around kernel debugger enter/leave
202 int want_console = -1;
203 static int saved_fg_console;
204 static int saved_last_console;
205 static int saved_want_console;
206 static int saved_vc_mode;
207 static int saved_console_blanked;
210 * For each existing display, we have a pointer to console currently visible
211 * on that display, allowing consoles other than fg_console to be refreshed
212 * appropriately. Unless the low-level driver supplies its own display_fg
213 * variable, we use this one for the "master display".
215 static struct vc_data *master_display_fg;
218 * Unfortunately, we need to delay tty echo when we're currently writing to the
219 * console since the code is (and always was) not re-entrant, so we schedule
220 * all flip requests to process context with schedule-task() and run it from
221 * console_callback().
225 * For the same reason, we defer scrollback to the console callback.
227 static int scrollback_delta;
230 * Hook so that the power management routines can (un)blank
231 * the console on our behalf.
233 int (*console_blank_hook)(int);
235 static DEFINE_TIMER(console_timer, blank_screen_t);
236 static int blank_state;
237 static int blank_timer_expired;
245 * /sys/class/tty/tty0/
247 * the attribute 'active' contains the name of the current vc
248 * console and it supports poll() to detect vc switches
250 static struct device *tty0dev;
253 * Notifier list for console events.
255 static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
257 int register_vt_notifier(struct notifier_block *nb)
259 return atomic_notifier_chain_register(&vt_notifier_list, nb);
261 EXPORT_SYMBOL_GPL(register_vt_notifier);
263 int unregister_vt_notifier(struct notifier_block *nb)
265 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
267 EXPORT_SYMBOL_GPL(unregister_vt_notifier);
269 static void notify_write(struct vc_data *vc, unsigned int unicode)
271 struct vt_notifier_param param = { .vc = vc, .c = unicode };
272 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, ¶m);
275 static void notify_update(struct vc_data *vc)
277 struct vt_notifier_param param = { .vc = vc };
278 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, ¶m);
281 * Low-Level Functions
284 static inline bool con_is_fg(const struct vc_data *vc)
286 return vc->vc_num == fg_console;
289 static inline bool con_should_update(const struct vc_data *vc)
291 return con_is_visible(vc) && !console_blanked;
294 static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
299 p = (unsigned short *)(vc->vc_origin + offset);
300 else if (!vc->vc_sw->con_screen_pos)
301 p = (unsigned short *)(vc->vc_visible_origin + offset);
303 p = vc->vc_sw->con_screen_pos(vc, offset);
307 /* Called from the keyboard irq path.. */
308 static inline void scrolldelta(int lines)
311 /* scrolldelta needs some kind of consistency lock, but the BKL was
312 and still is not protecting versus the scheduled back end */
313 scrollback_delta += lines;
314 schedule_console_callback();
317 void schedule_console_callback(void)
319 schedule_work(&console_work);
323 * Code to manage unicode-based screen buffers
326 #ifdef NO_VC_UNI_SCREEN
327 /* this disables and optimizes related code away at compile time */
328 #define get_vc_uniscr(vc) NULL
330 #define get_vc_uniscr(vc) vc->vc_uni_screen
333 #define VC_UNI_SCREEN_DEBUG 0
335 typedef uint32_t char32_t;
338 * Our screen buffer is preceded by an array of line pointers so that
339 * scrolling only implies some pointer shuffling.
345 static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
347 struct uni_screen *uniscr;
349 unsigned int memsize, i;
351 /* allocate everything in one go */
352 memsize = cols * rows * sizeof(char32_t);
353 memsize += rows * sizeof(char32_t *);
354 p = vmalloc(memsize);
358 /* initial line pointers */
360 p = uniscr->lines + rows;
361 for (i = 0; i < rows; i++) {
362 uniscr->lines[i] = p;
363 p += cols * sizeof(char32_t);
368 static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
370 vfree(vc->vc_uni_screen);
371 vc->vc_uni_screen = new_uniscr;
374 static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
376 struct uni_screen *uniscr = get_vc_uniscr(vc);
379 uniscr->lines[vc->vc_y][vc->vc_x] = uc;
382 static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
384 struct uni_screen *uniscr = get_vc_uniscr(vc);
387 char32_t *ln = uniscr->lines[vc->vc_y];
388 unsigned int x = vc->vc_x, cols = vc->vc_cols;
390 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
391 memset32(&ln[x], ' ', nr);
395 static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
397 struct uni_screen *uniscr = get_vc_uniscr(vc);
400 char32_t *ln = uniscr->lines[vc->vc_y];
401 unsigned int x = vc->vc_x, cols = vc->vc_cols;
403 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
404 memset32(&ln[cols - nr], ' ', nr);
408 static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
411 struct uni_screen *uniscr = get_vc_uniscr(vc);
414 char32_t *ln = uniscr->lines[vc->vc_y];
416 memset32(&ln[x], ' ', nr);
420 static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
423 struct uni_screen *uniscr = get_vc_uniscr(vc);
426 unsigned int cols = vc->vc_cols;
429 memset32(uniscr->lines[y++], ' ', cols);
433 static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
434 enum con_scroll dir, unsigned int nr)
436 struct uni_screen *uniscr = get_vc_uniscr(vc);
439 unsigned int i, j, k, sz, d, clear;
444 if (dir == SM_DOWN) {
448 for (i = 0; i < gcd(d, sz); i++) {
449 char32_t *tmp = uniscr->lines[t + i];
457 uniscr->lines[t + j] = uniscr->lines[t + k];
460 uniscr->lines[t + j] = tmp;
462 vc_uniscr_clear_lines(vc, clear, nr);
466 static void vc_uniscr_copy_area(struct uni_screen *dst,
467 unsigned int dst_cols,
468 unsigned int dst_rows,
469 struct uni_screen *src,
470 unsigned int src_cols,
471 unsigned int src_top_row,
472 unsigned int src_bot_row)
474 unsigned int dst_row = 0;
479 while (src_top_row < src_bot_row) {
480 char32_t *src_line = src->lines[src_top_row];
481 char32_t *dst_line = dst->lines[dst_row];
483 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
484 if (dst_cols - src_cols)
485 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
489 while (dst_row < dst_rows) {
490 char32_t *dst_line = dst->lines[dst_row];
492 memset32(dst_line, ' ', dst_cols);
498 * Called from vcs_read() to make sure unicode screen retrieval is possible.
499 * This will initialize the unicode screen buffer if not already done.
500 * This returns 0 if OK, or a negative error code otherwise.
501 * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
503 int vc_uniscr_check(struct vc_data *vc)
505 struct uni_screen *uniscr;
509 if (__is_defined(NO_VC_UNI_SCREEN))
512 WARN_CONSOLE_UNLOCKED();
517 if (vc->vc_uni_screen)
520 uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
525 * Let's populate it initially with (imperfect) reverse translation.
526 * This is the next best thing we can do short of having it enabled
527 * from the start even when no users rely on this functionality. True
528 * unicode content will be available after a complete screen refresh.
530 p = (unsigned short *)vc->vc_origin;
531 mask = vc->vc_hi_font_mask | 0xff;
532 for (y = 0; y < vc->vc_rows; y++) {
533 char32_t *line = uniscr->lines[y];
534 for (x = 0; x < vc->vc_cols; x++) {
535 u16 glyph = scr_readw(p++) & mask;
536 line[x] = inverse_translate(vc, glyph, true);
540 vc->vc_uni_screen = uniscr;
545 * Called from vcs_read() to get the unicode data from the screen.
546 * This must be preceded by a successful call to vc_uniscr_check() once
547 * the console lock has been taken.
549 void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
550 unsigned int row, unsigned int col, unsigned int nr)
552 struct uni_screen *uniscr = get_vc_uniscr(vc);
553 int offset = row * vc->vc_size_row + col * 2;
558 pos = (unsigned long)screenpos(vc, offset, viewed);
559 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
561 * Desired position falls in the main screen buffer.
562 * However the actual row/col might be different if
563 * scrollback is active.
565 row = (pos - vc->vc_origin) / vc->vc_size_row;
566 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
567 memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
570 * Scrollback is active. For now let's simply backtranslate
571 * the screen glyphs until the unicode screen buffer does
572 * synchronize with console display drivers for a scrollback
576 int mask = vc->vc_hi_font_mask | 0xff;
577 char32_t *uni_buf = dest;
579 u16 glyph = scr_readw(p++) & mask;
580 *uni_buf++ = inverse_translate(vc, glyph, true);
585 /* this is for validation and debugging only */
586 static void vc_uniscr_debug_check(struct vc_data *vc)
588 struct uni_screen *uniscr = get_vc_uniscr(vc);
592 if (!VC_UNI_SCREEN_DEBUG || !uniscr)
595 WARN_CONSOLE_UNLOCKED();
598 * Make sure our unicode screen translates into the same glyphs
599 * as the actual screen. This is brutal indeed.
601 p = (unsigned short *)vc->vc_origin;
602 mask = vc->vc_hi_font_mask | 0xff;
603 for (y = 0; y < vc->vc_rows; y++) {
604 char32_t *line = uniscr->lines[y];
605 for (x = 0; x < vc->vc_cols; x++) {
606 u16 glyph = scr_readw(p++) & mask;
607 char32_t uc = line[x];
608 int tc = conv_uni_to_pc(vc, uc);
610 tc = conv_uni_to_pc(vc, 0xfffd);
612 tc = conv_uni_to_pc(vc, '?');
615 "%s: mismatch at %d,%d: glyph=%#x tc=%#x\n",
616 __func__, x, y, glyph, tc);
622 static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
623 enum con_scroll dir, unsigned int nr)
629 if (b > vc->vc_rows || t >= b || nr < 1)
631 vc_uniscr_scroll(vc, t, b, dir, nr);
632 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
635 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
636 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
639 clear = s + (b - t - nr) * vc->vc_cols;
642 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
643 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
646 static void do_update_region(struct vc_data *vc, unsigned long start, int count)
648 unsigned int xx, yy, offset;
652 if (!vc->vc_sw->con_getxy) {
653 offset = (start - vc->vc_origin) / 2;
654 xx = offset % vc->vc_cols;
655 yy = offset / vc->vc_cols;
658 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
662 u16 attrib = scr_readw(p) & 0xff00;
665 while (xx < vc->vc_cols && count) {
666 if (attrib != (scr_readw(p) & 0xff00)) {
668 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
671 attrib = scr_readw(p) & 0xff00;
678 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
683 if (vc->vc_sw->con_getxy) {
685 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
690 void update_region(struct vc_data *vc, unsigned long start, int count)
692 WARN_CONSOLE_UNLOCKED();
694 if (con_should_update(vc)) {
696 do_update_region(vc, start, count);
701 /* Structure of attributes is hardware-dependent */
703 static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
704 u8 _underline, u8 _reverse, u8 _italic)
706 if (vc->vc_sw->con_build_attr)
707 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
708 _blink, _underline, _reverse, _italic);
711 * ++roman: I completely changed the attribute format for monochrome
712 * mode (!can_do_color). The formerly used MDA (monochrome display
713 * adapter) format didn't allow the combination of certain effects.
714 * Now the attribute is just a bit vector:
715 * Bit 0..1: intensity (0..2)
722 if (!vc->vc_can_do_color)
725 (_underline ? 4 : 0) |
729 a = (a & 0xF0) | vc->vc_itcolor;
731 a = (a & 0xf0) | vc->vc_ulcolor;
732 else if (_intensity == 0)
733 a = (a & 0xf0) | vc->vc_halfcolor;
735 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
740 if (vc->vc_hi_font_mask == 0x100)
746 static void update_attr(struct vc_data *vc)
748 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
749 vc->vc_blink, vc->vc_underline,
750 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
751 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
754 /* Note: inverting the screen twice should revert to the original state */
755 void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
759 WARN_CONSOLE_UNLOCKED();
762 p = screenpos(vc, offset, viewed);
763 if (vc->vc_sw->con_invert_region) {
764 vc->vc_sw->con_invert_region(vc, p, count);
770 if (!vc->vc_can_do_color) {
777 } else if (vc->vc_hi_font_mask == 0x100) {
780 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
787 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
794 if (con_should_update(vc))
795 do_update_region(vc, (unsigned long) p, count);
799 /* used by selection: complement pointer position */
800 void complement_pos(struct vc_data *vc, int offset)
802 static int old_offset = -1;
803 static unsigned short old;
804 static unsigned short oldx, oldy;
806 WARN_CONSOLE_UNLOCKED();
808 if (old_offset != -1 && old_offset >= 0 &&
809 old_offset < vc->vc_screenbuf_size) {
810 scr_writew(old, screenpos(vc, old_offset, 1));
811 if (con_should_update(vc))
812 vc->vc_sw->con_putc(vc, old, oldy, oldx);
818 if (offset != -1 && offset >= 0 &&
819 offset < vc->vc_screenbuf_size) {
822 p = screenpos(vc, offset, 1);
824 new = old ^ vc->vc_complement_mask;
826 if (con_should_update(vc)) {
827 oldx = (offset >> 1) % vc->vc_cols;
828 oldy = (offset >> 1) / vc->vc_cols;
829 vc->vc_sw->con_putc(vc, new, oldy, oldx);
835 static void insert_char(struct vc_data *vc, unsigned int nr)
837 unsigned short *p = (unsigned short *) vc->vc_pos;
839 vc_uniscr_insert(vc, nr);
840 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
841 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
842 vc->vc_need_wrap = 0;
843 if (con_should_update(vc))
844 do_update_region(vc, (unsigned long) p,
845 vc->vc_cols - vc->vc_x);
848 static void delete_char(struct vc_data *vc, unsigned int nr)
850 unsigned short *p = (unsigned short *) vc->vc_pos;
852 vc_uniscr_delete(vc, nr);
853 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
854 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
856 vc->vc_need_wrap = 0;
857 if (con_should_update(vc))
858 do_update_region(vc, (unsigned long) p,
859 vc->vc_cols - vc->vc_x);
862 static int softcursor_original = -1;
864 static void add_softcursor(struct vc_data *vc)
866 int i = scr_readw((u16 *) vc->vc_pos);
867 u32 type = vc->vc_cursor_type;
869 if (! (type & 0x10)) return;
870 if (softcursor_original != -1) return;
871 softcursor_original = i;
872 i |= ((type >> 8) & 0xff00 );
873 i ^= ((type) & 0xff00 );
874 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
875 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
876 scr_writew(i, (u16 *) vc->vc_pos);
877 if (con_should_update(vc))
878 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
881 static void hide_softcursor(struct vc_data *vc)
883 if (softcursor_original != -1) {
884 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
885 if (con_should_update(vc))
886 vc->vc_sw->con_putc(vc, softcursor_original,
888 softcursor_original = -1;
892 static void hide_cursor(struct vc_data *vc)
897 vc->vc_sw->con_cursor(vc, CM_ERASE);
901 static void set_cursor(struct vc_data *vc)
903 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
909 if ((vc->vc_cursor_type & 0x0f) != 1)
910 vc->vc_sw->con_cursor(vc, CM_DRAW);
915 static void set_origin(struct vc_data *vc)
917 WARN_CONSOLE_UNLOCKED();
919 if (!con_is_visible(vc) ||
920 !vc->vc_sw->con_set_origin ||
921 !vc->vc_sw->con_set_origin(vc))
922 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
923 vc->vc_visible_origin = vc->vc_origin;
924 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
925 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
928 static void save_screen(struct vc_data *vc)
930 WARN_CONSOLE_UNLOCKED();
932 if (vc->vc_sw->con_save_screen)
933 vc->vc_sw->con_save_screen(vc);
936 static void flush_scrollback(struct vc_data *vc)
938 WARN_CONSOLE_UNLOCKED();
941 if (vc->vc_sw->con_flush_scrollback) {
942 vc->vc_sw->con_flush_scrollback(vc);
943 } else if (con_is_visible(vc)) {
945 * When no con_flush_scrollback method is provided then the
946 * legacy way for flushing the scrollback buffer is to use
947 * a side effect of the con_switch method. We do it only on
948 * the foreground console as background consoles have no
949 * scrollback buffers in that case and we obviously don't
950 * want to switch to them.
953 vc->vc_sw->con_switch(vc);
959 * Redrawing of screen
962 void clear_buffer_attributes(struct vc_data *vc)
964 unsigned short *p = (unsigned short *)vc->vc_origin;
965 int count = vc->vc_screenbuf_size / 2;
966 int mask = vc->vc_hi_font_mask | 0xff;
968 for (; count > 0; count--, p++) {
969 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
973 void redraw_screen(struct vc_data *vc, int is_switch)
977 WARN_CONSOLE_UNLOCKED();
981 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
986 struct vc_data *old_vc = vc_cons[fg_console].d;
989 if (!con_is_visible(vc))
991 *vc->vc_display_fg = vc;
992 fg_console = vc->vc_num;
994 if (!con_is_visible(old_vc)) {
999 sysfs_notify(&tty0dev->kobj, NULL, "active");
1007 int old_was_color = vc->vc_can_do_color;
1010 update = vc->vc_sw->con_switch(vc);
1013 * If console changed from mono<->color, the best we can do
1014 * is to clear the buffer attributes. As it currently stands,
1015 * rebuilding new attributes from the old buffer is not doable
1016 * without overly complex code.
1018 if (old_was_color != vc->vc_can_do_color) {
1020 clear_buffer_attributes(vc);
1023 if (update && vc->vc_mode != KD_GRAPHICS)
1024 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
1029 compute_shiftstate();
1035 * Allocation, freeing and resizing of VTs.
1038 int vc_cons_allocated(unsigned int i)
1040 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
1043 static void visual_init(struct vc_data *vc, int num, int init)
1045 /* ++Geert: vc->vc_sw->con_init determines console size */
1047 module_put(vc->vc_sw->owner);
1048 vc->vc_sw = conswitchp;
1049 #ifndef VT_SINGLE_DRIVER
1050 if (con_driver_map[num])
1051 vc->vc_sw = con_driver_map[num];
1053 __module_get(vc->vc_sw->owner);
1055 vc->vc_display_fg = &master_display_fg;
1056 if (vc->vc_uni_pagedir_loc)
1057 con_free_unimap(vc);
1058 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
1059 vc->vc_uni_pagedir = NULL;
1060 vc->vc_hi_font_mask = 0;
1061 vc->vc_complement_mask = 0;
1062 vc->vc_can_do_color = 0;
1063 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1064 vc->vc_sw->con_init(vc, init);
1065 if (!vc->vc_complement_mask)
1066 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1067 vc->vc_s_complement_mask = vc->vc_complement_mask;
1068 vc->vc_size_row = vc->vc_cols << 1;
1069 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
1073 static void visual_deinit(struct vc_data *vc)
1075 vc->vc_sw->con_deinit(vc);
1076 module_put(vc->vc_sw->owner);
1079 static void vc_port_destruct(struct tty_port *port)
1081 struct vc_data *vc = container_of(port, struct vc_data, port);
1086 static const struct tty_port_operations vc_port_ops = {
1087 .destruct = vc_port_destruct,
1090 int vc_allocate(unsigned int currcons) /* return 0 on success */
1092 struct vt_notifier_param param;
1095 WARN_CONSOLE_UNLOCKED();
1097 if (currcons >= MAX_NR_CONSOLES)
1100 if (vc_cons[currcons].d)
1103 /* due to the granularity of kmalloc, we waste some memory here */
1104 /* the alloc is done in two steps, to optimize the common situation
1105 of a 25x80 console (structsize=216, screenbuf_size=4000) */
1106 /* although the numbers above are not valid since long ago, the
1107 point is still up-to-date and the comment still has its value
1108 even if only as a historical artifact. --mj, July 1998 */
1109 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
1113 vc_cons[currcons].d = vc;
1114 tty_port_init(&vc->port);
1115 vc->port.ops = &vc_port_ops;
1116 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1118 visual_init(vc, currcons, 1);
1120 if (!*vc->vc_uni_pagedir_loc)
1121 con_set_default_unimap(vc);
1123 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
1124 if (!vc->vc_screenbuf)
1127 /* If no drivers have overridden us and the user didn't pass a
1128 boot option, default to displaying the cursor */
1129 if (global_cursor_default == -1)
1130 global_cursor_default = 1;
1132 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
1133 vcs_make_sysfs(currcons);
1134 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, ¶m);
1140 vc_cons[currcons].d = NULL;
1144 static inline int resize_screen(struct vc_data *vc, int width, int height,
1147 /* Resizes the resolution of the display adapater */
1150 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
1151 err = vc->vc_sw->con_resize(vc, width, height, user);
1157 * Change # of rows and columns (0 means unchanged/the size of fg_console)
1158 * [this is to be used together with some user program
1159 * like resize that changes the hardware videomode]
1161 #define VC_RESIZE_MAXCOL (32767)
1162 #define VC_RESIZE_MAXROW (32767)
1165 * vc_do_resize - resizing method for the tty
1166 * @tty: tty being resized
1167 * @real_tty: real tty (different to tty if a pty/tty pair)
1168 * @vc: virtual console private data
1172 * Resize a virtual console, clipping according to the actual constraints.
1173 * If the caller passes a tty structure then update the termios winsize
1174 * information and perform any necessary signal handling.
1176 * Caller must hold the console semaphore. Takes the termios rwsem and
1177 * ctrl_lock of the tty IFF a tty is passed.
1180 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1181 unsigned int cols, unsigned int lines)
1183 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
1185 unsigned int old_rows, old_row_size, first_copied_row;
1186 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
1188 unsigned short *newscreen;
1189 struct uni_screen *new_uniscr = NULL;
1191 WARN_CONSOLE_UNLOCKED();
1196 user = vc->vc_resize_user;
1197 vc->vc_resize_user = 0;
1199 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1202 new_cols = (cols ? cols : vc->vc_cols);
1203 new_rows = (lines ? lines : vc->vc_rows);
1204 new_row_size = new_cols << 1;
1205 new_screen_size = new_row_size * new_rows;
1207 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1210 if (new_screen_size > (4 << 20))
1212 newscreen = kzalloc(new_screen_size, GFP_USER);
1216 if (get_vc_uniscr(vc)) {
1217 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1227 old_rows = vc->vc_rows;
1228 old_row_size = vc->vc_size_row;
1230 err = resize_screen(vc, new_cols, new_rows, user);
1237 vc->vc_rows = new_rows;
1238 vc->vc_cols = new_cols;
1239 vc->vc_size_row = new_row_size;
1240 vc->vc_screenbuf_size = new_screen_size;
1242 rlth = min(old_row_size, new_row_size);
1243 rrem = new_row_size - rlth;
1244 old_origin = vc->vc_origin;
1245 new_origin = (long) newscreen;
1246 new_scr_end = new_origin + new_screen_size;
1248 if (vc->vc_y > new_rows) {
1249 if (old_rows - vc->vc_y < new_rows) {
1251 * Cursor near the bottom, copy contents from the
1254 first_copied_row = (old_rows - new_rows);
1257 * Cursor is in no man's land, copy 1/2 screenful
1258 * from the top and bottom of cursor position
1260 first_copied_row = (vc->vc_y - new_rows/2);
1262 old_origin += first_copied_row * old_row_size;
1264 first_copied_row = 0;
1265 end = old_origin + old_row_size * min(old_rows, new_rows);
1267 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1268 get_vc_uniscr(vc), rlth/2, first_copied_row,
1269 min(old_rows, new_rows));
1270 vc_uniscr_set(vc, new_uniscr);
1274 while (old_origin < end) {
1275 scr_memcpyw((unsigned short *) new_origin,
1276 (unsigned short *) old_origin, rlth);
1278 scr_memsetw((void *)(new_origin + rlth),
1279 vc->vc_video_erase_char, rrem);
1280 old_origin += old_row_size;
1281 new_origin += new_row_size;
1283 if (new_scr_end > new_origin)
1284 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1285 new_scr_end - new_origin);
1286 kfree(vc->vc_screenbuf);
1287 vc->vc_screenbuf = newscreen;
1288 vc->vc_screenbuf_size = new_screen_size;
1291 /* do part of a reset_terminal() */
1293 vc->vc_bottom = vc->vc_rows;
1294 gotoxy(vc, vc->vc_x, vc->vc_y);
1298 /* Rewrite the requested winsize data with the actual
1301 memset(&ws, 0, sizeof(ws));
1302 ws.ws_row = vc->vc_rows;
1303 ws.ws_col = vc->vc_cols;
1304 ws.ws_ypixel = vc->vc_scan_lines;
1305 tty_do_resize(tty, &ws);
1308 if (con_is_visible(vc))
1310 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
1316 * vc_resize - resize a VT
1317 * @vc: virtual console
1321 * Resize a virtual console as seen from the console end of things. We
1322 * use the common vc_do_resize methods to update the structures. The
1323 * caller must hold the console sem to protect console internals and
1327 int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
1329 return vc_do_resize(vc->port.tty, vc, cols, rows);
1333 * vt_resize - resize a VT
1334 * @tty: tty to resize
1335 * @ws: winsize attributes
1337 * Resize a virtual terminal. This is called by the tty layer as we
1338 * register our own handler for resizing. The mutual helper does all
1341 * Takes the console sem and the called methods then take the tty
1342 * termios_rwsem and the tty ctrl_lock in that order.
1344 static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1346 struct vc_data *vc = tty->driver_data;
1350 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1355 struct vc_data *vc_deallocate(unsigned int currcons)
1357 struct vc_data *vc = NULL;
1359 WARN_CONSOLE_UNLOCKED();
1361 if (vc_cons_allocated(currcons)) {
1362 struct vt_notifier_param param;
1364 param.vc = vc = vc_cons[currcons].d;
1365 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m);
1366 vcs_remove_sysfs(currcons);
1368 put_pid(vc->vt_pid);
1369 vc_uniscr_set(vc, NULL);
1370 kfree(vc->vc_screenbuf);
1371 vc_cons[currcons].d = NULL;
1380 enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
1382 #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1383 #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1384 #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
1386 #define decarm VC_REPEAT
1387 #define decckm VC_CKMODE
1388 #define kbdapplic VC_APPLIC
1392 * this is what the terminal answers to a ESC-Z or csi0c query.
1394 #define VT100ID "\033[?1;2c"
1395 #define VT102ID "\033[?6c"
1397 const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1398 8,12,10,14, 9,13,11,15 };
1400 /* the default colour table, for VGA+ colour systems */
1401 unsigned char default_red[] = {
1402 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1403 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1405 module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
1407 unsigned char default_grn[] = {
1408 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1409 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1411 module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1413 unsigned char default_blu[] = {
1414 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1415 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1417 module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
1420 * gotoxy() must verify all boundaries, because the arguments
1421 * might also be negative. If the given position is out of
1422 * bounds, the cursor is placed at the nearest margin.
1424 static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1431 if (new_x >= vc->vc_cols)
1432 vc->vc_x = vc->vc_cols - 1;
1439 max_y = vc->vc_bottom;
1442 max_y = vc->vc_rows;
1446 else if (new_y >= max_y)
1447 vc->vc_y = max_y - 1;
1450 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1451 vc->vc_need_wrap = 0;
1454 /* for absolute user moves, when decom is set */
1455 static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1457 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1460 void scrollback(struct vc_data *vc)
1462 scrolldelta(-(vc->vc_rows / 2));
1465 void scrollfront(struct vc_data *vc, int lines)
1468 lines = vc->vc_rows / 2;
1472 static void lf(struct vc_data *vc)
1474 /* don't scroll if above bottom of scrolling region, or
1475 * if below scrolling region
1477 if (vc->vc_y + 1 == vc->vc_bottom)
1478 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
1479 else if (vc->vc_y < vc->vc_rows - 1) {
1481 vc->vc_pos += vc->vc_size_row;
1483 vc->vc_need_wrap = 0;
1484 notify_write(vc, '\n');
1487 static void ri(struct vc_data *vc)
1489 /* don't scroll if below top of scrolling region, or
1490 * if above scrolling region
1492 if (vc->vc_y == vc->vc_top)
1493 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
1494 else if (vc->vc_y > 0) {
1496 vc->vc_pos -= vc->vc_size_row;
1498 vc->vc_need_wrap = 0;
1501 static inline void cr(struct vc_data *vc)
1503 vc->vc_pos -= vc->vc_x << 1;
1504 vc->vc_need_wrap = vc->vc_x = 0;
1505 notify_write(vc, '\r');
1508 static inline void bs(struct vc_data *vc)
1513 vc->vc_need_wrap = 0;
1514 notify_write(vc, '\b');
1518 static inline void del(struct vc_data *vc)
1523 static void csi_J(struct vc_data *vc, int vpar)
1526 unsigned short * start;
1529 case 0: /* erase from cursor to end of display */
1530 vc_uniscr_clear_line(vc, vc->vc_x,
1531 vc->vc_cols - vc->vc_x);
1532 vc_uniscr_clear_lines(vc, vc->vc_y + 1,
1533 vc->vc_rows - vc->vc_y - 1);
1534 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1535 start = (unsigned short *)vc->vc_pos;
1537 case 1: /* erase from start to cursor */
1538 vc_uniscr_clear_line(vc, 0, vc->vc_x + 1);
1539 vc_uniscr_clear_lines(vc, 0, vc->vc_y);
1540 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1541 start = (unsigned short *)vc->vc_origin;
1543 case 3: /* include scrollback */
1544 flush_scrollback(vc);
1546 case 2: /* erase whole display */
1547 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
1548 count = vc->vc_cols * vc->vc_rows;
1549 start = (unsigned short *)vc->vc_origin;
1554 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1555 if (con_should_update(vc))
1556 do_update_region(vc, (unsigned long) start, count);
1557 vc->vc_need_wrap = 0;
1560 static void csi_K(struct vc_data *vc, int vpar)
1563 unsigned short *start = (unsigned short *)vc->vc_pos;
1567 case 0: /* erase from cursor to end of line */
1569 count = vc->vc_cols - vc->vc_x;
1571 case 1: /* erase from start of line to cursor */
1573 count = vc->vc_x + 1;
1575 case 2: /* erase whole line */
1577 count = vc->vc_cols;
1582 vc_uniscr_clear_line(vc, vc->vc_x + offset, count);
1583 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
1584 vc->vc_need_wrap = 0;
1585 if (con_should_update(vc))
1586 do_update_region(vc, (unsigned long)(start + offset), count);
1589 static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1595 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1597 vc_uniscr_clear_line(vc, vc->vc_x, count);
1598 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1599 if (con_should_update(vc))
1600 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1601 vc->vc_need_wrap = 0;
1604 static void default_attr(struct vc_data *vc)
1606 vc->vc_intensity = 1;
1608 vc->vc_underline = 0;
1611 vc->vc_color = vc->vc_def_color;
1614 struct rgb { u8 r; u8 g; u8 b; };
1616 static void rgb_from_256(int i, struct rgb *c)
1618 if (i < 8) { /* Standard colours. */
1619 c->r = i&1 ? 0xaa : 0x00;
1620 c->g = i&2 ? 0xaa : 0x00;
1621 c->b = i&4 ? 0xaa : 0x00;
1622 } else if (i < 16) {
1623 c->r = i&1 ? 0xff : 0x55;
1624 c->g = i&2 ? 0xff : 0x55;
1625 c->b = i&4 ? 0xff : 0x55;
1626 } else if (i < 232) { /* 6x6x6 colour cube. */
1627 c->r = (i - 16) / 36 * 85 / 2;
1628 c->g = (i - 16) / 6 % 6 * 85 / 2;
1629 c->b = (i - 16) % 6 * 85 / 2;
1630 } else /* Grayscale ramp. */
1631 c->r = c->g = c->b = i * 10 - 2312;
1634 static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
1636 u8 hue = 0, max = max3(c->r, c->g, c->b);
1645 if (hue == 7 && max <= 0x55) {
1647 vc->vc_intensity = 2;
1648 } else if (max > 0xaa)
1649 vc->vc_intensity = 2;
1651 vc->vc_intensity = 1;
1653 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1656 static void rgb_background(struct vc_data *vc, const struct rgb *c)
1658 /* For backgrounds, err on the dark side. */
1659 vc->vc_color = (vc->vc_color & 0x0f)
1660 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
1664 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1665 * and thus need to be detected and ignored by hand. That standard also
1666 * wants : rather than ; as separators but sequences containing : are currently
1667 * completely ignored by the parser.
1669 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1672 static int vc_t416_color(struct vc_data *vc, int i,
1673 void(*set_color)(struct vc_data *vc, const struct rgb *c))
1678 if (i > vc->vc_npar)
1681 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
1684 rgb_from_256(vc->vc_par[i], &c);
1685 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
1687 c.r = vc->vc_par[i + 1];
1688 c.g = vc->vc_par[i + 2];
1689 c.b = vc->vc_par[i + 3];
1699 /* console_lock is held */
1700 static void csi_m(struct vc_data *vc)
1704 for (i = 0; i <= vc->vc_npar; i++)
1705 switch (vc->vc_par[i]) {
1706 case 0: /* all attributes off */
1710 vc->vc_intensity = 2;
1713 vc->vc_intensity = 0;
1720 * No console drivers support double underline, so
1721 * convert it to a single underline.
1724 vc->vc_underline = 1;
1732 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1733 * Select primary font, don't display control chars if
1734 * defined, don't set bit 8 on output.
1736 vc->vc_translate = set_translate(vc->vc_charset == 0
1738 : vc->vc_G1_charset, vc);
1739 vc->vc_disp_ctrl = 0;
1740 vc->vc_toggle_meta = 0;
1742 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1743 * Select first alternate font, lets chars < 32 be
1744 * displayed as ROM chars.
1746 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1747 vc->vc_disp_ctrl = 1;
1748 vc->vc_toggle_meta = 0;
1750 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1751 * Select second alternate font, toggle high bit
1752 * before displaying as ROM char.
1754 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1755 vc->vc_disp_ctrl = 1;
1756 vc->vc_toggle_meta = 1;
1759 vc->vc_intensity = 1;
1765 vc->vc_underline = 0;
1774 i = vc_t416_color(vc, i, rgb_foreground);
1777 i = vc_t416_color(vc, i, rgb_background);
1780 vc->vc_color = (vc->vc_def_color & 0x0f) |
1781 (vc->vc_color & 0xf0);
1784 vc->vc_color = (vc->vc_def_color & 0xf0) |
1785 (vc->vc_color & 0x0f);
1788 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1789 if (vc->vc_par[i] < 100)
1790 vc->vc_intensity = 2;
1791 vc->vc_par[i] -= 60;
1793 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1794 vc->vc_color = color_table[vc->vc_par[i] - 30]
1795 | (vc->vc_color & 0xf0);
1796 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1797 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1798 | (vc->vc_color & 0x0f);
1804 static void respond_string(const char *p, struct tty_port *port)
1807 tty_insert_flip_char(port, *p, 0);
1810 tty_schedule_flip(port);
1813 static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1817 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1818 respond_string(buf, tty->port);
1821 static inline void status_report(struct tty_struct *tty)
1823 respond_string("\033[0n", tty->port); /* Terminal ok */
1826 static inline void respond_ID(struct tty_struct *tty)
1828 respond_string(VT102ID, tty->port);
1831 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1835 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1837 respond_string(buf, tty->port);
1840 /* invoked via ioctl(TIOCLINUX) and through set_selection_user */
1841 int mouse_reporting(void)
1843 return vc_cons[fg_console].d->vc_report_mouse;
1846 /* console_lock is held */
1847 static void set_mode(struct vc_data *vc, int on_off)
1851 for (i = 0; i <= vc->vc_npar; i++)
1852 if (vc->vc_priv == EPdec) {
1853 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1854 case 1: /* Cursor keys send ^[Ox/^[[x */
1856 set_kbd(vc, decckm);
1858 clr_kbd(vc, decckm);
1860 case 3: /* 80/132 mode switch unimplemented */
1862 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1863 /* this alone does not suffice; some user mode
1864 utility has to change the hardware regs */
1867 case 5: /* Inverted screen on/off */
1868 if (vc->vc_decscnm != on_off) {
1869 vc->vc_decscnm = on_off;
1870 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1874 case 6: /* Origin relative/absolute */
1875 vc->vc_decom = on_off;
1878 case 7: /* Autowrap on/off */
1879 vc->vc_decawm = on_off;
1881 case 8: /* Autorepeat on/off */
1883 set_kbd(vc, decarm);
1885 clr_kbd(vc, decarm);
1888 vc->vc_report_mouse = on_off ? 1 : 0;
1890 case 25: /* Cursor on/off */
1891 vc->vc_deccm = on_off;
1894 vc->vc_report_mouse = on_off ? 2 : 0;
1898 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1899 case 3: /* Monitor (display ctrls) */
1900 vc->vc_disp_ctrl = on_off;
1902 case 4: /* Insert Mode on/off */
1903 vc->vc_decim = on_off;
1905 case 20: /* Lf, Enter == CrLf/Lf */
1915 /* console_lock is held */
1916 static void setterm_command(struct vc_data *vc)
1918 switch (vc->vc_par[0]) {
1919 case 1: /* set color for underline mode */
1920 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1921 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1922 if (vc->vc_underline)
1926 case 2: /* set color for half intensity mode */
1927 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1928 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1929 if (vc->vc_intensity == 0)
1933 case 8: /* store colors as defaults */
1934 vc->vc_def_color = vc->vc_attr;
1935 if (vc->vc_hi_font_mask == 0x100)
1936 vc->vc_def_color >>= 1;
1940 case 9: /* set blanking interval */
1941 blankinterval = min(vc->vc_par[1], 60U) * 60;
1942 poke_blanked_console();
1944 case 10: /* set bell frequency in Hz */
1945 if (vc->vc_npar >= 1)
1946 vc->vc_bell_pitch = vc->vc_par[1];
1948 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1950 case 11: /* set bell duration in msec */
1951 if (vc->vc_npar >= 1)
1952 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1953 msecs_to_jiffies(vc->vc_par[1]) : 0;
1955 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1957 case 12: /* bring specified console to the front */
1958 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1959 set_console(vc->vc_par[1] - 1);
1961 case 13: /* unblank the screen */
1962 poke_blanked_console();
1964 case 14: /* set vesa powerdown interval */
1965 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1967 case 15: /* activate the previous console */
1968 set_console(last_console);
1970 case 16: /* set cursor blink duration in msec */
1971 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1972 vc->vc_par[1] <= USHRT_MAX)
1973 vc->vc_cur_blink_ms = vc->vc_par[1];
1975 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1980 /* console_lock is held */
1981 static void csi_at(struct vc_data *vc, unsigned int nr)
1983 if (nr > vc->vc_cols - vc->vc_x)
1984 nr = vc->vc_cols - vc->vc_x;
1987 insert_char(vc, nr);
1990 /* console_lock is held */
1991 static void csi_L(struct vc_data *vc, unsigned int nr)
1993 if (nr > vc->vc_rows - vc->vc_y)
1994 nr = vc->vc_rows - vc->vc_y;
1997 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_DOWN, nr);
1998 vc->vc_need_wrap = 0;
2001 /* console_lock is held */
2002 static void csi_P(struct vc_data *vc, unsigned int nr)
2004 if (nr > vc->vc_cols - vc->vc_x)
2005 nr = vc->vc_cols - vc->vc_x;
2008 delete_char(vc, nr);
2011 /* console_lock is held */
2012 static void csi_M(struct vc_data *vc, unsigned int nr)
2014 if (nr > vc->vc_rows - vc->vc_y)
2015 nr = vc->vc_rows - vc->vc_y;
2018 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_UP, nr);
2019 vc->vc_need_wrap = 0;
2022 /* console_lock is held (except via vc_init->reset_terminal */
2023 static void save_cur(struct vc_data *vc)
2025 vc->vc_saved_x = vc->vc_x;
2026 vc->vc_saved_y = vc->vc_y;
2027 vc->vc_s_intensity = vc->vc_intensity;
2028 vc->vc_s_italic = vc->vc_italic;
2029 vc->vc_s_underline = vc->vc_underline;
2030 vc->vc_s_blink = vc->vc_blink;
2031 vc->vc_s_reverse = vc->vc_reverse;
2032 vc->vc_s_charset = vc->vc_charset;
2033 vc->vc_s_color = vc->vc_color;
2034 vc->vc_saved_G0 = vc->vc_G0_charset;
2035 vc->vc_saved_G1 = vc->vc_G1_charset;
2038 /* console_lock is held */
2039 static void restore_cur(struct vc_data *vc)
2041 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
2042 vc->vc_intensity = vc->vc_s_intensity;
2043 vc->vc_italic = vc->vc_s_italic;
2044 vc->vc_underline = vc->vc_s_underline;
2045 vc->vc_blink = vc->vc_s_blink;
2046 vc->vc_reverse = vc->vc_s_reverse;
2047 vc->vc_charset = vc->vc_s_charset;
2048 vc->vc_color = vc->vc_s_color;
2049 vc->vc_G0_charset = vc->vc_saved_G0;
2050 vc->vc_G1_charset = vc->vc_saved_G1;
2051 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
2053 vc->vc_need_wrap = 0;
2056 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
2057 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
2060 /* console_lock is held (except via vc_init()) */
2061 static void reset_terminal(struct vc_data *vc, int do_clear)
2064 vc->vc_bottom = vc->vc_rows;
2065 vc->vc_state = ESnormal;
2066 vc->vc_priv = EPecma;
2067 vc->vc_translate = set_translate(LAT1_MAP, vc);
2068 vc->vc_G0_charset = LAT1_MAP;
2069 vc->vc_G1_charset = GRAF_MAP;
2071 vc->vc_need_wrap = 0;
2072 vc->vc_report_mouse = 0;
2073 vc->vc_utf = default_utf8;
2074 vc->vc_utf_count = 0;
2076 vc->vc_disp_ctrl = 0;
2077 vc->vc_toggle_meta = 0;
2082 vc->vc_deccm = global_cursor_default;
2085 vt_reset_keyboard(vc->vc_num);
2087 vc->vc_cursor_type = cur_default;
2088 vc->vc_complement_mask = vc->vc_s_complement_mask;
2093 vc->vc_tab_stop[0] =
2094 vc->vc_tab_stop[1] =
2095 vc->vc_tab_stop[2] =
2096 vc->vc_tab_stop[3] =
2097 vc->vc_tab_stop[4] =
2098 vc->vc_tab_stop[5] =
2099 vc->vc_tab_stop[6] =
2100 vc->vc_tab_stop[7] = 0x01010101;
2102 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2103 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
2104 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
2112 /* console_lock is held */
2113 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2116 * Control characters can be used in the _middle_
2117 * of an escape sequence.
2119 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2125 if (vc->vc_state == ESosc)
2126 vc->vc_state = ESnormal;
2127 else if (vc->vc_bell_duration)
2128 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2134 vc->vc_pos -= (vc->vc_x << 1);
2135 while (vc->vc_x < vc->vc_cols - 1) {
2137 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
2140 vc->vc_pos += (vc->vc_x << 1);
2141 notify_write(vc, '\t');
2143 case 10: case 11: case 12:
2145 if (!is_kbd(vc, lnm))
2153 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2154 vc->vc_disp_ctrl = 1;
2158 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2159 vc->vc_disp_ctrl = 0;
2162 vc->vc_state = ESnormal;
2165 vc->vc_state = ESesc;
2171 vc->vc_state = ESsquare;
2174 switch(vc->vc_state) {
2176 vc->vc_state = ESnormal;
2179 vc->vc_state = ESsquare;
2182 vc->vc_state = ESnonstd;
2185 vc->vc_state = ESpercent;
2198 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
2210 vc->vc_state = ESsetG0;
2213 vc->vc_state = ESsetG1;
2216 vc->vc_state = EShash;
2219 reset_terminal(vc, 1);
2221 case '>': /* Numeric keypad */
2222 clr_kbd(vc, kbdapplic);
2224 case '=': /* Appl. keypad */
2225 set_kbd(vc, kbdapplic);
2230 if (c=='P') { /* palette escape sequence */
2231 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2232 vc->vc_par[vc->vc_npar] = 0;
2234 vc->vc_state = ESpalette;
2236 } else if (c=='R') { /* reset palette */
2238 vc->vc_state = ESnormal;
2239 } else if (c>='0' && c<='9')
2240 vc->vc_state = ESosc;
2242 vc->vc_state = ESnormal;
2246 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
2247 if (vc->vc_npar == 7) {
2248 int i = vc->vc_par[0] * 3, j = 1;
2249 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2250 vc->vc_palette[i++] += vc->vc_par[j++];
2251 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2252 vc->vc_palette[i++] += vc->vc_par[j++];
2253 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2254 vc->vc_palette[i] += vc->vc_par[j];
2256 vc->vc_state = ESnormal;
2259 vc->vc_state = ESnormal;
2262 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2263 vc->vc_par[vc->vc_npar] = 0;
2265 vc->vc_state = ESgetpars;
2266 if (c == '[') { /* Function key */
2267 vc->vc_state=ESfunckey;
2272 vc->vc_priv = EPdec;
2284 vc->vc_priv = EPecma;
2287 if (c == ';' && vc->vc_npar < NPAR - 1) {
2290 } else if (c>='0' && c<='9') {
2291 vc->vc_par[vc->vc_npar] *= 10;
2292 vc->vc_par[vc->vc_npar] += c - '0';
2295 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
2296 vc->vc_state = EScsiignore;
2299 vc->vc_state = ESnormal;
2302 if (vc->vc_priv <= EPdec)
2306 if (vc->vc_priv <= EPdec)
2310 if (vc->vc_priv == EPdec) {
2312 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2314 vc->vc_cursor_type = cur_default;
2319 if (vc->vc_priv == EPdec) {
2322 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2324 vc->vc_complement_mask = vc->vc_s_complement_mask;
2329 if (vc->vc_priv == EPecma) {
2330 if (vc->vc_par[0] == 5)
2332 else if (vc->vc_par[0] == 6)
2333 cursor_report(vc, tty);
2337 if (vc->vc_priv != EPecma) {
2338 vc->vc_priv = EPecma;
2345 gotoxy(vc, vc->vc_par[0], vc->vc_y);
2350 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
2355 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
2360 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
2365 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
2370 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
2375 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
2380 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2387 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2390 csi_J(vc, vc->vc_par[0]);
2393 csi_K(vc, vc->vc_par[0]);
2396 csi_L(vc, vc->vc_par[0]);
2399 csi_M(vc, vc->vc_par[0]);
2402 csi_P(vc, vc->vc_par[0]);
2410 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
2411 else if (vc->vc_par[0] == 3) {
2412 vc->vc_tab_stop[0] =
2413 vc->vc_tab_stop[1] =
2414 vc->vc_tab_stop[2] =
2415 vc->vc_tab_stop[3] =
2416 vc->vc_tab_stop[4] =
2417 vc->vc_tab_stop[5] =
2418 vc->vc_tab_stop[6] =
2419 vc->vc_tab_stop[7] = 0;
2425 case 'q': /* DECLL - but only 3 leds */
2426 /* map 0,1,2,3 to 0,1,2,4 */
2427 if (vc->vc_par[0] < 4)
2428 vt_set_led_state(vc->vc_num,
2429 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2435 vc->vc_par[1] = vc->vc_rows;
2436 /* Minimum allowed region is 2 lines */
2437 if (vc->vc_par[0] < vc->vc_par[1] &&
2438 vc->vc_par[1] <= vc->vc_rows) {
2439 vc->vc_top = vc->vc_par[0] - 1;
2440 vc->vc_bottom = vc->vc_par[1];
2451 csi_X(vc, vc->vc_par[0]);
2454 csi_at(vc, vc->vc_par[0]);
2456 case ']': /* setterm functions */
2457 setterm_command(vc);
2462 if (c >= 20 && c <= 0x3f)
2464 vc->vc_state = ESnormal;
2467 vc->vc_state = ESnormal;
2469 case '@': /* defined in ISO 2022 */
2472 case 'G': /* prelim official escape code */
2473 case '8': /* retained for compatibility */
2479 vc->vc_state = ESnormal;
2482 vc->vc_state = ESnormal;
2484 /* DEC screen alignment test. kludge :-) */
2485 vc->vc_video_erase_char =
2486 (vc->vc_video_erase_char & 0xff00) | 'E';
2488 vc->vc_video_erase_char =
2489 (vc->vc_video_erase_char & 0xff00) | ' ';
2490 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2495 vc->vc_G0_charset = GRAF_MAP;
2497 vc->vc_G0_charset = LAT1_MAP;
2499 vc->vc_G0_charset = IBMPC_MAP;
2501 vc->vc_G0_charset = USER_MAP;
2502 if (vc->vc_charset == 0)
2503 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2504 vc->vc_state = ESnormal;
2508 vc->vc_G1_charset = GRAF_MAP;
2510 vc->vc_G1_charset = LAT1_MAP;
2512 vc->vc_G1_charset = IBMPC_MAP;
2514 vc->vc_G1_charset = USER_MAP;
2515 if (vc->vc_charset == 1)
2516 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2517 vc->vc_state = ESnormal;
2522 vc->vc_state = ESnormal;
2526 /* is_double_width() is based on the wcwidth() implementation by
2527 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2528 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2535 static int ucs_cmp(const void *key, const void *elt)
2537 uint32_t ucs = *(uint32_t *)key;
2538 struct interval e = *(struct interval *) elt;
2542 else if (ucs < e.first)
2547 static int is_double_width(uint32_t ucs)
2549 static const struct interval double_width[] = {
2550 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2551 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2552 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2553 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2555 if (ucs < double_width[0].first ||
2556 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2559 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2560 sizeof(struct interval), ucs_cmp) != NULL;
2563 static void con_flush(struct vc_data *vc, unsigned long draw_from,
2564 unsigned long draw_to, int *draw_x)
2569 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2570 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2574 /* acquires console_lock */
2575 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2577 int c, next_c, tc, ok, n = 0, draw_x = -1;
2578 unsigned int currcons;
2579 unsigned long draw_from = 0, draw_to = 0;
2581 unsigned char vc_attr;
2582 struct vt_notifier_param param;
2586 u16 himask, charmask;
2592 vc = tty->driver_data;
2594 pr_err("vt: argh, driver_data is NULL !\n");
2599 currcons = vc->vc_num;
2600 if (!vc_cons_allocated(currcons)) {
2601 /* could this happen? */
2602 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2607 himask = vc->vc_hi_font_mask;
2608 charmask = himask ? 0x1ff : 0xff;
2610 /* undraw cursor first */
2616 while (!tty->stopped && count) {
2626 /* Do no translation at all in control states */
2627 if (vc->vc_state != ESnormal) {
2629 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2630 /* Combine UTF-8 into Unicode in vc_utf_char.
2631 * vc_utf_count is the number of continuation bytes still
2632 * expected to arrive.
2633 * vc_npar is the number of continuation bytes arrived so
2637 if ((c & 0xc0) == 0x80) {
2638 /* Continuation byte received */
2639 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2640 if (vc->vc_utf_count) {
2641 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2643 if (--vc->vc_utf_count) {
2644 /* Still need some bytes */
2647 /* Got a whole character */
2648 c = vc->vc_utf_char;
2649 /* Reject overlong sequences */
2650 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2651 c > utf8_length_changes[vc->vc_npar])
2654 /* Unexpected continuation byte */
2655 vc->vc_utf_count = 0;
2659 /* Single ASCII byte or first byte of a sequence received */
2660 if (vc->vc_utf_count) {
2661 /* Continuation byte expected */
2663 vc->vc_utf_count = 0;
2665 } else if (c > 0x7f) {
2666 /* First byte of a multibyte sequence received */
2668 if ((c & 0xe0) == 0xc0) {
2669 vc->vc_utf_count = 1;
2670 vc->vc_utf_char = (c & 0x1f);
2671 } else if ((c & 0xf0) == 0xe0) {
2672 vc->vc_utf_count = 2;
2673 vc->vc_utf_char = (c & 0x0f);
2674 } else if ((c & 0xf8) == 0xf0) {
2675 vc->vc_utf_count = 3;
2676 vc->vc_utf_char = (c & 0x07);
2677 } else if ((c & 0xfc) == 0xf8) {
2678 vc->vc_utf_count = 4;
2679 vc->vc_utf_char = (c & 0x03);
2680 } else if ((c & 0xfe) == 0xfc) {
2681 vc->vc_utf_count = 5;
2682 vc->vc_utf_char = (c & 0x01);
2684 /* 254 and 255 are invalid */
2687 if (vc->vc_utf_count) {
2688 /* Still need some bytes */
2692 /* Nothing to do if an ASCII byte was received */
2694 /* End of UTF-8 decoding. */
2695 /* c is the received character, or U+FFFD for invalid sequences. */
2696 /* Replace invalid Unicode code points with U+FFFD too */
2697 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2700 } else { /* no utf or alternate charset mode */
2701 tc = vc_translate(vc, c);
2705 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2706 ¶m) == NOTIFY_STOP)
2709 /* If the original code was a control character we
2710 * only allow a glyph to be displayed if the code is
2711 * not normally used (such as for cursor movement) or
2712 * if the disp_ctrl mode has been explicitly enabled.
2713 * Certain characters (as given by the CTRL_ALWAYS
2714 * bitmap) are always displayed as control characters,
2715 * as the console would be pretty useless without
2716 * them; to display an arbitrary font position use the
2717 * direct-to-font zone in UTF-8 mode.
2719 ok = tc && (c >= 32 ||
2720 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2721 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2722 && (c != 127 || vc->vc_disp_ctrl)
2725 if (vc->vc_state == ESnormal && ok) {
2726 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2727 if (is_double_width(c))
2730 /* Now try to find out how to display it */
2731 tc = conv_uni_to_pc(vc, tc);
2732 if (tc & ~charmask) {
2733 if (tc == -1 || tc == -2) {
2734 continue; /* nothing to display */
2736 /* Glyph not found */
2737 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2738 /* In legacy mode use the glyph we get by a 1:1 mapping.
2739 This would make absolutely no sense with Unicode in mind,
2740 but do this for ASCII characters since a font may lack
2741 Unicode mapping info and we don't want to end up with
2742 having question marks only. */
2745 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2746 tc = conv_uni_to_pc(vc, 0xfffd);
2749 tc = conv_uni_to_pc(vc, '?');
2750 if (tc < 0) tc = '?';
2756 vc_attr = vc->vc_attr;
2758 /* invert vc_attr */
2759 if (!vc->vc_can_do_color) {
2760 vc_attr = (vc->vc_attr) ^ 0x08;
2761 } else if (vc->vc_hi_font_mask == 0x100) {
2762 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2764 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2766 con_flush(vc, draw_from, draw_to, &draw_x);
2771 if (vc->vc_need_wrap || vc->vc_decim)
2772 con_flush(vc, draw_from, draw_to,
2774 if (vc->vc_need_wrap) {
2780 vc_uniscr_putc(vc, next_c);
2782 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2783 (vc_attr << 8) + tc,
2784 (u16 *) vc->vc_pos);
2785 if (con_should_update(vc) && draw_x < 0) {
2787 draw_from = vc->vc_pos;
2789 if (vc->vc_x == vc->vc_cols - 1) {
2790 vc->vc_need_wrap = vc->vc_decawm;
2791 draw_to = vc->vc_pos + 2;
2794 draw_to = (vc->vc_pos += 2);
2797 if (!--width) break;
2799 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2800 if (tc < 0) tc = ' ';
2803 notify_write(vc, c);
2806 con_flush(vc, draw_from, draw_to, &draw_x);
2813 goto rescan_last_byte;
2817 con_flush(vc, draw_from, draw_to, &draw_x);
2818 do_con_trol(tty, vc, orig);
2820 con_flush(vc, draw_from, draw_to, &draw_x);
2821 vc_uniscr_debug_check(vc);
2822 console_conditional_schedule();
2829 * This is the console switching callback.
2831 * Doing console switching in a process context allows
2832 * us to do the switches asynchronously (needed when we want
2833 * to switch due to a keyboard interrupt). Synchronization
2834 * with other console code and prevention of re-entrancy is
2835 * ensured with console_lock.
2837 static void console_callback(struct work_struct *ignored)
2841 if (want_console >= 0) {
2842 if (want_console != fg_console &&
2843 vc_cons_allocated(want_console)) {
2844 hide_cursor(vc_cons[fg_console].d);
2845 change_console(vc_cons[want_console].d);
2846 /* we only changed when the console had already
2847 been allocated - a new console is not created
2848 in an interrupt routine */
2852 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2853 do_poke_blanked_console = 0;
2854 poke_blanked_console();
2856 if (scrollback_delta) {
2857 struct vc_data *vc = vc_cons[fg_console].d;
2859 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
2860 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2861 scrollback_delta = 0;
2863 if (blank_timer_expired) {
2865 blank_timer_expired = 0;
2867 notify_update(vc_cons[fg_console].d);
2872 int set_console(int nr)
2874 struct vc_data *vc = vc_cons[fg_console].d;
2876 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2877 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2880 * Console switch will fail in console_callback() or
2881 * change_console() so there is no point scheduling
2884 * Existing set_console() users don't check the return
2885 * value so this shouldn't break anything
2891 schedule_console_callback();
2896 struct tty_driver *console_driver;
2898 #ifdef CONFIG_VT_CONSOLE
2901 * vt_kmsg_redirect() - Sets/gets the kernel message console
2902 * @new: The new virtual terminal number or -1 if the console should stay
2905 * By default, the kernel messages are always printed on the current virtual
2906 * console. However, the user may modify that default with the
2907 * TIOCL_SETKMSGREDIRECT ioctl call.
2909 * This function sets the kernel message console to be @new. It returns the old
2910 * virtual console number. The virtual terminal number 0 (both as parameter and
2911 * return value) means no redirection (i.e. always printed on the currently
2914 * The parameter -1 means that only the current console is returned, but the
2915 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2916 * case to make the code more understandable.
2918 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2919 * the parameter and always returns 0.
2921 int vt_kmsg_redirect(int new)
2923 static int kmsg_con;
2926 return xchg(&kmsg_con, new);
2932 * Console on virtual terminal
2934 * The console must be locked when we get here.
2937 static void vt_console_print(struct console *co, const char *b, unsigned count)
2939 struct vc_data *vc = vc_cons[fg_console].d;
2941 static DEFINE_SPINLOCK(printing_lock);
2942 const ushort *start;
2943 ushort start_x, cnt;
2946 /* console busy or not yet initialized */
2949 if (!spin_trylock(&printing_lock))
2952 kmsg_console = vt_get_kmsg_redirect();
2953 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2954 vc = vc_cons[kmsg_console - 1].d;
2956 if (!vc_cons_allocated(fg_console)) {
2958 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2962 if (vc->vc_mode != KD_TEXT)
2965 /* undraw cursor first */
2969 start = (ushort *)vc->vc_pos;
2974 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2975 if (cnt && con_is_visible(vc))
2976 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, start_x);
2978 if (c == 8) { /* backspace */
2980 start = (ushort *)vc->vc_pos;
2987 start = (ushort *)vc->vc_pos;
2989 if (c == 10 || c == 13)
2992 vc_uniscr_putc(vc, c);
2993 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2994 notify_write(vc, c);
2996 if (vc->vc_x == vc->vc_cols - 1) {
2997 vc->vc_need_wrap = 1;
3003 if (cnt && con_is_visible(vc))
3004 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, start_x);
3009 spin_unlock(&printing_lock);
3012 static struct tty_driver *vt_console_device(struct console *c, int *index)
3014 *index = c->index ? c->index-1 : fg_console;
3015 return console_driver;
3018 static struct console vt_console_driver = {
3020 .write = vt_console_print,
3021 .device = vt_console_device,
3022 .unblank = unblank_screen,
3023 .flags = CON_PRINTBUFFER,
3029 * Handling of Linux-specific VC ioctls
3033 * Generally a bit racy with respect to console_lock();.
3035 * There are some functions which don't need it.
3037 * There are some functions which can sleep for arbitrary periods
3038 * (paste_selection) but we don't need the lock there anyway.
3040 * set_selection_user has locking, and definitely needs it
3043 int tioclinux(struct tty_struct *tty, unsigned long arg)
3046 char __user *p = (char __user *)arg;
3050 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3052 if (get_user(type, p))
3059 ret = set_selection_user((struct tiocl_selection
3060 __user *)(p+1), tty);
3062 case TIOCL_PASTESEL:
3063 ret = paste_selection(tty);
3065 case TIOCL_UNBLANKSCREEN:
3070 case TIOCL_SELLOADLUT:
3072 ret = sel_loadlut(p);
3075 case TIOCL_GETSHIFTSTATE:
3078 * Make it possible to react to Shift+Mousebutton.
3079 * Note that 'shift_state' is an undocumented
3080 * kernel-internal variable; programs not closely
3081 * related to the kernel should not use this.
3083 data = vt_get_shift_state();
3084 ret = put_user(data, p);
3086 case TIOCL_GETMOUSEREPORTING:
3087 console_lock(); /* May be overkill */
3088 data = mouse_reporting();
3090 ret = put_user(data, p);
3092 case TIOCL_SETVESABLANK:
3094 ret = set_vesa_blanking(p);
3097 case TIOCL_GETKMSGREDIRECT:
3098 data = vt_get_kmsg_redirect();
3099 ret = put_user(data, p);
3101 case TIOCL_SETKMSGREDIRECT:
3102 if (!capable(CAP_SYS_ADMIN)) {
3105 if (get_user(data, p+1))
3108 vt_kmsg_redirect(data);
3111 case TIOCL_GETFGCONSOLE:
3112 /* No locking needed as this is a transiently
3113 correct return anyway if the caller hasn't
3114 disabled switching */
3117 case TIOCL_SCROLLCONSOLE:
3118 if (get_user(lines, (s32 __user *)(p+4))) {
3121 /* Need the console lock here. Note that lots
3122 of other calls need fixing before the lock
3123 is actually useful ! */
3125 scrollfront(vc_cons[fg_console].d, lines);
3130 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
3136 case TIOCL_BLANKEDSCREEN:
3137 ret = console_blanked;
3147 * /dev/ttyN handling
3150 static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3154 retval = do_con_write(tty, buf, count);
3155 con_flush_chars(tty);
3160 static int con_put_char(struct tty_struct *tty, unsigned char ch)
3163 return 0; /* n_r3964 calls put_char() from interrupt context */
3164 return do_con_write(tty, &ch, 1);
3167 static int con_write_room(struct tty_struct *tty)
3171 return 32768; /* No limit, really; we're not buffering */
3174 static int con_chars_in_buffer(struct tty_struct *tty)
3176 return 0; /* we're not buffering */
3180 * con_throttle and con_unthrottle are only used for
3181 * paste_selection(), which has to stuff in a large number of
3184 static void con_throttle(struct tty_struct *tty)
3188 static void con_unthrottle(struct tty_struct *tty)
3190 struct vc_data *vc = tty->driver_data;
3192 wake_up_interruptible(&vc->paste_wait);
3196 * Turn the Scroll-Lock LED on when the tty is stopped
3198 static void con_stop(struct tty_struct *tty)
3203 console_num = tty->index;
3204 if (!vc_cons_allocated(console_num))
3206 vt_kbd_con_stop(console_num);
3210 * Turn the Scroll-Lock LED off when the console is started
3212 static void con_start(struct tty_struct *tty)
3217 console_num = tty->index;
3218 if (!vc_cons_allocated(console_num))
3220 vt_kbd_con_start(console_num);
3223 static void con_flush_chars(struct tty_struct *tty)
3227 if (in_interrupt()) /* from flush_to_ldisc */
3230 /* if we race with con_close(), vt may be null */
3232 vc = tty->driver_data;
3239 * Allocate the console screen memory.
3241 static int con_install(struct tty_driver *driver, struct tty_struct *tty)
3243 unsigned int currcons = tty->index;
3248 ret = vc_allocate(currcons);
3252 vc = vc_cons[currcons].d;
3254 /* Still being freed */
3260 ret = tty_port_install(&vc->port, driver, tty);
3264 tty->driver_data = vc;
3266 tty_port_get(&vc->port);
3268 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3269 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3270 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3273 tty->termios.c_iflag |= IUTF8;
3275 tty->termios.c_iflag &= ~IUTF8;
3281 static int con_open(struct tty_struct *tty, struct file *filp)
3283 /* everything done in install */
3288 static void con_close(struct tty_struct *tty, struct file *filp)
3290 /* Nothing to do - we defer to shutdown */
3293 static void con_shutdown(struct tty_struct *tty)
3295 struct vc_data *vc = tty->driver_data;
3298 vc->port.tty = NULL;
3302 static void con_cleanup(struct tty_struct *tty)
3304 struct vc_data *vc = tty->driver_data;
3306 tty_port_put(&vc->port);
3309 static int default_color = 7; /* white */
3310 static int default_italic_color = 2; // green (ASCII)
3311 static int default_underline_color = 3; // cyan (ASCII)
3312 module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
3313 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3314 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3316 static void vc_init(struct vc_data *vc, unsigned int rows,
3317 unsigned int cols, int do_clear)
3323 vc->vc_size_row = cols << 1;
3324 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3327 vc->vc_pos = vc->vc_origin;
3329 for (j=k=0; j<16; j++) {
3330 vc->vc_palette[k++] = default_red[j] ;
3331 vc->vc_palette[k++] = default_grn[j] ;
3332 vc->vc_palette[k++] = default_blu[j] ;
3334 vc->vc_def_color = default_color;
3335 vc->vc_ulcolor = default_underline_color;
3336 vc->vc_itcolor = default_italic_color;
3337 vc->vc_halfcolor = 0x08; /* grey */
3338 init_waitqueue_head(&vc->paste_wait);
3339 reset_terminal(vc, do_clear);
3343 * This routine initializes console interrupts, and does nothing
3344 * else. If you want the screen to clear, call tty_write with
3345 * the appropriate escape-sequence.
3348 static int __init con_init(void)
3350 const char *display_desc = NULL;
3352 unsigned int currcons = 0, i;
3357 conswitchp = &dummy_con;
3358 display_desc = conswitchp->con_startup();
3359 if (!display_desc) {
3365 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3366 struct con_driver *con_driver = ®istered_con_driver[i];
3368 if (con_driver->con == NULL) {
3369 con_driver->con = conswitchp;
3370 con_driver->desc = display_desc;
3371 con_driver->flag = CON_DRIVER_FLAG_INIT;
3372 con_driver->first = 0;
3373 con_driver->last = MAX_NR_CONSOLES - 1;
3378 for (i = 0; i < MAX_NR_CONSOLES; i++)
3379 con_driver_map[i] = conswitchp;
3381 if (blankinterval) {
3382 blank_state = blank_normal_wait;
3383 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3386 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
3387 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
3388 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
3389 tty_port_init(&vc->port);
3390 visual_init(vc, currcons, 1);
3391 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
3392 vc_init(vc, vc->vc_rows, vc->vc_cols,
3393 currcons || !vc->vc_sw->con_save_screen);
3395 currcons = fg_console = 0;
3396 master_display_fg = vc = vc_cons[currcons].d;
3399 gotoxy(vc, vc->vc_x, vc->vc_y);
3402 pr_info("Console: %s %s %dx%d\n",
3403 vc->vc_can_do_color ? "colour" : "mono",
3404 display_desc, vc->vc_cols, vc->vc_rows);
3409 #ifdef CONFIG_VT_CONSOLE
3410 register_console(&vt_console_driver);
3414 console_initcall(con_init);
3416 static const struct tty_operations con_ops = {
3417 .install = con_install,
3421 .write_room = con_write_room,
3422 .put_char = con_put_char,
3423 .flush_chars = con_flush_chars,
3424 .chars_in_buffer = con_chars_in_buffer,
3426 #ifdef CONFIG_COMPAT
3427 .compat_ioctl = vt_compat_ioctl,
3431 .throttle = con_throttle,
3432 .unthrottle = con_unthrottle,
3433 .resize = vt_resize,
3434 .shutdown = con_shutdown,
3435 .cleanup = con_cleanup,
3438 static struct cdev vc0_cdev;
3440 static ssize_t show_tty_active(struct device *dev,
3441 struct device_attribute *attr, char *buf)
3443 return sprintf(buf, "tty%d\n", fg_console + 1);
3445 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3447 static struct attribute *vt_dev_attrs[] = {
3448 &dev_attr_active.attr,
3452 ATTRIBUTE_GROUPS(vt_dev);
3454 int __init vty_init(const struct file_operations *console_fops)
3456 cdev_init(&vc0_cdev, console_fops);
3457 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3458 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3459 panic("Couldn't register /dev/tty0 driver\n");
3460 tty0dev = device_create_with_groups(tty_class, NULL,
3461 MKDEV(TTY_MAJOR, 0), NULL,
3462 vt_dev_groups, "tty0");
3463 if (IS_ERR(tty0dev))
3468 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3469 if (!console_driver)
3470 panic("Couldn't allocate console driver\n");
3472 console_driver->name = "tty";
3473 console_driver->name_base = 1;
3474 console_driver->major = TTY_MAJOR;
3475 console_driver->minor_start = 1;
3476 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3477 console_driver->init_termios = tty_std_termios;
3479 console_driver->init_termios.c_iflag |= IUTF8;
3480 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3481 tty_set_operations(console_driver, &con_ops);
3482 if (tty_register_driver(console_driver))
3483 panic("Couldn't register console driver\n");
3486 #ifdef CONFIG_MDA_CONSOLE
3492 #ifndef VT_SINGLE_DRIVER
3494 static struct class *vtconsole_class;
3496 static int do_bind_con_driver(const struct consw *csw, int first, int last,
3499 struct module *owner = csw->owner;
3500 const char *desc = NULL;
3501 struct con_driver *con_driver;
3502 int i, j = -1, k = -1, retval = -ENODEV;
3504 if (!try_module_get(owner))
3507 WARN_CONSOLE_UNLOCKED();
3509 /* check if driver is registered */
3510 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3511 con_driver = ®istered_con_driver[i];
3513 if (con_driver->con == csw) {
3514 desc = con_driver->desc;
3523 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3525 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3530 module_put(conswitchp->owner);
3532 __module_get(owner);
3536 first = max(first, con_driver->first);
3537 last = min(last, con_driver->last);
3539 for (i = first; i <= last; i++) {
3541 struct vc_data *vc = vc_cons[i].d;
3543 if (con_driver_map[i])
3544 module_put(con_driver_map[i]->owner);
3545 __module_get(owner);
3546 con_driver_map[i] = csw;
3548 if (!vc || !vc->vc_sw)
3553 if (con_is_visible(vc)) {
3558 old_was_color = vc->vc_can_do_color;
3559 vc->vc_sw->con_deinit(vc);
3560 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3561 visual_init(vc, i, 0);
3565 /* If the console changed between mono <-> color, then
3566 * the attributes in the screenbuf will be wrong. The
3567 * following resets all attributes to something sane.
3569 if (old_was_color != vc->vc_can_do_color)
3570 clear_buffer_attributes(vc);
3573 pr_info("Console: switching ");
3575 pr_cont("consoles %d-%d ", first + 1, last + 1);
3577 struct vc_data *vc = vc_cons[j].d;
3579 pr_cont("to %s %s %dx%d\n",
3580 vc->vc_can_do_color ? "colour" : "mono",
3581 desc, vc->vc_cols, vc->vc_rows);
3588 pr_cont("to %s\n", desc);
3598 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3599 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3601 struct module *owner = csw->owner;
3602 const struct consw *defcsw = NULL;
3603 struct con_driver *con_driver = NULL, *con_back = NULL;
3604 int i, retval = -ENODEV;
3606 if (!try_module_get(owner))
3609 WARN_CONSOLE_UNLOCKED();
3611 /* check if driver is registered and if it is unbindable */
3612 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3613 con_driver = ®istered_con_driver[i];
3615 if (con_driver->con == csw &&
3616 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3627 /* check if backup driver exists */
3628 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3629 con_back = ®istered_con_driver[i];
3631 if (con_back->con && con_back->con != csw) {
3632 defcsw = con_back->con;
3641 if (!con_is_bound(csw))
3644 first = max(first, con_driver->first);
3645 last = min(last, con_driver->last);
3647 for (i = first; i <= last; i++) {
3648 if (con_driver_map[i] == csw) {
3649 module_put(csw->owner);
3650 con_driver_map[i] = NULL;
3654 if (!con_is_bound(defcsw)) {
3655 const struct consw *defconsw = conswitchp;
3657 defcsw->con_startup();
3658 con_back->flag |= CON_DRIVER_FLAG_INIT;
3660 * vgacon may change the default driver to point
3661 * to dummycon, we restore it here...
3663 conswitchp = defconsw;
3666 if (!con_is_bound(csw))
3667 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3669 /* ignore return value, binding should not fail */
3670 do_bind_con_driver(defcsw, first, last, deflt);
3676 EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3678 static int vt_bind(struct con_driver *con)
3680 const struct consw *defcsw = NULL, *csw = NULL;
3681 int i, more = 1, first = -1, last = -1, deflt = 0;
3683 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3688 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3689 struct con_driver *con = ®istered_con_driver[i];
3691 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3703 for (i = con->first; i <= con->last; i++) {
3704 if (con_driver_map[i] == defcsw) {
3709 } else if (first != -1)
3713 if (first == 0 && last == MAX_NR_CONSOLES -1)
3717 do_bind_con_driver(csw, first, last, deflt);
3728 static int vt_unbind(struct con_driver *con)
3730 const struct consw *csw = NULL;
3731 int i, more = 1, first = -1, last = -1, deflt = 0;
3734 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3742 for (i = con->first; i <= con->last; i++) {
3743 if (con_driver_map[i] == csw) {
3748 } else if (first != -1)
3752 if (first == 0 && last == MAX_NR_CONSOLES -1)
3756 ret = do_unbind_con_driver(csw, first, last, deflt);
3770 static inline int vt_bind(struct con_driver *con)
3774 static inline int vt_unbind(struct con_driver *con)
3778 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3780 static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3781 const char *buf, size_t count)
3783 struct con_driver *con = dev_get_drvdata(dev);
3784 int bind = simple_strtoul(buf, NULL, 0);
3798 static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3801 struct con_driver *con = dev_get_drvdata(dev);
3805 bind = con_is_bound(con->con);
3808 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3811 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3814 struct con_driver *con = dev_get_drvdata(dev);
3816 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3817 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3822 static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3823 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3825 static struct attribute *con_dev_attrs[] = {
3826 &dev_attr_bind.attr,
3827 &dev_attr_name.attr,
3831 ATTRIBUTE_GROUPS(con_dev);
3833 static int vtconsole_init_device(struct con_driver *con)
3835 con->flag |= CON_DRIVER_FLAG_ATTR;
3839 static void vtconsole_deinit_device(struct con_driver *con)
3841 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3845 * con_is_bound - checks if driver is bound to the console
3846 * @csw: console driver
3848 * RETURNS: zero if unbound, nonzero if bound
3850 * Drivers can call this and if zero, they should release
3851 * all resources allocated on con_startup()
3853 int con_is_bound(const struct consw *csw)
3857 WARN_CONSOLE_UNLOCKED();
3859 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3860 if (con_driver_map[i] == csw) {
3868 EXPORT_SYMBOL(con_is_bound);
3871 * con_is_visible - checks whether the current console is visible
3872 * @vc: virtual console
3874 * RETURNS: zero if not visible, nonzero if visible
3876 bool con_is_visible(const struct vc_data *vc)
3878 WARN_CONSOLE_UNLOCKED();
3880 return *vc->vc_display_fg == vc;
3882 EXPORT_SYMBOL(con_is_visible);
3885 * con_debug_enter - prepare the console for the kernel debugger
3886 * @sw: console driver
3888 * Called when the console is taken over by the kernel debugger, this
3889 * function needs to save the current console state, then put the console
3890 * into a state suitable for the kernel debugger.
3893 * Zero on success, nonzero if a failure occurred when trying to prepare
3894 * the console for the debugger.
3896 int con_debug_enter(struct vc_data *vc)
3900 saved_fg_console = fg_console;
3901 saved_last_console = last_console;
3902 saved_want_console = want_console;
3903 saved_vc_mode = vc->vc_mode;
3904 saved_console_blanked = console_blanked;
3905 vc->vc_mode = KD_TEXT;
3906 console_blanked = 0;
3907 if (vc->vc_sw->con_debug_enter)
3908 ret = vc->vc_sw->con_debug_enter(vc);
3909 #ifdef CONFIG_KGDB_KDB
3910 /* Set the initial LINES variable if it is not already set */
3911 if (vc->vc_rows < 999) {
3914 const char *setargs[3] = {
3919 if (kdbgetintenv(setargs[0], &linecount)) {
3920 snprintf(lns, 4, "%i", vc->vc_rows);
3921 kdb_set(2, setargs);
3924 if (vc->vc_cols < 999) {
3927 const char *setargs[3] = {
3932 if (kdbgetintenv(setargs[0], &colcount)) {
3933 snprintf(cols, 4, "%i", vc->vc_cols);
3934 kdb_set(2, setargs);
3937 #endif /* CONFIG_KGDB_KDB */
3940 EXPORT_SYMBOL_GPL(con_debug_enter);
3943 * con_debug_leave - restore console state
3944 * @sw: console driver
3946 * Restore the console state to what it was before the kernel debugger
3950 * Zero on success, nonzero if a failure occurred when trying to restore
3953 int con_debug_leave(void)
3958 fg_console = saved_fg_console;
3959 last_console = saved_last_console;
3960 want_console = saved_want_console;
3961 console_blanked = saved_console_blanked;
3962 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3964 vc = vc_cons[fg_console].d;
3965 if (vc->vc_sw->con_debug_leave)
3966 ret = vc->vc_sw->con_debug_leave(vc);
3969 EXPORT_SYMBOL_GPL(con_debug_leave);
3971 static int do_register_con_driver(const struct consw *csw, int first, int last)
3973 struct module *owner = csw->owner;
3974 struct con_driver *con_driver;
3978 WARN_CONSOLE_UNLOCKED();
3980 if (!try_module_get(owner))
3983 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3984 con_driver = ®istered_con_driver[i];
3986 /* already registered */
3987 if (con_driver->con == csw) {
3993 desc = csw->con_startup();
4001 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4002 con_driver = ®istered_con_driver[i];
4004 if (con_driver->con == NULL &&
4005 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
4006 con_driver->con = csw;
4007 con_driver->desc = desc;
4008 con_driver->node = i;
4009 con_driver->flag = CON_DRIVER_FLAG_MODULE |
4010 CON_DRIVER_FLAG_INIT;
4011 con_driver->first = first;
4012 con_driver->last = last;
4022 device_create_with_groups(vtconsole_class, NULL,
4023 MKDEV(0, con_driver->node),
4024 con_driver, con_dev_groups,
4025 "vtcon%i", con_driver->node);
4026 if (IS_ERR(con_driver->dev)) {
4027 pr_warn("Unable to create device for %s; errno = %ld\n",
4028 con_driver->desc, PTR_ERR(con_driver->dev));
4029 con_driver->dev = NULL;
4031 vtconsole_init_device(con_driver);
4041 * do_unregister_con_driver - unregister console driver from console layer
4042 * @csw: console driver
4044 * DESCRIPTION: All drivers that registers to the console layer must
4045 * call this function upon exit, or if the console driver is in a state
4046 * where it won't be able to handle console services, such as the
4047 * framebuffer console without loaded framebuffer drivers.
4049 * The driver must unbind first prior to unregistration.
4051 int do_unregister_con_driver(const struct consw *csw)
4055 /* cannot unregister a bound driver */
4056 if (con_is_bound(csw))
4059 if (csw == conswitchp)
4062 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4063 struct con_driver *con_driver = ®istered_con_driver[i];
4065 if (con_driver->con == csw) {
4067 * Defer the removal of the sysfs entries since that
4068 * will acquire the kernfs s_active lock and we can't
4069 * acquire this lock while holding the console lock:
4070 * the unbind sysfs entry imposes already the opposite
4071 * order. Reset con already here to prevent any later
4072 * lookup to succeed and mark this slot as zombie, so
4073 * it won't get reused until we complete the removal
4074 * in the deferred work.
4076 con_driver->con = NULL;
4077 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4078 schedule_work(&con_driver_unregister_work);
4086 EXPORT_SYMBOL_GPL(do_unregister_con_driver);
4088 static void con_driver_unregister_callback(struct work_struct *ignored)
4094 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4095 struct con_driver *con_driver = ®istered_con_driver[i];
4097 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4102 vtconsole_deinit_device(con_driver);
4103 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4107 if (WARN_ON_ONCE(con_driver->con))
4108 con_driver->con = NULL;
4109 con_driver->desc = NULL;
4110 con_driver->dev = NULL;
4111 con_driver->node = 0;
4112 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4113 con_driver->flag = 0;
4114 con_driver->first = 0;
4115 con_driver->last = 0;
4122 * If we support more console drivers, this function is used
4123 * when a driver wants to take over some existing consoles
4124 * and become default driver for newly opened ones.
4126 * do_take_over_console is basically a register followed by bind
4128 int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4132 err = do_register_con_driver(csw, first, last);
4134 * If we get an busy error we still want to bind the console driver
4135 * and return success, as we may have unbound the console driver
4136 * but not unregistered it.
4141 do_bind_con_driver(csw, first, last, deflt);
4145 EXPORT_SYMBOL_GPL(do_take_over_console);
4149 * give_up_console is a wrapper to unregister_con_driver. It will only
4150 * work if driver is fully unbound.
4152 void give_up_console(const struct consw *csw)
4155 do_unregister_con_driver(csw);
4159 static int __init vtconsole_class_init(void)
4163 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4164 if (IS_ERR(vtconsole_class)) {
4165 pr_warn("Unable to create vt console class; errno = %ld\n",
4166 PTR_ERR(vtconsole_class));
4167 vtconsole_class = NULL;
4170 /* Add system drivers to sysfs */
4171 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4172 struct con_driver *con = ®istered_con_driver[i];
4174 if (con->con && !con->dev) {
4176 device_create_with_groups(vtconsole_class, NULL,
4177 MKDEV(0, con->node),
4178 con, con_dev_groups,
4179 "vtcon%i", con->node);
4181 if (IS_ERR(con->dev)) {
4182 pr_warn("Unable to create device for %s; errno = %ld\n",
4183 con->desc, PTR_ERR(con->dev));
4186 vtconsole_init_device(con);
4193 postcore_initcall(vtconsole_class_init);
4201 static int set_vesa_blanking(char __user *p)
4205 if (get_user(mode, p + 1))
4208 vesa_blank_mode = (mode < 4) ? mode : 0;
4212 void do_blank_screen(int entering_gfx)
4214 struct vc_data *vc = vc_cons[fg_console].d;
4219 WARN_CONSOLE_UNLOCKED();
4221 if (console_blanked) {
4222 if (blank_state == blank_vesa_wait) {
4223 blank_state = blank_off;
4224 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
4229 /* entering graphics mode? */
4233 vc->vc_sw->con_blank(vc, -1, 1);
4234 console_blanked = fg_console + 1;
4235 blank_state = blank_off;
4240 blank_state = blank_off;
4242 /* don't blank graphics */
4243 if (vc->vc_mode != KD_TEXT) {
4244 console_blanked = fg_console + 1;
4249 del_timer_sync(&console_timer);
4250 blank_timer_expired = 0;
4253 /* In case we need to reset origin, blanking hook returns 1 */
4254 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
4255 console_blanked = fg_console + 1;
4259 if (console_blank_hook && console_blank_hook(1))
4262 if (vesa_off_interval && vesa_blank_mode) {
4263 blank_state = blank_vesa_wait;
4264 mod_timer(&console_timer, jiffies + vesa_off_interval);
4266 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
4268 EXPORT_SYMBOL(do_blank_screen);
4271 * Called by timer as well as from vt_console_driver
4273 void do_unblank_screen(int leaving_gfx)
4277 /* This should now always be called from a "sane" (read: can schedule)
4278 * context for the sake of the low level drivers, except in the special
4279 * case of oops_in_progress
4281 if (!oops_in_progress)
4284 WARN_CONSOLE_UNLOCKED();
4287 if (!console_blanked)
4289 if (!vc_cons_allocated(fg_console)) {
4291 pr_warn("unblank_screen: tty %d not allocated ??\n",
4295 vc = vc_cons[fg_console].d;
4296 if (vc->vc_mode != KD_TEXT)
4297 return; /* but leave console_blanked != 0 */
4299 if (blankinterval) {
4300 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
4301 blank_state = blank_normal_wait;
4304 console_blanked = 0;
4305 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
4306 /* Low-level driver cannot restore -> do it ourselves */
4308 if (console_blank_hook)
4309 console_blank_hook(0);
4312 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
4314 EXPORT_SYMBOL(do_unblank_screen);
4317 * This is called by the outside world to cause a forced unblank, mostly for
4318 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4319 * call it with 1 as an argument and so force a mode restore... that may kill
4320 * X or at least garbage the screen but would also make the Oops visible...
4322 void unblank_screen(void)
4324 do_unblank_screen(0);
4328 * We defer the timer blanking to work queue so it can take the console mutex
4329 * (console operations can still happen at irq time, but only from printk which
4330 * has the console mutex. Not perfect yet, but better than no locking
4332 static void blank_screen_t(struct timer_list *unused)
4334 blank_timer_expired = 1;
4335 schedule_work(&console_work);
4338 void poke_blanked_console(void)
4340 WARN_CONSOLE_UNLOCKED();
4342 /* Add this so we quickly catch whoever might call us in a non
4343 * safe context. Nowadays, unblank_screen() isn't to be called in
4344 * atomic contexts and is allowed to schedule (with the special case
4345 * of oops_in_progress, but that isn't of any concern for this
4350 /* This isn't perfectly race free, but a race here would be mostly harmless,
4351 * at worse, we'll do a spurrious blank and it's unlikely
4353 del_timer(&console_timer);
4354 blank_timer_expired = 0;
4356 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4358 if (console_blanked)
4360 else if (blankinterval) {
4361 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
4362 blank_state = blank_normal_wait;
4370 static void set_palette(struct vc_data *vc)
4372 WARN_CONSOLE_UNLOCKED();
4374 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
4375 vc->vc_sw->con_set_palette(vc, color_table);
4379 * Load palette into the DAC registers. arg points to a colour
4380 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4383 int con_set_cmap(unsigned char __user *arg)
4386 unsigned char colormap[3*16];
4388 if (copy_from_user(colormap, arg, sizeof(colormap)))
4392 for (i = k = 0; i < 16; i++) {
4393 default_red[i] = colormap[k++];
4394 default_grn[i] = colormap[k++];
4395 default_blu[i] = colormap[k++];
4397 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4398 if (!vc_cons_allocated(i))
4400 for (j = k = 0; j < 16; j++) {
4401 vc_cons[i].d->vc_palette[k++] = default_red[j];
4402 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4403 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4405 set_palette(vc_cons[i].d);
4412 int con_get_cmap(unsigned char __user *arg)
4415 unsigned char colormap[3*16];
4418 for (i = k = 0; i < 16; i++) {
4419 colormap[k++] = default_red[i];
4420 colormap[k++] = default_grn[i];
4421 colormap[k++] = default_blu[i];
4425 if (copy_to_user(arg, colormap, sizeof(colormap)))
4431 void reset_palette(struct vc_data *vc)
4434 for (j=k=0; j<16; j++) {
4435 vc->vc_palette[k++] = default_red[j];
4436 vc->vc_palette[k++] = default_grn[j];
4437 vc->vc_palette[k++] = default_blu[j];
4445 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4446 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4447 * depending on width) reserved for each character which is kinda wasty, but
4448 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
4449 * is up to the actual low-level console-driver convert data into its favorite
4450 * format (maybe we should add a `fontoffset' field to the `display'
4451 * structure so we won't have to convert the fontdata all the time.
4455 #define max_font_size 65536
4457 static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4459 struct console_font font;
4464 font.data = kmalloc(max_font_size, GFP_KERNEL);
4471 if (vc->vc_mode != KD_TEXT)
4473 else if (vc->vc_sw->con_font_get)
4474 rc = vc->vc_sw->con_font_get(vc, &font);
4482 c = (font.width+7)/8 * 32 * font.charcount;
4484 if (op->data && font.charcount > op->charcount)
4486 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4487 if (font.width > op->width || font.height > op->height)
4490 if (font.width != 8)
4492 else if ((op->height && font.height > op->height) ||
4499 op->height = font.height;
4500 op->width = font.width;
4501 op->charcount = font.charcount;
4503 if (op->data && copy_to_user(op->data, font.data, c))
4511 static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4513 struct console_font font;
4517 if (vc->vc_mode != KD_TEXT)
4521 if (op->charcount > 512)
4523 if (op->width <= 0 || op->width > 32 || op->height > 32)
4525 size = (op->width+7)/8 * 32 * op->charcount;
4526 if (size > max_font_size)
4529 font.data = memdup_user(op->data, size);
4530 if (IS_ERR(font.data))
4531 return PTR_ERR(font.data);
4533 if (!op->height) { /* Need to guess font height [compat] */
4535 u8 *charmap = font.data;
4538 * If from KDFONTOP ioctl, don't allow things which can be done
4539 * in userland,so that we can get rid of this soon
4541 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4546 for (h = 32; h > 0; h--)
4547 for (i = 0; i < op->charcount; i++)
4548 if (charmap[32*i+h-1])
4558 font.charcount = op->charcount;
4559 font.width = op->width;
4560 font.height = op->height;
4563 if (vc->vc_mode != KD_TEXT)
4565 else if (vc->vc_sw->con_font_set)
4566 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4574 static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4576 struct console_font font = {.width = op->width, .height = op->height};
4577 char name[MAX_FONT_NAME];
4584 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4587 name[MAX_FONT_NAME - 1] = 0;
4590 if (vc->vc_mode != KD_TEXT) {
4594 if (vc->vc_sw->con_font_default)
4595 rc = vc->vc_sw->con_font_default(vc, &font, s);
4600 op->width = font.width;
4601 op->height = font.height;
4606 static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4608 int con = op->height;
4613 if (vc->vc_mode != KD_TEXT)
4615 else if (!vc->vc_sw->con_font_copy)
4617 else if (con < 0 || !vc_cons_allocated(con))
4619 else if (con == vc->vc_num) /* nothing to do */
4622 rc = vc->vc_sw->con_font_copy(vc, con);
4627 int con_font_op(struct vc_data *vc, struct console_font_op *op)
4630 case KD_FONT_OP_SET:
4631 return con_font_set(vc, op);
4632 case KD_FONT_OP_GET:
4633 return con_font_get(vc, op);
4634 case KD_FONT_OP_SET_DEFAULT:
4635 return con_font_default(vc, op);
4636 case KD_FONT_OP_COPY:
4637 return con_font_copy(vc, op);
4643 * Interface exported to selection and vcs.
4646 /* used by selection */
4647 u16 screen_glyph(struct vc_data *vc, int offset)
4649 u16 w = scr_readw(screenpos(vc, offset, 1));
4652 if (w & vc->vc_hi_font_mask)
4656 EXPORT_SYMBOL_GPL(screen_glyph);
4658 u32 screen_glyph_unicode(struct vc_data *vc, int n)
4660 struct uni_screen *uniscr = get_vc_uniscr(vc);
4663 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4664 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4666 EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4668 /* used by vcs - note the word offset */
4669 unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4671 return screenpos(vc, 2 * w_offset, viewed);
4673 EXPORT_SYMBOL_GPL(screen_pos);
4675 void getconsxy(struct vc_data *vc, unsigned char *p)
4677 /* clamp values if they don't fit */
4678 p[0] = min(vc->vc_x, 0xFFu);
4679 p[1] = min(vc->vc_y, 0xFFu);
4682 void putconsxy(struct vc_data *vc, unsigned char *p)
4685 gotoxy(vc, p[0], p[1]);
4689 u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4691 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4692 return softcursor_original;
4693 return scr_readw(org);
4696 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4698 scr_writew(val, org);
4699 if ((unsigned long)org == vc->vc_pos) {
4700 softcursor_original = -1;
4705 void vcs_scr_updated(struct vc_data *vc)
4710 void vc_scrolldelta_helper(struct vc_data *c, int lines,
4711 unsigned int rolled_over, void *base, unsigned int size)
4713 unsigned long ubase = (unsigned long)base;
4714 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4715 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4716 ptrdiff_t origin = (void *)c->vc_origin - base;
4717 int margin = c->vc_size_row * 4;
4718 int from, wrap, from_off, avail;
4720 /* Turn scrollback off */
4722 c->vc_visible_origin = c->vc_origin;
4726 /* Do we have already enough to allow jumping from 0 to the end? */
4727 if (rolled_over > scr_end + margin) {
4729 wrap = rolled_over + c->vc_size_row;
4735 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4736 avail = (origin - from + wrap) % wrap;
4738 /* Only a little piece would be left? Show all incl. the piece! */
4739 if (avail < 2 * margin)
4741 if (from_off < margin)
4743 if (from_off > avail - margin)
4746 c->vc_visible_origin = ubase + (from + from_off) % wrap;
4748 EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4751 * Visible symbols for modules
4754 EXPORT_SYMBOL(color_table);
4755 EXPORT_SYMBOL(default_red);
4756 EXPORT_SYMBOL(default_grn);
4757 EXPORT_SYMBOL(default_blu);
4758 EXPORT_SYMBOL(update_region);
4759 EXPORT_SYMBOL(redraw_screen);
4760 EXPORT_SYMBOL(vc_resize);
4761 EXPORT_SYMBOL(fg_console);
4762 EXPORT_SYMBOL(console_blank_hook);
4763 EXPORT_SYMBOL(console_blanked);
4764 EXPORT_SYMBOL(vc_cons);
4765 EXPORT_SYMBOL(global_cursor_default);
4766 #ifndef VT_SINGLE_DRIVER
4767 EXPORT_SYMBOL(give_up_console);