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