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