blob: 29fb08c8a2fd6efc6e1e04dc16a7e053ff500161 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * Hopefully this will be a rather complete VT102 implementation.
7 *
8 * Beeping thanks to John T Kohl.
9 *
10 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
11 * Chars, and VT100 enhancements by Peter MacDonald.
12 *
13 * Copy and paste function by Andrew Haylett,
14 * some enhancements by Alessandro Rubini.
15 *
16 * Code to check for different video-cards mostly by Galen Hunt,
17 * <g-hunt@ee.utah.edu>
18 *
19 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
20 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
21 *
22 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
23 * Resizing of consoles, aeb, 940926
24 *
25 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
26 * <poe@daimi.aau.dk>
27 *
28 * User-defined bell sound, new setterm control sequences and printk
29 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
30 *
31 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
32 *
33 * Merge with the abstract console driver by Geert Uytterhoeven
34 * <geert@linux-m68k.org>, Jan 1997.
35 *
36 * Original m68k console driver modifications by
37 *
38 * - Arno Griffioen <arno@usn.nl>
39 * - David Carter <carter@cs.bris.ac.uk>
40 *
41 * The abstract console driver provides a generic interface for a text
42 * console. It supports VGA text mode, frame buffer based graphical consoles
43 * and special graphics processors that are only accessible through some
44 * registers (e.g. a TMS340x0 GSP).
45 *
46 * The interface to the hardware is specified using a special structure
47 * (struct consw) which contains function pointers to console operations
48 * (see <linux/console.h> for more information).
49 *
50 * Support for changeable cursor shape
51 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
52 *
53 * Ported to i386 and con_scrolldelta fixed
54 * by Emmanuel Marty <core@ggi-project.org>, April 1998
55 *
56 * Resurrected character buffers in videoram plus lots of other trickery
57 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
58 *
59 * Removed old-style timers, introduced console_timer, made timer
Francois Camie1f8e872008-10-15 22:01:59 -070060 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Removed console_lock, enabled interrupts across all console operations
63 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070064 *
65 * Fixed UTF-8 mode so alternate charset modes always work according
66 * to control sequences interpreted in do_con_trol function
67 * preserving backward VT100 semigraphics compatibility,
68 * malformed UTF sequences represented as sequences of replacement glyphs,
69 * original codes or '?' as a last resort if replacement glyph is undefined
70 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
73#include <linux/module.h>
74#include <linux/types.h>
75#include <linux/sched.h>
76#include <linux/tty.h>
77#include <linux/tty_flip.h>
78#include <linux/kernel.h>
79#include <linux/string.h>
80#include <linux/errno.h>
81#include <linux/kd.h>
82#include <linux/slab.h>
83#include <linux/major.h>
84#include <linux/mm.h>
85#include <linux/console.h>
86#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070087#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/vt_kern.h>
89#include <linux/selection.h>
90#include <linux/tiocl.h>
91#include <linux/kbd_kern.h>
92#include <linux/consolemap.h>
93#include <linux/timer.h>
94#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/pm.h>
97#include <linux/font.h>
98#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -070099#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100100#include <linux/device.h>
101#include <linux/io.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100102#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500103#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300104#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700106#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700108#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700109#define CON_DRIVER_FLAG_INIT 2
110#define CON_DRIVER_FLAG_ATTR 4
Imre Deakd364b5c2015-04-01 21:06:16 +0300111#define CON_DRIVER_FLAG_ZOMBIE 8
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700112
113struct con_driver {
114 const struct consw *con;
115 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700116 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700117 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700118 int first;
119 int last;
120 int flag;
121};
122
123static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124const struct consw *conswitchp;
125
126/* A bitmap for codes <32. A bit of 1 indicates that the code
127 * corresponding to that bit number invokes some special action
128 * (such as cursor movement) and should not be displayed as a
129 * glyph unless the disp_ctrl mode is explicitly enabled.
130 */
131#define CTRL_ACTION 0x0d00ff81
132#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
133
134/*
135 * Here is the default bell parameters: 750HZ, 1/8th of a second
136 */
137#define DEFAULT_BELL_PITCH 750
138#define DEFAULT_BELL_DURATION (HZ/8)
Scot Doylebd633642015-03-26 13:54:39 +0000139#define DEFAULT_CURSOR_BLINK_MS 200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141struct vc vc_cons [MAX_NR_CONSOLES];
142
143#ifndef VT_SINGLE_DRIVER
144static const struct consw *con_driver_map[MAX_NR_CONSOLES];
145#endif
146
147static int con_open(struct tty_struct *, struct file *);
148static void vc_init(struct vc_data *vc, unsigned int rows,
149 unsigned int cols, int do_clear);
150static void gotoxy(struct vc_data *vc, int new_x, int new_y);
151static void save_cur(struct vc_data *vc);
152static void reset_terminal(struct vc_data *vc, int do_clear);
153static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800154static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155static void set_cursor(struct vc_data *vc);
156static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000157static void console_callback(struct work_struct *ignored);
Imre Deakd364b5c2015-04-01 21:06:16 +0300158static void con_driver_unregister_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void blank_screen_t(unsigned long dummy);
160static void set_palette(struct vc_data *vc);
161
Peter Hurley52c40fc2014-11-11 11:20:56 -0500162#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700165int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700166module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500167int global_cursor_default = -1;
168module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800170static int cur_default = CUR_DEFAULT;
171module_param(cur_default, int, S_IRUGO | S_IWUSR);
172
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173/*
174 * ignore_poke: don't unblank the screen when things are typed. This is
175 * mainly for the privacy of braille terminal users.
176 */
177static int ignore_poke;
178
179int do_poke_blanked_console;
180int console_blanked;
181
182static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static int vesa_off_interval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700184static int blankinterval = 10*60;
185core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
David Howells65f27f32006-11-22 14:55:48 +0000187static DECLARE_WORK(console_work, console_callback);
Imre Deakd364b5c2015-04-01 21:06:16 +0300188static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190/*
191 * fg_console is the current virtual console,
192 * last_console is the last used one,
193 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500194 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
196int fg_console;
197int last_console;
198int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500199static int saved_fg_console;
200static int saved_last_console;
201static int saved_want_console;
202static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500203static int saved_console_blanked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
205/*
206 * For each existing display, we have a pointer to console currently visible
207 * on that display, allowing consoles other than fg_console to be refreshed
208 * appropriately. Unless the low-level driver supplies its own display_fg
209 * variable, we use this one for the "master display".
210 */
211static struct vc_data *master_display_fg;
212
213/*
214 * Unfortunately, we need to delay tty echo when we're currently writing to the
215 * console since the code is (and always was) not re-entrant, so we schedule
216 * all flip requests to process context with schedule-task() and run it from
217 * console_callback().
218 */
219
220/*
221 * For the same reason, we defer scrollback to the console callback.
222 */
223static int scrollback_delta;
224
225/*
226 * Hook so that the power management routines can (un)blank
227 * the console on our behalf.
228 */
229int (*console_blank_hook)(int);
230
Jiri Slaby40565f12007-02-12 00:52:31 -0800231static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int blank_state;
233static int blank_timer_expired;
234enum {
235 blank_off = 0,
236 blank_normal_wait,
237 blank_vesa_wait,
238};
239
240/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100241 * /sys/class/tty/tty0/
242 *
243 * the attribute 'active' contains the name of the current vc
244 * console and it supports poll() to detect vc switches
245 */
246static struct device *tty0dev;
247
248/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700249 * Notifier list for console events.
250 */
251static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
252
253int register_vt_notifier(struct notifier_block *nb)
254{
255 return atomic_notifier_chain_register(&vt_notifier_list, nb);
256}
257EXPORT_SYMBOL_GPL(register_vt_notifier);
258
259int unregister_vt_notifier(struct notifier_block *nb)
260{
261 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
262}
263EXPORT_SYMBOL_GPL(unregister_vt_notifier);
264
265static void notify_write(struct vc_data *vc, unsigned int unicode)
266{
Mathias Krause502fcb72011-07-30 14:01:59 +0200267 struct vt_notifier_param param = { .vc = vc, .c = unicode };
Samuel Thibaultb293d752007-10-18 23:39:17 -0700268 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
269}
270
271static void notify_update(struct vc_data *vc)
272{
273 struct vt_notifier_param param = { .vc = vc };
274 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
275}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700276/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Low-Level Functions
278 */
279
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200280static inline bool con_is_fg(const struct vc_data *vc)
281{
282 return vc->vc_num == fg_console;
283}
284
285static inline bool con_should_update(const struct vc_data *vc)
286{
287 return con_is_visible(vc) && !console_blanked;
288}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
291{
292 unsigned short *p;
293
294 if (!viewed)
295 p = (unsigned short *)(vc->vc_origin + offset);
296 else if (!vc->vc_sw->con_screen_pos)
297 p = (unsigned short *)(vc->vc_visible_origin + offset);
298 else
299 p = vc->vc_sw->con_screen_pos(vc, offset);
300 return p;
301}
302
Alan Coxe33ac1c2010-06-01 22:52:54 +0200303/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304static inline void scrolldelta(int lines)
305{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200306 /* FIXME */
307 /* scrolldelta needs some kind of consistency lock, but the BKL was
308 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 scrollback_delta += lines;
310 schedule_console_callback();
311}
312
313void schedule_console_callback(void)
314{
315 schedule_work(&console_work);
316}
317
318static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
319{
320 unsigned short *d, *s;
321
322 if (t+nr >= b)
323 nr = b - t - 1;
324 if (b > vc->vc_rows || t >= b || nr < 1)
325 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200326 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return;
328 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
329 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
330 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds386f40c2010-06-06 20:44:04 -0700331 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 vc->vc_size_row * nr);
333}
334
335static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
336{
337 unsigned short *s;
338 unsigned int step;
339
340 if (t+nr >= b)
341 nr = b - t - 1;
342 if (b > vc->vc_rows || t >= b || nr < 1)
343 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200344 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 return;
346 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
347 step = vc->vc_cols * nr;
348 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds93f78da2008-10-14 12:12:02 -0700349 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352static void do_update_region(struct vc_data *vc, unsigned long start, int count)
353{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 unsigned int xx, yy, offset;
355 u16 *p;
356
357 p = (u16 *) start;
358 if (!vc->vc_sw->con_getxy) {
359 offset = (start - vc->vc_origin) / 2;
360 xx = offset % vc->vc_cols;
361 yy = offset / vc->vc_cols;
362 } else {
363 int nxx, nyy;
364 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
365 xx = nxx; yy = nyy;
366 }
367 for(;;) {
368 u16 attrib = scr_readw(p) & 0xff00;
369 int startx = xx;
370 u16 *q = p;
371 while (xx < vc->vc_cols && count) {
372 if (attrib != (scr_readw(p) & 0xff00)) {
373 if (p > q)
374 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
375 startx = xx;
376 q = p;
377 attrib = scr_readw(p) & 0xff00;
378 }
379 p++;
380 xx++;
381 count--;
382 }
383 if (p > q)
384 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
385 if (!count)
386 break;
387 xx = 0;
388 yy++;
389 if (vc->vc_sw->con_getxy) {
390 p = (u16 *)start;
391 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
392 }
393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394}
395
396void update_region(struct vc_data *vc, unsigned long start, int count)
397{
398 WARN_CONSOLE_UNLOCKED();
399
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200400 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 hide_cursor(vc);
402 do_update_region(vc, start, count);
403 set_cursor(vc);
404 }
405}
406
407/* Structure of attributes is hardware-dependent */
408
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700409static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
410 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700413 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
414 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416/*
417 * ++roman: I completely changed the attribute format for monochrome
418 * mode (!can_do_color). The formerly used MDA (monochrome display
419 * adapter) format didn't allow the combination of certain effects.
420 * Now the attribute is just a bit vector:
421 * Bit 0..1: intensity (0..2)
422 * Bit 2 : underline
423 * Bit 3 : reverse
424 * Bit 7 : blink
425 */
426 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700427 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (!vc->vc_can_do_color)
429 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700430 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 (_underline ? 4 : 0) |
432 (_reverse ? 8 : 0) |
433 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700434 if (_italic)
435 a = (a & 0xF0) | vc->vc_itcolor;
436 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 a = (a & 0xf0) | vc->vc_ulcolor;
438 else if (_intensity == 0)
439 a = (a & 0xf0) | vc->vc_ulcolor;
440 if (_reverse)
441 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
442 if (_blink)
443 a ^= 0x80;
444 if (_intensity == 2)
445 a ^= 0x08;
446 if (vc->vc_hi_font_mask == 0x100)
447 a <<= 1;
448 return a;
449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static void update_attr(struct vc_data *vc)
453{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700454 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
455 vc->vc_blink, vc->vc_underline,
456 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
457 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/* Note: inverting the screen twice should revert to the original state */
461void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
462{
463 unsigned short *p;
464
465 WARN_CONSOLE_UNLOCKED();
466
467 count /= 2;
468 p = screenpos(vc, offset, viewed);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200469 if (vc->vc_sw->con_invert_region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 vc->vc_sw->con_invert_region(vc, p, count);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200471 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 u16 *q = p;
473 int cnt = count;
474 u16 a;
475
476 if (!vc->vc_can_do_color) {
477 while (cnt--) {
478 a = scr_readw(q);
479 a ^= 0x0800;
480 scr_writew(a, q);
481 q++;
482 }
483 } else if (vc->vc_hi_font_mask == 0x100) {
484 while (cnt--) {
485 a = scr_readw(q);
486 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
487 scr_writew(a, q);
488 q++;
489 }
490 } else {
491 while (cnt--) {
492 a = scr_readw(q);
493 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
494 scr_writew(a, q);
495 q++;
496 }
497 }
498 }
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200499
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200500 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 do_update_region(vc, (unsigned long) p, count);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500502 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505/* used by selection: complement pointer position */
506void complement_pos(struct vc_data *vc, int offset)
507{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700508 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 static unsigned short old;
510 static unsigned short oldx, oldy;
511
512 WARN_CONSOLE_UNLOCKED();
513
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700514 if (old_offset != -1 && old_offset >= 0 &&
515 old_offset < vc->vc_screenbuf_size) {
516 scr_writew(old, screenpos(vc, old_offset, 1));
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200517 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 vc->vc_sw->con_putc(vc, old, oldy, oldx);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500519 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700521
522 old_offset = offset;
523
524 if (offset != -1 && offset >= 0 &&
525 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700527 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 p = screenpos(vc, offset, 1);
529 old = scr_readw(p);
530 new = old ^ vc->vc_complement_mask;
531 scr_writew(new, p);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200532 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 oldx = (offset >> 1) % vc->vc_cols;
534 oldy = (offset >> 1) / vc->vc_cols;
535 vc->vc_sw->con_putc(vc, new, oldy, oldx);
536 }
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500537 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539}
540
541static void insert_char(struct vc_data *vc, unsigned int nr)
542{
Jean-François Moine81732c32012-09-06 19:24:13 +0200543 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Nicolas Pitrea883b702013-02-24 20:06:09 -0500545 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200546 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200548 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200549 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100550 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static void delete_char(struct vc_data *vc, unsigned int nr)
554{
Jean-François Moine81732c32012-09-06 19:24:13 +0200555 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100557 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200558 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
559 nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200561 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200562 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100563 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Melchior FRANZe882f712015-11-01 19:48:18 +0100566static int softcursor_original = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568static void add_softcursor(struct vc_data *vc)
569{
570 int i = scr_readw((u16 *) vc->vc_pos);
571 u32 type = vc->vc_cursor_type;
572
573 if (! (type & 0x10)) return;
574 if (softcursor_original != -1) return;
575 softcursor_original = i;
576 i |= ((type >> 8) & 0xff00 );
577 i ^= ((type) & 0xff00 );
578 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
579 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
580 scr_writew(i, (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200581 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
583}
584
585static void hide_softcursor(struct vc_data *vc)
586{
587 if (softcursor_original != -1) {
588 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200589 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 vc->vc_sw->con_putc(vc, softcursor_original,
591 vc->vc_y, vc->vc_x);
592 softcursor_original = -1;
593 }
594}
595
596static void hide_cursor(struct vc_data *vc)
597{
Jiri Slabyfc3d6dd2020-02-19 08:39:43 +0100598 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 clear_selection();
Jiri Slabyfc3d6dd2020-02-19 08:39:43 +0100600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 vc->vc_sw->con_cursor(vc, CM_ERASE);
602 hide_softcursor(vc);
603}
604
605static void set_cursor(struct vc_data *vc)
606{
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200607 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return;
609 if (vc->vc_deccm) {
Jiri Slabyfc3d6dd2020-02-19 08:39:43 +0100610 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 clear_selection();
612 add_softcursor(vc);
613 if ((vc->vc_cursor_type & 0x0f) != 1)
614 vc->vc_sw->con_cursor(vc, CM_DRAW);
615 } else
616 hide_cursor(vc);
617}
618
619static void set_origin(struct vc_data *vc)
620{
621 WARN_CONSOLE_UNLOCKED();
622
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200623 if (!con_is_visible(vc) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 !vc->vc_sw->con_set_origin ||
625 !vc->vc_sw->con_set_origin(vc))
626 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
627 vc->vc_visible_origin = vc->vc_origin;
628 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
629 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
630}
631
Denys Vlasenkoc0f160a2015-10-27 18:46:47 +0100632static void save_screen(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 WARN_CONSOLE_UNLOCKED();
635
636 if (vc->vc_sw->con_save_screen)
637 vc->vc_sw->con_save_screen(vc);
638}
639
640/*
641 * Redrawing of screen
642 */
643
Dave Airlie2a248302013-01-24 14:14:19 +1000644void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
646 unsigned short *p = (unsigned short *)vc->vc_origin;
647 int count = vc->vc_screenbuf_size / 2;
648 int mask = vc->vc_hi_font_mask | 0xff;
649
650 for (; count > 0; count--, p++) {
651 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
652 }
653}
654
655void redraw_screen(struct vc_data *vc, int is_switch)
656{
657 int redraw = 0;
658
659 WARN_CONSOLE_UNLOCKED();
660
661 if (!vc) {
662 /* strange ... */
663 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
664 return;
665 }
666
667 if (is_switch) {
668 struct vc_data *old_vc = vc_cons[fg_console].d;
669 if (old_vc == vc)
670 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200671 if (!con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 redraw = 1;
673 *vc->vc_display_fg = vc;
674 fg_console = vc->vc_num;
675 hide_cursor(old_vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200676 if (!con_is_visible(old_vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 save_screen(old_vc);
678 set_origin(old_vc);
679 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100680 if (tty0dev)
681 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 } else {
683 hide_cursor(vc);
684 redraw = 1;
685 }
686
687 if (redraw) {
688 int update;
689 int old_was_color = vc->vc_can_do_color;
690
691 set_origin(vc);
692 update = vc->vc_sw->con_switch(vc);
693 set_palette(vc);
694 /*
695 * If console changed from mono<->color, the best we can do
696 * is to clear the buffer attributes. As it currently stands,
697 * rebuilding new attributes from the old buffer is not doable
698 * without overly complex code.
699 */
700 if (old_was_color != vc->vc_can_do_color) {
701 update_attr(vc);
702 clear_buffer_attributes(vc);
703 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700704
705 /* Forcibly update if we're panicing */
706 if ((update && vc->vc_mode != KD_GRAPHICS) ||
707 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
709 }
710 set_cursor(vc);
711 if (is_switch) {
712 set_leds();
713 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800714 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716}
717
718/*
719 * Allocation, freeing and resizing of VTs.
720 */
721
722int vc_cons_allocated(unsigned int i)
723{
724 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
725}
726
727static void visual_init(struct vc_data *vc, int num, int init)
728{
729 /* ++Geert: vc->vc_sw->con_init determines console size */
730 if (vc->vc_sw)
731 module_put(vc->vc_sw->owner);
732 vc->vc_sw = conswitchp;
733#ifndef VT_SINGLE_DRIVER
734 if (con_driver_map[num])
735 vc->vc_sw = con_driver_map[num];
736#endif
737 __module_get(vc->vc_sw->owner);
738 vc->vc_num = num;
739 vc->vc_display_fg = &master_display_fg;
Dongxing Zhang08b33242015-06-10 15:21:10 +0800740 if (vc->vc_uni_pagedir_loc)
741 con_free_unimap(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +0200743 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 vc->vc_hi_font_mask = 0;
745 vc->vc_complement_mask = 0;
746 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700747 vc->vc_panic_force_write = false;
David Daney1b459962016-05-17 11:41:04 -0700748 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 vc->vc_sw->con_init(vc, init);
750 if (!vc->vc_complement_mask)
751 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
752 vc->vc_s_complement_mask = vc->vc_complement_mask;
753 vc->vc_size_row = vc->vc_cols << 1;
754 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
755}
756
Eric Biggers6bc9bf72020-03-21 20:43:04 -0700757static void vc_port_destruct(struct tty_port *port)
758{
759 struct vc_data *vc = container_of(port, struct vc_data, port);
760
761 kfree(vc);
762}
763
764static const struct tty_port_operations vc_port_ops = {
765 .destruct = vc_port_destruct,
766};
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768int vc_allocate(unsigned int currcons) /* return 0 on success */
769{
Jiri Slaby34902b72016-03-31 10:08:15 +0200770 struct vt_notifier_param param;
771 struct vc_data *vc;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 WARN_CONSOLE_UNLOCKED();
774
775 if (currcons >= MAX_NR_CONSOLES)
776 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Jiri Slaby34902b72016-03-31 10:08:15 +0200778 if (vc_cons[currcons].d)
779 return 0;
780
781 /* due to the granularity of kmalloc, we waste some memory here */
782 /* the alloc is done in two steps, to optimize the common situation
783 of a 25x80 console (structsize=216, screenbuf_size=4000) */
784 /* although the numbers above are not valid since long ago, the
785 point is still up-to-date and the comment still has its value
786 even if only as a historical artifact. --mj, July 1998 */
787 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
788 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return -ENOMEM;
Jiri Slaby34902b72016-03-31 10:08:15 +0200790
791 vc_cons[currcons].d = vc;
792 tty_port_init(&vc->port);
Eric Biggers6bc9bf72020-03-21 20:43:04 -0700793 vc->port.ops = &vc_port_ops;
Jiri Slaby34902b72016-03-31 10:08:15 +0200794 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
795
796 visual_init(vc, currcons, 1);
797
798 if (!*vc->vc_uni_pagedir_loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 con_set_default_unimap(vc);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500800
Alexander Potapenko3bf351b2018-06-14 12:23:09 +0200801 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Jiri Slaby34902b72016-03-31 10:08:15 +0200802 if (!vc->vc_screenbuf)
803 goto err_free;
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500804
Jiri Slaby34902b72016-03-31 10:08:15 +0200805 /* If no drivers have overridden us and the user didn't pass a
806 boot option, default to displaying the cursor */
807 if (global_cursor_default == -1)
808 global_cursor_default = 1;
809
810 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
811 vcs_make_sysfs(currcons);
812 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return 0;
Jiri Slaby34902b72016-03-31 10:08:15 +0200815err_free:
816 kfree(vc);
817 vc_cons[currcons].d = NULL;
818 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700821static inline int resize_screen(struct vc_data *vc, int width, int height,
822 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 /* Resizes the resolution of the display adapater */
825 int err = 0;
826
827 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700828 err = vc->vc_sw->con_resize(vc, width, height, user);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return err;
831}
832
833/*
834 * Change # of rows and columns (0 means unchanged/the size of fg_console)
835 * [this is to be used together with some user program
836 * like resize that changes the hardware videomode]
837 */
838#define VC_RESIZE_MAXCOL (32767)
839#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100840
841/**
842 * vc_do_resize - resizing method for the tty
843 * @tty: tty being resized
844 * @real_tty: real tty (different to tty if a pty/tty pair)
845 * @vc: virtual console private data
846 * @cols: columns
847 * @lines: lines
848 *
849 * Resize a virtual console, clipping according to the actual constraints.
850 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800851 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100852 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400853 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100854 * ctrl_lock of the tty IFF a tty is passed.
855 */
856
Alan Coxfc6f6232009-01-02 13:43:17 +0000857static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
858 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
860 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700861 unsigned long end;
Jiri Slabyf2ee4ae2011-03-23 09:49:57 +0100862 unsigned int old_rows, old_row_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700864 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 unsigned short *newscreen;
866
867 WARN_CONSOLE_UNLOCKED();
868
869 if (!vc)
870 return -ENXIO;
871
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700872 user = vc->vc_resize_user;
873 vc->vc_resize_user = 0;
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
876 return -EINVAL;
877
878 new_cols = (cols ? cols : vc->vc_cols);
879 new_rows = (lines ? lines : vc->vc_rows);
880 new_row_size = new_cols << 1;
881 new_screen_size = new_row_size * new_rows;
882
883 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
884 return 0;
885
Dmitry Vyukov32b29212016-10-14 15:18:28 +0200886 if (new_screen_size > (4 << 20))
887 return -EINVAL;
Alexander Potapenko3bf351b2018-06-14 12:23:09 +0200888 newscreen = kzalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!newscreen)
890 return -ENOMEM;
891
Jiri Slabyfc3d6dd2020-02-19 08:39:43 +0100892 if (vc_is_sel(vc))
Scot Doyle009e39a2016-10-13 12:12:43 -0500893 clear_selection();
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700898 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (err) {
900 kfree(newscreen);
901 return err;
902 }
903
904 vc->vc_rows = new_rows;
905 vc->vc_cols = new_cols;
906 vc->vc_size_row = new_row_size;
907 vc->vc_screenbuf_size = new_screen_size;
908
909 rlth = min(old_row_size, new_row_size);
910 rrem = new_row_size - rlth;
911 old_origin = vc->vc_origin;
912 new_origin = (long) newscreen;
913 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700914
915 if (vc->vc_y > new_rows) {
916 if (old_rows - vc->vc_y < new_rows) {
917 /*
918 * Cursor near the bottom, copy contents from the
919 * bottom of buffer
920 */
921 old_origin += (old_rows - new_rows) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 } else {
923 /*
924 * Cursor is in no man's land, copy 1/2 screenful
925 * from the top and bottom of cursor position
926 */
927 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700928 }
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +0200929 }
930
931 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 update_attr(vc);
934
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700935 while (old_origin < end) {
936 scr_memcpyw((unsigned short *) new_origin,
937 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700939 scr_memsetw((void *)(new_origin + rlth),
940 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 old_origin += old_row_size;
942 new_origin += new_row_size;
943 }
944 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700945 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
946 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100947 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 vc->vc_screenbuf_size = new_screen_size;
950 set_origin(vc);
951
952 /* do part of a reset_terminal() */
953 vc->vc_top = 0;
954 vc->vc_bottom = vc->vc_rows;
955 gotoxy(vc, vc->vc_x, vc->vc_y);
956 save_cur(vc);
957
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100958 if (tty) {
959 /* Rewrite the requested winsize data with the actual
960 resulting sizes */
961 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 memset(&ws, 0, sizeof(ws));
963 ws.ws_row = vc->vc_rows;
964 ws.ws_col = vc->vc_cols;
965 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000966 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200969 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700971 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Nicolas Pitrec13c2a52019-01-08 22:55:01 -0500972 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return err;
974}
975
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100976/**
977 * vc_resize - resize a VT
978 * @vc: virtual console
979 * @cols: columns
980 * @rows: rows
981 *
982 * Resize a virtual console as seen from the console end of things. We
983 * use the common vc_do_resize methods to update the structures. The
984 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +0200985 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100986 */
987
988int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700989{
Alan Cox8ce73262010-06-01 22:52:56 +0200990 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100991}
992
993/**
994 * vt_resize - resize a VT
995 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100996 * @ws: winsize attributes
997 *
998 * Resize a virtual terminal. This is called by the tty layer as we
999 * register our own handler for resizing. The mutual helper does all
1000 * the actual work.
1001 *
1002 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -04001003 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001004 */
Roel Kluinda2bdf92009-01-07 18:09:15 -08001005static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001006{
1007 struct vc_data *vc = tty->driver_data;
1008 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Torben Hohnac751ef2011-01-25 15:07:35 -08001010 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001011 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001012 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001013 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001014}
1015
Peter Hurley421b40a2013-05-17 12:41:03 -04001016struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Peter Hurley421b40a2013-05-17 12:41:03 -04001018 struct vc_data *vc = NULL;
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 WARN_CONSOLE_UNLOCKED();
1021
1022 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001023 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001024
Peter Hurley421b40a2013-05-17 12:41:03 -04001025 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001026 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001027 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001029 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001030 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001031 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 vc_cons[currcons].d = NULL;
1033 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001034 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035}
1036
1037/*
1038 * VT102 emulator
1039 */
1040
Alan Cox079c9532012-02-28 14:49:23 +00001041#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1042#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1043#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045#define decarm VC_REPEAT
1046#define decckm VC_CKMODE
1047#define kbdapplic VC_APPLIC
1048#define lnm VC_CRLF
1049
1050/*
1051 * this is what the terminal answers to a ESC-Z or csi0c query.
1052 */
1053#define VT100ID "\033[?1;2c"
1054#define VT102ID "\033[?6c"
1055
Jiri Slaby8ede5cc2016-03-31 10:08:16 +02001056const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 8,12,10,14, 9,13,11,15 };
1058
1059/* the default colour table, for VGA+ colour systems */
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001060unsigned char default_red[] = {
1061 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1062 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1063};
1064module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001066unsigned char default_grn[] = {
1067 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1068 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1069};
1070module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1071
1072unsigned char default_blu[] = {
1073 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1074 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1075};
1076module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/*
1079 * gotoxy() must verify all boundaries, because the arguments
1080 * might also be negative. If the given position is out of
1081 * bounds, the cursor is placed at the nearest margin.
1082 */
1083static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1084{
1085 int min_y, max_y;
1086
1087 if (new_x < 0)
1088 vc->vc_x = 0;
1089 else {
1090 if (new_x >= vc->vc_cols)
1091 vc->vc_x = vc->vc_cols - 1;
1092 else
1093 vc->vc_x = new_x;
1094 }
1095
1096 if (vc->vc_decom) {
1097 min_y = vc->vc_top;
1098 max_y = vc->vc_bottom;
1099 } else {
1100 min_y = 0;
1101 max_y = vc->vc_rows;
1102 }
1103 if (new_y < min_y)
1104 vc->vc_y = min_y;
1105 else if (new_y >= max_y)
1106 vc->vc_y = max_y - 1;
1107 else
1108 vc->vc_y = new_y;
1109 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1110 vc->vc_need_wrap = 0;
1111}
1112
1113/* for absolute user moves, when decom is set */
1114static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1115{
1116 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1117}
1118
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001119void scrollback(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001121 scrolldelta(-(vc->vc_rows / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124void scrollfront(struct vc_data *vc, int lines)
1125{
1126 if (!lines)
1127 lines = vc->vc_rows / 2;
1128 scrolldelta(lines);
1129}
1130
1131static void lf(struct vc_data *vc)
1132{
1133 /* don't scroll if above bottom of scrolling region, or
1134 * if below scrolling region
1135 */
1136 if (vc->vc_y + 1 == vc->vc_bottom)
1137 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1138 else if (vc->vc_y < vc->vc_rows - 1) {
1139 vc->vc_y++;
1140 vc->vc_pos += vc->vc_size_row;
1141 }
1142 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001143 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
1146static void ri(struct vc_data *vc)
1147{
1148 /* don't scroll if below top of scrolling region, or
1149 * if above scrolling region
1150 */
1151 if (vc->vc_y == vc->vc_top)
1152 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1153 else if (vc->vc_y > 0) {
1154 vc->vc_y--;
1155 vc->vc_pos -= vc->vc_size_row;
1156 }
1157 vc->vc_need_wrap = 0;
1158}
1159
1160static inline void cr(struct vc_data *vc)
1161{
1162 vc->vc_pos -= vc->vc_x << 1;
1163 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001164 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165}
1166
1167static inline void bs(struct vc_data *vc)
1168{
1169 if (vc->vc_x) {
1170 vc->vc_pos -= 2;
1171 vc->vc_x--;
1172 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001173 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
1175}
1176
1177static inline void del(struct vc_data *vc)
1178{
1179 /* ignored */
1180}
1181
1182static void csi_J(struct vc_data *vc, int vpar)
1183{
1184 unsigned int count;
1185 unsigned short * start;
1186
1187 switch (vpar) {
1188 case 0: /* erase from cursor to end of display */
1189 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1190 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
1192 case 1: /* erase from start to cursor */
1193 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1194 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 break;
Petr Písařf8df13e2011-04-15 10:08:08 +02001196 case 3: /* erase scroll-back buffer (and whole display) */
1197 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
Jiri Slaby42acfc62016-10-03 11:00:17 +02001198 vc->vc_screenbuf_size);
Petr Písařf8df13e2011-04-15 10:08:08 +02001199 set_origin(vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001200 if (con_is_visible(vc))
Petr Písař0930b092014-02-06 21:01:23 +01001201 update_screen(vc);
Petr Písařf8df13e2011-04-15 10:08:08 +02001202 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 case 2: /* erase whole display */
1204 count = vc->vc_cols * vc->vc_rows;
1205 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 default:
1208 return;
1209 }
1210 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001211 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001212 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 vc->vc_need_wrap = 0;
1214}
1215
1216static void csi_K(struct vc_data *vc, int vpar)
1217{
1218 unsigned int count;
1219 unsigned short * start;
1220
1221 switch (vpar) {
1222 case 0: /* erase from cursor to end of line */
1223 count = vc->vc_cols - vc->vc_x;
1224 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 break;
1226 case 1: /* erase from start of line to cursor */
1227 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1228 count = vc->vc_x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 break;
1230 case 2: /* erase whole line */
1231 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1232 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 break;
1234 default:
1235 return;
1236 }
1237 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1238 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001239 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001240 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241}
1242
1243static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1244{ /* not vt100? */
1245 int count;
1246
1247 if (!vpar)
1248 vpar++;
1249 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1250
1251 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001252 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1254 vc->vc_need_wrap = 0;
1255}
1256
1257static void default_attr(struct vc_data *vc)
1258{
1259 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001260 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 vc->vc_underline = 0;
1262 vc->vc_reverse = 0;
1263 vc->vc_blink = 0;
1264 vc->vc_color = vc->vc_def_color;
1265}
1266
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001267struct rgb { u8 r; u8 g; u8 b; };
1268
Jiri Slaby0f91e142016-06-23 13:34:32 +02001269static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001270{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001271 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001272 c->r = i&1 ? 0xaa : 0x00;
1273 c->g = i&2 ? 0xaa : 0x00;
1274 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001275 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001276 c->r = i&1 ? 0xff : 0x55;
1277 c->g = i&2 ? 0xff : 0x55;
1278 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001279 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001280 c->r = (i - 16) / 36 * 85 / 2;
1281 c->g = (i - 16) / 6 % 6 * 85 / 2;
1282 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001283 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001284 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001285}
1286
Jiri Slaby0f91e142016-06-23 13:34:32 +02001287static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001288{
Jiri Slaby193df022016-06-23 13:34:33 +02001289 u8 hue = 0, max = max3(c->r, c->g, c->b);
1290
1291 if (c->r > max / 2)
1292 hue |= 4;
1293 if (c->g > max / 2)
1294 hue |= 2;
1295 if (c->b > max / 2)
1296 hue |= 1;
1297
1298 if (hue == 7 && max <= 0x55) {
1299 hue = 0;
1300 vc->vc_intensity = 2;
1301 } else if (max > 0xaa)
1302 vc->vc_intensity = 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001303 else
Jiri Slaby193df022016-06-23 13:34:33 +02001304 vc->vc_intensity = 1;
1305
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001306 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1307}
1308
Jiri Slaby0f91e142016-06-23 13:34:32 +02001309static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001310{
1311 /* For backgrounds, err on the dark side. */
1312 vc->vc_color = (vc->vc_color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001313 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001314}
1315
Jiri Slabye05ab232016-06-23 13:34:31 +02001316/*
1317 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1318 * and thus need to be detected and ignored by hand. Strictly speaking, that
1319 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1320 * no one produces such codes and almost no one accepts them.
1321 *
1322 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1323 * supporting them.
1324 */
1325static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001326 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001327{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001328 struct rgb c;
1329
Jiri Slabye05ab232016-06-23 13:34:31 +02001330 i++;
1331 if (i > vc->vc_npar)
1332 return i;
1333
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001334 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001335 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001336 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001337 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001338 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001339 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001340 c.r = vc->vc_par[i + 1];
1341 c.g = vc->vc_par[i + 2];
1342 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001343 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001344 } else
1345 return i;
1346
1347 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001348
1349 return i;
1350}
1351
Torben Hohnac751ef2011-01-25 15:07:35 -08001352/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353static void csi_m(struct vc_data *vc)
1354{
1355 int i;
1356
1357 for (i = 0; i <= vc->vc_npar; i++)
1358 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001359 case 0: /* all attributes off */
1360 default_attr(vc);
1361 break;
1362 case 1:
1363 vc->vc_intensity = 2;
1364 break;
1365 case 2:
1366 vc->vc_intensity = 0;
1367 break;
1368 case 3:
1369 vc->vc_italic = 1;
1370 break;
Mike Frysinger4d2e98c2018-01-29 17:08:21 -05001371 case 21:
1372 /*
1373 * No console drivers support double underline, so
1374 * convert it to a single underline.
1375 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001376 case 4:
1377 vc->vc_underline = 1;
1378 break;
1379 case 5:
1380 vc->vc_blink = 1;
1381 break;
1382 case 7:
1383 vc->vc_reverse = 1;
1384 break;
1385 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1386 * Select primary font, don't display control chars if
1387 * defined, don't set bit 8 on output.
1388 */
1389 vc->vc_translate = set_translate(vc->vc_charset == 0
1390 ? vc->vc_G0_charset
1391 : vc->vc_G1_charset, vc);
1392 vc->vc_disp_ctrl = 0;
1393 vc->vc_toggle_meta = 0;
1394 break;
1395 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1396 * Select first alternate font, lets chars < 32 be
1397 * displayed as ROM chars.
1398 */
1399 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1400 vc->vc_disp_ctrl = 1;
1401 vc->vc_toggle_meta = 0;
1402 break;
1403 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1404 * Select second alternate font, toggle high bit
1405 * before displaying as ROM char.
1406 */
1407 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1408 vc->vc_disp_ctrl = 1;
1409 vc->vc_toggle_meta = 1;
1410 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001411 case 22:
1412 vc->vc_intensity = 1;
1413 break;
1414 case 23:
1415 vc->vc_italic = 0;
1416 break;
1417 case 24:
1418 vc->vc_underline = 0;
1419 break;
1420 case 25:
1421 vc->vc_blink = 0;
1422 break;
1423 case 27:
1424 vc->vc_reverse = 0;
1425 break;
1426 case 38:
1427 i = vc_t416_color(vc, i, rgb_foreground);
1428 break;
1429 case 48:
1430 i = vc_t416_color(vc, i, rgb_background);
1431 break;
1432 case 39:
1433 vc->vc_color = (vc->vc_def_color & 0x0f) |
1434 (vc->vc_color & 0xf0);
1435 break;
1436 case 49:
1437 vc->vc_color = (vc->vc_def_color & 0xf0) |
1438 (vc->vc_color & 0x0f);
1439 break;
1440 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001441 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1442 if (vc->vc_par[i] < 100)
1443 vc->vc_intensity = 2;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001444 vc->vc_par[i] -= 60;
1445 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001446 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1447 vc->vc_color = color_table[vc->vc_par[i] - 30]
1448 | (vc->vc_color & 0xf0);
1449 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1450 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1451 | (vc->vc_color & 0x0f);
1452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 update_attr(vc);
1455}
1456
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001457static void respond_string(const char *p, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458{
1459 while (*p) {
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001460 tty_insert_flip_char(port, *p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 p++;
1462 }
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001463 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
1466static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1467{
1468 char buf[40];
1469
1470 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001471 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474static inline void status_report(struct tty_struct *tty)
1475{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001476 respond_string("\033[0n", tty->port); /* Terminal ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477}
1478
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001479static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001481 respond_string(VT102ID, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482}
1483
1484void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1485{
1486 char buf[8];
1487
1488 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1489 (char)('!' + mry));
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001490 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
1493/* invoked via ioctl(TIOCLINUX) and through set_selection */
1494int mouse_reporting(void)
1495{
1496 return vc_cons[fg_console].d->vc_report_mouse;
1497}
1498
Torben Hohnac751ef2011-01-25 15:07:35 -08001499/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500static void set_mode(struct vc_data *vc, int on_off)
1501{
1502 int i;
1503
1504 for (i = 0; i <= vc->vc_npar; i++)
1505 if (vc->vc_ques) {
1506 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1507 case 1: /* Cursor keys send ^[Ox/^[[x */
1508 if (on_off)
1509 set_kbd(vc, decckm);
1510 else
1511 clr_kbd(vc, decckm);
1512 break;
1513 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514#if 0
1515 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1516 /* this alone does not suffice; some user mode
1517 utility has to change the hardware regs */
1518#endif
1519 break;
1520 case 5: /* Inverted screen on/off */
1521 if (vc->vc_decscnm != on_off) {
1522 vc->vc_decscnm = on_off;
1523 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1524 update_attr(vc);
1525 }
1526 break;
1527 case 6: /* Origin relative/absolute */
1528 vc->vc_decom = on_off;
1529 gotoxay(vc, 0, 0);
1530 break;
1531 case 7: /* Autowrap on/off */
1532 vc->vc_decawm = on_off;
1533 break;
1534 case 8: /* Autorepeat on/off */
1535 if (on_off)
1536 set_kbd(vc, decarm);
1537 else
1538 clr_kbd(vc, decarm);
1539 break;
1540 case 9:
1541 vc->vc_report_mouse = on_off ? 1 : 0;
1542 break;
1543 case 25: /* Cursor on/off */
1544 vc->vc_deccm = on_off;
1545 break;
1546 case 1000:
1547 vc->vc_report_mouse = on_off ? 2 : 0;
1548 break;
1549 }
1550 } else {
1551 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1552 case 3: /* Monitor (display ctrls) */
1553 vc->vc_disp_ctrl = on_off;
1554 break;
1555 case 4: /* Insert Mode on/off */
1556 vc->vc_decim = on_off;
1557 break;
1558 case 20: /* Lf, Enter == CrLf/Lf */
1559 if (on_off)
1560 set_kbd(vc, lnm);
1561 else
1562 clr_kbd(vc, lnm);
1563 break;
1564 }
1565 }
1566}
1567
Torben Hohnac751ef2011-01-25 15:07:35 -08001568/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569static void setterm_command(struct vc_data *vc)
1570{
1571 switch(vc->vc_par[0]) {
1572 case 1: /* set color for underline mode */
1573 if (vc->vc_can_do_color &&
1574 vc->vc_par[1] < 16) {
1575 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1576 if (vc->vc_underline)
1577 update_attr(vc);
1578 }
1579 break;
1580 case 2: /* set color for half intensity mode */
1581 if (vc->vc_can_do_color &&
1582 vc->vc_par[1] < 16) {
1583 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1584 if (vc->vc_intensity == 0)
1585 update_attr(vc);
1586 }
1587 break;
1588 case 8: /* store colors as defaults */
1589 vc->vc_def_color = vc->vc_attr;
1590 if (vc->vc_hi_font_mask == 0x100)
1591 vc->vc_def_color >>= 1;
1592 default_attr(vc);
1593 update_attr(vc);
1594 break;
1595 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001596 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 poke_blanked_console();
1598 break;
1599 case 10: /* set bell frequency in Hz */
1600 if (vc->vc_npar >= 1)
1601 vc->vc_bell_pitch = vc->vc_par[1];
1602 else
1603 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1604 break;
1605 case 11: /* set bell duration in msec */
1606 if (vc->vc_npar >= 1)
1607 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
Nicholas Mc Guire3372ec22015-02-09 10:54:10 -05001608 msecs_to_jiffies(vc->vc_par[1]) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 else
1610 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1611 break;
1612 case 12: /* bring specified console to the front */
1613 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1614 set_console(vc->vc_par[1] - 1);
1615 break;
1616 case 13: /* unblank the screen */
1617 poke_blanked_console();
1618 break;
1619 case 14: /* set vesa powerdown interval */
1620 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1621 break;
1622 case 15: /* activate the previous console */
1623 set_console(last_console);
1624 break;
Scot Doylebd633642015-03-26 13:54:39 +00001625 case 16: /* set cursor blink duration in msec */
1626 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1627 vc->vc_par[1] <= USHRT_MAX)
1628 vc->vc_cur_blink_ms = vc->vc_par[1];
1629 else
1630 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1631 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633}
1634
Torben Hohnac751ef2011-01-25 15:07:35 -08001635/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static void csi_at(struct vc_data *vc, unsigned int nr)
1637{
1638 if (nr > vc->vc_cols - vc->vc_x)
1639 nr = vc->vc_cols - vc->vc_x;
1640 else if (!nr)
1641 nr = 1;
1642 insert_char(vc, nr);
1643}
1644
Torben Hohnac751ef2011-01-25 15:07:35 -08001645/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646static void csi_L(struct vc_data *vc, unsigned int nr)
1647{
1648 if (nr > vc->vc_rows - vc->vc_y)
1649 nr = vc->vc_rows - vc->vc_y;
1650 else if (!nr)
1651 nr = 1;
1652 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1653 vc->vc_need_wrap = 0;
1654}
1655
Torben Hohnac751ef2011-01-25 15:07:35 -08001656/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657static void csi_P(struct vc_data *vc, unsigned int nr)
1658{
1659 if (nr > vc->vc_cols - vc->vc_x)
1660 nr = vc->vc_cols - vc->vc_x;
1661 else if (!nr)
1662 nr = 1;
1663 delete_char(vc, nr);
1664}
1665
Torben Hohnac751ef2011-01-25 15:07:35 -08001666/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667static void csi_M(struct vc_data *vc, unsigned int nr)
1668{
1669 if (nr > vc->vc_rows - vc->vc_y)
1670 nr = vc->vc_rows - vc->vc_y;
1671 else if (!nr)
1672 nr=1;
1673 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1674 vc->vc_need_wrap = 0;
1675}
1676
Torben Hohnac751ef2011-01-25 15:07:35 -08001677/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678static void save_cur(struct vc_data *vc)
1679{
1680 vc->vc_saved_x = vc->vc_x;
1681 vc->vc_saved_y = vc->vc_y;
1682 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001683 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 vc->vc_s_underline = vc->vc_underline;
1685 vc->vc_s_blink = vc->vc_blink;
1686 vc->vc_s_reverse = vc->vc_reverse;
1687 vc->vc_s_charset = vc->vc_charset;
1688 vc->vc_s_color = vc->vc_color;
1689 vc->vc_saved_G0 = vc->vc_G0_charset;
1690 vc->vc_saved_G1 = vc->vc_G1_charset;
1691}
1692
Torben Hohnac751ef2011-01-25 15:07:35 -08001693/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694static void restore_cur(struct vc_data *vc)
1695{
1696 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1697 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001698 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 vc->vc_underline = vc->vc_s_underline;
1700 vc->vc_blink = vc->vc_s_blink;
1701 vc->vc_reverse = vc->vc_s_reverse;
1702 vc->vc_charset = vc->vc_s_charset;
1703 vc->vc_color = vc->vc_s_color;
1704 vc->vc_G0_charset = vc->vc_saved_G0;
1705 vc->vc_G1_charset = vc->vc_saved_G1;
1706 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1707 update_attr(vc);
1708 vc->vc_need_wrap = 0;
1709}
1710
Adam Borowskib290af62014-02-19 01:40:16 +01001711enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01001713 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714
Torben Hohnac751ef2011-01-25 15:07:35 -08001715/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716static void reset_terminal(struct vc_data *vc, int do_clear)
1717{
1718 vc->vc_top = 0;
1719 vc->vc_bottom = vc->vc_rows;
1720 vc->vc_state = ESnormal;
1721 vc->vc_ques = 0;
1722 vc->vc_translate = set_translate(LAT1_MAP, vc);
1723 vc->vc_G0_charset = LAT1_MAP;
1724 vc->vc_G1_charset = GRAF_MAP;
1725 vc->vc_charset = 0;
1726 vc->vc_need_wrap = 0;
1727 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001728 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 vc->vc_utf_count = 0;
1730
1731 vc->vc_disp_ctrl = 0;
1732 vc->vc_toggle_meta = 0;
1733
1734 vc->vc_decscnm = 0;
1735 vc->vc_decom = 0;
1736 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001737 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 vc->vc_decim = 0;
1739
Alan Cox079c9532012-02-28 14:49:23 +00001740 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001742 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 vc->vc_complement_mask = vc->vc_s_complement_mask;
1744
1745 default_attr(vc);
1746 update_attr(vc);
1747
Linus Torvalds7c280672018-03-24 10:43:26 +01001748 vc->vc_tab_stop[0] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 vc->vc_tab_stop[1] =
1750 vc->vc_tab_stop[2] =
1751 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001752 vc->vc_tab_stop[4] =
1753 vc->vc_tab_stop[5] =
1754 vc->vc_tab_stop[6] =
1755 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1758 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00001759 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 gotoxy(vc, 0, 0);
1762 save_cur(vc);
1763 if (do_clear)
1764 csi_J(vc, 2);
1765}
1766
Torben Hohnac751ef2011-01-25 15:07:35 -08001767/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1769{
1770 /*
1771 * Control characters can be used in the _middle_
1772 * of an escape sequence.
1773 */
Adam Borowski63f3a162014-02-19 01:38:04 +01001774 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1775 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 switch (c) {
1777 case 0:
1778 return;
1779 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01001780 if (vc->vc_state == ESosc)
1781 vc->vc_state = ESnormal;
1782 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1784 return;
1785 case 8:
1786 bs(vc);
1787 return;
1788 case 9:
1789 vc->vc_pos -= (vc->vc_x << 1);
1790 while (vc->vc_x < vc->vc_cols - 1) {
1791 vc->vc_x++;
Linus Torvalds7c280672018-03-24 10:43:26 +01001792 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 break;
1794 }
1795 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001796 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 return;
1798 case 10: case 11: case 12:
1799 lf(vc);
1800 if (!is_kbd(vc, lnm))
1801 return;
1802 case 13:
1803 cr(vc);
1804 return;
1805 case 14:
1806 vc->vc_charset = 1;
1807 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1808 vc->vc_disp_ctrl = 1;
1809 return;
1810 case 15:
1811 vc->vc_charset = 0;
1812 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1813 vc->vc_disp_ctrl = 0;
1814 return;
1815 case 24: case 26:
1816 vc->vc_state = ESnormal;
1817 return;
1818 case 27:
1819 vc->vc_state = ESesc;
1820 return;
1821 case 127:
1822 del(vc);
1823 return;
1824 case 128+27:
1825 vc->vc_state = ESsquare;
1826 return;
1827 }
1828 switch(vc->vc_state) {
1829 case ESesc:
1830 vc->vc_state = ESnormal;
1831 switch (c) {
1832 case '[':
1833 vc->vc_state = ESsquare;
1834 return;
1835 case ']':
1836 vc->vc_state = ESnonstd;
1837 return;
1838 case '%':
1839 vc->vc_state = ESpercent;
1840 return;
1841 case 'E':
1842 cr(vc);
1843 lf(vc);
1844 return;
1845 case 'M':
1846 ri(vc);
1847 return;
1848 case 'D':
1849 lf(vc);
1850 return;
1851 case 'H':
Linus Torvalds7c280672018-03-24 10:43:26 +01001852 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return;
1854 case 'Z':
1855 respond_ID(tty);
1856 return;
1857 case '7':
1858 save_cur(vc);
1859 return;
1860 case '8':
1861 restore_cur(vc);
1862 return;
1863 case '(':
1864 vc->vc_state = ESsetG0;
1865 return;
1866 case ')':
1867 vc->vc_state = ESsetG1;
1868 return;
1869 case '#':
1870 vc->vc_state = EShash;
1871 return;
1872 case 'c':
1873 reset_terminal(vc, 1);
1874 return;
1875 case '>': /* Numeric keypad */
1876 clr_kbd(vc, kbdapplic);
1877 return;
1878 case '=': /* Appl. keypad */
1879 set_kbd(vc, kbdapplic);
1880 return;
1881 }
1882 return;
1883 case ESnonstd:
1884 if (c=='P') { /* palette escape sequence */
1885 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1886 vc->vc_par[vc->vc_npar] = 0;
1887 vc->vc_npar = 0;
1888 vc->vc_state = ESpalette;
1889 return;
1890 } else if (c=='R') { /* reset palette */
1891 reset_palette(vc);
1892 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01001893 } else if (c>='0' && c<='9')
1894 vc->vc_state = ESosc;
1895 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 vc->vc_state = ESnormal;
1897 return;
1898 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001899 if (isxdigit(c)) {
1900 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 if (vc->vc_npar == 7) {
1902 int i = vc->vc_par[0] * 3, j = 1;
1903 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1904 vc->vc_palette[i++] += vc->vc_par[j++];
1905 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1906 vc->vc_palette[i++] += vc->vc_par[j++];
1907 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1908 vc->vc_palette[i] += vc->vc_par[j];
1909 set_palette(vc);
1910 vc->vc_state = ESnormal;
1911 }
1912 } else
1913 vc->vc_state = ESnormal;
1914 return;
1915 case ESsquare:
1916 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1917 vc->vc_par[vc->vc_npar] = 0;
1918 vc->vc_npar = 0;
1919 vc->vc_state = ESgetpars;
1920 if (c == '[') { /* Function key */
1921 vc->vc_state=ESfunckey;
1922 return;
1923 }
1924 vc->vc_ques = (c == '?');
1925 if (vc->vc_ques)
1926 return;
1927 case ESgetpars:
1928 if (c == ';' && vc->vc_npar < NPAR - 1) {
1929 vc->vc_npar++;
1930 return;
1931 } else if (c>='0' && c<='9') {
1932 vc->vc_par[vc->vc_npar] *= 10;
1933 vc->vc_par[vc->vc_npar] += c - '0';
1934 return;
Adam Borowskib290af62014-02-19 01:40:16 +01001935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 vc->vc_state = ESnormal;
1937 switch(c) {
1938 case 'h':
1939 set_mode(vc, 1);
1940 return;
1941 case 'l':
1942 set_mode(vc, 0);
1943 return;
1944 case 'c':
1945 if (vc->vc_ques) {
1946 if (vc->vc_par[0])
1947 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1948 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001949 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return;
1951 }
1952 break;
1953 case 'm':
1954 if (vc->vc_ques) {
1955 clear_selection();
1956 if (vc->vc_par[0])
1957 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1958 else
1959 vc->vc_complement_mask = vc->vc_s_complement_mask;
1960 return;
1961 }
1962 break;
1963 case 'n':
1964 if (!vc->vc_ques) {
1965 if (vc->vc_par[0] == 5)
1966 status_report(tty);
1967 else if (vc->vc_par[0] == 6)
1968 cursor_report(vc, tty);
1969 }
1970 return;
1971 }
1972 if (vc->vc_ques) {
1973 vc->vc_ques = 0;
1974 return;
1975 }
1976 switch(c) {
1977 case 'G': case '`':
1978 if (vc->vc_par[0])
1979 vc->vc_par[0]--;
1980 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1981 return;
1982 case 'A':
1983 if (!vc->vc_par[0])
1984 vc->vc_par[0]++;
1985 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1986 return;
1987 case 'B': case 'e':
1988 if (!vc->vc_par[0])
1989 vc->vc_par[0]++;
1990 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1991 return;
1992 case 'C': case 'a':
1993 if (!vc->vc_par[0])
1994 vc->vc_par[0]++;
1995 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1996 return;
1997 case 'D':
1998 if (!vc->vc_par[0])
1999 vc->vc_par[0]++;
2000 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
2001 return;
2002 case 'E':
2003 if (!vc->vc_par[0])
2004 vc->vc_par[0]++;
2005 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
2006 return;
2007 case 'F':
2008 if (!vc->vc_par[0])
2009 vc->vc_par[0]++;
2010 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
2011 return;
2012 case 'd':
2013 if (vc->vc_par[0])
2014 vc->vc_par[0]--;
2015 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2016 return;
2017 case 'H': case 'f':
2018 if (vc->vc_par[0])
2019 vc->vc_par[0]--;
2020 if (vc->vc_par[1])
2021 vc->vc_par[1]--;
2022 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2023 return;
2024 case 'J':
2025 csi_J(vc, vc->vc_par[0]);
2026 return;
2027 case 'K':
2028 csi_K(vc, vc->vc_par[0]);
2029 return;
2030 case 'L':
2031 csi_L(vc, vc->vc_par[0]);
2032 return;
2033 case 'M':
2034 csi_M(vc, vc->vc_par[0]);
2035 return;
2036 case 'P':
2037 csi_P(vc, vc->vc_par[0]);
2038 return;
2039 case 'c':
2040 if (!vc->vc_par[0])
2041 respond_ID(tty);
2042 return;
2043 case 'g':
2044 if (!vc->vc_par[0])
Linus Torvalds7c280672018-03-24 10:43:26 +01002045 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 else if (vc->vc_par[0] == 3) {
2047 vc->vc_tab_stop[0] =
2048 vc->vc_tab_stop[1] =
2049 vc->vc_tab_stop[2] =
2050 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08002051 vc->vc_tab_stop[4] =
2052 vc->vc_tab_stop[5] =
2053 vc->vc_tab_stop[6] =
2054 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 return;
2057 case 'm':
2058 csi_m(vc);
2059 return;
2060 case 'q': /* DECLL - but only 3 leds */
2061 /* map 0,1,2,3 to 0,1,2,4 */
2062 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002063 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2065 return;
2066 case 'r':
2067 if (!vc->vc_par[0])
2068 vc->vc_par[0]++;
2069 if (!vc->vc_par[1])
2070 vc->vc_par[1] = vc->vc_rows;
2071 /* Minimum allowed region is 2 lines */
2072 if (vc->vc_par[0] < vc->vc_par[1] &&
2073 vc->vc_par[1] <= vc->vc_rows) {
2074 vc->vc_top = vc->vc_par[0] - 1;
2075 vc->vc_bottom = vc->vc_par[1];
2076 gotoxay(vc, 0, 0);
2077 }
2078 return;
2079 case 's':
2080 save_cur(vc);
2081 return;
2082 case 'u':
2083 restore_cur(vc);
2084 return;
2085 case 'X':
2086 csi_X(vc, vc->vc_par[0]);
2087 return;
2088 case '@':
2089 csi_at(vc, vc->vc_par[0]);
2090 return;
2091 case ']': /* setterm functions */
2092 setterm_command(vc);
2093 return;
2094 }
2095 return;
2096 case ESpercent:
2097 vc->vc_state = ESnormal;
2098 switch (c) {
2099 case '@': /* defined in ISO 2022 */
2100 vc->vc_utf = 0;
2101 return;
2102 case 'G': /* prelim official escape code */
2103 case '8': /* retained for compatibility */
2104 vc->vc_utf = 1;
2105 return;
2106 }
2107 return;
2108 case ESfunckey:
2109 vc->vc_state = ESnormal;
2110 return;
2111 case EShash:
2112 vc->vc_state = ESnormal;
2113 if (c == '8') {
2114 /* DEC screen alignment test. kludge :-) */
2115 vc->vc_video_erase_char =
2116 (vc->vc_video_erase_char & 0xff00) | 'E';
2117 csi_J(vc, 2);
2118 vc->vc_video_erase_char =
2119 (vc->vc_video_erase_char & 0xff00) | ' ';
2120 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2121 }
2122 return;
2123 case ESsetG0:
2124 if (c == '0')
2125 vc->vc_G0_charset = GRAF_MAP;
2126 else if (c == 'B')
2127 vc->vc_G0_charset = LAT1_MAP;
2128 else if (c == 'U')
2129 vc->vc_G0_charset = IBMPC_MAP;
2130 else if (c == 'K')
2131 vc->vc_G0_charset = USER_MAP;
2132 if (vc->vc_charset == 0)
2133 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2134 vc->vc_state = ESnormal;
2135 return;
2136 case ESsetG1:
2137 if (c == '0')
2138 vc->vc_G1_charset = GRAF_MAP;
2139 else if (c == 'B')
2140 vc->vc_G1_charset = LAT1_MAP;
2141 else if (c == 'U')
2142 vc->vc_G1_charset = IBMPC_MAP;
2143 else if (c == 'K')
2144 vc->vc_G1_charset = USER_MAP;
2145 if (vc->vc_charset == 1)
2146 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2147 vc->vc_state = ESnormal;
2148 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002149 case ESosc:
2150 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 default:
2152 vc->vc_state = ESnormal;
2153 }
2154}
2155
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002156/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002157 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002158 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2159 */
2160struct interval {
2161 uint32_t first;
2162 uint32_t last;
2163};
2164
2165static int bisearch(uint32_t ucs, const struct interval *table, int max)
2166{
2167 int min = 0;
2168 int mid;
2169
2170 if (ucs < table[0].first || ucs > table[max].last)
2171 return 0;
2172 while (max >= min) {
2173 mid = (min + max) / 2;
2174 if (ucs > table[mid].last)
2175 min = mid + 1;
2176 else if (ucs < table[mid].first)
2177 max = mid - 1;
2178 else
2179 return 1;
2180 }
2181 return 0;
2182}
2183
2184static int is_double_width(uint32_t ucs)
2185{
2186 static const struct interval double_width[] = {
2187 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2188 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002189 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2190 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002191 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002192 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002193}
2194
Jiri Slabyd711ea82016-06-23 13:34:30 +02002195static void con_flush(struct vc_data *vc, unsigned long draw_from,
2196 unsigned long draw_to, int *draw_x)
2197{
2198 if (*draw_x < 0)
2199 return;
2200
2201 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2202 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2203 *draw_x = -1;
2204}
2205
Torben Hohnac751ef2011-01-25 15:07:35 -08002206/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2208{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 int c, tc, ok, n = 0, draw_x = -1;
2210 unsigned int currcons;
2211 unsigned long draw_from = 0, draw_to = 0;
2212 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002213 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002214 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002215 uint8_t rescan;
2216 uint8_t inverse;
2217 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219
2220 if (in_interrupt())
2221 return count;
2222
2223 might_sleep();
2224
Torben Hohnac751ef2011-01-25 15:07:35 -08002225 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 vc = tty->driver_data;
2227 if (vc == NULL) {
2228 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002229 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 return 0;
2231 }
2232
2233 currcons = vc->vc_num;
2234 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002235 /* could this happen? */
2236 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2237 console_unlock();
2238 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 himask = vc->vc_hi_font_mask;
2242 charmask = himask ? 0x1ff : 0xff;
2243
2244 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002245 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 hide_cursor(vc);
2247
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002248 param.vc = vc;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 while (!tty->stopped && count) {
2251 int orig = *buf;
2252 c = orig;
2253 buf++;
2254 n++;
2255 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002256 rescan = 0;
2257 inverse = 0;
2258 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 /* Do no translation at all in control states */
2261 if (vc->vc_state != ESnormal) {
2262 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002263 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002264 /* Combine UTF-8 into Unicode in vc_utf_char.
2265 * vc_utf_count is the number of continuation bytes still
2266 * expected to arrive.
2267 * vc_npar is the number of continuation bytes arrived so
2268 * far
2269 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002270rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002271 if ((c & 0xc0) == 0x80) {
2272 /* Continuation byte received */
2273 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002274 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002275 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2276 vc->vc_npar++;
2277 if (--vc->vc_utf_count) {
2278 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002280 }
2281 /* Got a whole character */
2282 c = vc->vc_utf_char;
2283 /* Reject overlong sequences */
2284 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2285 c > utf8_length_changes[vc->vc_npar])
2286 c = 0xfffd;
2287 } else {
2288 /* Unexpected continuation byte */
2289 vc->vc_utf_count = 0;
2290 c = 0xfffd;
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002293 /* Single ASCII byte or first byte of a sequence received */
2294 if (vc->vc_utf_count) {
2295 /* Continuation byte expected */
2296 rescan = 1;
2297 vc->vc_utf_count = 0;
2298 c = 0xfffd;
2299 } else if (c > 0x7f) {
2300 /* First byte of a multibyte sequence received */
2301 vc->vc_npar = 0;
2302 if ((c & 0xe0) == 0xc0) {
2303 vc->vc_utf_count = 1;
2304 vc->vc_utf_char = (c & 0x1f);
2305 } else if ((c & 0xf0) == 0xe0) {
2306 vc->vc_utf_count = 2;
2307 vc->vc_utf_char = (c & 0x0f);
2308 } else if ((c & 0xf8) == 0xf0) {
2309 vc->vc_utf_count = 3;
2310 vc->vc_utf_char = (c & 0x07);
2311 } else if ((c & 0xfc) == 0xf8) {
2312 vc->vc_utf_count = 4;
2313 vc->vc_utf_char = (c & 0x03);
2314 } else if ((c & 0xfe) == 0xfc) {
2315 vc->vc_utf_count = 5;
2316 vc->vc_utf_char = (c & 0x01);
2317 } else {
2318 /* 254 and 255 are invalid */
2319 c = 0xfffd;
2320 }
2321 if (vc->vc_utf_count) {
2322 /* Still need some bytes */
2323 continue;
2324 }
2325 }
2326 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002328 /* End of UTF-8 decoding. */
2329 /* c is the received character, or U+FFFD for invalid sequences. */
2330 /* Replace invalid Unicode code points with U+FFFD too */
2331 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2332 c = 0xfffd;
2333 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002334 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002335 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 }
2337
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002338 param.c = tc;
2339 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2340 &param) == NOTIFY_STOP)
2341 continue;
2342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /* If the original code was a control character we
2344 * only allow a glyph to be displayed if the code is
2345 * not normally used (such as for cursor movement) or
2346 * if the disp_ctrl mode has been explicitly enabled.
2347 * Certain characters (as given by the CTRL_ALWAYS
2348 * bitmap) are always displayed as control characters,
2349 * as the console would be pretty useless without
2350 * them; to display an arbitrary font position use the
2351 * direct-to-font zone in UTF-8 mode.
2352 */
2353 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002354 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2355 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 && (c != 127 || vc->vc_disp_ctrl)
2357 && (c != 128+27);
2358
2359 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002360 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2361 if (is_double_width(c))
2362 width = 2;
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /* Now try to find out how to display it */
2365 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002366 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002367 if (tc == -1 || tc == -2) {
2368 continue; /* nothing to display */
2369 }
2370 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002371 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002372 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002373 This would make absolutely no sense with Unicode in mind,
2374 but do this for ASCII characters since a font may lack
2375 Unicode mapping info and we don't want to end up with
2376 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002377 tc = c;
2378 } else {
2379 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2380 tc = conv_uni_to_pc(vc, 0xfffd);
2381 if (tc < 0) {
2382 inverse = 1;
2383 tc = conv_uni_to_pc(vc, '?');
2384 if (tc < 0) tc = '?';
2385 }
2386 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002389 if (!inverse) {
2390 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002392 /* invert vc_attr */
2393 if (!vc->vc_can_do_color) {
2394 vc_attr = (vc->vc_attr) ^ 0x08;
2395 } else if (vc->vc_hi_font_mask == 0x100) {
2396 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2397 } else {
2398 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002399 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002400 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002401 }
2402
2403 while (1) {
2404 if (vc->vc_need_wrap || vc->vc_decim)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002405 con_flush(vc, draw_from, draw_to,
2406 &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002407 if (vc->vc_need_wrap) {
2408 cr(vc);
2409 lf(vc);
2410 }
2411 if (vc->vc_decim)
2412 insert_char(vc, 1);
2413 scr_writew(himask ?
2414 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2415 (vc_attr << 8) + tc,
2416 (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002417 if (con_should_update(vc) && draw_x < 0) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002418 draw_x = vc->vc_x;
2419 draw_from = vc->vc_pos;
2420 }
2421 if (vc->vc_x == vc->vc_cols - 1) {
2422 vc->vc_need_wrap = vc->vc_decawm;
2423 draw_to = vc->vc_pos + 2;
2424 } else {
2425 vc->vc_x++;
2426 draw_to = (vc->vc_pos += 2);
2427 }
2428
2429 if (!--width) break;
2430
2431 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2432 if (tc < 0) tc = ' ';
2433 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002434 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002435
Jiri Slabyd711ea82016-06-23 13:34:30 +02002436 if (inverse)
2437 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002438
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002439 if (rescan) {
2440 rescan = 0;
2441 inverse = 0;
2442 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002443 c = orig;
2444 goto rescan_last_byte;
2445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 continue;
2447 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002448 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 do_con_trol(tty, vc, orig);
2450 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002451 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002453 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002454 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456}
2457
2458/*
2459 * This is the console switching callback.
2460 *
2461 * Doing console switching in a process context allows
2462 * us to do the switches asynchronously (needed when we want
2463 * to switch due to a keyboard interrupt). Synchronization
2464 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002465 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 */
David Howells65f27f32006-11-22 14:55:48 +00002467static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Torben Hohnac751ef2011-01-25 15:07:35 -08002469 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
2471 if (want_console >= 0) {
2472 if (want_console != fg_console &&
2473 vc_cons_allocated(want_console)) {
2474 hide_cursor(vc_cons[fg_console].d);
2475 change_console(vc_cons[want_console].d);
2476 /* we only changed when the console had already
2477 been allocated - a new console is not created
2478 in an interrupt routine */
2479 }
2480 want_console = -1;
2481 }
2482 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2483 do_poke_blanked_console = 0;
2484 poke_blanked_console();
2485 }
2486 if (scrollback_delta) {
2487 struct vc_data *vc = vc_cons[fg_console].d;
2488 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002489 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2491 scrollback_delta = 0;
2492 }
2493 if (blank_timer_expired) {
2494 do_blank_screen(0);
2495 blank_timer_expired = 0;
2496 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002497 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
Torben Hohnac751ef2011-01-25 15:07:35 -08002499 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002502int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002504 struct vc_data *vc = vc_cons[fg_console].d;
2505
2506 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2507 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2508
2509 /*
2510 * Console switch will fail in console_callback() or
2511 * change_console() so there is no point scheduling
2512 * the callback
2513 *
2514 * Existing set_console() users don't check the return
2515 * value so this shouldn't break anything
2516 */
2517 return -EINVAL;
2518 }
2519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 want_console = nr;
2521 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002522
2523 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524}
2525
2526struct tty_driver *console_driver;
2527
2528#ifdef CONFIG_VT_CONSOLE
2529
Bernhard Walle5ada9182009-12-14 18:00:43 -08002530/**
2531 * vt_kmsg_redirect() - Sets/gets the kernel message console
2532 * @new: The new virtual terminal number or -1 if the console should stay
2533 * unchanged
2534 *
2535 * By default, the kernel messages are always printed on the current virtual
2536 * console. However, the user may modify that default with the
2537 * TIOCL_SETKMSGREDIRECT ioctl call.
2538 *
2539 * This function sets the kernel message console to be @new. It returns the old
2540 * virtual console number. The virtual terminal number 0 (both as parameter and
2541 * return value) means no redirection (i.e. always printed on the currently
2542 * active console).
2543 *
2544 * The parameter -1 means that only the current console is returned, but the
2545 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2546 * case to make the code more understandable.
2547 *
2548 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2549 * the parameter and always returns 0.
2550 */
2551int vt_kmsg_redirect(int new)
2552{
2553 static int kmsg_con;
2554
2555 if (new != -1)
2556 return xchg(&kmsg_con, new);
2557 else
2558 return kmsg_con;
2559}
2560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561/*
2562 * Console on virtual terminal
2563 *
2564 * The console must be locked when we get here.
2565 */
2566
2567static void vt_console_print(struct console *co, const char *b, unsigned count)
2568{
2569 struct vc_data *vc = vc_cons[fg_console].d;
2570 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002571 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 const ushort *start;
2573 ushort cnt = 0;
2574 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002575 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002578 if (!printable)
2579 return;
2580 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 return;
2582
Bernhard Walle5ada9182009-12-14 18:00:43 -08002583 kmsg_console = vt_get_kmsg_redirect();
2584 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2585 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
2587 /* read `x' only after setting currcons properly (otherwise
2588 the `x' macro will read the x of the foreground console). */
2589 myx = vc->vc_x;
2590
2591 if (!vc_cons_allocated(fg_console)) {
2592 /* impossible */
2593 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2594 goto quit;
2595 }
2596
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002597 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 goto quit;
2599
2600 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002601 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 hide_cursor(vc);
2603
2604 start = (ushort *)vc->vc_pos;
2605
2606 /* Contrived structure to try to emulate original need_wrap behaviour
2607 * Problems caused when we have need_wrap set on '\n' character */
2608 while (count--) {
2609 c = *b++;
2610 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2611 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002612 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2614 vc->vc_x += cnt;
2615 if (vc->vc_need_wrap)
2616 vc->vc_x--;
2617 cnt = 0;
2618 }
2619 if (c == 8) { /* backspace */
2620 bs(vc);
2621 start = (ushort *)vc->vc_pos;
2622 myx = vc->vc_x;
2623 continue;
2624 }
2625 if (c != 13)
2626 lf(vc);
2627 cr(vc);
2628 start = (ushort *)vc->vc_pos;
2629 myx = vc->vc_x;
2630 if (c == 10 || c == 13)
2631 continue;
2632 }
2633 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002634 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 cnt++;
2636 if (myx == vc->vc_cols - 1) {
2637 vc->vc_need_wrap = 1;
2638 continue;
2639 }
2640 vc->vc_pos += 2;
2641 myx++;
2642 }
2643 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002644 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2646 vc->vc_x += cnt;
2647 if (vc->vc_x == vc->vc_cols) {
2648 vc->vc_x--;
2649 vc->vc_need_wrap = 1;
2650 }
2651 }
2652 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002653 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002656 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657}
2658
2659static struct tty_driver *vt_console_device(struct console *c, int *index)
2660{
2661 *index = c->index ? c->index-1 : fg_console;
2662 return console_driver;
2663}
2664
2665static struct console vt_console_driver = {
2666 .name = "tty",
2667 .write = vt_console_print,
2668 .device = vt_console_device,
2669 .unblank = unblank_screen,
2670 .flags = CON_PRINTBUFFER,
2671 .index = -1,
2672};
2673#endif
2674
2675/*
2676 * Handling of Linux-specific VC ioctls
2677 */
2678
2679/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002680 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 *
2682 * There are some functions which don't need it.
2683 *
2684 * There are some functions which can sleep for arbitrary periods
2685 * (paste_selection) but we don't need the lock there anyway.
2686 *
2687 * set_selection has locking, and definitely needs it
2688 */
2689
2690int tioclinux(struct tty_struct *tty, unsigned long arg)
2691{
2692 char type, data;
2693 char __user *p = (char __user *)arg;
2694 int lines;
2695 int ret;
2696
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2698 return -EPERM;
2699 if (get_user(type, p))
2700 return -EFAULT;
2701 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 switch (type)
2704 {
2705 case TIOCL_SETSEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case TIOCL_PASTESEL:
2709 ret = paste_selection(tty);
2710 break;
2711 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002712 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002714 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 break;
2716 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00002717 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00002719 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 break;
2721 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 /*
2724 * Make it possible to react to Shift+Mousebutton.
2725 * Note that 'shift_state' is an undocumented
2726 * kernel-internal variable; programs not closely
2727 * related to the kernel should not use this.
2728 */
Alan Cox079c9532012-02-28 14:49:23 +00002729 data = vt_get_shift_state();
Adam Borowski63c634c2017-06-03 09:35:06 +02002730 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 break;
2732 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00002733 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00002735 console_unlock();
Adam Borowski63c634c2017-06-03 09:35:06 +02002736 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 break;
2738 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00002739 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002740 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00002741 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002743 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002744 data = vt_get_kmsg_redirect();
Adam Borowski63c634c2017-06-03 09:35:06 +02002745 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002746 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 case TIOCL_SETKMSGREDIRECT:
2748 if (!capable(CAP_SYS_ADMIN)) {
2749 ret = -EPERM;
2750 } else {
2751 if (get_user(data, p+1))
2752 ret = -EFAULT;
2753 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002754 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
2756 break;
2757 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00002758 /* No locking needed as this is a transiently
2759 correct return anyway if the caller hasn't
2760 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 ret = fg_console;
2762 break;
2763 case TIOCL_SCROLLCONSOLE:
2764 if (get_user(lines, (s32 __user *)(p+4))) {
2765 ret = -EFAULT;
2766 } else {
Alan Cox20f62572012-03-02 14:59:37 +00002767 /* Need the console lock here. Note that lots
2768 of other calls need fixing before the lock
2769 is actually useful ! */
2770 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00002772 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 ret = 0;
2774 }
2775 break;
2776 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08002777 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 ignore_poke = 1;
2779 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08002780 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 break;
2782 case TIOCL_BLANKEDSCREEN:
2783 ret = console_blanked;
2784 break;
2785 default:
2786 ret = -EINVAL;
2787 break;
2788 }
2789 return ret;
2790}
2791
2792/*
2793 * /dev/ttyN handling
2794 */
2795
2796static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2797{
2798 int retval;
2799
2800 retval = do_con_write(tty, buf, count);
2801 con_flush_chars(tty);
2802
2803 return retval;
2804}
2805
Alan Cox5d19f542008-04-30 00:54:08 -07002806static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807{
2808 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002809 return 0; /* n_r3964 calls put_char() from interrupt context */
2810 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811}
2812
2813static int con_write_room(struct tty_struct *tty)
2814{
2815 if (tty->stopped)
2816 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002817 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
2820static int con_chars_in_buffer(struct tty_struct *tty)
2821{
2822 return 0; /* we're not buffering */
2823}
2824
2825/*
2826 * con_throttle and con_unthrottle are only used for
2827 * paste_selection(), which has to stuff in a large number of
2828 * characters...
2829 */
2830static void con_throttle(struct tty_struct *tty)
2831{
2832}
2833
2834static void con_unthrottle(struct tty_struct *tty)
2835{
2836 struct vc_data *vc = tty->driver_data;
2837
2838 wake_up_interruptible(&vc->paste_wait);
2839}
2840
2841/*
2842 * Turn the Scroll-Lock LED on when the tty is stopped
2843 */
2844static void con_stop(struct tty_struct *tty)
2845{
2846 int console_num;
2847 if (!tty)
2848 return;
2849 console_num = tty->index;
2850 if (!vc_cons_allocated(console_num))
2851 return;
Alan Cox079c9532012-02-28 14:49:23 +00002852 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853}
2854
2855/*
2856 * Turn the Scroll-Lock LED off when the console is started
2857 */
2858static void con_start(struct tty_struct *tty)
2859{
2860 int console_num;
2861 if (!tty)
2862 return;
2863 console_num = tty->index;
2864 if (!vc_cons_allocated(console_num))
2865 return;
Alan Cox079c9532012-02-28 14:49:23 +00002866 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867}
2868
2869static void con_flush_chars(struct tty_struct *tty)
2870{
2871 struct vc_data *vc;
2872
2873 if (in_interrupt()) /* from flush_to_ldisc */
2874 return;
2875
2876 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08002877 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 vc = tty->driver_data;
2879 if (vc)
2880 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08002881 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/*
2885 * Allocate the console screen memory.
2886 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002887static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
2889 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002890 struct vc_data *vc;
2891 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Torben Hohnac751ef2011-01-25 15:07:35 -08002893 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002894 ret = vc_allocate(currcons);
2895 if (ret)
2896 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01002897
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002898 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002900 /* Still being freed */
2901 if (vc->port.tty) {
2902 ret = -ERESTARTSYS;
2903 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002905
2906 ret = tty_port_install(&vc->port, driver, tty);
2907 if (ret)
2908 goto unlock;
2909
2910 tty->driver_data = vc;
2911 vc->port.tty = tty;
Eric Biggers6bc9bf72020-03-21 20:43:04 -07002912 tty_port_get(&vc->port);
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002913
2914 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2915 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2916 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2917 }
2918 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01002919 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002920 else
Alan Coxadc8d742012-07-14 15:31:47 +01002921 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002922unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08002923 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return ret;
2925}
2926
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002927static int con_open(struct tty_struct *tty, struct file *filp)
2928{
2929 /* everything done in install */
2930 return 0;
2931}
2932
2933
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934static void con_close(struct tty_struct *tty, struct file *filp)
2935{
Alan Coxfeebed62008-10-13 10:41:30 +01002936 /* Nothing to do - we defer to shutdown */
2937}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Alan Coxfeebed62008-10-13 10:41:30 +01002939static void con_shutdown(struct tty_struct *tty)
2940{
2941 struct vc_data *vc = tty->driver_data;
2942 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08002943 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02002944 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08002945 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946}
2947
Eric Biggers6bc9bf72020-03-21 20:43:04 -07002948static void con_cleanup(struct tty_struct *tty)
2949{
2950 struct vc_data *vc = tty->driver_data;
2951
2952 tty_port_put(&vc->port);
2953}
2954
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002955static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002956static int default_italic_color = 2; // green (ASCII)
2957static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002958module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002959module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2960module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2961
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962static void vc_init(struct vc_data *vc, unsigned int rows,
2963 unsigned int cols, int do_clear)
2964{
2965 int j, k ;
2966
2967 vc->vc_cols = cols;
2968 vc->vc_rows = rows;
2969 vc->vc_size_row = cols << 1;
2970 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2971
2972 set_origin(vc);
2973 vc->vc_pos = vc->vc_origin;
2974 reset_vc(vc);
2975 for (j=k=0; j<16; j++) {
2976 vc->vc_palette[k++] = default_red[j] ;
2977 vc->vc_palette[k++] = default_grn[j] ;
2978 vc->vc_palette[k++] = default_blu[j] ;
2979 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002980 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002981 vc->vc_ulcolor = default_underline_color;
2982 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 vc->vc_halfcolor = 0x08; /* grey */
2984 init_waitqueue_head(&vc->paste_wait);
2985 reset_terminal(vc, do_clear);
2986}
2987
2988/*
2989 * This routine initializes console interrupts, and does nothing
2990 * else. If you want the screen to clear, call tty_write with
2991 * the appropriate escape-sequence.
2992 */
2993
2994static int __init con_init(void)
2995{
2996 const char *display_desc = NULL;
2997 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002998 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
Torben Hohnac751ef2011-01-25 15:07:35 -08003000 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 if (conswitchp)
3003 display_desc = conswitchp->con_startup();
3004 if (!display_desc) {
3005 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08003006 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 return 0;
3008 }
3009
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003010 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3011 struct con_driver *con_driver = &registered_con_driver[i];
3012
3013 if (con_driver->con == NULL) {
3014 con_driver->con = conswitchp;
3015 con_driver->desc = display_desc;
3016 con_driver->flag = CON_DRIVER_FLAG_INIT;
3017 con_driver->first = 0;
3018 con_driver->last = MAX_NR_CONSOLES - 1;
3019 break;
3020 }
3021 }
3022
3023 for (i = 0; i < MAX_NR_CONSOLES; i++)
3024 con_driver_map[i] = conswitchp;
3025
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (blankinterval) {
3027 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003028 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003032 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003033 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003034 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003036 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 vc_init(vc, vc->vc_rows, vc->vc_cols,
3038 currcons || !vc->vc_sw->con_save_screen);
3039 }
3040 currcons = fg_console = 0;
3041 master_display_fg = vc = vc_cons[currcons].d;
3042 set_origin(vc);
3043 save_screen(vc);
3044 gotoxy(vc, vc->vc_x, vc->vc_y);
3045 csi_J(vc, 0);
3046 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003047 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 vc->vc_can_do_color ? "colour" : "mono",
3049 display_desc, vc->vc_cols, vc->vc_rows);
3050 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Torben Hohnac751ef2011-01-25 15:07:35 -08003052 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
3054#ifdef CONFIG_VT_CONSOLE
3055 register_console(&vt_console_driver);
3056#endif
3057 return 0;
3058}
3059console_initcall(con_init);
3060
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003061static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003062 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .open = con_open,
3064 .close = con_close,
3065 .write = con_write,
3066 .write_room = con_write_room,
3067 .put_char = con_put_char,
3068 .flush_chars = con_flush_chars,
3069 .chars_in_buffer = con_chars_in_buffer,
3070 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003071#ifdef CONFIG_COMPAT
3072 .compat_ioctl = vt_compat_ioctl,
3073#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .stop = con_stop,
3075 .start = con_start,
3076 .throttle = con_throttle,
3077 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003078 .resize = vt_resize,
Eric Biggers6bc9bf72020-03-21 20:43:04 -07003079 .shutdown = con_shutdown,
3080 .cleanup = con_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081};
3082
Alan Coxd81ed102008-10-13 10:41:42 +01003083static struct cdev vc0_cdev;
3084
Kay Sieversfbc92a32010-12-01 18:51:05 +01003085static ssize_t show_tty_active(struct device *dev,
3086 struct device_attribute *attr, char *buf)
3087{
3088 return sprintf(buf, "tty%d\n", fg_console + 1);
3089}
3090static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3091
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003092static struct attribute *vt_dev_attrs[] = {
3093 &dev_attr_active.attr,
3094 NULL
3095};
3096
3097ATTRIBUTE_GROUPS(vt_dev);
3098
Alan Coxd81ed102008-10-13 10:41:42 +01003099int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100{
Alan Coxd81ed102008-10-13 10:41:42 +01003101 cdev_init(&vc0_cdev, console_fops);
3102 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3103 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3104 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003105 tty0dev = device_create_with_groups(tty_class, NULL,
3106 MKDEV(TTY_MAJOR, 0), NULL,
3107 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003108 if (IS_ERR(tty0dev))
3109 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 vcs_init();
3112
3113 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3114 if (!console_driver)
3115 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 console_driver->name = "tty";
3118 console_driver->name_base = 1;
3119 console_driver->major = TTY_MAJOR;
3120 console_driver->minor_start = 1;
3121 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3122 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003123 if (default_utf8)
3124 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3126 tty_set_operations(console_driver, &con_ops);
3127 if (tty_register_driver(console_driver))
3128 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 kbd_init();
3130 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131#ifdef CONFIG_MDA_CONSOLE
3132 mda_console_init();
3133#endif
3134 return 0;
3135}
3136
3137#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003138
3139static struct class *vtconsole_class;
3140
Alan Cox50e244c2013-01-25 10:28:15 +10003141static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003142 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003144 struct module *owner = csw->owner;
3145 const char *desc = NULL;
3146 struct con_driver *con_driver;
3147 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 if (!try_module_get(owner))
3150 return -ENODEV;
3151
Alan Cox50e244c2013-01-25 10:28:15 +10003152 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003153
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003154 /* check if driver is registered */
3155 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3156 con_driver = &registered_con_driver[i];
3157
3158 if (con_driver->con == csw) {
3159 desc = con_driver->desc;
3160 retval = 0;
3161 break;
3162 }
3163 }
3164
3165 if (retval)
3166 goto err;
3167
3168 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3169 csw->con_startup();
3170 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 if (deflt) {
3174 if (conswitchp)
3175 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003176
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 __module_get(owner);
3178 conswitchp = csw;
3179 }
3180
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003181 first = max(first, con_driver->first);
3182 last = min(last, con_driver->last);
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 for (i = first; i <= last; i++) {
3185 int old_was_color;
3186 struct vc_data *vc = vc_cons[i].d;
3187
3188 if (con_driver_map[i])
3189 module_put(con_driver_map[i]->owner);
3190 __module_get(owner);
3191 con_driver_map[i] = csw;
3192
3193 if (!vc || !vc->vc_sw)
3194 continue;
3195
3196 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003197
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003198 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003199 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003201 }
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 old_was_color = vc->vc_can_do_color;
3204 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003205 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003207 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 update_attr(vc);
3209
3210 /* If the console changed between mono <-> color, then
3211 * the attributes in the screenbuf will be wrong. The
3212 * following resets all attributes to something sane.
3213 */
3214 if (old_was_color != vc->vc_can_do_color)
3215 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003217
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003218 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (!deflt)
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003220 printk(KERN_CONT "consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003221 if (j >= 0) {
3222 struct vc_data *vc = vc_cons[j].d;
3223
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003224 printk(KERN_CONT "to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003225 vc->vc_can_do_color ? "colour" : "mono",
3226 desc, vc->vc_cols, vc->vc_rows);
3227
3228 if (k >= 0) {
3229 vc = vc_cons[k].d;
3230 update_screen(vc);
3231 }
3232 } else
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003233 printk(KERN_CONT "to %s\n", desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003235 retval = 0;
3236err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003238 return retval;
3239};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Alan Cox50e244c2013-01-25 10:28:15 +10003241
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003242#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003243/* unlocked version of unbind_con_driver() */
3244int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3245{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003246 struct module *owner = csw->owner;
3247 const struct consw *defcsw = NULL;
3248 struct con_driver *con_driver = NULL, *con_back = NULL;
3249 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003251 if (!try_module_get(owner))
3252 return -ENODEV;
3253
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003254 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003255
3256 /* check if driver is registered and if it is unbindable */
3257 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3258 con_driver = &registered_con_driver[i];
3259
3260 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003261 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003262 retval = 0;
3263 break;
3264 }
3265 }
3266
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003267 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003268 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003269
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003270 retval = -ENODEV;
3271
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003272 /* check if backup driver exists */
3273 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3274 con_back = &registered_con_driver[i];
3275
Daniel Vetter249f7b32014-06-05 16:24:47 +02003276 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003277 defcsw = con_back->con;
3278 retval = 0;
3279 break;
3280 }
3281 }
3282
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003283 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003284 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003285
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003286 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003287 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003288
3289 first = max(first, con_driver->first);
3290 last = min(last, con_driver->last);
3291
3292 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 if (con_driver_map[i] == csw) {
3294 module_put(csw->owner);
3295 con_driver_map[i] = NULL;
3296 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003297 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003298
3299 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003300 const struct consw *defconsw = conswitchp;
3301
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003302 defcsw->con_startup();
3303 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003304 /*
3305 * vgacon may change the default driver to point
3306 * to dummycon, we restore it here...
3307 */
3308 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003309 }
3310
3311 if (!con_is_bound(csw))
3312 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3313
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003314 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003315 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003316err:
3317 module_put(owner);
3318 return retval;
3319
3320}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003321EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003322
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003323static int vt_bind(struct con_driver *con)
3324{
3325 const struct consw *defcsw = NULL, *csw = NULL;
3326 int i, more = 1, first = -1, last = -1, deflt = 0;
3327
Daniel Vetter77232f72015-04-13 11:16:21 +02003328 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003329 goto err;
3330
3331 csw = con->con;
3332
3333 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3334 struct con_driver *con = &registered_con_driver[i];
3335
3336 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3337 defcsw = con->con;
3338 break;
3339 }
3340 }
3341
3342 if (!defcsw)
3343 goto err;
3344
3345 while (more) {
3346 more = 0;
3347
3348 for (i = con->first; i <= con->last; i++) {
3349 if (con_driver_map[i] == defcsw) {
3350 if (first == -1)
3351 first = i;
3352 last = i;
3353 more = 1;
3354 } else if (first != -1)
3355 break;
3356 }
3357
3358 if (first == 0 && last == MAX_NR_CONSOLES -1)
3359 deflt = 1;
3360
Imre Deak4c215fe2014-12-16 00:16:00 +02003361 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003362 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003363
3364 first = -1;
3365 last = -1;
3366 deflt = 0;
3367 }
3368
3369err:
3370 return 0;
3371}
3372
3373static int vt_unbind(struct con_driver *con)
3374{
3375 const struct consw *csw = NULL;
3376 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003377 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003378
Daniel Vetter77232f72015-04-13 11:16:21 +02003379 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003380 goto err;
3381
3382 csw = con->con;
3383
3384 while (more) {
3385 more = 0;
3386
3387 for (i = con->first; i <= con->last; i++) {
3388 if (con_driver_map[i] == csw) {
3389 if (first == -1)
3390 first = i;
3391 last = i;
3392 more = 1;
3393 } else if (first != -1)
3394 break;
3395 }
3396
3397 if (first == 0 && last == MAX_NR_CONSOLES -1)
3398 deflt = 1;
3399
Wang YanQing618f2b92013-05-09 02:14:07 +08003400 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003401 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003402 if (ret != 0)
3403 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003404 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003405
3406 first = -1;
3407 last = -1;
3408 deflt = 0;
3409 }
3410
3411err:
3412 return 0;
3413}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003414#else
3415static inline int vt_bind(struct con_driver *con)
3416{
3417 return 0;
3418}
3419static inline int vt_unbind(struct con_driver *con)
3420{
3421 return 0;
3422}
3423#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003424
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003425static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003426 const char *buf, size_t count)
3427{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003428 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003429 int bind = simple_strtoul(buf, NULL, 0);
3430
Imre Deak4c215fe2014-12-16 00:16:00 +02003431 console_lock();
3432
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003433 if (bind)
3434 vt_bind(con);
3435 else
3436 vt_unbind(con);
3437
Imre Deak4c215fe2014-12-16 00:16:00 +02003438 console_unlock();
3439
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003440 return count;
3441}
3442
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003443static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3444 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003445{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003446 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003447 int bind = con_is_bound(con->con);
3448
3449 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3450}
3451
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003452static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3453 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003454{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003455 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003456
3457 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3458 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3459 con->desc);
3460
3461}
3462
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003463static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3464static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3465
3466static struct attribute *con_dev_attrs[] = {
3467 &dev_attr_bind.attr,
3468 &dev_attr_name.attr,
3469 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003470};
3471
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003472ATTRIBUTE_GROUPS(con_dev);
3473
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003474static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003475{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003476 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003477 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003478}
3479
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003480static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003481{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003482 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003483}
3484
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003485/**
3486 * con_is_bound - checks if driver is bound to the console
3487 * @csw: console driver
3488 *
3489 * RETURNS: zero if unbound, nonzero if bound
3490 *
3491 * Drivers can call this and if zero, they should release
3492 * all resources allocated on con_startup()
3493 */
3494int con_is_bound(const struct consw *csw)
3495{
3496 int i, bound = 0;
3497
3498 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3499 if (con_driver_map[i] == csw) {
3500 bound = 1;
3501 break;
3502 }
3503 }
3504
3505 return bound;
3506}
3507EXPORT_SYMBOL(con_is_bound);
3508
3509/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003510 * con_debug_enter - prepare the console for the kernel debugger
3511 * @sw: console driver
3512 *
3513 * Called when the console is taken over by the kernel debugger, this
3514 * function needs to save the current console state, then put the console
3515 * into a state suitable for the kernel debugger.
3516 *
3517 * RETURNS:
3518 * Zero on success, nonzero if a failure occurred when trying to prepare
3519 * the console for the debugger.
3520 */
3521int con_debug_enter(struct vc_data *vc)
3522{
3523 int ret = 0;
3524
3525 saved_fg_console = fg_console;
3526 saved_last_console = last_console;
3527 saved_want_console = want_console;
3528 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003529 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003530 vc->vc_mode = KD_TEXT;
3531 console_blanked = 0;
3532 if (vc->vc_sw->con_debug_enter)
3533 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003534#ifdef CONFIG_KGDB_KDB
3535 /* Set the initial LINES variable if it is not already set */
3536 if (vc->vc_rows < 999) {
3537 int linecount;
3538 char lns[4];
3539 const char *setargs[3] = {
3540 "set",
3541 "LINES",
3542 lns,
3543 };
3544 if (kdbgetintenv(setargs[0], &linecount)) {
3545 snprintf(lns, 4, "%i", vc->vc_rows);
3546 kdb_set(2, setargs);
3547 }
3548 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003549 if (vc->vc_cols < 999) {
3550 int colcount;
3551 char cols[4];
3552 const char *setargs[3] = {
3553 "set",
3554 "COLUMNS",
3555 cols,
3556 };
3557 if (kdbgetintenv(setargs[0], &colcount)) {
3558 snprintf(cols, 4, "%i", vc->vc_cols);
3559 kdb_set(2, setargs);
3560 }
3561 }
Jason Wessel81d44502010-08-05 09:22:30 -05003562#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003563 return ret;
3564}
3565EXPORT_SYMBOL_GPL(con_debug_enter);
3566
3567/**
3568 * con_debug_leave - restore console state
3569 * @sw: console driver
3570 *
3571 * Restore the console state to what it was before the kernel debugger
3572 * was invoked.
3573 *
3574 * RETURNS:
3575 * Zero on success, nonzero if a failure occurred when trying to restore
3576 * the console.
3577 */
3578int con_debug_leave(void)
3579{
3580 struct vc_data *vc;
3581 int ret = 0;
3582
3583 fg_console = saved_fg_console;
3584 last_console = saved_last_console;
3585 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003586 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003587 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3588
3589 vc = vc_cons[fg_console].d;
3590 if (vc->vc_sw->con_debug_leave)
3591 ret = vc->vc_sw->con_debug_leave(vc);
3592 return ret;
3593}
3594EXPORT_SYMBOL_GPL(con_debug_leave);
3595
Alan Cox50e244c2013-01-25 10:28:15 +10003596static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003597{
3598 struct module *owner = csw->owner;
3599 struct con_driver *con_driver;
3600 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02003601 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003602
Alan Cox50e244c2013-01-25 10:28:15 +10003603 WARN_CONSOLE_UNLOCKED();
3604
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003605 if (!try_module_get(owner))
3606 return -ENODEV;
3607
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003608 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3609 con_driver = &registered_con_driver[i];
3610
3611 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02003612 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10003613 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02003614 goto err;
3615 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003616 }
3617
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003618 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02003619 if (!desc) {
3620 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003621 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02003622 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003623
3624 retval = -EINVAL;
3625
3626 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3627 con_driver = &registered_con_driver[i];
3628
Imre Deakd364b5c2015-04-01 21:06:16 +03003629 if (con_driver->con == NULL &&
3630 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003631 con_driver->con = csw;
3632 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003633 con_driver->node = i;
3634 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003635 CON_DRIVER_FLAG_INIT;
3636 con_driver->first = first;
3637 con_driver->last = last;
3638 retval = 0;
3639 break;
3640 }
3641 }
3642
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003643 if (retval)
3644 goto err;
3645
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003646 con_driver->dev =
3647 device_create_with_groups(vtconsole_class, NULL,
3648 MKDEV(0, con_driver->node),
3649 con_driver, con_dev_groups,
3650 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003651 if (IS_ERR(con_driver->dev)) {
3652 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003653 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003654 PTR_ERR(con_driver->dev));
3655 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003656 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003657 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003658 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003659
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003660err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003661 module_put(owner);
3662 return retval;
3663}
Alan Cox50e244c2013-01-25 10:28:15 +10003664
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003665
3666/**
Wang YanQing50539dd2013-05-09 02:14:44 +08003667 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003668 * @csw: console driver
3669 *
3670 * DESCRIPTION: All drivers that registers to the console layer must
3671 * call this function upon exit, or if the console driver is in a state
3672 * where it won't be able to handle console services, such as the
3673 * framebuffer console without loaded framebuffer drivers.
3674 *
3675 * The driver must unbind first prior to unregistration.
3676 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003677int do_unregister_con_driver(const struct consw *csw)
3678{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003679 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003680
3681 /* cannot unregister a bound driver */
3682 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003683 return -EBUSY;
3684
3685 if (csw == conswitchp)
3686 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003687
3688 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3689 struct con_driver *con_driver = &registered_con_driver[i];
3690
Imre Deak2cf30f72014-12-16 00:15:59 +02003691 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03003692 /*
3693 * Defer the removal of the sysfs entries since that
3694 * will acquire the kernfs s_active lock and we can't
3695 * acquire this lock while holding the console lock:
3696 * the unbind sysfs entry imposes already the opposite
3697 * order. Reset con already here to prevent any later
3698 * lookup to succeed and mark this slot as zombie, so
3699 * it won't get reused until we complete the removal
3700 * in the deferred work.
3701 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003702 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03003703 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
3704 schedule_work(&con_driver_unregister_work);
3705
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003706 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003707 }
3708 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003709
3710 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003711}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003712EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003713
Imre Deakd364b5c2015-04-01 21:06:16 +03003714static void con_driver_unregister_callback(struct work_struct *ignored)
3715{
3716 int i;
3717
3718 console_lock();
3719
3720 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3721 struct con_driver *con_driver = &registered_con_driver[i];
3722
3723 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
3724 continue;
3725
3726 console_unlock();
3727
3728 vtconsole_deinit_device(con_driver);
3729 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
3730
3731 console_lock();
3732
3733 if (WARN_ON_ONCE(con_driver->con))
3734 con_driver->con = NULL;
3735 con_driver->desc = NULL;
3736 con_driver->dev = NULL;
3737 con_driver->node = 0;
3738 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
3739 con_driver->flag = 0;
3740 con_driver->first = 0;
3741 con_driver->last = 0;
3742 }
3743
3744 console_unlock();
3745}
3746
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003747/*
3748 * If we support more console drivers, this function is used
3749 * when a driver wants to take over some existing consoles
3750 * and become default driver for newly opened ones.
3751 *
Wang YanQing155957f2013-05-21 13:15:12 +08003752 * do_take_over_console is basically a register followed by unbind
Alan Cox50e244c2013-01-25 10:28:15 +10003753 */
3754int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3755{
3756 int err;
3757
3758 err = do_register_con_driver(csw, first, last);
3759 /*
3760 * If we get an busy error we still want to bind the console driver
3761 * and return success, as we may have unbound the console driver
3762 * but not unregistered it.
3763 */
3764 if (err == -EBUSY)
3765 err = 0;
3766 if (!err)
3767 do_bind_con_driver(csw, first, last, deflt);
3768
3769 return err;
3770}
3771EXPORT_SYMBOL_GPL(do_take_over_console);
3772
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003773
3774/*
3775 * give_up_console is a wrapper to unregister_con_driver. It will only
3776 * work if driver is fully unbound.
3777 */
3778void give_up_console(const struct consw *csw)
3779{
Wang YanQing70125e72013-05-09 02:14:39 +08003780 console_lock();
3781 do_unregister_con_driver(csw);
3782 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003783}
3784
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003785static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003786{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003787 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003788
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003789 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3790 if (IS_ERR(vtconsole_class)) {
3791 printk(KERN_WARNING "Unable to create vt console class; "
3792 "errno = %ld\n", PTR_ERR(vtconsole_class));
3793 vtconsole_class = NULL;
3794 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003795
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003796 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003797 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3798 struct con_driver *con = &registered_con_driver[i];
3799
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003800 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003801 con->dev =
3802 device_create_with_groups(vtconsole_class, NULL,
3803 MKDEV(0, con->node),
3804 con, con_dev_groups,
3805 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003806
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003807 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003808 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003809 "device for %s; errno = %ld\n",
3810 con->desc, PTR_ERR(con->dev));
3811 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003812 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003813 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003814 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003815 }
3816 }
3817
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003818 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003819}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003820postcore_initcall(vtconsole_class_init);
3821
3822#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003823
3824/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 * Screen blanking
3826 */
3827
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003828static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003830 unsigned int mode;
3831
3832 if (get_user(mode, p + 1))
3833 return -EFAULT;
3834
3835 vesa_blank_mode = (mode < 4) ? mode : 0;
3836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839void do_blank_screen(int entering_gfx)
3840{
3841 struct vc_data *vc = vc_cons[fg_console].d;
3842 int i;
3843
3844 WARN_CONSOLE_UNLOCKED();
3845
3846 if (console_blanked) {
3847 if (blank_state == blank_vesa_wait) {
3848 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003849 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 }
3851 return;
3852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853
3854 /* entering graphics mode? */
3855 if (entering_gfx) {
3856 hide_cursor(vc);
3857 save_screen(vc);
3858 vc->vc_sw->con_blank(vc, -1, 1);
3859 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003860 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 set_origin(vc);
3862 return;
3863 }
3864
izumib6e8f002007-07-17 04:05:49 -07003865 if (blank_state != blank_normal_wait)
3866 return;
3867 blank_state = blank_off;
3868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 /* don't blank graphics */
3870 if (vc->vc_mode != KD_TEXT) {
3871 console_blanked = fg_console + 1;
3872 return;
3873 }
3874
3875 hide_cursor(vc);
3876 del_timer_sync(&console_timer);
3877 blank_timer_expired = 0;
3878
3879 save_screen(vc);
3880 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003881 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882 console_blanked = fg_console + 1;
3883 if (i)
3884 set_origin(vc);
3885
3886 if (console_blank_hook && console_blank_hook(1))
3887 return;
3888
Ville Syrjalad060a322006-01-09 20:53:49 -08003889 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003890 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 mod_timer(&console_timer, jiffies + vesa_off_interval);
3892 }
Alan Cox8b92e872009-09-19 13:13:24 -07003893 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894}
3895EXPORT_SYMBOL(do_blank_screen);
3896
3897/*
3898 * Called by timer as well as from vt_console_driver
3899 */
3900void do_unblank_screen(int leaving_gfx)
3901{
3902 struct vc_data *vc;
3903
3904 /* This should now always be called from a "sane" (read: can schedule)
3905 * context for the sake of the low level drivers, except in the special
3906 * case of oops_in_progress
3907 */
3908 if (!oops_in_progress)
3909 might_sleep();
3910
3911 WARN_CONSOLE_UNLOCKED();
3912
3913 ignore_poke = 0;
3914 if (!console_blanked)
3915 return;
3916 if (!vc_cons_allocated(fg_console)) {
3917 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08003918 pr_warn("unblank_screen: tty %d not allocated ??\n",
3919 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 return;
3921 }
3922 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003923 /* Try to unblank in oops case too */
3924 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 return; /* but leave console_blanked != 0 */
3926
3927 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003928 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 blank_state = blank_normal_wait;
3930 }
3931
3932 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003933 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 /* Low-level driver cannot restore -> do it ourselves */
3935 update_screen(vc);
3936 if (console_blank_hook)
3937 console_blank_hook(0);
3938 set_palette(vc);
3939 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003940 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941}
3942EXPORT_SYMBOL(do_unblank_screen);
3943
3944/*
3945 * This is called by the outside world to cause a forced unblank, mostly for
3946 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3947 * call it with 1 as an argument and so force a mode restore... that may kill
3948 * X or at least garbage the screen but would also make the Oops visible...
3949 */
3950void unblank_screen(void)
3951{
3952 do_unblank_screen(0);
3953}
3954
3955/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003956 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003958 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 */
3960static void blank_screen_t(unsigned long dummy)
3961{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003962 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003963 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003964 return;
3965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 blank_timer_expired = 1;
3967 schedule_work(&console_work);
3968}
3969
3970void poke_blanked_console(void)
3971{
3972 WARN_CONSOLE_UNLOCKED();
3973
3974 /* Add this so we quickly catch whoever might call us in a non
3975 * safe context. Nowadays, unblank_screen() isn't to be called in
3976 * atomic contexts and is allowed to schedule (with the special case
3977 * of oops_in_progress, but that isn't of any concern for this
3978 * function. --BenH.
3979 */
3980 might_sleep();
3981
3982 /* This isn't perfectly race free, but a race here would be mostly harmless,
3983 * at worse, we'll do a spurrious blank and it's unlikely
3984 */
3985 del_timer(&console_timer);
3986 blank_timer_expired = 0;
3987
3988 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3989 return;
3990 if (console_blanked)
3991 unblank_screen();
3992 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003993 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 blank_state = blank_normal_wait;
3995 }
3996}
3997
3998/*
3999 * Palettes
4000 */
4001
4002static void set_palette(struct vc_data *vc)
4003{
4004 WARN_CONSOLE_UNLOCKED();
4005
Jiri Slaby709280d2016-06-23 13:34:27 +02004006 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 vc->vc_sw->con_set_palette(vc, color_table);
4008}
4009
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010/*
4011 * Load palette into the DAC registers. arg points to a colour
4012 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4013 */
4014
4015int con_set_cmap(unsigned char __user *arg)
4016{
Michael Gehring871bdea2012-03-21 01:26:45 +01004017 int i, j, k;
4018 unsigned char colormap[3*16];
4019
4020 if (copy_from_user(colormap, arg, sizeof(colormap)))
4021 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Torben Hohnac751ef2011-01-25 15:07:35 -08004023 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004024 for (i = k = 0; i < 16; i++) {
4025 default_red[i] = colormap[k++];
4026 default_grn[i] = colormap[k++];
4027 default_blu[i] = colormap[k++];
4028 }
4029 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4030 if (!vc_cons_allocated(i))
4031 continue;
4032 for (j = k = 0; j < 16; j++) {
4033 vc_cons[i].d->vc_palette[k++] = default_red[j];
4034 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4035 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4036 }
4037 set_palette(vc_cons[i].d);
4038 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004039 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Michael Gehring871bdea2012-03-21 01:26:45 +01004041 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042}
4043
4044int con_get_cmap(unsigned char __user *arg)
4045{
Michael Gehring871bdea2012-03-21 01:26:45 +01004046 int i, k;
4047 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048
Torben Hohnac751ef2011-01-25 15:07:35 -08004049 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004050 for (i = k = 0; i < 16; i++) {
4051 colormap[k++] = default_red[i];
4052 colormap[k++] = default_grn[i];
4053 colormap[k++] = default_blu[i];
4054 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004055 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Michael Gehring871bdea2012-03-21 01:26:45 +01004057 if (copy_to_user(arg, colormap, sizeof(colormap)))
4058 return -EFAULT;
4059
4060 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061}
4062
4063void reset_palette(struct vc_data *vc)
4064{
4065 int j, k;
4066 for (j=k=0; j<16; j++) {
4067 vc->vc_palette[k++] = default_red[j];
4068 vc->vc_palette[k++] = default_grn[j];
4069 vc->vc_palette[k++] = default_blu[j];
4070 }
4071 set_palette(vc);
4072}
4073
4074/*
4075 * Font switching
4076 *
4077 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4078 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4079 * depending on width) reserved for each character which is kinda wasty, but
4080 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004081 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 * format (maybe we should add a `fontoffset' field to the `display'
4083 * structure so we won't have to convert the fontdata all the time.
4084 * /Jes
4085 */
4086
4087#define max_font_size 65536
4088
4089static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4090{
4091 struct console_font font;
4092 int rc = -EINVAL;
4093 int c;
4094
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (op->data) {
4096 font.data = kmalloc(max_font_size, GFP_KERNEL);
4097 if (!font.data)
4098 return -ENOMEM;
4099 } else
4100 font.data = NULL;
4101
Torben Hohnac751ef2011-01-25 15:07:35 -08004102 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004103 if (vc->vc_mode != KD_TEXT)
4104 rc = -EINVAL;
4105 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 rc = vc->vc_sw->con_font_get(vc, &font);
4107 else
4108 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004109 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
4111 if (rc)
4112 goto out;
4113
4114 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004115
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (op->data && font.charcount > op->charcount)
4117 rc = -ENOSPC;
4118 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4119 if (font.width > op->width || font.height > op->height)
4120 rc = -ENOSPC;
4121 } else {
4122 if (font.width != 8)
4123 rc = -EIO;
4124 else if ((op->height && font.height > op->height) ||
4125 font.height > 32)
4126 rc = -ENOSPC;
4127 }
4128 if (rc)
4129 goto out;
4130
4131 op->height = font.height;
4132 op->width = font.width;
4133 op->charcount = font.charcount;
4134
4135 if (op->data && copy_to_user(op->data, font.data, c))
4136 rc = -EFAULT;
4137
4138out:
4139 kfree(font.data);
4140 return rc;
4141}
4142
4143static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4144{
4145 struct console_font font;
4146 int rc = -EINVAL;
4147 int size;
4148
4149 if (vc->vc_mode != KD_TEXT)
4150 return -EINVAL;
4151 if (!op->data)
4152 return -EINVAL;
4153 if (op->charcount > 512)
4154 return -EINVAL;
4155 if (!op->height) { /* Need to guess font height [compat] */
4156 int h, i;
4157 u8 __user *charmap = op->data;
4158 u8 tmp;
4159
4160 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4161 so that we can get rid of this soon */
4162 if (!(op->flags & KD_FONT_FLAG_OLD))
4163 return -EINVAL;
4164 for (h = 32; h > 0; h--)
4165 for (i = 0; i < op->charcount; i++) {
4166 if (get_user(tmp, &charmap[32*i+h-1]))
4167 return -EFAULT;
4168 if (tmp)
4169 goto nonzero;
4170 }
4171 return -EINVAL;
4172 nonzero:
4173 op->height = h;
4174 }
4175 if (op->width <= 0 || op->width > 32 || op->height > 32)
4176 return -EINVAL;
4177 size = (op->width+7)/8 * 32 * op->charcount;
4178 if (size > max_font_size)
4179 return -ENOSPC;
4180 font.charcount = op->charcount;
4181 font.height = op->height;
4182 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004183 font.data = memdup_user(op->data, size);
4184 if (IS_ERR(font.data))
4185 return PTR_ERR(font.data);
Torben Hohnac751ef2011-01-25 15:07:35 -08004186 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004187 if (vc->vc_mode != KD_TEXT)
4188 rc = -EINVAL;
4189 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4191 else
4192 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004193 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 kfree(font.data);
4195 return rc;
4196}
4197
4198static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4199{
4200 struct console_font font = {.width = op->width, .height = op->height};
4201 char name[MAX_FONT_NAME];
4202 char *s = name;
4203 int rc;
4204
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
4206 if (!op->data)
4207 s = NULL;
4208 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4209 return -EFAULT;
4210 else
4211 name[MAX_FONT_NAME - 1] = 0;
4212
Torben Hohnac751ef2011-01-25 15:07:35 -08004213 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004214 if (vc->vc_mode != KD_TEXT) {
4215 console_unlock();
4216 return -EINVAL;
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 if (vc->vc_sw->con_font_default)
4219 rc = vc->vc_sw->con_font_default(vc, &font, s);
4220 else
4221 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004222 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 if (!rc) {
4224 op->width = font.width;
4225 op->height = font.height;
4226 }
4227 return rc;
4228}
4229
4230static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4231{
4232 int con = op->height;
4233 int rc;
4234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
Torben Hohnac751ef2011-01-25 15:07:35 -08004236 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004237 if (vc->vc_mode != KD_TEXT)
4238 rc = -EINVAL;
4239 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 rc = -ENOSYS;
4241 else if (con < 0 || !vc_cons_allocated(con))
4242 rc = -ENOTTY;
4243 else if (con == vc->vc_num) /* nothing to do */
4244 rc = 0;
4245 else
4246 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004247 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 return rc;
4249}
4250
4251int con_font_op(struct vc_data *vc, struct console_font_op *op)
4252{
4253 switch (op->op) {
4254 case KD_FONT_OP_SET:
4255 return con_font_set(vc, op);
4256 case KD_FONT_OP_GET:
4257 return con_font_get(vc, op);
4258 case KD_FONT_OP_SET_DEFAULT:
4259 return con_font_default(vc, op);
4260 case KD_FONT_OP_COPY:
4261 return con_font_copy(vc, op);
4262 }
4263 return -ENOSYS;
4264}
4265
4266/*
4267 * Interface exported to selection and vcs.
4268 */
4269
4270/* used by selection */
4271u16 screen_glyph(struct vc_data *vc, int offset)
4272{
4273 u16 w = scr_readw(screenpos(vc, offset, 1));
4274 u16 c = w & 0xff;
4275
4276 if (w & vc->vc_hi_font_mask)
4277 c |= 0x100;
4278 return c;
4279}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004280EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282/* used by vcs - note the word offset */
4283unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4284{
4285 return screenpos(vc, 2 * w_offset, viewed);
4286}
Samuel Thibault88867e32016-01-25 01:32:08 +01004287EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
4289void getconsxy(struct vc_data *vc, unsigned char *p)
4290{
4291 p[0] = vc->vc_x;
4292 p[1] = vc->vc_y;
4293}
4294
4295void putconsxy(struct vc_data *vc, unsigned char *p)
4296{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004297 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 gotoxy(vc, p[0], p[1]);
4299 set_cursor(vc);
4300}
4301
4302u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4303{
4304 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4305 return softcursor_original;
4306 return scr_readw(org);
4307}
4308
4309void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4310{
4311 scr_writew(val, org);
4312 if ((unsigned long)org == vc->vc_pos) {
4313 softcursor_original = -1;
4314 add_softcursor(vc);
4315 }
4316}
4317
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004318void vcs_scr_updated(struct vc_data *vc)
4319{
4320 notify_update(vc);
4321}
4322
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323/*
4324 * Visible symbols for modules
4325 */
4326
4327EXPORT_SYMBOL(color_table);
4328EXPORT_SYMBOL(default_red);
4329EXPORT_SYMBOL(default_grn);
4330EXPORT_SYMBOL(default_blu);
4331EXPORT_SYMBOL(update_region);
4332EXPORT_SYMBOL(redraw_screen);
4333EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334EXPORT_SYMBOL(fg_console);
4335EXPORT_SYMBOL(console_blank_hook);
4336EXPORT_SYMBOL(console_blanked);
4337EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004338EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340EXPORT_SYMBOL(give_up_console);
4341#endif