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_free(struct uni_screen *uniscr)
373 static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
375 vc_uniscr_free(vc->vc_uni_screen);
376 vc->vc_uni_screen = new_uniscr;
379 static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
381 struct uni_screen *uniscr = get_vc_uniscr(vc);
384 uniscr->lines[vc->state.y][vc->state.x] = uc;
387 static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
389 struct uni_screen *uniscr = get_vc_uniscr(vc);
392 char32_t *ln = uniscr->lines[vc->state.y];
393 unsigned int x = vc->state.x, cols = vc->vc_cols;
395 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
396 memset32(&ln[x], ' ', nr);
400 static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
402 struct uni_screen *uniscr = get_vc_uniscr(vc);
405 char32_t *ln = uniscr->lines[vc->state.y];
406 unsigned int x = vc->state.x, cols = vc->vc_cols;
408 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
409 memset32(&ln[cols - nr], ' ', nr);
413 static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
416 struct uni_screen *uniscr = get_vc_uniscr(vc);
419 char32_t *ln = uniscr->lines[vc->state.y];
421 memset32(&ln[x], ' ', nr);
425 static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
428 struct uni_screen *uniscr = get_vc_uniscr(vc);
431 unsigned int cols = vc->vc_cols;
434 memset32(uniscr->lines[y++], ' ', cols);
438 static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
439 enum con_scroll dir, unsigned int nr)
441 struct uni_screen *uniscr = get_vc_uniscr(vc);
444 unsigned int i, j, k, sz, d, clear;
449 if (dir == SM_DOWN) {
453 for (i = 0; i < gcd(d, sz); i++) {
454 char32_t *tmp = uniscr->lines[t + i];
462 uniscr->lines[t + j] = uniscr->lines[t + k];
465 uniscr->lines[t + j] = tmp;
467 vc_uniscr_clear_lines(vc, clear, nr);
471 static void vc_uniscr_copy_area(struct uni_screen *dst,
472 unsigned int dst_cols,
473 unsigned int dst_rows,
474 struct uni_screen *src,
475 unsigned int src_cols,
476 unsigned int src_top_row,
477 unsigned int src_bot_row)
479 unsigned int dst_row = 0;
484 while (src_top_row < src_bot_row) {
485 char32_t *src_line = src->lines[src_top_row];
486 char32_t *dst_line = dst->lines[dst_row];
488 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
489 if (dst_cols - src_cols)
490 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
494 while (dst_row < dst_rows) {
495 char32_t *dst_line = dst->lines[dst_row];
497 memset32(dst_line, ' ', dst_cols);
503 * Called from vcs_read() to make sure unicode screen retrieval is possible.
504 * This will initialize the unicode screen buffer if not already done.
505 * This returns 0 if OK, or a negative error code otherwise.
506 * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
508 int vc_uniscr_check(struct vc_data *vc)
510 struct uni_screen *uniscr;
514 if (__is_defined(NO_VC_UNI_SCREEN))
517 WARN_CONSOLE_UNLOCKED();
522 if (vc->vc_uni_screen)
525 uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
530 * Let's populate it initially with (imperfect) reverse translation.
531 * This is the next best thing we can do short of having it enabled
532 * from the start even when no users rely on this functionality. True
533 * unicode content will be available after a complete screen refresh.
535 p = (unsigned short *)vc->vc_origin;
536 mask = vc->vc_hi_font_mask | 0xff;
537 for (y = 0; y < vc->vc_rows; y++) {
538 char32_t *line = uniscr->lines[y];
539 for (x = 0; x < vc->vc_cols; x++) {
540 u16 glyph = scr_readw(p++) & mask;
541 line[x] = inverse_translate(vc, glyph, true);
545 vc->vc_uni_screen = uniscr;
550 * Called from vcs_read() to get the unicode data from the screen.
551 * This must be preceded by a successful call to vc_uniscr_check() once
552 * the console lock has been taken.
554 void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
555 unsigned int row, unsigned int col, unsigned int nr)
557 struct uni_screen *uniscr = get_vc_uniscr(vc);
558 int offset = row * vc->vc_size_row + col * 2;
563 pos = (unsigned long)screenpos(vc, offset, viewed);
564 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
566 * Desired position falls in the main screen buffer.
567 * However the actual row/col might be different if
568 * scrollback is active.
570 row = (pos - vc->vc_origin) / vc->vc_size_row;
571 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
572 memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
575 * Scrollback is active. For now let's simply backtranslate
576 * the screen glyphs until the unicode screen buffer does
577 * synchronize with console display drivers for a scrollback
581 int mask = vc->vc_hi_font_mask | 0xff;
582 char32_t *uni_buf = dest;
584 u16 glyph = scr_readw(p++) & mask;
585 *uni_buf++ = inverse_translate(vc, glyph, true);
590 /* this is for validation and debugging only */
591 static void vc_uniscr_debug_check(struct vc_data *vc)
593 struct uni_screen *uniscr = get_vc_uniscr(vc);
597 if (!VC_UNI_SCREEN_DEBUG || !uniscr)
600 WARN_CONSOLE_UNLOCKED();
603 * Make sure our unicode screen translates into the same glyphs
604 * as the actual screen. This is brutal indeed.
606 p = (unsigned short *)vc->vc_origin;
607 mask = vc->vc_hi_font_mask | 0xff;
608 for (y = 0; y < vc->vc_rows; y++) {
609 char32_t *line = uniscr->lines[y];
610 for (x = 0; x < vc->vc_cols; x++) {
611 u16 glyph = scr_readw(p++) & mask;
612 char32_t uc = line[x];
613 int tc = conv_uni_to_pc(vc, uc);
615 tc = conv_uni_to_pc(vc, 0xfffd);
617 tc = conv_uni_to_pc(vc, '?');
620 "%s: mismatch at %d,%d: glyph=%#x tc=%#x\n",
621 __func__, x, y, glyph, tc);
627 static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
628 enum con_scroll dir, unsigned int nr)
634 if (b > vc->vc_rows || t >= b || nr < 1)
636 vc_uniscr_scroll(vc, t, b, dir, nr);
637 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
640 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
641 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
644 clear = s + (b - t - nr) * vc->vc_cols;
647 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
648 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
651 static void do_update_region(struct vc_data *vc, unsigned long start, int count)
653 unsigned int xx, yy, offset;
657 if (!vc->vc_sw->con_getxy) {
658 offset = (start - vc->vc_origin) / 2;
659 xx = offset % vc->vc_cols;
660 yy = offset / vc->vc_cols;
663 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
667 u16 attrib = scr_readw(p) & 0xff00;
670 while (xx < vc->vc_cols && count) {
671 if (attrib != (scr_readw(p) & 0xff00)) {
673 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
676 attrib = scr_readw(p) & 0xff00;
683 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
688 if (vc->vc_sw->con_getxy) {
690 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
695 void update_region(struct vc_data *vc, unsigned long start, int count)
697 WARN_CONSOLE_UNLOCKED();
699 if (con_should_update(vc)) {
701 do_update_region(vc, start, count);
706 /* Structure of attributes is hardware-dependent */
708 static u8 build_attr(struct vc_data *vc, u8 _color,
709 enum vc_intensity _intensity, u8 _blink, u8 _underline,
710 u8 _reverse, u8 _italic)
712 if (vc->vc_sw->con_build_attr)
713 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
714 _blink, _underline, _reverse, _italic);
717 * ++roman: I completely changed the attribute format for monochrome
718 * mode (!can_do_color). The formerly used MDA (monochrome display
719 * adapter) format didn't allow the combination of certain effects.
720 * Now the attribute is just a bit vector:
721 * Bit 0..1: intensity (0..2)
728 if (!vc->vc_can_do_color)
731 (_underline ? 4 : 0) |
735 a = (a & 0xF0) | vc->vc_itcolor;
737 a = (a & 0xf0) | vc->vc_ulcolor;
738 else if (_intensity == VCI_HALF_BRIGHT)
739 a = (a & 0xf0) | vc->vc_halfcolor;
741 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
744 if (_intensity == VCI_BOLD)
746 if (vc->vc_hi_font_mask == 0x100)
752 static void update_attr(struct vc_data *vc)
754 vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity,
755 vc->state.blink, vc->state.underline,
756 vc->state.reverse ^ vc->vc_decscnm, vc->state.italic);
757 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color,
758 VCI_NORMAL, vc->state.blink, 0, vc->vc_decscnm,
762 /* Note: inverting the screen twice should revert to the original state */
763 void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
767 WARN_CONSOLE_UNLOCKED();
770 p = screenpos(vc, offset, viewed);
771 if (vc->vc_sw->con_invert_region) {
772 vc->vc_sw->con_invert_region(vc, p, count);
778 if (!vc->vc_can_do_color) {
785 } else if (vc->vc_hi_font_mask == 0x100) {
788 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
795 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
802 if (con_should_update(vc))
803 do_update_region(vc, (unsigned long) p, count);
807 /* used by selection: complement pointer position */
808 void complement_pos(struct vc_data *vc, int offset)
810 static int old_offset = -1;
811 static unsigned short old;
812 static unsigned short oldx, oldy;
814 WARN_CONSOLE_UNLOCKED();
816 if (old_offset != -1 && old_offset >= 0 &&
817 old_offset < vc->vc_screenbuf_size) {
818 scr_writew(old, screenpos(vc, old_offset, 1));
819 if (con_should_update(vc))
820 vc->vc_sw->con_putc(vc, old, oldy, oldx);
826 if (offset != -1 && offset >= 0 &&
827 offset < vc->vc_screenbuf_size) {
830 p = screenpos(vc, offset, 1);
832 new = old ^ vc->vc_complement_mask;
834 if (con_should_update(vc)) {
835 oldx = (offset >> 1) % vc->vc_cols;
836 oldy = (offset >> 1) / vc->vc_cols;
837 vc->vc_sw->con_putc(vc, new, oldy, oldx);
843 static void insert_char(struct vc_data *vc, unsigned int nr)
845 unsigned short *p = (unsigned short *) vc->vc_pos;
847 vc_uniscr_insert(vc, nr);
848 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2);
849 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
850 vc->vc_need_wrap = 0;
851 if (con_should_update(vc))
852 do_update_region(vc, (unsigned long) p,
853 vc->vc_cols - vc->state.x);
856 static void delete_char(struct vc_data *vc, unsigned int nr)
858 unsigned short *p = (unsigned short *) vc->vc_pos;
860 vc_uniscr_delete(vc, nr);
861 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
862 scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
864 vc->vc_need_wrap = 0;
865 if (con_should_update(vc))
866 do_update_region(vc, (unsigned long) p,
867 vc->vc_cols - vc->state.x);
870 static int softcursor_original = -1;
872 static void add_softcursor(struct vc_data *vc)
874 int i = scr_readw((u16 *) vc->vc_pos);
875 u32 type = vc->vc_cursor_type;
877 if (! (type & 0x10)) return;
878 if (softcursor_original != -1) return;
879 softcursor_original = i;
880 i |= ((type >> 8) & 0xff00 );
881 i ^= ((type) & 0xff00 );
882 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
883 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
884 scr_writew(i, (u16 *) vc->vc_pos);
885 if (con_should_update(vc))
886 vc->vc_sw->con_putc(vc, i, vc->state.y, vc->state.x);
889 static void hide_softcursor(struct vc_data *vc)
891 if (softcursor_original != -1) {
892 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
893 if (con_should_update(vc))
894 vc->vc_sw->con_putc(vc, softcursor_original,
895 vc->state.y, vc->state.x);
896 softcursor_original = -1;
900 static void hide_cursor(struct vc_data *vc)
905 vc->vc_sw->con_cursor(vc, CM_ERASE);
909 static void set_cursor(struct vc_data *vc)
911 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
917 if ((vc->vc_cursor_type & 0x0f) != 1)
918 vc->vc_sw->con_cursor(vc, CM_DRAW);
923 static void set_origin(struct vc_data *vc)
925 WARN_CONSOLE_UNLOCKED();
927 if (!con_is_visible(vc) ||
928 !vc->vc_sw->con_set_origin ||
929 !vc->vc_sw->con_set_origin(vc))
930 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
931 vc->vc_visible_origin = vc->vc_origin;
932 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
933 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y +
937 static void save_screen(struct vc_data *vc)
939 WARN_CONSOLE_UNLOCKED();
941 if (vc->vc_sw->con_save_screen)
942 vc->vc_sw->con_save_screen(vc);
945 static void flush_scrollback(struct vc_data *vc)
947 WARN_CONSOLE_UNLOCKED();
950 if (vc->vc_sw->con_flush_scrollback) {
951 vc->vc_sw->con_flush_scrollback(vc);
952 } else if (con_is_visible(vc)) {
954 * When no con_flush_scrollback method is provided then the
955 * legacy way for flushing the scrollback buffer is to use
956 * a side effect of the con_switch method. We do it only on
957 * the foreground console as background consoles have no
958 * scrollback buffers in that case and we obviously don't
959 * want to switch to them.
962 vc->vc_sw->con_switch(vc);
968 * Redrawing of screen
971 void clear_buffer_attributes(struct vc_data *vc)
973 unsigned short *p = (unsigned short *)vc->vc_origin;
974 int count = vc->vc_screenbuf_size / 2;
975 int mask = vc->vc_hi_font_mask | 0xff;
977 for (; count > 0; count--, p++) {
978 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
982 void redraw_screen(struct vc_data *vc, int is_switch)
986 WARN_CONSOLE_UNLOCKED();
990 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
995 struct vc_data *old_vc = vc_cons[fg_console].d;
998 if (!con_is_visible(vc))
1000 *vc->vc_display_fg = vc;
1001 fg_console = vc->vc_num;
1002 hide_cursor(old_vc);
1003 if (!con_is_visible(old_vc)) {
1004 save_screen(old_vc);
1008 sysfs_notify(&tty0dev->kobj, NULL, "active");
1016 int old_was_color = vc->vc_can_do_color;
1019 update = vc->vc_sw->con_switch(vc);
1022 * If console changed from mono<->color, the best we can do
1023 * is to clear the buffer attributes. As it currently stands,
1024 * rebuilding new attributes from the old buffer is not doable
1025 * without overly complex code.
1027 if (old_was_color != vc->vc_can_do_color) {
1029 clear_buffer_attributes(vc);
1032 if (update && vc->vc_mode != KD_GRAPHICS)
1033 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
1038 compute_shiftstate();
1044 * Allocation, freeing and resizing of VTs.
1047 int vc_cons_allocated(unsigned int i)
1049 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
1052 static void visual_init(struct vc_data *vc, int num, int init)
1054 /* ++Geert: vc->vc_sw->con_init determines console size */
1056 module_put(vc->vc_sw->owner);
1057 vc->vc_sw = conswitchp;
1058 #ifndef VT_SINGLE_DRIVER
1059 if (con_driver_map[num])
1060 vc->vc_sw = con_driver_map[num];
1062 __module_get(vc->vc_sw->owner);
1064 vc->vc_display_fg = &master_display_fg;
1065 if (vc->vc_uni_pagedir_loc)
1066 con_free_unimap(vc);
1067 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
1068 vc->vc_uni_pagedir = NULL;
1069 vc->vc_hi_font_mask = 0;
1070 vc->vc_complement_mask = 0;
1071 vc->vc_can_do_color = 0;
1072 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1073 vc->vc_sw->con_init(vc, init);
1074 if (!vc->vc_complement_mask)
1075 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1076 vc->vc_s_complement_mask = vc->vc_complement_mask;
1077 vc->vc_size_row = vc->vc_cols << 1;
1078 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
1082 static void visual_deinit(struct vc_data *vc)
1084 vc->vc_sw->con_deinit(vc);
1085 module_put(vc->vc_sw->owner);
1088 static void vc_port_destruct(struct tty_port *port)
1090 struct vc_data *vc = container_of(port, struct vc_data, port);
1095 static const struct tty_port_operations vc_port_ops = {
1096 .destruct = vc_port_destruct,
1099 int vc_allocate(unsigned int currcons) /* return 0 on success */
1101 struct vt_notifier_param param;
1104 WARN_CONSOLE_UNLOCKED();
1106 if (currcons >= MAX_NR_CONSOLES)
1109 if (vc_cons[currcons].d)
1112 /* due to the granularity of kmalloc, we waste some memory here */
1113 /* the alloc is done in two steps, to optimize the common situation
1114 of a 25x80 console (structsize=216, screenbuf_size=4000) */
1115 /* although the numbers above are not valid since long ago, the
1116 point is still up-to-date and the comment still has its value
1117 even if only as a historical artifact. --mj, July 1998 */
1118 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
1122 vc_cons[currcons].d = vc;
1123 tty_port_init(&vc->port);
1124 vc->port.ops = &vc_port_ops;
1125 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1127 visual_init(vc, currcons, 1);
1129 if (!*vc->vc_uni_pagedir_loc)
1130 con_set_default_unimap(vc);
1132 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
1133 if (!vc->vc_screenbuf)
1136 /* If no drivers have overridden us and the user didn't pass a
1137 boot option, default to displaying the cursor */
1138 if (global_cursor_default == -1)
1139 global_cursor_default = 1;
1141 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
1142 vcs_make_sysfs(currcons);
1143 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, ¶m);
1149 vc_cons[currcons].d = NULL;
1153 static inline int resize_screen(struct vc_data *vc, int width, int height,
1156 /* Resizes the resolution of the display adapater */
1159 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
1160 err = vc->vc_sw->con_resize(vc, width, height, user);
1166 * Change # of rows and columns (0 means unchanged/the size of fg_console)
1167 * [this is to be used together with some user program
1168 * like resize that changes the hardware videomode]
1170 #define VC_RESIZE_MAXCOL (32767)
1171 #define VC_RESIZE_MAXROW (32767)
1174 * vc_do_resize - resizing method for the tty
1175 * @tty: tty being resized
1176 * @real_tty: real tty (different to tty if a pty/tty pair)
1177 * @vc: virtual console private data
1181 * Resize a virtual console, clipping according to the actual constraints.
1182 * If the caller passes a tty structure then update the termios winsize
1183 * information and perform any necessary signal handling.
1185 * Caller must hold the console semaphore. Takes the termios rwsem and
1186 * ctrl_lock of the tty IFF a tty is passed.
1189 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1190 unsigned int cols, unsigned int lines)
1192 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
1194 unsigned int old_rows, old_row_size, first_copied_row;
1195 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
1197 unsigned short *newscreen;
1198 struct uni_screen *new_uniscr = NULL;
1200 WARN_CONSOLE_UNLOCKED();
1205 user = vc->vc_resize_user;
1206 vc->vc_resize_user = 0;
1208 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1211 new_cols = (cols ? cols : vc->vc_cols);
1212 new_rows = (lines ? lines : vc->vc_rows);
1213 new_row_size = new_cols << 1;
1214 new_screen_size = new_row_size * new_rows;
1216 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1219 if (new_screen_size > KMALLOC_MAX_SIZE)
1221 newscreen = kzalloc(new_screen_size, GFP_USER);
1225 if (get_vc_uniscr(vc)) {
1226 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1236 old_rows = vc->vc_rows;
1237 old_row_size = vc->vc_size_row;
1239 err = resize_screen(vc, new_cols, new_rows, user);
1242 vc_uniscr_free(new_uniscr);
1246 vc->vc_rows = new_rows;
1247 vc->vc_cols = new_cols;
1248 vc->vc_size_row = new_row_size;
1249 vc->vc_screenbuf_size = new_screen_size;
1251 rlth = min(old_row_size, new_row_size);
1252 rrem = new_row_size - rlth;
1253 old_origin = vc->vc_origin;
1254 new_origin = (long) newscreen;
1255 new_scr_end = new_origin + new_screen_size;
1257 if (vc->state.y > new_rows) {
1258 if (old_rows - vc->state.y < new_rows) {
1260 * Cursor near the bottom, copy contents from the
1263 first_copied_row = (old_rows - new_rows);
1266 * Cursor is in no man's land, copy 1/2 screenful
1267 * from the top and bottom of cursor position
1269 first_copied_row = (vc->state.y - new_rows/2);
1271 old_origin += first_copied_row * old_row_size;
1273 first_copied_row = 0;
1274 end = old_origin + old_row_size * min(old_rows, new_rows);
1276 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1277 get_vc_uniscr(vc), rlth/2, first_copied_row,
1278 min(old_rows, new_rows));
1279 vc_uniscr_set(vc, new_uniscr);
1283 while (old_origin < end) {
1284 scr_memcpyw((unsigned short *) new_origin,
1285 (unsigned short *) old_origin, rlth);
1287 scr_memsetw((void *)(new_origin + rlth),
1288 vc->vc_video_erase_char, rrem);
1289 old_origin += old_row_size;
1290 new_origin += new_row_size;
1292 if (new_scr_end > new_origin)
1293 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1294 new_scr_end - new_origin);
1295 kfree(vc->vc_screenbuf);
1296 vc->vc_screenbuf = newscreen;
1297 vc->vc_screenbuf_size = new_screen_size;
1300 /* do part of a reset_terminal() */
1302 vc->vc_bottom = vc->vc_rows;
1303 gotoxy(vc, vc->state.x, vc->state.y);
1307 /* Rewrite the requested winsize data with the actual
1310 memset(&ws, 0, sizeof(ws));
1311 ws.ws_row = vc->vc_rows;
1312 ws.ws_col = vc->vc_cols;
1313 ws.ws_ypixel = vc->vc_scan_lines;
1314 tty_do_resize(tty, &ws);
1317 if (con_is_visible(vc))
1319 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
1325 * vc_resize - resize a VT
1326 * @vc: virtual console
1330 * Resize a virtual console as seen from the console end of things. We
1331 * use the common vc_do_resize methods to update the structures. The
1332 * caller must hold the console sem to protect console internals and
1336 int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
1338 return vc_do_resize(vc->port.tty, vc, cols, rows);
1342 * vt_resize - resize a VT
1343 * @tty: tty to resize
1344 * @ws: winsize attributes
1346 * Resize a virtual terminal. This is called by the tty layer as we
1347 * register our own handler for resizing. The mutual helper does all
1350 * Takes the console sem and the called methods then take the tty
1351 * termios_rwsem and the tty ctrl_lock in that order.
1353 static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1355 struct vc_data *vc = tty->driver_data;
1359 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1364 struct vc_data *vc_deallocate(unsigned int currcons)
1366 struct vc_data *vc = NULL;
1368 WARN_CONSOLE_UNLOCKED();
1370 if (vc_cons_allocated(currcons)) {
1371 struct vt_notifier_param param;
1373 param.vc = vc = vc_cons[currcons].d;
1374 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m);
1375 vcs_remove_sysfs(currcons);
1377 put_pid(vc->vt_pid);
1378 vc_uniscr_set(vc, NULL);
1379 kfree(vc->vc_screenbuf);
1380 vc_cons[currcons].d = NULL;
1389 enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
1391 #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1392 #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1393 #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
1395 #define decarm VC_REPEAT
1396 #define decckm VC_CKMODE
1397 #define kbdapplic VC_APPLIC
1401 * this is what the terminal answers to a ESC-Z or csi0c query.
1403 #define VT100ID "\033[?1;2c"
1404 #define VT102ID "\033[?6c"
1406 const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1407 8,12,10,14, 9,13,11,15 };
1409 /* the default colour table, for VGA+ colour systems */
1410 unsigned char default_red[] = {
1411 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1412 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1414 module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
1416 unsigned char default_grn[] = {
1417 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1418 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1420 module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1422 unsigned char default_blu[] = {
1423 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1424 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1426 module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
1429 * gotoxy() must verify all boundaries, because the arguments
1430 * might also be negative. If the given position is out of
1431 * bounds, the cursor is placed at the nearest margin.
1433 static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1440 if (new_x >= vc->vc_cols)
1441 vc->state.x = vc->vc_cols - 1;
1443 vc->state.x = new_x;
1448 max_y = vc->vc_bottom;
1451 max_y = vc->vc_rows;
1454 vc->state.y = min_y;
1455 else if (new_y >= max_y)
1456 vc->state.y = max_y - 1;
1458 vc->state.y = new_y;
1459 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row +
1461 vc->vc_need_wrap = 0;
1464 /* for absolute user moves, when decom is set */
1465 static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1467 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1470 void scrollback(struct vc_data *vc)
1472 scrolldelta(-(vc->vc_rows / 2));
1475 void scrollfront(struct vc_data *vc, int lines)
1478 lines = vc->vc_rows / 2;
1482 static void lf(struct vc_data *vc)
1484 /* don't scroll if above bottom of scrolling region, or
1485 * if below scrolling region
1487 if (vc->state.y + 1 == vc->vc_bottom)
1488 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
1489 else if (vc->state.y < vc->vc_rows - 1) {
1491 vc->vc_pos += vc->vc_size_row;
1493 vc->vc_need_wrap = 0;
1494 notify_write(vc, '\n');
1497 static void ri(struct vc_data *vc)
1499 /* don't scroll if below top of scrolling region, or
1500 * if above scrolling region
1502 if (vc->state.y == vc->vc_top)
1503 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
1504 else if (vc->state.y > 0) {
1506 vc->vc_pos -= vc->vc_size_row;
1508 vc->vc_need_wrap = 0;
1511 static inline void cr(struct vc_data *vc)
1513 vc->vc_pos -= vc->state.x << 1;
1514 vc->vc_need_wrap = vc->state.x = 0;
1515 notify_write(vc, '\r');
1518 static inline void bs(struct vc_data *vc)
1523 vc->vc_need_wrap = 0;
1524 notify_write(vc, '\b');
1528 static inline void del(struct vc_data *vc)
1533 static void csi_J(struct vc_data *vc, int vpar)
1536 unsigned short * start;
1539 case 0: /* erase from cursor to end of display */
1540 vc_uniscr_clear_line(vc, vc->state.x,
1541 vc->vc_cols - vc->state.x);
1542 vc_uniscr_clear_lines(vc, vc->state.y + 1,
1543 vc->vc_rows - vc->state.y - 1);
1544 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1545 start = (unsigned short *)vc->vc_pos;
1547 case 1: /* erase from start to cursor */
1548 vc_uniscr_clear_line(vc, 0, vc->state.x + 1);
1549 vc_uniscr_clear_lines(vc, 0, vc->state.y);
1550 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1551 start = (unsigned short *)vc->vc_origin;
1553 case 3: /* include scrollback */
1554 flush_scrollback(vc);
1556 case 2: /* erase whole display */
1557 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
1558 count = vc->vc_cols * vc->vc_rows;
1559 start = (unsigned short *)vc->vc_origin;
1564 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1565 if (con_should_update(vc))
1566 do_update_region(vc, (unsigned long) start, count);
1567 vc->vc_need_wrap = 0;
1570 static void csi_K(struct vc_data *vc, int vpar)
1573 unsigned short *start = (unsigned short *)vc->vc_pos;
1577 case 0: /* erase from cursor to end of line */
1579 count = vc->vc_cols - vc->state.x;
1581 case 1: /* erase from start of line to cursor */
1582 offset = -vc->state.x;
1583 count = vc->state.x + 1;
1585 case 2: /* erase whole line */
1586 offset = -vc->state.x;
1587 count = vc->vc_cols;
1592 vc_uniscr_clear_line(vc, vc->state.x + offset, count);
1593 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
1594 vc->vc_need_wrap = 0;
1595 if (con_should_update(vc))
1596 do_update_region(vc, (unsigned long)(start + offset), count);
1599 static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1605 count = (vpar > vc->vc_cols - vc->state.x) ? (vc->vc_cols - vc->state.x) : vpar;
1607 vc_uniscr_clear_line(vc, vc->state.x, count);
1608 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1609 if (con_should_update(vc))
1610 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, 1, count);
1611 vc->vc_need_wrap = 0;
1614 static void default_attr(struct vc_data *vc)
1616 vc->state.intensity = VCI_NORMAL;
1617 vc->state.italic = 0;
1618 vc->state.underline = 0;
1619 vc->state.reverse = 0;
1620 vc->state.blink = 0;
1621 vc->state.color = vc->vc_def_color;
1624 struct rgb { u8 r; u8 g; u8 b; };
1626 static void rgb_from_256(int i, struct rgb *c)
1628 if (i < 8) { /* Standard colours. */
1629 c->r = i&1 ? 0xaa : 0x00;
1630 c->g = i&2 ? 0xaa : 0x00;
1631 c->b = i&4 ? 0xaa : 0x00;
1632 } else if (i < 16) {
1633 c->r = i&1 ? 0xff : 0x55;
1634 c->g = i&2 ? 0xff : 0x55;
1635 c->b = i&4 ? 0xff : 0x55;
1636 } else if (i < 232) { /* 6x6x6 colour cube. */
1637 c->r = (i - 16) / 36 * 85 / 2;
1638 c->g = (i - 16) / 6 % 6 * 85 / 2;
1639 c->b = (i - 16) % 6 * 85 / 2;
1640 } else /* Grayscale ramp. */
1641 c->r = c->g = c->b = i * 10 - 2312;
1644 static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
1646 u8 hue = 0, max = max3(c->r, c->g, c->b);
1655 if (hue == 7 && max <= 0x55) {
1657 vc->state.intensity = VCI_BOLD;
1658 } else if (max > 0xaa)
1659 vc->state.intensity = VCI_BOLD;
1661 vc->state.intensity = VCI_NORMAL;
1663 vc->state.color = (vc->state.color & 0xf0) | hue;
1666 static void rgb_background(struct vc_data *vc, const struct rgb *c)
1668 /* For backgrounds, err on the dark side. */
1669 vc->state.color = (vc->state.color & 0x0f)
1670 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
1674 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1675 * and thus need to be detected and ignored by hand. That standard also
1676 * wants : rather than ; as separators but sequences containing : are currently
1677 * completely ignored by the parser.
1679 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1682 static int vc_t416_color(struct vc_data *vc, int i,
1683 void(*set_color)(struct vc_data *vc, const struct rgb *c))
1688 if (i > vc->vc_npar)
1691 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
1694 rgb_from_256(vc->vc_par[i], &c);
1695 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
1697 c.r = vc->vc_par[i + 1];
1698 c.g = vc->vc_par[i + 2];
1699 c.b = vc->vc_par[i + 3];
1709 /* console_lock is held */
1710 static void csi_m(struct vc_data *vc)
1714 for (i = 0; i <= vc->vc_npar; i++)
1715 switch (vc->vc_par[i]) {
1716 case 0: /* all attributes off */
1720 vc->state.intensity = VCI_BOLD;
1723 vc->state.intensity = VCI_HALF_BRIGHT;
1726 vc->state.italic = 1;
1730 * No console drivers support double underline, so
1731 * convert it to a single underline.
1734 vc->state.underline = 1;
1737 vc->state.blink = 1;
1740 vc->state.reverse = 1;
1742 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1743 * Select primary font, don't display control chars if
1744 * defined, don't set bit 8 on output.
1746 vc->vc_translate = set_translate(vc->state.charset == 0
1747 ? vc->state.G0_charset
1748 : vc->state.G1_charset, vc);
1749 vc->vc_disp_ctrl = 0;
1750 vc->vc_toggle_meta = 0;
1752 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1753 * Select first alternate font, lets chars < 32 be
1754 * displayed as ROM chars.
1756 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1757 vc->vc_disp_ctrl = 1;
1758 vc->vc_toggle_meta = 0;
1760 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1761 * Select second alternate font, toggle high bit
1762 * before displaying as ROM char.
1764 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1765 vc->vc_disp_ctrl = 1;
1766 vc->vc_toggle_meta = 1;
1769 vc->state.intensity = VCI_NORMAL;
1772 vc->state.italic = 0;
1775 vc->state.underline = 0;
1778 vc->state.blink = 0;
1781 vc->state.reverse = 0;
1784 i = vc_t416_color(vc, i, rgb_foreground);
1787 i = vc_t416_color(vc, i, rgb_background);
1790 vc->state.color = (vc->vc_def_color & 0x0f) |
1791 (vc->state.color & 0xf0);
1794 vc->state.color = (vc->vc_def_color & 0xf0) |
1795 (vc->state.color & 0x0f);
1798 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1799 if (vc->vc_par[i] < 100)
1800 vc->state.intensity = VCI_BOLD;
1801 vc->vc_par[i] -= 60;
1803 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1804 vc->state.color = color_table[vc->vc_par[i] - 30]
1805 | (vc->state.color & 0xf0);
1806 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1807 vc->state.color = (color_table[vc->vc_par[i] - 40] << 4)
1808 | (vc->state.color & 0x0f);
1814 static void respond_string(const char *p, struct tty_port *port)
1817 tty_insert_flip_char(port, *p, 0);
1820 tty_schedule_flip(port);
1823 static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1827 sprintf(buf, "\033[%d;%dR", vc->state.y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->state.x + 1);
1828 respond_string(buf, tty->port);
1831 static inline void status_report(struct tty_struct *tty)
1833 respond_string("\033[0n", tty->port); /* Terminal ok */
1836 static inline void respond_ID(struct tty_struct *tty)
1838 respond_string(VT102ID, tty->port);
1841 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1845 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1847 respond_string(buf, tty->port);
1850 /* invoked via ioctl(TIOCLINUX) and through set_selection_user */
1851 int mouse_reporting(void)
1853 return vc_cons[fg_console].d->vc_report_mouse;
1856 /* console_lock is held */
1857 static void set_mode(struct vc_data *vc, int on_off)
1861 for (i = 0; i <= vc->vc_npar; i++)
1862 if (vc->vc_priv == EPdec) {
1863 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1864 case 1: /* Cursor keys send ^[Ox/^[[x */
1866 set_kbd(vc, decckm);
1868 clr_kbd(vc, decckm);
1870 case 3: /* 80/132 mode switch unimplemented */
1872 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1873 /* this alone does not suffice; some user mode
1874 utility has to change the hardware regs */
1877 case 5: /* Inverted screen on/off */
1878 if (vc->vc_decscnm != on_off) {
1879 vc->vc_decscnm = on_off;
1880 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1884 case 6: /* Origin relative/absolute */
1885 vc->vc_decom = on_off;
1888 case 7: /* Autowrap on/off */
1889 vc->vc_decawm = on_off;
1891 case 8: /* Autorepeat on/off */
1893 set_kbd(vc, decarm);
1895 clr_kbd(vc, decarm);
1898 vc->vc_report_mouse = on_off ? 1 : 0;
1900 case 25: /* Cursor on/off */
1901 vc->vc_deccm = on_off;
1904 vc->vc_report_mouse = on_off ? 2 : 0;
1908 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1909 case 3: /* Monitor (display ctrls) */
1910 vc->vc_disp_ctrl = on_off;
1912 case 4: /* Insert Mode on/off */
1913 vc->vc_decim = on_off;
1915 case 20: /* Lf, Enter == CrLf/Lf */
1925 /* console_lock is held */
1926 static void setterm_command(struct vc_data *vc)
1928 switch (vc->vc_par[0]) {
1929 case 1: /* set color for underline mode */
1930 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1931 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1932 if (vc->state.underline)
1936 case 2: /* set color for half intensity mode */
1937 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1938 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1939 if (vc->state.intensity == VCI_HALF_BRIGHT)
1943 case 8: /* store colors as defaults */
1944 vc->vc_def_color = vc->vc_attr;
1945 if (vc->vc_hi_font_mask == 0x100)
1946 vc->vc_def_color >>= 1;
1950 case 9: /* set blanking interval */
1951 blankinterval = min(vc->vc_par[1], 60U) * 60;
1952 poke_blanked_console();
1954 case 10: /* set bell frequency in Hz */
1955 if (vc->vc_npar >= 1)
1956 vc->vc_bell_pitch = vc->vc_par[1];
1958 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1960 case 11: /* set bell duration in msec */
1961 if (vc->vc_npar >= 1)
1962 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1963 msecs_to_jiffies(vc->vc_par[1]) : 0;
1965 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1967 case 12: /* bring specified console to the front */
1968 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1969 set_console(vc->vc_par[1] - 1);
1971 case 13: /* unblank the screen */
1972 poke_blanked_console();
1974 case 14: /* set vesa powerdown interval */
1975 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1977 case 15: /* activate the previous console */
1978 set_console(last_console);
1980 case 16: /* set cursor blink duration in msec */
1981 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1982 vc->vc_par[1] <= USHRT_MAX)
1983 vc->vc_cur_blink_ms = vc->vc_par[1];
1985 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1990 /* console_lock is held */
1991 static void csi_at(struct vc_data *vc, unsigned int nr)
1993 if (nr > vc->vc_cols - vc->state.x)
1994 nr = vc->vc_cols - vc->state.x;
1997 insert_char(vc, nr);
2000 /* console_lock is held */
2001 static void csi_L(struct vc_data *vc, unsigned int nr)
2003 if (nr > vc->vc_rows - vc->state.y)
2004 nr = vc->vc_rows - vc->state.y;
2007 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
2008 vc->vc_need_wrap = 0;
2011 /* console_lock is held */
2012 static void csi_P(struct vc_data *vc, unsigned int nr)
2014 if (nr > vc->vc_cols - vc->state.x)
2015 nr = vc->vc_cols - vc->state.x;
2018 delete_char(vc, nr);
2021 /* console_lock is held */
2022 static void csi_M(struct vc_data *vc, unsigned int nr)
2024 if (nr > vc->vc_rows - vc->state.y)
2025 nr = vc->vc_rows - vc->state.y;
2028 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
2029 vc->vc_need_wrap = 0;
2032 /* console_lock is held (except via vc_init->reset_terminal */
2033 static void save_cur(struct vc_data *vc)
2035 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
2038 /* console_lock is held */
2039 static void restore_cur(struct vc_data *vc)
2041 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
2043 gotoxy(vc, vc->state.x, vc->state.y);
2044 vc->vc_translate = set_translate(vc->state.charset ? vc->state.G1_charset :
2045 vc->state.G0_charset, vc);
2047 vc->vc_need_wrap = 0;
2050 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
2051 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
2054 /* console_lock is held (except via vc_init()) */
2055 static void reset_terminal(struct vc_data *vc, int do_clear)
2058 vc->vc_bottom = vc->vc_rows;
2059 vc->vc_state = ESnormal;
2060 vc->vc_priv = EPecma;
2061 vc->vc_translate = set_translate(LAT1_MAP, vc);
2062 vc->state.G0_charset = LAT1_MAP;
2063 vc->state.G1_charset = GRAF_MAP;
2064 vc->state.charset = 0;
2065 vc->vc_need_wrap = 0;
2066 vc->vc_report_mouse = 0;
2067 vc->vc_utf = default_utf8;
2068 vc->vc_utf_count = 0;
2070 vc->vc_disp_ctrl = 0;
2071 vc->vc_toggle_meta = 0;
2076 vc->vc_deccm = global_cursor_default;
2079 vt_reset_keyboard(vc->vc_num);
2081 vc->vc_cursor_type = cur_default;
2082 vc->vc_complement_mask = vc->vc_s_complement_mask;
2087 vc->vc_tab_stop[0] =
2088 vc->vc_tab_stop[1] =
2089 vc->vc_tab_stop[2] =
2090 vc->vc_tab_stop[3] =
2091 vc->vc_tab_stop[4] =
2092 vc->vc_tab_stop[5] =
2093 vc->vc_tab_stop[6] =
2094 vc->vc_tab_stop[7] = 0x01010101;
2096 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2097 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
2098 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
2106 /* console_lock is held */
2107 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2110 * Control characters can be used in the _middle_
2111 * of an escape sequence.
2113 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2119 if (vc->vc_state == ESosc)
2120 vc->vc_state = ESnormal;
2121 else if (vc->vc_bell_duration)
2122 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2128 vc->vc_pos -= (vc->state.x << 1);
2129 while (vc->state.x < vc->vc_cols - 1) {
2131 if (vc->vc_tab_stop[7 & (vc->state.x >> 5)] & (1 << (vc->state.x & 31)))
2134 vc->vc_pos += (vc->state.x << 1);
2135 notify_write(vc, '\t');
2137 case 10: case 11: case 12:
2139 if (!is_kbd(vc, lnm))
2146 vc->state.charset = 1;
2147 vc->vc_translate = set_translate(vc->state.G1_charset, vc);
2148 vc->vc_disp_ctrl = 1;
2151 vc->state.charset = 0;
2152 vc->vc_translate = set_translate(vc->state.G0_charset, vc);
2153 vc->vc_disp_ctrl = 0;
2156 vc->vc_state = ESnormal;
2159 vc->vc_state = ESesc;
2165 vc->vc_state = ESsquare;
2168 switch(vc->vc_state) {
2170 vc->vc_state = ESnormal;
2173 vc->vc_state = ESsquare;
2176 vc->vc_state = ESnonstd;
2179 vc->vc_state = ESpercent;
2192 vc->vc_tab_stop[7 & (vc->state.x >> 5)] |= (1 << (vc->state.x & 31));
2204 vc->vc_state = ESsetG0;
2207 vc->vc_state = ESsetG1;
2210 vc->vc_state = EShash;
2213 reset_terminal(vc, 1);
2215 case '>': /* Numeric keypad */
2216 clr_kbd(vc, kbdapplic);
2218 case '=': /* Appl. keypad */
2219 set_kbd(vc, kbdapplic);
2224 if (c=='P') { /* palette escape sequence */
2225 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2226 vc->vc_par[vc->vc_npar] = 0;
2228 vc->vc_state = ESpalette;
2230 } else if (c=='R') { /* reset palette */
2232 vc->vc_state = ESnormal;
2233 } else if (c>='0' && c<='9')
2234 vc->vc_state = ESosc;
2236 vc->vc_state = ESnormal;
2240 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
2241 if (vc->vc_npar == 7) {
2242 int i = vc->vc_par[0] * 3, j = 1;
2243 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2244 vc->vc_palette[i++] += vc->vc_par[j++];
2245 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2246 vc->vc_palette[i++] += vc->vc_par[j++];
2247 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2248 vc->vc_palette[i] += vc->vc_par[j];
2250 vc->vc_state = ESnormal;
2253 vc->vc_state = ESnormal;
2256 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2257 vc->vc_par[vc->vc_npar] = 0;
2259 vc->vc_state = ESgetpars;
2260 if (c == '[') { /* Function key */
2261 vc->vc_state=ESfunckey;
2266 vc->vc_priv = EPdec;
2278 vc->vc_priv = EPecma;
2281 if (c == ';' && vc->vc_npar < NPAR - 1) {
2284 } else if (c>='0' && c<='9') {
2285 vc->vc_par[vc->vc_npar] *= 10;
2286 vc->vc_par[vc->vc_npar] += c - '0';
2289 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
2290 vc->vc_state = EScsiignore;
2293 vc->vc_state = ESnormal;
2296 if (vc->vc_priv <= EPdec)
2300 if (vc->vc_priv <= EPdec)
2304 if (vc->vc_priv == EPdec) {
2306 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2308 vc->vc_cursor_type = cur_default;
2313 if (vc->vc_priv == EPdec) {
2316 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2318 vc->vc_complement_mask = vc->vc_s_complement_mask;
2323 if (vc->vc_priv == EPecma) {
2324 if (vc->vc_par[0] == 5)
2326 else if (vc->vc_par[0] == 6)
2327 cursor_report(vc, tty);
2331 if (vc->vc_priv != EPecma) {
2332 vc->vc_priv = EPecma;
2339 gotoxy(vc, vc->vc_par[0], vc->state.y);
2344 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
2349 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
2354 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
2359 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
2364 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
2369 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
2374 gotoxay(vc, vc->state.x ,vc->vc_par[0]);
2381 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2384 csi_J(vc, vc->vc_par[0]);
2387 csi_K(vc, vc->vc_par[0]);
2390 csi_L(vc, vc->vc_par[0]);
2393 csi_M(vc, vc->vc_par[0]);
2396 csi_P(vc, vc->vc_par[0]);
2404 vc->vc_tab_stop[7 & (vc->state.x >> 5)] &= ~(1 << (vc->state.x & 31));
2405 else if (vc->vc_par[0] == 3) {
2406 vc->vc_tab_stop[0] =
2407 vc->vc_tab_stop[1] =
2408 vc->vc_tab_stop[2] =
2409 vc->vc_tab_stop[3] =
2410 vc->vc_tab_stop[4] =
2411 vc->vc_tab_stop[5] =
2412 vc->vc_tab_stop[6] =
2413 vc->vc_tab_stop[7] = 0;
2419 case 'q': /* DECLL - but only 3 leds */
2420 /* map 0,1,2,3 to 0,1,2,4 */
2421 if (vc->vc_par[0] < 4)
2422 vt_set_led_state(vc->vc_num,
2423 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2429 vc->vc_par[1] = vc->vc_rows;
2430 /* Minimum allowed region is 2 lines */
2431 if (vc->vc_par[0] < vc->vc_par[1] &&
2432 vc->vc_par[1] <= vc->vc_rows) {
2433 vc->vc_top = vc->vc_par[0] - 1;
2434 vc->vc_bottom = vc->vc_par[1];
2445 csi_X(vc, vc->vc_par[0]);
2448 csi_at(vc, vc->vc_par[0]);
2450 case ']': /* setterm functions */
2451 setterm_command(vc);
2456 if (c >= 20 && c <= 0x3f)
2458 vc->vc_state = ESnormal;
2461 vc->vc_state = ESnormal;
2463 case '@': /* defined in ISO 2022 */
2466 case 'G': /* prelim official escape code */
2467 case '8': /* retained for compatibility */
2473 vc->vc_state = ESnormal;
2476 vc->vc_state = ESnormal;
2478 /* DEC screen alignment test. kludge :-) */
2479 vc->vc_video_erase_char =
2480 (vc->vc_video_erase_char & 0xff00) | 'E';
2482 vc->vc_video_erase_char =
2483 (vc->vc_video_erase_char & 0xff00) | ' ';
2484 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2489 vc->state.G0_charset = GRAF_MAP;
2491 vc->state.G0_charset = LAT1_MAP;
2493 vc->state.G0_charset = IBMPC_MAP;
2495 vc->state.G0_charset = USER_MAP;
2496 if (vc->state.charset == 0)
2497 vc->vc_translate = set_translate(vc->state.G0_charset, vc);
2498 vc->vc_state = ESnormal;
2502 vc->state.G1_charset = GRAF_MAP;
2504 vc->state.G1_charset = LAT1_MAP;
2506 vc->state.G1_charset = IBMPC_MAP;
2508 vc->state.G1_charset = USER_MAP;
2509 if (vc->state.charset == 1)
2510 vc->vc_translate = set_translate(vc->state.G1_charset, vc);
2511 vc->vc_state = ESnormal;
2516 vc->vc_state = ESnormal;
2520 /* is_double_width() is based on the wcwidth() implementation by
2521 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2522 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2529 static int ucs_cmp(const void *key, const void *elt)
2531 uint32_t ucs = *(uint32_t *)key;
2532 struct interval e = *(struct interval *) elt;
2536 else if (ucs < e.first)
2541 static int is_double_width(uint32_t ucs)
2543 static const struct interval double_width[] = {
2544 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2545 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2546 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2547 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2549 if (ucs < double_width[0].first ||
2550 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2553 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2554 sizeof(struct interval), ucs_cmp) != NULL;
2557 static void con_flush(struct vc_data *vc, unsigned long draw_from,
2558 unsigned long draw_to, int *draw_x)
2563 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2564 (u16 *)draw_to - (u16 *)draw_from, vc->state.y, *draw_x);
2568 /* acquires console_lock */
2569 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2571 int c, next_c, tc, ok, n = 0, draw_x = -1;
2572 unsigned int currcons;
2573 unsigned long draw_from = 0, draw_to = 0;
2575 unsigned char vc_attr;
2576 struct vt_notifier_param param;
2580 u16 himask, charmask;
2586 vc = tty->driver_data;
2588 pr_err("vt: argh, driver_data is NULL !\n");
2593 currcons = vc->vc_num;
2594 if (!vc_cons_allocated(currcons)) {
2595 /* could this happen? */
2596 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2601 himask = vc->vc_hi_font_mask;
2602 charmask = himask ? 0x1ff : 0xff;
2604 /* undraw cursor first */
2610 while (!tty->stopped && count) {
2620 /* Do no translation at all in control states */
2621 if (vc->vc_state != ESnormal) {
2623 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2624 /* Combine UTF-8 into Unicode in vc_utf_char.
2625 * vc_utf_count is the number of continuation bytes still
2626 * expected to arrive.
2627 * vc_npar is the number of continuation bytes arrived so
2631 if ((c & 0xc0) == 0x80) {
2632 /* Continuation byte received */
2633 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2634 if (vc->vc_utf_count) {
2635 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2637 if (--vc->vc_utf_count) {
2638 /* Still need some bytes */
2641 /* Got a whole character */
2642 c = vc->vc_utf_char;
2643 /* Reject overlong sequences */
2644 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2645 c > utf8_length_changes[vc->vc_npar])
2648 /* Unexpected continuation byte */
2649 vc->vc_utf_count = 0;
2653 /* Single ASCII byte or first byte of a sequence received */
2654 if (vc->vc_utf_count) {
2655 /* Continuation byte expected */
2657 vc->vc_utf_count = 0;
2659 } else if (c > 0x7f) {
2660 /* First byte of a multibyte sequence received */
2662 if ((c & 0xe0) == 0xc0) {
2663 vc->vc_utf_count = 1;
2664 vc->vc_utf_char = (c & 0x1f);
2665 } else if ((c & 0xf0) == 0xe0) {
2666 vc->vc_utf_count = 2;
2667 vc->vc_utf_char = (c & 0x0f);
2668 } else if ((c & 0xf8) == 0xf0) {
2669 vc->vc_utf_count = 3;
2670 vc->vc_utf_char = (c & 0x07);
2671 } else if ((c & 0xfc) == 0xf8) {
2672 vc->vc_utf_count = 4;
2673 vc->vc_utf_char = (c & 0x03);
2674 } else if ((c & 0xfe) == 0xfc) {
2675 vc->vc_utf_count = 5;
2676 vc->vc_utf_char = (c & 0x01);
2678 /* 254 and 255 are invalid */
2681 if (vc->vc_utf_count) {
2682 /* Still need some bytes */
2686 /* Nothing to do if an ASCII byte was received */
2688 /* End of UTF-8 decoding. */
2689 /* c is the received character, or U+FFFD for invalid sequences. */
2690 /* Replace invalid Unicode code points with U+FFFD too */
2691 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2694 } else { /* no utf or alternate charset mode */
2695 tc = vc_translate(vc, c);
2699 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2700 ¶m) == NOTIFY_STOP)
2703 /* If the original code was a control character we
2704 * only allow a glyph to be displayed if the code is
2705 * not normally used (such as for cursor movement) or
2706 * if the disp_ctrl mode has been explicitly enabled.
2707 * Certain characters (as given by the CTRL_ALWAYS
2708 * bitmap) are always displayed as control characters,
2709 * as the console would be pretty useless without
2710 * them; to display an arbitrary font position use the
2711 * direct-to-font zone in UTF-8 mode.
2713 ok = tc && (c >= 32 ||
2714 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2715 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2716 && (c != 127 || vc->vc_disp_ctrl)
2719 if (vc->vc_state == ESnormal && ok) {
2720 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2721 if (is_double_width(c))
2724 /* Now try to find out how to display it */
2725 tc = conv_uni_to_pc(vc, tc);
2726 if (tc & ~charmask) {
2727 if (tc == -1 || tc == -2) {
2728 continue; /* nothing to display */
2730 /* Glyph not found */
2731 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2732 /* In legacy mode use the glyph we get by a 1:1 mapping.
2733 This would make absolutely no sense with Unicode in mind,
2734 but do this for ASCII characters since a font may lack
2735 Unicode mapping info and we don't want to end up with
2736 having question marks only. */
2739 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2740 tc = conv_uni_to_pc(vc, 0xfffd);
2743 tc = conv_uni_to_pc(vc, '?');
2744 if (tc < 0) tc = '?';
2750 vc_attr = vc->vc_attr;
2752 /* invert vc_attr */
2753 if (!vc->vc_can_do_color) {
2754 vc_attr = (vc->vc_attr) ^ 0x08;
2755 } else if (vc->vc_hi_font_mask == 0x100) {
2756 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2758 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2760 con_flush(vc, draw_from, draw_to, &draw_x);
2765 if (vc->vc_need_wrap || vc->vc_decim)
2766 con_flush(vc, draw_from, draw_to,
2768 if (vc->vc_need_wrap) {
2774 vc_uniscr_putc(vc, next_c);
2776 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2777 (vc_attr << 8) + tc,
2778 (u16 *) vc->vc_pos);
2779 if (con_should_update(vc) && draw_x < 0) {
2780 draw_x = vc->state.x;
2781 draw_from = vc->vc_pos;
2783 if (vc->state.x == vc->vc_cols - 1) {
2784 vc->vc_need_wrap = vc->vc_decawm;
2785 draw_to = vc->vc_pos + 2;
2788 draw_to = (vc->vc_pos += 2);
2791 if (!--width) break;
2793 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2794 if (tc < 0) tc = ' ';
2797 notify_write(vc, c);
2800 con_flush(vc, draw_from, draw_to, &draw_x);
2807 goto rescan_last_byte;
2811 con_flush(vc, draw_from, draw_to, &draw_x);
2812 do_con_trol(tty, vc, orig);
2814 con_flush(vc, draw_from, draw_to, &draw_x);
2815 vc_uniscr_debug_check(vc);
2816 console_conditional_schedule();
2823 * This is the console switching callback.
2825 * Doing console switching in a process context allows
2826 * us to do the switches asynchronously (needed when we want
2827 * to switch due to a keyboard interrupt). Synchronization
2828 * with other console code and prevention of re-entrancy is
2829 * ensured with console_lock.
2831 static void console_callback(struct work_struct *ignored)
2835 if (want_console >= 0) {
2836 if (want_console != fg_console &&
2837 vc_cons_allocated(want_console)) {
2838 hide_cursor(vc_cons[fg_console].d);
2839 change_console(vc_cons[want_console].d);
2840 /* we only changed when the console had already
2841 been allocated - a new console is not created
2842 in an interrupt routine */
2846 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2847 do_poke_blanked_console = 0;
2848 poke_blanked_console();
2850 if (scrollback_delta) {
2851 struct vc_data *vc = vc_cons[fg_console].d;
2853 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
2854 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2855 scrollback_delta = 0;
2857 if (blank_timer_expired) {
2859 blank_timer_expired = 0;
2861 notify_update(vc_cons[fg_console].d);
2866 int set_console(int nr)
2868 struct vc_data *vc = vc_cons[fg_console].d;
2870 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2871 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2874 * Console switch will fail in console_callback() or
2875 * change_console() so there is no point scheduling
2878 * Existing set_console() users don't check the return
2879 * value so this shouldn't break anything
2885 schedule_console_callback();
2890 struct tty_driver *console_driver;
2892 #ifdef CONFIG_VT_CONSOLE
2895 * vt_kmsg_redirect() - Sets/gets the kernel message console
2896 * @new: The new virtual terminal number or -1 if the console should stay
2899 * By default, the kernel messages are always printed on the current virtual
2900 * console. However, the user may modify that default with the
2901 * TIOCL_SETKMSGREDIRECT ioctl call.
2903 * This function sets the kernel message console to be @new. It returns the old
2904 * virtual console number. The virtual terminal number 0 (both as parameter and
2905 * return value) means no redirection (i.e. always printed on the currently
2908 * The parameter -1 means that only the current console is returned, but the
2909 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2910 * case to make the code more understandable.
2912 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2913 * the parameter and always returns 0.
2915 int vt_kmsg_redirect(int new)
2917 static int kmsg_con;
2920 return xchg(&kmsg_con, new);
2926 * Console on virtual terminal
2928 * The console must be locked when we get here.
2931 static void vt_console_print(struct console *co, const char *b, unsigned count)
2933 struct vc_data *vc = vc_cons[fg_console].d;
2935 static DEFINE_SPINLOCK(printing_lock);
2936 const ushort *start;
2937 ushort start_x, cnt;
2940 /* console busy or not yet initialized */
2943 if (!spin_trylock(&printing_lock))
2946 kmsg_console = vt_get_kmsg_redirect();
2947 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2948 vc = vc_cons[kmsg_console - 1].d;
2950 if (!vc_cons_allocated(fg_console)) {
2952 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2956 if (vc->vc_mode != KD_TEXT)
2959 /* undraw cursor first */
2963 start = (ushort *)vc->vc_pos;
2964 start_x = vc->state.x;
2968 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2969 if (cnt && con_is_visible(vc))
2970 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
2972 if (c == 8) { /* backspace */
2974 start = (ushort *)vc->vc_pos;
2975 start_x = vc->state.x;
2981 start = (ushort *)vc->vc_pos;
2982 start_x = vc->state.x;
2983 if (c == 10 || c == 13)
2986 vc_uniscr_putc(vc, c);
2987 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2988 notify_write(vc, c);
2990 if (vc->state.x == vc->vc_cols - 1) {
2991 vc->vc_need_wrap = 1;
2997 if (cnt && con_is_visible(vc))
2998 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
3003 spin_unlock(&printing_lock);
3006 static struct tty_driver *vt_console_device(struct console *c, int *index)
3008 *index = c->index ? c->index-1 : fg_console;
3009 return console_driver;
3012 static struct console vt_console_driver = {
3014 .write = vt_console_print,
3015 .device = vt_console_device,
3016 .unblank = unblank_screen,
3017 .flags = CON_PRINTBUFFER,
3023 * Handling of Linux-specific VC ioctls
3027 * Generally a bit racy with respect to console_lock();.
3029 * There are some functions which don't need it.
3031 * There are some functions which can sleep for arbitrary periods
3032 * (paste_selection) but we don't need the lock there anyway.
3034 * set_selection_user has locking, and definitely needs it
3037 int tioclinux(struct tty_struct *tty, unsigned long arg)
3040 char __user *p = (char __user *)arg;
3044 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3046 if (get_user(type, p))
3053 ret = set_selection_user((struct tiocl_selection
3054 __user *)(p+1), tty);
3056 case TIOCL_PASTESEL:
3057 ret = paste_selection(tty);
3059 case TIOCL_UNBLANKSCREEN:
3064 case TIOCL_SELLOADLUT:
3066 ret = sel_loadlut(p);
3069 case TIOCL_GETSHIFTSTATE:
3072 * Make it possible to react to Shift+Mousebutton.
3073 * Note that 'shift_state' is an undocumented
3074 * kernel-internal variable; programs not closely
3075 * related to the kernel should not use this.
3077 data = vt_get_shift_state();
3078 ret = put_user(data, p);
3080 case TIOCL_GETMOUSEREPORTING:
3081 console_lock(); /* May be overkill */
3082 data = mouse_reporting();
3084 ret = put_user(data, p);
3086 case TIOCL_SETVESABLANK:
3088 ret = set_vesa_blanking(p);
3091 case TIOCL_GETKMSGREDIRECT:
3092 data = vt_get_kmsg_redirect();
3093 ret = put_user(data, p);
3095 case TIOCL_SETKMSGREDIRECT:
3096 if (!capable(CAP_SYS_ADMIN)) {
3099 if (get_user(data, p+1))
3102 vt_kmsg_redirect(data);
3105 case TIOCL_GETFGCONSOLE:
3106 /* No locking needed as this is a transiently
3107 correct return anyway if the caller hasn't
3108 disabled switching */
3111 case TIOCL_SCROLLCONSOLE:
3112 if (get_user(lines, (s32 __user *)(p+4))) {
3115 /* Need the console lock here. Note that lots
3116 of other calls need fixing before the lock
3117 is actually useful ! */
3119 scrollfront(vc_cons[fg_console].d, lines);
3124 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
3130 case TIOCL_BLANKEDSCREEN:
3131 ret = console_blanked;
3141 * /dev/ttyN handling
3144 static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3148 retval = do_con_write(tty, buf, count);
3149 con_flush_chars(tty);
3154 static int con_put_char(struct tty_struct *tty, unsigned char ch)
3157 return 0; /* n_r3964 calls put_char() from interrupt context */
3158 return do_con_write(tty, &ch, 1);
3161 static int con_write_room(struct tty_struct *tty)
3165 return 32768; /* No limit, really; we're not buffering */
3168 static int con_chars_in_buffer(struct tty_struct *tty)
3170 return 0; /* we're not buffering */
3174 * con_throttle and con_unthrottle are only used for
3175 * paste_selection(), which has to stuff in a large number of
3178 static void con_throttle(struct tty_struct *tty)
3182 static void con_unthrottle(struct tty_struct *tty)
3184 struct vc_data *vc = tty->driver_data;
3186 wake_up_interruptible(&vc->paste_wait);
3190 * Turn the Scroll-Lock LED on when the tty is stopped
3192 static void con_stop(struct tty_struct *tty)
3197 console_num = tty->index;
3198 if (!vc_cons_allocated(console_num))
3200 vt_kbd_con_stop(console_num);
3204 * Turn the Scroll-Lock LED off when the console is started
3206 static void con_start(struct tty_struct *tty)
3211 console_num = tty->index;
3212 if (!vc_cons_allocated(console_num))
3214 vt_kbd_con_start(console_num);
3217 static void con_flush_chars(struct tty_struct *tty)
3221 if (in_interrupt()) /* from flush_to_ldisc */
3224 /* if we race with con_close(), vt may be null */
3226 vc = tty->driver_data;
3233 * Allocate the console screen memory.
3235 static int con_install(struct tty_driver *driver, struct tty_struct *tty)
3237 unsigned int currcons = tty->index;
3242 ret = vc_allocate(currcons);
3246 vc = vc_cons[currcons].d;
3248 /* Still being freed */
3254 ret = tty_port_install(&vc->port, driver, tty);
3258 tty->driver_data = vc;
3260 tty_port_get(&vc->port);
3262 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3263 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3264 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3267 tty->termios.c_iflag |= IUTF8;
3269 tty->termios.c_iflag &= ~IUTF8;
3275 static int con_open(struct tty_struct *tty, struct file *filp)
3277 /* everything done in install */
3282 static void con_close(struct tty_struct *tty, struct file *filp)
3284 /* Nothing to do - we defer to shutdown */
3287 static void con_shutdown(struct tty_struct *tty)
3289 struct vc_data *vc = tty->driver_data;
3292 vc->port.tty = NULL;
3296 static void con_cleanup(struct tty_struct *tty)
3298 struct vc_data *vc = tty->driver_data;
3300 tty_port_put(&vc->port);
3303 static int default_color = 7; /* white */
3304 static int default_italic_color = 2; // green (ASCII)
3305 static int default_underline_color = 3; // cyan (ASCII)
3306 module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
3307 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3308 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3310 static void vc_init(struct vc_data *vc, unsigned int rows,
3311 unsigned int cols, int do_clear)
3317 vc->vc_size_row = cols << 1;
3318 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3321 vc->vc_pos = vc->vc_origin;
3323 for (j=k=0; j<16; j++) {
3324 vc->vc_palette[k++] = default_red[j] ;
3325 vc->vc_palette[k++] = default_grn[j] ;
3326 vc->vc_palette[k++] = default_blu[j] ;
3328 vc->vc_def_color = default_color;
3329 vc->vc_ulcolor = default_underline_color;
3330 vc->vc_itcolor = default_italic_color;
3331 vc->vc_halfcolor = 0x08; /* grey */
3332 init_waitqueue_head(&vc->paste_wait);
3333 reset_terminal(vc, do_clear);
3337 * This routine initializes console interrupts, and does nothing
3338 * else. If you want the screen to clear, call tty_write with
3339 * the appropriate escape-sequence.
3342 static int __init con_init(void)
3344 const char *display_desc = NULL;
3346 unsigned int currcons = 0, i;
3351 conswitchp = &dummy_con;
3352 display_desc = conswitchp->con_startup();
3353 if (!display_desc) {
3359 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3360 struct con_driver *con_driver = ®istered_con_driver[i];
3362 if (con_driver->con == NULL) {
3363 con_driver->con = conswitchp;
3364 con_driver->desc = display_desc;
3365 con_driver->flag = CON_DRIVER_FLAG_INIT;
3366 con_driver->first = 0;
3367 con_driver->last = MAX_NR_CONSOLES - 1;
3372 for (i = 0; i < MAX_NR_CONSOLES; i++)
3373 con_driver_map[i] = conswitchp;
3375 if (blankinterval) {
3376 blank_state = blank_normal_wait;
3377 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3380 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
3381 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
3382 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
3383 tty_port_init(&vc->port);
3384 visual_init(vc, currcons, 1);
3385 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
3386 vc_init(vc, vc->vc_rows, vc->vc_cols,
3387 currcons || !vc->vc_sw->con_save_screen);
3389 currcons = fg_console = 0;
3390 master_display_fg = vc = vc_cons[currcons].d;
3393 gotoxy(vc, vc->state.x, vc->state.y);
3396 pr_info("Console: %s %s %dx%d\n",
3397 vc->vc_can_do_color ? "colour" : "mono",
3398 display_desc, vc->vc_cols, vc->vc_rows);
3403 #ifdef CONFIG_VT_CONSOLE
3404 register_console(&vt_console_driver);
3408 console_initcall(con_init);
3410 static const struct tty_operations con_ops = {
3411 .install = con_install,
3415 .write_room = con_write_room,
3416 .put_char = con_put_char,
3417 .flush_chars = con_flush_chars,
3418 .chars_in_buffer = con_chars_in_buffer,
3420 #ifdef CONFIG_COMPAT
3421 .compat_ioctl = vt_compat_ioctl,
3425 .throttle = con_throttle,
3426 .unthrottle = con_unthrottle,
3427 .resize = vt_resize,
3428 .shutdown = con_shutdown,
3429 .cleanup = con_cleanup,
3432 static struct cdev vc0_cdev;
3434 static ssize_t show_tty_active(struct device *dev,
3435 struct device_attribute *attr, char *buf)
3437 return sprintf(buf, "tty%d\n", fg_console + 1);
3439 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3441 static struct attribute *vt_dev_attrs[] = {
3442 &dev_attr_active.attr,
3446 ATTRIBUTE_GROUPS(vt_dev);
3448 int __init vty_init(const struct file_operations *console_fops)
3450 cdev_init(&vc0_cdev, console_fops);
3451 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3452 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3453 panic("Couldn't register /dev/tty0 driver\n");
3454 tty0dev = device_create_with_groups(tty_class, NULL,
3455 MKDEV(TTY_MAJOR, 0), NULL,
3456 vt_dev_groups, "tty0");
3457 if (IS_ERR(tty0dev))
3462 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3463 if (!console_driver)
3464 panic("Couldn't allocate console driver\n");
3466 console_driver->name = "tty";
3467 console_driver->name_base = 1;
3468 console_driver->major = TTY_MAJOR;
3469 console_driver->minor_start = 1;
3470 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3471 console_driver->init_termios = tty_std_termios;
3473 console_driver->init_termios.c_iflag |= IUTF8;
3474 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3475 tty_set_operations(console_driver, &con_ops);
3476 if (tty_register_driver(console_driver))
3477 panic("Couldn't register console driver\n");
3480 #ifdef CONFIG_MDA_CONSOLE
3486 #ifndef VT_SINGLE_DRIVER
3488 static struct class *vtconsole_class;
3490 static int do_bind_con_driver(const struct consw *csw, int first, int last,
3493 struct module *owner = csw->owner;
3494 const char *desc = NULL;
3495 struct con_driver *con_driver;
3496 int i, j = -1, k = -1, retval = -ENODEV;
3498 if (!try_module_get(owner))
3501 WARN_CONSOLE_UNLOCKED();
3503 /* check if driver is registered */
3504 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3505 con_driver = ®istered_con_driver[i];
3507 if (con_driver->con == csw) {
3508 desc = con_driver->desc;
3517 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3519 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3524 module_put(conswitchp->owner);
3526 __module_get(owner);
3530 first = max(first, con_driver->first);
3531 last = min(last, con_driver->last);
3533 for (i = first; i <= last; i++) {
3535 struct vc_data *vc = vc_cons[i].d;
3537 if (con_driver_map[i])
3538 module_put(con_driver_map[i]->owner);
3539 __module_get(owner);
3540 con_driver_map[i] = csw;
3542 if (!vc || !vc->vc_sw)
3547 if (con_is_visible(vc)) {
3552 old_was_color = vc->vc_can_do_color;
3553 vc->vc_sw->con_deinit(vc);
3554 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3555 visual_init(vc, i, 0);
3559 /* If the console changed between mono <-> color, then
3560 * the attributes in the screenbuf will be wrong. The
3561 * following resets all attributes to something sane.
3563 if (old_was_color != vc->vc_can_do_color)
3564 clear_buffer_attributes(vc);
3567 pr_info("Console: switching ");
3569 pr_cont("consoles %d-%d ", first + 1, last + 1);
3571 struct vc_data *vc = vc_cons[j].d;
3573 pr_cont("to %s %s %dx%d\n",
3574 vc->vc_can_do_color ? "colour" : "mono",
3575 desc, vc->vc_cols, vc->vc_rows);
3582 pr_cont("to %s\n", desc);
3592 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3593 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3595 struct module *owner = csw->owner;
3596 const struct consw *defcsw = NULL;
3597 struct con_driver *con_driver = NULL, *con_back = NULL;
3598 int i, retval = -ENODEV;
3600 if (!try_module_get(owner))
3603 WARN_CONSOLE_UNLOCKED();
3605 /* check if driver is registered and if it is unbindable */
3606 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3607 con_driver = ®istered_con_driver[i];
3609 if (con_driver->con == csw &&
3610 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3621 /* check if backup driver exists */
3622 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3623 con_back = ®istered_con_driver[i];
3625 if (con_back->con && con_back->con != csw) {
3626 defcsw = con_back->con;
3635 if (!con_is_bound(csw))
3638 first = max(first, con_driver->first);
3639 last = min(last, con_driver->last);
3641 for (i = first; i <= last; i++) {
3642 if (con_driver_map[i] == csw) {
3643 module_put(csw->owner);
3644 con_driver_map[i] = NULL;
3648 if (!con_is_bound(defcsw)) {
3649 const struct consw *defconsw = conswitchp;
3651 defcsw->con_startup();
3652 con_back->flag |= CON_DRIVER_FLAG_INIT;
3654 * vgacon may change the default driver to point
3655 * to dummycon, we restore it here...
3657 conswitchp = defconsw;
3660 if (!con_is_bound(csw))
3661 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3663 /* ignore return value, binding should not fail */
3664 do_bind_con_driver(defcsw, first, last, deflt);
3670 EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3672 static int vt_bind(struct con_driver *con)
3674 const struct consw *defcsw = NULL, *csw = NULL;
3675 int i, more = 1, first = -1, last = -1, deflt = 0;
3677 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3682 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3683 struct con_driver *con = ®istered_con_driver[i];
3685 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3697 for (i = con->first; i <= con->last; i++) {
3698 if (con_driver_map[i] == defcsw) {
3703 } else if (first != -1)
3707 if (first == 0 && last == MAX_NR_CONSOLES -1)
3711 do_bind_con_driver(csw, first, last, deflt);
3722 static int vt_unbind(struct con_driver *con)
3724 const struct consw *csw = NULL;
3725 int i, more = 1, first = -1, last = -1, deflt = 0;
3728 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3736 for (i = con->first; i <= con->last; i++) {
3737 if (con_driver_map[i] == csw) {
3742 } else if (first != -1)
3746 if (first == 0 && last == MAX_NR_CONSOLES -1)
3750 ret = do_unbind_con_driver(csw, first, last, deflt);
3764 static inline int vt_bind(struct con_driver *con)
3768 static inline int vt_unbind(struct con_driver *con)
3772 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3774 static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3775 const char *buf, size_t count)
3777 struct con_driver *con = dev_get_drvdata(dev);
3778 int bind = simple_strtoul(buf, NULL, 0);
3792 static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3795 struct con_driver *con = dev_get_drvdata(dev);
3799 bind = con_is_bound(con->con);
3802 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3805 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3808 struct con_driver *con = dev_get_drvdata(dev);
3810 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3811 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3816 static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3817 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3819 static struct attribute *con_dev_attrs[] = {
3820 &dev_attr_bind.attr,
3821 &dev_attr_name.attr,
3825 ATTRIBUTE_GROUPS(con_dev);
3827 static int vtconsole_init_device(struct con_driver *con)
3829 con->flag |= CON_DRIVER_FLAG_ATTR;
3833 static void vtconsole_deinit_device(struct con_driver *con)
3835 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3839 * con_is_bound - checks if driver is bound to the console
3840 * @csw: console driver
3842 * RETURNS: zero if unbound, nonzero if bound
3844 * Drivers can call this and if zero, they should release
3845 * all resources allocated on con_startup()
3847 int con_is_bound(const struct consw *csw)
3851 WARN_CONSOLE_UNLOCKED();
3853 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3854 if (con_driver_map[i] == csw) {
3862 EXPORT_SYMBOL(con_is_bound);
3865 * con_is_visible - checks whether the current console is visible
3866 * @vc: virtual console
3868 * RETURNS: zero if not visible, nonzero if visible
3870 bool con_is_visible(const struct vc_data *vc)
3872 WARN_CONSOLE_UNLOCKED();
3874 return *vc->vc_display_fg == vc;
3876 EXPORT_SYMBOL(con_is_visible);
3879 * con_debug_enter - prepare the console for the kernel debugger
3880 * @sw: console driver
3882 * Called when the console is taken over by the kernel debugger, this
3883 * function needs to save the current console state, then put the console
3884 * into a state suitable for the kernel debugger.
3887 * Zero on success, nonzero if a failure occurred when trying to prepare
3888 * the console for the debugger.
3890 int con_debug_enter(struct vc_data *vc)
3894 saved_fg_console = fg_console;
3895 saved_last_console = last_console;
3896 saved_want_console = want_console;
3897 saved_vc_mode = vc->vc_mode;
3898 saved_console_blanked = console_blanked;
3899 vc->vc_mode = KD_TEXT;
3900 console_blanked = 0;
3901 if (vc->vc_sw->con_debug_enter)
3902 ret = vc->vc_sw->con_debug_enter(vc);
3903 #ifdef CONFIG_KGDB_KDB
3904 /* Set the initial LINES variable if it is not already set */
3905 if (vc->vc_rows < 999) {
3908 const char *setargs[3] = {
3913 if (kdbgetintenv(setargs[0], &linecount)) {
3914 snprintf(lns, 4, "%i", vc->vc_rows);
3915 kdb_set(2, setargs);
3918 if (vc->vc_cols < 999) {
3921 const char *setargs[3] = {
3926 if (kdbgetintenv(setargs[0], &colcount)) {
3927 snprintf(cols, 4, "%i", vc->vc_cols);
3928 kdb_set(2, setargs);
3931 #endif /* CONFIG_KGDB_KDB */
3934 EXPORT_SYMBOL_GPL(con_debug_enter);
3937 * con_debug_leave - restore console state
3938 * @sw: console driver
3940 * Restore the console state to what it was before the kernel debugger
3944 * Zero on success, nonzero if a failure occurred when trying to restore
3947 int con_debug_leave(void)
3952 fg_console = saved_fg_console;
3953 last_console = saved_last_console;
3954 want_console = saved_want_console;
3955 console_blanked = saved_console_blanked;
3956 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3958 vc = vc_cons[fg_console].d;
3959 if (vc->vc_sw->con_debug_leave)
3960 ret = vc->vc_sw->con_debug_leave(vc);
3963 EXPORT_SYMBOL_GPL(con_debug_leave);
3965 static int do_register_con_driver(const struct consw *csw, int first, int last)
3967 struct module *owner = csw->owner;
3968 struct con_driver *con_driver;
3972 WARN_CONSOLE_UNLOCKED();
3974 if (!try_module_get(owner))
3977 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3978 con_driver = ®istered_con_driver[i];
3980 /* already registered */
3981 if (con_driver->con == csw) {
3987 desc = csw->con_startup();
3995 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3996 con_driver = ®istered_con_driver[i];
3998 if (con_driver->con == NULL &&
3999 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
4000 con_driver->con = csw;
4001 con_driver->desc = desc;
4002 con_driver->node = i;
4003 con_driver->flag = CON_DRIVER_FLAG_MODULE |
4004 CON_DRIVER_FLAG_INIT;
4005 con_driver->first = first;
4006 con_driver->last = last;
4016 device_create_with_groups(vtconsole_class, NULL,
4017 MKDEV(0, con_driver->node),
4018 con_driver, con_dev_groups,
4019 "vtcon%i", con_driver->node);
4020 if (IS_ERR(con_driver->dev)) {
4021 pr_warn("Unable to create device for %s; errno = %ld\n",
4022 con_driver->desc, PTR_ERR(con_driver->dev));
4023 con_driver->dev = NULL;
4025 vtconsole_init_device(con_driver);
4035 * do_unregister_con_driver - unregister console driver from console layer
4036 * @csw: console driver
4038 * DESCRIPTION: All drivers that registers to the console layer must
4039 * call this function upon exit, or if the console driver is in a state
4040 * where it won't be able to handle console services, such as the
4041 * framebuffer console without loaded framebuffer drivers.
4043 * The driver must unbind first prior to unregistration.
4045 int do_unregister_con_driver(const struct consw *csw)
4049 /* cannot unregister a bound driver */
4050 if (con_is_bound(csw))
4053 if (csw == conswitchp)
4056 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4057 struct con_driver *con_driver = ®istered_con_driver[i];
4059 if (con_driver->con == csw) {
4061 * Defer the removal of the sysfs entries since that
4062 * will acquire the kernfs s_active lock and we can't
4063 * acquire this lock while holding the console lock:
4064 * the unbind sysfs entry imposes already the opposite
4065 * order. Reset con already here to prevent any later
4066 * lookup to succeed and mark this slot as zombie, so
4067 * it won't get reused until we complete the removal
4068 * in the deferred work.
4070 con_driver->con = NULL;
4071 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4072 schedule_work(&con_driver_unregister_work);
4080 EXPORT_SYMBOL_GPL(do_unregister_con_driver);
4082 static void con_driver_unregister_callback(struct work_struct *ignored)
4088 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4089 struct con_driver *con_driver = ®istered_con_driver[i];
4091 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4096 vtconsole_deinit_device(con_driver);
4097 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4101 if (WARN_ON_ONCE(con_driver->con))
4102 con_driver->con = NULL;
4103 con_driver->desc = NULL;
4104 con_driver->dev = NULL;
4105 con_driver->node = 0;
4106 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4107 con_driver->flag = 0;
4108 con_driver->first = 0;
4109 con_driver->last = 0;
4116 * If we support more console drivers, this function is used
4117 * when a driver wants to take over some existing consoles
4118 * and become default driver for newly opened ones.
4120 * do_take_over_console is basically a register followed by bind
4122 int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4126 err = do_register_con_driver(csw, first, last);
4128 * If we get an busy error we still want to bind the console driver
4129 * and return success, as we may have unbound the console driver
4130 * but not unregistered it.
4135 do_bind_con_driver(csw, first, last, deflt);
4139 EXPORT_SYMBOL_GPL(do_take_over_console);
4143 * give_up_console is a wrapper to unregister_con_driver. It will only
4144 * work if driver is fully unbound.
4146 void give_up_console(const struct consw *csw)
4149 do_unregister_con_driver(csw);
4153 static int __init vtconsole_class_init(void)
4157 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4158 if (IS_ERR(vtconsole_class)) {
4159 pr_warn("Unable to create vt console class; errno = %ld\n",
4160 PTR_ERR(vtconsole_class));
4161 vtconsole_class = NULL;
4164 /* Add system drivers to sysfs */
4165 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4166 struct con_driver *con = ®istered_con_driver[i];
4168 if (con->con && !con->dev) {
4170 device_create_with_groups(vtconsole_class, NULL,
4171 MKDEV(0, con->node),
4172 con, con_dev_groups,
4173 "vtcon%i", con->node);
4175 if (IS_ERR(con->dev)) {
4176 pr_warn("Unable to create device for %s; errno = %ld\n",
4177 con->desc, PTR_ERR(con->dev));
4180 vtconsole_init_device(con);
4187 postcore_initcall(vtconsole_class_init);
4195 static int set_vesa_blanking(char __user *p)
4199 if (get_user(mode, p + 1))
4202 vesa_blank_mode = (mode < 4) ? mode : 0;
4206 void do_blank_screen(int entering_gfx)
4208 struct vc_data *vc = vc_cons[fg_console].d;
4213 WARN_CONSOLE_UNLOCKED();
4215 if (console_blanked) {
4216 if (blank_state == blank_vesa_wait) {
4217 blank_state = blank_off;
4218 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
4223 /* entering graphics mode? */
4227 vc->vc_sw->con_blank(vc, -1, 1);
4228 console_blanked = fg_console + 1;
4229 blank_state = blank_off;
4234 blank_state = blank_off;
4236 /* don't blank graphics */
4237 if (vc->vc_mode != KD_TEXT) {
4238 console_blanked = fg_console + 1;
4243 del_timer_sync(&console_timer);
4244 blank_timer_expired = 0;
4247 /* In case we need to reset origin, blanking hook returns 1 */
4248 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
4249 console_blanked = fg_console + 1;
4253 if (console_blank_hook && console_blank_hook(1))
4256 if (vesa_off_interval && vesa_blank_mode) {
4257 blank_state = blank_vesa_wait;
4258 mod_timer(&console_timer, jiffies + vesa_off_interval);
4260 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
4262 EXPORT_SYMBOL(do_blank_screen);
4265 * Called by timer as well as from vt_console_driver
4267 void do_unblank_screen(int leaving_gfx)
4271 /* This should now always be called from a "sane" (read: can schedule)
4272 * context for the sake of the low level drivers, except in the special
4273 * case of oops_in_progress
4275 if (!oops_in_progress)
4278 WARN_CONSOLE_UNLOCKED();
4281 if (!console_blanked)
4283 if (!vc_cons_allocated(fg_console)) {
4285 pr_warn("unblank_screen: tty %d not allocated ??\n",
4289 vc = vc_cons[fg_console].d;
4290 if (vc->vc_mode != KD_TEXT)
4291 return; /* but leave console_blanked != 0 */
4293 if (blankinterval) {
4294 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
4295 blank_state = blank_normal_wait;
4298 console_blanked = 0;
4299 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
4300 /* Low-level driver cannot restore -> do it ourselves */
4302 if (console_blank_hook)
4303 console_blank_hook(0);
4306 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
4308 EXPORT_SYMBOL(do_unblank_screen);
4311 * This is called by the outside world to cause a forced unblank, mostly for
4312 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4313 * call it with 1 as an argument and so force a mode restore... that may kill
4314 * X or at least garbage the screen but would also make the Oops visible...
4316 void unblank_screen(void)
4318 do_unblank_screen(0);
4322 * We defer the timer blanking to work queue so it can take the console mutex
4323 * (console operations can still happen at irq time, but only from printk which
4324 * has the console mutex. Not perfect yet, but better than no locking
4326 static void blank_screen_t(struct timer_list *unused)
4328 blank_timer_expired = 1;
4329 schedule_work(&console_work);
4332 void poke_blanked_console(void)
4334 WARN_CONSOLE_UNLOCKED();
4336 /* Add this so we quickly catch whoever might call us in a non
4337 * safe context. Nowadays, unblank_screen() isn't to be called in
4338 * atomic contexts and is allowed to schedule (with the special case
4339 * of oops_in_progress, but that isn't of any concern for this
4344 /* This isn't perfectly race free, but a race here would be mostly harmless,
4345 * at worse, we'll do a spurrious blank and it's unlikely
4347 del_timer(&console_timer);
4348 blank_timer_expired = 0;
4350 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4352 if (console_blanked)
4354 else if (blankinterval) {
4355 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
4356 blank_state = blank_normal_wait;
4364 static void set_palette(struct vc_data *vc)
4366 WARN_CONSOLE_UNLOCKED();
4368 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
4369 vc->vc_sw->con_set_palette(vc, color_table);
4373 * Load palette into the DAC registers. arg points to a colour
4374 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4377 int con_set_cmap(unsigned char __user *arg)
4380 unsigned char colormap[3*16];
4382 if (copy_from_user(colormap, arg, sizeof(colormap)))
4386 for (i = k = 0; i < 16; i++) {
4387 default_red[i] = colormap[k++];
4388 default_grn[i] = colormap[k++];
4389 default_blu[i] = colormap[k++];
4391 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4392 if (!vc_cons_allocated(i))
4394 for (j = k = 0; j < 16; j++) {
4395 vc_cons[i].d->vc_palette[k++] = default_red[j];
4396 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4397 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4399 set_palette(vc_cons[i].d);
4406 int con_get_cmap(unsigned char __user *arg)
4409 unsigned char colormap[3*16];
4412 for (i = k = 0; i < 16; i++) {
4413 colormap[k++] = default_red[i];
4414 colormap[k++] = default_grn[i];
4415 colormap[k++] = default_blu[i];
4419 if (copy_to_user(arg, colormap, sizeof(colormap)))
4425 void reset_palette(struct vc_data *vc)
4428 for (j=k=0; j<16; j++) {
4429 vc->vc_palette[k++] = default_red[j];
4430 vc->vc_palette[k++] = default_grn[j];
4431 vc->vc_palette[k++] = default_blu[j];
4439 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4440 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4441 * depending on width) reserved for each character which is kinda wasty, but
4442 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
4443 * is up to the actual low-level console-driver convert data into its favorite
4444 * format (maybe we should add a `fontoffset' field to the `display'
4445 * structure so we won't have to convert the fontdata all the time.
4449 #define max_font_size 65536
4451 static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4453 struct console_font font;
4458 font.data = kmalloc(max_font_size, GFP_KERNEL);
4465 if (vc->vc_mode != KD_TEXT)
4467 else if (vc->vc_sw->con_font_get)
4468 rc = vc->vc_sw->con_font_get(vc, &font);
4476 c = (font.width+7)/8 * 32 * font.charcount;
4478 if (op->data && font.charcount > op->charcount)
4480 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4481 if (font.width > op->width || font.height > op->height)
4484 if (font.width != 8)
4486 else if ((op->height && font.height > op->height) ||
4493 op->height = font.height;
4494 op->width = font.width;
4495 op->charcount = font.charcount;
4497 if (op->data && copy_to_user(op->data, font.data, c))
4505 static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4507 struct console_font font;
4511 if (vc->vc_mode != KD_TEXT)
4515 if (op->charcount > 512)
4517 if (op->width <= 0 || op->width > 32 || op->height > 32)
4519 size = (op->width+7)/8 * 32 * op->charcount;
4520 if (size > max_font_size)
4523 font.data = memdup_user(op->data, size);
4524 if (IS_ERR(font.data))
4525 return PTR_ERR(font.data);
4527 if (!op->height) { /* Need to guess font height [compat] */
4529 u8 *charmap = font.data;
4532 * If from KDFONTOP ioctl, don't allow things which can be done
4533 * in userland,so that we can get rid of this soon
4535 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4540 for (h = 32; h > 0; h--)
4541 for (i = 0; i < op->charcount; i++)
4542 if (charmap[32*i+h-1])
4552 font.charcount = op->charcount;
4553 font.width = op->width;
4554 font.height = op->height;
4557 if (vc->vc_mode != KD_TEXT)
4559 else if (vc->vc_sw->con_font_set)
4560 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4568 static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4570 struct console_font font = {.width = op->width, .height = op->height};
4571 char name[MAX_FONT_NAME];
4578 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4581 name[MAX_FONT_NAME - 1] = 0;
4584 if (vc->vc_mode != KD_TEXT) {
4588 if (vc->vc_sw->con_font_default)
4589 rc = vc->vc_sw->con_font_default(vc, &font, s);
4594 op->width = font.width;
4595 op->height = font.height;
4600 static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4602 int con = op->height;
4607 if (vc->vc_mode != KD_TEXT)
4609 else if (!vc->vc_sw->con_font_copy)
4611 else if (con < 0 || !vc_cons_allocated(con))
4613 else if (con == vc->vc_num) /* nothing to do */
4616 rc = vc->vc_sw->con_font_copy(vc, con);
4621 int con_font_op(struct vc_data *vc, struct console_font_op *op)
4624 case KD_FONT_OP_SET:
4625 return con_font_set(vc, op);
4626 case KD_FONT_OP_GET:
4627 return con_font_get(vc, op);
4628 case KD_FONT_OP_SET_DEFAULT:
4629 return con_font_default(vc, op);
4630 case KD_FONT_OP_COPY:
4631 return con_font_copy(vc, op);
4637 * Interface exported to selection and vcs.
4640 /* used by selection */
4641 u16 screen_glyph(struct vc_data *vc, int offset)
4643 u16 w = scr_readw(screenpos(vc, offset, 1));
4646 if (w & vc->vc_hi_font_mask)
4650 EXPORT_SYMBOL_GPL(screen_glyph);
4652 u32 screen_glyph_unicode(struct vc_data *vc, int n)
4654 struct uni_screen *uniscr = get_vc_uniscr(vc);
4657 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4658 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4660 EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4662 /* used by vcs - note the word offset */
4663 unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4665 return screenpos(vc, 2 * w_offset, viewed);
4667 EXPORT_SYMBOL_GPL(screen_pos);
4669 void getconsxy(struct vc_data *vc, unsigned char *p)
4671 /* clamp values if they don't fit */
4672 p[0] = min(vc->state.x, 0xFFu);
4673 p[1] = min(vc->state.y, 0xFFu);
4676 void putconsxy(struct vc_data *vc, unsigned char *p)
4679 gotoxy(vc, p[0], p[1]);
4683 u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4685 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4686 return softcursor_original;
4687 return scr_readw(org);
4690 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4692 scr_writew(val, org);
4693 if ((unsigned long)org == vc->vc_pos) {
4694 softcursor_original = -1;
4699 void vcs_scr_updated(struct vc_data *vc)
4704 void vc_scrolldelta_helper(struct vc_data *c, int lines,
4705 unsigned int rolled_over, void *base, unsigned int size)
4707 unsigned long ubase = (unsigned long)base;
4708 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4709 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4710 ptrdiff_t origin = (void *)c->vc_origin - base;
4711 int margin = c->vc_size_row * 4;
4712 int from, wrap, from_off, avail;
4714 /* Turn scrollback off */
4716 c->vc_visible_origin = c->vc_origin;
4720 /* Do we have already enough to allow jumping from 0 to the end? */
4721 if (rolled_over > scr_end + margin) {
4723 wrap = rolled_over + c->vc_size_row;
4729 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4730 avail = (origin - from + wrap) % wrap;
4732 /* Only a little piece would be left? Show all incl. the piece! */
4733 if (avail < 2 * margin)
4735 if (from_off < margin)
4737 if (from_off > avail - margin)
4740 c->vc_visible_origin = ubase + (from + from_off) % wrap;
4742 EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4745 * Visible symbols for modules
4748 EXPORT_SYMBOL(color_table);
4749 EXPORT_SYMBOL(default_red);
4750 EXPORT_SYMBOL(default_grn);
4751 EXPORT_SYMBOL(default_blu);
4752 EXPORT_SYMBOL(update_region);
4753 EXPORT_SYMBOL(redraw_screen);
4754 EXPORT_SYMBOL(vc_resize);
4755 EXPORT_SYMBOL(fg_console);
4756 EXPORT_SYMBOL(console_blank_hook);
4757 EXPORT_SYMBOL(console_blanked);
4758 EXPORT_SYMBOL(vc_cons);
4759 EXPORT_SYMBOL(global_cursor_default);
4760 #ifndef VT_SINGLE_DRIVER
4761 EXPORT_SYMBOL(give_up_console);