1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 1991, 1992 Linus Torvalds 4 */ 5 6 /* 7 * Hopefully this will be a rather complete VT102 implementation. 8 * 9 * Beeping thanks to John T Kohl. 10 * 11 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics 12 * Chars, and VT100 enhancements by Peter MacDonald. 13 * 14 * Copy and paste function by Andrew Haylett, 15 * some enhancements by Alessandro Rubini. 16 * 17 * Code to check for different video-cards mostly by Galen Hunt, 18 * <g-hunt@ee.utah.edu> 19 * 20 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by 21 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>. 22 * 23 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994 24 * Resizing of consoles, aeb, 940926 25 * 26 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94 27 * <poe@daimi.aau.dk> 28 * 29 * User-defined bell sound, new setterm control sequences and printk 30 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95 31 * 32 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp> 33 * 34 * Merge with the abstract console driver by Geert Uytterhoeven 35 * <geert@linux-m68k.org>, Jan 1997. 36 * 37 * Original m68k console driver modifications by 38 * 39 * - Arno Griffioen <arno@usn.nl> 40 * - David Carter <carter@cs.bris.ac.uk> 41 * 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). 46 * 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). 50 * 51 * Support for changeable cursor shape 52 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997 53 * 54 * Ported to i386 and con_scrolldelta fixed 55 * by Emmanuel Marty <core@ggi-project.org>, April 1998 56 * 57 * Resurrected character buffers in videoram plus lots of other trickery 58 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998 59 * 60 * Removed old-style timers, introduced console_timer, made timer 61 * deletion SMP-safe. 17Jun00, Andrew Morton 62 * 63 * Removed console_lock, enabled interrupts across all console operations 64 * 13 March 2001, Andrew Morton 65 * 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 72 */ 73 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> 82 #include <linux/hex.h> 83 #include <linux/kd.h> 84 #include <linux/slab.h> 85 #include <linux/vmalloc.h> 86 #include <linux/major.h> 87 #include <linux/mm.h> 88 #include <linux/console.h> 89 #include <linux/init.h> 90 #include <linux/mutex.h> 91 #include <linux/vt_kern.h> 92 #include <linux/selection.h> 93 #include <linux/tiocl.h> 94 #include <linux/kbd_kern.h> 95 #include <linux/consolemap.h> 96 #include <linux/timer.h> 97 #include <linux/interrupt.h> 98 #include <linux/workqueue.h> 99 #include <linux/pm.h> 100 #include <linux/font.h> 101 #include <linux/bitops.h> 102 #include <linux/notifier.h> 103 #include <linux/device.h> 104 #include <linux/io.h> 105 #include <linux/uaccess.h> 106 #include <linux/kdb.h> 107 #include <linux/ctype.h> 108 #include <linux/gcd.h> 109 110 #define MAX_NR_CON_DRIVER 16 111 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 116 117 struct con_driver { 118 const struct consw *con; 119 const char *desc; 120 struct device *dev; 121 int node; 122 int first; 123 int last; 124 int flag; 125 }; 126 127 static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER]; 128 const struct consw *conswitchp; 129 130 /* 131 * Here is the default bell parameters: 750HZ, 1/8th of a second 132 */ 133 #define DEFAULT_BELL_PITCH 750 134 #define DEFAULT_BELL_DURATION (HZ/8) 135 #define DEFAULT_CURSOR_BLINK_MS 200 136 137 struct vc vc_cons [MAX_NR_CONSOLES]; 138 EXPORT_SYMBOL(vc_cons); 139 140 static const struct consw *con_driver_map[MAX_NR_CONSOLES]; 141 142 static int con_open(struct tty_struct *, struct file *); 143 static void vc_init(struct vc_data *vc, int do_clear); 144 static void gotoxy(struct vc_data *vc, int new_x, int new_y); 145 static void restore_cur(struct vc_data *vc); 146 static void save_cur(struct vc_data *vc); 147 static void reset_terminal(struct vc_data *vc, int do_clear); 148 static void con_flush_chars(struct tty_struct *tty); 149 static int set_vesa_blanking(u8 __user *mode); 150 static void set_cursor(struct vc_data *vc); 151 static void hide_cursor(struct vc_data *vc); 152 static void console_callback(struct work_struct *ignored); 153 static void con_driver_unregister_callback(struct work_struct *ignored); 154 static void blank_screen_t(struct timer_list *unused); 155 static void set_palette(struct vc_data *vc); 156 static void unblank_screen(void); 157 158 #define vt_get_kmsg_redirect() vt_kmsg_redirect(-1) 159 160 int default_utf8 = true; 161 module_param(default_utf8, int, S_IRUGO | S_IWUSR); 162 int global_cursor_default = -1; 163 module_param(global_cursor_default, int, S_IRUGO | S_IWUSR); 164 EXPORT_SYMBOL(global_cursor_default); 165 166 static int cur_default = CUR_UNDERLINE; 167 module_param(cur_default, int, S_IRUGO | S_IWUSR); 168 169 /* 170 * ignore_poke: don't unblank the screen when things are typed. This is 171 * mainly for the privacy of braille terminal users. 172 */ 173 static int ignore_poke; 174 175 int do_poke_blanked_console; 176 int console_blanked; 177 EXPORT_SYMBOL(console_blanked); 178 179 static enum vesa_blank_mode vesa_blank_mode; 180 static int vesa_off_interval; 181 static int blankinterval; 182 core_param(consoleblank, blankinterval, int, 0444); 183 184 static DECLARE_WORK(console_work, console_callback); 185 static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback); 186 187 /* 188 * fg_console is the current virtual console, 189 * last_console is the last used one, 190 * want_console is the console we want to switch to, 191 */ 192 int fg_console; 193 EXPORT_SYMBOL(fg_console); 194 int last_console; 195 int want_console = -1; 196 197 /* 198 * For each existing display, we have a pointer to console currently visible 199 * on that display, allowing consoles other than fg_console to be refreshed 200 * appropriately. Unless the low-level driver supplies its own display_fg 201 * variable, we use this one for the "master display". 202 */ 203 static struct vc_data *master_display_fg; 204 205 /* 206 * Unfortunately, we need to delay tty echo when we're currently writing to the 207 * console since the code is (and always was) not re-entrant, so we schedule 208 * all flip requests to process context with schedule-task() and run it from 209 * console_callback(). 210 */ 211 212 /* 213 * For the same reason, we defer scrollback to the console callback. 214 */ 215 static int scrollback_delta; 216 217 /* 218 * Hook so that the power management routines can (un)blank 219 * the console on our behalf. 220 */ 221 int (*console_blank_hook)(int); 222 EXPORT_SYMBOL(console_blank_hook); 223 224 static DEFINE_TIMER(console_timer, blank_screen_t); 225 static int blank_state; 226 static int blank_timer_expired; 227 enum { 228 blank_off = 0, 229 blank_normal_wait, 230 blank_vesa_wait, 231 }; 232 233 /* 234 * struct vc_font 235 */ 236 237 /** 238 * vc_font_pitch - Calculates the number of bytes between two adjacent scanlines 239 * @font: The VC font 240 * 241 * Returns: 242 * The number of bytes between two adjacent scanlines in the font data 243 */ 244 unsigned int vc_font_pitch(const struct vc_font *font) 245 { 246 return font_glyph_pitch(font->width); 247 } 248 EXPORT_SYMBOL_GPL(vc_font_pitch); 249 250 /** 251 * vc_font_size - Calculates the size of the font data in bytes 252 * @font: The VC font 253 * 254 * vc_font_size() calculates the number of bytes of font data in the 255 * font specified by @font. The function calculates the size from the 256 * font parameters. 257 * 258 * Returns: 259 * The size of the font data in bytes. 260 */ 261 unsigned int vc_font_size(const struct vc_font *font) 262 { 263 return font_glyph_size(font->width, font->height) * font->charcount; 264 } 265 EXPORT_SYMBOL_GPL(vc_font_size); 266 267 /* 268 * /sys/class/tty/tty0/ 269 * 270 * the attribute 'active' contains the name of the current vc 271 * console and it supports poll() to detect vc switches 272 */ 273 static struct device *tty0dev; 274 275 /* 276 * Notifier list for console events. 277 */ 278 static ATOMIC_NOTIFIER_HEAD(vt_notifier_list); 279 280 int register_vt_notifier(struct notifier_block *nb) 281 { 282 return atomic_notifier_chain_register(&vt_notifier_list, nb); 283 } 284 EXPORT_SYMBOL_GPL(register_vt_notifier); 285 286 int unregister_vt_notifier(struct notifier_block *nb) 287 { 288 return atomic_notifier_chain_unregister(&vt_notifier_list, nb); 289 } 290 EXPORT_SYMBOL_GPL(unregister_vt_notifier); 291 292 static void notify_write(struct vc_data *vc, unsigned int unicode) 293 { 294 struct vt_notifier_param param = { .vc = vc, .c = unicode }; 295 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, ¶m); 296 } 297 298 static void notify_update(struct vc_data *vc) 299 { 300 struct vt_notifier_param param = { .vc = vc }; 301 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, ¶m); 302 } 303 /* 304 * Low-Level Functions 305 */ 306 307 static inline bool con_is_fg(const struct vc_data *vc) 308 { 309 return vc->vc_num == fg_console; 310 } 311 312 static inline bool con_should_update(const struct vc_data *vc) 313 { 314 return con_is_visible(vc) && !console_blanked; 315 } 316 317 static inline u16 *screenpos(const struct vc_data *vc, unsigned int offset, 318 bool viewed) 319 { 320 unsigned long origin = viewed ? vc->vc_visible_origin : vc->vc_origin; 321 322 return (u16 *)(origin + offset); 323 } 324 325 static void con_putc(struct vc_data *vc, u16 ca, unsigned int y, unsigned int x) 326 { 327 if (vc->vc_sw->con_putc) 328 vc->vc_sw->con_putc(vc, ca, y, x); 329 else 330 vc->vc_sw->con_putcs(vc, &ca, 1, y, x); 331 } 332 333 /* Called from the keyboard irq path.. */ 334 static inline void scrolldelta(int lines) 335 { 336 /* FIXME */ 337 /* scrolldelta needs some kind of consistency lock, but the BKL was 338 and still is not protecting versus the scheduled back end */ 339 scrollback_delta += lines; 340 schedule_console_callback(); 341 } 342 343 void schedule_console_callback(void) 344 { 345 schedule_work(&console_work); 346 } 347 348 /* 349 * Code to manage unicode-based screen buffers 350 */ 351 352 /* 353 * Our screen buffer is preceded by an array of line pointers so that 354 * scrolling only implies some pointer shuffling. 355 */ 356 357 static u32 **vc_uniscr_alloc(unsigned int cols, unsigned int rows) 358 { 359 u32 **uni_lines; 360 void *p; 361 unsigned int memsize, i, col_size = cols * sizeof(**uni_lines); 362 363 /* allocate everything in one go */ 364 memsize = col_size * rows; 365 memsize += rows * sizeof(*uni_lines); 366 uni_lines = vzalloc(memsize); 367 if (!uni_lines) 368 return NULL; 369 370 /* initial line pointers */ 371 p = uni_lines + rows; 372 for (i = 0; i < rows; i++) { 373 uni_lines[i] = p; 374 p += col_size; 375 } 376 377 return uni_lines; 378 } 379 380 static void vc_uniscr_free(u32 **uni_lines) 381 { 382 vfree(uni_lines); 383 } 384 385 static void vc_uniscr_set(struct vc_data *vc, u32 **new_uni_lines) 386 { 387 vc_uniscr_free(vc->vc_uni_lines); 388 vc->vc_uni_lines = new_uni_lines; 389 } 390 391 static void vc_uniscr_putc(struct vc_data *vc, u32 uc) 392 { 393 if (vc->vc_uni_lines) 394 vc->vc_uni_lines[vc->state.y][vc->state.x] = uc; 395 } 396 397 static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr) 398 { 399 if (vc->vc_uni_lines) { 400 u32 *ln = vc->vc_uni_lines[vc->state.y]; 401 unsigned int x = vc->state.x, cols = vc->vc_cols; 402 403 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln)); 404 memset32(&ln[x], ' ', nr); 405 } 406 } 407 408 static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr) 409 { 410 if (vc->vc_uni_lines) { 411 u32 *ln = vc->vc_uni_lines[vc->state.y]; 412 unsigned int x = vc->state.x, cols = vc->vc_cols; 413 414 memmove(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln)); 415 memset32(&ln[cols - nr], ' ', nr); 416 } 417 } 418 419 static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x, 420 unsigned int nr) 421 { 422 if (vc->vc_uni_lines) 423 memset32(&vc->vc_uni_lines[vc->state.y][x], ' ', nr); 424 } 425 426 static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y, 427 unsigned int nr) 428 { 429 if (vc->vc_uni_lines) 430 while (nr--) 431 memset32(vc->vc_uni_lines[y++], ' ', vc->vc_cols); 432 } 433 434 /* juggling array rotation algorithm (complexity O(N), size complexity O(1)) */ 435 static void juggle_array(u32 **array, unsigned int size, unsigned int nr) 436 { 437 unsigned int gcd_idx; 438 439 for (gcd_idx = 0; gcd_idx < gcd(nr, size); gcd_idx++) { 440 u32 *gcd_idx_val = array[gcd_idx]; 441 unsigned int dst_idx = gcd_idx; 442 443 while (1) { 444 unsigned int src_idx = (dst_idx + nr) % size; 445 if (src_idx == gcd_idx) 446 break; 447 448 array[dst_idx] = array[src_idx]; 449 dst_idx = src_idx; 450 } 451 452 array[dst_idx] = gcd_idx_val; 453 } 454 } 455 456 static void vc_uniscr_scroll(struct vc_data *vc, unsigned int top, 457 unsigned int bottom, enum con_scroll dir, 458 unsigned int nr) 459 { 460 u32 **uni_lines = vc->vc_uni_lines; 461 unsigned int size = bottom - top; 462 463 if (!uni_lines) 464 return; 465 466 if (dir == SM_DOWN) { 467 juggle_array(&uni_lines[top], size, size - nr); 468 vc_uniscr_clear_lines(vc, top, nr); 469 } else { 470 juggle_array(&uni_lines[top], size, nr); 471 vc_uniscr_clear_lines(vc, bottom - nr, nr); 472 } 473 } 474 475 static u32 vc_uniscr_getc(struct vc_data *vc, int relative_pos) 476 { 477 int pos = vc->state.x + vc->vc_need_wrap + relative_pos; 478 479 if (vc->vc_uni_lines && in_range(pos, 0, vc->vc_cols)) 480 return vc->vc_uni_lines[vc->state.y][pos]; 481 return 0; 482 } 483 484 static void vc_uniscr_copy_area(u32 **dst_lines, 485 unsigned int dst_cols, 486 unsigned int dst_rows, 487 u32 **src_lines, 488 unsigned int src_cols, 489 unsigned int src_top_row, 490 unsigned int src_bot_row) 491 { 492 unsigned int dst_row = 0; 493 494 if (!dst_lines) 495 return; 496 497 while (src_top_row < src_bot_row) { 498 u32 *src_line = src_lines[src_top_row]; 499 u32 *dst_line = dst_lines[dst_row]; 500 501 memcpy(dst_line, src_line, src_cols * sizeof(*src_line)); 502 if (dst_cols - src_cols) 503 memset32(dst_line + src_cols, ' ', dst_cols - src_cols); 504 src_top_row++; 505 dst_row++; 506 } 507 while (dst_row < dst_rows) { 508 u32 *dst_line = dst_lines[dst_row]; 509 510 memset32(dst_line, ' ', dst_cols); 511 dst_row++; 512 } 513 } 514 515 /* 516 * Called from vcs_read() to make sure unicode screen retrieval is possible. 517 * This will initialize the unicode screen buffer if not already done. 518 * This returns 0 if OK, or a negative error code otherwise. 519 * In particular, -ENODATA is returned if the console is not in UTF-8 mode. 520 */ 521 int vc_uniscr_check(struct vc_data *vc) 522 { 523 u32 **uni_lines; 524 unsigned short *p; 525 int x, y, mask; 526 527 WARN_CONSOLE_UNLOCKED(); 528 529 if (!vc->vc_utf) 530 return -ENODATA; 531 532 if (vc->vc_uni_lines) 533 return 0; 534 535 uni_lines = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 536 if (!uni_lines) 537 return -ENOMEM; 538 539 /* 540 * Let's populate it initially with (imperfect) reverse translation. 541 * This is the next best thing we can do short of having it enabled 542 * from the start even when no users rely on this functionality. True 543 * unicode content will be available after a complete screen refresh. 544 */ 545 p = (unsigned short *)vc->vc_origin; 546 mask = vc->vc_hi_font_mask | 0xff; 547 for (y = 0; y < vc->vc_rows; y++) { 548 u32 *line = uni_lines[y]; 549 for (x = 0; x < vc->vc_cols; x++) { 550 u16 glyph = scr_readw(p++) & mask; 551 line[x] = inverse_translate(vc, glyph, true); 552 } 553 } 554 555 vc->vc_uni_lines = uni_lines; 556 557 return 0; 558 } 559 560 /* 561 * Called from vcs_read() to get the unicode data from the screen. 562 * This must be preceded by a successful call to vc_uniscr_check() once 563 * the console lock has been taken. 564 */ 565 void vc_uniscr_copy_line(const struct vc_data *vc, void *dest, bool viewed, 566 unsigned int row, unsigned int col, unsigned int nr) 567 { 568 u32 **uni_lines = vc->vc_uni_lines; 569 int offset = row * vc->vc_size_row + col * 2; 570 unsigned long pos; 571 572 if (WARN_ON_ONCE(!uni_lines)) 573 return; 574 575 pos = (unsigned long)screenpos(vc, offset, viewed); 576 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) { 577 /* 578 * Desired position falls in the main screen buffer. 579 * However the actual row/col might be different if 580 * scrollback is active. 581 */ 582 row = (pos - vc->vc_origin) / vc->vc_size_row; 583 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2; 584 memcpy(dest, &uni_lines[row][col], nr * sizeof(u32)); 585 } else { 586 /* 587 * Scrollback is active. For now let's simply backtranslate 588 * the screen glyphs until the unicode screen buffer does 589 * synchronize with console display drivers for a scrollback 590 * buffer of its own. 591 */ 592 u16 *p = (u16 *)pos; 593 int mask = vc->vc_hi_font_mask | 0xff; 594 u32 *uni_buf = dest; 595 while (nr--) { 596 u16 glyph = scr_readw(p++) & mask; 597 *uni_buf++ = inverse_translate(vc, glyph, true); 598 } 599 } 600 } 601 602 static void con_scroll(struct vc_data *vc, unsigned int top, 603 unsigned int bottom, enum con_scroll dir, 604 unsigned int nr) 605 { 606 unsigned int rows = bottom - top; 607 u16 *clear, *dst, *src; 608 609 if (top + nr >= bottom) 610 nr = rows - 1; 611 if (bottom > vc->vc_rows || top >= bottom || nr < 1) 612 return; 613 614 vc_uniscr_scroll(vc, top, bottom, dir, nr); 615 if (con_is_visible(vc) && 616 vc->vc_sw->con_scroll(vc, top, bottom, dir, nr)) 617 return; 618 619 src = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * top); 620 dst = (u16 *)(vc->vc_origin + vc->vc_size_row * (top + nr)); 621 622 if (dir == SM_UP) { 623 clear = src + (rows - nr) * vc->vc_cols; 624 swap(src, dst); 625 } 626 scr_memmovew(dst, src, (rows - nr) * vc->vc_size_row); 627 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr); 628 } 629 630 static void do_update_region(struct vc_data *vc, unsigned long start, int count) 631 { 632 unsigned int xx, yy, offset; 633 u16 *p = (u16 *)start; 634 635 offset = (start - vc->vc_origin) / 2; 636 xx = offset % vc->vc_cols; 637 yy = offset / vc->vc_cols; 638 639 for(;;) { 640 u16 attrib = scr_readw(p) & 0xff00; 641 int startx = xx; 642 u16 *q = p; 643 while (xx < vc->vc_cols && count) { 644 if (attrib != (scr_readw(p) & 0xff00)) { 645 if (p > q) 646 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); 647 startx = xx; 648 q = p; 649 attrib = scr_readw(p) & 0xff00; 650 } 651 p++; 652 xx++; 653 count--; 654 } 655 if (p > q) 656 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx); 657 if (!count) 658 break; 659 xx = 0; 660 yy++; 661 } 662 } 663 664 void update_region(struct vc_data *vc, unsigned long start, int count) 665 { 666 WARN_CONSOLE_UNLOCKED(); 667 668 if (con_should_update(vc)) { 669 hide_cursor(vc); 670 do_update_region(vc, start, count); 671 set_cursor(vc); 672 } 673 } 674 EXPORT_SYMBOL(update_region); 675 676 /* Structure of attributes is hardware-dependent */ 677 678 static u8 build_attr(struct vc_data *vc, u8 _color, 679 enum vc_intensity _intensity, bool _blink, bool _underline, 680 bool _reverse, bool _italic) 681 { 682 if (vc->vc_sw->con_build_attr) 683 return vc->vc_sw->con_build_attr(vc, _color, _intensity, 684 _blink, _underline, _reverse, _italic); 685 686 /* 687 * ++roman: I completely changed the attribute format for monochrome 688 * mode (!can_do_color). The formerly used MDA (monochrome display 689 * adapter) format didn't allow the combination of certain effects. 690 * Now the attribute is just a bit vector: 691 * Bit 0..1: intensity (0..2) 692 * Bit 2 : underline 693 * Bit 3 : reverse 694 * Bit 7 : blink 695 */ 696 { 697 u8 a = _color; 698 if (!vc->vc_can_do_color) 699 return _intensity | 700 (_italic << 1) | 701 (_underline << 2) | 702 (_reverse << 3) | 703 (_blink << 7); 704 if (_italic) 705 a = (a & 0xF0) | vc->vc_itcolor; 706 else if (_underline) 707 a = (a & 0xf0) | vc->vc_ulcolor; 708 else if (_intensity == VCI_HALF_BRIGHT) 709 a = (a & 0xf0) | vc->vc_halfcolor; 710 if (_reverse) 711 a = (a & 0x88) | (((a >> 4) | (a << 4)) & 0x77); 712 if (_blink) 713 a ^= 0x80; 714 if (_intensity == VCI_BOLD) 715 a ^= 0x08; 716 if (vc->vc_hi_font_mask == 0x100) 717 a <<= 1; 718 return a; 719 } 720 } 721 722 static void update_attr(struct vc_data *vc) 723 { 724 vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity, 725 vc->state.blink, vc->state.underline, 726 vc->state.reverse ^ vc->vc_decscnm, vc->state.italic); 727 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color, 728 VCI_NORMAL, vc->state.blink, false, 729 vc->vc_decscnm, false) << 8); 730 } 731 732 /* Note: inverting the screen twice should revert to the original state */ 733 void invert_screen(struct vc_data *vc, int offset, int count, bool viewed) 734 { 735 u16 *p; 736 737 WARN_CONSOLE_UNLOCKED(); 738 739 count /= 2; 740 p = screenpos(vc, offset, viewed); 741 if (vc->vc_sw->con_invert_region) { 742 vc->vc_sw->con_invert_region(vc, p, count); 743 } else { 744 u16 *q = p; 745 int cnt = count; 746 u16 a; 747 748 if (!vc->vc_can_do_color) { 749 while (cnt--) { 750 a = scr_readw(q); 751 a ^= 0x0800; 752 scr_writew(a, q); 753 q++; 754 } 755 } else if (vc->vc_hi_font_mask == 0x100) { 756 while (cnt--) { 757 a = scr_readw(q); 758 a = (a & 0x11ff) | 759 ((a & 0xe000) >> 4) | 760 ((a & 0x0e00) << 4); 761 scr_writew(a, q); 762 q++; 763 } 764 } else { 765 while (cnt--) { 766 a = scr_readw(q); 767 a = (a & 0x88ff) | 768 ((a & 0x7000) >> 4) | 769 ((a & 0x0700) << 4); 770 scr_writew(a, q); 771 q++; 772 } 773 } 774 } 775 776 if (con_should_update(vc)) 777 do_update_region(vc, (unsigned long) p, count); 778 notify_update(vc); 779 } 780 781 /* used by selection: complement pointer position */ 782 void complement_pos(struct vc_data *vc, int offset) 783 { 784 static int old_offset = -1; 785 static unsigned short old; 786 static unsigned short oldx, oldy; 787 788 WARN_CONSOLE_UNLOCKED(); 789 790 if (old_offset != -1 && old_offset >= 0 && 791 old_offset < vc->vc_screenbuf_size) { 792 scr_writew(old, screenpos(vc, old_offset, true)); 793 if (con_should_update(vc)) 794 con_putc(vc, old, oldy, oldx); 795 notify_update(vc); 796 } 797 798 old_offset = offset; 799 800 if (offset != -1 && offset >= 0 && 801 offset < vc->vc_screenbuf_size) { 802 unsigned short new; 803 u16 *p = screenpos(vc, offset, true); 804 old = scr_readw(p); 805 new = old ^ vc->vc_complement_mask; 806 scr_writew(new, p); 807 if (con_should_update(vc)) { 808 oldx = (offset >> 1) % vc->vc_cols; 809 oldy = (offset >> 1) / vc->vc_cols; 810 con_putc(vc, new, oldy, oldx); 811 } 812 notify_update(vc); 813 } 814 } 815 816 static void insert_char(struct vc_data *vc, unsigned int nr) 817 { 818 unsigned short *p = (unsigned short *) vc->vc_pos; 819 820 vc_uniscr_insert(vc, nr); 821 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2); 822 scr_memsetw(p, vc->vc_video_erase_char, nr * 2); 823 vc->vc_need_wrap = 0; 824 if (con_should_update(vc)) 825 do_update_region(vc, (unsigned long) p, 826 vc->vc_cols - vc->state.x); 827 } 828 829 static void delete_char(struct vc_data *vc, unsigned int nr) 830 { 831 unsigned short *p = (unsigned short *) vc->vc_pos; 832 833 vc_uniscr_delete(vc, nr); 834 scr_memmovew(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2); 835 scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char, 836 nr * 2); 837 vc->vc_need_wrap = 0; 838 if (con_should_update(vc)) 839 do_update_region(vc, (unsigned long) p, 840 vc->vc_cols - vc->state.x); 841 } 842 843 static int softcursor_original = -1; 844 845 static void add_softcursor(struct vc_data *vc) 846 { 847 int i = scr_readw((u16 *) vc->vc_pos); 848 u32 type = vc->vc_cursor_type; 849 850 if (!(type & CUR_SW)) 851 return; 852 if (softcursor_original != -1) 853 return; 854 softcursor_original = i; 855 i |= CUR_SET(type); 856 i ^= CUR_CHANGE(type); 857 if ((type & CUR_ALWAYS_BG) && 858 (softcursor_original & CUR_BG) == (i & CUR_BG)) 859 i ^= CUR_BG; 860 if ((type & CUR_INVERT_FG_BG) && (i & CUR_FG) == ((i & CUR_BG) >> 4)) 861 i ^= CUR_FG; 862 scr_writew(i, (u16 *)vc->vc_pos); 863 if (con_should_update(vc)) 864 con_putc(vc, i, vc->state.y, vc->state.x); 865 } 866 867 static void hide_softcursor(struct vc_data *vc) 868 { 869 if (softcursor_original != -1) { 870 scr_writew(softcursor_original, (u16 *)vc->vc_pos); 871 if (con_should_update(vc)) 872 con_putc(vc, softcursor_original, vc->state.y, 873 vc->state.x); 874 softcursor_original = -1; 875 } 876 } 877 878 static void hide_cursor(struct vc_data *vc) 879 { 880 if (vc_is_sel(vc)) 881 clear_selection(); 882 883 vc->vc_sw->con_cursor(vc, false); 884 hide_softcursor(vc); 885 } 886 887 static void set_cursor(struct vc_data *vc) 888 { 889 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS) 890 return; 891 if (vc->vc_deccm) { 892 if (vc_is_sel(vc)) 893 clear_selection(); 894 add_softcursor(vc); 895 if (CUR_SIZE(vc->vc_cursor_type) != CUR_NONE) 896 vc->vc_sw->con_cursor(vc, true); 897 } else 898 hide_cursor(vc); 899 } 900 901 static void set_origin(struct vc_data *vc) 902 { 903 WARN_CONSOLE_UNLOCKED(); 904 905 if (!con_is_visible(vc) || 906 !vc->vc_sw->con_set_origin || 907 !vc->vc_sw->con_set_origin(vc)) 908 vc->vc_origin = (unsigned long)vc->vc_screenbuf; 909 vc->vc_visible_origin = vc->vc_origin; 910 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size; 911 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y + 912 2 * vc->state.x; 913 } 914 915 static void save_screen(struct vc_data *vc) 916 { 917 WARN_CONSOLE_UNLOCKED(); 918 919 if (vc->vc_sw->con_save_screen) 920 vc->vc_sw->con_save_screen(vc); 921 } 922 923 static void flush_scrollback(struct vc_data *vc) 924 { 925 WARN_CONSOLE_UNLOCKED(); 926 927 set_origin(vc); 928 if (!con_is_visible(vc)) 929 return; 930 931 /* 932 * The legacy way for flushing the scrollback buffer is to use a side 933 * effect of the con_switch method. We do it only on the foreground 934 * console as background consoles have no scrollback buffers in that 935 * case and we obviously don't want to switch to them. 936 */ 937 hide_cursor(vc); 938 vc->vc_sw->con_switch(vc); 939 set_cursor(vc); 940 } 941 942 /* 943 * Redrawing of screen 944 */ 945 946 void clear_buffer_attributes(struct vc_data *vc) 947 { 948 unsigned short *p = (unsigned short *)vc->vc_origin; 949 int count = vc->vc_screenbuf_size / 2; 950 int mask = vc->vc_hi_font_mask | 0xff; 951 952 for (; count > 0; count--, p++) { 953 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p); 954 } 955 } 956 957 void redraw_screen(struct vc_data *vc, int is_switch) 958 { 959 int redraw = 0; 960 961 WARN_CONSOLE_UNLOCKED(); 962 963 if (!vc) { 964 /* strange ... */ 965 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */ 966 return; 967 } 968 969 if (is_switch) { 970 struct vc_data *old_vc = vc_cons[fg_console].d; 971 if (old_vc == vc) 972 return; 973 if (!con_is_visible(vc)) 974 redraw = 1; 975 *vc->vc_display_fg = vc; 976 fg_console = vc->vc_num; 977 hide_cursor(old_vc); 978 if (!con_is_visible(old_vc)) { 979 save_screen(old_vc); 980 set_origin(old_vc); 981 } 982 if (tty0dev) 983 sysfs_notify(&tty0dev->kobj, NULL, "active"); 984 } else { 985 hide_cursor(vc); 986 redraw = 1; 987 } 988 989 if (redraw) { 990 bool update; 991 int old_was_color = vc->vc_can_do_color; 992 993 set_origin(vc); 994 update = vc->vc_sw->con_switch(vc); 995 set_palette(vc); 996 /* 997 * If console changed from mono<->color, the best we can do 998 * is to clear the buffer attributes. As it currently stands, 999 * rebuilding new attributes from the old buffer is not doable 1000 * without overly complex code. 1001 */ 1002 if (old_was_color != vc->vc_can_do_color) { 1003 update_attr(vc); 1004 clear_buffer_attributes(vc); 1005 } 1006 1007 if (update && vc->vc_mode != KD_GRAPHICS) 1008 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 1009 } 1010 set_cursor(vc); 1011 if (is_switch) { 1012 vt_set_leds_compute_shiftstate(); 1013 notify_update(vc); 1014 } 1015 } 1016 EXPORT_SYMBOL(redraw_screen); 1017 1018 /* 1019 * Allocation, freeing and resizing of VTs. 1020 */ 1021 1022 int vc_cons_allocated(unsigned int i) 1023 { 1024 return (i < MAX_NR_CONSOLES && vc_cons[i].d); 1025 } 1026 1027 static void visual_init(struct vc_data *vc, int num, bool init) 1028 { 1029 /* ++Geert: vc->vc_sw->con_init determines console size */ 1030 if (vc->vc_sw) 1031 module_put(vc->vc_sw->owner); 1032 vc->vc_sw = conswitchp; 1033 1034 if (con_driver_map[num]) 1035 vc->vc_sw = con_driver_map[num]; 1036 1037 __module_get(vc->vc_sw->owner); 1038 vc->vc_num = num; 1039 vc->vc_display_fg = &master_display_fg; 1040 if (vc->uni_pagedict_loc) 1041 con_free_unimap(vc); 1042 vc->uni_pagedict_loc = &vc->uni_pagedict; 1043 vc->uni_pagedict = NULL; 1044 vc->vc_hi_font_mask = 0; 1045 vc->vc_complement_mask = 0; 1046 vc->vc_can_do_color = 0; 1047 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 1048 vc->vc_sw->con_init(vc, init); 1049 if (!vc->vc_complement_mask) 1050 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800; 1051 vc->vc_s_complement_mask = vc->vc_complement_mask; 1052 vc->vc_size_row = vc->vc_cols << 1; 1053 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; 1054 } 1055 1056 1057 static void visual_deinit(struct vc_data *vc) 1058 { 1059 vc->vc_sw->con_deinit(vc); 1060 module_put(vc->vc_sw->owner); 1061 } 1062 1063 static void vc_port_destruct(struct tty_port *port) 1064 { 1065 struct vc_data *vc = container_of(port, struct vc_data, port); 1066 1067 kfree(vc); 1068 } 1069 1070 static const struct tty_port_operations vc_port_ops = { 1071 .destruct = vc_port_destruct, 1072 }; 1073 1074 /* 1075 * Change # of rows and columns (0 means unchanged/the size of fg_console) 1076 * [this is to be used together with some user program 1077 * like resize that changes the hardware videomode] 1078 */ 1079 #define VC_MAXCOL (32767) 1080 #define VC_MAXROW (32767) 1081 1082 int vc_allocate(unsigned int currcons) /* return 0 on success */ 1083 { 1084 struct vt_notifier_param param; 1085 struct vc_data *vc; 1086 int err; 1087 1088 WARN_CONSOLE_UNLOCKED(); 1089 1090 if (currcons >= MAX_NR_CONSOLES) 1091 return -ENXIO; 1092 1093 if (vc_cons[currcons].d) 1094 return 0; 1095 1096 /* due to the granularity of kmalloc, we waste some memory here */ 1097 /* the alloc is done in two steps, to optimize the common situation 1098 of a 25x80 console (structsize=216, screenbuf_size=4000) */ 1099 /* although the numbers above are not valid since long ago, the 1100 point is still up-to-date and the comment still has its value 1101 even if only as a historical artifact. --mj, July 1998 */ 1102 param.vc = vc = kzalloc_obj(struct vc_data); 1103 if (!vc) 1104 return -ENOMEM; 1105 1106 vc_cons[currcons].d = vc; 1107 tty_port_init(&vc->port); 1108 vc->port.ops = &vc_port_ops; 1109 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 1110 1111 visual_init(vc, currcons, true); 1112 1113 if (!*vc->uni_pagedict_loc) 1114 con_set_default_unimap(vc); 1115 1116 err = -EINVAL; 1117 if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW || 1118 vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size) 1119 goto err_free; 1120 err = -ENOMEM; 1121 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL); 1122 if (!vc->vc_screenbuf) 1123 goto err_free; 1124 1125 /* If no drivers have overridden us and the user didn't pass a 1126 boot option, default to displaying the cursor */ 1127 if (global_cursor_default == -1) 1128 global_cursor_default = 1; 1129 1130 vc_init(vc, 1); 1131 vcs_make_sysfs(currcons); 1132 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, ¶m); 1133 1134 return 0; 1135 err_free: 1136 visual_deinit(vc); 1137 kfree(vc); 1138 vc_cons[currcons].d = NULL; 1139 return err; 1140 } 1141 1142 static inline int resize_screen(struct vc_data *vc, int width, int height, 1143 bool from_user) 1144 { 1145 /* Resizes the resolution of the display adapater */ 1146 int err = 0; 1147 1148 if (vc->vc_sw->con_resize) 1149 err = vc->vc_sw->con_resize(vc, width, height, from_user); 1150 1151 return err; 1152 } 1153 1154 /** 1155 * vc_do_resize - resizing method for the tty 1156 * @tty: tty being resized 1157 * @vc: virtual console private data 1158 * @cols: columns 1159 * @lines: lines 1160 * @from_user: invoked by a user? 1161 * 1162 * Resize a virtual console, clipping according to the actual constraints. If 1163 * the caller passes a tty structure then update the termios winsize 1164 * information and perform any necessary signal handling. 1165 * 1166 * Locking: Caller must hold the console semaphore. Takes the termios rwsem and 1167 * ctrl.lock of the tty IFF a tty is passed. 1168 */ 1169 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, 1170 unsigned int cols, unsigned int lines, bool from_user) 1171 { 1172 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0; 1173 unsigned long end; 1174 unsigned int old_rows, old_row_size, first_copied_row; 1175 unsigned int new_cols, new_rows, new_row_size, new_screen_size; 1176 unsigned short *oldscreen, *newscreen; 1177 u32 **new_uniscr = NULL; 1178 1179 WARN_CONSOLE_UNLOCKED(); 1180 1181 if (cols > VC_MAXCOL || lines > VC_MAXROW) 1182 return -EINVAL; 1183 1184 new_cols = (cols ? cols : vc->vc_cols); 1185 new_rows = (lines ? lines : vc->vc_rows); 1186 new_row_size = new_cols << 1; 1187 new_screen_size = new_row_size * new_rows; 1188 1189 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) { 1190 /* 1191 * This function is being called here to cover the case 1192 * where the userspace calls the FBIOPUT_VSCREENINFO twice, 1193 * passing the same fb_var_screeninfo containing the fields 1194 * yres/xres equal to a number non-multiple of vc_font.height 1195 * and yres_virtual/xres_virtual equal to number lesser than the 1196 * vc_font.height and yres/xres. 1197 * In the second call, the struct fb_var_screeninfo isn't 1198 * being modified by the underlying driver because of the 1199 * if above, and this causes the fbcon_display->vrows to become 1200 * negative and it eventually leads to out-of-bound 1201 * access by the imageblit function. 1202 * To give the correct values to the struct and to not have 1203 * to deal with possible errors from the code below, we call 1204 * the resize_screen here as well. 1205 */ 1206 return resize_screen(vc, new_cols, new_rows, from_user); 1207 } 1208 1209 if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size) 1210 return -EINVAL; 1211 newscreen = kzalloc(new_screen_size, GFP_USER); 1212 if (!newscreen) 1213 return -ENOMEM; 1214 1215 if (vc->vc_uni_lines) { 1216 new_uniscr = vc_uniscr_alloc(new_cols, new_rows); 1217 if (!new_uniscr) { 1218 kfree(newscreen); 1219 return -ENOMEM; 1220 } 1221 } 1222 1223 if (vc_is_sel(vc)) 1224 clear_selection(); 1225 1226 old_rows = vc->vc_rows; 1227 old_row_size = vc->vc_size_row; 1228 1229 err = resize_screen(vc, new_cols, new_rows, from_user); 1230 if (err) { 1231 kfree(newscreen); 1232 vc_uniscr_free(new_uniscr); 1233 return err; 1234 } 1235 1236 vc->vc_rows = new_rows; 1237 vc->vc_cols = new_cols; 1238 vc->vc_size_row = new_row_size; 1239 vc->vc_screenbuf_size = new_screen_size; 1240 1241 rlth = min(old_row_size, new_row_size); 1242 rrem = new_row_size - rlth; 1243 old_origin = vc->vc_origin; 1244 new_origin = (long) newscreen; 1245 new_scr_end = new_origin + new_screen_size; 1246 1247 if (vc->state.y > new_rows) { 1248 if (old_rows - vc->state.y < new_rows) { 1249 /* 1250 * Cursor near the bottom, copy contents from the 1251 * bottom of buffer 1252 */ 1253 first_copied_row = (old_rows - new_rows); 1254 } else { 1255 /* 1256 * Cursor is in no man's land, copy 1/2 screenful 1257 * from the top and bottom of cursor position 1258 */ 1259 first_copied_row = (vc->state.y - new_rows/2); 1260 } 1261 old_origin += first_copied_row * old_row_size; 1262 } else 1263 first_copied_row = 0; 1264 end = old_origin + old_row_size * min(old_rows, new_rows); 1265 1266 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows, 1267 vc->vc_uni_lines, rlth/2, first_copied_row, 1268 min(old_rows, new_rows)); 1269 vc_uniscr_set(vc, new_uniscr); 1270 1271 update_attr(vc); 1272 1273 while (old_origin < end) { 1274 scr_memcpyw((unsigned short *) new_origin, 1275 (unsigned short *) old_origin, rlth); 1276 if (rrem) 1277 scr_memsetw((void *)(new_origin + rlth), 1278 vc->vc_video_erase_char, rrem); 1279 old_origin += old_row_size; 1280 new_origin += new_row_size; 1281 } 1282 if (new_scr_end > new_origin) 1283 scr_memsetw((void *)new_origin, vc->vc_video_erase_char, 1284 new_scr_end - new_origin); 1285 oldscreen = vc->vc_screenbuf; 1286 vc->vc_screenbuf = newscreen; 1287 vc->vc_screenbuf_size = new_screen_size; 1288 set_origin(vc); 1289 kfree(oldscreen); 1290 1291 /* do part of a reset_terminal() */ 1292 vc->vc_top = 0; 1293 vc->vc_bottom = vc->vc_rows; 1294 gotoxy(vc, vc->state.x, vc->state.y); 1295 save_cur(vc); 1296 1297 if (tty) { 1298 /* Rewrite the requested winsize data with the actual 1299 resulting sizes */ 1300 struct winsize ws; 1301 memset(&ws, 0, sizeof(ws)); 1302 ws.ws_row = vc->vc_rows; 1303 ws.ws_col = vc->vc_cols; 1304 ws.ws_ypixel = vc->vc_scan_lines; 1305 tty_do_resize(tty, &ws); 1306 } 1307 1308 if (con_is_visible(vc)) 1309 update_screen(vc); 1310 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num); 1311 notify_update(vc); 1312 return err; 1313 } 1314 1315 /** 1316 * __vc_resize - resize a VT 1317 * @vc: virtual console 1318 * @cols: columns 1319 * @rows: rows 1320 * @from_user: invoked by a user? 1321 * 1322 * Resize a virtual console as seen from the console end of things. We use the 1323 * common vc_do_resize() method to update the structures. 1324 * 1325 * Locking: The caller must hold the console sem to protect console internals 1326 * and @vc->port.tty. 1327 */ 1328 int __vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows, 1329 bool from_user) 1330 { 1331 return vc_do_resize(vc->port.tty, vc, cols, rows, from_user); 1332 } 1333 EXPORT_SYMBOL(__vc_resize); 1334 1335 /** 1336 * vt_resize - resize a VT 1337 * @tty: tty to resize 1338 * @ws: winsize attributes 1339 * 1340 * Resize a virtual terminal. This is called by the tty layer as we register 1341 * our own handler for resizing. The mutual helper does all the actual work. 1342 * 1343 * Locking: Takes the console sem and the called methods then take the tty 1344 * termios_rwsem and the tty ctrl.lock in that order. 1345 */ 1346 static int vt_resize(struct tty_struct *tty, struct winsize *ws) 1347 { 1348 struct vc_data *vc = tty->driver_data; 1349 1350 guard(console_lock)(); 1351 return vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false); 1352 } 1353 1354 struct vc_data *vc_deallocate(unsigned int currcons) 1355 { 1356 struct vc_data *vc = NULL; 1357 1358 WARN_CONSOLE_UNLOCKED(); 1359 1360 if (vc_cons_allocated(currcons)) { 1361 struct vt_notifier_param param; 1362 1363 param.vc = vc = vc_cons[currcons].d; 1364 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m); 1365 vcs_remove_sysfs(currcons); 1366 visual_deinit(vc); 1367 con_free_unimap(vc); 1368 put_pid(vc->vt_pid); 1369 vc_uniscr_set(vc, NULL); 1370 kfree(vc->vc_screenbuf); 1371 vc_cons[currcons].d = NULL; 1372 if (vc->vc_saved_screen != NULL) { 1373 kfree(vc->vc_saved_screen); 1374 vc->vc_saved_screen = NULL; 1375 } 1376 vc_uniscr_free(vc->vc_saved_uni_lines); 1377 vc->vc_saved_uni_lines = NULL; 1378 } 1379 return vc; 1380 } 1381 1382 /* 1383 * VT102 emulator 1384 */ 1385 1386 enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt}; 1387 1388 #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x)) 1389 #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x)) 1390 #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x)) 1391 1392 #define decarm VC_REPEAT 1393 #define decckm VC_CKMODE 1394 #define kbdapplic VC_APPLIC 1395 #define lnm VC_CRLF 1396 1397 const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7, 1398 8,12,10,14, 9,13,11,15 }; 1399 EXPORT_SYMBOL(color_table); 1400 1401 /* the default colour table, for VGA+ colour systems */ 1402 unsigned char default_red[] = { 1403 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 1404 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff 1405 }; 1406 module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR); 1407 EXPORT_SYMBOL(default_red); 1408 1409 unsigned char default_grn[] = { 1410 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa, 1411 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff 1412 }; 1413 module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR); 1414 EXPORT_SYMBOL(default_grn); 1415 1416 unsigned char default_blu[] = { 1417 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 1418 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff 1419 }; 1420 module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR); 1421 EXPORT_SYMBOL(default_blu); 1422 1423 /* 1424 * gotoxy() must verify all boundaries, because the arguments 1425 * might also be negative. If the given position is out of 1426 * bounds, the cursor is placed at the nearest margin. 1427 */ 1428 static void gotoxy(struct vc_data *vc, int new_x, int new_y) 1429 { 1430 int min_y, max_y; 1431 1432 if (new_x < 0) 1433 vc->state.x = 0; 1434 else { 1435 if (new_x >= vc->vc_cols) 1436 vc->state.x = vc->vc_cols - 1; 1437 else 1438 vc->state.x = new_x; 1439 } 1440 1441 if (vc->vc_decom) { 1442 min_y = vc->vc_top; 1443 max_y = vc->vc_bottom; 1444 } else { 1445 min_y = 0; 1446 max_y = vc->vc_rows; 1447 } 1448 if (new_y < min_y) 1449 vc->state.y = min_y; 1450 else if (new_y >= max_y) 1451 vc->state.y = max_y - 1; 1452 else 1453 vc->state.y = new_y; 1454 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row + 1455 (vc->state.x << 1); 1456 vc->vc_need_wrap = 0; 1457 } 1458 1459 /* for absolute user moves, when decom is set */ 1460 static void gotoxay(struct vc_data *vc, int new_x, int new_y) 1461 { 1462 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y); 1463 } 1464 1465 void scrollback(struct vc_data *vc) 1466 { 1467 scrolldelta(-(vc->vc_rows / 2)); 1468 } 1469 1470 void scrollfront(struct vc_data *vc, int lines) 1471 { 1472 if (!lines) 1473 lines = vc->vc_rows / 2; 1474 scrolldelta(lines); 1475 } 1476 1477 static void lf(struct vc_data *vc) 1478 { 1479 /* don't scroll if above bottom of scrolling region, or 1480 * if below scrolling region 1481 */ 1482 if (vc->state.y + 1 == vc->vc_bottom) 1483 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1); 1484 else if (vc->state.y < vc->vc_rows - 1) { 1485 vc->state.y++; 1486 vc->vc_pos += vc->vc_size_row; 1487 } 1488 vc->vc_need_wrap = 0; 1489 notify_write(vc, '\n'); 1490 } 1491 1492 static void ri(struct vc_data *vc) 1493 { 1494 /* don't scroll if below top of scrolling region, or 1495 * if above scrolling region 1496 */ 1497 if (vc->state.y == vc->vc_top) 1498 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1); 1499 else if (vc->state.y > 0) { 1500 vc->state.y--; 1501 vc->vc_pos -= vc->vc_size_row; 1502 } 1503 vc->vc_need_wrap = 0; 1504 } 1505 1506 static inline void cr(struct vc_data *vc) 1507 { 1508 vc->vc_pos -= vc->state.x << 1; 1509 vc->vc_need_wrap = vc->state.x = 0; 1510 notify_write(vc, '\r'); 1511 } 1512 1513 static inline void bs(struct vc_data *vc) 1514 { 1515 if (vc->state.x) { 1516 vc->vc_pos -= 2; 1517 vc->state.x--; 1518 vc->vc_need_wrap = 0; 1519 notify_write(vc, '\b'); 1520 } 1521 } 1522 1523 static inline void del(struct vc_data *vc) 1524 { 1525 /* ignored */ 1526 } 1527 1528 enum CSI_J { 1529 CSI_J_CURSOR_TO_END = 0, 1530 CSI_J_START_TO_CURSOR = 1, 1531 CSI_J_VISIBLE = 2, 1532 CSI_J_FULL = 3, 1533 }; 1534 1535 static void csi_J(struct vc_data *vc, enum CSI_J vpar) 1536 { 1537 unsigned short *start; 1538 unsigned int count; 1539 1540 switch (vpar) { 1541 case CSI_J_CURSOR_TO_END: 1542 vc_uniscr_clear_line(vc, vc->state.x, 1543 vc->vc_cols - vc->state.x); 1544 vc_uniscr_clear_lines(vc, vc->state.y + 1, 1545 vc->vc_rows - vc->state.y - 1); 1546 count = (vc->vc_scr_end - vc->vc_pos) >> 1; 1547 start = (unsigned short *)vc->vc_pos; 1548 break; 1549 case CSI_J_START_TO_CURSOR: 1550 vc_uniscr_clear_line(vc, 0, vc->state.x + 1); 1551 vc_uniscr_clear_lines(vc, 0, vc->state.y); 1552 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1; 1553 start = (unsigned short *)vc->vc_origin; 1554 break; 1555 case CSI_J_FULL: 1556 flush_scrollback(vc); 1557 fallthrough; 1558 case CSI_J_VISIBLE: 1559 vc_uniscr_clear_lines(vc, 0, vc->vc_rows); 1560 count = vc->vc_cols * vc->vc_rows; 1561 start = (unsigned short *)vc->vc_origin; 1562 break; 1563 default: 1564 return; 1565 } 1566 scr_memsetw(start, vc->vc_video_erase_char, 2 * count); 1567 if (con_should_update(vc)) 1568 do_update_region(vc, (unsigned long) start, count); 1569 vc->vc_need_wrap = 0; 1570 } 1571 1572 enum { 1573 CSI_K_CURSOR_TO_LINEEND = 0, 1574 CSI_K_LINESTART_TO_CURSOR = 1, 1575 CSI_K_LINE = 2, 1576 }; 1577 1578 static void csi_K(struct vc_data *vc) 1579 { 1580 unsigned int count; 1581 unsigned short *start = (unsigned short *)vc->vc_pos; 1582 int offset; 1583 1584 switch (vc->vc_par[0]) { 1585 case CSI_K_CURSOR_TO_LINEEND: 1586 offset = 0; 1587 count = vc->vc_cols - vc->state.x; 1588 break; 1589 case CSI_K_LINESTART_TO_CURSOR: 1590 offset = -vc->state.x; 1591 count = vc->state.x + 1; 1592 break; 1593 case CSI_K_LINE: 1594 offset = -vc->state.x; 1595 count = vc->vc_cols; 1596 break; 1597 default: 1598 return; 1599 } 1600 vc_uniscr_clear_line(vc, vc->state.x + offset, count); 1601 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count); 1602 vc->vc_need_wrap = 0; 1603 if (con_should_update(vc)) 1604 do_update_region(vc, (unsigned long)(start + offset), count); 1605 } 1606 1607 /* erase the following count positions */ 1608 static void csi_X(struct vc_data *vc) 1609 { /* not vt100? */ 1610 unsigned int count = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 1611 1612 vc_uniscr_clear_line(vc, vc->state.x, count); 1613 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count); 1614 if (con_should_update(vc)) 1615 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, count); 1616 vc->vc_need_wrap = 0; 1617 } 1618 1619 static void default_attr(struct vc_data *vc) 1620 { 1621 vc->state.intensity = VCI_NORMAL; 1622 vc->state.italic = false; 1623 vc->state.underline = false; 1624 vc->state.reverse = false; 1625 vc->state.blink = false; 1626 vc->state.color = vc->vc_def_color; 1627 } 1628 1629 struct rgb { u8 r; u8 g; u8 b; }; 1630 1631 static void rgb_from_256(unsigned int i, struct rgb *c) 1632 { 1633 if (i < 8) { /* Standard colours. */ 1634 c->r = i&1 ? 0xaa : 0x00; 1635 c->g = i&2 ? 0xaa : 0x00; 1636 c->b = i&4 ? 0xaa : 0x00; 1637 } else if (i < 16) { 1638 c->r = i&1 ? 0xff : 0x55; 1639 c->g = i&2 ? 0xff : 0x55; 1640 c->b = i&4 ? 0xff : 0x55; 1641 } else if (i < 232) { /* 6x6x6 colour cube. */ 1642 i -= 16; 1643 c->b = i % 6 * 255 / 6; 1644 i /= 6; 1645 c->g = i % 6 * 255 / 6; 1646 i /= 6; 1647 c->r = i * 255 / 6; 1648 } else /* Grayscale ramp. */ 1649 c->r = c->g = c->b = i * 10 - 2312; 1650 } 1651 1652 static void rgb_foreground(struct vc_data *vc, const struct rgb *c) 1653 { 1654 u8 hue = 0, max = max3(c->r, c->g, c->b); 1655 1656 if (c->r > max / 2) 1657 hue |= 4; 1658 if (c->g > max / 2) 1659 hue |= 2; 1660 if (c->b > max / 2) 1661 hue |= 1; 1662 1663 if (hue == 7 && max <= 0x55) { 1664 hue = 0; 1665 vc->state.intensity = VCI_BOLD; 1666 } else if (max > 0xaa) 1667 vc->state.intensity = VCI_BOLD; 1668 else 1669 vc->state.intensity = VCI_NORMAL; 1670 1671 vc->state.color = (vc->state.color & 0xf0) | hue; 1672 } 1673 1674 static void rgb_background(struct vc_data *vc, const struct rgb *c) 1675 { 1676 /* For backgrounds, err on the dark side. */ 1677 vc->state.color = (vc->state.color & 0x0f) 1678 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3; 1679 } 1680 1681 /* 1682 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes 1683 * and thus need to be detected and ignored by hand. 1684 * 1685 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in 1686 * supporting them. 1687 */ 1688 static int vc_t416_color(struct vc_data *vc, int i, 1689 void(*set_color)(struct vc_data *vc, const struct rgb *c)) 1690 { 1691 struct rgb c; 1692 1693 i++; 1694 if (i > vc->vc_npar) 1695 return i; 1696 1697 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) { 1698 /* 256 colours */ 1699 i++; 1700 rgb_from_256(vc->vc_par[i], &c); 1701 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) { 1702 /* 24 bit */ 1703 c.r = vc->vc_par[i + 1]; 1704 c.g = vc->vc_par[i + 2]; 1705 c.b = vc->vc_par[i + 3]; 1706 i += 3; 1707 } else 1708 return i; 1709 1710 set_color(vc, &c); 1711 1712 return i; 1713 } 1714 1715 enum { 1716 CSI_m_DEFAULT = 0, 1717 CSI_m_BOLD = 1, 1718 CSI_m_HALF_BRIGHT = 2, 1719 CSI_m_ITALIC = 3, 1720 CSI_m_UNDERLINE = 4, 1721 CSI_m_BLINK = 5, 1722 CSI_m_REVERSE = 7, 1723 CSI_m_PRI_FONT = 10, 1724 CSI_m_ALT_FONT1 = 11, 1725 CSI_m_ALT_FONT2 = 12, 1726 CSI_m_DOUBLE_UNDERLINE = 21, 1727 CSI_m_NORMAL_INTENSITY = 22, 1728 CSI_m_NO_ITALIC = 23, 1729 CSI_m_NO_UNDERLINE = 24, 1730 CSI_m_NO_BLINK = 25, 1731 CSI_m_NO_REVERSE = 27, 1732 CSI_m_FG_COLOR_BEG = 30, 1733 CSI_m_FG_COLOR_END = 37, 1734 CSI_m_FG_COLOR = 38, 1735 CSI_m_DEFAULT_FG_COLOR = 39, 1736 CSI_m_BG_COLOR_BEG = 40, 1737 CSI_m_BG_COLOR_END = 47, 1738 CSI_m_BG_COLOR = 48, 1739 CSI_m_DEFAULT_BG_COLOR = 49, 1740 CSI_m_UNDERLINE_COLOR = 58, 1741 CSI_m_BRIGHT_FG_COLOR_BEG = 90, 1742 CSI_m_BRIGHT_FG_COLOR_END = 97, 1743 CSI_m_BRIGHT_FG_COLOR_OFF = CSI_m_BRIGHT_FG_COLOR_BEG - CSI_m_FG_COLOR_BEG, 1744 CSI_m_BRIGHT_BG_COLOR_BEG = 100, 1745 CSI_m_BRIGHT_BG_COLOR_END = 107, 1746 CSI_m_BRIGHT_BG_COLOR_OFF = CSI_m_BRIGHT_BG_COLOR_BEG - CSI_m_BG_COLOR_BEG, 1747 }; 1748 1749 /* console_lock is held */ 1750 static void csi_m(struct vc_data *vc) 1751 { 1752 int i; 1753 1754 for (i = 0; i <= vc->vc_npar; i++) 1755 switch (vc->vc_par[i]) { 1756 case CSI_m_DEFAULT: /* all attributes off */ 1757 default_attr(vc); 1758 break; 1759 case CSI_m_BOLD: 1760 vc->state.intensity = VCI_BOLD; 1761 break; 1762 case CSI_m_HALF_BRIGHT: 1763 vc->state.intensity = VCI_HALF_BRIGHT; 1764 break; 1765 case CSI_m_ITALIC: 1766 vc->state.italic = true; 1767 break; 1768 case CSI_m_DOUBLE_UNDERLINE: 1769 /* 1770 * No console drivers support double underline, so 1771 * convert it to a single underline. 1772 */ 1773 case CSI_m_UNDERLINE: 1774 vc->state.underline = true; 1775 break; 1776 case CSI_m_BLINK: 1777 vc->state.blink = true; 1778 break; 1779 case CSI_m_REVERSE: 1780 vc->state.reverse = true; 1781 break; 1782 case CSI_m_PRI_FONT: /* ANSI X3.64-1979 (SCO-ish?) 1783 * Select primary font, don't display control chars if 1784 * defined, don't set bit 8 on output. 1785 */ 1786 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc); 1787 vc->vc_disp_ctrl = 0; 1788 vc->vc_toggle_meta = 0; 1789 break; 1790 case CSI_m_ALT_FONT1: /* ANSI X3.64-1979 (SCO-ish?) 1791 * Select first alternate font, lets chars < 32 be 1792 * displayed as ROM chars. 1793 */ 1794 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1795 vc->vc_disp_ctrl = 1; 1796 vc->vc_toggle_meta = 0; 1797 break; 1798 case CSI_m_ALT_FONT2: /* ANSI X3.64-1979 (SCO-ish?) 1799 * Select second alternate font, toggle high bit 1800 * before displaying as ROM char. 1801 */ 1802 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1803 vc->vc_disp_ctrl = 1; 1804 vc->vc_toggle_meta = 1; 1805 break; 1806 case CSI_m_NORMAL_INTENSITY: 1807 vc->state.intensity = VCI_NORMAL; 1808 break; 1809 case CSI_m_NO_ITALIC: 1810 vc->state.italic = false; 1811 break; 1812 case CSI_m_NO_UNDERLINE: 1813 vc->state.underline = false; 1814 break; 1815 case CSI_m_NO_BLINK: 1816 vc->state.blink = false; 1817 break; 1818 case CSI_m_NO_REVERSE: 1819 vc->state.reverse = false; 1820 break; 1821 case CSI_m_FG_COLOR: 1822 i = vc_t416_color(vc, i, rgb_foreground); 1823 break; 1824 case CSI_m_BG_COLOR: 1825 i = vc_t416_color(vc, i, rgb_background); 1826 break; 1827 case CSI_m_DEFAULT_FG_COLOR: 1828 vc->state.color = (vc->vc_def_color & 0x0f) | 1829 (vc->state.color & 0xf0); 1830 break; 1831 case CSI_m_DEFAULT_BG_COLOR: 1832 vc->state.color = (vc->vc_def_color & 0xf0) | 1833 (vc->state.color & 0x0f); 1834 break; 1835 case CSI_m_BRIGHT_FG_COLOR_BEG ... CSI_m_BRIGHT_FG_COLOR_END: 1836 vc->state.intensity = VCI_BOLD; 1837 vc->vc_par[i] -= CSI_m_BRIGHT_FG_COLOR_OFF; 1838 fallthrough; 1839 case CSI_m_FG_COLOR_BEG ... CSI_m_FG_COLOR_END: 1840 vc->vc_par[i] -= CSI_m_FG_COLOR_BEG; 1841 vc->state.color = color_table[vc->vc_par[i]] | 1842 (vc->state.color & 0xf0); 1843 break; 1844 case CSI_m_BRIGHT_BG_COLOR_BEG ... CSI_m_BRIGHT_BG_COLOR_END: 1845 vc->vc_par[i] -= CSI_m_BRIGHT_BG_COLOR_OFF; 1846 fallthrough; 1847 case CSI_m_BG_COLOR_BEG ... CSI_m_BG_COLOR_END: 1848 vc->vc_par[i] -= CSI_m_BG_COLOR_BEG; 1849 vc->state.color = (color_table[vc->vc_par[i]] << 4) | 1850 (vc->state.color & 0x0f); 1851 break; 1852 } 1853 update_attr(vc); 1854 } 1855 1856 static void respond_string(const char *p, size_t len, struct tty_port *port) 1857 { 1858 tty_insert_flip_string(port, p, len); 1859 tty_flip_buffer_push(port); 1860 } 1861 1862 static void cursor_report(struct vc_data *vc, struct tty_struct *tty) 1863 { 1864 char buf[40]; 1865 int len; 1866 1867 len = sprintf(buf, "\033[%d;%dR", vc->state.y + 1868 (vc->vc_decom ? vc->vc_top + 1 : 1), 1869 vc->state.x + 1); 1870 respond_string(buf, len, tty->port); 1871 } 1872 1873 static inline void status_report(struct tty_struct *tty) 1874 { 1875 static const char teminal_ok[] = "\033[0n"; 1876 1877 respond_string(teminal_ok, strlen(teminal_ok), tty->port); 1878 } 1879 1880 static inline void respond_ID(struct tty_struct *tty) 1881 { 1882 /* terminal answer to an ESC-Z or csi0c query. */ 1883 static const char vt102_id[] = "\033[?6c"; 1884 1885 respond_string(vt102_id, strlen(vt102_id), tty->port); 1886 } 1887 1888 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry) 1889 { 1890 char buf[8]; 1891 int len; 1892 1893 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), 1894 (char)('!' + mrx), (char)('!' + mry)); 1895 respond_string(buf, len, tty->port); 1896 } 1897 1898 /* invoked via ioctl(TIOCLINUX) and through set_selection_user */ 1899 int mouse_reporting(void) 1900 { 1901 return vc_cons[fg_console].d->vc_report_mouse; 1902 } 1903 1904 /* invoked via ioctl(TIOCLINUX) */ 1905 static int get_bracketed_paste(struct tty_struct *tty) 1906 { 1907 struct vc_data *vc = tty->driver_data; 1908 1909 return vc->vc_bracketed_paste; 1910 } 1911 1912 /* console_lock is held */ 1913 static void enter_alt_screen(struct vc_data *vc) 1914 { 1915 unsigned int size = vc->vc_rows * vc->vc_cols * 2; 1916 1917 if (vc->vc_saved_screen != NULL) 1918 return; /* Already inside an alt-screen */ 1919 vc->vc_saved_screen = kmemdup((u16 *)vc->vc_origin, size, GFP_KERNEL); 1920 if (vc->vc_saved_screen == NULL) 1921 return; 1922 vc->vc_saved_uni_lines = vc->vc_uni_lines; 1923 vc->vc_uni_lines = NULL; 1924 vc->vc_saved_rows = vc->vc_rows; 1925 vc->vc_saved_cols = vc->vc_cols; 1926 save_cur(vc); 1927 /* clear entire screen */ 1928 csi_J(vc, CSI_J_FULL); 1929 } 1930 1931 /* console_lock is held */ 1932 static void leave_alt_screen(struct vc_data *vc) 1933 { 1934 unsigned int rows = min(vc->vc_saved_rows, vc->vc_rows); 1935 unsigned int cols = min(vc->vc_saved_cols, vc->vc_cols); 1936 u16 *src, *dest; 1937 1938 if (vc->vc_saved_screen == NULL) 1939 return; /* Not inside an alt-screen */ 1940 for (unsigned int r = 0; r < rows; r++) { 1941 src = vc->vc_saved_screen + r * vc->vc_saved_cols; 1942 dest = ((u16 *)vc->vc_origin) + r * vc->vc_cols; 1943 memcpy(dest, src, 2 * cols); 1944 } 1945 /* 1946 * If the console was resized while in the alternate screen, 1947 * resize the saved unicode buffer to the current dimensions. 1948 * On allocation failure new_uniscr is NULL, causing the old 1949 * buffer to be freed and vc_uni_lines to be lazily rebuilt 1950 * via vc_uniscr_check() when next needed. 1951 */ 1952 if (vc->vc_saved_uni_lines && 1953 (vc->vc_saved_rows != vc->vc_rows || 1954 vc->vc_saved_cols != vc->vc_cols)) { 1955 u32 **new_uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 1956 1957 if (new_uniscr) 1958 vc_uniscr_copy_area(new_uniscr, vc->vc_cols, vc->vc_rows, 1959 vc->vc_saved_uni_lines, cols, 0, rows); 1960 vc_uniscr_free(vc->vc_saved_uni_lines); 1961 vc->vc_saved_uni_lines = new_uniscr; 1962 } 1963 vc_uniscr_set(vc, vc->vc_saved_uni_lines); 1964 vc->vc_saved_uni_lines = NULL; 1965 restore_cur(vc); 1966 /* Update the entire screen */ 1967 if (con_should_update(vc)) 1968 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 1969 kfree(vc->vc_saved_screen); 1970 vc->vc_saved_screen = NULL; 1971 } 1972 1973 enum { 1974 CSI_DEC_hl_CURSOR_KEYS = 1, /* CKM: cursor keys send ^[Ox/^[[x */ 1975 CSI_DEC_hl_132_COLUMNS = 3, /* COLM: 80/132 mode switch */ 1976 CSI_DEC_hl_REVERSE_VIDEO = 5, /* SCNM */ 1977 CSI_DEC_hl_ORIGIN_MODE = 6, /* OM: origin relative/absolute */ 1978 CSI_DEC_hl_AUTOWRAP = 7, /* AWM */ 1979 CSI_DEC_hl_AUTOREPEAT = 8, /* ARM */ 1980 CSI_DEC_hl_MOUSE_X10 = 9, 1981 CSI_DEC_hl_SHOW_CURSOR = 25, /* TCEM */ 1982 CSI_DEC_hl_MOUSE_VT200 = 1000, 1983 CSI_DEC_hl_ALT_SCREEN = 1049, 1984 CSI_DEC_hl_BRACKETED_PASTE = 2004, 1985 }; 1986 1987 /* console_lock is held */ 1988 static void csi_DEC_hl(struct vc_data *vc, bool on_off) 1989 { 1990 unsigned int i; 1991 1992 for (i = 0; i <= vc->vc_npar; i++) 1993 switch (vc->vc_par[i]) { 1994 case CSI_DEC_hl_CURSOR_KEYS: 1995 if (on_off) 1996 set_kbd(vc, decckm); 1997 else 1998 clr_kbd(vc, decckm); 1999 break; 2000 case CSI_DEC_hl_132_COLUMNS: /* unimplemented */ 2001 #if 0 2002 vc_resize(deccolm ? 132 : 80, vc->vc_rows); 2003 /* this alone does not suffice; some user mode 2004 utility has to change the hardware regs */ 2005 #endif 2006 break; 2007 case CSI_DEC_hl_REVERSE_VIDEO: 2008 if (vc->vc_decscnm != on_off) { 2009 vc->vc_decscnm = on_off; 2010 invert_screen(vc, 0, vc->vc_screenbuf_size, 2011 false); 2012 update_attr(vc); 2013 } 2014 break; 2015 case CSI_DEC_hl_ORIGIN_MODE: 2016 vc->vc_decom = on_off; 2017 gotoxay(vc, 0, 0); 2018 break; 2019 case CSI_DEC_hl_AUTOWRAP: 2020 vc->vc_decawm = on_off; 2021 break; 2022 case CSI_DEC_hl_AUTOREPEAT: 2023 if (on_off) 2024 set_kbd(vc, decarm); 2025 else 2026 clr_kbd(vc, decarm); 2027 break; 2028 case CSI_DEC_hl_MOUSE_X10: 2029 vc->vc_report_mouse = on_off ? 1 : 0; 2030 break; 2031 case CSI_DEC_hl_SHOW_CURSOR: 2032 vc->vc_deccm = on_off; 2033 break; 2034 case CSI_DEC_hl_MOUSE_VT200: 2035 vc->vc_report_mouse = on_off ? 2 : 0; 2036 break; 2037 case CSI_DEC_hl_BRACKETED_PASTE: 2038 vc->vc_bracketed_paste = on_off; 2039 break; 2040 case CSI_DEC_hl_ALT_SCREEN: 2041 if (on_off) 2042 enter_alt_screen(vc); 2043 else 2044 leave_alt_screen(vc); 2045 break; 2046 } 2047 } 2048 2049 enum { 2050 CSI_hl_DISPLAY_CTRL = 3, /* handle ansi control chars */ 2051 CSI_hl_INSERT = 4, /* IRM: insert/replace */ 2052 CSI_hl_AUTO_NL = 20, /* LNM: Enter == CrLf/Lf */ 2053 }; 2054 2055 /* console_lock is held */ 2056 static void csi_hl(struct vc_data *vc, bool on_off) 2057 { 2058 unsigned int i; 2059 2060 for (i = 0; i <= vc->vc_npar; i++) 2061 switch (vc->vc_par[i]) { /* ANSI modes set/reset */ 2062 case CSI_hl_DISPLAY_CTRL: 2063 vc->vc_disp_ctrl = on_off; 2064 break; 2065 case CSI_hl_INSERT: 2066 vc->vc_decim = on_off; 2067 break; 2068 case CSI_hl_AUTO_NL: 2069 if (on_off) 2070 set_kbd(vc, lnm); 2071 else 2072 clr_kbd(vc, lnm); 2073 break; 2074 } 2075 } 2076 2077 enum CSI_right_square_bracket { 2078 CSI_RSB_COLOR_FOR_UNDERLINE = 1, 2079 CSI_RSB_COLOR_FOR_HALF_BRIGHT = 2, 2080 CSI_RSB_MAKE_CUR_COLOR_DEFAULT = 8, 2081 CSI_RSB_BLANKING_INTERVAL = 9, 2082 CSI_RSB_BELL_FREQUENCY = 10, 2083 CSI_RSB_BELL_DURATION = 11, 2084 CSI_RSB_BRING_CONSOLE_TO_FRONT = 12, 2085 CSI_RSB_UNBLANK = 13, 2086 CSI_RSB_VESA_OFF_INTERVAL = 14, 2087 CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT = 15, 2088 CSI_RSB_CURSOR_BLINK_INTERVAL = 16, 2089 }; 2090 2091 /* 2092 * csi_RSB - csi+] (Right Square Bracket) handler 2093 * 2094 * These are linux console private sequences. 2095 * 2096 * console_lock is held 2097 */ 2098 static void csi_RSB(struct vc_data *vc) 2099 { 2100 switch (vc->vc_par[0]) { 2101 case CSI_RSB_COLOR_FOR_UNDERLINE: 2102 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2103 vc->vc_ulcolor = color_table[vc->vc_par[1]]; 2104 if (vc->state.underline) 2105 update_attr(vc); 2106 } 2107 break; 2108 case CSI_RSB_COLOR_FOR_HALF_BRIGHT: 2109 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2110 vc->vc_halfcolor = color_table[vc->vc_par[1]]; 2111 if (vc->state.intensity == VCI_HALF_BRIGHT) 2112 update_attr(vc); 2113 } 2114 break; 2115 case CSI_RSB_MAKE_CUR_COLOR_DEFAULT: 2116 vc->vc_def_color = vc->vc_attr; 2117 if (vc->vc_hi_font_mask == 0x100) 2118 vc->vc_def_color >>= 1; 2119 default_attr(vc); 2120 update_attr(vc); 2121 break; 2122 case CSI_RSB_BLANKING_INTERVAL: 2123 blankinterval = min(vc->vc_par[1], 60U) * 60; 2124 poke_blanked_console(); 2125 break; 2126 case CSI_RSB_BELL_FREQUENCY: 2127 if (vc->vc_npar >= 1) 2128 vc->vc_bell_pitch = vc->vc_par[1]; 2129 else 2130 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2131 break; 2132 case CSI_RSB_BELL_DURATION: 2133 if (vc->vc_npar >= 1) 2134 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ? 2135 msecs_to_jiffies(vc->vc_par[1]) : 0; 2136 else 2137 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2138 break; 2139 case CSI_RSB_BRING_CONSOLE_TO_FRONT: 2140 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1)) 2141 set_console(vc->vc_par[1] - 1); 2142 break; 2143 case CSI_RSB_UNBLANK: 2144 poke_blanked_console(); 2145 break; 2146 case CSI_RSB_VESA_OFF_INTERVAL: 2147 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ; 2148 break; 2149 case CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT: 2150 set_console(last_console); 2151 break; 2152 case CSI_RSB_CURSOR_BLINK_INTERVAL: 2153 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 && 2154 vc->vc_par[1] <= USHRT_MAX) 2155 vc->vc_cur_blink_ms = vc->vc_par[1]; 2156 else 2157 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2158 break; 2159 } 2160 } 2161 2162 /* console_lock is held */ 2163 static void csi_at(struct vc_data *vc, unsigned int nr) 2164 { 2165 nr = clamp(nr, 1, vc->vc_cols - vc->state.x); 2166 insert_char(vc, nr); 2167 } 2168 2169 /* console_lock is held */ 2170 static void csi_L(struct vc_data *vc) 2171 { 2172 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2173 2174 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr); 2175 vc->vc_need_wrap = 0; 2176 } 2177 2178 /* console_lock is held */ 2179 static void csi_P(struct vc_data *vc) 2180 { 2181 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 2182 2183 delete_char(vc, nr); 2184 } 2185 2186 /* console_lock is held */ 2187 static void csi_M(struct vc_data *vc) 2188 { 2189 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2190 2191 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr); 2192 vc->vc_need_wrap = 0; 2193 } 2194 2195 /* console_lock is held (except via vc_init->reset_terminal */ 2196 static void save_cur(struct vc_data *vc) 2197 { 2198 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state)); 2199 } 2200 2201 /* console_lock is held */ 2202 static void restore_cur(struct vc_data *vc) 2203 { 2204 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state)); 2205 2206 gotoxy(vc, vc->state.x, vc->state.y); 2207 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], 2208 vc); 2209 update_attr(vc); 2210 vc->vc_need_wrap = 0; 2211 } 2212 2213 /** 2214 * enum vc_ctl_state - control characters state of a vt 2215 * 2216 * @ESnormal: initial state, no control characters parsed 2217 * @ESesc: ESC parsed 2218 * @ESsquare: CSI parsed -- modifiers/parameters/ctrl chars expected 2219 * @ESgetpars: CSI parsed -- parameters/ctrl chars expected 2220 * @ESgetsubpars: CSI m parsed -- subparameters expected 2221 * @ESfunckey: CSI [ parsed 2222 * @EShash: ESC # parsed 2223 * @ESsetG0: ESC ( parsed 2224 * @ESsetG1: ESC ) parsed 2225 * @ESpercent: ESC % parsed 2226 * @EScsiignore: CSI [0x20-0x3f] parsed 2227 * @ESnonstd: OSC parsed 2228 * @ESpalette: OSC P parsed 2229 * @ESosc: OSC [0-9] parsed 2230 * @ESANSI_first: first state for ignoring ansi control sequences 2231 * @ESapc: ESC _ parsed 2232 * @ESpm: ESC ^ parsed 2233 * @ESdcs: ESC P parsed 2234 * @ESANSI_last: last state for ignoring ansi control sequences 2235 */ 2236 enum vc_ctl_state { 2237 ESnormal, 2238 ESesc, 2239 ESsquare, 2240 ESgetpars, 2241 ESgetsubpars, 2242 ESfunckey, 2243 EShash, 2244 ESsetG0, 2245 ESsetG1, 2246 ESpercent, 2247 EScsiignore, 2248 ESnonstd, 2249 ESpalette, 2250 ESosc, 2251 ESANSI_first = ESosc, 2252 ESapc, 2253 ESpm, 2254 ESdcs, 2255 ESANSI_last = ESdcs, 2256 }; 2257 2258 /* console_lock is held (except via vc_init()) */ 2259 static void reset_terminal(struct vc_data *vc, int do_clear) 2260 { 2261 unsigned int i; 2262 2263 vc->vc_top = 0; 2264 vc->vc_bottom = vc->vc_rows; 2265 vc->vc_state = ESnormal; 2266 vc->vc_priv = EPecma; 2267 vc->vc_translate = set_translate(LAT1_MAP, vc); 2268 vc->state.Gx_charset[0] = LAT1_MAP; 2269 vc->state.Gx_charset[1] = GRAF_MAP; 2270 vc->state.charset = 0; 2271 vc->vc_need_wrap = 0; 2272 vc->vc_report_mouse = 0; 2273 vc->vc_bracketed_paste = 0; 2274 vc->vc_utf = default_utf8; 2275 vc->vc_utf_count = 0; 2276 2277 vc->vc_disp_ctrl = 0; 2278 vc->vc_toggle_meta = 0; 2279 2280 vc->vc_decscnm = 0; 2281 vc->vc_decom = 0; 2282 vc->vc_decawm = 1; 2283 vc->vc_deccm = global_cursor_default; 2284 vc->vc_decim = 0; 2285 2286 if (vc->vc_saved_screen != NULL) { 2287 kfree(vc->vc_saved_screen); 2288 vc->vc_saved_screen = NULL; 2289 vc_uniscr_free(vc->vc_saved_uni_lines); 2290 vc->vc_saved_uni_lines = NULL; 2291 vc->vc_saved_rows = 0; 2292 vc->vc_saved_cols = 0; 2293 } 2294 2295 vt_reset_keyboard(vc->vc_num); 2296 2297 vc->vc_cursor_type = cur_default; 2298 vc->vc_complement_mask = vc->vc_s_complement_mask; 2299 2300 default_attr(vc); 2301 update_attr(vc); 2302 2303 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2304 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8) 2305 set_bit(i, vc->vc_tab_stop); 2306 2307 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2308 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2309 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2310 2311 gotoxy(vc, 0, 0); 2312 save_cur(vc); 2313 if (do_clear) 2314 csi_J(vc, CSI_J_VISIBLE); 2315 } 2316 2317 static void vc_setGx(struct vc_data *vc, unsigned int which, u8 c) 2318 { 2319 unsigned char *charset = &vc->state.Gx_charset[which]; 2320 2321 switch (c) { 2322 case '0': 2323 *charset = GRAF_MAP; 2324 break; 2325 case 'B': 2326 *charset = LAT1_MAP; 2327 break; 2328 case 'U': 2329 *charset = IBMPC_MAP; 2330 break; 2331 case 'K': 2332 *charset = USER_MAP; 2333 break; 2334 } 2335 2336 if (vc->state.charset == which) 2337 vc->vc_translate = set_translate(*charset, vc); 2338 } 2339 2340 static bool ansi_control_string(enum vc_ctl_state state) 2341 { 2342 return state >= ESANSI_first && state <= ESANSI_last; 2343 } 2344 2345 enum { 2346 ASCII_NULL = 0, 2347 ASCII_BELL = 7, 2348 ASCII_BACKSPACE = 8, 2349 ASCII_IGNORE_FIRST = ASCII_BACKSPACE, 2350 ASCII_HTAB = 9, 2351 ASCII_LINEFEED = 10, 2352 ASCII_VTAB = 11, 2353 ASCII_FORMFEED = 12, 2354 ASCII_CAR_RET = 13, 2355 ASCII_IGNORE_LAST = ASCII_CAR_RET, 2356 ASCII_SHIFTOUT = 14, 2357 ASCII_SHIFTIN = 15, 2358 ASCII_CANCEL = 24, 2359 ASCII_SUBSTITUTE = 26, 2360 ASCII_ESCAPE = 27, 2361 ASCII_CSI_IGNORE_FIRST = ' ', /* 0x2x, 0x3a and 0x3c - 0x3f */ 2362 ASCII_CSI_IGNORE_LAST = '?', 2363 ASCII_DEL = 127, 2364 ASCII_EXT_CSI = 128 + ASCII_ESCAPE, 2365 }; 2366 2367 /* 2368 * Handle ascii characters in control sequences and change states accordingly. 2369 * E.g. ESC sets the state of vc to ESesc. 2370 * 2371 * Returns: true if @c handled. 2372 */ 2373 static bool handle_ascii(struct tty_struct *tty, struct vc_data *vc, u8 c) 2374 { 2375 switch (c) { 2376 case ASCII_NULL: 2377 return true; 2378 case ASCII_BELL: 2379 if (ansi_control_string(vc->vc_state)) 2380 vc->vc_state = ESnormal; 2381 else if (vc->vc_bell_duration) 2382 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration); 2383 return true; 2384 case ASCII_BACKSPACE: 2385 bs(vc); 2386 return true; 2387 case ASCII_HTAB: 2388 vc->vc_pos -= (vc->state.x << 1); 2389 2390 vc->state.x = find_next_bit(vc->vc_tab_stop, 2391 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT), 2392 vc->state.x + 1); 2393 if (vc->state.x >= VC_TABSTOPS_COUNT) 2394 vc->state.x = vc->vc_cols - 1; 2395 2396 vc->vc_pos += (vc->state.x << 1); 2397 notify_write(vc, '\t'); 2398 return true; 2399 case ASCII_LINEFEED: 2400 case ASCII_VTAB: 2401 case ASCII_FORMFEED: 2402 lf(vc); 2403 if (!is_kbd(vc, lnm)) 2404 return true; 2405 fallthrough; 2406 case ASCII_CAR_RET: 2407 cr(vc); 2408 return true; 2409 case ASCII_SHIFTOUT: 2410 vc->state.charset = 1; 2411 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc); 2412 vc->vc_disp_ctrl = 1; 2413 return true; 2414 case ASCII_SHIFTIN: 2415 vc->state.charset = 0; 2416 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc); 2417 vc->vc_disp_ctrl = 0; 2418 return true; 2419 case ASCII_CANCEL: 2420 case ASCII_SUBSTITUTE: 2421 vc->vc_state = ESnormal; 2422 return true; 2423 case ASCII_ESCAPE: 2424 vc->vc_state = ESesc; 2425 return true; 2426 case ASCII_DEL: 2427 del(vc); 2428 return true; 2429 case ASCII_EXT_CSI: 2430 vc->vc_state = ESsquare; 2431 return true; 2432 } 2433 2434 return false; 2435 } 2436 2437 /* 2438 * Handle a character (@c) following an ESC (when @vc is in the ESesc state). 2439 * E.g. previous ESC with @c == '[' here yields the ESsquare state (that is: 2440 * CSI). 2441 */ 2442 static void handle_esc(struct tty_struct *tty, struct vc_data *vc, u8 c) 2443 { 2444 vc->vc_state = ESnormal; 2445 switch (c) { 2446 case '[': 2447 vc->vc_state = ESsquare; 2448 break; 2449 case ']': 2450 vc->vc_state = ESnonstd; 2451 break; 2452 case '_': 2453 vc->vc_state = ESapc; 2454 break; 2455 case '^': 2456 vc->vc_state = ESpm; 2457 break; 2458 case '%': 2459 vc->vc_state = ESpercent; 2460 break; 2461 case 'E': 2462 cr(vc); 2463 lf(vc); 2464 break; 2465 case 'M': 2466 ri(vc); 2467 break; 2468 case 'D': 2469 lf(vc); 2470 break; 2471 case 'H': 2472 if (vc->state.x < VC_TABSTOPS_COUNT) 2473 set_bit(vc->state.x, vc->vc_tab_stop); 2474 break; 2475 case 'P': 2476 vc->vc_state = ESdcs; 2477 break; 2478 case 'Z': 2479 respond_ID(tty); 2480 break; 2481 case '7': 2482 save_cur(vc); 2483 break; 2484 case '8': 2485 restore_cur(vc); 2486 break; 2487 case '(': 2488 vc->vc_state = ESsetG0; 2489 break; 2490 case ')': 2491 vc->vc_state = ESsetG1; 2492 break; 2493 case '#': 2494 vc->vc_state = EShash; 2495 break; 2496 case 'c': 2497 reset_terminal(vc, 1); 2498 break; 2499 case '>': /* Numeric keypad */ 2500 clr_kbd(vc, kbdapplic); 2501 break; 2502 case '=': /* Appl. keypad */ 2503 set_kbd(vc, kbdapplic); 2504 break; 2505 } 2506 } 2507 2508 /* 2509 * Handle special DEC control sequences ("ESC [ ? parameters char"). Parameters 2510 * are in @vc->vc_par and the char is in @c here. 2511 */ 2512 static void csi_DEC(struct tty_struct *tty, struct vc_data *vc, u8 c) 2513 { 2514 switch (c) { 2515 case 'h': 2516 csi_DEC_hl(vc, true); 2517 break; 2518 case 'l': 2519 csi_DEC_hl(vc, false); 2520 break; 2521 case 'c': 2522 if (vc->vc_par[0]) 2523 vc->vc_cursor_type = CUR_MAKE(vc->vc_par[0], 2524 vc->vc_par[1], 2525 vc->vc_par[2]); 2526 else 2527 vc->vc_cursor_type = cur_default; 2528 break; 2529 case 'm': 2530 clear_selection(); 2531 if (vc->vc_par[0]) 2532 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1]; 2533 else 2534 vc->vc_complement_mask = vc->vc_s_complement_mask; 2535 break; 2536 case 'n': 2537 if (vc->vc_par[0] == 5) 2538 status_report(tty); 2539 else if (vc->vc_par[0] == 6) 2540 cursor_report(vc, tty); 2541 break; 2542 } 2543 } 2544 2545 /* 2546 * Handle Control Sequence Introducer control characters. That is 2547 * "ESC [ parameters char". Parameters are in @vc->vc_par and the char is in 2548 * @c here. 2549 */ 2550 static void csi_ECMA(struct tty_struct *tty, struct vc_data *vc, u8 c) 2551 { 2552 switch (c) { 2553 case 'G': 2554 case '`': 2555 if (vc->vc_par[0]) 2556 vc->vc_par[0]--; 2557 gotoxy(vc, vc->vc_par[0], vc->state.y); 2558 break; 2559 case 'A': 2560 if (!vc->vc_par[0]) 2561 vc->vc_par[0]++; 2562 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]); 2563 break; 2564 case 'B': 2565 case 'e': 2566 if (!vc->vc_par[0]) 2567 vc->vc_par[0]++; 2568 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]); 2569 break; 2570 case 'C': 2571 case 'a': 2572 if (!vc->vc_par[0]) 2573 vc->vc_par[0]++; 2574 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y); 2575 break; 2576 case 'D': 2577 if (!vc->vc_par[0]) 2578 vc->vc_par[0]++; 2579 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y); 2580 break; 2581 case 'E': 2582 if (!vc->vc_par[0]) 2583 vc->vc_par[0]++; 2584 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]); 2585 break; 2586 case 'F': 2587 if (!vc->vc_par[0]) 2588 vc->vc_par[0]++; 2589 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]); 2590 break; 2591 case 'd': 2592 if (vc->vc_par[0]) 2593 vc->vc_par[0]--; 2594 gotoxay(vc, vc->state.x ,vc->vc_par[0]); 2595 break; 2596 case 'H': 2597 case 'f': 2598 if (vc->vc_par[0]) 2599 vc->vc_par[0]--; 2600 if (vc->vc_par[1]) 2601 vc->vc_par[1]--; 2602 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]); 2603 break; 2604 case 'J': 2605 csi_J(vc, vc->vc_par[0]); 2606 break; 2607 case 'K': 2608 csi_K(vc); 2609 break; 2610 case 'L': 2611 csi_L(vc); 2612 break; 2613 case 'M': 2614 csi_M(vc); 2615 break; 2616 case 'P': 2617 csi_P(vc); 2618 break; 2619 case 'c': 2620 if (!vc->vc_par[0]) 2621 respond_ID(tty); 2622 break; 2623 case 'g': 2624 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT) 2625 set_bit(vc->state.x, vc->vc_tab_stop); 2626 else if (vc->vc_par[0] == 3) 2627 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2628 break; 2629 case 'h': 2630 csi_hl(vc, true); 2631 break; 2632 case 'l': 2633 csi_hl(vc, false); 2634 break; 2635 case 'm': 2636 csi_m(vc); 2637 break; 2638 case 'n': 2639 if (vc->vc_par[0] == 5) 2640 status_report(tty); 2641 else if (vc->vc_par[0] == 6) 2642 cursor_report(vc, tty); 2643 break; 2644 case 'q': /* DECLL - but only 3 leds */ 2645 /* map 0,1,2,3 to 0,1,2,4 */ 2646 if (vc->vc_par[0] < 4) 2647 vt_set_led_state(vc->vc_num, 2648 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4); 2649 break; 2650 case 'r': 2651 if (!vc->vc_par[0]) 2652 vc->vc_par[0]++; 2653 if (!vc->vc_par[1]) 2654 vc->vc_par[1] = vc->vc_rows; 2655 /* Minimum allowed region is 2 lines */ 2656 if (vc->vc_par[0] < vc->vc_par[1] && 2657 vc->vc_par[1] <= vc->vc_rows) { 2658 vc->vc_top = vc->vc_par[0] - 1; 2659 vc->vc_bottom = vc->vc_par[1]; 2660 gotoxay(vc, 0, 0); 2661 } 2662 break; 2663 case 's': 2664 save_cur(vc); 2665 break; 2666 case 'u': 2667 restore_cur(vc); 2668 break; 2669 case 'X': 2670 csi_X(vc); 2671 break; 2672 case '@': 2673 csi_at(vc, vc->vc_par[0]); 2674 break; 2675 case ']': 2676 csi_RSB(vc); 2677 break; 2678 } 2679 2680 } 2681 2682 static void vc_reset_params(struct vc_data *vc) 2683 { 2684 memset(vc->vc_par, 0, sizeof(vc->vc_par)); 2685 vc->vc_npar = 0; 2686 } 2687 2688 /* console_lock is held */ 2689 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, u8 c) 2690 { 2691 /* 2692 * Control characters can be used in the _middle_ 2693 * of an escape sequence, aside from ANSI control strings. 2694 */ 2695 if (ansi_control_string(vc->vc_state) && c >= ASCII_IGNORE_FIRST && 2696 c <= ASCII_IGNORE_LAST) 2697 return; 2698 2699 if (handle_ascii(tty, vc, c)) 2700 return; 2701 2702 switch(vc->vc_state) { 2703 case ESesc: /* ESC */ 2704 handle_esc(tty, vc, c); 2705 return; 2706 case ESnonstd: /* ESC ] aka OSC */ 2707 switch (c) { 2708 case 'P': /* palette escape sequence */ 2709 vc_reset_params(vc); 2710 vc->vc_state = ESpalette; 2711 return; 2712 case 'R': /* reset palette */ 2713 reset_palette(vc); 2714 break; 2715 case '0' ... '9': 2716 vc->vc_state = ESosc; 2717 return; 2718 } 2719 vc->vc_state = ESnormal; 2720 return; 2721 case ESpalette: /* ESC ] P aka OSC P */ 2722 if (isxdigit(c)) { 2723 vc->vc_par[vc->vc_npar++] = hex_to_bin(c); 2724 if (vc->vc_npar == 7) { 2725 int i = vc->vc_par[0] * 3, j = 1; 2726 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2727 vc->vc_palette[i++] += vc->vc_par[j++]; 2728 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2729 vc->vc_palette[i++] += vc->vc_par[j++]; 2730 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2731 vc->vc_palette[i] += vc->vc_par[j]; 2732 set_palette(vc); 2733 vc->vc_state = ESnormal; 2734 } 2735 } else 2736 vc->vc_state = ESnormal; 2737 return; 2738 case ESsquare: /* ESC [ aka CSI, parameters or modifiers expected */ 2739 vc_reset_params(vc); 2740 2741 vc->vc_state = ESgetpars; 2742 switch (c) { 2743 case '[': /* Function key */ 2744 vc->vc_state = ESfunckey; 2745 return; 2746 case '?': 2747 vc->vc_priv = EPdec; 2748 return; 2749 case '>': 2750 vc->vc_priv = EPgt; 2751 return; 2752 case '=': 2753 vc->vc_priv = EPeq; 2754 return; 2755 case '<': 2756 vc->vc_priv = EPlt; 2757 return; 2758 } 2759 vc->vc_priv = EPecma; 2760 fallthrough; 2761 case ESgetpars: /* ESC [ aka CSI, parameters expected */ 2762 switch (c) { 2763 case ':': /* ITU-T T.416 color subparameters */ 2764 if (vc->vc_par[vc->vc_npar] == CSI_m_FG_COLOR || 2765 vc->vc_par[vc->vc_npar] == CSI_m_BG_COLOR || 2766 vc->vc_par[vc->vc_npar] == CSI_m_UNDERLINE_COLOR) 2767 vc->vc_state = ESgetsubpars; 2768 else 2769 break; 2770 fallthrough; 2771 case ';': 2772 if (vc->vc_npar < NPAR - 1) { 2773 vc->vc_npar++; 2774 return; 2775 } 2776 break; 2777 case '0' ... '9': 2778 vc->vc_par[vc->vc_npar] *= 10; 2779 vc->vc_par[vc->vc_npar] += c - '0'; 2780 return; 2781 } 2782 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2783 vc->vc_state = EScsiignore; 2784 return; 2785 } 2786 2787 /* parameters done, handle the control char @c */ 2788 2789 vc->vc_state = ESnormal; 2790 2791 switch (vc->vc_priv) { 2792 case EPdec: 2793 csi_DEC(tty, vc, c); 2794 return; 2795 case EPecma: 2796 csi_ECMA(tty, vc, c); 2797 return; 2798 default: 2799 return; 2800 } 2801 case ESgetsubpars: /* ESC [ 38/48/58, subparameters expected */ 2802 switch (c) { 2803 case ':': 2804 case ';': 2805 if (vc->vc_npar < NPAR - 1) { 2806 vc->vc_npar++; 2807 return; 2808 } 2809 break; 2810 case '0' ... '9': 2811 vc->vc_par[vc->vc_npar] *= 10; 2812 vc->vc_par[vc->vc_npar] += c - '0'; 2813 return; 2814 } 2815 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2816 vc->vc_state = EScsiignore; 2817 return; 2818 } 2819 2820 /* parameters done, handle the control char @c */ 2821 2822 vc->vc_state = ESnormal; 2823 2824 switch (vc->vc_priv) { 2825 case EPdec: 2826 csi_DEC(tty, vc, c); 2827 return; 2828 case EPecma: 2829 csi_ECMA(tty, vc, c); 2830 return; 2831 default: 2832 return; 2833 } 2834 case EScsiignore: 2835 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) 2836 return; 2837 vc->vc_state = ESnormal; 2838 return; 2839 case ESpercent: /* ESC % */ 2840 vc->vc_state = ESnormal; 2841 switch (c) { 2842 case '@': /* defined in ISO 2022 */ 2843 vc->vc_utf = 0; 2844 return; 2845 case 'G': /* prelim official escape code */ 2846 case '8': /* retained for compatibility */ 2847 vc->vc_utf = 1; 2848 return; 2849 } 2850 return; 2851 case ESfunckey: /* ESC [ [ aka CSI [ */ 2852 vc->vc_state = ESnormal; 2853 return; 2854 case EShash: /* ESC # */ 2855 vc->vc_state = ESnormal; 2856 if (c == '8') { 2857 /* DEC screen alignment test. kludge :-) */ 2858 vc->vc_video_erase_char = 2859 (vc->vc_video_erase_char & 0xff00) | 'E'; 2860 csi_J(vc, CSI_J_VISIBLE); 2861 vc->vc_video_erase_char = 2862 (vc->vc_video_erase_char & 0xff00) | ' '; 2863 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 2864 } 2865 return; 2866 case ESsetG0: /* ESC ( */ 2867 vc_setGx(vc, 0, c); 2868 vc->vc_state = ESnormal; 2869 return; 2870 case ESsetG1: /* ESC ) */ 2871 vc_setGx(vc, 1, c); 2872 vc->vc_state = ESnormal; 2873 return; 2874 case ESapc: /* ESC _ */ 2875 return; 2876 case ESosc: /* ESC ] [0-9] aka OSC [0-9] */ 2877 return; 2878 case ESpm: /* ESC ^ */ 2879 return; 2880 case ESdcs: /* ESC P */ 2881 return; 2882 default: 2883 vc->vc_state = ESnormal; 2884 } 2885 } 2886 2887 struct vc_draw_region { 2888 unsigned long from, to; 2889 int x; 2890 }; 2891 2892 static void con_flush(struct vc_data *vc, struct vc_draw_region *draw) 2893 { 2894 if (draw->x < 0) 2895 return; 2896 2897 vc->vc_sw->con_putcs(vc, (u16 *)draw->from, 2898 (u16 *)draw->to - (u16 *)draw->from, vc->state.y, 2899 draw->x); 2900 draw->x = -1; 2901 } 2902 2903 static inline int vc_translate_ascii(const struct vc_data *vc, int c) 2904 { 2905 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) { 2906 if (vc->vc_toggle_meta) 2907 c |= 0x80; 2908 2909 return vc->vc_translate[c]; 2910 } 2911 2912 return c; 2913 } 2914 2915 2916 /** 2917 * vc_sanitize_unicode - Replace invalid Unicode code points with ``U+FFFD`` 2918 * @c: the received code point 2919 */ 2920 static inline int vc_sanitize_unicode(const int c) 2921 { 2922 if (c >= 0xd800 && c <= 0xdfff) 2923 return 0xfffd; 2924 2925 return c; 2926 } 2927 2928 /** 2929 * vc_translate_unicode - Combine UTF-8 into Unicode in &vc_data.vc_utf_char 2930 * @vc: virtual console 2931 * @c: UTF-8 byte to translate 2932 * @rescan: set to true iff @c wasn't consumed here and needs to be re-processed 2933 * 2934 * * &vc_data.vc_utf_char is the being-constructed Unicode code point. 2935 * * &vc_data.vc_utf_count is the number of continuation bytes still expected to 2936 * arrive. 2937 * * &vc_data.vc_npar is the number of continuation bytes arrived so far. 2938 * 2939 * Return: 2940 * * %-1 - Input OK so far, @c consumed, further bytes expected. 2941 * * %0xFFFD - Possibility 1: input invalid, @c may have been consumed (see 2942 * desc. of @rescan). Possibility 2: input OK, @c consumed, 2943 * ``U+FFFD`` is the resulting code point. ``U+FFFD`` is valid, 2944 * ``REPLACEMENT CHARACTER``. 2945 * * otherwise - Input OK, @c consumed, resulting code point returned. 2946 */ 2947 static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan) 2948 { 2949 static const u32 utf8_length_changes[] = {0x7f, 0x7ff, 0xffff, 0x10ffff}; 2950 2951 /* Continuation byte received */ 2952 if ((c & 0xc0) == 0x80) { 2953 /* Unexpected continuation byte? */ 2954 if (!vc->vc_utf_count) 2955 goto bad_sequence; 2956 2957 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f); 2958 vc->vc_npar++; 2959 if (--vc->vc_utf_count) 2960 goto need_more_bytes; 2961 2962 /* Got a whole character */ 2963 c = vc->vc_utf_char; 2964 /* Reject overlong sequences */ 2965 if (c <= utf8_length_changes[vc->vc_npar - 1] || 2966 c > utf8_length_changes[vc->vc_npar]) 2967 goto bad_sequence; 2968 2969 return vc_sanitize_unicode(c); 2970 } 2971 2972 /* Single ASCII byte or first byte of a sequence received */ 2973 if (vc->vc_utf_count) { 2974 /* A continuation byte was expected */ 2975 *rescan = true; 2976 vc->vc_utf_count = 0; 2977 goto bad_sequence; 2978 } 2979 2980 /* Nothing to do if an ASCII byte was received */ 2981 if (c <= 0x7f) 2982 return c; 2983 2984 /* First byte of a multibyte sequence received */ 2985 vc->vc_npar = 0; 2986 if ((c & 0xe0) == 0xc0) { 2987 vc->vc_utf_count = 1; 2988 vc->vc_utf_char = (c & 0x1f); 2989 } else if ((c & 0xf0) == 0xe0) { 2990 vc->vc_utf_count = 2; 2991 vc->vc_utf_char = (c & 0x0f); 2992 } else if ((c & 0xf8) == 0xf0) { 2993 vc->vc_utf_count = 3; 2994 vc->vc_utf_char = (c & 0x07); 2995 } else { 2996 goto bad_sequence; 2997 } 2998 2999 need_more_bytes: 3000 return -1; 3001 3002 bad_sequence: 3003 return 0xfffd; 3004 } 3005 3006 static int vc_translate(struct vc_data *vc, int *c, bool *rescan) 3007 { 3008 /* Do no translation at all in control states */ 3009 if (vc->vc_state != ESnormal) 3010 return *c; 3011 3012 if (vc->vc_utf && !vc->vc_disp_ctrl) 3013 return *c = vc_translate_unicode(vc, *c, rescan); 3014 3015 /* no utf or alternate charset mode */ 3016 return vc_translate_ascii(vc, *c); 3017 } 3018 3019 static inline unsigned char vc_invert_attr(const struct vc_data *vc) 3020 { 3021 if (!vc->vc_can_do_color) 3022 return vc->vc_attr ^ 0x08; 3023 3024 if (vc->vc_hi_font_mask == 0x100) 3025 return (vc->vc_attr & 0x11) | 3026 ((vc->vc_attr & 0xe0) >> 4) | 3027 ((vc->vc_attr & 0x0e) << 4); 3028 3029 return (vc->vc_attr & 0x88) | 3030 ((vc->vc_attr & 0x70) >> 4) | 3031 ((vc->vc_attr & 0x07) << 4); 3032 } 3033 3034 static bool vc_is_control(struct vc_data *vc, int tc, int c) 3035 { 3036 /* 3037 * A bitmap for codes <32. A bit of 1 indicates that the code 3038 * corresponding to that bit number invokes some special action (such 3039 * as cursor movement) and should not be displayed as a glyph unless 3040 * the disp_ctrl mode is explicitly enabled. 3041 */ 3042 static const u32 CTRL_ACTION = BIT(ASCII_NULL) | 3043 GENMASK(ASCII_SHIFTIN, ASCII_BELL) | BIT(ASCII_CANCEL) | 3044 BIT(ASCII_SUBSTITUTE) | BIT(ASCII_ESCAPE); 3045 /* Cannot be overridden by disp_ctrl */ 3046 static const u32 CTRL_ALWAYS = BIT(ASCII_NULL) | BIT(ASCII_BACKSPACE) | 3047 BIT(ASCII_LINEFEED) | BIT(ASCII_SHIFTIN) | BIT(ASCII_SHIFTOUT) | 3048 BIT(ASCII_CAR_RET) | BIT(ASCII_FORMFEED) | BIT(ASCII_ESCAPE); 3049 3050 if (vc->vc_state != ESnormal) 3051 return true; 3052 3053 if (!tc) 3054 return true; 3055 3056 /* 3057 * If the original code was a control character we only allow a glyph 3058 * to be displayed if the code is not normally used (such as for cursor 3059 * movement) or if the disp_ctrl mode has been explicitly enabled. 3060 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always 3061 * displayed as control characters, as the console would be pretty 3062 * useless without them; to display an arbitrary font position use the 3063 * direct-to-font zone in UTF-8 mode. 3064 */ 3065 if (c < BITS_PER_TYPE(CTRL_ALWAYS)) { 3066 if (vc->vc_disp_ctrl) 3067 return CTRL_ALWAYS & BIT(c); 3068 else 3069 return vc->vc_utf || (CTRL_ACTION & BIT(c)); 3070 } 3071 3072 if (c == ASCII_DEL && !vc->vc_disp_ctrl) 3073 return true; 3074 3075 if (c == ASCII_EXT_CSI) 3076 return true; 3077 3078 return false; 3079 } 3080 3081 static void vc_con_rewind(struct vc_data *vc) 3082 { 3083 if (vc->state.x && !vc->vc_need_wrap) { 3084 vc->vc_pos -= 2; 3085 vc->state.x--; 3086 } 3087 vc->vc_need_wrap = 0; 3088 } 3089 3090 #define UCS_ZWS 0x200b /* Zero Width Space */ 3091 #define UCS_VS16 0xfe0f /* Variation Selector 16 */ 3092 #define UCS_REPLACEMENT 0xfffd /* Replacement Character */ 3093 3094 static int vc_process_ucs(struct vc_data *vc, int *c, int *tc) 3095 { 3096 u32 prev_c, curr_c = *c; 3097 3098 if (ucs_is_double_width(curr_c)) { 3099 /* 3100 * The Unicode screen memory is allocated only when 3101 * required. This is one such case as we need to remember 3102 * which displayed characters are double-width. 3103 */ 3104 vc_uniscr_check(vc); 3105 return 2; 3106 } 3107 3108 if (!ucs_is_zero_width(curr_c)) 3109 return 1; 3110 3111 /* From here curr_c is known to be zero-width. */ 3112 3113 if (ucs_is_double_width(vc_uniscr_getc(vc, -2))) { 3114 /* 3115 * Let's merge this zero-width code point with the preceding 3116 * double-width code point by replacing the existing 3117 * zero-width space padding. To do so we rewind one column 3118 * and pretend this has a width of 1. 3119 * We give the legacy display the same initial space padding. 3120 */ 3121 vc_con_rewind(vc); 3122 *tc = ' '; 3123 return 1; 3124 } 3125 3126 /* From here the preceding character, if any, must be single-width. */ 3127 prev_c = vc_uniscr_getc(vc, -1); 3128 3129 if (curr_c == UCS_VS16 && prev_c != 0) { 3130 /* 3131 * VS16 (U+FE0F) is special. It typically turns the preceding 3132 * single-width character into a double-width one. Let it 3133 * have a width of 1 effectively making the combination with 3134 * the preceding character double-width. 3135 */ 3136 *tc = ' '; 3137 return 1; 3138 } 3139 3140 /* try recomposition */ 3141 prev_c = ucs_recompose(prev_c, curr_c); 3142 if (prev_c != 0) { 3143 vc_con_rewind(vc); 3144 *tc = *c = prev_c; 3145 return 1; 3146 } 3147 3148 /* Otherwise zero-width code points are ignored. */ 3149 return 0; 3150 } 3151 3152 static int vc_get_glyph(struct vc_data *vc, int tc) 3153 { 3154 int glyph = conv_uni_to_pc(vc, tc); 3155 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 3156 3157 if (!(glyph & ~charmask)) 3158 return glyph; 3159 3160 if (glyph == -1) 3161 return -1; /* nothing to display */ 3162 3163 /* Glyph not found */ 3164 if ((!vc->vc_utf || vc->vc_disp_ctrl || tc < 128) && !(tc & ~charmask)) { 3165 /* 3166 * In legacy mode use the glyph we get by a 1:1 mapping. 3167 * This would make absolutely no sense with Unicode in mind, but do this for 3168 * ASCII characters since a font may lack Unicode mapping info and we don't 3169 * want to end up with having question marks only. 3170 */ 3171 return tc; 3172 } 3173 3174 /* 3175 * The Unicode screen memory is allocated only when required. 3176 * This is one such case: we're about to "cheat" with the displayed 3177 * character meaning the simple screen buffer won't hold the original 3178 * information, whereas the Unicode screen buffer always does. 3179 */ 3180 vc_uniscr_check(vc); 3181 3182 /* Try getting a simpler fallback character. */ 3183 tc = ucs_get_fallback(tc); 3184 if (tc) 3185 return vc_get_glyph(vc, tc); 3186 3187 /* Display U+FFFD (Unicode Replacement Character). */ 3188 return conv_uni_to_pc(vc, UCS_REPLACEMENT); 3189 } 3190 3191 static int vc_con_write_normal(struct vc_data *vc, int tc, int c, 3192 struct vc_draw_region *draw) 3193 { 3194 int next_c; 3195 unsigned char vc_attr = vc->vc_attr; 3196 u16 himask = vc->vc_hi_font_mask; 3197 u8 width = 1; 3198 bool inverse = false; 3199 3200 if (vc->vc_utf && !vc->vc_disp_ctrl) { 3201 width = vc_process_ucs(vc, &c, &tc); 3202 if (!width) 3203 goto out; 3204 } 3205 3206 /* Now try to find out how to display it */ 3207 tc = vc_get_glyph(vc, tc); 3208 if (tc == -1) 3209 return -1; /* nothing to display */ 3210 if (tc < 0) { 3211 inverse = true; 3212 tc = conv_uni_to_pc(vc, '?'); 3213 if (tc < 0) 3214 tc = '?'; 3215 3216 vc_attr = vc_invert_attr(vc); 3217 con_flush(vc, draw); 3218 } 3219 3220 next_c = c; 3221 while (1) { 3222 if (vc->vc_need_wrap || vc->vc_decim) 3223 con_flush(vc, draw); 3224 if (vc->vc_need_wrap) { 3225 cr(vc); 3226 lf(vc); 3227 } 3228 if (vc->vc_decim) 3229 insert_char(vc, 1); 3230 vc_uniscr_putc(vc, next_c); 3231 3232 if (himask) 3233 tc = ((tc & 0x100) ? himask : 0) | 3234 (tc & 0xff); 3235 tc |= (vc_attr << 8) & ~himask; 3236 3237 scr_writew(tc, (u16 *)vc->vc_pos); 3238 3239 if (con_should_update(vc) && draw->x < 0) { 3240 draw->x = vc->state.x; 3241 draw->from = vc->vc_pos; 3242 } 3243 if (vc->state.x == vc->vc_cols - 1) { 3244 vc->vc_need_wrap = vc->vc_decawm; 3245 draw->to = vc->vc_pos + 2; 3246 } else { 3247 vc->state.x++; 3248 draw->to = (vc->vc_pos += 2); 3249 } 3250 3251 if (!--width) 3252 break; 3253 3254 /* A space is printed in the second column */ 3255 tc = conv_uni_to_pc(vc, ' '); 3256 if (tc < 0) 3257 tc = ' '; 3258 /* 3259 * Store a zero-width space in the Unicode screen given that 3260 * the previous code point is semantically double width. 3261 */ 3262 next_c = UCS_ZWS; 3263 } 3264 3265 out: 3266 notify_write(vc, c); 3267 3268 if (inverse) 3269 con_flush(vc, draw); 3270 3271 return 0; 3272 } 3273 3274 /* acquires console_lock */ 3275 static int do_con_write(struct tty_struct *tty, const u8 *buf, int count) 3276 { 3277 struct vc_draw_region draw = { 3278 .x = -1, 3279 }; 3280 int c, tc, n = 0; 3281 unsigned int currcons; 3282 struct vc_data *vc = tty->driver_data; 3283 struct vt_notifier_param param; 3284 bool rescan; 3285 3286 if (in_interrupt()) 3287 return count; 3288 3289 guard(console_lock)(); 3290 currcons = vc->vc_num; 3291 if (!vc_cons_allocated(currcons)) { 3292 /* could this happen? */ 3293 pr_warn_once("con_write: tty %d not allocated\n", currcons+1); 3294 return 0; 3295 } 3296 3297 3298 /* undraw cursor first */ 3299 if (con_is_fg(vc)) 3300 hide_cursor(vc); 3301 3302 param.vc = vc; 3303 3304 while (!tty->flow.stopped && count) { 3305 u8 orig = *buf; 3306 buf++; 3307 n++; 3308 count--; 3309 rescan_last_byte: 3310 c = orig; 3311 rescan = false; 3312 3313 tc = vc_translate(vc, &c, &rescan); 3314 if (tc == -1) 3315 continue; 3316 3317 param.c = tc; 3318 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE, 3319 ¶m) == NOTIFY_STOP) 3320 continue; 3321 3322 if (vc_is_control(vc, tc, c)) { 3323 con_flush(vc, &draw); 3324 do_con_trol(tty, vc, orig); 3325 continue; 3326 } 3327 3328 if (vc_con_write_normal(vc, tc, c, &draw) < 0) 3329 continue; 3330 3331 if (rescan) 3332 goto rescan_last_byte; 3333 } 3334 con_flush(vc, &draw); 3335 notify_update(vc); 3336 3337 return n; 3338 } 3339 3340 /* 3341 * This is the console switching callback. 3342 * 3343 * Doing console switching in a process context allows 3344 * us to do the switches asynchronously (needed when we want 3345 * to switch due to a keyboard interrupt). Synchronization 3346 * with other console code and prevention of re-entrancy is 3347 * ensured with console_lock. 3348 */ 3349 static void console_callback(struct work_struct *ignored) 3350 { 3351 guard(console_lock)(); 3352 3353 if (want_console >= 0) { 3354 if (want_console != fg_console && 3355 vc_cons_allocated(want_console)) { 3356 hide_cursor(vc_cons[fg_console].d); 3357 change_console(vc_cons[want_console].d); 3358 /* we only changed when the console had already 3359 been allocated - a new console is not created 3360 in an interrupt routine */ 3361 } 3362 want_console = -1; 3363 } 3364 if (do_poke_blanked_console) { /* do not unblank for a LED change */ 3365 do_poke_blanked_console = 0; 3366 poke_blanked_console(); 3367 } 3368 if (scrollback_delta) { 3369 struct vc_data *vc = vc_cons[fg_console].d; 3370 clear_selection(); 3371 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta) 3372 vc->vc_sw->con_scrolldelta(vc, scrollback_delta); 3373 scrollback_delta = 0; 3374 } 3375 if (blank_timer_expired) { 3376 do_blank_screen(0); 3377 blank_timer_expired = 0; 3378 } 3379 notify_update(vc_cons[fg_console].d); 3380 } 3381 3382 int set_console(int nr) 3383 { 3384 struct vc_data *vc = vc_cons[fg_console].d; 3385 3386 if (!vc_cons_allocated(nr) || vt_dont_switch || 3387 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) { 3388 3389 /* 3390 * Console switch will fail in console_callback() or 3391 * change_console() so there is no point scheduling 3392 * the callback 3393 * 3394 * Existing set_console() users don't check the return 3395 * value so this shouldn't break anything 3396 */ 3397 return -EINVAL; 3398 } 3399 3400 want_console = nr; 3401 schedule_console_callback(); 3402 3403 return 0; 3404 } 3405 3406 struct tty_driver *console_driver; 3407 3408 #ifdef CONFIG_VT_CONSOLE 3409 3410 /** 3411 * vt_kmsg_redirect() - sets/gets the kernel message console 3412 * @new: the new virtual terminal number or -1 if the console should stay 3413 * unchanged 3414 * 3415 * By default, the kernel messages are always printed on the current virtual 3416 * console. However, the user may modify that default with the 3417 * %TIOCL_SETKMSGREDIRECT ioctl call. 3418 * 3419 * This function sets the kernel message console to be @new. It returns the old 3420 * virtual console number. The virtual terminal number %0 (both as parameter and 3421 * return value) means no redirection (i.e. always printed on the currently 3422 * active console). 3423 * 3424 * The parameter -1 means that only the current console is returned, but the 3425 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that 3426 * case to make the code more understandable. 3427 * 3428 * When the kernel is compiled without %CONFIG_VT_CONSOLE, this function ignores 3429 * the parameter and always returns %0. 3430 */ 3431 int vt_kmsg_redirect(int new) 3432 { 3433 static int kmsg_con; 3434 3435 if (new != -1) 3436 return xchg(&kmsg_con, new); 3437 else 3438 return kmsg_con; 3439 } 3440 3441 /* 3442 * Console on virtual terminal 3443 * 3444 * The console must be locked when we get here. 3445 */ 3446 3447 static void vt_console_print(struct console *co, const char *b, unsigned count) 3448 { 3449 struct vc_data *vc = vc_cons[fg_console].d; 3450 unsigned char c; 3451 static DEFINE_SPINLOCK(printing_lock); 3452 const ushort *start; 3453 ushort start_x, cnt; 3454 int kmsg_console; 3455 3456 WARN_CONSOLE_UNLOCKED(); 3457 3458 /* this protects against concurrent oops only */ 3459 if (!spin_trylock(&printing_lock)) 3460 return; 3461 3462 kmsg_console = vt_get_kmsg_redirect(); 3463 if (kmsg_console && vc_cons_allocated(kmsg_console - 1)) 3464 vc = vc_cons[kmsg_console - 1].d; 3465 3466 if (!vc_cons_allocated(fg_console)) { 3467 /* impossible */ 3468 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */ 3469 goto quit; 3470 } 3471 3472 if (vc->vc_mode != KD_TEXT) 3473 goto quit; 3474 3475 /* undraw cursor first */ 3476 if (con_is_fg(vc)) 3477 hide_cursor(vc); 3478 3479 start = (ushort *)vc->vc_pos; 3480 start_x = vc->state.x; 3481 cnt = 0; 3482 while (count--) { 3483 c = *b++; 3484 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET || 3485 c == ASCII_BACKSPACE || vc->vc_need_wrap) { 3486 if (cnt && con_is_visible(vc)) 3487 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3488 cnt = 0; 3489 if (c == ASCII_BACKSPACE) { 3490 bs(vc); 3491 start = (ushort *)vc->vc_pos; 3492 start_x = vc->state.x; 3493 continue; 3494 } 3495 if (c != ASCII_CAR_RET) 3496 lf(vc); 3497 cr(vc); 3498 start = (ushort *)vc->vc_pos; 3499 start_x = vc->state.x; 3500 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET) 3501 continue; 3502 } 3503 vc_uniscr_putc(vc, c); 3504 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos); 3505 notify_write(vc, c); 3506 cnt++; 3507 if (vc->state.x == vc->vc_cols - 1) { 3508 vc->vc_need_wrap = 1; 3509 } else { 3510 vc->vc_pos += 2; 3511 vc->state.x++; 3512 } 3513 } 3514 if (cnt && con_is_visible(vc)) 3515 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3516 set_cursor(vc); 3517 notify_update(vc); 3518 3519 quit: 3520 spin_unlock(&printing_lock); 3521 } 3522 3523 static struct tty_driver *vt_console_device(struct console *c, int *index) 3524 { 3525 *index = c->index ? c->index-1 : fg_console; 3526 return console_driver; 3527 } 3528 3529 static int vt_console_setup(struct console *co, char *options) 3530 { 3531 return co->index >= MAX_NR_CONSOLES ? -EINVAL : 0; 3532 } 3533 3534 static struct console vt_console_driver = { 3535 .name = "tty", 3536 .setup = vt_console_setup, 3537 .write = vt_console_print, 3538 .device = vt_console_device, 3539 .unblank = unblank_screen, 3540 .flags = CON_PRINTBUFFER, 3541 .index = -1, 3542 }; 3543 #endif 3544 3545 /* 3546 * Handling of Linux-specific VC ioctls 3547 */ 3548 3549 /* 3550 * Generally a bit racy with respect to console_lock();. 3551 * 3552 * There are some functions which don't need it. 3553 * 3554 * There are some functions which can sleep for arbitrary periods 3555 * (paste_selection) but we don't need the lock there anyway. 3556 * 3557 * set_selection_user has locking, and definitely needs it 3558 */ 3559 3560 int tioclinux(struct tty_struct *tty, unsigned long arg) 3561 { 3562 char type, data; 3563 char __user *p = (char __user *)arg; 3564 void __user *param_aligned32 = (u32 __user *)arg + 1; 3565 void __user *param = (void __user *)arg + 1; 3566 int lines; 3567 int ret; 3568 3569 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN)) 3570 return -EPERM; 3571 if (get_user(type, p)) 3572 return -EFAULT; 3573 ret = 0; 3574 3575 switch (type) { 3576 case TIOCL_SETSEL: 3577 return set_selection_user(param, tty); 3578 case TIOCL_PASTESEL: 3579 if (!capable(CAP_SYS_ADMIN)) 3580 return -EPERM; 3581 return paste_selection(tty); 3582 case TIOCL_UNBLANKSCREEN: 3583 scoped_guard(console_lock) 3584 unblank_screen(); 3585 break; 3586 case TIOCL_SELLOADLUT: 3587 if (!capable(CAP_SYS_ADMIN)) 3588 return -EPERM; 3589 return sel_loadlut(param_aligned32); 3590 case TIOCL_GETSHIFTSTATE: 3591 /* 3592 * Make it possible to react to Shift+Mousebutton. Note that 3593 * 'shift_state' is an undocumented kernel-internal variable; 3594 * programs not closely related to the kernel should not use 3595 * this. 3596 */ 3597 data = vt_get_shift_state(); 3598 return put_user(data, p); 3599 case TIOCL_GETMOUSEREPORTING: 3600 scoped_guard(console_lock) /* May be overkill */ 3601 data = mouse_reporting(); 3602 return put_user(data, p); 3603 case TIOCL_SETVESABLANK: 3604 return set_vesa_blanking(param); 3605 case TIOCL_GETKMSGREDIRECT: 3606 data = vt_get_kmsg_redirect(); 3607 return put_user(data, p); 3608 case TIOCL_SETKMSGREDIRECT: 3609 if (!capable(CAP_SYS_ADMIN)) 3610 return -EPERM; 3611 3612 if (get_user(data, p+1)) 3613 return -EFAULT; 3614 3615 vt_kmsg_redirect(data); 3616 3617 break; 3618 case TIOCL_GETFGCONSOLE: 3619 /* 3620 * No locking needed as this is a transiently correct return 3621 * anyway if the caller hasn't disabled switching. 3622 */ 3623 return fg_console; 3624 case TIOCL_SCROLLCONSOLE: 3625 if (get_user(lines, (s32 __user *)param_aligned32)) 3626 return -EFAULT; 3627 3628 /* 3629 * Needs the console lock here. Note that lots of other calls 3630 * need fixing before the lock is actually useful! 3631 */ 3632 scoped_guard(console_lock) 3633 scrollfront(vc_cons[fg_console].d, lines); 3634 break; 3635 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ 3636 scoped_guard(console_lock) { 3637 ignore_poke = 1; 3638 do_blank_screen(0); 3639 } 3640 break; 3641 case TIOCL_BLANKEDSCREEN: 3642 return console_blanked; 3643 case TIOCL_GETBRACKETEDPASTE: 3644 return get_bracketed_paste(tty); 3645 default: 3646 return -EINVAL; 3647 } 3648 3649 return ret; 3650 } 3651 3652 /* 3653 * /dev/ttyN handling 3654 */ 3655 3656 static ssize_t con_write(struct tty_struct *tty, const u8 *buf, size_t count) 3657 { 3658 int retval; 3659 3660 retval = do_con_write(tty, buf, count); 3661 con_flush_chars(tty); 3662 3663 return retval; 3664 } 3665 3666 static int con_put_char(struct tty_struct *tty, u8 ch) 3667 { 3668 return do_con_write(tty, &ch, 1); 3669 } 3670 3671 static unsigned int con_write_room(struct tty_struct *tty) 3672 { 3673 if (tty->flow.stopped) 3674 return 0; 3675 return 32768; /* No limit, really; we're not buffering */ 3676 } 3677 3678 /* 3679 * con_throttle and con_unthrottle are only used for 3680 * paste_selection(), which has to stuff in a large number of 3681 * characters... 3682 */ 3683 static void con_throttle(struct tty_struct *tty) 3684 { 3685 } 3686 3687 static void con_unthrottle(struct tty_struct *tty) 3688 { 3689 struct vc_data *vc = tty->driver_data; 3690 3691 wake_up_interruptible(&vc->paste_wait); 3692 } 3693 3694 /* 3695 * Turn the Scroll-Lock LED on when the tty is stopped 3696 */ 3697 static void con_stop(struct tty_struct *tty) 3698 { 3699 int console_num; 3700 if (!tty) 3701 return; 3702 console_num = tty->index; 3703 if (!vc_cons_allocated(console_num)) 3704 return; 3705 vt_kbd_con_stop(console_num); 3706 } 3707 3708 /* 3709 * Turn the Scroll-Lock LED off when the console is started 3710 */ 3711 static void con_start(struct tty_struct *tty) 3712 { 3713 int console_num; 3714 if (!tty) 3715 return; 3716 console_num = tty->index; 3717 if (!vc_cons_allocated(console_num)) 3718 return; 3719 vt_kbd_con_start(console_num); 3720 } 3721 3722 static void con_flush_chars(struct tty_struct *tty) 3723 { 3724 struct vc_data *vc = tty->driver_data; 3725 3726 if (in_interrupt()) /* from flush_to_ldisc */ 3727 return; 3728 3729 guard(console_lock)(); 3730 set_cursor(vc); 3731 } 3732 3733 /* 3734 * Allocate the console screen memory. 3735 */ 3736 static int con_install(struct tty_driver *driver, struct tty_struct *tty) 3737 { 3738 unsigned int currcons = tty->index; 3739 struct vc_data *vc; 3740 int ret; 3741 3742 guard(console_lock)(); 3743 ret = vc_allocate(currcons); 3744 if (ret) 3745 return ret; 3746 3747 vc = vc_cons[currcons].d; 3748 3749 /* Still being freed */ 3750 if (vc->port.tty) 3751 return -ERESTARTSYS; 3752 3753 ret = tty_port_install(&vc->port, driver, tty); 3754 if (ret) 3755 return ret; 3756 3757 tty->driver_data = vc; 3758 vc->port.tty = tty; 3759 tty_port_get(&vc->port); 3760 3761 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 3762 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; 3763 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols; 3764 } 3765 if (vc->vc_utf) 3766 tty->termios.c_iflag |= IUTF8; 3767 else 3768 tty->termios.c_iflag &= ~IUTF8; 3769 3770 return 0; 3771 } 3772 3773 static int con_open(struct tty_struct *tty, struct file *filp) 3774 { 3775 /* everything done in install */ 3776 return 0; 3777 } 3778 3779 3780 static void con_close(struct tty_struct *tty, struct file *filp) 3781 { 3782 /* Nothing to do - we defer to shutdown */ 3783 } 3784 3785 static void con_shutdown(struct tty_struct *tty) 3786 { 3787 struct vc_data *vc = tty->driver_data; 3788 BUG_ON(vc == NULL); 3789 3790 guard(console_lock)(); 3791 vc->port.tty = NULL; 3792 } 3793 3794 static void con_cleanup(struct tty_struct *tty) 3795 { 3796 struct vc_data *vc = tty->driver_data; 3797 3798 tty_port_put(&vc->port); 3799 } 3800 3801 /* 3802 * We can't deal with anything but the N_TTY ldisc, 3803 * because we can sleep in our write() routine. 3804 */ 3805 static int con_ldisc_ok(struct tty_struct *tty, int ldisc) 3806 { 3807 return ldisc == N_TTY ? 0 : -EINVAL; 3808 } 3809 3810 static int default_color = 7; /* white */ 3811 static int default_italic_color = 2; // green (ASCII) 3812 static int default_underline_color = 3; // cyan (ASCII) 3813 module_param_named(color, default_color, int, S_IRUGO | S_IWUSR); 3814 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); 3815 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); 3816 3817 static void vc_init(struct vc_data *vc, int do_clear) 3818 { 3819 int j, k ; 3820 3821 set_origin(vc); 3822 vc->vc_pos = vc->vc_origin; 3823 reset_vc(vc); 3824 for (j=k=0; j<16; j++) { 3825 vc->vc_palette[k++] = default_red[j] ; 3826 vc->vc_palette[k++] = default_grn[j] ; 3827 vc->vc_palette[k++] = default_blu[j] ; 3828 } 3829 vc->vc_def_color = default_color; 3830 vc->vc_ulcolor = default_underline_color; 3831 vc->vc_itcolor = default_italic_color; 3832 vc->vc_halfcolor = 0x08; /* grey */ 3833 init_waitqueue_head(&vc->paste_wait); 3834 reset_terminal(vc, do_clear); 3835 } 3836 3837 /* 3838 * This routine initializes console interrupts, and does nothing 3839 * else. If you want the screen to clear, call tty_write with 3840 * the appropriate escape-sequence. 3841 */ 3842 3843 static int __init con_init(void) 3844 { 3845 const char *display_desc = NULL; 3846 struct vc_data *vc; 3847 unsigned int currcons = 0, i; 3848 3849 console_lock(); 3850 3851 if (!conswitchp) 3852 conswitchp = &dummy_con; 3853 display_desc = conswitchp->con_startup(); 3854 if (!display_desc) { 3855 fg_console = 0; 3856 console_unlock(); 3857 return 0; 3858 } 3859 3860 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3861 struct con_driver *con_driver = ®istered_con_driver[i]; 3862 3863 if (con_driver->con == NULL) { 3864 con_driver->con = conswitchp; 3865 con_driver->desc = display_desc; 3866 con_driver->flag = CON_DRIVER_FLAG_INIT; 3867 con_driver->first = 0; 3868 con_driver->last = MAX_NR_CONSOLES - 1; 3869 break; 3870 } 3871 } 3872 3873 for (i = 0; i < MAX_NR_CONSOLES; i++) 3874 con_driver_map[i] = conswitchp; 3875 3876 if (blankinterval) { 3877 blank_state = blank_normal_wait; 3878 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 3879 } 3880 3881 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { 3882 vc_cons[currcons].d = vc = kzalloc_obj(struct vc_data, 3883 GFP_NOWAIT); 3884 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 3885 tty_port_init(&vc->port); 3886 visual_init(vc, currcons, true); 3887 /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */ 3888 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); 3889 vc_init(vc, currcons || !vc->vc_sw->con_save_screen); 3890 } 3891 currcons = fg_console = 0; 3892 master_display_fg = vc = vc_cons[currcons].d; 3893 set_origin(vc); 3894 save_screen(vc); 3895 gotoxy(vc, vc->state.x, vc->state.y); 3896 csi_J(vc, CSI_J_CURSOR_TO_END); 3897 update_screen(vc); 3898 pr_info("Console: %s %s %dx%d\n", 3899 vc->vc_can_do_color ? "colour" : "mono", 3900 display_desc, vc->vc_cols, vc->vc_rows); 3901 3902 console_unlock(); 3903 3904 #ifdef CONFIG_VT_CONSOLE 3905 register_console(&vt_console_driver); 3906 #endif 3907 return 0; 3908 } 3909 console_initcall(con_init); 3910 3911 static const struct tty_operations con_ops = { 3912 .install = con_install, 3913 .open = con_open, 3914 .close = con_close, 3915 .write = con_write, 3916 .write_room = con_write_room, 3917 .put_char = con_put_char, 3918 .flush_chars = con_flush_chars, 3919 .ioctl = vt_ioctl, 3920 #ifdef CONFIG_COMPAT 3921 .compat_ioctl = vt_compat_ioctl, 3922 #endif 3923 .stop = con_stop, 3924 .start = con_start, 3925 .throttle = con_throttle, 3926 .unthrottle = con_unthrottle, 3927 .resize = vt_resize, 3928 .shutdown = con_shutdown, 3929 .cleanup = con_cleanup, 3930 .ldisc_ok = con_ldisc_ok, 3931 }; 3932 3933 static struct cdev vc0_cdev; 3934 3935 static ssize_t show_tty_active(struct device *dev, 3936 struct device_attribute *attr, char *buf) 3937 { 3938 return sprintf(buf, "tty%d\n", fg_console + 1); 3939 } 3940 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL); 3941 3942 static struct attribute *vt_dev_attrs[] = { 3943 &dev_attr_active.attr, 3944 NULL 3945 }; 3946 3947 ATTRIBUTE_GROUPS(vt_dev); 3948 3949 int __init vty_init(const struct file_operations *console_fops) 3950 { 3951 cdev_init(&vc0_cdev, console_fops); 3952 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || 3953 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) 3954 panic("Couldn't register /dev/tty0 driver\n"); 3955 tty0dev = device_create_with_groups(&tty_class, NULL, 3956 MKDEV(TTY_MAJOR, 0), NULL, 3957 vt_dev_groups, "tty0"); 3958 if (IS_ERR(tty0dev)) 3959 tty0dev = NULL; 3960 3961 vcs_init(); 3962 3963 console_driver = tty_alloc_driver(MAX_NR_CONSOLES, TTY_DRIVER_REAL_RAW | 3964 TTY_DRIVER_RESET_TERMIOS); 3965 if (IS_ERR(console_driver)) 3966 panic("Couldn't allocate console driver\n"); 3967 3968 console_driver->name = "tty"; 3969 console_driver->name_base = 1; 3970 console_driver->major = TTY_MAJOR; 3971 console_driver->minor_start = 1; 3972 console_driver->type = TTY_DRIVER_TYPE_CONSOLE; 3973 console_driver->init_termios = tty_std_termios; 3974 if (default_utf8) 3975 console_driver->init_termios.c_iflag |= IUTF8; 3976 tty_set_operations(console_driver, &con_ops); 3977 if (tty_register_driver(console_driver)) 3978 panic("Couldn't register console driver\n"); 3979 kbd_init(); 3980 console_map_init(); 3981 #ifdef CONFIG_MDA_CONSOLE 3982 mda_console_init(); 3983 #endif 3984 return 0; 3985 } 3986 3987 static const struct class vtconsole_class = { 3988 .name = "vtconsole", 3989 }; 3990 3991 static int do_bind_con_driver(const struct consw *csw, int first, int last, 3992 int deflt) 3993 { 3994 struct module *owner = csw->owner; 3995 const char *desc = NULL; 3996 struct con_driver *con_driver; 3997 int i, j = -1, k = -1, retval = -ENODEV; 3998 3999 if (!try_module_get(owner)) 4000 return -ENODEV; 4001 4002 WARN_CONSOLE_UNLOCKED(); 4003 4004 /* check if driver is registered */ 4005 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4006 con_driver = ®istered_con_driver[i]; 4007 4008 if (con_driver->con == csw) { 4009 desc = con_driver->desc; 4010 retval = 0; 4011 break; 4012 } 4013 } 4014 4015 if (retval) 4016 goto err; 4017 4018 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) { 4019 csw->con_startup(); 4020 con_driver->flag |= CON_DRIVER_FLAG_INIT; 4021 } 4022 4023 if (deflt) { 4024 if (conswitchp) 4025 module_put(conswitchp->owner); 4026 4027 __module_get(owner); 4028 conswitchp = csw; 4029 } 4030 4031 first = max(first, con_driver->first); 4032 last = min(last, con_driver->last); 4033 4034 for (i = first; i <= last; i++) { 4035 int old_was_color; 4036 struct vc_data *vc = vc_cons[i].d; 4037 4038 if (con_driver_map[i]) 4039 module_put(con_driver_map[i]->owner); 4040 __module_get(owner); 4041 con_driver_map[i] = csw; 4042 4043 if (!vc || !vc->vc_sw) 4044 continue; 4045 4046 j = i; 4047 4048 if (con_is_visible(vc)) { 4049 k = i; 4050 save_screen(vc); 4051 } 4052 4053 old_was_color = vc->vc_can_do_color; 4054 vc->vc_sw->con_deinit(vc); 4055 vc->vc_origin = (unsigned long)vc->vc_screenbuf; 4056 visual_init(vc, i, false); 4057 set_origin(vc); 4058 update_attr(vc); 4059 4060 /* If the console changed between mono <-> color, then 4061 * the attributes in the screenbuf will be wrong. The 4062 * following resets all attributes to something sane. 4063 */ 4064 if (old_was_color != vc->vc_can_do_color) 4065 clear_buffer_attributes(vc); 4066 } 4067 4068 pr_info("Console: switching "); 4069 if (!deflt) 4070 pr_cont("consoles %d-%d ", first + 1, last + 1); 4071 if (j >= 0) { 4072 struct vc_data *vc = vc_cons[j].d; 4073 4074 pr_cont("to %s %s %dx%d\n", 4075 vc->vc_can_do_color ? "colour" : "mono", 4076 desc, vc->vc_cols, vc->vc_rows); 4077 4078 if (k >= 0) { 4079 vc = vc_cons[k].d; 4080 update_screen(vc); 4081 } 4082 } else { 4083 pr_cont("to %s\n", desc); 4084 } 4085 4086 retval = 0; 4087 err: 4088 module_put(owner); 4089 return retval; 4090 }; 4091 4092 4093 #ifdef CONFIG_VT_HW_CONSOLE_BINDING 4094 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt) 4095 { 4096 struct module *owner = csw->owner; 4097 const struct consw *defcsw = NULL; 4098 struct con_driver *con_driver = NULL, *con_back = NULL; 4099 int i, retval = -ENODEV; 4100 4101 if (!try_module_get(owner)) 4102 return -ENODEV; 4103 4104 WARN_CONSOLE_UNLOCKED(); 4105 4106 /* check if driver is registered and if it is unbindable */ 4107 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4108 con_driver = ®istered_con_driver[i]; 4109 4110 if (con_driver->con == csw && 4111 con_driver->flag & CON_DRIVER_FLAG_MODULE) { 4112 retval = 0; 4113 break; 4114 } 4115 } 4116 4117 if (retval) 4118 goto err; 4119 4120 retval = -ENODEV; 4121 4122 /* check if backup driver exists */ 4123 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4124 con_back = ®istered_con_driver[i]; 4125 4126 if (con_back->con && con_back->con != csw) { 4127 defcsw = con_back->con; 4128 retval = 0; 4129 break; 4130 } 4131 } 4132 4133 if (retval) 4134 goto err; 4135 4136 if (!con_is_bound(csw)) 4137 goto err; 4138 4139 first = max(first, con_driver->first); 4140 last = min(last, con_driver->last); 4141 4142 for (i = first; i <= last; i++) { 4143 if (con_driver_map[i] == csw) { 4144 module_put(csw->owner); 4145 con_driver_map[i] = NULL; 4146 } 4147 } 4148 4149 if (!con_is_bound(defcsw)) { 4150 const struct consw *defconsw = conswitchp; 4151 4152 defcsw->con_startup(); 4153 con_back->flag |= CON_DRIVER_FLAG_INIT; 4154 /* 4155 * vgacon may change the default driver to point 4156 * to dummycon, we restore it here... 4157 */ 4158 conswitchp = defconsw; 4159 } 4160 4161 if (!con_is_bound(csw)) 4162 con_driver->flag &= ~CON_DRIVER_FLAG_INIT; 4163 4164 /* ignore return value, binding should not fail */ 4165 do_bind_con_driver(defcsw, first, last, deflt); 4166 err: 4167 module_put(owner); 4168 return retval; 4169 4170 } 4171 EXPORT_SYMBOL_GPL(do_unbind_con_driver); 4172 4173 static int vt_bind(struct con_driver *con) 4174 { 4175 const struct consw *defcsw = NULL, *csw = NULL; 4176 int i, more = 1, first = -1, last = -1, deflt = 0; 4177 4178 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4179 goto err; 4180 4181 csw = con->con; 4182 4183 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4184 struct con_driver *con = ®istered_con_driver[i]; 4185 4186 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) { 4187 defcsw = con->con; 4188 break; 4189 } 4190 } 4191 4192 if (!defcsw) 4193 goto err; 4194 4195 while (more) { 4196 more = 0; 4197 4198 for (i = con->first; i <= con->last; i++) { 4199 if (con_driver_map[i] == defcsw) { 4200 if (first == -1) 4201 first = i; 4202 last = i; 4203 more = 1; 4204 } else if (first != -1) 4205 break; 4206 } 4207 4208 if (first == 0 && last == MAX_NR_CONSOLES -1) 4209 deflt = 1; 4210 4211 if (first != -1) 4212 do_bind_con_driver(csw, first, last, deflt); 4213 4214 first = -1; 4215 last = -1; 4216 deflt = 0; 4217 } 4218 4219 err: 4220 return 0; 4221 } 4222 4223 static int vt_unbind(struct con_driver *con) 4224 { 4225 const struct consw *csw = NULL; 4226 int i, more = 1, first = -1, last = -1, deflt = 0; 4227 int ret; 4228 4229 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4230 goto err; 4231 4232 csw = con->con; 4233 4234 while (more) { 4235 more = 0; 4236 4237 for (i = con->first; i <= con->last; i++) { 4238 if (con_driver_map[i] == csw) { 4239 if (first == -1) 4240 first = i; 4241 last = i; 4242 more = 1; 4243 } else if (first != -1) 4244 break; 4245 } 4246 4247 if (first == 0 && last == MAX_NR_CONSOLES -1) 4248 deflt = 1; 4249 4250 if (first != -1) { 4251 ret = do_unbind_con_driver(csw, first, last, deflt); 4252 if (ret != 0) 4253 return ret; 4254 } 4255 4256 first = -1; 4257 last = -1; 4258 deflt = 0; 4259 } 4260 4261 err: 4262 return 0; 4263 } 4264 #else 4265 static inline int vt_bind(struct con_driver *con) 4266 { 4267 return 0; 4268 } 4269 static inline int vt_unbind(struct con_driver *con) 4270 { 4271 return 0; 4272 } 4273 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ 4274 4275 static ssize_t store_bind(struct device *dev, struct device_attribute *attr, 4276 const char *buf, size_t count) 4277 { 4278 struct con_driver *con = dev_get_drvdata(dev); 4279 int bind = simple_strtoul(buf, NULL, 0); 4280 4281 guard(console_lock)(); 4282 4283 if (bind) 4284 vt_bind(con); 4285 else 4286 vt_unbind(con); 4287 4288 return count; 4289 } 4290 4291 static ssize_t show_bind(struct device *dev, struct device_attribute *attr, 4292 char *buf) 4293 { 4294 struct con_driver *con = dev_get_drvdata(dev); 4295 int bind; 4296 4297 scoped_guard(console_lock) 4298 bind = con_is_bound(con->con); 4299 4300 return sysfs_emit(buf, "%i\n", bind); 4301 } 4302 4303 static ssize_t show_name(struct device *dev, struct device_attribute *attr, 4304 char *buf) 4305 { 4306 struct con_driver *con = dev_get_drvdata(dev); 4307 4308 return sysfs_emit(buf, "%s %s\n", 4309 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)", 4310 con->desc); 4311 4312 } 4313 4314 static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind); 4315 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 4316 4317 static struct attribute *con_dev_attrs[] = { 4318 &dev_attr_bind.attr, 4319 &dev_attr_name.attr, 4320 NULL 4321 }; 4322 4323 ATTRIBUTE_GROUPS(con_dev); 4324 4325 static int vtconsole_init_device(struct con_driver *con) 4326 { 4327 con->flag |= CON_DRIVER_FLAG_ATTR; 4328 return 0; 4329 } 4330 4331 static void vtconsole_deinit_device(struct con_driver *con) 4332 { 4333 con->flag &= ~CON_DRIVER_FLAG_ATTR; 4334 } 4335 4336 /** 4337 * con_is_bound - checks if driver is bound to the console 4338 * @csw: console driver 4339 * 4340 * RETURNS: zero if unbound, nonzero if bound 4341 * 4342 * Drivers can call this and if zero, they should release 4343 * all resources allocated on &consw.con_startup() 4344 */ 4345 int con_is_bound(const struct consw *csw) 4346 { 4347 int i, bound = 0; 4348 4349 WARN_CONSOLE_UNLOCKED(); 4350 4351 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4352 if (con_driver_map[i] == csw) { 4353 bound = 1; 4354 break; 4355 } 4356 } 4357 4358 return bound; 4359 } 4360 EXPORT_SYMBOL(con_is_bound); 4361 4362 /** 4363 * con_is_visible - checks whether the current console is visible 4364 * @vc: virtual console 4365 * 4366 * RETURNS: zero if not visible, nonzero if visible 4367 */ 4368 bool con_is_visible(const struct vc_data *vc) 4369 { 4370 WARN_CONSOLE_UNLOCKED(); 4371 4372 return *vc->vc_display_fg == vc; 4373 } 4374 EXPORT_SYMBOL(con_is_visible); 4375 4376 /** 4377 * con_debug_enter - prepare the console for the kernel debugger 4378 * @vc: virtual console 4379 * 4380 * Called when the console is taken over by the kernel debugger, this 4381 * function needs to save the current console state, then put the console 4382 * into a state suitable for the kernel debugger. 4383 */ 4384 void con_debug_enter(struct vc_data *vc) 4385 { 4386 #ifdef CONFIG_KGDB_KDB 4387 /* Set the initial LINES variable if it is not already set */ 4388 if (vc->vc_rows < 999) { 4389 int linecount; 4390 char lns[4]; 4391 const char *setargs[3] = { 4392 "set", 4393 "LINES", 4394 lns, 4395 }; 4396 if (kdbgetintenv(setargs[0], &linecount)) { 4397 snprintf(lns, 4, "%i", vc->vc_rows); 4398 kdb_set(2, setargs); 4399 } 4400 } 4401 if (vc->vc_cols < 999) { 4402 int colcount; 4403 char cols[4]; 4404 const char *setargs[3] = { 4405 "set", 4406 "COLUMNS", 4407 cols, 4408 }; 4409 if (kdbgetintenv(setargs[0], &colcount)) { 4410 snprintf(cols, 4, "%i", vc->vc_cols); 4411 kdb_set(2, setargs); 4412 } 4413 } 4414 #endif /* CONFIG_KGDB_KDB */ 4415 } 4416 EXPORT_SYMBOL_GPL(con_debug_enter); 4417 4418 /** 4419 * con_debug_leave - restore console state 4420 * 4421 * Restore the console state to what it was before the kernel debugger 4422 * was invoked. 4423 */ 4424 void con_debug_leave(void) 4425 { } 4426 EXPORT_SYMBOL_GPL(con_debug_leave); 4427 4428 static int do_register_con_driver(const struct consw *csw, int first, int last) 4429 { 4430 struct module *owner = csw->owner; 4431 struct con_driver *con_driver; 4432 const char *desc; 4433 int i, retval; 4434 4435 WARN_CONSOLE_UNLOCKED(); 4436 4437 if (!try_module_get(owner)) 4438 return -ENODEV; 4439 4440 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4441 con_driver = ®istered_con_driver[i]; 4442 4443 /* already registered */ 4444 if (con_driver->con == csw) { 4445 retval = -EBUSY; 4446 goto err; 4447 } 4448 } 4449 4450 desc = csw->con_startup(); 4451 if (!desc) { 4452 retval = -ENODEV; 4453 goto err; 4454 } 4455 4456 retval = -EINVAL; 4457 4458 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4459 con_driver = ®istered_con_driver[i]; 4460 4461 if (con_driver->con == NULL && 4462 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) { 4463 con_driver->con = csw; 4464 con_driver->desc = desc; 4465 con_driver->node = i; 4466 con_driver->flag = CON_DRIVER_FLAG_MODULE | 4467 CON_DRIVER_FLAG_INIT; 4468 con_driver->first = first; 4469 con_driver->last = last; 4470 retval = 0; 4471 break; 4472 } 4473 } 4474 4475 if (retval) 4476 goto err; 4477 4478 con_driver->dev = 4479 device_create_with_groups(&vtconsole_class, NULL, 4480 MKDEV(0, con_driver->node), 4481 con_driver, con_dev_groups, 4482 "vtcon%i", con_driver->node); 4483 if (IS_ERR(con_driver->dev)) { 4484 pr_warn("Unable to create device for %s; errno = %ld\n", 4485 con_driver->desc, PTR_ERR(con_driver->dev)); 4486 con_driver->dev = NULL; 4487 } else { 4488 vtconsole_init_device(con_driver); 4489 } 4490 4491 err: 4492 module_put(owner); 4493 return retval; 4494 } 4495 4496 4497 /** 4498 * do_unregister_con_driver - unregister console driver from console layer 4499 * @csw: console driver 4500 * 4501 * DESCRIPTION: All drivers that registers to the console layer must 4502 * call this function upon exit, or if the console driver is in a state 4503 * where it won't be able to handle console services, such as the 4504 * framebuffer console without loaded framebuffer drivers. 4505 * 4506 * The driver must unbind first prior to unregistration. 4507 */ 4508 int do_unregister_con_driver(const struct consw *csw) 4509 { 4510 int i; 4511 4512 /* cannot unregister a bound driver */ 4513 if (con_is_bound(csw)) 4514 return -EBUSY; 4515 4516 if (csw == conswitchp) 4517 return -EINVAL; 4518 4519 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4520 struct con_driver *con_driver = ®istered_con_driver[i]; 4521 4522 if (con_driver->con == csw) { 4523 /* 4524 * Defer the removal of the sysfs entries since that 4525 * will acquire the kernfs s_active lock and we can't 4526 * acquire this lock while holding the console lock: 4527 * the unbind sysfs entry imposes already the opposite 4528 * order. Reset con already here to prevent any later 4529 * lookup to succeed and mark this slot as zombie, so 4530 * it won't get reused until we complete the removal 4531 * in the deferred work. 4532 */ 4533 con_driver->con = NULL; 4534 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE; 4535 schedule_work(&con_driver_unregister_work); 4536 4537 return 0; 4538 } 4539 } 4540 4541 return -ENODEV; 4542 } 4543 EXPORT_SYMBOL_GPL(do_unregister_con_driver); 4544 4545 static void con_driver_unregister_callback(struct work_struct *ignored) 4546 { 4547 int i; 4548 4549 guard(console_lock)(); 4550 4551 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4552 struct con_driver *con_driver = ®istered_con_driver[i]; 4553 4554 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) 4555 continue; 4556 4557 console_unlock(); 4558 4559 vtconsole_deinit_device(con_driver); 4560 device_destroy(&vtconsole_class, MKDEV(0, con_driver->node)); 4561 4562 console_lock(); 4563 4564 if (WARN_ON_ONCE(con_driver->con)) 4565 con_driver->con = NULL; 4566 con_driver->desc = NULL; 4567 con_driver->dev = NULL; 4568 con_driver->node = 0; 4569 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE); 4570 con_driver->flag = 0; 4571 con_driver->first = 0; 4572 con_driver->last = 0; 4573 } 4574 } 4575 4576 /* 4577 * If we support more console drivers, this function is used 4578 * when a driver wants to take over some existing consoles 4579 * and become default driver for newly opened ones. 4580 * 4581 * do_take_over_console is basically a register followed by bind 4582 */ 4583 int do_take_over_console(const struct consw *csw, int first, int last, int deflt) 4584 { 4585 int err; 4586 4587 err = do_register_con_driver(csw, first, last); 4588 /* 4589 * If we get an busy error we still want to bind the console driver 4590 * and return success, as we may have unbound the console driver 4591 * but not unregistered it. 4592 */ 4593 if (err == -EBUSY) 4594 err = 0; 4595 if (!err) 4596 do_bind_con_driver(csw, first, last, deflt); 4597 4598 return err; 4599 } 4600 EXPORT_SYMBOL_GPL(do_take_over_console); 4601 4602 4603 /* 4604 * give_up_console is a wrapper to unregister_con_driver. It will only 4605 * work if driver is fully unbound. 4606 */ 4607 void give_up_console(const struct consw *csw) 4608 { 4609 guard(console_lock)(); 4610 do_unregister_con_driver(csw); 4611 } 4612 EXPORT_SYMBOL(give_up_console); 4613 4614 static int __init vtconsole_class_init(void) 4615 { 4616 int i; 4617 4618 i = class_register(&vtconsole_class); 4619 if (i) 4620 pr_warn("Unable to create vt console class; errno = %d\n", i); 4621 4622 /* Add system drivers to sysfs */ 4623 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4624 struct con_driver *con = ®istered_con_driver[i]; 4625 4626 if (con->con && !con->dev) { 4627 con->dev = 4628 device_create_with_groups(&vtconsole_class, NULL, 4629 MKDEV(0, con->node), 4630 con, con_dev_groups, 4631 "vtcon%i", con->node); 4632 4633 if (IS_ERR(con->dev)) { 4634 pr_warn("Unable to create device for %s; errno = %ld\n", 4635 con->desc, PTR_ERR(con->dev)); 4636 con->dev = NULL; 4637 } else { 4638 vtconsole_init_device(con); 4639 } 4640 } 4641 } 4642 4643 return 0; 4644 } 4645 postcore_initcall(vtconsole_class_init); 4646 4647 /* 4648 * Screen blanking 4649 */ 4650 4651 static int set_vesa_blanking(u8 __user *mode_user) 4652 { 4653 u8 mode; 4654 4655 if (get_user(mode, mode_user)) 4656 return -EFAULT; 4657 4658 guard(console_lock)(); 4659 vesa_blank_mode = (mode <= VESA_BLANK_MAX) ? mode : VESA_NO_BLANKING; 4660 4661 return 0; 4662 } 4663 4664 void do_blank_screen(int entering_gfx) 4665 { 4666 struct vc_data *vc = vc_cons[fg_console].d; 4667 int i; 4668 4669 might_sleep(); 4670 4671 WARN_CONSOLE_UNLOCKED(); 4672 4673 if (console_blanked) { 4674 if (blank_state == blank_vesa_wait) { 4675 blank_state = blank_off; 4676 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0); 4677 } 4678 return; 4679 } 4680 4681 /* entering graphics mode? */ 4682 if (entering_gfx) { 4683 hide_cursor(vc); 4684 save_screen(vc); 4685 vc->vc_sw->con_blank(vc, VESA_VSYNC_SUSPEND, 1); 4686 console_blanked = fg_console + 1; 4687 blank_state = blank_off; 4688 set_origin(vc); 4689 return; 4690 } 4691 4692 blank_state = blank_off; 4693 4694 /* don't blank graphics */ 4695 if (vc->vc_mode != KD_TEXT) { 4696 console_blanked = fg_console + 1; 4697 return; 4698 } 4699 4700 hide_cursor(vc); 4701 timer_delete_sync(&console_timer); 4702 blank_timer_expired = 0; 4703 4704 save_screen(vc); 4705 /* In case we need to reset origin, blanking hook returns 1 */ 4706 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? VESA_VSYNC_SUSPEND : 4707 (vesa_blank_mode + 1), 0); 4708 console_blanked = fg_console + 1; 4709 if (i) 4710 set_origin(vc); 4711 4712 if (console_blank_hook && console_blank_hook(1)) 4713 return; 4714 4715 if (vesa_off_interval && vesa_blank_mode) { 4716 blank_state = blank_vesa_wait; 4717 mod_timer(&console_timer, jiffies + vesa_off_interval); 4718 } 4719 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num); 4720 } 4721 EXPORT_SYMBOL(do_blank_screen); 4722 4723 /* 4724 * Called by timer as well as from vt_console_driver 4725 */ 4726 void do_unblank_screen(int leaving_gfx) 4727 { 4728 struct vc_data *vc; 4729 4730 /* This should now always be called from a "sane" (read: can schedule) 4731 * context for the sake of the low level drivers, except in the special 4732 * case of oops_in_progress 4733 */ 4734 if (!oops_in_progress) 4735 might_sleep(); 4736 4737 WARN_CONSOLE_UNLOCKED(); 4738 4739 ignore_poke = 0; 4740 if (!console_blanked) 4741 return; 4742 if (!vc_cons_allocated(fg_console)) { 4743 /* impossible */ 4744 pr_warn("unblank_screen: tty %d not allocated ??\n", 4745 fg_console + 1); 4746 return; 4747 } 4748 vc = vc_cons[fg_console].d; 4749 if (vc->vc_mode != KD_TEXT) 4750 return; /* but leave console_blanked != 0 */ 4751 4752 if (blankinterval) { 4753 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4754 blank_state = blank_normal_wait; 4755 } 4756 4757 console_blanked = 0; 4758 if (vc->vc_sw->con_blank(vc, VESA_NO_BLANKING, leaving_gfx)) 4759 /* Low-level driver cannot restore -> do it ourselves */ 4760 update_screen(vc); 4761 if (console_blank_hook) 4762 console_blank_hook(0); 4763 set_palette(vc); 4764 set_cursor(vc); 4765 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num); 4766 notify_update(vc); 4767 } 4768 EXPORT_SYMBOL(do_unblank_screen); 4769 4770 /* 4771 * This is called by the outside world to cause a forced unblank, mostly for 4772 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually 4773 * call it with 1 as an argument and so force a mode restore... that may kill 4774 * X or at least garbage the screen but would also make the Oops visible... 4775 */ 4776 static void unblank_screen(void) 4777 { 4778 do_unblank_screen(0); 4779 } 4780 4781 /* 4782 * We defer the timer blanking to work queue so it can take the console mutex 4783 * (console operations can still happen at irq time, but only from printk which 4784 * has the console mutex. Not perfect yet, but better than no locking 4785 */ 4786 static void blank_screen_t(struct timer_list *unused) 4787 { 4788 blank_timer_expired = 1; 4789 schedule_work(&console_work); 4790 } 4791 4792 void poke_blanked_console(void) 4793 { 4794 WARN_CONSOLE_UNLOCKED(); 4795 4796 /* Add this so we quickly catch whoever might call us in a non 4797 * safe context. Nowadays, unblank_screen() isn't to be called in 4798 * atomic contexts and is allowed to schedule (with the special case 4799 * of oops_in_progress, but that isn't of any concern for this 4800 * function. --BenH. 4801 */ 4802 might_sleep(); 4803 4804 /* This isn't perfectly race free, but a race here would be mostly harmless, 4805 * at worst, we'll do a spurious blank and it's unlikely 4806 */ 4807 timer_delete(&console_timer); 4808 blank_timer_expired = 0; 4809 4810 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS) 4811 return; 4812 if (console_blanked) 4813 unblank_screen(); 4814 else if (blankinterval) { 4815 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4816 blank_state = blank_normal_wait; 4817 } 4818 } 4819 4820 /* 4821 * Palettes 4822 */ 4823 4824 static void set_palette(struct vc_data *vc) 4825 { 4826 WARN_CONSOLE_UNLOCKED(); 4827 4828 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette) 4829 vc->vc_sw->con_set_palette(vc, color_table); 4830 } 4831 4832 /* 4833 * Load palette into the DAC registers. arg points to a colour 4834 * map, 3 bytes per colour, 16 colours, range from 0 to 255. 4835 */ 4836 4837 int con_set_cmap(unsigned char __user *arg) 4838 { 4839 int i, j, k; 4840 unsigned char colormap[3*16]; 4841 4842 if (copy_from_user(colormap, arg, sizeof(colormap))) 4843 return -EFAULT; 4844 4845 guard(console_lock)(); 4846 for (i = k = 0; i < 16; i++) { 4847 default_red[i] = colormap[k++]; 4848 default_grn[i] = colormap[k++]; 4849 default_blu[i] = colormap[k++]; 4850 } 4851 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4852 if (!vc_cons_allocated(i)) 4853 continue; 4854 for (j = k = 0; j < 16; j++) { 4855 vc_cons[i].d->vc_palette[k++] = default_red[j]; 4856 vc_cons[i].d->vc_palette[k++] = default_grn[j]; 4857 vc_cons[i].d->vc_palette[k++] = default_blu[j]; 4858 } 4859 set_palette(vc_cons[i].d); 4860 } 4861 4862 return 0; 4863 } 4864 4865 int con_get_cmap(unsigned char __user *arg) 4866 { 4867 int i, k; 4868 unsigned char colormap[3*16]; 4869 4870 scoped_guard(console_lock) 4871 for (i = k = 0; i < 16; i++) { 4872 colormap[k++] = default_red[i]; 4873 colormap[k++] = default_grn[i]; 4874 colormap[k++] = default_blu[i]; 4875 } 4876 4877 if (copy_to_user(arg, colormap, sizeof(colormap))) 4878 return -EFAULT; 4879 4880 return 0; 4881 } 4882 4883 void reset_palette(struct vc_data *vc) 4884 { 4885 int j, k; 4886 for (j=k=0; j<16; j++) { 4887 vc->vc_palette[k++] = default_red[j]; 4888 vc->vc_palette[k++] = default_grn[j]; 4889 vc->vc_palette[k++] = default_blu[j]; 4890 } 4891 set_palette(vc); 4892 } 4893 4894 /* 4895 * Font switching 4896 * 4897 * Currently we only support fonts up to 128 pixels wide, at a maximum height 4898 * of 128 pixels. Userspace fontdata may have to be stored with 32 bytes 4899 * (shorts/ints, depending on width) reserved for each character which is 4900 * kinda wasty, but this is done in order to maintain compatibility with the 4901 * EGA/VGA fonts. It is up to the actual low-level console-driver convert data 4902 * into its favorite format (maybe we should add a `fontoffset' field to the 4903 * `display' structure so we won't have to convert the fontdata all the time. 4904 * /Jes 4905 */ 4906 4907 #define max_font_width 64 4908 #define max_font_height 128 4909 #define max_font_glyphs 512 4910 #define max_font_size (max_font_glyphs*max_font_width*max_font_height) 4911 4912 static int con_font_get(struct vc_data *vc, struct console_font_op *op) 4913 { 4914 struct console_font font; 4915 int c; 4916 unsigned int vpitch = op->op == KD_FONT_OP_GET_TALL ? op->height : 32; 4917 4918 if (vpitch > max_font_height) 4919 return -EINVAL; 4920 4921 void *font_data __free(kvfree) = NULL; 4922 if (op->data) { 4923 font.data = font_data = kvzalloc(max_font_size, GFP_KERNEL); 4924 if (!font.data) 4925 return -ENOMEM; 4926 } else 4927 font.data = NULL; 4928 4929 scoped_guard(console_lock) { 4930 if (vc->vc_mode != KD_TEXT) 4931 return -EINVAL; 4932 if (!vc->vc_sw->con_font_get) 4933 return -ENOSYS; 4934 4935 int ret = vc->vc_sw->con_font_get(vc, &font, vpitch); 4936 if (ret) 4937 return ret; 4938 } 4939 4940 c = DIV_ROUND_UP(font.width, 8) * vpitch * font.charcount; 4941 4942 if (op->data && font.charcount > op->charcount) 4943 return -ENOSPC; 4944 if (font.width > op->width || font.height > op->height) 4945 return -ENOSPC; 4946 4947 op->height = font.height; 4948 op->width = font.width; 4949 op->charcount = font.charcount; 4950 4951 if (op->data && copy_to_user(op->data, font.data, c)) 4952 return -EFAULT; 4953 4954 return 0; 4955 } 4956 4957 static int con_font_set(struct vc_data *vc, const struct console_font_op *op) 4958 { 4959 struct console_font font; 4960 int size; 4961 unsigned int vpitch = op->op == KD_FONT_OP_SET_TALL ? op->height : 32; 4962 4963 if (!op->data) 4964 return -EINVAL; 4965 if (op->charcount > max_font_glyphs) 4966 return -EINVAL; 4967 if (op->width <= 0 || op->width > max_font_width || !op->height || 4968 op->height > max_font_height) 4969 return -EINVAL; 4970 if (vpitch < op->height) 4971 return -EINVAL; 4972 size = DIV_ROUND_UP(op->width, 8) * vpitch * op->charcount; 4973 if (size > max_font_size) 4974 return -ENOSPC; 4975 4976 void *font_data __free(kfree) = font.data = memdup_user(op->data, size); 4977 if (IS_ERR(font.data)) 4978 return PTR_ERR(font.data); 4979 4980 font.charcount = op->charcount; 4981 font.width = op->width; 4982 font.height = op->height; 4983 4984 guard(console_lock)(); 4985 4986 if (vc->vc_mode != KD_TEXT) 4987 return -EINVAL; 4988 if (!vc->vc_sw->con_font_set) 4989 return -ENOSYS; 4990 4991 if (vc_is_sel(vc)) 4992 clear_selection(); 4993 4994 return vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags); 4995 } 4996 4997 static int con_font_default(struct vc_data *vc, struct console_font_op *op) 4998 { 4999 struct console_font font = {.width = op->width, .height = op->height}; 5000 char name[MAX_FONT_NAME]; 5001 char *s = name; 5002 5003 if (!op->data) 5004 s = NULL; 5005 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0) 5006 return -EFAULT; 5007 else 5008 name[MAX_FONT_NAME - 1] = 0; 5009 5010 scoped_guard(console_lock) { 5011 if (vc->vc_mode != KD_TEXT) 5012 return -EINVAL; 5013 if (!vc->vc_sw->con_font_default) 5014 return -ENOSYS; 5015 5016 if (vc_is_sel(vc)) 5017 clear_selection(); 5018 int ret = vc->vc_sw->con_font_default(vc, &font, s); 5019 if (ret) 5020 return ret; 5021 } 5022 5023 op->width = font.width; 5024 op->height = font.height; 5025 5026 return 0; 5027 } 5028 5029 int con_font_op(struct vc_data *vc, struct console_font_op *op) 5030 { 5031 switch (op->op) { 5032 case KD_FONT_OP_SET: 5033 case KD_FONT_OP_SET_TALL: 5034 return con_font_set(vc, op); 5035 case KD_FONT_OP_GET: 5036 case KD_FONT_OP_GET_TALL: 5037 return con_font_get(vc, op); 5038 case KD_FONT_OP_SET_DEFAULT: 5039 return con_font_default(vc, op); 5040 case KD_FONT_OP_COPY: 5041 /* was buggy and never really used */ 5042 return -EINVAL; 5043 } 5044 return -ENOSYS; 5045 } 5046 5047 /* 5048 * Interface exported to selection and vcs. 5049 */ 5050 5051 /* used by selection */ 5052 u16 screen_glyph(const struct vc_data *vc, int offset) 5053 { 5054 u16 w = scr_readw(screenpos(vc, offset, true)); 5055 u16 c = w & 0xff; 5056 5057 if (w & vc->vc_hi_font_mask) 5058 c |= 0x100; 5059 return c; 5060 } 5061 EXPORT_SYMBOL_GPL(screen_glyph); 5062 5063 u32 screen_glyph_unicode(const struct vc_data *vc, int n) 5064 { 5065 u32 **uni_lines = vc->vc_uni_lines; 5066 5067 if (uni_lines) 5068 return uni_lines[n / vc->vc_cols][n % vc->vc_cols]; 5069 5070 return inverse_translate(vc, screen_glyph(vc, n * 2), true); 5071 } 5072 EXPORT_SYMBOL_GPL(screen_glyph_unicode); 5073 5074 /* used by vcs - note the word offset */ 5075 unsigned short *screen_pos(const struct vc_data *vc, int w_offset, bool viewed) 5076 { 5077 return screenpos(vc, 2 * w_offset, viewed); 5078 } 5079 EXPORT_SYMBOL_GPL(screen_pos); 5080 5081 void getconsxy(const struct vc_data *vc, unsigned char xy[static 2]) 5082 { 5083 /* clamp values if they don't fit */ 5084 xy[0] = min(vc->state.x, 0xFFu); 5085 xy[1] = min(vc->state.y, 0xFFu); 5086 } 5087 5088 void putconsxy(struct vc_data *vc, unsigned char xy[static const 2]) 5089 { 5090 hide_cursor(vc); 5091 gotoxy(vc, xy[0], xy[1]); 5092 set_cursor(vc); 5093 } 5094 5095 u16 vcs_scr_readw(const struct vc_data *vc, const u16 *org) 5096 { 5097 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1) 5098 return softcursor_original; 5099 return scr_readw(org); 5100 } 5101 5102 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org) 5103 { 5104 scr_writew(val, org); 5105 if ((unsigned long)org == vc->vc_pos) { 5106 softcursor_original = -1; 5107 add_softcursor(vc); 5108 } 5109 } 5110 5111 void vcs_scr_updated(struct vc_data *vc) 5112 { 5113 notify_update(vc); 5114 } 5115