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 if (*vc->uni_pagedict_loc) 1138 con_free_unimap(vc); 1139 kfree(vc); 1140 vc_cons[currcons].d = NULL; 1141 return err; 1142 } 1143 1144 static inline int resize_screen(struct vc_data *vc, int width, int height, 1145 bool from_user) 1146 { 1147 /* Resizes the resolution of the display adapater */ 1148 int err = 0; 1149 1150 if (vc->vc_sw->con_resize) 1151 err = vc->vc_sw->con_resize(vc, width, height, from_user); 1152 1153 return err; 1154 } 1155 1156 /** 1157 * vc_do_resize - resizing method for the tty 1158 * @tty: tty being resized 1159 * @vc: virtual console private data 1160 * @cols: columns 1161 * @lines: lines 1162 * @from_user: invoked by a user? 1163 * 1164 * Resize a virtual console, clipping according to the actual constraints. If 1165 * the caller passes a tty structure then update the termios winsize 1166 * information and perform any necessary signal handling. 1167 * 1168 * Locking: Caller must hold the console semaphore. Takes the termios rwsem and 1169 * ctrl.lock of the tty IFF a tty is passed. 1170 */ 1171 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc, 1172 unsigned int cols, unsigned int lines, bool from_user) 1173 { 1174 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0; 1175 unsigned long end; 1176 unsigned int old_rows, old_row_size, first_copied_row; 1177 unsigned int new_cols, new_rows, new_row_size, new_screen_size; 1178 unsigned short *oldscreen, *newscreen; 1179 u32 **new_uniscr = NULL; 1180 1181 WARN_CONSOLE_UNLOCKED(); 1182 1183 if (cols > VC_MAXCOL || lines > VC_MAXROW) 1184 return -EINVAL; 1185 1186 new_cols = (cols ? cols : vc->vc_cols); 1187 new_rows = (lines ? lines : vc->vc_rows); 1188 new_row_size = new_cols << 1; 1189 new_screen_size = new_row_size * new_rows; 1190 1191 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows) { 1192 /* 1193 * This function is being called here to cover the case 1194 * where the userspace calls the FBIOPUT_VSCREENINFO twice, 1195 * passing the same fb_var_screeninfo containing the fields 1196 * yres/xres equal to a number non-multiple of vc_font.height 1197 * and yres_virtual/xres_virtual equal to number lesser than the 1198 * vc_font.height and yres/xres. 1199 * In the second call, the struct fb_var_screeninfo isn't 1200 * being modified by the underlying driver because of the 1201 * if above, and this causes the fbcon_display->vrows to become 1202 * negative and it eventually leads to out-of-bound 1203 * access by the imageblit function. 1204 * To give the correct values to the struct and to not have 1205 * to deal with possible errors from the code below, we call 1206 * the resize_screen here as well. 1207 */ 1208 return resize_screen(vc, new_cols, new_rows, from_user); 1209 } 1210 1211 if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size) 1212 return -EINVAL; 1213 newscreen = kzalloc(new_screen_size, GFP_USER); 1214 if (!newscreen) 1215 return -ENOMEM; 1216 1217 if (vc->vc_uni_lines) { 1218 new_uniscr = vc_uniscr_alloc(new_cols, new_rows); 1219 if (!new_uniscr) { 1220 kfree(newscreen); 1221 return -ENOMEM; 1222 } 1223 } 1224 1225 if (vc_is_sel(vc)) 1226 clear_selection(); 1227 1228 old_rows = vc->vc_rows; 1229 old_row_size = vc->vc_size_row; 1230 1231 err = resize_screen(vc, new_cols, new_rows, from_user); 1232 if (err) { 1233 kfree(newscreen); 1234 vc_uniscr_free(new_uniscr); 1235 return err; 1236 } 1237 1238 vc->vc_rows = new_rows; 1239 vc->vc_cols = new_cols; 1240 vc->vc_size_row = new_row_size; 1241 vc->vc_screenbuf_size = new_screen_size; 1242 1243 rlth = min(old_row_size, new_row_size); 1244 rrem = new_row_size - rlth; 1245 old_origin = vc->vc_origin; 1246 new_origin = (long) newscreen; 1247 new_scr_end = new_origin + new_screen_size; 1248 1249 if (vc->state.y > new_rows) { 1250 if (old_rows - vc->state.y < new_rows) { 1251 /* 1252 * Cursor near the bottom, copy contents from the 1253 * bottom of buffer 1254 */ 1255 first_copied_row = (old_rows - new_rows); 1256 } else { 1257 /* 1258 * Cursor is in no man's land, copy 1/2 screenful 1259 * from the top and bottom of cursor position 1260 */ 1261 first_copied_row = (vc->state.y - new_rows/2); 1262 } 1263 old_origin += first_copied_row * old_row_size; 1264 } else 1265 first_copied_row = 0; 1266 end = old_origin + old_row_size * min(old_rows, new_rows); 1267 1268 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows, 1269 vc->vc_uni_lines, rlth/2, first_copied_row, 1270 min(old_rows, new_rows)); 1271 vc_uniscr_set(vc, new_uniscr); 1272 1273 update_attr(vc); 1274 1275 while (old_origin < end) { 1276 scr_memcpyw((unsigned short *) new_origin, 1277 (unsigned short *) old_origin, rlth); 1278 if (rrem) 1279 scr_memsetw((void *)(new_origin + rlth), 1280 vc->vc_video_erase_char, rrem); 1281 old_origin += old_row_size; 1282 new_origin += new_row_size; 1283 } 1284 if (new_scr_end > new_origin) 1285 scr_memsetw((void *)new_origin, vc->vc_video_erase_char, 1286 new_scr_end - new_origin); 1287 oldscreen = vc->vc_screenbuf; 1288 vc->vc_screenbuf = newscreen; 1289 vc->vc_screenbuf_size = new_screen_size; 1290 set_origin(vc); 1291 kfree(oldscreen); 1292 1293 /* do part of a reset_terminal() */ 1294 vc->vc_top = 0; 1295 vc->vc_bottom = vc->vc_rows; 1296 gotoxy(vc, vc->state.x, vc->state.y); 1297 save_cur(vc); 1298 1299 if (tty) { 1300 /* Rewrite the requested winsize data with the actual 1301 resulting sizes */ 1302 struct winsize ws; 1303 memset(&ws, 0, sizeof(ws)); 1304 ws.ws_row = vc->vc_rows; 1305 ws.ws_col = vc->vc_cols; 1306 ws.ws_ypixel = vc->vc_scan_lines; 1307 tty_do_resize(tty, &ws); 1308 } 1309 1310 if (con_is_visible(vc)) 1311 update_screen(vc); 1312 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num); 1313 notify_update(vc); 1314 return err; 1315 } 1316 1317 /** 1318 * __vc_resize - resize a VT 1319 * @vc: virtual console 1320 * @cols: columns 1321 * @rows: rows 1322 * @from_user: invoked by a user? 1323 * 1324 * Resize a virtual console as seen from the console end of things. We use the 1325 * common vc_do_resize() method to update the structures. 1326 * 1327 * Locking: The caller must hold the console sem to protect console internals 1328 * and @vc->port.tty. 1329 */ 1330 int __vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows, 1331 bool from_user) 1332 { 1333 return vc_do_resize(vc->port.tty, vc, cols, rows, from_user); 1334 } 1335 EXPORT_SYMBOL(__vc_resize); 1336 1337 /** 1338 * vt_resize - resize a VT 1339 * @tty: tty to resize 1340 * @ws: winsize attributes 1341 * 1342 * Resize a virtual terminal. This is called by the tty layer as we register 1343 * our own handler for resizing. The mutual helper does all the actual work. 1344 * 1345 * Locking: Takes the console sem and the called methods then take the tty 1346 * termios_rwsem and the tty ctrl.lock in that order. 1347 */ 1348 static int vt_resize(struct tty_struct *tty, struct winsize *ws) 1349 { 1350 struct vc_data *vc = tty->driver_data; 1351 1352 guard(console_lock)(); 1353 return vc_do_resize(tty, vc, ws->ws_col, ws->ws_row, false); 1354 } 1355 1356 struct vc_data *vc_deallocate(unsigned int currcons) 1357 { 1358 struct vc_data *vc = NULL; 1359 1360 WARN_CONSOLE_UNLOCKED(); 1361 1362 if (vc_cons_allocated(currcons)) { 1363 struct vt_notifier_param param; 1364 1365 param.vc = vc = vc_cons[currcons].d; 1366 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, ¶m); 1367 vcs_remove_sysfs(currcons); 1368 visual_deinit(vc); 1369 con_free_unimap(vc); 1370 put_pid(vc->vt_pid); 1371 vc_uniscr_set(vc, NULL); 1372 kfree(vc->vc_screenbuf); 1373 vc_cons[currcons].d = NULL; 1374 if (vc->vc_saved_screen != NULL) { 1375 kfree(vc->vc_saved_screen); 1376 vc->vc_saved_screen = NULL; 1377 } 1378 vc_uniscr_free(vc->vc_saved_uni_lines); 1379 vc->vc_saved_uni_lines = NULL; 1380 } 1381 return vc; 1382 } 1383 1384 /* 1385 * VT102 emulator 1386 */ 1387 1388 enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt}; 1389 1390 #define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x)) 1391 #define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x)) 1392 #define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x)) 1393 1394 #define decarm VC_REPEAT 1395 #define decckm VC_CKMODE 1396 #define kbdapplic VC_APPLIC 1397 #define lnm VC_CRLF 1398 1399 const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7, 1400 8,12,10,14, 9,13,11,15 }; 1401 EXPORT_SYMBOL(color_table); 1402 1403 /* the default colour table, for VGA+ colour systems */ 1404 unsigned char default_red[] = { 1405 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 1406 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff 1407 }; 1408 module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR); 1409 EXPORT_SYMBOL(default_red); 1410 1411 unsigned char default_grn[] = { 1412 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa, 1413 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff 1414 }; 1415 module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR); 1416 EXPORT_SYMBOL(default_grn); 1417 1418 unsigned char default_blu[] = { 1419 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa, 1420 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff 1421 }; 1422 module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR); 1423 EXPORT_SYMBOL(default_blu); 1424 1425 /* 1426 * gotoxy() must verify all boundaries, because the arguments 1427 * might also be negative. If the given position is out of 1428 * bounds, the cursor is placed at the nearest margin. 1429 */ 1430 static void gotoxy(struct vc_data *vc, int new_x, int new_y) 1431 { 1432 int min_y, max_y; 1433 1434 if (new_x < 0) 1435 vc->state.x = 0; 1436 else { 1437 if (new_x >= vc->vc_cols) 1438 vc->state.x = vc->vc_cols - 1; 1439 else 1440 vc->state.x = new_x; 1441 } 1442 1443 if (vc->vc_decom) { 1444 min_y = vc->vc_top; 1445 max_y = vc->vc_bottom; 1446 } else { 1447 min_y = 0; 1448 max_y = vc->vc_rows; 1449 } 1450 if (new_y < min_y) 1451 vc->state.y = min_y; 1452 else if (new_y >= max_y) 1453 vc->state.y = max_y - 1; 1454 else 1455 vc->state.y = new_y; 1456 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row + 1457 (vc->state.x << 1); 1458 vc->vc_need_wrap = 0; 1459 } 1460 1461 /* for absolute user moves, when decom is set */ 1462 static void gotoxay(struct vc_data *vc, int new_x, int new_y) 1463 { 1464 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y); 1465 } 1466 1467 void scrollback(struct vc_data *vc) 1468 { 1469 scrolldelta(-(vc->vc_rows / 2)); 1470 } 1471 1472 void scrollfront(struct vc_data *vc, int lines) 1473 { 1474 if (!lines) 1475 lines = vc->vc_rows / 2; 1476 scrolldelta(lines); 1477 } 1478 1479 static void lf(struct vc_data *vc) 1480 { 1481 /* don't scroll if above bottom of scrolling region, or 1482 * if below scrolling region 1483 */ 1484 if (vc->state.y + 1 == vc->vc_bottom) 1485 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1); 1486 else if (vc->state.y < vc->vc_rows - 1) { 1487 vc->state.y++; 1488 vc->vc_pos += vc->vc_size_row; 1489 } 1490 vc->vc_need_wrap = 0; 1491 notify_write(vc, '\n'); 1492 } 1493 1494 static void ri(struct vc_data *vc) 1495 { 1496 /* don't scroll if below top of scrolling region, or 1497 * if above scrolling region 1498 */ 1499 if (vc->state.y == vc->vc_top) 1500 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1); 1501 else if (vc->state.y > 0) { 1502 vc->state.y--; 1503 vc->vc_pos -= vc->vc_size_row; 1504 } 1505 vc->vc_need_wrap = 0; 1506 } 1507 1508 static inline void cr(struct vc_data *vc) 1509 { 1510 vc->vc_pos -= vc->state.x << 1; 1511 vc->vc_need_wrap = vc->state.x = 0; 1512 notify_write(vc, '\r'); 1513 } 1514 1515 static inline void bs(struct vc_data *vc) 1516 { 1517 if (vc->state.x) { 1518 vc->vc_pos -= 2; 1519 vc->state.x--; 1520 vc->vc_need_wrap = 0; 1521 notify_write(vc, '\b'); 1522 } 1523 } 1524 1525 static inline void del(struct vc_data *vc) 1526 { 1527 /* ignored */ 1528 } 1529 1530 enum CSI_J { 1531 CSI_J_CURSOR_TO_END = 0, 1532 CSI_J_START_TO_CURSOR = 1, 1533 CSI_J_VISIBLE = 2, 1534 CSI_J_FULL = 3, 1535 }; 1536 1537 static void csi_J(struct vc_data *vc, enum CSI_J vpar) 1538 { 1539 unsigned short *start; 1540 unsigned int count; 1541 1542 switch (vpar) { 1543 case CSI_J_CURSOR_TO_END: 1544 vc_uniscr_clear_line(vc, vc->state.x, 1545 vc->vc_cols - vc->state.x); 1546 vc_uniscr_clear_lines(vc, vc->state.y + 1, 1547 vc->vc_rows - vc->state.y - 1); 1548 count = (vc->vc_scr_end - vc->vc_pos) >> 1; 1549 start = (unsigned short *)vc->vc_pos; 1550 break; 1551 case CSI_J_START_TO_CURSOR: 1552 vc_uniscr_clear_line(vc, 0, vc->state.x + 1); 1553 vc_uniscr_clear_lines(vc, 0, vc->state.y); 1554 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1; 1555 start = (unsigned short *)vc->vc_origin; 1556 break; 1557 case CSI_J_FULL: 1558 flush_scrollback(vc); 1559 fallthrough; 1560 case CSI_J_VISIBLE: 1561 vc_uniscr_clear_lines(vc, 0, vc->vc_rows); 1562 count = vc->vc_cols * vc->vc_rows; 1563 start = (unsigned short *)vc->vc_origin; 1564 break; 1565 default: 1566 return; 1567 } 1568 scr_memsetw(start, vc->vc_video_erase_char, 2 * count); 1569 if (con_should_update(vc)) 1570 do_update_region(vc, (unsigned long) start, count); 1571 vc->vc_need_wrap = 0; 1572 } 1573 1574 enum { 1575 CSI_K_CURSOR_TO_LINEEND = 0, 1576 CSI_K_LINESTART_TO_CURSOR = 1, 1577 CSI_K_LINE = 2, 1578 }; 1579 1580 static void csi_K(struct vc_data *vc) 1581 { 1582 unsigned int count; 1583 unsigned short *start = (unsigned short *)vc->vc_pos; 1584 int offset; 1585 1586 switch (vc->vc_par[0]) { 1587 case CSI_K_CURSOR_TO_LINEEND: 1588 offset = 0; 1589 count = vc->vc_cols - vc->state.x; 1590 break; 1591 case CSI_K_LINESTART_TO_CURSOR: 1592 offset = -vc->state.x; 1593 count = vc->state.x + 1; 1594 break; 1595 case CSI_K_LINE: 1596 offset = -vc->state.x; 1597 count = vc->vc_cols; 1598 break; 1599 default: 1600 return; 1601 } 1602 vc_uniscr_clear_line(vc, vc->state.x + offset, count); 1603 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count); 1604 vc->vc_need_wrap = 0; 1605 if (con_should_update(vc)) 1606 do_update_region(vc, (unsigned long)(start + offset), count); 1607 } 1608 1609 /* erase the following count positions */ 1610 static void csi_X(struct vc_data *vc) 1611 { /* not vt100? */ 1612 unsigned int count = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 1613 1614 vc_uniscr_clear_line(vc, vc->state.x, count); 1615 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count); 1616 if (con_should_update(vc)) 1617 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, count); 1618 vc->vc_need_wrap = 0; 1619 } 1620 1621 static void default_attr(struct vc_data *vc) 1622 { 1623 vc->state.intensity = VCI_NORMAL; 1624 vc->state.italic = false; 1625 vc->state.underline = false; 1626 vc->state.reverse = false; 1627 vc->state.blink = false; 1628 vc->state.color = vc->vc_def_color; 1629 } 1630 1631 struct rgb { u8 r; u8 g; u8 b; }; 1632 1633 static void rgb_from_256(unsigned int i, struct rgb *c) 1634 { 1635 if (i < 8) { /* Standard colours. */ 1636 c->r = i&1 ? 0xaa : 0x00; 1637 c->g = i&2 ? 0xaa : 0x00; 1638 c->b = i&4 ? 0xaa : 0x00; 1639 } else if (i < 16) { 1640 c->r = i&1 ? 0xff : 0x55; 1641 c->g = i&2 ? 0xff : 0x55; 1642 c->b = i&4 ? 0xff : 0x55; 1643 } else if (i < 232) { /* 6x6x6 colour cube. */ 1644 i -= 16; 1645 c->b = i % 6 * 255 / 6; 1646 i /= 6; 1647 c->g = i % 6 * 255 / 6; 1648 i /= 6; 1649 c->r = i * 255 / 6; 1650 } else /* Grayscale ramp. */ 1651 c->r = c->g = c->b = i * 10 - 2312; 1652 } 1653 1654 static void rgb_foreground(struct vc_data *vc, const struct rgb *c) 1655 { 1656 u8 hue = 0, max = max3(c->r, c->g, c->b); 1657 1658 if (c->r > max / 2) 1659 hue |= 4; 1660 if (c->g > max / 2) 1661 hue |= 2; 1662 if (c->b > max / 2) 1663 hue |= 1; 1664 1665 if (hue == 7 && max <= 0x55) { 1666 hue = 0; 1667 vc->state.intensity = VCI_BOLD; 1668 } else if (max > 0xaa) 1669 vc->state.intensity = VCI_BOLD; 1670 else 1671 vc->state.intensity = VCI_NORMAL; 1672 1673 vc->state.color = (vc->state.color & 0xf0) | hue; 1674 } 1675 1676 static void rgb_background(struct vc_data *vc, const struct rgb *c) 1677 { 1678 /* For backgrounds, err on the dark side. */ 1679 vc->state.color = (vc->state.color & 0x0f) 1680 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3; 1681 } 1682 1683 /* 1684 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes 1685 * and thus need to be detected and ignored by hand. 1686 * 1687 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in 1688 * supporting them. 1689 */ 1690 static int vc_t416_color(struct vc_data *vc, int i, 1691 void(*set_color)(struct vc_data *vc, const struct rgb *c)) 1692 { 1693 struct rgb c; 1694 1695 i++; 1696 if (i > vc->vc_npar) 1697 return i; 1698 1699 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) { 1700 /* 256 colours */ 1701 i++; 1702 rgb_from_256(vc->vc_par[i], &c); 1703 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) { 1704 /* 24 bit */ 1705 c.r = vc->vc_par[i + 1]; 1706 c.g = vc->vc_par[i + 2]; 1707 c.b = vc->vc_par[i + 3]; 1708 i += 3; 1709 } else 1710 return i; 1711 1712 set_color(vc, &c); 1713 1714 return i; 1715 } 1716 1717 enum { 1718 CSI_m_DEFAULT = 0, 1719 CSI_m_BOLD = 1, 1720 CSI_m_HALF_BRIGHT = 2, 1721 CSI_m_ITALIC = 3, 1722 CSI_m_UNDERLINE = 4, 1723 CSI_m_BLINK = 5, 1724 CSI_m_REVERSE = 7, 1725 CSI_m_PRI_FONT = 10, 1726 CSI_m_ALT_FONT1 = 11, 1727 CSI_m_ALT_FONT2 = 12, 1728 CSI_m_DOUBLE_UNDERLINE = 21, 1729 CSI_m_NORMAL_INTENSITY = 22, 1730 CSI_m_NO_ITALIC = 23, 1731 CSI_m_NO_UNDERLINE = 24, 1732 CSI_m_NO_BLINK = 25, 1733 CSI_m_NO_REVERSE = 27, 1734 CSI_m_FG_COLOR_BEG = 30, 1735 CSI_m_FG_COLOR_END = 37, 1736 CSI_m_FG_COLOR = 38, 1737 CSI_m_DEFAULT_FG_COLOR = 39, 1738 CSI_m_BG_COLOR_BEG = 40, 1739 CSI_m_BG_COLOR_END = 47, 1740 CSI_m_BG_COLOR = 48, 1741 CSI_m_DEFAULT_BG_COLOR = 49, 1742 CSI_m_UNDERLINE_COLOR = 58, 1743 CSI_m_BRIGHT_FG_COLOR_BEG = 90, 1744 CSI_m_BRIGHT_FG_COLOR_END = 97, 1745 CSI_m_BRIGHT_FG_COLOR_OFF = CSI_m_BRIGHT_FG_COLOR_BEG - CSI_m_FG_COLOR_BEG, 1746 CSI_m_BRIGHT_BG_COLOR_BEG = 100, 1747 CSI_m_BRIGHT_BG_COLOR_END = 107, 1748 CSI_m_BRIGHT_BG_COLOR_OFF = CSI_m_BRIGHT_BG_COLOR_BEG - CSI_m_BG_COLOR_BEG, 1749 }; 1750 1751 /* console_lock is held */ 1752 static void csi_m(struct vc_data *vc) 1753 { 1754 int i; 1755 1756 for (i = 0; i <= vc->vc_npar; i++) 1757 switch (vc->vc_par[i]) { 1758 case CSI_m_DEFAULT: /* all attributes off */ 1759 default_attr(vc); 1760 break; 1761 case CSI_m_BOLD: 1762 vc->state.intensity = VCI_BOLD; 1763 break; 1764 case CSI_m_HALF_BRIGHT: 1765 vc->state.intensity = VCI_HALF_BRIGHT; 1766 break; 1767 case CSI_m_ITALIC: 1768 vc->state.italic = true; 1769 break; 1770 case CSI_m_DOUBLE_UNDERLINE: 1771 /* 1772 * No console drivers support double underline, so 1773 * convert it to a single underline. 1774 */ 1775 case CSI_m_UNDERLINE: 1776 vc->state.underline = true; 1777 break; 1778 case CSI_m_BLINK: 1779 vc->state.blink = true; 1780 break; 1781 case CSI_m_REVERSE: 1782 vc->state.reverse = true; 1783 break; 1784 case CSI_m_PRI_FONT: /* ANSI X3.64-1979 (SCO-ish?) 1785 * Select primary font, don't display control chars if 1786 * defined, don't set bit 8 on output. 1787 */ 1788 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc); 1789 vc->vc_disp_ctrl = 0; 1790 vc->vc_toggle_meta = 0; 1791 break; 1792 case CSI_m_ALT_FONT1: /* ANSI X3.64-1979 (SCO-ish?) 1793 * Select first alternate font, lets chars < 32 be 1794 * displayed as ROM chars. 1795 */ 1796 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1797 vc->vc_disp_ctrl = 1; 1798 vc->vc_toggle_meta = 0; 1799 break; 1800 case CSI_m_ALT_FONT2: /* ANSI X3.64-1979 (SCO-ish?) 1801 * Select second alternate font, toggle high bit 1802 * before displaying as ROM char. 1803 */ 1804 vc->vc_translate = set_translate(IBMPC_MAP, vc); 1805 vc->vc_disp_ctrl = 1; 1806 vc->vc_toggle_meta = 1; 1807 break; 1808 case CSI_m_NORMAL_INTENSITY: 1809 vc->state.intensity = VCI_NORMAL; 1810 break; 1811 case CSI_m_NO_ITALIC: 1812 vc->state.italic = false; 1813 break; 1814 case CSI_m_NO_UNDERLINE: 1815 vc->state.underline = false; 1816 break; 1817 case CSI_m_NO_BLINK: 1818 vc->state.blink = false; 1819 break; 1820 case CSI_m_NO_REVERSE: 1821 vc->state.reverse = false; 1822 break; 1823 case CSI_m_FG_COLOR: 1824 i = vc_t416_color(vc, i, rgb_foreground); 1825 break; 1826 case CSI_m_BG_COLOR: 1827 i = vc_t416_color(vc, i, rgb_background); 1828 break; 1829 case CSI_m_DEFAULT_FG_COLOR: 1830 vc->state.color = (vc->vc_def_color & 0x0f) | 1831 (vc->state.color & 0xf0); 1832 break; 1833 case CSI_m_DEFAULT_BG_COLOR: 1834 vc->state.color = (vc->vc_def_color & 0xf0) | 1835 (vc->state.color & 0x0f); 1836 break; 1837 case CSI_m_BRIGHT_FG_COLOR_BEG ... CSI_m_BRIGHT_FG_COLOR_END: 1838 vc->state.intensity = VCI_BOLD; 1839 vc->vc_par[i] -= CSI_m_BRIGHT_FG_COLOR_OFF; 1840 fallthrough; 1841 case CSI_m_FG_COLOR_BEG ... CSI_m_FG_COLOR_END: 1842 vc->vc_par[i] -= CSI_m_FG_COLOR_BEG; 1843 vc->state.color = color_table[vc->vc_par[i]] | 1844 (vc->state.color & 0xf0); 1845 break; 1846 case CSI_m_BRIGHT_BG_COLOR_BEG ... CSI_m_BRIGHT_BG_COLOR_END: 1847 vc->vc_par[i] -= CSI_m_BRIGHT_BG_COLOR_OFF; 1848 fallthrough; 1849 case CSI_m_BG_COLOR_BEG ... CSI_m_BG_COLOR_END: 1850 vc->vc_par[i] -= CSI_m_BG_COLOR_BEG; 1851 vc->state.color = (color_table[vc->vc_par[i]] << 4) | 1852 (vc->state.color & 0x0f); 1853 break; 1854 } 1855 update_attr(vc); 1856 } 1857 1858 static void respond_string(const char *p, size_t len, struct tty_port *port) 1859 { 1860 tty_insert_flip_string(port, p, len); 1861 tty_flip_buffer_push(port); 1862 } 1863 1864 static void cursor_report(struct vc_data *vc, struct tty_struct *tty) 1865 { 1866 char buf[40]; 1867 int len; 1868 1869 len = sprintf(buf, "\033[%d;%dR", vc->state.y + 1870 (vc->vc_decom ? vc->vc_top + 1 : 1), 1871 vc->state.x + 1); 1872 respond_string(buf, len, tty->port); 1873 } 1874 1875 static inline void status_report(struct tty_struct *tty) 1876 { 1877 static const char teminal_ok[] = "\033[0n"; 1878 1879 respond_string(teminal_ok, strlen(teminal_ok), tty->port); 1880 } 1881 1882 static inline void respond_ID(struct tty_struct *tty) 1883 { 1884 /* terminal answer to an ESC-Z or csi0c query. */ 1885 static const char vt102_id[] = "\033[?6c"; 1886 1887 respond_string(vt102_id, strlen(vt102_id), tty->port); 1888 } 1889 1890 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry) 1891 { 1892 char buf[8]; 1893 int len; 1894 1895 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), 1896 (char)('!' + mrx), (char)('!' + mry)); 1897 respond_string(buf, len, tty->port); 1898 } 1899 1900 /* invoked via ioctl(TIOCLINUX) and through set_selection_user */ 1901 int mouse_reporting(void) 1902 { 1903 return vc_cons[fg_console].d->vc_report_mouse; 1904 } 1905 1906 /* invoked via ioctl(TIOCLINUX) */ 1907 static int get_bracketed_paste(struct tty_struct *tty) 1908 { 1909 struct vc_data *vc = tty->driver_data; 1910 1911 return vc->vc_bracketed_paste; 1912 } 1913 1914 /* console_lock is held */ 1915 static void enter_alt_screen(struct vc_data *vc) 1916 { 1917 unsigned int size = vc->vc_rows * vc->vc_cols * 2; 1918 1919 if (vc->vc_saved_screen != NULL) 1920 return; /* Already inside an alt-screen */ 1921 vc->vc_saved_screen = kmemdup((u16 *)vc->vc_origin, size, GFP_KERNEL); 1922 if (vc->vc_saved_screen == NULL) 1923 return; 1924 vc->vc_saved_uni_lines = vc->vc_uni_lines; 1925 vc->vc_uni_lines = NULL; 1926 vc->vc_saved_rows = vc->vc_rows; 1927 vc->vc_saved_cols = vc->vc_cols; 1928 save_cur(vc); 1929 /* clear entire screen */ 1930 csi_J(vc, CSI_J_FULL); 1931 } 1932 1933 /* console_lock is held */ 1934 static void leave_alt_screen(struct vc_data *vc) 1935 { 1936 unsigned int rows = min(vc->vc_saved_rows, vc->vc_rows); 1937 unsigned int cols = min(vc->vc_saved_cols, vc->vc_cols); 1938 u16 *src, *dest; 1939 1940 if (vc->vc_saved_screen == NULL) 1941 return; /* Not inside an alt-screen */ 1942 for (unsigned int r = 0; r < rows; r++) { 1943 src = vc->vc_saved_screen + r * vc->vc_saved_cols; 1944 dest = ((u16 *)vc->vc_origin) + r * vc->vc_cols; 1945 memcpy(dest, src, 2 * cols); 1946 } 1947 /* 1948 * If the console was resized while in the alternate screen, 1949 * resize the saved unicode buffer to the current dimensions. 1950 * On allocation failure new_uniscr is NULL, causing the old 1951 * buffer to be freed and vc_uni_lines to be lazily rebuilt 1952 * via vc_uniscr_check() when next needed. 1953 */ 1954 if (vc->vc_saved_uni_lines && 1955 (vc->vc_saved_rows != vc->vc_rows || 1956 vc->vc_saved_cols != vc->vc_cols)) { 1957 u32 **new_uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows); 1958 1959 if (new_uniscr) 1960 vc_uniscr_copy_area(new_uniscr, vc->vc_cols, vc->vc_rows, 1961 vc->vc_saved_uni_lines, cols, 0, rows); 1962 vc_uniscr_free(vc->vc_saved_uni_lines); 1963 vc->vc_saved_uni_lines = new_uniscr; 1964 } 1965 vc_uniscr_set(vc, vc->vc_saved_uni_lines); 1966 vc->vc_saved_uni_lines = NULL; 1967 restore_cur(vc); 1968 /* Update the entire screen */ 1969 if (con_should_update(vc)) 1970 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 1971 kfree(vc->vc_saved_screen); 1972 vc->vc_saved_screen = NULL; 1973 } 1974 1975 enum { 1976 CSI_DEC_hl_CURSOR_KEYS = 1, /* CKM: cursor keys send ^[Ox/^[[x */ 1977 CSI_DEC_hl_132_COLUMNS = 3, /* COLM: 80/132 mode switch */ 1978 CSI_DEC_hl_REVERSE_VIDEO = 5, /* SCNM */ 1979 CSI_DEC_hl_ORIGIN_MODE = 6, /* OM: origin relative/absolute */ 1980 CSI_DEC_hl_AUTOWRAP = 7, /* AWM */ 1981 CSI_DEC_hl_AUTOREPEAT = 8, /* ARM */ 1982 CSI_DEC_hl_MOUSE_X10 = 9, 1983 CSI_DEC_hl_SHOW_CURSOR = 25, /* TCEM */ 1984 CSI_DEC_hl_MOUSE_VT200 = 1000, 1985 CSI_DEC_hl_ALT_SCREEN = 1049, 1986 CSI_DEC_hl_BRACKETED_PASTE = 2004, 1987 }; 1988 1989 /* console_lock is held */ 1990 static void csi_DEC_hl(struct vc_data *vc, bool on_off) 1991 { 1992 unsigned int i; 1993 1994 for (i = 0; i <= vc->vc_npar; i++) 1995 switch (vc->vc_par[i]) { 1996 case CSI_DEC_hl_CURSOR_KEYS: 1997 if (on_off) 1998 set_kbd(vc, decckm); 1999 else 2000 clr_kbd(vc, decckm); 2001 break; 2002 case CSI_DEC_hl_132_COLUMNS: /* unimplemented */ 2003 #if 0 2004 vc_resize(deccolm ? 132 : 80, vc->vc_rows); 2005 /* this alone does not suffice; some user mode 2006 utility has to change the hardware regs */ 2007 #endif 2008 break; 2009 case CSI_DEC_hl_REVERSE_VIDEO: 2010 if (vc->vc_decscnm != on_off) { 2011 vc->vc_decscnm = on_off; 2012 invert_screen(vc, 0, vc->vc_screenbuf_size, 2013 false); 2014 update_attr(vc); 2015 } 2016 break; 2017 case CSI_DEC_hl_ORIGIN_MODE: 2018 vc->vc_decom = on_off; 2019 gotoxay(vc, 0, 0); 2020 break; 2021 case CSI_DEC_hl_AUTOWRAP: 2022 vc->vc_decawm = on_off; 2023 break; 2024 case CSI_DEC_hl_AUTOREPEAT: 2025 if (on_off) 2026 set_kbd(vc, decarm); 2027 else 2028 clr_kbd(vc, decarm); 2029 break; 2030 case CSI_DEC_hl_MOUSE_X10: 2031 vc->vc_report_mouse = on_off ? 1 : 0; 2032 break; 2033 case CSI_DEC_hl_SHOW_CURSOR: 2034 vc->vc_deccm = on_off; 2035 break; 2036 case CSI_DEC_hl_MOUSE_VT200: 2037 vc->vc_report_mouse = on_off ? 2 : 0; 2038 break; 2039 case CSI_DEC_hl_BRACKETED_PASTE: 2040 vc->vc_bracketed_paste = on_off; 2041 break; 2042 case CSI_DEC_hl_ALT_SCREEN: 2043 if (on_off) 2044 enter_alt_screen(vc); 2045 else 2046 leave_alt_screen(vc); 2047 break; 2048 } 2049 } 2050 2051 enum { 2052 CSI_hl_DISPLAY_CTRL = 3, /* handle ansi control chars */ 2053 CSI_hl_INSERT = 4, /* IRM: insert/replace */ 2054 CSI_hl_AUTO_NL = 20, /* LNM: Enter == CrLf/Lf */ 2055 }; 2056 2057 /* console_lock is held */ 2058 static void csi_hl(struct vc_data *vc, bool on_off) 2059 { 2060 unsigned int i; 2061 2062 for (i = 0; i <= vc->vc_npar; i++) 2063 switch (vc->vc_par[i]) { /* ANSI modes set/reset */ 2064 case CSI_hl_DISPLAY_CTRL: 2065 vc->vc_disp_ctrl = on_off; 2066 break; 2067 case CSI_hl_INSERT: 2068 vc->vc_decim = on_off; 2069 break; 2070 case CSI_hl_AUTO_NL: 2071 if (on_off) 2072 set_kbd(vc, lnm); 2073 else 2074 clr_kbd(vc, lnm); 2075 break; 2076 } 2077 } 2078 2079 enum CSI_right_square_bracket { 2080 CSI_RSB_COLOR_FOR_UNDERLINE = 1, 2081 CSI_RSB_COLOR_FOR_HALF_BRIGHT = 2, 2082 CSI_RSB_MAKE_CUR_COLOR_DEFAULT = 8, 2083 CSI_RSB_BLANKING_INTERVAL = 9, 2084 CSI_RSB_BELL_FREQUENCY = 10, 2085 CSI_RSB_BELL_DURATION = 11, 2086 CSI_RSB_BRING_CONSOLE_TO_FRONT = 12, 2087 CSI_RSB_UNBLANK = 13, 2088 CSI_RSB_VESA_OFF_INTERVAL = 14, 2089 CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT = 15, 2090 CSI_RSB_CURSOR_BLINK_INTERVAL = 16, 2091 }; 2092 2093 /* 2094 * csi_RSB - csi+] (Right Square Bracket) handler 2095 * 2096 * These are linux console private sequences. 2097 * 2098 * console_lock is held 2099 */ 2100 static void csi_RSB(struct vc_data *vc) 2101 { 2102 switch (vc->vc_par[0]) { 2103 case CSI_RSB_COLOR_FOR_UNDERLINE: 2104 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2105 vc->vc_ulcolor = color_table[vc->vc_par[1]]; 2106 if (vc->state.underline) 2107 update_attr(vc); 2108 } 2109 break; 2110 case CSI_RSB_COLOR_FOR_HALF_BRIGHT: 2111 if (vc->vc_can_do_color && vc->vc_par[1] < 16) { 2112 vc->vc_halfcolor = color_table[vc->vc_par[1]]; 2113 if (vc->state.intensity == VCI_HALF_BRIGHT) 2114 update_attr(vc); 2115 } 2116 break; 2117 case CSI_RSB_MAKE_CUR_COLOR_DEFAULT: 2118 vc->vc_def_color = vc->vc_attr; 2119 if (vc->vc_hi_font_mask == 0x100) 2120 vc->vc_def_color >>= 1; 2121 default_attr(vc); 2122 update_attr(vc); 2123 break; 2124 case CSI_RSB_BLANKING_INTERVAL: 2125 blankinterval = min(vc->vc_par[1], 60U) * 60; 2126 poke_blanked_console(); 2127 break; 2128 case CSI_RSB_BELL_FREQUENCY: 2129 if (vc->vc_npar >= 1) 2130 vc->vc_bell_pitch = vc->vc_par[1]; 2131 else 2132 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2133 break; 2134 case CSI_RSB_BELL_DURATION: 2135 if (vc->vc_npar >= 1) 2136 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ? 2137 msecs_to_jiffies(vc->vc_par[1]) : 0; 2138 else 2139 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2140 break; 2141 case CSI_RSB_BRING_CONSOLE_TO_FRONT: 2142 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1)) 2143 set_console(vc->vc_par[1] - 1); 2144 break; 2145 case CSI_RSB_UNBLANK: 2146 poke_blanked_console(); 2147 break; 2148 case CSI_RSB_VESA_OFF_INTERVAL: 2149 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ; 2150 break; 2151 case CSI_RSB_BRING_PREV_CONSOLE_TO_FRONT: 2152 set_console(last_console); 2153 break; 2154 case CSI_RSB_CURSOR_BLINK_INTERVAL: 2155 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 && 2156 vc->vc_par[1] <= USHRT_MAX) 2157 vc->vc_cur_blink_ms = vc->vc_par[1]; 2158 else 2159 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2160 break; 2161 } 2162 } 2163 2164 /* console_lock is held */ 2165 static void csi_at(struct vc_data *vc, unsigned int nr) 2166 { 2167 nr = clamp(nr, 1, vc->vc_cols - vc->state.x); 2168 insert_char(vc, nr); 2169 } 2170 2171 /* console_lock is held */ 2172 static void csi_L(struct vc_data *vc) 2173 { 2174 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2175 2176 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr); 2177 vc->vc_need_wrap = 0; 2178 } 2179 2180 /* console_lock is held */ 2181 static void csi_P(struct vc_data *vc) 2182 { 2183 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_cols - vc->state.x); 2184 2185 delete_char(vc, nr); 2186 } 2187 2188 /* console_lock is held */ 2189 static void csi_M(struct vc_data *vc) 2190 { 2191 unsigned int nr = clamp(vc->vc_par[0], 1, vc->vc_rows - vc->state.y); 2192 2193 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr); 2194 vc->vc_need_wrap = 0; 2195 } 2196 2197 /* console_lock is held (except via vc_init->reset_terminal */ 2198 static void save_cur(struct vc_data *vc) 2199 { 2200 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state)); 2201 } 2202 2203 /* console_lock is held */ 2204 static void restore_cur(struct vc_data *vc) 2205 { 2206 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state)); 2207 2208 gotoxy(vc, vc->state.x, vc->state.y); 2209 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], 2210 vc); 2211 update_attr(vc); 2212 vc->vc_need_wrap = 0; 2213 } 2214 2215 /** 2216 * enum vc_ctl_state - control characters state of a vt 2217 * 2218 * @ESnormal: initial state, no control characters parsed 2219 * @ESesc: ESC parsed 2220 * @ESsquare: CSI parsed -- modifiers/parameters/ctrl chars expected 2221 * @ESgetpars: CSI parsed -- parameters/ctrl chars expected 2222 * @ESgetsubpars: CSI m parsed -- subparameters expected 2223 * @ESfunckey: CSI [ parsed 2224 * @EShash: ESC # parsed 2225 * @ESsetG0: ESC ( parsed 2226 * @ESsetG1: ESC ) parsed 2227 * @ESpercent: ESC % parsed 2228 * @EScsiignore: CSI [0x20-0x3f] parsed 2229 * @ESnonstd: OSC parsed 2230 * @ESpalette: OSC P parsed 2231 * @ESosc: OSC [0-9] parsed 2232 * @ESANSI_first: first state for ignoring ansi control sequences 2233 * @ESapc: ESC _ parsed 2234 * @ESpm: ESC ^ parsed 2235 * @ESdcs: ESC P parsed 2236 * @ESANSI_last: last state for ignoring ansi control sequences 2237 */ 2238 enum vc_ctl_state { 2239 ESnormal, 2240 ESesc, 2241 ESsquare, 2242 ESgetpars, 2243 ESgetsubpars, 2244 ESfunckey, 2245 EShash, 2246 ESsetG0, 2247 ESsetG1, 2248 ESpercent, 2249 EScsiignore, 2250 ESnonstd, 2251 ESpalette, 2252 ESosc, 2253 ESANSI_first = ESosc, 2254 ESapc, 2255 ESpm, 2256 ESdcs, 2257 ESANSI_last = ESdcs, 2258 }; 2259 2260 /* console_lock is held (except via vc_init()) */ 2261 static void reset_terminal(struct vc_data *vc, int do_clear) 2262 { 2263 unsigned int i; 2264 2265 vc->vc_top = 0; 2266 vc->vc_bottom = vc->vc_rows; 2267 vc->vc_state = ESnormal; 2268 vc->vc_priv = EPecma; 2269 vc->vc_translate = set_translate(LAT1_MAP, vc); 2270 vc->state.Gx_charset[0] = LAT1_MAP; 2271 vc->state.Gx_charset[1] = GRAF_MAP; 2272 vc->state.charset = 0; 2273 vc->vc_need_wrap = 0; 2274 vc->vc_report_mouse = 0; 2275 vc->vc_bracketed_paste = 0; 2276 vc->vc_utf = default_utf8; 2277 vc->vc_utf_count = 0; 2278 2279 vc->vc_disp_ctrl = 0; 2280 vc->vc_toggle_meta = 0; 2281 2282 vc->vc_decscnm = 0; 2283 vc->vc_decom = 0; 2284 vc->vc_decawm = 1; 2285 vc->vc_deccm = global_cursor_default; 2286 vc->vc_decim = 0; 2287 2288 if (vc->vc_saved_screen != NULL) { 2289 kfree(vc->vc_saved_screen); 2290 vc->vc_saved_screen = NULL; 2291 vc_uniscr_free(vc->vc_saved_uni_lines); 2292 vc->vc_saved_uni_lines = NULL; 2293 vc->vc_saved_rows = 0; 2294 vc->vc_saved_cols = 0; 2295 } 2296 2297 vt_reset_keyboard(vc->vc_num); 2298 2299 vc->vc_cursor_type = cur_default; 2300 vc->vc_complement_mask = vc->vc_s_complement_mask; 2301 2302 default_attr(vc); 2303 update_attr(vc); 2304 2305 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2306 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8) 2307 set_bit(i, vc->vc_tab_stop); 2308 2309 vc->vc_bell_pitch = DEFAULT_BELL_PITCH; 2310 vc->vc_bell_duration = DEFAULT_BELL_DURATION; 2311 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS; 2312 2313 gotoxy(vc, 0, 0); 2314 save_cur(vc); 2315 if (do_clear) 2316 csi_J(vc, CSI_J_VISIBLE); 2317 } 2318 2319 static void vc_setGx(struct vc_data *vc, unsigned int which, u8 c) 2320 { 2321 unsigned char *charset = &vc->state.Gx_charset[which]; 2322 2323 switch (c) { 2324 case '0': 2325 *charset = GRAF_MAP; 2326 break; 2327 case 'B': 2328 *charset = LAT1_MAP; 2329 break; 2330 case 'U': 2331 *charset = IBMPC_MAP; 2332 break; 2333 case 'K': 2334 *charset = USER_MAP; 2335 break; 2336 } 2337 2338 if (vc->state.charset == which) 2339 vc->vc_translate = set_translate(*charset, vc); 2340 } 2341 2342 static bool ansi_control_string(enum vc_ctl_state state) 2343 { 2344 return state >= ESANSI_first && state <= ESANSI_last; 2345 } 2346 2347 enum { 2348 ASCII_NULL = 0, 2349 ASCII_BELL = 7, 2350 ASCII_BACKSPACE = 8, 2351 ASCII_IGNORE_FIRST = ASCII_BACKSPACE, 2352 ASCII_HTAB = 9, 2353 ASCII_LINEFEED = 10, 2354 ASCII_VTAB = 11, 2355 ASCII_FORMFEED = 12, 2356 ASCII_CAR_RET = 13, 2357 ASCII_IGNORE_LAST = ASCII_CAR_RET, 2358 ASCII_SHIFTOUT = 14, 2359 ASCII_SHIFTIN = 15, 2360 ASCII_CANCEL = 24, 2361 ASCII_SUBSTITUTE = 26, 2362 ASCII_ESCAPE = 27, 2363 ASCII_CSI_IGNORE_FIRST = ' ', /* 0x2x, 0x3a and 0x3c - 0x3f */ 2364 ASCII_CSI_IGNORE_LAST = '?', 2365 ASCII_DEL = 127, 2366 ASCII_EXT_CSI = 128 + ASCII_ESCAPE, 2367 }; 2368 2369 /* 2370 * Handle ascii characters in control sequences and change states accordingly. 2371 * E.g. ESC sets the state of vc to ESesc. 2372 * 2373 * Returns: true if @c handled. 2374 */ 2375 static bool handle_ascii(struct tty_struct *tty, struct vc_data *vc, u8 c) 2376 { 2377 switch (c) { 2378 case ASCII_NULL: 2379 return true; 2380 case ASCII_BELL: 2381 if (ansi_control_string(vc->vc_state)) 2382 vc->vc_state = ESnormal; 2383 else if (vc->vc_bell_duration) 2384 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration); 2385 return true; 2386 case ASCII_BACKSPACE: 2387 bs(vc); 2388 return true; 2389 case ASCII_HTAB: 2390 vc->vc_pos -= (vc->state.x << 1); 2391 2392 vc->state.x = find_next_bit(vc->vc_tab_stop, 2393 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT), 2394 vc->state.x + 1); 2395 if (vc->state.x >= VC_TABSTOPS_COUNT) 2396 vc->state.x = vc->vc_cols - 1; 2397 2398 vc->vc_pos += (vc->state.x << 1); 2399 notify_write(vc, '\t'); 2400 return true; 2401 case ASCII_LINEFEED: 2402 case ASCII_VTAB: 2403 case ASCII_FORMFEED: 2404 lf(vc); 2405 if (!is_kbd(vc, lnm)) 2406 return true; 2407 fallthrough; 2408 case ASCII_CAR_RET: 2409 cr(vc); 2410 return true; 2411 case ASCII_SHIFTOUT: 2412 vc->state.charset = 1; 2413 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc); 2414 vc->vc_disp_ctrl = 1; 2415 return true; 2416 case ASCII_SHIFTIN: 2417 vc->state.charset = 0; 2418 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc); 2419 vc->vc_disp_ctrl = 0; 2420 return true; 2421 case ASCII_CANCEL: 2422 case ASCII_SUBSTITUTE: 2423 vc->vc_state = ESnormal; 2424 return true; 2425 case ASCII_ESCAPE: 2426 vc->vc_state = ESesc; 2427 return true; 2428 case ASCII_DEL: 2429 del(vc); 2430 return true; 2431 case ASCII_EXT_CSI: 2432 vc->vc_state = ESsquare; 2433 return true; 2434 } 2435 2436 return false; 2437 } 2438 2439 /* 2440 * Handle a character (@c) following an ESC (when @vc is in the ESesc state). 2441 * E.g. previous ESC with @c == '[' here yields the ESsquare state (that is: 2442 * CSI). 2443 */ 2444 static void handle_esc(struct tty_struct *tty, struct vc_data *vc, u8 c) 2445 { 2446 vc->vc_state = ESnormal; 2447 switch (c) { 2448 case '[': 2449 vc->vc_state = ESsquare; 2450 break; 2451 case ']': 2452 vc->vc_state = ESnonstd; 2453 break; 2454 case '_': 2455 vc->vc_state = ESapc; 2456 break; 2457 case '^': 2458 vc->vc_state = ESpm; 2459 break; 2460 case '%': 2461 vc->vc_state = ESpercent; 2462 break; 2463 case 'E': 2464 cr(vc); 2465 lf(vc); 2466 break; 2467 case 'M': 2468 ri(vc); 2469 break; 2470 case 'D': 2471 lf(vc); 2472 break; 2473 case 'H': 2474 if (vc->state.x < VC_TABSTOPS_COUNT) 2475 set_bit(vc->state.x, vc->vc_tab_stop); 2476 break; 2477 case 'P': 2478 vc->vc_state = ESdcs; 2479 break; 2480 case 'Z': 2481 respond_ID(tty); 2482 break; 2483 case '7': 2484 save_cur(vc); 2485 break; 2486 case '8': 2487 restore_cur(vc); 2488 break; 2489 case '(': 2490 vc->vc_state = ESsetG0; 2491 break; 2492 case ')': 2493 vc->vc_state = ESsetG1; 2494 break; 2495 case '#': 2496 vc->vc_state = EShash; 2497 break; 2498 case 'c': 2499 reset_terminal(vc, 1); 2500 break; 2501 case '>': /* Numeric keypad */ 2502 clr_kbd(vc, kbdapplic); 2503 break; 2504 case '=': /* Appl. keypad */ 2505 set_kbd(vc, kbdapplic); 2506 break; 2507 } 2508 } 2509 2510 /* 2511 * Handle special DEC control sequences ("ESC [ ? parameters char"). Parameters 2512 * are in @vc->vc_par and the char is in @c here. 2513 */ 2514 static void csi_DEC(struct tty_struct *tty, struct vc_data *vc, u8 c) 2515 { 2516 switch (c) { 2517 case 'h': 2518 csi_DEC_hl(vc, true); 2519 break; 2520 case 'l': 2521 csi_DEC_hl(vc, false); 2522 break; 2523 case 'c': 2524 if (vc->vc_par[0]) 2525 vc->vc_cursor_type = CUR_MAKE(vc->vc_par[0], 2526 vc->vc_par[1], 2527 vc->vc_par[2]); 2528 else 2529 vc->vc_cursor_type = cur_default; 2530 break; 2531 case 'm': 2532 clear_selection(); 2533 if (vc->vc_par[0]) 2534 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1]; 2535 else 2536 vc->vc_complement_mask = vc->vc_s_complement_mask; 2537 break; 2538 case 'n': 2539 if (vc->vc_par[0] == 5) 2540 status_report(tty); 2541 else if (vc->vc_par[0] == 6) 2542 cursor_report(vc, tty); 2543 break; 2544 } 2545 } 2546 2547 /* 2548 * Handle Control Sequence Introducer control characters. That is 2549 * "ESC [ parameters char". Parameters are in @vc->vc_par and the char is in 2550 * @c here. 2551 */ 2552 static void csi_ECMA(struct tty_struct *tty, struct vc_data *vc, u8 c) 2553 { 2554 switch (c) { 2555 case 'G': 2556 case '`': 2557 if (vc->vc_par[0]) 2558 vc->vc_par[0]--; 2559 gotoxy(vc, vc->vc_par[0], vc->state.y); 2560 break; 2561 case 'A': 2562 if (!vc->vc_par[0]) 2563 vc->vc_par[0]++; 2564 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]); 2565 break; 2566 case 'B': 2567 case 'e': 2568 if (!vc->vc_par[0]) 2569 vc->vc_par[0]++; 2570 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]); 2571 break; 2572 case 'C': 2573 case 'a': 2574 if (!vc->vc_par[0]) 2575 vc->vc_par[0]++; 2576 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y); 2577 break; 2578 case 'D': 2579 if (!vc->vc_par[0]) 2580 vc->vc_par[0]++; 2581 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y); 2582 break; 2583 case 'E': 2584 if (!vc->vc_par[0]) 2585 vc->vc_par[0]++; 2586 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]); 2587 break; 2588 case 'F': 2589 if (!vc->vc_par[0]) 2590 vc->vc_par[0]++; 2591 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]); 2592 break; 2593 case 'd': 2594 if (vc->vc_par[0]) 2595 vc->vc_par[0]--; 2596 gotoxay(vc, vc->state.x ,vc->vc_par[0]); 2597 break; 2598 case 'H': 2599 case 'f': 2600 if (vc->vc_par[0]) 2601 vc->vc_par[0]--; 2602 if (vc->vc_par[1]) 2603 vc->vc_par[1]--; 2604 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]); 2605 break; 2606 case 'J': 2607 csi_J(vc, vc->vc_par[0]); 2608 break; 2609 case 'K': 2610 csi_K(vc); 2611 break; 2612 case 'L': 2613 csi_L(vc); 2614 break; 2615 case 'M': 2616 csi_M(vc); 2617 break; 2618 case 'P': 2619 csi_P(vc); 2620 break; 2621 case 'c': 2622 if (!vc->vc_par[0]) 2623 respond_ID(tty); 2624 break; 2625 case 'g': 2626 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT) 2627 set_bit(vc->state.x, vc->vc_tab_stop); 2628 else if (vc->vc_par[0] == 3) 2629 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT); 2630 break; 2631 case 'h': 2632 csi_hl(vc, true); 2633 break; 2634 case 'l': 2635 csi_hl(vc, false); 2636 break; 2637 case 'm': 2638 csi_m(vc); 2639 break; 2640 case 'n': 2641 if (vc->vc_par[0] == 5) 2642 status_report(tty); 2643 else if (vc->vc_par[0] == 6) 2644 cursor_report(vc, tty); 2645 break; 2646 case 'q': /* DECLL - but only 3 leds */ 2647 /* map 0,1,2,3 to 0,1,2,4 */ 2648 if (vc->vc_par[0] < 4) 2649 vt_set_led_state(vc->vc_num, 2650 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4); 2651 break; 2652 case 'r': 2653 if (!vc->vc_par[0]) 2654 vc->vc_par[0]++; 2655 if (!vc->vc_par[1]) 2656 vc->vc_par[1] = vc->vc_rows; 2657 /* Minimum allowed region is 2 lines */ 2658 if (vc->vc_par[0] < vc->vc_par[1] && 2659 vc->vc_par[1] <= vc->vc_rows) { 2660 vc->vc_top = vc->vc_par[0] - 1; 2661 vc->vc_bottom = vc->vc_par[1]; 2662 gotoxay(vc, 0, 0); 2663 } 2664 break; 2665 case 's': 2666 save_cur(vc); 2667 break; 2668 case 'u': 2669 restore_cur(vc); 2670 break; 2671 case 'X': 2672 csi_X(vc); 2673 break; 2674 case '@': 2675 csi_at(vc, vc->vc_par[0]); 2676 break; 2677 case ']': 2678 csi_RSB(vc); 2679 break; 2680 } 2681 2682 } 2683 2684 static void vc_reset_params(struct vc_data *vc) 2685 { 2686 memset(vc->vc_par, 0, sizeof(vc->vc_par)); 2687 vc->vc_npar = 0; 2688 } 2689 2690 /* console_lock is held */ 2691 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, u8 c) 2692 { 2693 /* 2694 * Control characters can be used in the _middle_ 2695 * of an escape sequence, aside from ANSI control strings. 2696 */ 2697 if (ansi_control_string(vc->vc_state) && c >= ASCII_IGNORE_FIRST && 2698 c <= ASCII_IGNORE_LAST) 2699 return; 2700 2701 if (handle_ascii(tty, vc, c)) 2702 return; 2703 2704 switch(vc->vc_state) { 2705 case ESesc: /* ESC */ 2706 handle_esc(tty, vc, c); 2707 return; 2708 case ESnonstd: /* ESC ] aka OSC */ 2709 switch (c) { 2710 case 'P': /* palette escape sequence */ 2711 vc_reset_params(vc); 2712 vc->vc_state = ESpalette; 2713 return; 2714 case 'R': /* reset palette */ 2715 reset_palette(vc); 2716 break; 2717 case '0' ... '9': 2718 vc->vc_state = ESosc; 2719 return; 2720 } 2721 vc->vc_state = ESnormal; 2722 return; 2723 case ESpalette: /* ESC ] P aka OSC P */ 2724 if (isxdigit(c)) { 2725 vc->vc_par[vc->vc_npar++] = hex_to_bin(c); 2726 if (vc->vc_npar == 7) { 2727 int i = vc->vc_par[0] * 3, j = 1; 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 vc->vc_palette[i] = 16 * vc->vc_par[j++]; 2733 vc->vc_palette[i] += vc->vc_par[j]; 2734 set_palette(vc); 2735 vc->vc_state = ESnormal; 2736 } 2737 } else 2738 vc->vc_state = ESnormal; 2739 return; 2740 case ESsquare: /* ESC [ aka CSI, parameters or modifiers expected */ 2741 vc_reset_params(vc); 2742 2743 vc->vc_state = ESgetpars; 2744 switch (c) { 2745 case '[': /* Function key */ 2746 vc->vc_state = ESfunckey; 2747 return; 2748 case '?': 2749 vc->vc_priv = EPdec; 2750 return; 2751 case '>': 2752 vc->vc_priv = EPgt; 2753 return; 2754 case '=': 2755 vc->vc_priv = EPeq; 2756 return; 2757 case '<': 2758 vc->vc_priv = EPlt; 2759 return; 2760 } 2761 vc->vc_priv = EPecma; 2762 fallthrough; 2763 case ESgetpars: /* ESC [ aka CSI, parameters expected */ 2764 switch (c) { 2765 case ':': /* ITU-T T.416 color subparameters */ 2766 if (vc->vc_par[vc->vc_npar] == CSI_m_FG_COLOR || 2767 vc->vc_par[vc->vc_npar] == CSI_m_BG_COLOR || 2768 vc->vc_par[vc->vc_npar] == CSI_m_UNDERLINE_COLOR) 2769 vc->vc_state = ESgetsubpars; 2770 else 2771 break; 2772 fallthrough; 2773 case ';': 2774 if (vc->vc_npar < NPAR - 1) { 2775 vc->vc_npar++; 2776 return; 2777 } 2778 break; 2779 case '0' ... '9': 2780 vc->vc_par[vc->vc_npar] *= 10; 2781 vc->vc_par[vc->vc_npar] += c - '0'; 2782 return; 2783 } 2784 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2785 vc->vc_state = EScsiignore; 2786 return; 2787 } 2788 2789 /* parameters done, handle the control char @c */ 2790 2791 vc->vc_state = ESnormal; 2792 2793 switch (vc->vc_priv) { 2794 case EPdec: 2795 csi_DEC(tty, vc, c); 2796 return; 2797 case EPecma: 2798 csi_ECMA(tty, vc, c); 2799 return; 2800 default: 2801 return; 2802 } 2803 case ESgetsubpars: /* ESC [ 38/48/58, subparameters expected */ 2804 switch (c) { 2805 case ':': 2806 case ';': 2807 if (vc->vc_npar < NPAR - 1) { 2808 vc->vc_npar++; 2809 return; 2810 } 2811 break; 2812 case '0' ... '9': 2813 vc->vc_par[vc->vc_npar] *= 10; 2814 vc->vc_par[vc->vc_npar] += c - '0'; 2815 return; 2816 } 2817 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) { 2818 vc->vc_state = EScsiignore; 2819 return; 2820 } 2821 2822 /* parameters done, handle the control char @c */ 2823 2824 vc->vc_state = ESnormal; 2825 2826 switch (vc->vc_priv) { 2827 case EPdec: 2828 csi_DEC(tty, vc, c); 2829 return; 2830 case EPecma: 2831 csi_ECMA(tty, vc, c); 2832 return; 2833 default: 2834 return; 2835 } 2836 case EScsiignore: 2837 if (c >= ASCII_CSI_IGNORE_FIRST && c <= ASCII_CSI_IGNORE_LAST) 2838 return; 2839 vc->vc_state = ESnormal; 2840 return; 2841 case ESpercent: /* ESC % */ 2842 vc->vc_state = ESnormal; 2843 switch (c) { 2844 case '@': /* defined in ISO 2022 */ 2845 vc->vc_utf = 0; 2846 return; 2847 case 'G': /* prelim official escape code */ 2848 case '8': /* retained for compatibility */ 2849 vc->vc_utf = 1; 2850 return; 2851 } 2852 return; 2853 case ESfunckey: /* ESC [ [ aka CSI [ */ 2854 vc->vc_state = ESnormal; 2855 return; 2856 case EShash: /* ESC # */ 2857 vc->vc_state = ESnormal; 2858 if (c == '8') { 2859 /* DEC screen alignment test. kludge :-) */ 2860 vc->vc_video_erase_char = 2861 (vc->vc_video_erase_char & 0xff00) | 'E'; 2862 csi_J(vc, CSI_J_VISIBLE); 2863 vc->vc_video_erase_char = 2864 (vc->vc_video_erase_char & 0xff00) | ' '; 2865 if (con_should_update(vc)) 2866 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2); 2867 } 2868 return; 2869 case ESsetG0: /* ESC ( */ 2870 vc_setGx(vc, 0, c); 2871 vc->vc_state = ESnormal; 2872 return; 2873 case ESsetG1: /* ESC ) */ 2874 vc_setGx(vc, 1, c); 2875 vc->vc_state = ESnormal; 2876 return; 2877 case ESapc: /* ESC _ */ 2878 return; 2879 case ESosc: /* ESC ] [0-9] aka OSC [0-9] */ 2880 return; 2881 case ESpm: /* ESC ^ */ 2882 return; 2883 case ESdcs: /* ESC P */ 2884 return; 2885 default: 2886 vc->vc_state = ESnormal; 2887 } 2888 } 2889 2890 struct vc_draw_region { 2891 unsigned long from, to; 2892 int x; 2893 }; 2894 2895 static void con_flush(struct vc_data *vc, struct vc_draw_region *draw) 2896 { 2897 if (draw->x < 0) 2898 return; 2899 2900 vc->vc_sw->con_putcs(vc, (u16 *)draw->from, 2901 (u16 *)draw->to - (u16 *)draw->from, vc->state.y, 2902 draw->x); 2903 draw->x = -1; 2904 } 2905 2906 static inline int vc_translate_ascii(const struct vc_data *vc, int c) 2907 { 2908 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) { 2909 if (vc->vc_toggle_meta) 2910 c |= 0x80; 2911 2912 return vc->vc_translate[c]; 2913 } 2914 2915 return c; 2916 } 2917 2918 2919 /** 2920 * vc_sanitize_unicode - Replace invalid Unicode code points with ``U+FFFD`` 2921 * @c: the received code point 2922 */ 2923 static inline int vc_sanitize_unicode(const int c) 2924 { 2925 if (c >= 0xd800 && c <= 0xdfff) 2926 return 0xfffd; 2927 2928 return c; 2929 } 2930 2931 /** 2932 * vc_translate_unicode - Combine UTF-8 into Unicode in &vc_data.vc_utf_char 2933 * @vc: virtual console 2934 * @c: UTF-8 byte to translate 2935 * @rescan: set to true iff @c wasn't consumed here and needs to be re-processed 2936 * 2937 * * &vc_data.vc_utf_char is the being-constructed Unicode code point. 2938 * * &vc_data.vc_utf_count is the number of continuation bytes still expected to 2939 * arrive. 2940 * * &vc_data.vc_npar is the number of continuation bytes arrived so far. 2941 * 2942 * Return: 2943 * * %-1 - Input OK so far, @c consumed, further bytes expected. 2944 * * %0xFFFD - Possibility 1: input invalid, @c may have been consumed (see 2945 * desc. of @rescan). Possibility 2: input OK, @c consumed, 2946 * ``U+FFFD`` is the resulting code point. ``U+FFFD`` is valid, 2947 * ``REPLACEMENT CHARACTER``. 2948 * * otherwise - Input OK, @c consumed, resulting code point returned. 2949 */ 2950 static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan) 2951 { 2952 static const u32 utf8_length_changes[] = {0x7f, 0x7ff, 0xffff, 0x10ffff}; 2953 2954 /* Continuation byte received */ 2955 if ((c & 0xc0) == 0x80) { 2956 /* Unexpected continuation byte? */ 2957 if (!vc->vc_utf_count) 2958 goto bad_sequence; 2959 2960 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f); 2961 vc->vc_npar++; 2962 if (--vc->vc_utf_count) 2963 goto need_more_bytes; 2964 2965 /* Got a whole character */ 2966 c = vc->vc_utf_char; 2967 /* Reject overlong sequences */ 2968 if (c <= utf8_length_changes[vc->vc_npar - 1] || 2969 c > utf8_length_changes[vc->vc_npar]) 2970 goto bad_sequence; 2971 2972 return vc_sanitize_unicode(c); 2973 } 2974 2975 /* Single ASCII byte or first byte of a sequence received */ 2976 if (vc->vc_utf_count) { 2977 /* A continuation byte was expected */ 2978 *rescan = true; 2979 vc->vc_utf_count = 0; 2980 goto bad_sequence; 2981 } 2982 2983 /* Nothing to do if an ASCII byte was received */ 2984 if (c <= 0x7f) 2985 return c; 2986 2987 /* First byte of a multibyte sequence received */ 2988 vc->vc_npar = 0; 2989 if ((c & 0xe0) == 0xc0) { 2990 vc->vc_utf_count = 1; 2991 vc->vc_utf_char = (c & 0x1f); 2992 } else if ((c & 0xf0) == 0xe0) { 2993 vc->vc_utf_count = 2; 2994 vc->vc_utf_char = (c & 0x0f); 2995 } else if ((c & 0xf8) == 0xf0) { 2996 vc->vc_utf_count = 3; 2997 vc->vc_utf_char = (c & 0x07); 2998 } else { 2999 goto bad_sequence; 3000 } 3001 3002 need_more_bytes: 3003 return -1; 3004 3005 bad_sequence: 3006 return 0xfffd; 3007 } 3008 3009 static int vc_translate(struct vc_data *vc, int *c, bool *rescan) 3010 { 3011 /* Do no translation at all in control states */ 3012 if (vc->vc_state != ESnormal) 3013 return *c; 3014 3015 if (vc->vc_utf && !vc->vc_disp_ctrl) 3016 return *c = vc_translate_unicode(vc, *c, rescan); 3017 3018 /* no utf or alternate charset mode */ 3019 return vc_translate_ascii(vc, *c); 3020 } 3021 3022 static inline unsigned char vc_invert_attr(const struct vc_data *vc) 3023 { 3024 if (!vc->vc_can_do_color) 3025 return vc->vc_attr ^ 0x08; 3026 3027 if (vc->vc_hi_font_mask == 0x100) 3028 return (vc->vc_attr & 0x11) | 3029 ((vc->vc_attr & 0xe0) >> 4) | 3030 ((vc->vc_attr & 0x0e) << 4); 3031 3032 return (vc->vc_attr & 0x88) | 3033 ((vc->vc_attr & 0x70) >> 4) | 3034 ((vc->vc_attr & 0x07) << 4); 3035 } 3036 3037 static bool vc_is_control(struct vc_data *vc, int tc, int c) 3038 { 3039 /* 3040 * A bitmap for codes <32. A bit of 1 indicates that the code 3041 * corresponding to that bit number invokes some special action (such 3042 * as cursor movement) and should not be displayed as a glyph unless 3043 * the disp_ctrl mode is explicitly enabled. 3044 */ 3045 static const u32 CTRL_ACTION = BIT(ASCII_NULL) | 3046 GENMASK(ASCII_SHIFTIN, ASCII_BELL) | BIT(ASCII_CANCEL) | 3047 BIT(ASCII_SUBSTITUTE) | BIT(ASCII_ESCAPE); 3048 /* Cannot be overridden by disp_ctrl */ 3049 static const u32 CTRL_ALWAYS = BIT(ASCII_NULL) | BIT(ASCII_BACKSPACE) | 3050 BIT(ASCII_LINEFEED) | BIT(ASCII_SHIFTIN) | BIT(ASCII_SHIFTOUT) | 3051 BIT(ASCII_CAR_RET) | BIT(ASCII_FORMFEED) | BIT(ASCII_ESCAPE); 3052 3053 if (vc->vc_state != ESnormal) 3054 return true; 3055 3056 if (!tc) 3057 return true; 3058 3059 /* 3060 * If the original code was a control character we only allow a glyph 3061 * to be displayed if the code is not normally used (such as for cursor 3062 * movement) or if the disp_ctrl mode has been explicitly enabled. 3063 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always 3064 * displayed as control characters, as the console would be pretty 3065 * useless without them; to display an arbitrary font position use the 3066 * direct-to-font zone in UTF-8 mode. 3067 */ 3068 if (c < BITS_PER_TYPE(CTRL_ALWAYS)) { 3069 if (vc->vc_disp_ctrl) 3070 return CTRL_ALWAYS & BIT(c); 3071 else 3072 return vc->vc_utf || (CTRL_ACTION & BIT(c)); 3073 } 3074 3075 if (c == ASCII_DEL && !vc->vc_disp_ctrl) 3076 return true; 3077 3078 if (c == ASCII_EXT_CSI) 3079 return true; 3080 3081 return false; 3082 } 3083 3084 static void vc_con_rewind(struct vc_data *vc) 3085 { 3086 if (vc->state.x && !vc->vc_need_wrap) { 3087 vc->vc_pos -= 2; 3088 vc->state.x--; 3089 } 3090 vc->vc_need_wrap = 0; 3091 } 3092 3093 #define UCS_ZWS 0x200b /* Zero Width Space */ 3094 #define UCS_VS16 0xfe0f /* Variation Selector 16 */ 3095 #define UCS_REPLACEMENT 0xfffd /* Replacement Character */ 3096 3097 static int vc_process_ucs(struct vc_data *vc, int *c, int *tc) 3098 { 3099 u32 prev_c, curr_c = *c; 3100 unsigned int w = ucs_get_width(curr_c); 3101 3102 if (likely(w == 1)) 3103 return 1; 3104 3105 if (w == 2) { 3106 /* 3107 * The Unicode screen memory is allocated only when 3108 * required. This is one such case as we need to remember 3109 * which displayed characters are double-width. 3110 */ 3111 vc_uniscr_check(vc); 3112 return 2; 3113 } 3114 3115 /* From here curr_c is known to be zero-width. */ 3116 3117 if (ucs_get_width(vc_uniscr_getc(vc, -2)) == 2) { 3118 /* 3119 * Let's merge this zero-width code point with the preceding 3120 * double-width code point by replacing the existing 3121 * zero-width space padding. To do so we rewind one column 3122 * and pretend this has a width of 1. 3123 * We give the legacy display the same initial space padding. 3124 */ 3125 vc_con_rewind(vc); 3126 *tc = ' '; 3127 return 1; 3128 } 3129 3130 /* From here the preceding character, if any, must be single-width. */ 3131 prev_c = vc_uniscr_getc(vc, -1); 3132 3133 if (curr_c == UCS_VS16 && prev_c != 0) { 3134 /* 3135 * VS16 (U+FE0F) is special. It typically turns the preceding 3136 * single-width character into a double-width one. Let it 3137 * have a width of 1 effectively making the combination with 3138 * the preceding character double-width. 3139 */ 3140 *tc = ' '; 3141 return 1; 3142 } 3143 3144 /* try recomposition */ 3145 prev_c = ucs_recompose(prev_c, curr_c); 3146 if (prev_c != 0) { 3147 vc_con_rewind(vc); 3148 *tc = *c = prev_c; 3149 return 1; 3150 } 3151 3152 /* Otherwise zero-width code points are ignored. */ 3153 return 0; 3154 } 3155 3156 static int vc_get_glyph(struct vc_data *vc, int tc) 3157 { 3158 int glyph = conv_uni_to_pc(vc, tc); 3159 u16 charmask = vc->vc_hi_font_mask ? 0x1ff : 0xff; 3160 3161 if (!(glyph & ~charmask)) 3162 return glyph; 3163 3164 if (glyph == -1) 3165 return -1; /* nothing to display */ 3166 3167 /* Glyph not found */ 3168 if ((!vc->vc_utf || vc->vc_disp_ctrl || tc < 128) && !(tc & ~charmask)) { 3169 /* 3170 * In legacy mode use the glyph we get by a 1:1 mapping. 3171 * This would make absolutely no sense with Unicode in mind, but do this for 3172 * ASCII characters since a font may lack Unicode mapping info and we don't 3173 * want to end up with having question marks only. 3174 */ 3175 return tc; 3176 } 3177 3178 /* 3179 * The Unicode screen memory is allocated only when required. 3180 * This is one such case: we're about to "cheat" with the displayed 3181 * character meaning the simple screen buffer won't hold the original 3182 * information, whereas the Unicode screen buffer always does. 3183 */ 3184 vc_uniscr_check(vc); 3185 3186 /* Try getting a simpler fallback character. */ 3187 tc = ucs_get_fallback(tc); 3188 if (tc) 3189 return vc_get_glyph(vc, tc); 3190 3191 /* Display U+FFFD (Unicode Replacement Character). */ 3192 return conv_uni_to_pc(vc, UCS_REPLACEMENT); 3193 } 3194 3195 static int vc_con_write_normal(struct vc_data *vc, int tc, int c, 3196 struct vc_draw_region *draw) 3197 { 3198 int next_c; 3199 unsigned char vc_attr = vc->vc_attr; 3200 u16 himask = vc->vc_hi_font_mask; 3201 u8 width = 1; 3202 bool inverse = false; 3203 3204 if (vc->vc_utf && !vc->vc_disp_ctrl) { 3205 width = vc_process_ucs(vc, &c, &tc); 3206 if (!width) 3207 goto out; 3208 } 3209 3210 /* Now try to find out how to display it */ 3211 tc = vc_get_glyph(vc, tc); 3212 if (tc == -1) 3213 return -1; /* nothing to display */ 3214 if (tc < 0) { 3215 inverse = true; 3216 tc = conv_uni_to_pc(vc, '?'); 3217 if (tc < 0) 3218 tc = '?'; 3219 3220 vc_attr = vc_invert_attr(vc); 3221 con_flush(vc, draw); 3222 } 3223 3224 next_c = c; 3225 while (1) { 3226 if (vc->vc_need_wrap || vc->vc_decim) 3227 con_flush(vc, draw); 3228 if (vc->vc_need_wrap) { 3229 cr(vc); 3230 lf(vc); 3231 } 3232 if (vc->vc_decim) 3233 insert_char(vc, 1); 3234 vc_uniscr_putc(vc, next_c); 3235 3236 if (himask) 3237 tc = ((tc & 0x100) ? himask : 0) | 3238 (tc & 0xff); 3239 tc |= (vc_attr << 8) & ~himask; 3240 3241 scr_writew(tc, (u16 *)vc->vc_pos); 3242 3243 if (con_should_update(vc) && draw->x < 0) { 3244 draw->x = vc->state.x; 3245 draw->from = vc->vc_pos; 3246 } 3247 if (vc->state.x == vc->vc_cols - 1) { 3248 vc->vc_need_wrap = vc->vc_decawm; 3249 draw->to = vc->vc_pos + 2; 3250 } else { 3251 vc->state.x++; 3252 draw->to = (vc->vc_pos += 2); 3253 } 3254 3255 if (!--width) 3256 break; 3257 3258 /* A space is printed in the second column */ 3259 tc = conv_uni_to_pc(vc, ' '); 3260 if (tc < 0) 3261 tc = ' '; 3262 /* 3263 * Store a zero-width space in the Unicode screen given that 3264 * the previous code point is semantically double width. 3265 */ 3266 next_c = UCS_ZWS; 3267 } 3268 3269 out: 3270 notify_write(vc, c); 3271 3272 if (inverse) 3273 con_flush(vc, draw); 3274 3275 return 0; 3276 } 3277 3278 /* acquires console_lock */ 3279 static int do_con_write(struct tty_struct *tty, const u8 *buf, int count) 3280 { 3281 struct vc_draw_region draw = { 3282 .x = -1, 3283 }; 3284 int c, tc, n = 0; 3285 unsigned int currcons; 3286 struct vc_data *vc = tty->driver_data; 3287 struct vt_notifier_param param; 3288 bool rescan; 3289 3290 if (in_interrupt()) 3291 return count; 3292 3293 guard(console_lock)(); 3294 currcons = vc->vc_num; 3295 if (!vc_cons_allocated(currcons)) { 3296 /* could this happen? */ 3297 pr_warn_once("con_write: tty %d not allocated\n", currcons+1); 3298 return 0; 3299 } 3300 3301 3302 /* undraw cursor first */ 3303 if (con_is_fg(vc)) 3304 hide_cursor(vc); 3305 3306 param.vc = vc; 3307 3308 while (!tty->flow.stopped && count) { 3309 u8 orig = *buf; 3310 buf++; 3311 n++; 3312 count--; 3313 rescan_last_byte: 3314 c = orig; 3315 rescan = false; 3316 3317 tc = vc_translate(vc, &c, &rescan); 3318 if (tc == -1) 3319 continue; 3320 3321 param.c = tc; 3322 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE, 3323 ¶m) == NOTIFY_STOP) 3324 continue; 3325 3326 if (vc_is_control(vc, tc, c)) { 3327 con_flush(vc, &draw); 3328 do_con_trol(tty, vc, orig); 3329 continue; 3330 } 3331 3332 if (vc_con_write_normal(vc, tc, c, &draw) < 0) 3333 continue; 3334 3335 if (rescan) 3336 goto rescan_last_byte; 3337 } 3338 con_flush(vc, &draw); 3339 notify_update(vc); 3340 3341 return n; 3342 } 3343 3344 /* 3345 * This is the console switching callback. 3346 * 3347 * Doing console switching in a process context allows 3348 * us to do the switches asynchronously (needed when we want 3349 * to switch due to a keyboard interrupt). Synchronization 3350 * with other console code and prevention of re-entrancy is 3351 * ensured with console_lock. 3352 */ 3353 static void console_callback(struct work_struct *ignored) 3354 { 3355 guard(console_lock)(); 3356 3357 if (want_console >= 0) { 3358 if (want_console != fg_console && 3359 vc_cons_allocated(want_console)) { 3360 hide_cursor(vc_cons[fg_console].d); 3361 change_console(vc_cons[want_console].d); 3362 /* we only changed when the console had already 3363 been allocated - a new console is not created 3364 in an interrupt routine */ 3365 } 3366 want_console = -1; 3367 } 3368 if (do_poke_blanked_console) { /* do not unblank for a LED change */ 3369 do_poke_blanked_console = 0; 3370 poke_blanked_console(); 3371 } 3372 if (scrollback_delta) { 3373 struct vc_data *vc = vc_cons[fg_console].d; 3374 clear_selection(); 3375 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta) 3376 vc->vc_sw->con_scrolldelta(vc, scrollback_delta); 3377 scrollback_delta = 0; 3378 } 3379 if (blank_timer_expired) { 3380 do_blank_screen(0); 3381 blank_timer_expired = 0; 3382 } 3383 notify_update(vc_cons[fg_console].d); 3384 } 3385 3386 int set_console(int nr) 3387 { 3388 struct vc_data *vc = vc_cons[fg_console].d; 3389 3390 if (!vc_cons_allocated(nr) || vt_dont_switch || 3391 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) { 3392 3393 /* 3394 * Console switch will fail in console_callback() or 3395 * change_console() so there is no point scheduling 3396 * the callback 3397 * 3398 * Existing set_console() users don't check the return 3399 * value so this shouldn't break anything 3400 */ 3401 return -EINVAL; 3402 } 3403 3404 want_console = nr; 3405 schedule_console_callback(); 3406 3407 return 0; 3408 } 3409 3410 struct tty_driver *console_driver; 3411 3412 #ifdef CONFIG_VT_CONSOLE 3413 3414 /** 3415 * vt_kmsg_redirect() - sets/gets the kernel message console 3416 * @new: the new virtual terminal number or -1 if the console should stay 3417 * unchanged 3418 * 3419 * By default, the kernel messages are always printed on the current virtual 3420 * console. However, the user may modify that default with the 3421 * %TIOCL_SETKMSGREDIRECT ioctl call. 3422 * 3423 * This function sets the kernel message console to be @new. It returns the old 3424 * virtual console number. The virtual terminal number %0 (both as parameter and 3425 * return value) means no redirection (i.e. always printed on the currently 3426 * active console). 3427 * 3428 * The parameter -1 means that only the current console is returned, but the 3429 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that 3430 * case to make the code more understandable. 3431 * 3432 * When the kernel is compiled without %CONFIG_VT_CONSOLE, this function ignores 3433 * the parameter and always returns %0. 3434 */ 3435 int vt_kmsg_redirect(int new) 3436 { 3437 static int kmsg_con; 3438 3439 if (new != -1) 3440 return xchg(&kmsg_con, new); 3441 else 3442 return kmsg_con; 3443 } 3444 3445 /* 3446 * Console on virtual terminal 3447 * 3448 * The console must be locked when we get here. 3449 */ 3450 3451 static void vt_console_print(struct console *co, const char *b, unsigned count) 3452 { 3453 struct vc_data *vc = vc_cons[fg_console].d; 3454 unsigned char c; 3455 static DEFINE_SPINLOCK(printing_lock); 3456 const ushort *start; 3457 ushort start_x, cnt; 3458 int kmsg_console; 3459 3460 WARN_CONSOLE_UNLOCKED(); 3461 3462 /* this protects against concurrent oops only */ 3463 if (!spin_trylock(&printing_lock)) 3464 return; 3465 3466 kmsg_console = vt_get_kmsg_redirect(); 3467 if (kmsg_console && vc_cons_allocated(kmsg_console - 1)) 3468 vc = vc_cons[kmsg_console - 1].d; 3469 3470 if (!vc_cons_allocated(fg_console)) { 3471 /* impossible */ 3472 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */ 3473 goto quit; 3474 } 3475 3476 if (vc->vc_mode != KD_TEXT) 3477 goto quit; 3478 3479 /* undraw cursor first */ 3480 if (con_is_fg(vc)) 3481 hide_cursor(vc); 3482 3483 start = (ushort *)vc->vc_pos; 3484 start_x = vc->state.x; 3485 cnt = 0; 3486 while (count--) { 3487 c = *b++; 3488 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET || 3489 c == ASCII_BACKSPACE || vc->vc_need_wrap) { 3490 if (cnt && con_is_visible(vc)) 3491 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3492 cnt = 0; 3493 if (c == ASCII_BACKSPACE) { 3494 bs(vc); 3495 start = (ushort *)vc->vc_pos; 3496 start_x = vc->state.x; 3497 continue; 3498 } 3499 if (c != ASCII_CAR_RET) 3500 lf(vc); 3501 cr(vc); 3502 start = (ushort *)vc->vc_pos; 3503 start_x = vc->state.x; 3504 if (c == ASCII_LINEFEED || c == ASCII_CAR_RET) 3505 continue; 3506 } 3507 vc_uniscr_putc(vc, c); 3508 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos); 3509 notify_write(vc, c); 3510 cnt++; 3511 if (vc->state.x == vc->vc_cols - 1) { 3512 vc->vc_need_wrap = 1; 3513 } else { 3514 vc->vc_pos += 2; 3515 vc->state.x++; 3516 } 3517 } 3518 if (cnt && con_is_visible(vc)) 3519 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x); 3520 set_cursor(vc); 3521 notify_update(vc); 3522 3523 quit: 3524 spin_unlock(&printing_lock); 3525 } 3526 3527 static struct tty_driver *vt_console_device(struct console *c, int *index) 3528 { 3529 *index = c->index ? c->index-1 : fg_console; 3530 return console_driver; 3531 } 3532 3533 static int vt_console_setup(struct console *co, char *options) 3534 { 3535 return co->index >= MAX_NR_CONSOLES ? -EINVAL : 0; 3536 } 3537 3538 static struct console vt_console_driver = { 3539 .name = "tty", 3540 .setup = vt_console_setup, 3541 .write = vt_console_print, 3542 .device = vt_console_device, 3543 .unblank = unblank_screen, 3544 .flags = CON_PRINTBUFFER, 3545 .index = -1, 3546 }; 3547 #endif 3548 3549 /* 3550 * Handling of Linux-specific VC ioctls 3551 */ 3552 3553 /* 3554 * Generally a bit racy with respect to console_lock();. 3555 * 3556 * There are some functions which don't need it. 3557 * 3558 * There are some functions which can sleep for arbitrary periods 3559 * (paste_selection) but we don't need the lock there anyway. 3560 * 3561 * set_selection_user has locking, and definitely needs it 3562 */ 3563 3564 int tioclinux(struct tty_struct *tty, unsigned long arg) 3565 { 3566 char type, data; 3567 char __user *p = (char __user *)arg; 3568 void __user *param_aligned32 = (u32 __user *)arg + 1; 3569 void __user *param = (void __user *)arg + 1; 3570 int lines; 3571 int ret; 3572 3573 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN)) 3574 return -EPERM; 3575 if (get_user(type, p)) 3576 return -EFAULT; 3577 ret = 0; 3578 3579 switch (type) { 3580 case TIOCL_SETSEL: 3581 return set_selection_user(param, tty); 3582 case TIOCL_PASTESEL: 3583 if (!capable(CAP_SYS_ADMIN)) 3584 return -EPERM; 3585 return paste_selection(tty); 3586 case TIOCL_UNBLANKSCREEN: 3587 scoped_guard(console_lock) 3588 unblank_screen(); 3589 break; 3590 case TIOCL_SELLOADLUT: 3591 if (!capable(CAP_SYS_ADMIN)) 3592 return -EPERM; 3593 return sel_loadlut(param_aligned32); 3594 case TIOCL_GETSHIFTSTATE: 3595 /* 3596 * Make it possible to react to Shift+Mousebutton. Note that 3597 * 'shift_state' is an undocumented kernel-internal variable; 3598 * programs not closely related to the kernel should not use 3599 * this. 3600 */ 3601 data = vt_get_shift_state(); 3602 return put_user(data, p); 3603 case TIOCL_GETMOUSEREPORTING: 3604 scoped_guard(console_lock) /* May be overkill */ 3605 data = mouse_reporting(); 3606 return put_user(data, p); 3607 case TIOCL_SETVESABLANK: 3608 return set_vesa_blanking(param); 3609 case TIOCL_GETKMSGREDIRECT: 3610 data = vt_get_kmsg_redirect(); 3611 return put_user(data, p); 3612 case TIOCL_SETKMSGREDIRECT: 3613 if (!capable(CAP_SYS_ADMIN)) 3614 return -EPERM; 3615 3616 if (get_user(data, p+1)) 3617 return -EFAULT; 3618 3619 vt_kmsg_redirect(data); 3620 3621 break; 3622 case TIOCL_GETFGCONSOLE: 3623 /* 3624 * No locking needed as this is a transiently correct return 3625 * anyway if the caller hasn't disabled switching. 3626 */ 3627 return fg_console; 3628 case TIOCL_SCROLLCONSOLE: 3629 if (get_user(lines, (s32 __user *)param_aligned32)) 3630 return -EFAULT; 3631 3632 /* 3633 * Needs the console lock here. Note that lots of other calls 3634 * need fixing before the lock is actually useful! 3635 */ 3636 scoped_guard(console_lock) 3637 scrollfront(vc_cons[fg_console].d, lines); 3638 break; 3639 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ 3640 scoped_guard(console_lock) { 3641 ignore_poke = 1; 3642 do_blank_screen(0); 3643 } 3644 break; 3645 case TIOCL_BLANKEDSCREEN: 3646 return console_blanked; 3647 case TIOCL_GETBRACKETEDPASTE: 3648 return get_bracketed_paste(tty); 3649 default: 3650 return -EINVAL; 3651 } 3652 3653 return ret; 3654 } 3655 3656 /* 3657 * /dev/ttyN handling 3658 */ 3659 3660 static ssize_t con_write(struct tty_struct *tty, const u8 *buf, size_t count) 3661 { 3662 int retval; 3663 3664 retval = do_con_write(tty, buf, count); 3665 con_flush_chars(tty); 3666 3667 return retval; 3668 } 3669 3670 static int con_put_char(struct tty_struct *tty, u8 ch) 3671 { 3672 return do_con_write(tty, &ch, 1); 3673 } 3674 3675 static unsigned int con_write_room(struct tty_struct *tty) 3676 { 3677 if (tty->flow.stopped) 3678 return 0; 3679 return 32768; /* No limit, really; we're not buffering */ 3680 } 3681 3682 /* 3683 * con_throttle and con_unthrottle are only used for 3684 * paste_selection(), which has to stuff in a large number of 3685 * characters... 3686 */ 3687 static void con_throttle(struct tty_struct *tty) 3688 { 3689 } 3690 3691 static void con_unthrottle(struct tty_struct *tty) 3692 { 3693 struct vc_data *vc = tty->driver_data; 3694 3695 wake_up_interruptible(&vc->paste_wait); 3696 } 3697 3698 /* 3699 * Turn the Scroll-Lock LED on when the tty is stopped 3700 */ 3701 static void con_stop(struct tty_struct *tty) 3702 { 3703 int console_num; 3704 if (!tty) 3705 return; 3706 console_num = tty->index; 3707 if (!vc_cons_allocated(console_num)) 3708 return; 3709 vt_kbd_con_stop(console_num); 3710 } 3711 3712 /* 3713 * Turn the Scroll-Lock LED off when the console is started 3714 */ 3715 static void con_start(struct tty_struct *tty) 3716 { 3717 int console_num; 3718 if (!tty) 3719 return; 3720 console_num = tty->index; 3721 if (!vc_cons_allocated(console_num)) 3722 return; 3723 vt_kbd_con_start(console_num); 3724 } 3725 3726 static void con_flush_chars(struct tty_struct *tty) 3727 { 3728 struct vc_data *vc = tty->driver_data; 3729 3730 if (in_interrupt()) /* from flush_to_ldisc */ 3731 return; 3732 3733 guard(console_lock)(); 3734 set_cursor(vc); 3735 } 3736 3737 /* 3738 * Allocate the console screen memory. 3739 */ 3740 static int con_install(struct tty_driver *driver, struct tty_struct *tty) 3741 { 3742 unsigned int currcons = tty->index; 3743 struct vc_data *vc; 3744 int ret; 3745 3746 guard(console_lock)(); 3747 ret = vc_allocate(currcons); 3748 if (ret) 3749 return ret; 3750 3751 vc = vc_cons[currcons].d; 3752 3753 /* Still being freed */ 3754 if (vc->port.tty) 3755 return -ERESTARTSYS; 3756 3757 ret = tty_port_install(&vc->port, driver, tty); 3758 if (ret) 3759 return ret; 3760 3761 tty->driver_data = vc; 3762 vc->port.tty = tty; 3763 tty_port_get(&vc->port); 3764 3765 if (!tty->winsize.ws_row && !tty->winsize.ws_col) { 3766 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; 3767 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols; 3768 } 3769 if (vc->vc_utf) 3770 tty->termios.c_iflag |= IUTF8; 3771 else 3772 tty->termios.c_iflag &= ~IUTF8; 3773 3774 return 0; 3775 } 3776 3777 static int con_open(struct tty_struct *tty, struct file *filp) 3778 { 3779 /* everything done in install */ 3780 return 0; 3781 } 3782 3783 3784 static void con_close(struct tty_struct *tty, struct file *filp) 3785 { 3786 /* Nothing to do - we defer to shutdown */ 3787 } 3788 3789 static void con_shutdown(struct tty_struct *tty) 3790 { 3791 struct vc_data *vc = tty->driver_data; 3792 BUG_ON(vc == NULL); 3793 3794 guard(console_lock)(); 3795 vc->port.tty = NULL; 3796 } 3797 3798 static void con_cleanup(struct tty_struct *tty) 3799 { 3800 struct vc_data *vc = tty->driver_data; 3801 3802 tty_port_put(&vc->port); 3803 } 3804 3805 /* 3806 * We can't deal with anything but the N_TTY ldisc, 3807 * because we can sleep in our write() routine. 3808 */ 3809 static int con_ldisc_ok(struct tty_struct *tty, int ldisc) 3810 { 3811 return ldisc == N_TTY ? 0 : -EINVAL; 3812 } 3813 3814 static int default_color = 7; /* white */ 3815 static int default_italic_color = 2; // green (ASCII) 3816 static int default_underline_color = 3; // cyan (ASCII) 3817 module_param_named(color, default_color, int, S_IRUGO | S_IWUSR); 3818 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); 3819 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); 3820 3821 static void vc_init(struct vc_data *vc, int do_clear) 3822 { 3823 int j, k ; 3824 3825 set_origin(vc); 3826 vc->vc_pos = vc->vc_origin; 3827 reset_vc(vc); 3828 for (j=k=0; j<16; j++) { 3829 vc->vc_palette[k++] = default_red[j] ; 3830 vc->vc_palette[k++] = default_grn[j] ; 3831 vc->vc_palette[k++] = default_blu[j] ; 3832 } 3833 vc->vc_def_color = default_color; 3834 vc->vc_ulcolor = default_underline_color; 3835 vc->vc_itcolor = default_italic_color; 3836 vc->vc_halfcolor = 0x08; /* grey */ 3837 init_waitqueue_head(&vc->paste_wait); 3838 reset_terminal(vc, do_clear); 3839 } 3840 3841 /* 3842 * This routine initializes console interrupts, and does nothing 3843 * else. If you want the screen to clear, call tty_write with 3844 * the appropriate escape-sequence. 3845 */ 3846 3847 static int __init con_init(void) 3848 { 3849 const char *display_desc = NULL; 3850 struct vc_data *vc; 3851 unsigned int currcons = 0, i; 3852 3853 console_lock(); 3854 3855 if (!conswitchp) 3856 conswitchp = &dummy_con; 3857 display_desc = conswitchp->con_startup(); 3858 if (!display_desc) { 3859 fg_console = 0; 3860 console_unlock(); 3861 return 0; 3862 } 3863 3864 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 3865 struct con_driver *con_driver = ®istered_con_driver[i]; 3866 3867 if (con_driver->con == NULL) { 3868 con_driver->con = conswitchp; 3869 con_driver->desc = display_desc; 3870 con_driver->flag = CON_DRIVER_FLAG_INIT; 3871 con_driver->first = 0; 3872 con_driver->last = MAX_NR_CONSOLES - 1; 3873 break; 3874 } 3875 } 3876 3877 for (i = 0; i < MAX_NR_CONSOLES; i++) 3878 con_driver_map[i] = conswitchp; 3879 3880 if (blankinterval) { 3881 blank_state = blank_normal_wait; 3882 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 3883 } 3884 3885 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { 3886 vc_cons[currcons].d = vc = kzalloc_obj(struct vc_data, 3887 GFP_NOWAIT); 3888 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); 3889 tty_port_init(&vc->port); 3890 visual_init(vc, currcons, true); 3891 /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */ 3892 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); 3893 vc_init(vc, currcons || !vc->vc_sw->con_save_screen); 3894 } 3895 currcons = fg_console = 0; 3896 master_display_fg = vc = vc_cons[currcons].d; 3897 set_origin(vc); 3898 save_screen(vc); 3899 gotoxy(vc, vc->state.x, vc->state.y); 3900 csi_J(vc, CSI_J_CURSOR_TO_END); 3901 update_screen(vc); 3902 pr_info("Console: %s %s %dx%d\n", 3903 vc->vc_can_do_color ? "colour" : "mono", 3904 display_desc, vc->vc_cols, vc->vc_rows); 3905 3906 console_unlock(); 3907 3908 #ifdef CONFIG_VT_CONSOLE 3909 register_console(&vt_console_driver); 3910 #endif 3911 return 0; 3912 } 3913 console_initcall(con_init); 3914 3915 static const struct tty_operations con_ops = { 3916 .install = con_install, 3917 .open = con_open, 3918 .close = con_close, 3919 .write = con_write, 3920 .write_room = con_write_room, 3921 .put_char = con_put_char, 3922 .flush_chars = con_flush_chars, 3923 .ioctl = vt_ioctl, 3924 #ifdef CONFIG_COMPAT 3925 .compat_ioctl = vt_compat_ioctl, 3926 #endif 3927 .stop = con_stop, 3928 .start = con_start, 3929 .throttle = con_throttle, 3930 .unthrottle = con_unthrottle, 3931 .resize = vt_resize, 3932 .shutdown = con_shutdown, 3933 .cleanup = con_cleanup, 3934 .ldisc_ok = con_ldisc_ok, 3935 }; 3936 3937 static struct cdev vc0_cdev; 3938 3939 static ssize_t show_tty_active(struct device *dev, 3940 struct device_attribute *attr, char *buf) 3941 { 3942 return sprintf(buf, "tty%d\n", fg_console + 1); 3943 } 3944 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL); 3945 3946 static struct attribute *vt_dev_attrs[] = { 3947 &dev_attr_active.attr, 3948 NULL 3949 }; 3950 3951 ATTRIBUTE_GROUPS(vt_dev); 3952 3953 int __init vty_init(const struct file_operations *console_fops) 3954 { 3955 cdev_init(&vc0_cdev, console_fops); 3956 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || 3957 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) 3958 panic("Couldn't register /dev/tty0 driver\n"); 3959 tty0dev = device_create_with_groups(&tty_class, NULL, 3960 MKDEV(TTY_MAJOR, 0), NULL, 3961 vt_dev_groups, "tty0"); 3962 if (IS_ERR(tty0dev)) 3963 tty0dev = NULL; 3964 3965 vcs_init(); 3966 3967 console_driver = tty_alloc_driver(MAX_NR_CONSOLES, TTY_DRIVER_REAL_RAW | 3968 TTY_DRIVER_RESET_TERMIOS); 3969 if (IS_ERR(console_driver)) 3970 panic("Couldn't allocate console driver\n"); 3971 3972 console_driver->name = "tty"; 3973 console_driver->name_base = 1; 3974 console_driver->major = TTY_MAJOR; 3975 console_driver->minor_start = 1; 3976 console_driver->type = TTY_DRIVER_TYPE_CONSOLE; 3977 console_driver->init_termios = tty_std_termios; 3978 if (default_utf8) 3979 console_driver->init_termios.c_iflag |= IUTF8; 3980 tty_set_operations(console_driver, &con_ops); 3981 if (tty_register_driver(console_driver)) 3982 panic("Couldn't register console driver\n"); 3983 kbd_init(); 3984 console_map_init(); 3985 return 0; 3986 } 3987 3988 static const struct class vtconsole_class = { 3989 .name = "vtconsole", 3990 }; 3991 3992 static int do_bind_con_driver(const struct consw *csw, int first, int last, 3993 int deflt) 3994 { 3995 struct module *owner = csw->owner; 3996 const char *desc = NULL; 3997 struct con_driver *con_driver; 3998 int i, j = -1, k = -1, retval = -ENODEV; 3999 4000 if (!try_module_get(owner)) 4001 return -ENODEV; 4002 4003 WARN_CONSOLE_UNLOCKED(); 4004 4005 /* check if driver is registered */ 4006 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4007 con_driver = ®istered_con_driver[i]; 4008 4009 if (con_driver->con == csw) { 4010 desc = con_driver->desc; 4011 retval = 0; 4012 break; 4013 } 4014 } 4015 4016 if (retval) 4017 goto err; 4018 4019 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) { 4020 csw->con_startup(); 4021 con_driver->flag |= CON_DRIVER_FLAG_INIT; 4022 } 4023 4024 if (deflt) { 4025 if (conswitchp) 4026 module_put(conswitchp->owner); 4027 4028 __module_get(owner); 4029 conswitchp = csw; 4030 } 4031 4032 first = max(first, con_driver->first); 4033 last = min(last, con_driver->last); 4034 4035 for (i = first; i <= last; i++) { 4036 int old_was_color; 4037 struct vc_data *vc = vc_cons[i].d; 4038 4039 if (con_driver_map[i]) 4040 module_put(con_driver_map[i]->owner); 4041 __module_get(owner); 4042 con_driver_map[i] = csw; 4043 4044 if (!vc || !vc->vc_sw) 4045 continue; 4046 4047 j = i; 4048 4049 if (con_is_visible(vc)) { 4050 k = i; 4051 save_screen(vc); 4052 } 4053 4054 old_was_color = vc->vc_can_do_color; 4055 vc->vc_sw->con_deinit(vc); 4056 vc->vc_origin = (unsigned long)vc->vc_screenbuf; 4057 visual_init(vc, i, false); 4058 set_origin(vc); 4059 update_attr(vc); 4060 4061 /* If the console changed between mono <-> color, then 4062 * the attributes in the screenbuf will be wrong. The 4063 * following resets all attributes to something sane. 4064 */ 4065 if (old_was_color != vc->vc_can_do_color) 4066 clear_buffer_attributes(vc); 4067 } 4068 4069 pr_info("Console: switching "); 4070 if (!deflt) 4071 pr_cont("consoles %d-%d ", first + 1, last + 1); 4072 if (j >= 0) { 4073 struct vc_data *vc = vc_cons[j].d; 4074 4075 pr_cont("to %s %s %dx%d\n", 4076 vc->vc_can_do_color ? "colour" : "mono", 4077 desc, vc->vc_cols, vc->vc_rows); 4078 4079 if (k >= 0) { 4080 vc = vc_cons[k].d; 4081 update_screen(vc); 4082 } 4083 } else { 4084 pr_cont("to %s\n", desc); 4085 } 4086 4087 retval = 0; 4088 err: 4089 module_put(owner); 4090 return retval; 4091 }; 4092 4093 4094 #ifdef CONFIG_VT_HW_CONSOLE_BINDING 4095 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt) 4096 { 4097 struct module *owner = csw->owner; 4098 const struct consw *defcsw = NULL; 4099 struct con_driver *con_driver = NULL, *con_back = NULL; 4100 int i, retval = -ENODEV; 4101 4102 if (!try_module_get(owner)) 4103 return -ENODEV; 4104 4105 WARN_CONSOLE_UNLOCKED(); 4106 4107 /* check if driver is registered and if it is unbindable */ 4108 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4109 con_driver = ®istered_con_driver[i]; 4110 4111 if (con_driver->con == csw && 4112 con_driver->flag & CON_DRIVER_FLAG_MODULE) { 4113 retval = 0; 4114 break; 4115 } 4116 } 4117 4118 if (retval) 4119 goto err; 4120 4121 retval = -ENODEV; 4122 4123 /* check if backup driver exists */ 4124 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4125 con_back = ®istered_con_driver[i]; 4126 4127 if (con_back->con && con_back->con != csw) { 4128 defcsw = con_back->con; 4129 retval = 0; 4130 break; 4131 } 4132 } 4133 4134 if (retval) 4135 goto err; 4136 4137 if (!con_is_bound(csw)) 4138 goto err; 4139 4140 first = max(first, con_driver->first); 4141 last = min(last, con_driver->last); 4142 4143 for (i = first; i <= last; i++) { 4144 if (con_driver_map[i] == csw) { 4145 module_put(csw->owner); 4146 con_driver_map[i] = NULL; 4147 } 4148 } 4149 4150 if (!con_is_bound(defcsw)) { 4151 const struct consw *defconsw = conswitchp; 4152 4153 defcsw->con_startup(); 4154 con_back->flag |= CON_DRIVER_FLAG_INIT; 4155 /* 4156 * vgacon may change the default driver to point 4157 * to dummycon, we restore it here... 4158 */ 4159 conswitchp = defconsw; 4160 } 4161 4162 if (!con_is_bound(csw)) 4163 con_driver->flag &= ~CON_DRIVER_FLAG_INIT; 4164 4165 /* ignore return value, binding should not fail */ 4166 do_bind_con_driver(defcsw, first, last, deflt); 4167 err: 4168 module_put(owner); 4169 return retval; 4170 4171 } 4172 EXPORT_SYMBOL_GPL(do_unbind_con_driver); 4173 4174 static int vt_bind(struct con_driver *con) 4175 { 4176 const struct consw *defcsw = NULL, *csw = NULL; 4177 int i, more = 1, first = -1, last = -1, deflt = 0; 4178 4179 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4180 goto err; 4181 4182 csw = con->con; 4183 4184 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4185 struct con_driver *con = ®istered_con_driver[i]; 4186 4187 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) { 4188 defcsw = con->con; 4189 break; 4190 } 4191 } 4192 4193 if (!defcsw) 4194 goto err; 4195 4196 while (more) { 4197 more = 0; 4198 4199 for (i = con->first; i <= con->last; i++) { 4200 if (con_driver_map[i] == defcsw) { 4201 if (first == -1) 4202 first = i; 4203 last = i; 4204 more = 1; 4205 } else if (first != -1) 4206 break; 4207 } 4208 4209 if (first == 0 && last == MAX_NR_CONSOLES -1) 4210 deflt = 1; 4211 4212 if (first != -1) 4213 do_bind_con_driver(csw, first, last, deflt); 4214 4215 first = -1; 4216 last = -1; 4217 deflt = 0; 4218 } 4219 4220 err: 4221 return 0; 4222 } 4223 4224 static int vt_unbind(struct con_driver *con) 4225 { 4226 const struct consw *csw = NULL; 4227 int i, more = 1, first = -1, last = -1, deflt = 0; 4228 int ret; 4229 4230 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) 4231 goto err; 4232 4233 csw = con->con; 4234 4235 while (more) { 4236 more = 0; 4237 4238 for (i = con->first; i <= con->last; i++) { 4239 if (con_driver_map[i] == csw) { 4240 if (first == -1) 4241 first = i; 4242 last = i; 4243 more = 1; 4244 } else if (first != -1) 4245 break; 4246 } 4247 4248 if (first == 0 && last == MAX_NR_CONSOLES -1) 4249 deflt = 1; 4250 4251 if (first != -1) { 4252 ret = do_unbind_con_driver(csw, first, last, deflt); 4253 if (ret != 0) 4254 return ret; 4255 } 4256 4257 first = -1; 4258 last = -1; 4259 deflt = 0; 4260 } 4261 4262 err: 4263 return 0; 4264 } 4265 #else 4266 static inline int vt_bind(struct con_driver *con) 4267 { 4268 return 0; 4269 } 4270 static inline int vt_unbind(struct con_driver *con) 4271 { 4272 return 0; 4273 } 4274 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ 4275 4276 static ssize_t store_bind(struct device *dev, struct device_attribute *attr, 4277 const char *buf, size_t count) 4278 { 4279 struct con_driver *con = dev_get_drvdata(dev); 4280 int bind = simple_strtoul(buf, NULL, 0); 4281 4282 guard(console_lock)(); 4283 4284 if (bind) 4285 vt_bind(con); 4286 else 4287 vt_unbind(con); 4288 4289 return count; 4290 } 4291 4292 static ssize_t show_bind(struct device *dev, struct device_attribute *attr, 4293 char *buf) 4294 { 4295 struct con_driver *con = dev_get_drvdata(dev); 4296 int bind; 4297 4298 scoped_guard(console_lock) 4299 bind = con_is_bound(con->con); 4300 4301 return sysfs_emit(buf, "%i\n", bind); 4302 } 4303 4304 static ssize_t show_name(struct device *dev, struct device_attribute *attr, 4305 char *buf) 4306 { 4307 struct con_driver *con = dev_get_drvdata(dev); 4308 4309 return sysfs_emit(buf, "%s %s\n", 4310 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)", 4311 con->desc); 4312 4313 } 4314 4315 static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind); 4316 static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); 4317 4318 static struct attribute *con_dev_attrs[] = { 4319 &dev_attr_bind.attr, 4320 &dev_attr_name.attr, 4321 NULL 4322 }; 4323 4324 ATTRIBUTE_GROUPS(con_dev); 4325 4326 static int vtconsole_init_device(struct con_driver *con) 4327 { 4328 con->flag |= CON_DRIVER_FLAG_ATTR; 4329 return 0; 4330 } 4331 4332 static void vtconsole_deinit_device(struct con_driver *con) 4333 { 4334 con->flag &= ~CON_DRIVER_FLAG_ATTR; 4335 } 4336 4337 /** 4338 * con_is_bound - checks if driver is bound to the console 4339 * @csw: console driver 4340 * 4341 * RETURNS: zero if unbound, nonzero if bound 4342 * 4343 * Drivers can call this and if zero, they should release 4344 * all resources allocated on &consw.con_startup() 4345 */ 4346 int con_is_bound(const struct consw *csw) 4347 { 4348 int i, bound = 0; 4349 4350 WARN_CONSOLE_UNLOCKED(); 4351 4352 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4353 if (con_driver_map[i] == csw) { 4354 bound = 1; 4355 break; 4356 } 4357 } 4358 4359 return bound; 4360 } 4361 EXPORT_SYMBOL(con_is_bound); 4362 4363 /** 4364 * con_is_visible - checks whether the current console is visible 4365 * @vc: virtual console 4366 * 4367 * RETURNS: zero if not visible, nonzero if visible 4368 */ 4369 bool con_is_visible(const struct vc_data *vc) 4370 { 4371 WARN_CONSOLE_UNLOCKED(); 4372 4373 return *vc->vc_display_fg == vc; 4374 } 4375 EXPORT_SYMBOL(con_is_visible); 4376 4377 /** 4378 * con_debug_enter - prepare the console for the kernel debugger 4379 * @vc: virtual console 4380 * 4381 * Called when the console is taken over by the kernel debugger, this 4382 * function needs to save the current console state, then put the console 4383 * into a state suitable for the kernel debugger. 4384 */ 4385 void con_debug_enter(struct vc_data *vc) 4386 { 4387 #ifdef CONFIG_KGDB_KDB 4388 /* Set the initial LINES variable if it is not already set */ 4389 if (vc->vc_rows < 999) { 4390 int linecount; 4391 char lns[4]; 4392 const char *setargs[3] = { 4393 "set", 4394 "LINES", 4395 lns, 4396 }; 4397 if (kdbgetintenv(setargs[0], &linecount)) { 4398 snprintf(lns, 4, "%i", vc->vc_rows); 4399 kdb_set(2, setargs); 4400 } 4401 } 4402 if (vc->vc_cols < 999) { 4403 int colcount; 4404 char cols[4]; 4405 const char *setargs[3] = { 4406 "set", 4407 "COLUMNS", 4408 cols, 4409 }; 4410 if (kdbgetintenv(setargs[0], &colcount)) { 4411 snprintf(cols, 4, "%i", vc->vc_cols); 4412 kdb_set(2, setargs); 4413 } 4414 } 4415 #endif /* CONFIG_KGDB_KDB */ 4416 } 4417 EXPORT_SYMBOL_GPL(con_debug_enter); 4418 4419 /** 4420 * con_debug_leave - restore console state 4421 * 4422 * Restore the console state to what it was before the kernel debugger 4423 * was invoked. 4424 */ 4425 void con_debug_leave(void) 4426 { } 4427 EXPORT_SYMBOL_GPL(con_debug_leave); 4428 4429 static int do_register_con_driver(const struct consw *csw, int first, int last) 4430 { 4431 struct module *owner = csw->owner; 4432 struct con_driver *con_driver; 4433 const char *desc; 4434 int i, retval; 4435 4436 WARN_CONSOLE_UNLOCKED(); 4437 4438 if (!try_module_get(owner)) 4439 return -ENODEV; 4440 4441 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4442 con_driver = ®istered_con_driver[i]; 4443 4444 /* already registered */ 4445 if (con_driver->con == csw) { 4446 retval = -EBUSY; 4447 goto err; 4448 } 4449 } 4450 4451 desc = csw->con_startup(); 4452 if (!desc) { 4453 retval = -ENODEV; 4454 goto err; 4455 } 4456 4457 retval = -EINVAL; 4458 4459 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4460 con_driver = ®istered_con_driver[i]; 4461 4462 if (con_driver->con == NULL && 4463 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) { 4464 con_driver->con = csw; 4465 con_driver->desc = desc; 4466 con_driver->node = i; 4467 con_driver->flag = CON_DRIVER_FLAG_MODULE | 4468 CON_DRIVER_FLAG_INIT; 4469 con_driver->first = first; 4470 con_driver->last = last; 4471 retval = 0; 4472 break; 4473 } 4474 } 4475 4476 if (retval) 4477 goto err; 4478 4479 con_driver->dev = 4480 device_create_with_groups(&vtconsole_class, NULL, 4481 MKDEV(0, con_driver->node), 4482 con_driver, con_dev_groups, 4483 "vtcon%i", con_driver->node); 4484 if (IS_ERR(con_driver->dev)) { 4485 pr_warn("Unable to create device for %s; errno = %ld\n", 4486 con_driver->desc, PTR_ERR(con_driver->dev)); 4487 con_driver->dev = NULL; 4488 } else { 4489 vtconsole_init_device(con_driver); 4490 } 4491 4492 err: 4493 module_put(owner); 4494 return retval; 4495 } 4496 4497 4498 /** 4499 * do_unregister_con_driver - unregister console driver from console layer 4500 * @csw: console driver 4501 * 4502 * DESCRIPTION: All drivers that registers to the console layer must 4503 * call this function upon exit, or if the console driver is in a state 4504 * where it won't be able to handle console services, such as the 4505 * framebuffer console without loaded framebuffer drivers. 4506 * 4507 * The driver must unbind first prior to unregistration. 4508 */ 4509 int do_unregister_con_driver(const struct consw *csw) 4510 { 4511 int i; 4512 4513 /* cannot unregister a bound driver */ 4514 if (con_is_bound(csw)) 4515 return -EBUSY; 4516 4517 if (csw == conswitchp) 4518 return -EINVAL; 4519 4520 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4521 struct con_driver *con_driver = ®istered_con_driver[i]; 4522 4523 if (con_driver->con == csw) { 4524 /* 4525 * Defer the removal of the sysfs entries since that 4526 * will acquire the kernfs s_active lock and we can't 4527 * acquire this lock while holding the console lock: 4528 * the unbind sysfs entry imposes already the opposite 4529 * order. Reset con already here to prevent any later 4530 * lookup to succeed and mark this slot as zombie, so 4531 * it won't get reused until we complete the removal 4532 * in the deferred work. 4533 */ 4534 con_driver->con = NULL; 4535 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE; 4536 schedule_work(&con_driver_unregister_work); 4537 4538 return 0; 4539 } 4540 } 4541 4542 return -ENODEV; 4543 } 4544 EXPORT_SYMBOL_GPL(do_unregister_con_driver); 4545 4546 static void con_driver_unregister_callback(struct work_struct *ignored) 4547 { 4548 int i; 4549 4550 guard(console_lock)(); 4551 4552 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4553 struct con_driver *con_driver = ®istered_con_driver[i]; 4554 4555 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) 4556 continue; 4557 4558 console_unlock(); 4559 4560 vtconsole_deinit_device(con_driver); 4561 device_destroy(&vtconsole_class, MKDEV(0, con_driver->node)); 4562 4563 console_lock(); 4564 4565 if (WARN_ON_ONCE(con_driver->con)) 4566 con_driver->con = NULL; 4567 con_driver->desc = NULL; 4568 con_driver->dev = NULL; 4569 con_driver->node = 0; 4570 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE); 4571 con_driver->flag = 0; 4572 con_driver->first = 0; 4573 con_driver->last = 0; 4574 } 4575 } 4576 4577 /* 4578 * If we support more console drivers, this function is used 4579 * when a driver wants to take over some existing consoles 4580 * and become default driver for newly opened ones. 4581 * 4582 * do_take_over_console is basically a register followed by bind 4583 */ 4584 int do_take_over_console(const struct consw *csw, int first, int last, int deflt) 4585 { 4586 int err; 4587 4588 err = do_register_con_driver(csw, first, last); 4589 /* 4590 * If we get an busy error we still want to bind the console driver 4591 * and return success, as we may have unbound the console driver 4592 * but not unregistered it. 4593 */ 4594 if (err == -EBUSY) 4595 err = 0; 4596 if (!err) 4597 do_bind_con_driver(csw, first, last, deflt); 4598 4599 return err; 4600 } 4601 EXPORT_SYMBOL_GPL(do_take_over_console); 4602 4603 4604 /* 4605 * give_up_console is a wrapper to unregister_con_driver. It will only 4606 * work if driver is fully unbound. 4607 */ 4608 void give_up_console(const struct consw *csw) 4609 { 4610 guard(console_lock)(); 4611 do_unregister_con_driver(csw); 4612 } 4613 EXPORT_SYMBOL(give_up_console); 4614 4615 static int __init vtconsole_class_init(void) 4616 { 4617 int i; 4618 4619 i = class_register(&vtconsole_class); 4620 if (i) 4621 pr_warn("Unable to create vt console class; errno = %d\n", i); 4622 4623 /* Add system drivers to sysfs */ 4624 for (i = 0; i < MAX_NR_CON_DRIVER; i++) { 4625 struct con_driver *con = ®istered_con_driver[i]; 4626 4627 if (con->con && !con->dev) { 4628 con->dev = 4629 device_create_with_groups(&vtconsole_class, NULL, 4630 MKDEV(0, con->node), 4631 con, con_dev_groups, 4632 "vtcon%i", con->node); 4633 4634 if (IS_ERR(con->dev)) { 4635 pr_warn("Unable to create device for %s; errno = %ld\n", 4636 con->desc, PTR_ERR(con->dev)); 4637 con->dev = NULL; 4638 } else { 4639 vtconsole_init_device(con); 4640 } 4641 } 4642 } 4643 4644 return 0; 4645 } 4646 postcore_initcall(vtconsole_class_init); 4647 4648 /* 4649 * Screen blanking 4650 */ 4651 4652 static int set_vesa_blanking(u8 __user *mode_user) 4653 { 4654 u8 mode; 4655 4656 if (get_user(mode, mode_user)) 4657 return -EFAULT; 4658 4659 guard(console_lock)(); 4660 vesa_blank_mode = (mode <= VESA_BLANK_MAX) ? mode : VESA_NO_BLANKING; 4661 4662 return 0; 4663 } 4664 4665 void do_blank_screen(int entering_gfx) 4666 { 4667 struct vc_data *vc = vc_cons[fg_console].d; 4668 int i; 4669 4670 might_sleep(); 4671 4672 WARN_CONSOLE_UNLOCKED(); 4673 4674 if (console_blanked) { 4675 if (blank_state == blank_vesa_wait) { 4676 blank_state = blank_off; 4677 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0); 4678 } 4679 return; 4680 } 4681 4682 /* entering graphics mode? */ 4683 if (entering_gfx) { 4684 hide_cursor(vc); 4685 save_screen(vc); 4686 vc->vc_sw->con_blank(vc, VESA_VSYNC_SUSPEND, 1); 4687 console_blanked = fg_console + 1; 4688 blank_state = blank_off; 4689 set_origin(vc); 4690 return; 4691 } 4692 4693 blank_state = blank_off; 4694 4695 /* don't blank graphics */ 4696 if (vc->vc_mode != KD_TEXT) { 4697 console_blanked = fg_console + 1; 4698 return; 4699 } 4700 4701 hide_cursor(vc); 4702 timer_delete_sync(&console_timer); 4703 blank_timer_expired = 0; 4704 4705 save_screen(vc); 4706 /* In case we need to reset origin, blanking hook returns 1 */ 4707 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? VESA_VSYNC_SUSPEND : 4708 (vesa_blank_mode + 1), 0); 4709 console_blanked = fg_console + 1; 4710 if (i) 4711 set_origin(vc); 4712 4713 if (console_blank_hook && console_blank_hook(1)) 4714 return; 4715 4716 if (vesa_off_interval && vesa_blank_mode) { 4717 blank_state = blank_vesa_wait; 4718 mod_timer(&console_timer, jiffies + vesa_off_interval); 4719 } 4720 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num); 4721 } 4722 EXPORT_SYMBOL(do_blank_screen); 4723 4724 /* 4725 * Called by timer as well as from vt_console_driver 4726 */ 4727 void do_unblank_screen(int leaving_gfx) 4728 { 4729 struct vc_data *vc; 4730 4731 /* This should now always be called from a "sane" (read: can schedule) 4732 * context for the sake of the low level drivers, except in the special 4733 * case of oops_in_progress 4734 */ 4735 if (!oops_in_progress) 4736 might_sleep(); 4737 4738 WARN_CONSOLE_UNLOCKED(); 4739 4740 ignore_poke = 0; 4741 if (!console_blanked) 4742 return; 4743 if (!vc_cons_allocated(fg_console)) { 4744 /* impossible */ 4745 pr_warn("unblank_screen: tty %d not allocated ??\n", 4746 fg_console + 1); 4747 return; 4748 } 4749 vc = vc_cons[fg_console].d; 4750 if (vc->vc_mode != KD_TEXT) 4751 return; /* but leave console_blanked != 0 */ 4752 4753 if (blankinterval) { 4754 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4755 blank_state = blank_normal_wait; 4756 } 4757 4758 console_blanked = 0; 4759 if (vc->vc_sw->con_blank(vc, VESA_NO_BLANKING, leaving_gfx)) 4760 /* Low-level driver cannot restore -> do it ourselves */ 4761 update_screen(vc); 4762 if (console_blank_hook) 4763 console_blank_hook(0); 4764 set_palette(vc); 4765 set_cursor(vc); 4766 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num); 4767 notify_update(vc); 4768 } 4769 EXPORT_SYMBOL(do_unblank_screen); 4770 4771 /* 4772 * This is called by the outside world to cause a forced unblank, mostly for 4773 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually 4774 * call it with 1 as an argument and so force a mode restore... that may kill 4775 * X or at least garbage the screen but would also make the Oops visible... 4776 */ 4777 static void unblank_screen(void) 4778 { 4779 do_unblank_screen(0); 4780 } 4781 4782 /* 4783 * We defer the timer blanking to work queue so it can take the console mutex 4784 * (console operations can still happen at irq time, but only from printk which 4785 * has the console mutex. Not perfect yet, but better than no locking 4786 */ 4787 static void blank_screen_t(struct timer_list *unused) 4788 { 4789 blank_timer_expired = 1; 4790 schedule_work(&console_work); 4791 } 4792 4793 void poke_blanked_console(void) 4794 { 4795 WARN_CONSOLE_UNLOCKED(); 4796 4797 /* Add this so we quickly catch whoever might call us in a non 4798 * safe context. Nowadays, unblank_screen() isn't to be called in 4799 * atomic contexts and is allowed to schedule (with the special case 4800 * of oops_in_progress, but that isn't of any concern for this 4801 * function. --BenH. 4802 */ 4803 might_sleep(); 4804 4805 /* This isn't perfectly race free, but a race here would be mostly harmless, 4806 * at worst, we'll do a spurious blank and it's unlikely 4807 */ 4808 timer_delete(&console_timer); 4809 blank_timer_expired = 0; 4810 4811 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS) 4812 return; 4813 if (console_blanked) 4814 unblank_screen(); 4815 else if (blankinterval) { 4816 mod_timer(&console_timer, jiffies + (blankinterval * HZ)); 4817 blank_state = blank_normal_wait; 4818 } 4819 } 4820 4821 /* 4822 * Palettes 4823 */ 4824 4825 static void set_palette(struct vc_data *vc) 4826 { 4827 WARN_CONSOLE_UNLOCKED(); 4828 4829 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette) 4830 vc->vc_sw->con_set_palette(vc, color_table); 4831 } 4832 4833 /* 4834 * Load palette into the DAC registers. arg points to a colour 4835 * map, 3 bytes per colour, 16 colours, range from 0 to 255. 4836 */ 4837 4838 int con_set_cmap(unsigned char __user *arg) 4839 { 4840 int i, j, k; 4841 unsigned char colormap[3*16]; 4842 4843 if (copy_from_user(colormap, arg, sizeof(colormap))) 4844 return -EFAULT; 4845 4846 guard(console_lock)(); 4847 for (i = k = 0; i < 16; i++) { 4848 default_red[i] = colormap[k++]; 4849 default_grn[i] = colormap[k++]; 4850 default_blu[i] = colormap[k++]; 4851 } 4852 for (i = 0; i < MAX_NR_CONSOLES; i++) { 4853 if (!vc_cons_allocated(i)) 4854 continue; 4855 for (j = k = 0; j < 16; j++) { 4856 vc_cons[i].d->vc_palette[k++] = default_red[j]; 4857 vc_cons[i].d->vc_palette[k++] = default_grn[j]; 4858 vc_cons[i].d->vc_palette[k++] = default_blu[j]; 4859 } 4860 set_palette(vc_cons[i].d); 4861 } 4862 4863 return 0; 4864 } 4865 4866 int con_get_cmap(unsigned char __user *arg) 4867 { 4868 int i, k; 4869 unsigned char colormap[3*16]; 4870 4871 scoped_guard(console_lock) 4872 for (i = k = 0; i < 16; i++) { 4873 colormap[k++] = default_red[i]; 4874 colormap[k++] = default_grn[i]; 4875 colormap[k++] = default_blu[i]; 4876 } 4877 4878 if (copy_to_user(arg, colormap, sizeof(colormap))) 4879 return -EFAULT; 4880 4881 return 0; 4882 } 4883 4884 void reset_palette(struct vc_data *vc) 4885 { 4886 int j, k; 4887 for (j=k=0; j<16; j++) { 4888 vc->vc_palette[k++] = default_red[j]; 4889 vc->vc_palette[k++] = default_grn[j]; 4890 vc->vc_palette[k++] = default_blu[j]; 4891 } 4892 set_palette(vc); 4893 } 4894 4895 /* 4896 * Font switching 4897 * 4898 * Currently we only support fonts up to 128 pixels wide, at a maximum height 4899 * of 128 pixels. Userspace fontdata may have to be stored with 32 bytes 4900 * (shorts/ints, depending on width) reserved for each character which is 4901 * kinda wasty, but this is done in order to maintain compatibility with the 4902 * EGA/VGA fonts. It is up to the actual low-level console-driver convert data 4903 * into its favorite format (maybe we should add a `fontoffset' field to the 4904 * `display' structure so we won't have to convert the fontdata all the time. 4905 * /Jes 4906 */ 4907 4908 #define max_font_width 64 4909 #define max_font_height 128 4910 #define max_font_glyphs 512 4911 #define max_font_size (max_font_glyphs*max_font_width*max_font_height) 4912 4913 static int con_font_get(struct vc_data *vc, struct console_font_op *op) 4914 { 4915 struct console_font font; 4916 int c; 4917 unsigned int vpitch = op->op == KD_FONT_OP_GET_TALL ? op->height : 32; 4918 4919 if (vpitch > max_font_height) 4920 return -EINVAL; 4921 4922 void *font_data __free(kvfree) = NULL; 4923 if (op->data) { 4924 font.data = font_data = kvzalloc(max_font_size, GFP_KERNEL); 4925 if (!font.data) 4926 return -ENOMEM; 4927 } else 4928 font.data = NULL; 4929 4930 scoped_guard(console_lock) { 4931 if (vc->vc_mode != KD_TEXT) 4932 return -EINVAL; 4933 if (!vc->vc_sw->con_font_get) 4934 return -ENOSYS; 4935 4936 int ret = vc->vc_sw->con_font_get(vc, &font, vpitch); 4937 if (ret) 4938 return ret; 4939 } 4940 4941 c = DIV_ROUND_UP(font.width, 8) * vpitch * font.charcount; 4942 4943 if (op->data && font.charcount > op->charcount) 4944 return -ENOSPC; 4945 if (font.width > op->width || font.height > op->height) 4946 return -ENOSPC; 4947 4948 op->height = font.height; 4949 op->width = font.width; 4950 op->charcount = font.charcount; 4951 4952 if (op->data && copy_to_user(op->data, font.data, c)) 4953 return -EFAULT; 4954 4955 return 0; 4956 } 4957 4958 static int con_font_set(struct vc_data *vc, const struct console_font_op *op) 4959 { 4960 struct console_font font; 4961 int size; 4962 unsigned int vpitch = op->op == KD_FONT_OP_SET_TALL ? op->height : 32; 4963 4964 if (!op->data) 4965 return -EINVAL; 4966 if (op->charcount > max_font_glyphs) 4967 return -EINVAL; 4968 if (op->width <= 0 || op->width > max_font_width || !op->height || 4969 op->height > max_font_height) 4970 return -EINVAL; 4971 if (vpitch < op->height) 4972 return -EINVAL; 4973 size = DIV_ROUND_UP(op->width, 8) * vpitch * op->charcount; 4974 if (size > max_font_size) 4975 return -ENOSPC; 4976 4977 void *font_data __free(kfree) = font.data = memdup_user(op->data, size); 4978 if (IS_ERR(font.data)) 4979 return PTR_ERR(font.data); 4980 4981 font.charcount = op->charcount; 4982 font.width = op->width; 4983 font.height = op->height; 4984 4985 guard(console_lock)(); 4986 4987 if (vc->vc_mode != KD_TEXT) 4988 return -EINVAL; 4989 if (!vc->vc_sw->con_font_set) 4990 return -ENOSYS; 4991 4992 /* hide selection and cursor prior font changes */ 4993 hide_cursor(vc); 4994 4995 return vc->vc_sw->con_font_set(vc, &font, vpitch, op->flags); 4996 } 4997 4998 static int con_font_default(struct vc_data *vc, struct console_font_op *op) 4999 { 5000 struct console_font font = {.width = op->width, .height = op->height}; 5001 char name[MAX_FONT_NAME]; 5002 char *s = name; 5003 5004 if (!op->data) 5005 s = NULL; 5006 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0) 5007 return -EFAULT; 5008 else 5009 name[MAX_FONT_NAME - 1] = 0; 5010 5011 scoped_guard(console_lock) { 5012 if (vc->vc_mode != KD_TEXT) 5013 return -EINVAL; 5014 if (!vc->vc_sw->con_font_default) 5015 return -ENOSYS; 5016 5017 /* hide selection and cursor prior font changes */ 5018 hide_cursor(vc); 5019 5020 int ret = vc->vc_sw->con_font_default(vc, &font, s); 5021 if (ret) 5022 return ret; 5023 } 5024 5025 op->width = font.width; 5026 op->height = font.height; 5027 5028 return 0; 5029 } 5030 5031 int con_font_op(struct vc_data *vc, struct console_font_op *op) 5032 { 5033 switch (op->op) { 5034 case KD_FONT_OP_SET: 5035 case KD_FONT_OP_SET_TALL: 5036 return con_font_set(vc, op); 5037 case KD_FONT_OP_GET: 5038 case KD_FONT_OP_GET_TALL: 5039 return con_font_get(vc, op); 5040 case KD_FONT_OP_SET_DEFAULT: 5041 return con_font_default(vc, op); 5042 case KD_FONT_OP_COPY: 5043 /* was buggy and never really used */ 5044 return -EINVAL; 5045 } 5046 return -ENOSYS; 5047 } 5048 5049 /* 5050 * Interface exported to selection and vcs. 5051 */ 5052 5053 /* used by selection */ 5054 u16 screen_glyph(const struct vc_data *vc, int offset) 5055 { 5056 u16 w = scr_readw(screenpos(vc, offset, true)); 5057 u16 c = w & 0xff; 5058 5059 if (w & vc->vc_hi_font_mask) 5060 c |= 0x100; 5061 return c; 5062 } 5063 EXPORT_SYMBOL_GPL(screen_glyph); 5064 5065 u32 screen_glyph_unicode(const struct vc_data *vc, int n) 5066 { 5067 u32 **uni_lines = vc->vc_uni_lines; 5068 5069 if (uni_lines) 5070 return uni_lines[n / vc->vc_cols][n % vc->vc_cols]; 5071 5072 return inverse_translate(vc, screen_glyph(vc, n * 2), true); 5073 } 5074 EXPORT_SYMBOL_GPL(screen_glyph_unicode); 5075 5076 /* used by vcs - note the word offset */ 5077 unsigned short *screen_pos(const struct vc_data *vc, int w_offset, bool viewed) 5078 { 5079 return screenpos(vc, 2 * w_offset, viewed); 5080 } 5081 EXPORT_SYMBOL_GPL(screen_pos); 5082 5083 void getconsxy(const struct vc_data *vc, unsigned char xy[static 2]) 5084 { 5085 /* clamp values if they don't fit */ 5086 xy[0] = min(vc->state.x, 0xFFu); 5087 xy[1] = min(vc->state.y, 0xFFu); 5088 } 5089 5090 void putconsxy(struct vc_data *vc, unsigned char xy[static const 2]) 5091 { 5092 hide_cursor(vc); 5093 gotoxy(vc, xy[0], xy[1]); 5094 set_cursor(vc); 5095 } 5096 5097 u16 vcs_scr_readw(const struct vc_data *vc, const u16 *org) 5098 { 5099 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1) 5100 return softcursor_original; 5101 return scr_readw(org); 5102 } 5103 5104 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org) 5105 { 5106 scr_writew(val, org); 5107 if ((unsigned long)org == vc->vc_pos) { 5108 softcursor_original = -1; 5109 add_softcursor(vc); 5110 } 5111 } 5112 5113 void vcs_scr_updated(struct vc_data *vc) 5114 { 5115 notify_update(vc); 5116 } 5117