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