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 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2729 | /* acquires console_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 2731 | { |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2732 | struct vc_draw_region draw = { |
| 2733 | .x = -1, |
| 2734 | }; |
Jiri Slaby | 754bda9 | 2020-06-15 09:48:50 +0200 | [diff] [blame^] | 2735 | int c, next_c, tc, n = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | unsigned int currcons; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | struct vc_data *vc; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2738 | unsigned char vc_attr; |
Karl Dahlke | 0341a4d | 2008-04-28 02:14:25 -0700 | [diff] [blame] | 2739 | struct vt_notifier_param param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | u16 himask, charmask; |
Jiri Slaby | da823b2 | 2020-06-15 09:48:43 +0200 | [diff] [blame] | 2741 | u8 width; |
| 2742 | bool rescan; |
| 2743 | bool inverse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | |
| 2745 | if (in_interrupt()) |
| 2746 | return count; |
| 2747 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2748 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | vc = tty->driver_data; |
| 2750 | if (vc == NULL) { |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 2751 | pr_err("vt: argh, driver_data is NULL !\n"); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2752 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | return 0; |
| 2754 | } |
| 2755 | |
| 2756 | currcons = vc->vc_num; |
| 2757 | if (!vc_cons_allocated(currcons)) { |
Mandeep Singh Baines | 1ffdda9 | 2011-02-06 09:31:53 -0800 | [diff] [blame] | 2758 | /* could this happen? */ |
| 2759 | pr_warn_once("con_write: tty %d not allocated\n", currcons+1); |
| 2760 | console_unlock(); |
| 2761 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | himask = vc->vc_hi_font_mask; |
| 2765 | charmask = himask ? 0x1ff : 0xff; |
| 2766 | |
| 2767 | /* undraw cursor first */ |
Jiri Slaby | 6ca8dfd | 2016-06-23 13:34:35 +0200 | [diff] [blame] | 2768 | if (con_is_fg(vc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | hide_cursor(vc); |
| 2770 | |
Karl Dahlke | 0341a4d | 2008-04-28 02:14:25 -0700 | [diff] [blame] | 2771 | param.vc = vc; |
| 2772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | while (!tty->stopped && count) { |
| 2774 | int orig = *buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | buf++; |
| 2776 | n++; |
| 2777 | count--; |
Jiri Slaby | 0d1ffef | 2020-06-15 09:48:47 +0200 | [diff] [blame] | 2778 | rescan_last_byte: |
| 2779 | c = orig; |
Jiri Slaby | da823b2 | 2020-06-15 09:48:43 +0200 | [diff] [blame] | 2780 | rescan = false; |
| 2781 | inverse = false; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2782 | width = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | |
Jiri Slaby | a423634 | 2020-06-15 09:48:48 +0200 | [diff] [blame] | 2784 | tc = vc_translate(vc, &c, &rescan); |
| 2785 | if (tc == -1) |
| 2786 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | |
Karl Dahlke | 0341a4d | 2008-04-28 02:14:25 -0700 | [diff] [blame] | 2788 | param.c = tc; |
| 2789 | if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE, |
| 2790 | ¶m) == NOTIFY_STOP) |
| 2791 | continue; |
| 2792 | |
Jiri Slaby | 754bda9 | 2020-06-15 09:48:50 +0200 | [diff] [blame^] | 2793 | if (!vc_is_control(vc, tc, c)) { |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2794 | if (vc->vc_utf && !vc->vc_disp_ctrl) { |
| 2795 | if (is_double_width(c)) |
| 2796 | width = 2; |
| 2797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | /* Now try to find out how to display it */ |
| 2799 | tc = conv_uni_to_pc(vc, tc); |
Adam Tlalka | d4328b4 | 2006-09-29 01:59:53 -0700 | [diff] [blame] | 2800 | if (tc & ~charmask) { |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2801 | if (tc == -1 || tc == -2) { |
| 2802 | continue; /* nothing to display */ |
| 2803 | } |
| 2804 | /* Glyph not found */ |
Samuel Thibault | c0b7988 | 2009-04-18 22:17:17 +0200 | [diff] [blame] | 2805 | if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) { |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2806 | /* In legacy mode use the glyph we get by a 1:1 mapping. |
Egmont Koblinger | 1ed8a2b | 2007-06-23 17:16:27 -0700 | [diff] [blame] | 2807 | This would make absolutely no sense with Unicode in mind, |
| 2808 | but do this for ASCII characters since a font may lack |
| 2809 | Unicode mapping info and we don't want to end up with |
| 2810 | having question marks only. */ |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2811 | tc = c; |
| 2812 | } else { |
| 2813 | /* Display U+FFFD. If it's not found, display an inverse question mark. */ |
| 2814 | tc = conv_uni_to_pc(vc, 0xfffd); |
| 2815 | if (tc < 0) { |
Jiri Slaby | da823b2 | 2020-06-15 09:48:43 +0200 | [diff] [blame] | 2816 | inverse = true; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2817 | tc = conv_uni_to_pc(vc, '?'); |
| 2818 | if (tc < 0) tc = '?'; |
| 2819 | } |
| 2820 | } |
Adam Tlalka | d4328b4 | 2006-09-29 01:59:53 -0700 | [diff] [blame] | 2821 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2822 | |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2823 | if (!inverse) { |
| 2824 | vc_attr = vc->vc_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | } else { |
Jiri Slaby | 881c878 | 2020-06-15 09:48:46 +0200 | [diff] [blame] | 2826 | vc_attr = vc_invert_attr(vc); |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2827 | con_flush(vc, &draw); |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2828 | } |
| 2829 | |
Nicolas Pitre | d8ae724 | 2018-06-26 23:56:40 -0400 | [diff] [blame] | 2830 | next_c = c; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2831 | while (1) { |
| 2832 | if (vc->vc_need_wrap || vc->vc_decim) |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2833 | con_flush(vc, &draw); |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2834 | if (vc->vc_need_wrap) { |
| 2835 | cr(vc); |
| 2836 | lf(vc); |
| 2837 | } |
| 2838 | if (vc->vc_decim) |
| 2839 | insert_char(vc, 1); |
Nicolas Pitre | d8ae724 | 2018-06-26 23:56:40 -0400 | [diff] [blame] | 2840 | vc_uniscr_putc(vc, next_c); |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2841 | scr_writew(himask ? |
| 2842 | ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) : |
| 2843 | (vc_attr << 8) + tc, |
| 2844 | (u16 *) vc->vc_pos); |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2845 | if (con_should_update(vc) && draw.x < 0) { |
| 2846 | draw.x = vc->state.x; |
| 2847 | draw.from = vc->vc_pos; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2848 | } |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 2849 | if (vc->state.x == vc->vc_cols - 1) { |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2850 | vc->vc_need_wrap = vc->vc_decawm; |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2851 | draw.to = vc->vc_pos + 2; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2852 | } else { |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 2853 | vc->state.x++; |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2854 | draw.to = (vc->vc_pos += 2); |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2855 | } |
| 2856 | |
| 2857 | if (!--width) break; |
| 2858 | |
| 2859 | tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */ |
| 2860 | if (tc < 0) tc = ' '; |
Nicolas Pitre | d8ae724 | 2018-06-26 23:56:40 -0400 | [diff] [blame] | 2861 | next_c = ' '; |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2862 | } |
Samuel Thibault | b293d75 | 2007-10-18 23:39:17 -0700 | [diff] [blame] | 2863 | notify_write(vc, c); |
Egmont Koblinger | 2f1a2cc | 2007-05-08 00:30:37 -0700 | [diff] [blame] | 2864 | |
Jiri Slaby | d711ea8 | 2016-06-23 13:34:30 +0200 | [diff] [blame] | 2865 | if (inverse) |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2866 | con_flush(vc, &draw); |
Egmont Koblinger | 1ed8a2b | 2007-06-23 17:16:27 -0700 | [diff] [blame] | 2867 | |
Jiri Slaby | 0d1ffef | 2020-06-15 09:48:47 +0200 | [diff] [blame] | 2868 | if (rescan) |
Adam Tlalka | d4328b4 | 2006-09-29 01:59:53 -0700 | [diff] [blame] | 2869 | goto rescan_last_byte; |
Jiri Slaby | 0d1ffef | 2020-06-15 09:48:47 +0200 | [diff] [blame] | 2870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | continue; |
| 2872 | } |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2873 | con_flush(vc, &draw); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | do_con_trol(tty, vc, orig); |
| 2875 | } |
Jiri Slaby | 917ae1a | 2020-06-15 09:48:49 +0200 | [diff] [blame] | 2876 | con_flush(vc, &draw); |
Nicolas Pitre | 0224080 | 2018-07-17 21:02:41 -0400 | [diff] [blame] | 2877 | vc_uniscr_debug_check(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | console_conditional_schedule(); |
Samuel Thibault | b293d75 | 2007-10-18 23:39:17 -0700 | [diff] [blame] | 2879 | notify_update(vc); |
Nicolas Pitre | 7e1d226 | 2019-01-08 22:55:00 -0500 | [diff] [blame] | 2880 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2881 | return n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | /* |
| 2885 | * This is the console switching callback. |
| 2886 | * |
| 2887 | * Doing console switching in a process context allows |
| 2888 | * us to do the switches asynchronously (needed when we want |
| 2889 | * to switch due to a keyboard interrupt). Synchronization |
| 2890 | * with other console code and prevention of re-entrancy is |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2891 | * ensured with console_lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | */ |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 2893 | static void console_callback(struct work_struct *ignored) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | { |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2895 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | |
| 2897 | if (want_console >= 0) { |
| 2898 | if (want_console != fg_console && |
| 2899 | vc_cons_allocated(want_console)) { |
| 2900 | hide_cursor(vc_cons[fg_console].d); |
| 2901 | change_console(vc_cons[want_console].d); |
| 2902 | /* we only changed when the console had already |
| 2903 | been allocated - a new console is not created |
| 2904 | in an interrupt routine */ |
| 2905 | } |
| 2906 | want_console = -1; |
| 2907 | } |
| 2908 | if (do_poke_blanked_console) { /* do not unblank for a LED change */ |
| 2909 | do_poke_blanked_console = 0; |
| 2910 | poke_blanked_console(); |
| 2911 | } |
| 2912 | if (scrollback_delta) { |
| 2913 | struct vc_data *vc = vc_cons[fg_console].d; |
| 2914 | clear_selection(); |
Jiri Slaby | 97293de | 2016-06-23 13:34:26 +0200 | [diff] [blame] | 2915 | if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | vc->vc_sw->con_scrolldelta(vc, scrollback_delta); |
| 2917 | scrollback_delta = 0; |
| 2918 | } |
| 2919 | if (blank_timer_expired) { |
| 2920 | do_blank_screen(0); |
| 2921 | blank_timer_expired = 0; |
| 2922 | } |
Samuel Thibault | b293d75 | 2007-10-18 23:39:17 -0700 | [diff] [blame] | 2923 | notify_update(vc_cons[fg_console].d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 2925 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2926 | } |
| 2927 | |
Andrew Johnson | b257bc0 | 2007-03-16 13:38:24 -0800 | [diff] [blame] | 2928 | int set_console(int nr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2929 | { |
Andrew Johnson | b257bc0 | 2007-03-16 13:38:24 -0800 | [diff] [blame] | 2930 | struct vc_data *vc = vc_cons[fg_console].d; |
| 2931 | |
| 2932 | if (!vc_cons_allocated(nr) || vt_dont_switch || |
| 2933 | (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) { |
| 2934 | |
| 2935 | /* |
| 2936 | * Console switch will fail in console_callback() or |
| 2937 | * change_console() so there is no point scheduling |
| 2938 | * the callback |
| 2939 | * |
| 2940 | * Existing set_console() users don't check the return |
| 2941 | * value so this shouldn't break anything |
| 2942 | */ |
| 2943 | return -EINVAL; |
| 2944 | } |
| 2945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | want_console = nr; |
| 2947 | schedule_console_callback(); |
Andrew Johnson | b257bc0 | 2007-03-16 13:38:24 -0800 | [diff] [blame] | 2948 | |
| 2949 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | } |
| 2951 | |
| 2952 | struct tty_driver *console_driver; |
| 2953 | |
| 2954 | #ifdef CONFIG_VT_CONSOLE |
| 2955 | |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 2956 | /** |
| 2957 | * vt_kmsg_redirect() - Sets/gets the kernel message console |
| 2958 | * @new: The new virtual terminal number or -1 if the console should stay |
| 2959 | * unchanged |
| 2960 | * |
| 2961 | * By default, the kernel messages are always printed on the current virtual |
| 2962 | * console. However, the user may modify that default with the |
| 2963 | * TIOCL_SETKMSGREDIRECT ioctl call. |
| 2964 | * |
| 2965 | * This function sets the kernel message console to be @new. It returns the old |
| 2966 | * virtual console number. The virtual terminal number 0 (both as parameter and |
| 2967 | * return value) means no redirection (i.e. always printed on the currently |
| 2968 | * active console). |
| 2969 | * |
| 2970 | * The parameter -1 means that only the current console is returned, but the |
| 2971 | * value is not modified. You may use the macro vt_get_kmsg_redirect() in that |
| 2972 | * case to make the code more understandable. |
| 2973 | * |
| 2974 | * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores |
| 2975 | * the parameter and always returns 0. |
| 2976 | */ |
| 2977 | int vt_kmsg_redirect(int new) |
| 2978 | { |
| 2979 | static int kmsg_con; |
| 2980 | |
| 2981 | if (new != -1) |
| 2982 | return xchg(&kmsg_con, new); |
| 2983 | else |
| 2984 | return kmsg_con; |
| 2985 | } |
| 2986 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2987 | /* |
| 2988 | * Console on virtual terminal |
| 2989 | * |
| 2990 | * The console must be locked when we get here. |
| 2991 | */ |
| 2992 | |
| 2993 | static void vt_console_print(struct console *co, const char *b, unsigned count) |
| 2994 | { |
| 2995 | struct vc_data *vc = vc_cons[fg_console].d; |
| 2996 | unsigned char c; |
Nick Piggin | b094000 | 2008-02-06 01:37:04 -0800 | [diff] [blame] | 2997 | static DEFINE_SPINLOCK(printing_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2998 | const ushort *start; |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 2999 | ushort start_x, cnt; |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 3000 | int kmsg_console; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | |
| 3002 | /* console busy or not yet initialized */ |
Nick Piggin | b094000 | 2008-02-06 01:37:04 -0800 | [diff] [blame] | 3003 | if (!printable) |
| 3004 | return; |
| 3005 | if (!spin_trylock(&printing_lock)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | return; |
| 3007 | |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 3008 | kmsg_console = vt_get_kmsg_redirect(); |
| 3009 | if (kmsg_console && vc_cons_allocated(kmsg_console - 1)) |
| 3010 | vc = vc_cons[kmsg_console - 1].d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3011 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | if (!vc_cons_allocated(fg_console)) { |
| 3013 | /* impossible */ |
| 3014 | /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */ |
| 3015 | goto quit; |
| 3016 | } |
| 3017 | |
Daniel Vetter | 8d7fc29 | 2018-08-22 10:54:03 +0200 | [diff] [blame] | 3018 | if (vc->vc_mode != KD_TEXT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | goto quit; |
| 3020 | |
| 3021 | /* undraw cursor first */ |
Jiri Slaby | 6ca8dfd | 2016-06-23 13:34:35 +0200 | [diff] [blame] | 3022 | if (con_is_fg(vc)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | hide_cursor(vc); |
| 3024 | |
| 3025 | start = (ushort *)vc->vc_pos; |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3026 | start_x = vc->state.x; |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 3027 | cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | while (count--) { |
| 3029 | c = *b++; |
| 3030 | if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) { |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 3031 | if (cnt && con_is_visible(vc)) |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3032 | 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] | 3033 | cnt = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3034 | if (c == 8) { /* backspace */ |
| 3035 | bs(vc); |
| 3036 | start = (ushort *)vc->vc_pos; |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3037 | start_x = vc->state.x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3038 | continue; |
| 3039 | } |
| 3040 | if (c != 13) |
| 3041 | lf(vc); |
| 3042 | cr(vc); |
| 3043 | start = (ushort *)vc->vc_pos; |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3044 | start_x = vc->state.x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3045 | if (c == 10 || c == 13) |
| 3046 | continue; |
| 3047 | } |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 3048 | vc_uniscr_putc(vc, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos); |
Samuel Thibault | b293d75 | 2007-10-18 23:39:17 -0700 | [diff] [blame] | 3050 | notify_write(vc, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | cnt++; |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3052 | if (vc->state.x == vc->vc_cols - 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3053 | vc->vc_need_wrap = 1; |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 3054 | } else { |
| 3055 | vc->vc_pos += 2; |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3056 | vc->state.x++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
Nicolas Pitre | 6609cff | 2019-01-08 22:54:59 -0500 | [diff] [blame] | 3059 | if (cnt && con_is_visible(vc)) |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3060 | 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] | 3061 | set_cursor(vc); |
Samuel Thibault | b293d75 | 2007-10-18 23:39:17 -0700 | [diff] [blame] | 3062 | notify_update(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3063 | |
| 3064 | quit: |
Nick Piggin | b094000 | 2008-02-06 01:37:04 -0800 | [diff] [blame] | 3065 | spin_unlock(&printing_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | } |
| 3067 | |
| 3068 | static struct tty_driver *vt_console_device(struct console *c, int *index) |
| 3069 | { |
| 3070 | *index = c->index ? c->index-1 : fg_console; |
| 3071 | return console_driver; |
| 3072 | } |
| 3073 | |
| 3074 | static struct console vt_console_driver = { |
| 3075 | .name = "tty", |
| 3076 | .write = vt_console_print, |
| 3077 | .device = vt_console_device, |
| 3078 | .unblank = unblank_screen, |
| 3079 | .flags = CON_PRINTBUFFER, |
| 3080 | .index = -1, |
| 3081 | }; |
| 3082 | #endif |
| 3083 | |
| 3084 | /* |
| 3085 | * Handling of Linux-specific VC ioctls |
| 3086 | */ |
| 3087 | |
| 3088 | /* |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3089 | * Generally a bit racy with respect to console_lock();. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | * |
| 3091 | * There are some functions which don't need it. |
| 3092 | * |
| 3093 | * There are some functions which can sleep for arbitrary periods |
| 3094 | * (paste_selection) but we don't need the lock there anyway. |
| 3095 | * |
Okash Khawaja | 496124e | 2019-04-17 13:21:13 +0100 | [diff] [blame] | 3096 | * set_selection_user has locking, and definitely needs it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | */ |
| 3098 | |
| 3099 | int tioclinux(struct tty_struct *tty, unsigned long arg) |
| 3100 | { |
| 3101 | char type, data; |
| 3102 | char __user *p = (char __user *)arg; |
| 3103 | int lines; |
| 3104 | int ret; |
| 3105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN)) |
| 3107 | return -EPERM; |
| 3108 | if (get_user(type, p)) |
| 3109 | return -EFAULT; |
| 3110 | ret = 0; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3112 | switch (type) |
| 3113 | { |
| 3114 | case TIOCL_SETSEL: |
Okash Khawaja | 496124e | 2019-04-17 13:21:13 +0100 | [diff] [blame] | 3115 | ret = set_selection_user((struct tiocl_selection |
| 3116 | __user *)(p+1), tty); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3117 | break; |
| 3118 | case TIOCL_PASTESEL: |
| 3119 | ret = paste_selection(tty); |
| 3120 | break; |
| 3121 | case TIOCL_UNBLANKSCREEN: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3122 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | unblank_screen(); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3124 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | break; |
| 3126 | case TIOCL_SELLOADLUT: |
Alan Cox | 5289475 | 2012-03-02 15:00:02 +0000 | [diff] [blame] | 3127 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | ret = sel_loadlut(p); |
Alan Cox | 5289475 | 2012-03-02 15:00:02 +0000 | [diff] [blame] | 3129 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3130 | break; |
| 3131 | case TIOCL_GETSHIFTSTATE: |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 3132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | /* |
| 3134 | * Make it possible to react to Shift+Mousebutton. |
| 3135 | * Note that 'shift_state' is an undocumented |
| 3136 | * kernel-internal variable; programs not closely |
| 3137 | * related to the kernel should not use this. |
| 3138 | */ |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 3139 | data = vt_get_shift_state(); |
Adam Borowski | 6987dc8 | 2017-06-03 09:35:06 +0200 | [diff] [blame] | 3140 | ret = put_user(data, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3141 | break; |
| 3142 | case TIOCL_GETMOUSEREPORTING: |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3143 | console_lock(); /* May be overkill */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | data = mouse_reporting(); |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3145 | console_unlock(); |
Adam Borowski | 6987dc8 | 2017-06-03 09:35:06 +0200 | [diff] [blame] | 3146 | ret = put_user(data, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | break; |
| 3148 | case TIOCL_SETVESABLANK: |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3149 | console_lock(); |
Yoichi Yuasa | 403aac9 | 2006-12-06 20:38:38 -0800 | [diff] [blame] | 3150 | ret = set_vesa_blanking(p); |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3151 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3152 | break; |
Rafael J. Wysocki | 0ca0773 | 2006-03-31 02:30:58 -0800 | [diff] [blame] | 3153 | case TIOCL_GETKMSGREDIRECT: |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 3154 | data = vt_get_kmsg_redirect(); |
Adam Borowski | 6987dc8 | 2017-06-03 09:35:06 +0200 | [diff] [blame] | 3155 | ret = put_user(data, p); |
Rafael J. Wysocki | 0ca0773 | 2006-03-31 02:30:58 -0800 | [diff] [blame] | 3156 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3157 | case TIOCL_SETKMSGREDIRECT: |
| 3158 | if (!capable(CAP_SYS_ADMIN)) { |
| 3159 | ret = -EPERM; |
| 3160 | } else { |
| 3161 | if (get_user(data, p+1)) |
| 3162 | ret = -EFAULT; |
| 3163 | else |
Bernhard Walle | 5ada918 | 2009-12-14 18:00:43 -0800 | [diff] [blame] | 3164 | vt_kmsg_redirect(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3165 | } |
| 3166 | break; |
| 3167 | case TIOCL_GETFGCONSOLE: |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3168 | /* No locking needed as this is a transiently |
| 3169 | correct return anyway if the caller hasn't |
| 3170 | disabled switching */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | ret = fg_console; |
| 3172 | break; |
| 3173 | case TIOCL_SCROLLCONSOLE: |
| 3174 | if (get_user(lines, (s32 __user *)(p+4))) { |
| 3175 | ret = -EFAULT; |
| 3176 | } else { |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3177 | /* Need the console lock here. Note that lots |
| 3178 | of other calls need fixing before the lock |
| 3179 | is actually useful ! */ |
| 3180 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | scrollfront(vc_cons[fg_console].d, lines); |
Alan Cox | 20f6257 | 2012-03-02 14:59:37 +0000 | [diff] [blame] | 3182 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | ret = 0; |
| 3184 | } |
| 3185 | break; |
| 3186 | case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3187 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3188 | ignore_poke = 1; |
| 3189 | do_blank_screen(0); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3190 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | break; |
| 3192 | case TIOCL_BLANKEDSCREEN: |
| 3193 | ret = console_blanked; |
| 3194 | break; |
| 3195 | default: |
| 3196 | ret = -EINVAL; |
| 3197 | break; |
| 3198 | } |
| 3199 | return ret; |
| 3200 | } |
| 3201 | |
| 3202 | /* |
| 3203 | * /dev/ttyN handling |
| 3204 | */ |
| 3205 | |
| 3206 | static int con_write(struct tty_struct *tty, const unsigned char *buf, int count) |
| 3207 | { |
| 3208 | int retval; |
| 3209 | |
| 3210 | retval = do_con_write(tty, buf, count); |
| 3211 | con_flush_chars(tty); |
| 3212 | |
| 3213 | return retval; |
| 3214 | } |
| 3215 | |
Alan Cox | 5d19f54 | 2008-04-30 00:54:08 -0700 | [diff] [blame] | 3216 | static int con_put_char(struct tty_struct *tty, unsigned char ch) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | { |
| 3218 | if (in_interrupt()) |
Alan Cox | 5d19f54 | 2008-04-30 00:54:08 -0700 | [diff] [blame] | 3219 | return 0; /* n_r3964 calls put_char() from interrupt context */ |
| 3220 | return do_con_write(tty, &ch, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | static int con_write_room(struct tty_struct *tty) |
| 3224 | { |
| 3225 | if (tty->stopped) |
| 3226 | return 0; |
Joe Peterson | a88a69c | 2009-01-02 13:40:53 +0000 | [diff] [blame] | 3227 | return 32768; /* No limit, really; we're not buffering */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | static int con_chars_in_buffer(struct tty_struct *tty) |
| 3231 | { |
| 3232 | return 0; /* we're not buffering */ |
| 3233 | } |
| 3234 | |
| 3235 | /* |
| 3236 | * con_throttle and con_unthrottle are only used for |
| 3237 | * paste_selection(), which has to stuff in a large number of |
| 3238 | * characters... |
| 3239 | */ |
| 3240 | static void con_throttle(struct tty_struct *tty) |
| 3241 | { |
| 3242 | } |
| 3243 | |
| 3244 | static void con_unthrottle(struct tty_struct *tty) |
| 3245 | { |
| 3246 | struct vc_data *vc = tty->driver_data; |
| 3247 | |
| 3248 | wake_up_interruptible(&vc->paste_wait); |
| 3249 | } |
| 3250 | |
| 3251 | /* |
| 3252 | * Turn the Scroll-Lock LED on when the tty is stopped |
| 3253 | */ |
| 3254 | static void con_stop(struct tty_struct *tty) |
| 3255 | { |
| 3256 | int console_num; |
| 3257 | if (!tty) |
| 3258 | return; |
| 3259 | console_num = tty->index; |
| 3260 | if (!vc_cons_allocated(console_num)) |
| 3261 | return; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 3262 | vt_kbd_con_stop(console_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | /* |
| 3266 | * Turn the Scroll-Lock LED off when the console is started |
| 3267 | */ |
| 3268 | static void con_start(struct tty_struct *tty) |
| 3269 | { |
| 3270 | int console_num; |
| 3271 | if (!tty) |
| 3272 | return; |
| 3273 | console_num = tty->index; |
| 3274 | if (!vc_cons_allocated(console_num)) |
| 3275 | return; |
Alan Cox | 079c953 | 2012-02-28 14:49:23 +0000 | [diff] [blame] | 3276 | vt_kbd_con_start(console_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | static void con_flush_chars(struct tty_struct *tty) |
| 3280 | { |
| 3281 | struct vc_data *vc; |
| 3282 | |
| 3283 | if (in_interrupt()) /* from flush_to_ldisc */ |
| 3284 | return; |
| 3285 | |
| 3286 | /* if we race with con_close(), vt may be null */ |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3287 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | vc = tty->driver_data; |
| 3289 | if (vc) |
| 3290 | set_cursor(vc); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3291 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | /* |
| 3295 | * Allocate the console screen memory. |
| 3296 | */ |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3297 | static int con_install(struct tty_driver *driver, struct tty_struct *tty) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | { |
| 3299 | unsigned int currcons = tty->index; |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3300 | struct vc_data *vc; |
| 3301 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3302 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3303 | console_lock(); |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3304 | ret = vc_allocate(currcons); |
| 3305 | if (ret) |
| 3306 | goto unlock; |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 3307 | |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3308 | vc = vc_cons[currcons].d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3310 | /* Still being freed */ |
| 3311 | if (vc->port.tty) { |
| 3312 | ret = -ERESTARTSYS; |
| 3313 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | } |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3315 | |
| 3316 | ret = tty_port_install(&vc->port, driver, tty); |
| 3317 | if (ret) |
| 3318 | goto unlock; |
| 3319 | |
| 3320 | tty->driver_data = vc; |
| 3321 | vc->port.tty = tty; |
Eric Biggers | ca4463b | 2020-03-21 20:43:04 -0700 | [diff] [blame] | 3322 | tty_port_get(&vc->port); |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3323 | |
| 3324 | if (!tty->winsize.ws_row && !tty->winsize.ws_col) { |
| 3325 | tty->winsize.ws_row = vc_cons[currcons].d->vc_rows; |
| 3326 | tty->winsize.ws_col = vc_cons[currcons].d->vc_cols; |
| 3327 | } |
| 3328 | if (vc->vc_utf) |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 3329 | tty->termios.c_iflag |= IUTF8; |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3330 | else |
Alan Cox | adc8d74 | 2012-07-14 15:31:47 +0100 | [diff] [blame] | 3331 | tty->termios.c_iflag &= ~IUTF8; |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3332 | unlock: |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3333 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3334 | return ret; |
| 3335 | } |
| 3336 | |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3337 | static int con_open(struct tty_struct *tty, struct file *filp) |
| 3338 | { |
| 3339 | /* everything done in install */ |
| 3340 | return 0; |
| 3341 | } |
| 3342 | |
| 3343 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | static void con_close(struct tty_struct *tty, struct file *filp) |
| 3345 | { |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 3346 | /* Nothing to do - we defer to shutdown */ |
| 3347 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3348 | |
Alan Cox | feebed6 | 2008-10-13 10:41:30 +0100 | [diff] [blame] | 3349 | static void con_shutdown(struct tty_struct *tty) |
| 3350 | { |
| 3351 | struct vc_data *vc = tty->driver_data; |
| 3352 | BUG_ON(vc == NULL); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3353 | console_lock(); |
Alan Cox | 8ce7326 | 2010-06-01 22:52:56 +0200 | [diff] [blame] | 3354 | vc->port.tty = NULL; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3355 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Eric Biggers | ca4463b | 2020-03-21 20:43:04 -0700 | [diff] [blame] | 3358 | static void con_cleanup(struct tty_struct *tty) |
| 3359 | { |
| 3360 | struct vc_data *vc = tty->driver_data; |
| 3361 | |
| 3362 | tty_port_put(&vc->port); |
| 3363 | } |
| 3364 | |
Clemens Ladisch | 3855ae1 | 2013-08-04 13:09:50 +0200 | [diff] [blame] | 3365 | static int default_color = 7; /* white */ |
Jan Engelhardt | fa6ce9a | 2007-05-08 00:38:04 -0700 | [diff] [blame] | 3366 | static int default_italic_color = 2; // green (ASCII) |
| 3367 | static int default_underline_color = 3; // cyan (ASCII) |
Clemens Ladisch | 3855ae1 | 2013-08-04 13:09:50 +0200 | [diff] [blame] | 3368 | module_param_named(color, default_color, int, S_IRUGO | S_IWUSR); |
Jan Engelhardt | fa6ce9a | 2007-05-08 00:38:04 -0700 | [diff] [blame] | 3369 | module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR); |
| 3370 | module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR); |
| 3371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3372 | static void vc_init(struct vc_data *vc, unsigned int rows, |
| 3373 | unsigned int cols, int do_clear) |
| 3374 | { |
| 3375 | int j, k ; |
| 3376 | |
| 3377 | vc->vc_cols = cols; |
| 3378 | vc->vc_rows = rows; |
| 3379 | vc->vc_size_row = cols << 1; |
| 3380 | vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row; |
| 3381 | |
| 3382 | set_origin(vc); |
| 3383 | vc->vc_pos = vc->vc_origin; |
| 3384 | reset_vc(vc); |
| 3385 | for (j=k=0; j<16; j++) { |
| 3386 | vc->vc_palette[k++] = default_red[j] ; |
| 3387 | vc->vc_palette[k++] = default_grn[j] ; |
| 3388 | vc->vc_palette[k++] = default_blu[j] ; |
| 3389 | } |
Clemens Ladisch | 3855ae1 | 2013-08-04 13:09:50 +0200 | [diff] [blame] | 3390 | vc->vc_def_color = default_color; |
Jan Engelhardt | fa6ce9a | 2007-05-08 00:38:04 -0700 | [diff] [blame] | 3391 | vc->vc_ulcolor = default_underline_color; |
| 3392 | vc->vc_itcolor = default_italic_color; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3393 | vc->vc_halfcolor = 0x08; /* grey */ |
| 3394 | init_waitqueue_head(&vc->paste_wait); |
| 3395 | reset_terminal(vc, do_clear); |
| 3396 | } |
| 3397 | |
| 3398 | /* |
| 3399 | * This routine initializes console interrupts, and does nothing |
| 3400 | * else. If you want the screen to clear, call tty_write with |
| 3401 | * the appropriate escape-sequence. |
| 3402 | */ |
| 3403 | |
| 3404 | static int __init con_init(void) |
| 3405 | { |
| 3406 | const char *display_desc = NULL; |
| 3407 | struct vc_data *vc; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3408 | unsigned int currcons = 0, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3410 | console_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | |
Arvind Sankar | 805ece2 | 2019-12-18 16:44:44 -0500 | [diff] [blame] | 3412 | if (!conswitchp) |
| 3413 | conswitchp = &dummy_con; |
| 3414 | display_desc = conswitchp->con_startup(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | if (!display_desc) { |
| 3416 | fg_console = 0; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3417 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | return 0; |
| 3419 | } |
| 3420 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3421 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 3422 | struct con_driver *con_driver = ®istered_con_driver[i]; |
| 3423 | |
| 3424 | if (con_driver->con == NULL) { |
| 3425 | con_driver->con = conswitchp; |
| 3426 | con_driver->desc = display_desc; |
| 3427 | con_driver->flag = CON_DRIVER_FLAG_INIT; |
| 3428 | con_driver->first = 0; |
| 3429 | con_driver->last = MAX_NR_CONSOLES - 1; |
| 3430 | break; |
| 3431 | } |
| 3432 | } |
| 3433 | |
| 3434 | for (i = 0; i < MAX_NR_CONSOLES; i++) |
| 3435 | con_driver_map[i] = conswitchp; |
| 3436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | if (blankinterval) { |
| 3438 | blank_state = blank_normal_wait; |
Daniel Mack | f324edc | 2009-06-16 15:33:52 -0700 | [diff] [blame] | 3439 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3440 | } |
| 3441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) { |
Pekka Enberg | a5f4f52 | 2009-06-10 23:53:37 +0300 | [diff] [blame] | 3443 | 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] | 3444 | INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK); |
Alan Cox | ff917ba | 2010-06-01 22:52:55 +0200 | [diff] [blame] | 3445 | tty_port_init(&vc->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | visual_init(vc, currcons, 1); |
Pekka Enberg | a5f4f52 | 2009-06-10 23:53:37 +0300 | [diff] [blame] | 3447 | vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3448 | vc_init(vc, vc->vc_rows, vc->vc_cols, |
| 3449 | currcons || !vc->vc_sw->con_save_screen); |
| 3450 | } |
| 3451 | currcons = fg_console = 0; |
| 3452 | master_display_fg = vc = vc_cons[currcons].d; |
| 3453 | set_origin(vc); |
| 3454 | save_screen(vc); |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 3455 | gotoxy(vc, vc->state.x, vc->state.y); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | csi_J(vc, 0); |
| 3457 | update_screen(vc); |
Kay Sievers | 5da527a | 2012-04-03 03:18:23 +0200 | [diff] [blame] | 3458 | pr_info("Console: %s %s %dx%d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3459 | vc->vc_can_do_color ? "colour" : "mono", |
| 3460 | display_desc, vc->vc_cols, vc->vc_rows); |
| 3461 | printable = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3462 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 3463 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3464 | |
| 3465 | #ifdef CONFIG_VT_CONSOLE |
| 3466 | register_console(&vt_console_driver); |
| 3467 | #endif |
| 3468 | return 0; |
| 3469 | } |
| 3470 | console_initcall(con_init); |
| 3471 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 3472 | static const struct tty_operations con_ops = { |
Jiri Slaby | bc1e99d | 2012-06-04 13:35:33 +0200 | [diff] [blame] | 3473 | .install = con_install, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | .open = con_open, |
| 3475 | .close = con_close, |
| 3476 | .write = con_write, |
| 3477 | .write_room = con_write_room, |
| 3478 | .put_char = con_put_char, |
| 3479 | .flush_chars = con_flush_chars, |
| 3480 | .chars_in_buffer = con_chars_in_buffer, |
| 3481 | .ioctl = vt_ioctl, |
Arnd Bergmann | e921665 | 2009-08-06 15:09:28 +0200 | [diff] [blame] | 3482 | #ifdef CONFIG_COMPAT |
| 3483 | .compat_ioctl = vt_compat_ioctl, |
| 3484 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | .stop = con_stop, |
| 3486 | .start = con_start, |
| 3487 | .throttle = con_throttle, |
| 3488 | .unthrottle = con_unthrottle, |
Alan Cox | 8c9a9dd | 2008-08-15 10:39:38 +0100 | [diff] [blame] | 3489 | .resize = vt_resize, |
Eric Biggers | ca4463b | 2020-03-21 20:43:04 -0700 | [diff] [blame] | 3490 | .shutdown = con_shutdown, |
| 3491 | .cleanup = con_cleanup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | }; |
| 3493 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3494 | static struct cdev vc0_cdev; |
| 3495 | |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3496 | static ssize_t show_tty_active(struct device *dev, |
| 3497 | struct device_attribute *attr, char *buf) |
| 3498 | { |
| 3499 | return sprintf(buf, "tty%d\n", fg_console + 1); |
| 3500 | } |
| 3501 | static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL); |
| 3502 | |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3503 | static struct attribute *vt_dev_attrs[] = { |
| 3504 | &dev_attr_active.attr, |
| 3505 | NULL |
| 3506 | }; |
| 3507 | |
| 3508 | ATTRIBUTE_GROUPS(vt_dev); |
| 3509 | |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3510 | int __init vty_init(const struct file_operations *console_fops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | { |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3512 | cdev_init(&vc0_cdev, console_fops); |
| 3513 | if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) || |
| 3514 | register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0) |
| 3515 | panic("Couldn't register /dev/tty0 driver\n"); |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3516 | tty0dev = device_create_with_groups(tty_class, NULL, |
| 3517 | MKDEV(TTY_MAJOR, 0), NULL, |
| 3518 | vt_dev_groups, "tty0"); |
Kay Sievers | fbc92a3 | 2010-12-01 18:51:05 +0100 | [diff] [blame] | 3519 | if (IS_ERR(tty0dev)) |
| 3520 | tty0dev = NULL; |
Alan Cox | d81ed10 | 2008-10-13 10:41:42 +0100 | [diff] [blame] | 3521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3522 | vcs_init(); |
| 3523 | |
| 3524 | console_driver = alloc_tty_driver(MAX_NR_CONSOLES); |
| 3525 | if (!console_driver) |
| 3526 | panic("Couldn't allocate console driver\n"); |
Jiri Slaby | 2f16669 | 2012-03-05 14:51:52 +0100 | [diff] [blame] | 3527 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | console_driver->name = "tty"; |
| 3529 | console_driver->name_base = 1; |
| 3530 | console_driver->major = TTY_MAJOR; |
| 3531 | console_driver->minor_start = 1; |
| 3532 | console_driver->type = TTY_DRIVER_TYPE_CONSOLE; |
| 3533 | console_driver->init_termios = tty_std_termios; |
Samuel Thibault | c1236d3 | 2008-05-06 20:42:37 -0700 | [diff] [blame] | 3534 | if (default_utf8) |
| 3535 | console_driver->init_termios.c_iflag |= IUTF8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS; |
| 3537 | tty_set_operations(console_driver, &con_ops); |
| 3538 | if (tty_register_driver(console_driver)) |
| 3539 | panic("Couldn't register console driver\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | kbd_init(); |
| 3541 | console_map_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3542 | #ifdef CONFIG_MDA_CONSOLE |
| 3543 | mda_console_init(); |
| 3544 | #endif |
| 3545 | return 0; |
| 3546 | } |
| 3547 | |
| 3548 | #ifndef VT_SINGLE_DRIVER |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3549 | |
| 3550 | static struct class *vtconsole_class; |
| 3551 | |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 3552 | 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] | 3553 | int deflt) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3554 | { |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3555 | struct module *owner = csw->owner; |
| 3556 | const char *desc = NULL; |
| 3557 | struct con_driver *con_driver; |
| 3558 | int i, j = -1, k = -1, retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3560 | if (!try_module_get(owner)) |
| 3561 | return -ENODEV; |
| 3562 | |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 3563 | WARN_CONSOLE_UNLOCKED(); |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3564 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3565 | /* check if driver is registered */ |
| 3566 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 3567 | con_driver = ®istered_con_driver[i]; |
| 3568 | |
| 3569 | if (con_driver->con == csw) { |
| 3570 | desc = con_driver->desc; |
| 3571 | retval = 0; |
| 3572 | break; |
| 3573 | } |
| 3574 | } |
| 3575 | |
| 3576 | if (retval) |
| 3577 | goto err; |
| 3578 | |
| 3579 | if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) { |
| 3580 | csw->con_startup(); |
| 3581 | con_driver->flag |= CON_DRIVER_FLAG_INIT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3582 | } |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | if (deflt) { |
| 3585 | if (conswitchp) |
| 3586 | module_put(conswitchp->owner); |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3588 | __module_get(owner); |
| 3589 | conswitchp = csw; |
| 3590 | } |
| 3591 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3592 | first = max(first, con_driver->first); |
| 3593 | last = min(last, con_driver->last); |
| 3594 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3595 | for (i = first; i <= last; i++) { |
| 3596 | int old_was_color; |
| 3597 | struct vc_data *vc = vc_cons[i].d; |
| 3598 | |
| 3599 | if (con_driver_map[i]) |
| 3600 | module_put(con_driver_map[i]->owner); |
| 3601 | __module_get(owner); |
| 3602 | con_driver_map[i] = csw; |
| 3603 | |
| 3604 | if (!vc || !vc->vc_sw) |
| 3605 | continue; |
| 3606 | |
| 3607 | j = i; |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3608 | |
Jiri Slaby | 6ca8dfd | 2016-06-23 13:34:35 +0200 | [diff] [blame] | 3609 | if (con_is_visible(vc)) { |
David Hollister | 4ee1acc | 2006-06-26 00:26:41 -0700 | [diff] [blame] | 3610 | k = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3611 | save_screen(vc); |
David Hollister | 4ee1acc | 2006-06-26 00:26:41 -0700 | [diff] [blame] | 3612 | } |
| 3613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3614 | old_was_color = vc->vc_can_do_color; |
| 3615 | vc->vc_sw->con_deinit(vc); |
Francisco Jerez | 9fc2b2d | 2010-08-22 17:37:24 +0200 | [diff] [blame] | 3616 | vc->vc_origin = (unsigned long)vc->vc_screenbuf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3617 | visual_init(vc, i, 0); |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3618 | set_origin(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3619 | update_attr(vc); |
| 3620 | |
| 3621 | /* If the console changed between mono <-> color, then |
| 3622 | * the attributes in the screenbuf will be wrong. The |
| 3623 | * following resets all attributes to something sane. |
| 3624 | */ |
| 3625 | if (old_was_color != vc->vc_can_do_color) |
| 3626 | clear_buffer_attributes(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3627 | } |
David Hollister | 4ee1acc | 2006-06-26 00:26:41 -0700 | [diff] [blame] | 3628 | |
Mandeep Singh Baines | 1ffdda9 | 2011-02-06 09:31:53 -0800 | [diff] [blame] | 3629 | pr_info("Console: switching "); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3630 | if (!deflt) |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 3631 | pr_cont("consoles %d-%d ", first + 1, last + 1); |
David Hollister | 4ee1acc | 2006-06-26 00:26:41 -0700 | [diff] [blame] | 3632 | if (j >= 0) { |
| 3633 | struct vc_data *vc = vc_cons[j].d; |
| 3634 | |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 3635 | pr_cont("to %s %s %dx%d\n", |
| 3636 | vc->vc_can_do_color ? "colour" : "mono", |
| 3637 | desc, vc->vc_cols, vc->vc_rows); |
David Hollister | 4ee1acc | 2006-06-26 00:26:41 -0700 | [diff] [blame] | 3638 | |
| 3639 | if (k >= 0) { |
| 3640 | vc = vc_cons[k].d; |
| 3641 | update_screen(vc); |
| 3642 | } |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 3643 | } else { |
| 3644 | pr_cont("to %s\n", desc); |
| 3645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3646 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3647 | retval = 0; |
| 3648 | err: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | module_put(owner); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3650 | return retval; |
| 3651 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3652 | |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 3653 | |
Antonino A. Daplas | 13ae664 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3654 | #ifdef CONFIG_VT_HW_CONSOLE_BINDING |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3655 | int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt) |
| 3656 | { |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3657 | struct module *owner = csw->owner; |
| 3658 | const struct consw *defcsw = NULL; |
| 3659 | struct con_driver *con_driver = NULL, *con_back = NULL; |
| 3660 | int i, retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3662 | if (!try_module_get(owner)) |
| 3663 | return -ENODEV; |
| 3664 | |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3665 | WARN_CONSOLE_UNLOCKED(); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3666 | |
| 3667 | /* check if driver is registered and if it is unbindable */ |
| 3668 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 3669 | con_driver = ®istered_con_driver[i]; |
| 3670 | |
| 3671 | if (con_driver->con == csw && |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3672 | con_driver->flag & CON_DRIVER_FLAG_MODULE) { |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3673 | retval = 0; |
| 3674 | break; |
| 3675 | } |
| 3676 | } |
| 3677 | |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3678 | if (retval) |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3679 | goto err; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3680 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3681 | retval = -ENODEV; |
| 3682 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3683 | /* check if backup driver exists */ |
| 3684 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 3685 | con_back = ®istered_con_driver[i]; |
| 3686 | |
Daniel Vetter | 249f7b3 | 2014-06-05 16:24:47 +0200 | [diff] [blame] | 3687 | if (con_back->con && con_back->con != csw) { |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3688 | defcsw = con_back->con; |
| 3689 | retval = 0; |
| 3690 | break; |
| 3691 | } |
| 3692 | } |
| 3693 | |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3694 | if (retval) |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3695 | goto err; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3696 | |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3697 | if (!con_is_bound(csw)) |
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 | |
| 3700 | first = max(first, con_driver->first); |
| 3701 | last = min(last, con_driver->last); |
| 3702 | |
| 3703 | for (i = first; i <= last; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3704 | if (con_driver_map[i] == csw) { |
| 3705 | module_put(csw->owner); |
| 3706 | con_driver_map[i] = NULL; |
| 3707 | } |
Antonino A. Daplas | 1c8ce27 | 2006-06-26 00:27:03 -0700 | [diff] [blame] | 3708 | } |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3709 | |
| 3710 | if (!con_is_bound(defcsw)) { |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3711 | const struct consw *defconsw = conswitchp; |
| 3712 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3713 | defcsw->con_startup(); |
| 3714 | con_back->flag |= CON_DRIVER_FLAG_INIT; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3715 | /* |
| 3716 | * vgacon may change the default driver to point |
| 3717 | * to dummycon, we restore it here... |
| 3718 | */ |
| 3719 | conswitchp = defconsw; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3720 | } |
| 3721 | |
| 3722 | if (!con_is_bound(csw)) |
| 3723 | con_driver->flag &= ~CON_DRIVER_FLAG_INIT; |
| 3724 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3725 | /* ignore return value, binding should not fail */ |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 3726 | do_bind_con_driver(defcsw, first, last, deflt); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3727 | err: |
| 3728 | module_put(owner); |
| 3729 | return retval; |
| 3730 | |
| 3731 | } |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 3732 | EXPORT_SYMBOL_GPL(do_unbind_con_driver); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3733 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3734 | static int vt_bind(struct con_driver *con) |
| 3735 | { |
| 3736 | const struct consw *defcsw = NULL, *csw = NULL; |
| 3737 | int i, more = 1, first = -1, last = -1, deflt = 0; |
| 3738 | |
Daniel Vetter | 77232f7 | 2015-04-13 11:16:21 +0200 | [diff] [blame] | 3739 | if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3740 | goto err; |
| 3741 | |
| 3742 | csw = con->con; |
| 3743 | |
| 3744 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 3745 | struct con_driver *con = ®istered_con_driver[i]; |
| 3746 | |
| 3747 | if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) { |
| 3748 | defcsw = con->con; |
| 3749 | break; |
| 3750 | } |
| 3751 | } |
| 3752 | |
| 3753 | if (!defcsw) |
| 3754 | goto err; |
| 3755 | |
| 3756 | while (more) { |
| 3757 | more = 0; |
| 3758 | |
| 3759 | for (i = con->first; i <= con->last; i++) { |
| 3760 | if (con_driver_map[i] == defcsw) { |
| 3761 | if (first == -1) |
| 3762 | first = i; |
| 3763 | last = i; |
| 3764 | more = 1; |
| 3765 | } else if (first != -1) |
| 3766 | break; |
| 3767 | } |
| 3768 | |
| 3769 | if (first == 0 && last == MAX_NR_CONSOLES -1) |
| 3770 | deflt = 1; |
| 3771 | |
Imre Deak | 4c215fe | 2014-12-16 00:16:00 +0200 | [diff] [blame] | 3772 | if (first != -1) |
Wang YanQing | c62a1e5 | 2013-05-09 02:14:21 +0800 | [diff] [blame] | 3773 | do_bind_con_driver(csw, first, last, deflt); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3774 | |
| 3775 | first = -1; |
| 3776 | last = -1; |
| 3777 | deflt = 0; |
| 3778 | } |
| 3779 | |
| 3780 | err: |
| 3781 | return 0; |
| 3782 | } |
| 3783 | |
| 3784 | static int vt_unbind(struct con_driver *con) |
| 3785 | { |
| 3786 | const struct consw *csw = NULL; |
| 3787 | int i, more = 1, first = -1, last = -1, deflt = 0; |
Daniel Vetter | f418f2e | 2014-06-05 16:33:24 +0200 | [diff] [blame] | 3788 | int ret; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3789 | |
Daniel Vetter | 77232f7 | 2015-04-13 11:16:21 +0200 | [diff] [blame] | 3790 | if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE)) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3791 | goto err; |
| 3792 | |
| 3793 | csw = con->con; |
| 3794 | |
| 3795 | while (more) { |
| 3796 | more = 0; |
| 3797 | |
| 3798 | for (i = con->first; i <= con->last; i++) { |
| 3799 | if (con_driver_map[i] == csw) { |
| 3800 | if (first == -1) |
| 3801 | first = i; |
| 3802 | last = i; |
| 3803 | more = 1; |
| 3804 | } else if (first != -1) |
| 3805 | break; |
| 3806 | } |
| 3807 | |
| 3808 | if (first == 0 && last == MAX_NR_CONSOLES -1) |
| 3809 | deflt = 1; |
| 3810 | |
Wang YanQing | 618f2b9 | 2013-05-09 02:14:07 +0800 | [diff] [blame] | 3811 | if (first != -1) { |
Daniel Vetter | f418f2e | 2014-06-05 16:33:24 +0200 | [diff] [blame] | 3812 | ret = do_unbind_con_driver(csw, first, last, deflt); |
Daniel Vetter | f418f2e | 2014-06-05 16:33:24 +0200 | [diff] [blame] | 3813 | if (ret != 0) |
| 3814 | return ret; |
Wang YanQing | 618f2b9 | 2013-05-09 02:14:07 +0800 | [diff] [blame] | 3815 | } |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3816 | |
| 3817 | first = -1; |
| 3818 | last = -1; |
| 3819 | deflt = 0; |
| 3820 | } |
| 3821 | |
| 3822 | err: |
| 3823 | return 0; |
| 3824 | } |
Antonino A. Daplas | 13ae664 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3825 | #else |
| 3826 | static inline int vt_bind(struct con_driver *con) |
| 3827 | { |
| 3828 | return 0; |
| 3829 | } |
| 3830 | static inline int vt_unbind(struct con_driver *con) |
| 3831 | { |
| 3832 | return 0; |
| 3833 | } |
| 3834 | #endif /* CONFIG_VT_HW_CONSOLE_BINDING */ |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3835 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3836 | 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] | 3837 | const char *buf, size_t count) |
| 3838 | { |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3839 | struct con_driver *con = dev_get_drvdata(dev); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3840 | int bind = simple_strtoul(buf, NULL, 0); |
| 3841 | |
Imre Deak | 4c215fe | 2014-12-16 00:16:00 +0200 | [diff] [blame] | 3842 | console_lock(); |
| 3843 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3844 | if (bind) |
| 3845 | vt_bind(con); |
| 3846 | else |
| 3847 | vt_unbind(con); |
| 3848 | |
Imre Deak | 4c215fe | 2014-12-16 00:16:00 +0200 | [diff] [blame] | 3849 | console_unlock(); |
| 3850 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3851 | return count; |
| 3852 | } |
| 3853 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3854 | static ssize_t show_bind(struct device *dev, struct device_attribute *attr, |
| 3855 | char *buf) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3856 | { |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3857 | struct con_driver *con = dev_get_drvdata(dev); |
Daniel Vetter | 61d5145 | 2019-07-18 10:09:03 +0200 | [diff] [blame] | 3858 | int bind; |
| 3859 | |
| 3860 | console_lock(); |
| 3861 | bind = con_is_bound(con->con); |
| 3862 | console_unlock(); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3863 | |
| 3864 | return snprintf(buf, PAGE_SIZE, "%i\n", bind); |
| 3865 | } |
| 3866 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3867 | static ssize_t show_name(struct device *dev, struct device_attribute *attr, |
| 3868 | char *buf) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3869 | { |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3870 | struct con_driver *con = dev_get_drvdata(dev); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3871 | |
| 3872 | return snprintf(buf, PAGE_SIZE, "%s %s\n", |
| 3873 | (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)", |
| 3874 | con->desc); |
| 3875 | |
| 3876 | } |
| 3877 | |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3878 | static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind); |
| 3879 | static DEVICE_ATTR(name, S_IRUGO, show_name, NULL); |
| 3880 | |
| 3881 | static struct attribute *con_dev_attrs[] = { |
| 3882 | &dev_attr_bind.attr, |
| 3883 | &dev_attr_name.attr, |
| 3884 | NULL |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3885 | }; |
| 3886 | |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3887 | ATTRIBUTE_GROUPS(con_dev); |
| 3888 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3889 | static int vtconsole_init_device(struct con_driver *con) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3890 | { |
Antonino A. Daplas | 928e964 | 2006-10-03 01:14:49 -0700 | [diff] [blame] | 3891 | con->flag |= CON_DRIVER_FLAG_ATTR; |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3892 | return 0; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3893 | } |
| 3894 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 3895 | static void vtconsole_deinit_device(struct con_driver *con) |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3896 | { |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 3897 | con->flag &= ~CON_DRIVER_FLAG_ATTR; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 3898 | } |
| 3899 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3900 | /** |
| 3901 | * con_is_bound - checks if driver is bound to the console |
| 3902 | * @csw: console driver |
| 3903 | * |
| 3904 | * RETURNS: zero if unbound, nonzero if bound |
| 3905 | * |
| 3906 | * Drivers can call this and if zero, they should release |
| 3907 | * all resources allocated on con_startup() |
| 3908 | */ |
| 3909 | int con_is_bound(const struct consw *csw) |
| 3910 | { |
| 3911 | int i, bound = 0; |
| 3912 | |
Daniel Vetter | ddde3c1 | 2019-05-28 11:02:35 +0200 | [diff] [blame] | 3913 | WARN_CONSOLE_UNLOCKED(); |
| 3914 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 3915 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
| 3916 | if (con_driver_map[i] == csw) { |
| 3917 | bound = 1; |
| 3918 | break; |
| 3919 | } |
| 3920 | } |
| 3921 | |
| 3922 | return bound; |
| 3923 | } |
| 3924 | EXPORT_SYMBOL(con_is_bound); |
| 3925 | |
| 3926 | /** |
Daniel Vetter | ddde3c1 | 2019-05-28 11:02:35 +0200 | [diff] [blame] | 3927 | * con_is_visible - checks whether the current console is visible |
| 3928 | * @vc: virtual console |
| 3929 | * |
| 3930 | * RETURNS: zero if not visible, nonzero if visible |
| 3931 | */ |
| 3932 | bool con_is_visible(const struct vc_data *vc) |
| 3933 | { |
| 3934 | WARN_CONSOLE_UNLOCKED(); |
| 3935 | |
| 3936 | return *vc->vc_display_fg == vc; |
| 3937 | } |
| 3938 | EXPORT_SYMBOL(con_is_visible); |
| 3939 | |
| 3940 | /** |
Jesse Barnes | b45cfba | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 3941 | * con_debug_enter - prepare the console for the kernel debugger |
| 3942 | * @sw: console driver |
| 3943 | * |
| 3944 | * Called when the console is taken over by the kernel debugger, this |
| 3945 | * function needs to save the current console state, then put the console |
| 3946 | * into a state suitable for the kernel debugger. |
| 3947 | * |
| 3948 | * RETURNS: |
| 3949 | * Zero on success, nonzero if a failure occurred when trying to prepare |
| 3950 | * the console for the debugger. |
| 3951 | */ |
| 3952 | int con_debug_enter(struct vc_data *vc) |
| 3953 | { |
| 3954 | int ret = 0; |
| 3955 | |
| 3956 | saved_fg_console = fg_console; |
| 3957 | saved_last_console = last_console; |
| 3958 | saved_want_console = want_console; |
| 3959 | saved_vc_mode = vc->vc_mode; |
Jason Wessel | beed533 | 2010-08-16 15:58:31 -0500 | [diff] [blame] | 3960 | saved_console_blanked = console_blanked; |
Jesse Barnes | b45cfba | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 3961 | vc->vc_mode = KD_TEXT; |
| 3962 | console_blanked = 0; |
| 3963 | if (vc->vc_sw->con_debug_enter) |
| 3964 | ret = vc->vc_sw->con_debug_enter(vc); |
Jason Wessel | 81d4450 | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 3965 | #ifdef CONFIG_KGDB_KDB |
| 3966 | /* Set the initial LINES variable if it is not already set */ |
| 3967 | if (vc->vc_rows < 999) { |
| 3968 | int linecount; |
| 3969 | char lns[4]; |
| 3970 | const char *setargs[3] = { |
| 3971 | "set", |
| 3972 | "LINES", |
| 3973 | lns, |
| 3974 | }; |
| 3975 | if (kdbgetintenv(setargs[0], &linecount)) { |
| 3976 | snprintf(lns, 4, "%i", vc->vc_rows); |
| 3977 | kdb_set(2, setargs); |
| 3978 | } |
| 3979 | } |
Jason Wessel | 17b572e8 | 2012-08-26 22:37:03 -0500 | [diff] [blame] | 3980 | if (vc->vc_cols < 999) { |
| 3981 | int colcount; |
| 3982 | char cols[4]; |
| 3983 | const char *setargs[3] = { |
| 3984 | "set", |
| 3985 | "COLUMNS", |
| 3986 | cols, |
| 3987 | }; |
| 3988 | if (kdbgetintenv(setargs[0], &colcount)) { |
| 3989 | snprintf(cols, 4, "%i", vc->vc_cols); |
| 3990 | kdb_set(2, setargs); |
| 3991 | } |
| 3992 | } |
Jason Wessel | 81d4450 | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 3993 | #endif /* CONFIG_KGDB_KDB */ |
Jesse Barnes | b45cfba | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 3994 | return ret; |
| 3995 | } |
| 3996 | EXPORT_SYMBOL_GPL(con_debug_enter); |
| 3997 | |
| 3998 | /** |
| 3999 | * con_debug_leave - restore console state |
| 4000 | * @sw: console driver |
| 4001 | * |
| 4002 | * Restore the console state to what it was before the kernel debugger |
| 4003 | * was invoked. |
| 4004 | * |
| 4005 | * RETURNS: |
| 4006 | * Zero on success, nonzero if a failure occurred when trying to restore |
| 4007 | * the console. |
| 4008 | */ |
| 4009 | int con_debug_leave(void) |
| 4010 | { |
| 4011 | struct vc_data *vc; |
| 4012 | int ret = 0; |
| 4013 | |
| 4014 | fg_console = saved_fg_console; |
| 4015 | last_console = saved_last_console; |
| 4016 | want_console = saved_want_console; |
Jason Wessel | beed533 | 2010-08-16 15:58:31 -0500 | [diff] [blame] | 4017 | console_blanked = saved_console_blanked; |
Jesse Barnes | b45cfba | 2010-08-05 09:22:30 -0500 | [diff] [blame] | 4018 | vc_cons[fg_console].d->vc_mode = saved_vc_mode; |
| 4019 | |
| 4020 | vc = vc_cons[fg_console].d; |
| 4021 | if (vc->vc_sw->con_debug_leave) |
| 4022 | ret = vc->vc_sw->con_debug_leave(vc); |
| 4023 | return ret; |
| 4024 | } |
| 4025 | EXPORT_SYMBOL_GPL(con_debug_leave); |
| 4026 | |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 4027 | 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] | 4028 | { |
| 4029 | struct module *owner = csw->owner; |
| 4030 | struct con_driver *con_driver; |
| 4031 | const char *desc; |
Jiri Slaby | 96317e9 | 2016-05-03 17:05:55 +0200 | [diff] [blame] | 4032 | int i, retval; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4033 | |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 4034 | WARN_CONSOLE_UNLOCKED(); |
| 4035 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4036 | if (!try_module_get(owner)) |
| 4037 | return -ENODEV; |
| 4038 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4039 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 4040 | con_driver = ®istered_con_driver[i]; |
| 4041 | |
| 4042 | /* already registered */ |
Jiri Slaby | 96317e9 | 2016-05-03 17:05:55 +0200 | [diff] [blame] | 4043 | if (con_driver->con == csw) { |
Dave Airlie | c55c63c | 2011-01-07 09:57:41 +1000 | [diff] [blame] | 4044 | retval = -EBUSY; |
Jiri Slaby | 96317e9 | 2016-05-03 17:05:55 +0200 | [diff] [blame] | 4045 | goto err; |
| 4046 | } |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4047 | } |
| 4048 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4049 | desc = csw->con_startup(); |
Jiri Slaby | 6798df4 | 2016-05-03 17:05:54 +0200 | [diff] [blame] | 4050 | if (!desc) { |
| 4051 | retval = -ENODEV; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4052 | goto err; |
Jiri Slaby | 6798df4 | 2016-05-03 17:05:54 +0200 | [diff] [blame] | 4053 | } |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4054 | |
| 4055 | retval = -EINVAL; |
| 4056 | |
| 4057 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 4058 | con_driver = ®istered_con_driver[i]; |
| 4059 | |
Imre Deak | d364b5c | 2015-04-01 21:06:16 +0300 | [diff] [blame] | 4060 | if (con_driver->con == NULL && |
| 4061 | !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) { |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4062 | con_driver->con = csw; |
| 4063 | con_driver->desc = desc; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4064 | con_driver->node = i; |
| 4065 | con_driver->flag = CON_DRIVER_FLAG_MODULE | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4066 | CON_DRIVER_FLAG_INIT; |
| 4067 | con_driver->first = first; |
| 4068 | con_driver->last = last; |
| 4069 | retval = 0; |
| 4070 | break; |
| 4071 | } |
| 4072 | } |
| 4073 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4074 | if (retval) |
| 4075 | goto err; |
| 4076 | |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 4077 | con_driver->dev = |
| 4078 | device_create_with_groups(vtconsole_class, NULL, |
| 4079 | MKDEV(0, con_driver->node), |
| 4080 | con_driver, con_dev_groups, |
| 4081 | "vtcon%i", con_driver->node); |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4082 | if (IS_ERR(con_driver->dev)) { |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 4083 | pr_warn("Unable to create device for %s; errno = %ld\n", |
| 4084 | con_driver->desc, PTR_ERR(con_driver->dev)); |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4085 | con_driver->dev = NULL; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4086 | } else { |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4087 | vtconsole_init_device(con_driver); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4088 | } |
Antonino A. Daplas | 928e964 | 2006-10-03 01:14:49 -0700 | [diff] [blame] | 4089 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4090 | err: |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4091 | module_put(owner); |
| 4092 | return retval; |
| 4093 | } |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 4094 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4095 | |
| 4096 | /** |
Wang YanQing | 50539dd | 2013-05-09 02:14:44 +0800 | [diff] [blame] | 4097 | * do_unregister_con_driver - unregister console driver from console layer |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4098 | * @csw: console driver |
| 4099 | * |
| 4100 | * DESCRIPTION: All drivers that registers to the console layer must |
| 4101 | * call this function upon exit, or if the console driver is in a state |
| 4102 | * where it won't be able to handle console services, such as the |
| 4103 | * framebuffer console without loaded framebuffer drivers. |
| 4104 | * |
| 4105 | * The driver must unbind first prior to unregistration. |
| 4106 | */ |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 4107 | int do_unregister_con_driver(const struct consw *csw) |
| 4108 | { |
Daniel Vetter | d9c660e | 2014-06-05 16:29:56 +0200 | [diff] [blame] | 4109 | int i; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4110 | |
| 4111 | /* cannot unregister a bound driver */ |
| 4112 | if (con_is_bound(csw)) |
Daniel Vetter | d9c660e | 2014-06-05 16:29:56 +0200 | [diff] [blame] | 4113 | return -EBUSY; |
| 4114 | |
| 4115 | if (csw == conswitchp) |
| 4116 | return -EINVAL; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4117 | |
| 4118 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 4119 | struct con_driver *con_driver = ®istered_con_driver[i]; |
| 4120 | |
Imre Deak | 2cf30f7 | 2014-12-16 00:15:59 +0200 | [diff] [blame] | 4121 | if (con_driver->con == csw) { |
Imre Deak | d364b5c | 2015-04-01 21:06:16 +0300 | [diff] [blame] | 4122 | /* |
| 4123 | * Defer the removal of the sysfs entries since that |
| 4124 | * will acquire the kernfs s_active lock and we can't |
| 4125 | * acquire this lock while holding the console lock: |
| 4126 | * the unbind sysfs entry imposes already the opposite |
| 4127 | * order. Reset con already here to prevent any later |
| 4128 | * lookup to succeed and mark this slot as zombie, so |
| 4129 | * it won't get reused until we complete the removal |
| 4130 | * in the deferred work. |
| 4131 | */ |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4132 | con_driver->con = NULL; |
Imre Deak | d364b5c | 2015-04-01 21:06:16 +0300 | [diff] [blame] | 4133 | con_driver->flag = CON_DRIVER_FLAG_ZOMBIE; |
| 4134 | schedule_work(&con_driver_unregister_work); |
| 4135 | |
Daniel Vetter | d9c660e | 2014-06-05 16:29:56 +0200 | [diff] [blame] | 4136 | return 0; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4137 | } |
| 4138 | } |
Daniel Vetter | d9c660e | 2014-06-05 16:29:56 +0200 | [diff] [blame] | 4139 | |
| 4140 | return -ENODEV; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4141 | } |
Takashi Iwai | e93a9a8 | 2013-01-25 10:28:18 +1000 | [diff] [blame] | 4142 | EXPORT_SYMBOL_GPL(do_unregister_con_driver); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4143 | |
Imre Deak | d364b5c | 2015-04-01 21:06:16 +0300 | [diff] [blame] | 4144 | static void con_driver_unregister_callback(struct work_struct *ignored) |
| 4145 | { |
| 4146 | int i; |
| 4147 | |
| 4148 | console_lock(); |
| 4149 | |
| 4150 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 4151 | struct con_driver *con_driver = ®istered_con_driver[i]; |
| 4152 | |
| 4153 | if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) |
| 4154 | continue; |
| 4155 | |
| 4156 | console_unlock(); |
| 4157 | |
| 4158 | vtconsole_deinit_device(con_driver); |
| 4159 | device_destroy(vtconsole_class, MKDEV(0, con_driver->node)); |
| 4160 | |
| 4161 | console_lock(); |
| 4162 | |
| 4163 | if (WARN_ON_ONCE(con_driver->con)) |
| 4164 | con_driver->con = NULL; |
| 4165 | con_driver->desc = NULL; |
| 4166 | con_driver->dev = NULL; |
| 4167 | con_driver->node = 0; |
| 4168 | WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE); |
| 4169 | con_driver->flag = 0; |
| 4170 | con_driver->first = 0; |
| 4171 | con_driver->last = 0; |
| 4172 | } |
| 4173 | |
| 4174 | console_unlock(); |
| 4175 | } |
| 4176 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4177 | /* |
| 4178 | * If we support more console drivers, this function is used |
| 4179 | * when a driver wants to take over some existing consoles |
| 4180 | * and become default driver for newly opened ones. |
| 4181 | * |
Lukas Wunner | 0095ab4 | 2020-01-09 13:59:21 +0100 | [diff] [blame] | 4182 | * do_take_over_console is basically a register followed by bind |
Alan Cox | 50e244c | 2013-01-25 10:28:15 +1000 | [diff] [blame] | 4183 | */ |
| 4184 | int do_take_over_console(const struct consw *csw, int first, int last, int deflt) |
| 4185 | { |
| 4186 | int err; |
| 4187 | |
| 4188 | err = do_register_con_driver(csw, first, last); |
| 4189 | /* |
| 4190 | * If we get an busy error we still want to bind the console driver |
| 4191 | * and return success, as we may have unbound the console driver |
| 4192 | * but not unregistered it. |
| 4193 | */ |
| 4194 | if (err == -EBUSY) |
| 4195 | err = 0; |
| 4196 | if (!err) |
| 4197 | do_bind_con_driver(csw, first, last, deflt); |
| 4198 | |
| 4199 | return err; |
| 4200 | } |
| 4201 | EXPORT_SYMBOL_GPL(do_take_over_console); |
| 4202 | |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4203 | |
| 4204 | /* |
| 4205 | * give_up_console is a wrapper to unregister_con_driver. It will only |
| 4206 | * work if driver is fully unbound. |
| 4207 | */ |
| 4208 | void give_up_console(const struct consw *csw) |
| 4209 | { |
Wang YanQing | 70125e7 | 2013-05-09 02:14:39 +0800 | [diff] [blame] | 4210 | console_lock(); |
| 4211 | do_unregister_con_driver(csw); |
| 4212 | console_unlock(); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4213 | } |
| 4214 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4215 | static int __init vtconsole_class_init(void) |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4216 | { |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4217 | int i; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4218 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4219 | vtconsole_class = class_create(THIS_MODULE, "vtconsole"); |
| 4220 | if (IS_ERR(vtconsole_class)) { |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 4221 | pr_warn("Unable to create vt console class; errno = %ld\n", |
| 4222 | PTR_ERR(vtconsole_class)); |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4223 | vtconsole_class = NULL; |
| 4224 | } |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4225 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4226 | /* Add system drivers to sysfs */ |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4227 | for (i = 0; i < MAX_NR_CON_DRIVER; i++) { |
| 4228 | struct con_driver *con = ®istered_con_driver[i]; |
| 4229 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4230 | if (con->con && !con->dev) { |
Takashi Iwai | 1083a7b | 2015-02-05 11:07:42 +0100 | [diff] [blame] | 4231 | con->dev = |
| 4232 | device_create_with_groups(vtconsole_class, NULL, |
| 4233 | MKDEV(0, con->node), |
| 4234 | con, con_dev_groups, |
| 4235 | "vtcon%i", con->node); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4236 | |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4237 | if (IS_ERR(con->dev)) { |
Joe Perches | bccf1da | 2017-10-02 08:48:55 -0700 | [diff] [blame] | 4238 | pr_warn("Unable to create device for %s; errno = %ld\n", |
| 4239 | con->desc, PTR_ERR(con->dev)); |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4240 | con->dev = NULL; |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4241 | } else { |
Greg Kroah-Hartman | 805952a | 2006-08-07 22:19:37 -0700 | [diff] [blame] | 4242 | vtconsole_init_device(con); |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4243 | } |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4244 | } |
| 4245 | } |
| 4246 | |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4247 | return 0; |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4248 | } |
Antonino A. Daplas | 6db4063 | 2006-06-26 00:27:12 -0700 | [diff] [blame] | 4249 | postcore_initcall(vtconsole_class_init); |
| 4250 | |
| 4251 | #endif |
Antonino A. Daplas | 3e795de | 2006-06-26 00:27:08 -0700 | [diff] [blame] | 4252 | |
| 4253 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4254 | * Screen blanking |
| 4255 | */ |
| 4256 | |
Yoichi Yuasa | 403aac9 | 2006-12-06 20:38:38 -0800 | [diff] [blame] | 4257 | static int set_vesa_blanking(char __user *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | { |
Yoichi Yuasa | 403aac9 | 2006-12-06 20:38:38 -0800 | [diff] [blame] | 4259 | unsigned int mode; |
| 4260 | |
| 4261 | if (get_user(mode, p + 1)) |
| 4262 | return -EFAULT; |
| 4263 | |
| 4264 | vesa_blank_mode = (mode < 4) ? mode : 0; |
| 4265 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4266 | } |
| 4267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4268 | void do_blank_screen(int entering_gfx) |
| 4269 | { |
| 4270 | struct vc_data *vc = vc_cons[fg_console].d; |
| 4271 | int i; |
| 4272 | |
Daniel Vetter | a135513 | 2019-05-28 11:02:34 +0200 | [diff] [blame] | 4273 | might_sleep(); |
| 4274 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4275 | WARN_CONSOLE_UNLOCKED(); |
| 4276 | |
| 4277 | if (console_blanked) { |
| 4278 | if (blank_state == blank_vesa_wait) { |
| 4279 | blank_state = blank_off; |
Ville Syrjala | d060a32 | 2006-01-09 20:53:49 -0800 | [diff] [blame] | 4280 | vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4281 | } |
| 4282 | return; |
| 4283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4284 | |
| 4285 | /* entering graphics mode? */ |
| 4286 | if (entering_gfx) { |
| 4287 | hide_cursor(vc); |
| 4288 | save_screen(vc); |
| 4289 | vc->vc_sw->con_blank(vc, -1, 1); |
| 4290 | console_blanked = fg_console + 1; |
izumi | b6e8f00 | 2007-07-17 04:05:49 -0700 | [diff] [blame] | 4291 | blank_state = blank_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4292 | set_origin(vc); |
| 4293 | return; |
| 4294 | } |
| 4295 | |
izumi | b6e8f00 | 2007-07-17 04:05:49 -0700 | [diff] [blame] | 4296 | blank_state = blank_off; |
| 4297 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4298 | /* don't blank graphics */ |
| 4299 | if (vc->vc_mode != KD_TEXT) { |
| 4300 | console_blanked = fg_console + 1; |
| 4301 | return; |
| 4302 | } |
| 4303 | |
| 4304 | hide_cursor(vc); |
| 4305 | del_timer_sync(&console_timer); |
| 4306 | blank_timer_expired = 0; |
| 4307 | |
| 4308 | save_screen(vc); |
| 4309 | /* In case we need to reset origin, blanking hook returns 1 */ |
Ville Syrjala | d060a32 | 2006-01-09 20:53:49 -0800 | [diff] [blame] | 4310 | 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] | 4311 | console_blanked = fg_console + 1; |
| 4312 | if (i) |
| 4313 | set_origin(vc); |
| 4314 | |
| 4315 | if (console_blank_hook && console_blank_hook(1)) |
| 4316 | return; |
| 4317 | |
Ville Syrjala | d060a32 | 2006-01-09 20:53:49 -0800 | [diff] [blame] | 4318 | if (vesa_off_interval && vesa_blank_mode) { |
Nishanth Aravamudan | 030baba | 2005-07-15 03:56:25 -0700 | [diff] [blame] | 4319 | blank_state = blank_vesa_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4320 | mod_timer(&console_timer, jiffies + vesa_off_interval); |
| 4321 | } |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 4322 | vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | } |
| 4324 | EXPORT_SYMBOL(do_blank_screen); |
| 4325 | |
| 4326 | /* |
| 4327 | * Called by timer as well as from vt_console_driver |
| 4328 | */ |
| 4329 | void do_unblank_screen(int leaving_gfx) |
| 4330 | { |
| 4331 | struct vc_data *vc; |
| 4332 | |
| 4333 | /* This should now always be called from a "sane" (read: can schedule) |
| 4334 | * context for the sake of the low level drivers, except in the special |
| 4335 | * case of oops_in_progress |
| 4336 | */ |
| 4337 | if (!oops_in_progress) |
| 4338 | might_sleep(); |
| 4339 | |
| 4340 | WARN_CONSOLE_UNLOCKED(); |
| 4341 | |
| 4342 | ignore_poke = 0; |
| 4343 | if (!console_blanked) |
| 4344 | return; |
| 4345 | if (!vc_cons_allocated(fg_console)) { |
| 4346 | /* impossible */ |
Joe Perches | e620e54 | 2014-11-09 22:46:35 -0800 | [diff] [blame] | 4347 | pr_warn("unblank_screen: tty %d not allocated ??\n", |
| 4348 | fg_console + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | return; |
| 4350 | } |
| 4351 | vc = vc_cons[fg_console].d; |
Daniel Vetter | 8d7fc29 | 2018-08-22 10:54:03 +0200 | [diff] [blame] | 4352 | if (vc->vc_mode != KD_TEXT) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | return; /* but leave console_blanked != 0 */ |
| 4354 | |
| 4355 | if (blankinterval) { |
Daniel Mack | f324edc | 2009-06-16 15:33:52 -0700 | [diff] [blame] | 4356 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | blank_state = blank_normal_wait; |
| 4358 | } |
| 4359 | |
| 4360 | console_blanked = 0; |
Daniel Vetter | 8d7fc29 | 2018-08-22 10:54:03 +0200 | [diff] [blame] | 4361 | if (vc->vc_sw->con_blank(vc, 0, leaving_gfx)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4362 | /* Low-level driver cannot restore -> do it ourselves */ |
| 4363 | update_screen(vc); |
| 4364 | if (console_blank_hook) |
| 4365 | console_blank_hook(0); |
| 4366 | set_palette(vc); |
| 4367 | set_cursor(vc); |
Alan Cox | 8b92e87 | 2009-09-19 13:13:24 -0700 | [diff] [blame] | 4368 | vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4369 | } |
| 4370 | EXPORT_SYMBOL(do_unblank_screen); |
| 4371 | |
| 4372 | /* |
| 4373 | * This is called by the outside world to cause a forced unblank, mostly for |
| 4374 | * oopses. Currently, I just call do_unblank_screen(0), but we could eventually |
| 4375 | * call it with 1 as an argument and so force a mode restore... that may kill |
| 4376 | * X or at least garbage the screen but would also make the Oops visible... |
| 4377 | */ |
| 4378 | void unblank_screen(void) |
| 4379 | { |
| 4380 | do_unblank_screen(0); |
| 4381 | } |
| 4382 | |
| 4383 | /* |
Ingo Molnar | 70522e1 | 2006-03-23 03:00:31 -0800 | [diff] [blame] | 4384 | * 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] | 4385 | * (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] | 4386 | * has the console mutex. Not perfect yet, but better than no locking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | */ |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 4388 | static void blank_screen_t(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4389 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | blank_timer_expired = 1; |
| 4391 | schedule_work(&console_work); |
| 4392 | } |
| 4393 | |
| 4394 | void poke_blanked_console(void) |
| 4395 | { |
| 4396 | WARN_CONSOLE_UNLOCKED(); |
| 4397 | |
| 4398 | /* Add this so we quickly catch whoever might call us in a non |
| 4399 | * safe context. Nowadays, unblank_screen() isn't to be called in |
| 4400 | * atomic contexts and is allowed to schedule (with the special case |
| 4401 | * of oops_in_progress, but that isn't of any concern for this |
| 4402 | * function. --BenH. |
| 4403 | */ |
| 4404 | might_sleep(); |
| 4405 | |
| 4406 | /* This isn't perfectly race free, but a race here would be mostly harmless, |
| 4407 | * at worse, we'll do a spurrious blank and it's unlikely |
| 4408 | */ |
| 4409 | del_timer(&console_timer); |
| 4410 | blank_timer_expired = 0; |
| 4411 | |
| 4412 | if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS) |
| 4413 | return; |
| 4414 | if (console_blanked) |
| 4415 | unblank_screen(); |
| 4416 | else if (blankinterval) { |
Daniel Mack | f324edc | 2009-06-16 15:33:52 -0700 | [diff] [blame] | 4417 | mod_timer(&console_timer, jiffies + (blankinterval * HZ)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | blank_state = blank_normal_wait; |
| 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | /* |
| 4423 | * Palettes |
| 4424 | */ |
| 4425 | |
| 4426 | static void set_palette(struct vc_data *vc) |
| 4427 | { |
| 4428 | WARN_CONSOLE_UNLOCKED(); |
| 4429 | |
Jiri Slaby | 709280d | 2016-06-23 13:34:27 +0200 | [diff] [blame] | 4430 | if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | vc->vc_sw->con_set_palette(vc, color_table); |
| 4432 | } |
| 4433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4434 | /* |
| 4435 | * Load palette into the DAC registers. arg points to a colour |
| 4436 | * map, 3 bytes per colour, 16 colours, range from 0 to 255. |
| 4437 | */ |
| 4438 | |
| 4439 | int con_set_cmap(unsigned char __user *arg) |
| 4440 | { |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4441 | int i, j, k; |
| 4442 | unsigned char colormap[3*16]; |
| 4443 | |
| 4444 | if (copy_from_user(colormap, arg, sizeof(colormap))) |
| 4445 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4447 | console_lock(); |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4448 | for (i = k = 0; i < 16; i++) { |
| 4449 | default_red[i] = colormap[k++]; |
| 4450 | default_grn[i] = colormap[k++]; |
| 4451 | default_blu[i] = colormap[k++]; |
| 4452 | } |
| 4453 | for (i = 0; i < MAX_NR_CONSOLES; i++) { |
| 4454 | if (!vc_cons_allocated(i)) |
| 4455 | continue; |
| 4456 | for (j = k = 0; j < 16; j++) { |
| 4457 | vc_cons[i].d->vc_palette[k++] = default_red[j]; |
| 4458 | vc_cons[i].d->vc_palette[k++] = default_grn[j]; |
| 4459 | vc_cons[i].d->vc_palette[k++] = default_blu[j]; |
| 4460 | } |
| 4461 | set_palette(vc_cons[i].d); |
| 4462 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4463 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4464 | |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4465 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | int con_get_cmap(unsigned char __user *arg) |
| 4469 | { |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4470 | int i, k; |
| 4471 | unsigned char colormap[3*16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4472 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4473 | console_lock(); |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4474 | for (i = k = 0; i < 16; i++) { |
| 4475 | colormap[k++] = default_red[i]; |
| 4476 | colormap[k++] = default_grn[i]; |
| 4477 | colormap[k++] = default_blu[i]; |
| 4478 | } |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4479 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4480 | |
Michael Gehring | 871bdea | 2012-03-21 01:26:45 +0100 | [diff] [blame] | 4481 | if (copy_to_user(arg, colormap, sizeof(colormap))) |
| 4482 | return -EFAULT; |
| 4483 | |
| 4484 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4485 | } |
| 4486 | |
| 4487 | void reset_palette(struct vc_data *vc) |
| 4488 | { |
| 4489 | int j, k; |
| 4490 | for (j=k=0; j<16; j++) { |
| 4491 | vc->vc_palette[k++] = default_red[j]; |
| 4492 | vc->vc_palette[k++] = default_grn[j]; |
| 4493 | vc->vc_palette[k++] = default_blu[j]; |
| 4494 | } |
| 4495 | set_palette(vc); |
| 4496 | } |
| 4497 | |
| 4498 | /* |
| 4499 | * Font switching |
| 4500 | * |
| 4501 | * Currently we only support fonts up to 32 pixels wide, at a maximum height |
| 4502 | * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints, |
| 4503 | * depending on width) reserved for each character which is kinda wasty, but |
| 4504 | * 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] | 4505 | * 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] | 4506 | * format (maybe we should add a `fontoffset' field to the `display' |
| 4507 | * structure so we won't have to convert the fontdata all the time. |
| 4508 | * /Jes |
| 4509 | */ |
| 4510 | |
| 4511 | #define max_font_size 65536 |
| 4512 | |
| 4513 | static int con_font_get(struct vc_data *vc, struct console_font_op *op) |
| 4514 | { |
| 4515 | struct console_font font; |
| 4516 | int rc = -EINVAL; |
| 4517 | int c; |
| 4518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | if (op->data) { |
| 4520 | font.data = kmalloc(max_font_size, GFP_KERNEL); |
| 4521 | if (!font.data) |
| 4522 | return -ENOMEM; |
| 4523 | } else |
| 4524 | font.data = NULL; |
| 4525 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4526 | console_lock(); |
Alan Cox | edab558 | 2012-03-02 14:59:08 +0000 | [diff] [blame] | 4527 | if (vc->vc_mode != KD_TEXT) |
| 4528 | rc = -EINVAL; |
| 4529 | else if (vc->vc_sw->con_font_get) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4530 | rc = vc->vc_sw->con_font_get(vc, &font); |
| 4531 | else |
| 4532 | rc = -ENOSYS; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4533 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4534 | |
| 4535 | if (rc) |
| 4536 | goto out; |
| 4537 | |
| 4538 | c = (font.width+7)/8 * 32 * font.charcount; |
Alan Cox | 04f378b | 2008-04-30 00:53:29 -0700 | [diff] [blame] | 4539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4540 | if (op->data && font.charcount > op->charcount) |
| 4541 | rc = -ENOSPC; |
| 4542 | if (!(op->flags & KD_FONT_FLAG_OLD)) { |
| 4543 | if (font.width > op->width || font.height > op->height) |
| 4544 | rc = -ENOSPC; |
| 4545 | } else { |
| 4546 | if (font.width != 8) |
| 4547 | rc = -EIO; |
| 4548 | else if ((op->height && font.height > op->height) || |
| 4549 | font.height > 32) |
| 4550 | rc = -ENOSPC; |
| 4551 | } |
| 4552 | if (rc) |
| 4553 | goto out; |
| 4554 | |
| 4555 | op->height = font.height; |
| 4556 | op->width = font.width; |
| 4557 | op->charcount = font.charcount; |
| 4558 | |
| 4559 | if (op->data && copy_to_user(op->data, font.data, c)) |
| 4560 | rc = -EFAULT; |
| 4561 | |
| 4562 | out: |
| 4563 | kfree(font.data); |
| 4564 | return rc; |
| 4565 | } |
| 4566 | |
| 4567 | static int con_font_set(struct vc_data *vc, struct console_font_op *op) |
| 4568 | { |
| 4569 | struct console_font font; |
| 4570 | int rc = -EINVAL; |
| 4571 | int size; |
| 4572 | |
| 4573 | if (vc->vc_mode != KD_TEXT) |
| 4574 | return -EINVAL; |
| 4575 | if (!op->data) |
| 4576 | return -EINVAL; |
| 4577 | if (op->charcount > 512) |
| 4578 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | if (op->width <= 0 || op->width > 32 || op->height > 32) |
| 4580 | return -EINVAL; |
| 4581 | size = (op->width+7)/8 * 32 * op->charcount; |
| 4582 | if (size > max_font_size) |
| 4583 | return -ENOSPC; |
Meng Xu | 8ffb820 | 2017-10-04 10:38:37 -0400 | [diff] [blame] | 4584 | |
Julia Lawall | 9b71ca2 | 2010-05-26 14:42:11 -0700 | [diff] [blame] | 4585 | font.data = memdup_user(op->data, size); |
| 4586 | if (IS_ERR(font.data)) |
| 4587 | return PTR_ERR(font.data); |
Meng Xu | 8ffb820 | 2017-10-04 10:38:37 -0400 | [diff] [blame] | 4588 | |
| 4589 | if (!op->height) { /* Need to guess font height [compat] */ |
| 4590 | int h, i; |
| 4591 | u8 *charmap = font.data; |
| 4592 | |
| 4593 | /* |
| 4594 | * If from KDFONTOP ioctl, don't allow things which can be done |
| 4595 | * in userland,so that we can get rid of this soon |
| 4596 | */ |
| 4597 | if (!(op->flags & KD_FONT_FLAG_OLD)) { |
| 4598 | kfree(font.data); |
| 4599 | return -EINVAL; |
| 4600 | } |
| 4601 | |
| 4602 | for (h = 32; h > 0; h--) |
| 4603 | for (i = 0; i < op->charcount; i++) |
| 4604 | if (charmap[32*i+h-1]) |
| 4605 | goto nonzero; |
| 4606 | |
| 4607 | kfree(font.data); |
| 4608 | return -EINVAL; |
| 4609 | |
| 4610 | nonzero: |
| 4611 | op->height = h; |
| 4612 | } |
| 4613 | |
| 4614 | font.charcount = op->charcount; |
| 4615 | font.width = op->width; |
| 4616 | font.height = op->height; |
| 4617 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4618 | console_lock(); |
Alan Cox | edab558 | 2012-03-02 14:59:08 +0000 | [diff] [blame] | 4619 | if (vc->vc_mode != KD_TEXT) |
| 4620 | rc = -EINVAL; |
| 4621 | else if (vc->vc_sw->con_font_set) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4622 | rc = vc->vc_sw->con_font_set(vc, &font, op->flags); |
| 4623 | else |
| 4624 | rc = -ENOSYS; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4625 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4626 | kfree(font.data); |
| 4627 | return rc; |
| 4628 | } |
| 4629 | |
| 4630 | static int con_font_default(struct vc_data *vc, struct console_font_op *op) |
| 4631 | { |
| 4632 | struct console_font font = {.width = op->width, .height = op->height}; |
| 4633 | char name[MAX_FONT_NAME]; |
| 4634 | char *s = name; |
| 4635 | int rc; |
| 4636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4637 | |
| 4638 | if (!op->data) |
| 4639 | s = NULL; |
| 4640 | else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0) |
| 4641 | return -EFAULT; |
| 4642 | else |
| 4643 | name[MAX_FONT_NAME - 1] = 0; |
| 4644 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4645 | console_lock(); |
Alan Cox | edab558 | 2012-03-02 14:59:08 +0000 | [diff] [blame] | 4646 | if (vc->vc_mode != KD_TEXT) { |
| 4647 | console_unlock(); |
| 4648 | return -EINVAL; |
| 4649 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | if (vc->vc_sw->con_font_default) |
| 4651 | rc = vc->vc_sw->con_font_default(vc, &font, s); |
| 4652 | else |
| 4653 | rc = -ENOSYS; |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4654 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | if (!rc) { |
| 4656 | op->width = font.width; |
| 4657 | op->height = font.height; |
| 4658 | } |
| 4659 | return rc; |
| 4660 | } |
| 4661 | |
| 4662 | static int con_font_copy(struct vc_data *vc, struct console_font_op *op) |
| 4663 | { |
| 4664 | int con = op->height; |
| 4665 | int rc; |
| 4666 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4668 | console_lock(); |
Alan Cox | edab558 | 2012-03-02 14:59:08 +0000 | [diff] [blame] | 4669 | if (vc->vc_mode != KD_TEXT) |
| 4670 | rc = -EINVAL; |
| 4671 | else if (!vc->vc_sw->con_font_copy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4672 | rc = -ENOSYS; |
| 4673 | else if (con < 0 || !vc_cons_allocated(con)) |
| 4674 | rc = -ENOTTY; |
| 4675 | else if (con == vc->vc_num) /* nothing to do */ |
| 4676 | rc = 0; |
| 4677 | else |
| 4678 | rc = vc->vc_sw->con_font_copy(vc, con); |
Torben Hohn | ac751ef | 2011-01-25 15:07:35 -0800 | [diff] [blame] | 4679 | console_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4680 | return rc; |
| 4681 | } |
| 4682 | |
| 4683 | int con_font_op(struct vc_data *vc, struct console_font_op *op) |
| 4684 | { |
| 4685 | switch (op->op) { |
| 4686 | case KD_FONT_OP_SET: |
| 4687 | return con_font_set(vc, op); |
| 4688 | case KD_FONT_OP_GET: |
| 4689 | return con_font_get(vc, op); |
| 4690 | case KD_FONT_OP_SET_DEFAULT: |
| 4691 | return con_font_default(vc, op); |
| 4692 | case KD_FONT_OP_COPY: |
| 4693 | return con_font_copy(vc, op); |
| 4694 | } |
| 4695 | return -ENOSYS; |
| 4696 | } |
| 4697 | |
| 4698 | /* |
| 4699 | * Interface exported to selection and vcs. |
| 4700 | */ |
| 4701 | |
| 4702 | /* used by selection */ |
| 4703 | u16 screen_glyph(struct vc_data *vc, int offset) |
| 4704 | { |
| 4705 | u16 w = scr_readw(screenpos(vc, offset, 1)); |
| 4706 | u16 c = w & 0xff; |
| 4707 | |
| 4708 | if (w & vc->vc_hi_font_mask) |
| 4709 | c |= 0x100; |
| 4710 | return c; |
| 4711 | } |
Samuel Thibault | f7511d5 | 2008-04-30 00:54:51 -0700 | [diff] [blame] | 4712 | EXPORT_SYMBOL_GPL(screen_glyph); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | |
Adam Borowski | 9bfdc26 | 2018-07-18 04:10:44 +0200 | [diff] [blame] | 4714 | u32 screen_glyph_unicode(struct vc_data *vc, int n) |
| 4715 | { |
| 4716 | struct uni_screen *uniscr = get_vc_uniscr(vc); |
| 4717 | |
| 4718 | if (uniscr) |
| 4719 | return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols]; |
| 4720 | return inverse_translate(vc, screen_glyph(vc, n * 2), 1); |
| 4721 | } |
| 4722 | EXPORT_SYMBOL_GPL(screen_glyph_unicode); |
| 4723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | /* used by vcs - note the word offset */ |
| 4725 | unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed) |
| 4726 | { |
| 4727 | return screenpos(vc, 2 * w_offset, viewed); |
| 4728 | } |
Samuel Thibault | 88867e3 | 2016-01-25 01:32:08 +0100 | [diff] [blame] | 4729 | EXPORT_SYMBOL_GPL(screen_pos); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | |
| 4731 | void getconsxy(struct vc_data *vc, unsigned char *p) |
| 4732 | { |
Nicolas Pitre | 8a08549 | 2019-01-08 22:55:02 -0500 | [diff] [blame] | 4733 | /* clamp values if they don't fit */ |
Jiri Slaby | 28bc24f | 2020-06-15 09:48:33 +0200 | [diff] [blame] | 4734 | p[0] = min(vc->state.x, 0xFFu); |
| 4735 | p[1] = min(vc->state.y, 0xFFu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4736 | } |
| 4737 | |
| 4738 | void putconsxy(struct vc_data *vc, unsigned char *p) |
| 4739 | { |
Antonino A. Daplas | 9477e26 | 2006-02-01 03:06:52 -0800 | [diff] [blame] | 4740 | hide_cursor(vc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | gotoxy(vc, p[0], p[1]); |
| 4742 | set_cursor(vc); |
| 4743 | } |
| 4744 | |
| 4745 | u16 vcs_scr_readw(struct vc_data *vc, const u16 *org) |
| 4746 | { |
| 4747 | if ((unsigned long)org == vc->vc_pos && softcursor_original != -1) |
| 4748 | return softcursor_original; |
| 4749 | return scr_readw(org); |
| 4750 | } |
| 4751 | |
| 4752 | void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org) |
| 4753 | { |
| 4754 | scr_writew(val, org); |
| 4755 | if ((unsigned long)org == vc->vc_pos) { |
| 4756 | softcursor_original = -1; |
| 4757 | add_softcursor(vc); |
| 4758 | } |
| 4759 | } |
| 4760 | |
Nicolas Pitre | 432c9ed | 2010-10-01 00:10:44 -0400 | [diff] [blame] | 4761 | void vcs_scr_updated(struct vc_data *vc) |
| 4762 | { |
| 4763 | notify_update(vc); |
| 4764 | } |
| 4765 | |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4766 | void vc_scrolldelta_helper(struct vc_data *c, int lines, |
| 4767 | unsigned int rolled_over, void *base, unsigned int size) |
| 4768 | { |
| 4769 | unsigned long ubase = (unsigned long)base; |
Jiri Slaby | 210fd74 | 2016-10-03 11:18:36 +0200 | [diff] [blame] | 4770 | ptrdiff_t scr_end = (void *)c->vc_scr_end - base; |
| 4771 | ptrdiff_t vorigin = (void *)c->vc_visible_origin - base; |
| 4772 | ptrdiff_t origin = (void *)c->vc_origin - base; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4773 | int margin = c->vc_size_row * 4; |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4774 | int from, wrap, from_off, avail; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4775 | |
| 4776 | /* Turn scrollback off */ |
| 4777 | if (!lines) { |
| 4778 | c->vc_visible_origin = c->vc_origin; |
| 4779 | return; |
| 4780 | } |
| 4781 | |
| 4782 | /* 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] | 4783 | if (rolled_over > scr_end + margin) { |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4784 | from = scr_end; |
| 4785 | wrap = rolled_over + c->vc_size_row; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4786 | } else { |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4787 | from = 0; |
| 4788 | wrap = size; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4789 | } |
| 4790 | |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4791 | from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row; |
| 4792 | avail = (origin - from + wrap) % wrap; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4793 | |
| 4794 | /* Only a little piece would be left? Show all incl. the piece! */ |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4795 | if (avail < 2 * margin) |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4796 | margin = 0; |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4797 | if (from_off < margin) |
| 4798 | from_off = 0; |
| 4799 | if (from_off > avail - margin) |
| 4800 | from_off = avail; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4801 | |
Jiri Slaby | 7c918cdc | 2016-10-03 11:18:37 +0200 | [diff] [blame] | 4802 | c->vc_visible_origin = ubase + (from + from_off) % wrap; |
Jiri Slaby | 35cc56f | 2016-10-03 11:18:35 +0200 | [diff] [blame] | 4803 | } |
| 4804 | EXPORT_SYMBOL_GPL(vc_scrolldelta_helper); |
| 4805 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | /* |
| 4807 | * Visible symbols for modules |
| 4808 | */ |
| 4809 | |
| 4810 | EXPORT_SYMBOL(color_table); |
| 4811 | EXPORT_SYMBOL(default_red); |
| 4812 | EXPORT_SYMBOL(default_grn); |
| 4813 | EXPORT_SYMBOL(default_blu); |
| 4814 | EXPORT_SYMBOL(update_region); |
| 4815 | EXPORT_SYMBOL(redraw_screen); |
| 4816 | EXPORT_SYMBOL(vc_resize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | EXPORT_SYMBOL(fg_console); |
| 4818 | EXPORT_SYMBOL(console_blank_hook); |
| 4819 | EXPORT_SYMBOL(console_blanked); |
| 4820 | EXPORT_SYMBOL(vc_cons); |
Matthew Garrett | f6c06b6 | 2009-11-13 15:14:11 -0500 | [diff] [blame] | 4821 | EXPORT_SYMBOL(global_cursor_default); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | #ifndef VT_SINGLE_DRIVER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | EXPORT_SYMBOL(give_up_console); |
| 4824 | #endif |