blob: c1d3b685a5877e4309ffa1734b34494e44eb55f1 [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{
598 if (vc == sel_cons)
599 clear_selection();
600 vc->vc_sw->con_cursor(vc, CM_ERASE);
601 hide_softcursor(vc);
602}
603
604static void set_cursor(struct vc_data *vc)
605{
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200606 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 return;
608 if (vc->vc_deccm) {
609 if (vc == sel_cons)
610 clear_selection();
611 add_softcursor(vc);
612 if ((vc->vc_cursor_type & 0x0f) != 1)
613 vc->vc_sw->con_cursor(vc, CM_DRAW);
614 } else
615 hide_cursor(vc);
616}
617
618static void set_origin(struct vc_data *vc)
619{
620 WARN_CONSOLE_UNLOCKED();
621
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200622 if (!con_is_visible(vc) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 !vc->vc_sw->con_set_origin ||
624 !vc->vc_sw->con_set_origin(vc))
625 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
626 vc->vc_visible_origin = vc->vc_origin;
627 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
628 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
629}
630
Denys Vlasenkoc0f160a2015-10-27 18:46:47 +0100631static void save_screen(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
633 WARN_CONSOLE_UNLOCKED();
634
635 if (vc->vc_sw->con_save_screen)
636 vc->vc_sw->con_save_screen(vc);
637}
638
639/*
640 * Redrawing of screen
641 */
642
Dave Airlie2a248302013-01-24 14:14:19 +1000643void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
645 unsigned short *p = (unsigned short *)vc->vc_origin;
646 int count = vc->vc_screenbuf_size / 2;
647 int mask = vc->vc_hi_font_mask | 0xff;
648
649 for (; count > 0; count--, p++) {
650 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
651 }
652}
653
654void redraw_screen(struct vc_data *vc, int is_switch)
655{
656 int redraw = 0;
657
658 WARN_CONSOLE_UNLOCKED();
659
660 if (!vc) {
661 /* strange ... */
662 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
663 return;
664 }
665
666 if (is_switch) {
667 struct vc_data *old_vc = vc_cons[fg_console].d;
668 if (old_vc == vc)
669 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200670 if (!con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 redraw = 1;
672 *vc->vc_display_fg = vc;
673 fg_console = vc->vc_num;
674 hide_cursor(old_vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200675 if (!con_is_visible(old_vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 save_screen(old_vc);
677 set_origin(old_vc);
678 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100679 if (tty0dev)
680 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 } else {
682 hide_cursor(vc);
683 redraw = 1;
684 }
685
686 if (redraw) {
687 int update;
688 int old_was_color = vc->vc_can_do_color;
689
690 set_origin(vc);
691 update = vc->vc_sw->con_switch(vc);
692 set_palette(vc);
693 /*
694 * If console changed from mono<->color, the best we can do
695 * is to clear the buffer attributes. As it currently stands,
696 * rebuilding new attributes from the old buffer is not doable
697 * without overly complex code.
698 */
699 if (old_was_color != vc->vc_can_do_color) {
700 update_attr(vc);
701 clear_buffer_attributes(vc);
702 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700703
704 /* Forcibly update if we're panicing */
705 if ((update && vc->vc_mode != KD_GRAPHICS) ||
706 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
708 }
709 set_cursor(vc);
710 if (is_switch) {
711 set_leds();
712 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800713 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715}
716
717/*
718 * Allocation, freeing and resizing of VTs.
719 */
720
721int vc_cons_allocated(unsigned int i)
722{
723 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
724}
725
726static void visual_init(struct vc_data *vc, int num, int init)
727{
728 /* ++Geert: vc->vc_sw->con_init determines console size */
729 if (vc->vc_sw)
730 module_put(vc->vc_sw->owner);
731 vc->vc_sw = conswitchp;
732#ifndef VT_SINGLE_DRIVER
733 if (con_driver_map[num])
734 vc->vc_sw = con_driver_map[num];
735#endif
736 __module_get(vc->vc_sw->owner);
737 vc->vc_num = num;
738 vc->vc_display_fg = &master_display_fg;
Dongxing Zhang08b33242015-06-10 15:21:10 +0800739 if (vc->vc_uni_pagedir_loc)
740 con_free_unimap(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +0200742 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 vc->vc_hi_font_mask = 0;
744 vc->vc_complement_mask = 0;
745 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700746 vc->vc_panic_force_write = false;
David Daney1b459962016-05-17 11:41:04 -0700747 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 vc->vc_sw->con_init(vc, init);
749 if (!vc->vc_complement_mask)
750 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
751 vc->vc_s_complement_mask = vc->vc_complement_mask;
752 vc->vc_size_row = vc->vc_cols << 1;
753 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
754}
755
756int vc_allocate(unsigned int currcons) /* return 0 on success */
757{
Jiri Slaby34902b72016-03-31 10:08:15 +0200758 struct vt_notifier_param param;
759 struct vc_data *vc;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 WARN_CONSOLE_UNLOCKED();
762
763 if (currcons >= MAX_NR_CONSOLES)
764 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Jiri Slaby34902b72016-03-31 10:08:15 +0200766 if (vc_cons[currcons].d)
767 return 0;
768
769 /* due to the granularity of kmalloc, we waste some memory here */
770 /* the alloc is done in two steps, to optimize the common situation
771 of a 25x80 console (structsize=216, screenbuf_size=4000) */
772 /* although the numbers above are not valid since long ago, the
773 point is still up-to-date and the comment still has its value
774 even if only as a historical artifact. --mj, July 1998 */
775 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
776 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 return -ENOMEM;
Jiri Slaby34902b72016-03-31 10:08:15 +0200778
779 vc_cons[currcons].d = vc;
780 tty_port_init(&vc->port);
781 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
782
783 visual_init(vc, currcons, 1);
784
785 if (!*vc->vc_uni_pagedir_loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 con_set_default_unimap(vc);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500787
Alexander Potapenko3bf351b2018-06-14 12:23:09 +0200788 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Jiri Slaby34902b72016-03-31 10:08:15 +0200789 if (!vc->vc_screenbuf)
790 goto err_free;
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500791
Jiri Slaby34902b72016-03-31 10:08:15 +0200792 /* If no drivers have overridden us and the user didn't pass a
793 boot option, default to displaying the cursor */
794 if (global_cursor_default == -1)
795 global_cursor_default = 1;
796
797 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
798 vcs_make_sysfs(currcons);
799 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return 0;
Jiri Slaby34902b72016-03-31 10:08:15 +0200802err_free:
803 kfree(vc);
804 vc_cons[currcons].d = NULL;
805 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700808static inline int resize_screen(struct vc_data *vc, int width, int height,
809 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 /* Resizes the resolution of the display adapater */
812 int err = 0;
813
814 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700815 err = vc->vc_sw->con_resize(vc, width, height, user);
816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return err;
818}
819
820/*
821 * Change # of rows and columns (0 means unchanged/the size of fg_console)
822 * [this is to be used together with some user program
823 * like resize that changes the hardware videomode]
824 */
825#define VC_RESIZE_MAXCOL (32767)
826#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100827
828/**
829 * vc_do_resize - resizing method for the tty
830 * @tty: tty being resized
831 * @real_tty: real tty (different to tty if a pty/tty pair)
832 * @vc: virtual console private data
833 * @cols: columns
834 * @lines: lines
835 *
836 * Resize a virtual console, clipping according to the actual constraints.
837 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800838 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100839 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400840 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100841 * ctrl_lock of the tty IFF a tty is passed.
842 */
843
Alan Coxfc6f6232009-01-02 13:43:17 +0000844static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
845 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700848 unsigned long end;
Jiri Slabyf2ee4ae2011-03-23 09:49:57 +0100849 unsigned int old_rows, old_row_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700851 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 unsigned short *newscreen;
853
854 WARN_CONSOLE_UNLOCKED();
855
856 if (!vc)
857 return -ENXIO;
858
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700859 user = vc->vc_resize_user;
860 vc->vc_resize_user = 0;
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
863 return -EINVAL;
864
865 new_cols = (cols ? cols : vc->vc_cols);
866 new_rows = (lines ? lines : vc->vc_rows);
867 new_row_size = new_cols << 1;
868 new_screen_size = new_row_size * new_rows;
869
870 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
871 return 0;
872
Dmitry Vyukov32b29212016-10-14 15:18:28 +0200873 if (new_screen_size > (4 << 20))
874 return -EINVAL;
Alexander Potapenko3bf351b2018-06-14 12:23:09 +0200875 newscreen = kzalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (!newscreen)
877 return -ENOMEM;
878
Scot Doyle009e39a2016-10-13 12:12:43 -0500879 if (vc == sel_cons)
880 clear_selection();
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700885 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (err) {
887 kfree(newscreen);
888 return err;
889 }
890
891 vc->vc_rows = new_rows;
892 vc->vc_cols = new_cols;
893 vc->vc_size_row = new_row_size;
894 vc->vc_screenbuf_size = new_screen_size;
895
896 rlth = min(old_row_size, new_row_size);
897 rrem = new_row_size - rlth;
898 old_origin = vc->vc_origin;
899 new_origin = (long) newscreen;
900 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700901
902 if (vc->vc_y > new_rows) {
903 if (old_rows - vc->vc_y < new_rows) {
904 /*
905 * Cursor near the bottom, copy contents from the
906 * bottom of buffer
907 */
908 old_origin += (old_rows - new_rows) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700909 } else {
910 /*
911 * Cursor is in no man's land, copy 1/2 screenful
912 * from the top and bottom of cursor position
913 */
914 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700915 }
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +0200916 }
917
918 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 update_attr(vc);
921
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 while (old_origin < end) {
923 scr_memcpyw((unsigned short *) new_origin,
924 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700926 scr_memsetw((void *)(new_origin + rlth),
927 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 old_origin += old_row_size;
929 new_origin += new_row_size;
930 }
931 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700932 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
933 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100934 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 vc->vc_screenbuf_size = new_screen_size;
937 set_origin(vc);
938
939 /* do part of a reset_terminal() */
940 vc->vc_top = 0;
941 vc->vc_bottom = vc->vc_rows;
942 gotoxy(vc, vc->vc_x, vc->vc_y);
943 save_cur(vc);
944
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100945 if (tty) {
946 /* Rewrite the requested winsize data with the actual
947 resulting sizes */
948 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 memset(&ws, 0, sizeof(ws));
950 ws.ws_row = vc->vc_rows;
951 ws.ws_col = vc->vc_cols;
952 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000953 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200956 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700958 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Nicolas Pitrec13c2a52019-01-08 22:55:01 -0500959 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return err;
961}
962
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100963/**
964 * vc_resize - resize a VT
965 * @vc: virtual console
966 * @cols: columns
967 * @rows: rows
968 *
969 * Resize a virtual console as seen from the console end of things. We
970 * use the common vc_do_resize methods to update the structures. The
971 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +0200972 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100973 */
974
975int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700976{
Alan Cox8ce73262010-06-01 22:52:56 +0200977 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100978}
979
980/**
981 * vt_resize - resize a VT
982 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100983 * @ws: winsize attributes
984 *
985 * Resize a virtual terminal. This is called by the tty layer as we
986 * register our own handler for resizing. The mutual helper does all
987 * the actual work.
988 *
989 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -0400990 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100991 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800992static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100993{
994 struct vc_data *vc = tty->driver_data;
995 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Torben Hohnac751ef2011-01-25 15:07:35 -0800997 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +0000998 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -0800999 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001000 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001001}
1002
Peter Hurley421b40a2013-05-17 12:41:03 -04001003struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Peter Hurley421b40a2013-05-17 12:41:03 -04001005 struct vc_data *vc = NULL;
1006
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 WARN_CONSOLE_UNLOCKED();
1008
1009 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001010 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001011
Peter Hurley421b40a2013-05-17 12:41:03 -04001012 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001013 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001014 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001016 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001017 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001018 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 vc_cons[currcons].d = NULL;
1020 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001021 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024/*
1025 * VT102 emulator
1026 */
1027
Alan Cox079c9532012-02-28 14:49:23 +00001028#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1029#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1030#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032#define decarm VC_REPEAT
1033#define decckm VC_CKMODE
1034#define kbdapplic VC_APPLIC
1035#define lnm VC_CRLF
1036
1037/*
1038 * this is what the terminal answers to a ESC-Z or csi0c query.
1039 */
1040#define VT100ID "\033[?1;2c"
1041#define VT102ID "\033[?6c"
1042
Jiri Slaby8ede5cc2016-03-31 10:08:16 +02001043const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 8,12,10,14, 9,13,11,15 };
1045
1046/* the default colour table, for VGA+ colour systems */
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001047unsigned char default_red[] = {
1048 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1049 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1050};
1051module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001053unsigned char default_grn[] = {
1054 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1055 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1056};
1057module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1058
1059unsigned char default_blu[] = {
1060 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1061 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1062};
1063module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * gotoxy() must verify all boundaries, because the arguments
1067 * might also be negative. If the given position is out of
1068 * bounds, the cursor is placed at the nearest margin.
1069 */
1070static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1071{
1072 int min_y, max_y;
1073
1074 if (new_x < 0)
1075 vc->vc_x = 0;
1076 else {
1077 if (new_x >= vc->vc_cols)
1078 vc->vc_x = vc->vc_cols - 1;
1079 else
1080 vc->vc_x = new_x;
1081 }
1082
1083 if (vc->vc_decom) {
1084 min_y = vc->vc_top;
1085 max_y = vc->vc_bottom;
1086 } else {
1087 min_y = 0;
1088 max_y = vc->vc_rows;
1089 }
1090 if (new_y < min_y)
1091 vc->vc_y = min_y;
1092 else if (new_y >= max_y)
1093 vc->vc_y = max_y - 1;
1094 else
1095 vc->vc_y = new_y;
1096 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1097 vc->vc_need_wrap = 0;
1098}
1099
1100/* for absolute user moves, when decom is set */
1101static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1102{
1103 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1104}
1105
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001106void scrollback(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001108 scrolldelta(-(vc->vc_rows / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109}
1110
1111void scrollfront(struct vc_data *vc, int lines)
1112{
1113 if (!lines)
1114 lines = vc->vc_rows / 2;
1115 scrolldelta(lines);
1116}
1117
1118static void lf(struct vc_data *vc)
1119{
1120 /* don't scroll if above bottom of scrolling region, or
1121 * if below scrolling region
1122 */
1123 if (vc->vc_y + 1 == vc->vc_bottom)
1124 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1125 else if (vc->vc_y < vc->vc_rows - 1) {
1126 vc->vc_y++;
1127 vc->vc_pos += vc->vc_size_row;
1128 }
1129 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001130 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static void ri(struct vc_data *vc)
1134{
1135 /* don't scroll if below top of scrolling region, or
1136 * if above scrolling region
1137 */
1138 if (vc->vc_y == vc->vc_top)
1139 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1140 else if (vc->vc_y > 0) {
1141 vc->vc_y--;
1142 vc->vc_pos -= vc->vc_size_row;
1143 }
1144 vc->vc_need_wrap = 0;
1145}
1146
1147static inline void cr(struct vc_data *vc)
1148{
1149 vc->vc_pos -= vc->vc_x << 1;
1150 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001151 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static inline void bs(struct vc_data *vc)
1155{
1156 if (vc->vc_x) {
1157 vc->vc_pos -= 2;
1158 vc->vc_x--;
1159 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001160 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162}
1163
1164static inline void del(struct vc_data *vc)
1165{
1166 /* ignored */
1167}
1168
1169static void csi_J(struct vc_data *vc, int vpar)
1170{
1171 unsigned int count;
1172 unsigned short * start;
1173
1174 switch (vpar) {
1175 case 0: /* erase from cursor to end of display */
1176 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1177 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 case 1: /* erase from start to cursor */
1180 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1181 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 break;
Petr Písařf8df13e2011-04-15 10:08:08 +02001183 case 3: /* erase scroll-back buffer (and whole display) */
1184 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
Jiri Slaby42acfc62016-10-03 11:00:17 +02001185 vc->vc_screenbuf_size);
Petr Písařf8df13e2011-04-15 10:08:08 +02001186 set_origin(vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001187 if (con_is_visible(vc))
Petr Písař0930b092014-02-06 21:01:23 +01001188 update_screen(vc);
Petr Písařf8df13e2011-04-15 10:08:08 +02001189 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 case 2: /* erase whole display */
1191 count = vc->vc_cols * vc->vc_rows;
1192 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 break;
1194 default:
1195 return;
1196 }
1197 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001198 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001199 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 vc->vc_need_wrap = 0;
1201}
1202
1203static void csi_K(struct vc_data *vc, int vpar)
1204{
1205 unsigned int count;
1206 unsigned short * start;
1207
1208 switch (vpar) {
1209 case 0: /* erase from cursor to end of line */
1210 count = vc->vc_cols - vc->vc_x;
1211 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
1213 case 1: /* erase from start of line to cursor */
1214 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1215 count = vc->vc_x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 break;
1217 case 2: /* erase whole line */
1218 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1219 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 break;
1221 default:
1222 return;
1223 }
1224 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1225 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001226 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001227 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1231{ /* not vt100? */
1232 int count;
1233
1234 if (!vpar)
1235 vpar++;
1236 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1237
1238 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001239 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1241 vc->vc_need_wrap = 0;
1242}
1243
1244static void default_attr(struct vc_data *vc)
1245{
1246 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001247 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 vc->vc_underline = 0;
1249 vc->vc_reverse = 0;
1250 vc->vc_blink = 0;
1251 vc->vc_color = vc->vc_def_color;
1252}
1253
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001254struct rgb { u8 r; u8 g; u8 b; };
1255
Jiri Slaby0f91e142016-06-23 13:34:32 +02001256static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001257{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001258 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001259 c->r = i&1 ? 0xaa : 0x00;
1260 c->g = i&2 ? 0xaa : 0x00;
1261 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001262 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001263 c->r = i&1 ? 0xff : 0x55;
1264 c->g = i&2 ? 0xff : 0x55;
1265 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001266 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001267 c->r = (i - 16) / 36 * 85 / 2;
1268 c->g = (i - 16) / 6 % 6 * 85 / 2;
1269 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001270 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001271 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001272}
1273
Jiri Slaby0f91e142016-06-23 13:34:32 +02001274static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001275{
Jiri Slaby193df022016-06-23 13:34:33 +02001276 u8 hue = 0, max = max3(c->r, c->g, c->b);
1277
1278 if (c->r > max / 2)
1279 hue |= 4;
1280 if (c->g > max / 2)
1281 hue |= 2;
1282 if (c->b > max / 2)
1283 hue |= 1;
1284
1285 if (hue == 7 && max <= 0x55) {
1286 hue = 0;
1287 vc->vc_intensity = 2;
1288 } else if (max > 0xaa)
1289 vc->vc_intensity = 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001290 else
Jiri Slaby193df022016-06-23 13:34:33 +02001291 vc->vc_intensity = 1;
1292
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001293 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1294}
1295
Jiri Slaby0f91e142016-06-23 13:34:32 +02001296static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001297{
1298 /* For backgrounds, err on the dark side. */
1299 vc->vc_color = (vc->vc_color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001300 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001301}
1302
Jiri Slabye05ab232016-06-23 13:34:31 +02001303/*
1304 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1305 * and thus need to be detected and ignored by hand. Strictly speaking, that
1306 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1307 * no one produces such codes and almost no one accepts them.
1308 *
1309 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1310 * supporting them.
1311 */
1312static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001313 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001314{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001315 struct rgb c;
1316
Jiri Slabye05ab232016-06-23 13:34:31 +02001317 i++;
1318 if (i > vc->vc_npar)
1319 return i;
1320
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001321 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001322 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001323 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001324 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001325 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001326 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001327 c.r = vc->vc_par[i + 1];
1328 c.g = vc->vc_par[i + 2];
1329 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001330 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001331 } else
1332 return i;
1333
1334 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001335
1336 return i;
1337}
1338
Torben Hohnac751ef2011-01-25 15:07:35 -08001339/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340static void csi_m(struct vc_data *vc)
1341{
1342 int i;
1343
1344 for (i = 0; i <= vc->vc_npar; i++)
1345 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001346 case 0: /* all attributes off */
1347 default_attr(vc);
1348 break;
1349 case 1:
1350 vc->vc_intensity = 2;
1351 break;
1352 case 2:
1353 vc->vc_intensity = 0;
1354 break;
1355 case 3:
1356 vc->vc_italic = 1;
1357 break;
Mike Frysinger4d2e98c2018-01-29 17:08:21 -05001358 case 21:
1359 /*
1360 * No console drivers support double underline, so
1361 * convert it to a single underline.
1362 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001363 case 4:
1364 vc->vc_underline = 1;
1365 break;
1366 case 5:
1367 vc->vc_blink = 1;
1368 break;
1369 case 7:
1370 vc->vc_reverse = 1;
1371 break;
1372 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1373 * Select primary font, don't display control chars if
1374 * defined, don't set bit 8 on output.
1375 */
1376 vc->vc_translate = set_translate(vc->vc_charset == 0
1377 ? vc->vc_G0_charset
1378 : vc->vc_G1_charset, vc);
1379 vc->vc_disp_ctrl = 0;
1380 vc->vc_toggle_meta = 0;
1381 break;
1382 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1383 * Select first alternate font, lets chars < 32 be
1384 * displayed as ROM chars.
1385 */
1386 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1387 vc->vc_disp_ctrl = 1;
1388 vc->vc_toggle_meta = 0;
1389 break;
1390 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1391 * Select second alternate font, toggle high bit
1392 * before displaying as ROM char.
1393 */
1394 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1395 vc->vc_disp_ctrl = 1;
1396 vc->vc_toggle_meta = 1;
1397 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001398 case 22:
1399 vc->vc_intensity = 1;
1400 break;
1401 case 23:
1402 vc->vc_italic = 0;
1403 break;
1404 case 24:
1405 vc->vc_underline = 0;
1406 break;
1407 case 25:
1408 vc->vc_blink = 0;
1409 break;
1410 case 27:
1411 vc->vc_reverse = 0;
1412 break;
1413 case 38:
1414 i = vc_t416_color(vc, i, rgb_foreground);
1415 break;
1416 case 48:
1417 i = vc_t416_color(vc, i, rgb_background);
1418 break;
1419 case 39:
1420 vc->vc_color = (vc->vc_def_color & 0x0f) |
1421 (vc->vc_color & 0xf0);
1422 break;
1423 case 49:
1424 vc->vc_color = (vc->vc_def_color & 0xf0) |
1425 (vc->vc_color & 0x0f);
1426 break;
1427 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001428 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1429 if (vc->vc_par[i] < 100)
1430 vc->vc_intensity = 2;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001431 vc->vc_par[i] -= 60;
1432 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001433 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1434 vc->vc_color = color_table[vc->vc_par[i] - 30]
1435 | (vc->vc_color & 0xf0);
1436 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1437 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1438 | (vc->vc_color & 0x0f);
1439 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 }
1441 update_attr(vc);
1442}
1443
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001444static void respond_string(const char *p, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
1446 while (*p) {
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001447 tty_insert_flip_char(port, *p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 p++;
1449 }
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001450 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
1453static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1454{
1455 char buf[40];
1456
1457 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 +01001458 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461static inline void status_report(struct tty_struct *tty)
1462{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001463 respond_string("\033[0n", tty->port); /* Terminal ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464}
1465
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001466static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001468 respond_string(VT102ID, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469}
1470
1471void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1472{
1473 char buf[8];
1474
1475 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1476 (char)('!' + mry));
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001477 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478}
1479
1480/* invoked via ioctl(TIOCLINUX) and through set_selection */
1481int mouse_reporting(void)
1482{
1483 return vc_cons[fg_console].d->vc_report_mouse;
1484}
1485
Torben Hohnac751ef2011-01-25 15:07:35 -08001486/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static void set_mode(struct vc_data *vc, int on_off)
1488{
1489 int i;
1490
1491 for (i = 0; i <= vc->vc_npar; i++)
1492 if (vc->vc_ques) {
1493 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1494 case 1: /* Cursor keys send ^[Ox/^[[x */
1495 if (on_off)
1496 set_kbd(vc, decckm);
1497 else
1498 clr_kbd(vc, decckm);
1499 break;
1500 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501#if 0
1502 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1503 /* this alone does not suffice; some user mode
1504 utility has to change the hardware regs */
1505#endif
1506 break;
1507 case 5: /* Inverted screen on/off */
1508 if (vc->vc_decscnm != on_off) {
1509 vc->vc_decscnm = on_off;
1510 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1511 update_attr(vc);
1512 }
1513 break;
1514 case 6: /* Origin relative/absolute */
1515 vc->vc_decom = on_off;
1516 gotoxay(vc, 0, 0);
1517 break;
1518 case 7: /* Autowrap on/off */
1519 vc->vc_decawm = on_off;
1520 break;
1521 case 8: /* Autorepeat on/off */
1522 if (on_off)
1523 set_kbd(vc, decarm);
1524 else
1525 clr_kbd(vc, decarm);
1526 break;
1527 case 9:
1528 vc->vc_report_mouse = on_off ? 1 : 0;
1529 break;
1530 case 25: /* Cursor on/off */
1531 vc->vc_deccm = on_off;
1532 break;
1533 case 1000:
1534 vc->vc_report_mouse = on_off ? 2 : 0;
1535 break;
1536 }
1537 } else {
1538 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1539 case 3: /* Monitor (display ctrls) */
1540 vc->vc_disp_ctrl = on_off;
1541 break;
1542 case 4: /* Insert Mode on/off */
1543 vc->vc_decim = on_off;
1544 break;
1545 case 20: /* Lf, Enter == CrLf/Lf */
1546 if (on_off)
1547 set_kbd(vc, lnm);
1548 else
1549 clr_kbd(vc, lnm);
1550 break;
1551 }
1552 }
1553}
1554
Torben Hohnac751ef2011-01-25 15:07:35 -08001555/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static void setterm_command(struct vc_data *vc)
1557{
1558 switch(vc->vc_par[0]) {
1559 case 1: /* set color for underline mode */
1560 if (vc->vc_can_do_color &&
1561 vc->vc_par[1] < 16) {
1562 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1563 if (vc->vc_underline)
1564 update_attr(vc);
1565 }
1566 break;
1567 case 2: /* set color for half intensity mode */
1568 if (vc->vc_can_do_color &&
1569 vc->vc_par[1] < 16) {
1570 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1571 if (vc->vc_intensity == 0)
1572 update_attr(vc);
1573 }
1574 break;
1575 case 8: /* store colors as defaults */
1576 vc->vc_def_color = vc->vc_attr;
1577 if (vc->vc_hi_font_mask == 0x100)
1578 vc->vc_def_color >>= 1;
1579 default_attr(vc);
1580 update_attr(vc);
1581 break;
1582 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001583 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 poke_blanked_console();
1585 break;
1586 case 10: /* set bell frequency in Hz */
1587 if (vc->vc_npar >= 1)
1588 vc->vc_bell_pitch = vc->vc_par[1];
1589 else
1590 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1591 break;
1592 case 11: /* set bell duration in msec */
1593 if (vc->vc_npar >= 1)
1594 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
Nicholas Mc Guire3372ec22015-02-09 10:54:10 -05001595 msecs_to_jiffies(vc->vc_par[1]) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 else
1597 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1598 break;
1599 case 12: /* bring specified console to the front */
1600 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1601 set_console(vc->vc_par[1] - 1);
1602 break;
1603 case 13: /* unblank the screen */
1604 poke_blanked_console();
1605 break;
1606 case 14: /* set vesa powerdown interval */
1607 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1608 break;
1609 case 15: /* activate the previous console */
1610 set_console(last_console);
1611 break;
Scot Doylebd633642015-03-26 13:54:39 +00001612 case 16: /* set cursor blink duration in msec */
1613 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1614 vc->vc_par[1] <= USHRT_MAX)
1615 vc->vc_cur_blink_ms = vc->vc_par[1];
1616 else
1617 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1618 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
1620}
1621
Torben Hohnac751ef2011-01-25 15:07:35 -08001622/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623static void csi_at(struct vc_data *vc, unsigned int nr)
1624{
1625 if (nr > vc->vc_cols - vc->vc_x)
1626 nr = vc->vc_cols - vc->vc_x;
1627 else if (!nr)
1628 nr = 1;
1629 insert_char(vc, nr);
1630}
1631
Torben Hohnac751ef2011-01-25 15:07:35 -08001632/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633static void csi_L(struct vc_data *vc, unsigned int nr)
1634{
1635 if (nr > vc->vc_rows - vc->vc_y)
1636 nr = vc->vc_rows - vc->vc_y;
1637 else if (!nr)
1638 nr = 1;
1639 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1640 vc->vc_need_wrap = 0;
1641}
1642
Torben Hohnac751ef2011-01-25 15:07:35 -08001643/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644static void csi_P(struct vc_data *vc, unsigned int nr)
1645{
1646 if (nr > vc->vc_cols - vc->vc_x)
1647 nr = vc->vc_cols - vc->vc_x;
1648 else if (!nr)
1649 nr = 1;
1650 delete_char(vc, nr);
1651}
1652
Torben Hohnac751ef2011-01-25 15:07:35 -08001653/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654static void csi_M(struct vc_data *vc, unsigned int nr)
1655{
1656 if (nr > vc->vc_rows - vc->vc_y)
1657 nr = vc->vc_rows - vc->vc_y;
1658 else if (!nr)
1659 nr=1;
1660 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1661 vc->vc_need_wrap = 0;
1662}
1663
Torben Hohnac751ef2011-01-25 15:07:35 -08001664/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static void save_cur(struct vc_data *vc)
1666{
1667 vc->vc_saved_x = vc->vc_x;
1668 vc->vc_saved_y = vc->vc_y;
1669 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001670 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 vc->vc_s_underline = vc->vc_underline;
1672 vc->vc_s_blink = vc->vc_blink;
1673 vc->vc_s_reverse = vc->vc_reverse;
1674 vc->vc_s_charset = vc->vc_charset;
1675 vc->vc_s_color = vc->vc_color;
1676 vc->vc_saved_G0 = vc->vc_G0_charset;
1677 vc->vc_saved_G1 = vc->vc_G1_charset;
1678}
1679
Torben Hohnac751ef2011-01-25 15:07:35 -08001680/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681static void restore_cur(struct vc_data *vc)
1682{
1683 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1684 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001685 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 vc->vc_underline = vc->vc_s_underline;
1687 vc->vc_blink = vc->vc_s_blink;
1688 vc->vc_reverse = vc->vc_s_reverse;
1689 vc->vc_charset = vc->vc_s_charset;
1690 vc->vc_color = vc->vc_s_color;
1691 vc->vc_G0_charset = vc->vc_saved_G0;
1692 vc->vc_G1_charset = vc->vc_saved_G1;
1693 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1694 update_attr(vc);
1695 vc->vc_need_wrap = 0;
1696}
1697
Adam Borowskib290af62014-02-19 01:40:16 +01001698enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01001700 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Torben Hohnac751ef2011-01-25 15:07:35 -08001702/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703static void reset_terminal(struct vc_data *vc, int do_clear)
1704{
1705 vc->vc_top = 0;
1706 vc->vc_bottom = vc->vc_rows;
1707 vc->vc_state = ESnormal;
1708 vc->vc_ques = 0;
1709 vc->vc_translate = set_translate(LAT1_MAP, vc);
1710 vc->vc_G0_charset = LAT1_MAP;
1711 vc->vc_G1_charset = GRAF_MAP;
1712 vc->vc_charset = 0;
1713 vc->vc_need_wrap = 0;
1714 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001715 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 vc->vc_utf_count = 0;
1717
1718 vc->vc_disp_ctrl = 0;
1719 vc->vc_toggle_meta = 0;
1720
1721 vc->vc_decscnm = 0;
1722 vc->vc_decom = 0;
1723 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001724 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 vc->vc_decim = 0;
1726
Alan Cox079c9532012-02-28 14:49:23 +00001727 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001729 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 vc->vc_complement_mask = vc->vc_s_complement_mask;
1731
1732 default_attr(vc);
1733 update_attr(vc);
1734
Linus Torvalds7c280672018-03-24 10:43:26 +01001735 vc->vc_tab_stop[0] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 vc->vc_tab_stop[1] =
1737 vc->vc_tab_stop[2] =
1738 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001739 vc->vc_tab_stop[4] =
1740 vc->vc_tab_stop[5] =
1741 vc->vc_tab_stop[6] =
1742 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
1744 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1745 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00001746 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 gotoxy(vc, 0, 0);
1749 save_cur(vc);
1750 if (do_clear)
1751 csi_J(vc, 2);
1752}
1753
Torben Hohnac751ef2011-01-25 15:07:35 -08001754/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1756{
1757 /*
1758 * Control characters can be used in the _middle_
1759 * of an escape sequence.
1760 */
Adam Borowski63f3a162014-02-19 01:38:04 +01001761 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1762 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 switch (c) {
1764 case 0:
1765 return;
1766 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01001767 if (vc->vc_state == ESosc)
1768 vc->vc_state = ESnormal;
1769 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1771 return;
1772 case 8:
1773 bs(vc);
1774 return;
1775 case 9:
1776 vc->vc_pos -= (vc->vc_x << 1);
1777 while (vc->vc_x < vc->vc_cols - 1) {
1778 vc->vc_x++;
Linus Torvalds7c280672018-03-24 10:43:26 +01001779 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 break;
1781 }
1782 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001783 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 return;
1785 case 10: case 11: case 12:
1786 lf(vc);
1787 if (!is_kbd(vc, lnm))
1788 return;
1789 case 13:
1790 cr(vc);
1791 return;
1792 case 14:
1793 vc->vc_charset = 1;
1794 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1795 vc->vc_disp_ctrl = 1;
1796 return;
1797 case 15:
1798 vc->vc_charset = 0;
1799 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1800 vc->vc_disp_ctrl = 0;
1801 return;
1802 case 24: case 26:
1803 vc->vc_state = ESnormal;
1804 return;
1805 case 27:
1806 vc->vc_state = ESesc;
1807 return;
1808 case 127:
1809 del(vc);
1810 return;
1811 case 128+27:
1812 vc->vc_state = ESsquare;
1813 return;
1814 }
1815 switch(vc->vc_state) {
1816 case ESesc:
1817 vc->vc_state = ESnormal;
1818 switch (c) {
1819 case '[':
1820 vc->vc_state = ESsquare;
1821 return;
1822 case ']':
1823 vc->vc_state = ESnonstd;
1824 return;
1825 case '%':
1826 vc->vc_state = ESpercent;
1827 return;
1828 case 'E':
1829 cr(vc);
1830 lf(vc);
1831 return;
1832 case 'M':
1833 ri(vc);
1834 return;
1835 case 'D':
1836 lf(vc);
1837 return;
1838 case 'H':
Linus Torvalds7c280672018-03-24 10:43:26 +01001839 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 return;
1841 case 'Z':
1842 respond_ID(tty);
1843 return;
1844 case '7':
1845 save_cur(vc);
1846 return;
1847 case '8':
1848 restore_cur(vc);
1849 return;
1850 case '(':
1851 vc->vc_state = ESsetG0;
1852 return;
1853 case ')':
1854 vc->vc_state = ESsetG1;
1855 return;
1856 case '#':
1857 vc->vc_state = EShash;
1858 return;
1859 case 'c':
1860 reset_terminal(vc, 1);
1861 return;
1862 case '>': /* Numeric keypad */
1863 clr_kbd(vc, kbdapplic);
1864 return;
1865 case '=': /* Appl. keypad */
1866 set_kbd(vc, kbdapplic);
1867 return;
1868 }
1869 return;
1870 case ESnonstd:
1871 if (c=='P') { /* palette escape sequence */
1872 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1873 vc->vc_par[vc->vc_npar] = 0;
1874 vc->vc_npar = 0;
1875 vc->vc_state = ESpalette;
1876 return;
1877 } else if (c=='R') { /* reset palette */
1878 reset_palette(vc);
1879 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01001880 } else if (c>='0' && c<='9')
1881 vc->vc_state = ESosc;
1882 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 vc->vc_state = ESnormal;
1884 return;
1885 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001886 if (isxdigit(c)) {
1887 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (vc->vc_npar == 7) {
1889 int i = vc->vc_par[0] * 3, j = 1;
1890 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1891 vc->vc_palette[i++] += vc->vc_par[j++];
1892 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1893 vc->vc_palette[i++] += vc->vc_par[j++];
1894 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1895 vc->vc_palette[i] += vc->vc_par[j];
1896 set_palette(vc);
1897 vc->vc_state = ESnormal;
1898 }
1899 } else
1900 vc->vc_state = ESnormal;
1901 return;
1902 case ESsquare:
1903 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1904 vc->vc_par[vc->vc_npar] = 0;
1905 vc->vc_npar = 0;
1906 vc->vc_state = ESgetpars;
1907 if (c == '[') { /* Function key */
1908 vc->vc_state=ESfunckey;
1909 return;
1910 }
1911 vc->vc_ques = (c == '?');
1912 if (vc->vc_ques)
1913 return;
1914 case ESgetpars:
1915 if (c == ';' && vc->vc_npar < NPAR - 1) {
1916 vc->vc_npar++;
1917 return;
1918 } else if (c>='0' && c<='9') {
1919 vc->vc_par[vc->vc_npar] *= 10;
1920 vc->vc_par[vc->vc_npar] += c - '0';
1921 return;
Adam Borowskib290af62014-02-19 01:40:16 +01001922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 vc->vc_state = ESnormal;
1924 switch(c) {
1925 case 'h':
1926 set_mode(vc, 1);
1927 return;
1928 case 'l':
1929 set_mode(vc, 0);
1930 return;
1931 case 'c':
1932 if (vc->vc_ques) {
1933 if (vc->vc_par[0])
1934 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1935 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001936 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 return;
1938 }
1939 break;
1940 case 'm':
1941 if (vc->vc_ques) {
1942 clear_selection();
1943 if (vc->vc_par[0])
1944 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1945 else
1946 vc->vc_complement_mask = vc->vc_s_complement_mask;
1947 return;
1948 }
1949 break;
1950 case 'n':
1951 if (!vc->vc_ques) {
1952 if (vc->vc_par[0] == 5)
1953 status_report(tty);
1954 else if (vc->vc_par[0] == 6)
1955 cursor_report(vc, tty);
1956 }
1957 return;
1958 }
1959 if (vc->vc_ques) {
1960 vc->vc_ques = 0;
1961 return;
1962 }
1963 switch(c) {
1964 case 'G': case '`':
1965 if (vc->vc_par[0])
1966 vc->vc_par[0]--;
1967 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1968 return;
1969 case 'A':
1970 if (!vc->vc_par[0])
1971 vc->vc_par[0]++;
1972 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1973 return;
1974 case 'B': case 'e':
1975 if (!vc->vc_par[0])
1976 vc->vc_par[0]++;
1977 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1978 return;
1979 case 'C': case 'a':
1980 if (!vc->vc_par[0])
1981 vc->vc_par[0]++;
1982 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1983 return;
1984 case 'D':
1985 if (!vc->vc_par[0])
1986 vc->vc_par[0]++;
1987 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1988 return;
1989 case 'E':
1990 if (!vc->vc_par[0])
1991 vc->vc_par[0]++;
1992 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1993 return;
1994 case 'F':
1995 if (!vc->vc_par[0])
1996 vc->vc_par[0]++;
1997 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1998 return;
1999 case 'd':
2000 if (vc->vc_par[0])
2001 vc->vc_par[0]--;
2002 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2003 return;
2004 case 'H': case 'f':
2005 if (vc->vc_par[0])
2006 vc->vc_par[0]--;
2007 if (vc->vc_par[1])
2008 vc->vc_par[1]--;
2009 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2010 return;
2011 case 'J':
2012 csi_J(vc, vc->vc_par[0]);
2013 return;
2014 case 'K':
2015 csi_K(vc, vc->vc_par[0]);
2016 return;
2017 case 'L':
2018 csi_L(vc, vc->vc_par[0]);
2019 return;
2020 case 'M':
2021 csi_M(vc, vc->vc_par[0]);
2022 return;
2023 case 'P':
2024 csi_P(vc, vc->vc_par[0]);
2025 return;
2026 case 'c':
2027 if (!vc->vc_par[0])
2028 respond_ID(tty);
2029 return;
2030 case 'g':
2031 if (!vc->vc_par[0])
Linus Torvalds7c280672018-03-24 10:43:26 +01002032 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 else if (vc->vc_par[0] == 3) {
2034 vc->vc_tab_stop[0] =
2035 vc->vc_tab_stop[1] =
2036 vc->vc_tab_stop[2] =
2037 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08002038 vc->vc_tab_stop[4] =
2039 vc->vc_tab_stop[5] =
2040 vc->vc_tab_stop[6] =
2041 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 return;
2044 case 'm':
2045 csi_m(vc);
2046 return;
2047 case 'q': /* DECLL - but only 3 leds */
2048 /* map 0,1,2,3 to 0,1,2,4 */
2049 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002050 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2052 return;
2053 case 'r':
2054 if (!vc->vc_par[0])
2055 vc->vc_par[0]++;
2056 if (!vc->vc_par[1])
2057 vc->vc_par[1] = vc->vc_rows;
2058 /* Minimum allowed region is 2 lines */
2059 if (vc->vc_par[0] < vc->vc_par[1] &&
2060 vc->vc_par[1] <= vc->vc_rows) {
2061 vc->vc_top = vc->vc_par[0] - 1;
2062 vc->vc_bottom = vc->vc_par[1];
2063 gotoxay(vc, 0, 0);
2064 }
2065 return;
2066 case 's':
2067 save_cur(vc);
2068 return;
2069 case 'u':
2070 restore_cur(vc);
2071 return;
2072 case 'X':
2073 csi_X(vc, vc->vc_par[0]);
2074 return;
2075 case '@':
2076 csi_at(vc, vc->vc_par[0]);
2077 return;
2078 case ']': /* setterm functions */
2079 setterm_command(vc);
2080 return;
2081 }
2082 return;
2083 case ESpercent:
2084 vc->vc_state = ESnormal;
2085 switch (c) {
2086 case '@': /* defined in ISO 2022 */
2087 vc->vc_utf = 0;
2088 return;
2089 case 'G': /* prelim official escape code */
2090 case '8': /* retained for compatibility */
2091 vc->vc_utf = 1;
2092 return;
2093 }
2094 return;
2095 case ESfunckey:
2096 vc->vc_state = ESnormal;
2097 return;
2098 case EShash:
2099 vc->vc_state = ESnormal;
2100 if (c == '8') {
2101 /* DEC screen alignment test. kludge :-) */
2102 vc->vc_video_erase_char =
2103 (vc->vc_video_erase_char & 0xff00) | 'E';
2104 csi_J(vc, 2);
2105 vc->vc_video_erase_char =
2106 (vc->vc_video_erase_char & 0xff00) | ' ';
2107 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2108 }
2109 return;
2110 case ESsetG0:
2111 if (c == '0')
2112 vc->vc_G0_charset = GRAF_MAP;
2113 else if (c == 'B')
2114 vc->vc_G0_charset = LAT1_MAP;
2115 else if (c == 'U')
2116 vc->vc_G0_charset = IBMPC_MAP;
2117 else if (c == 'K')
2118 vc->vc_G0_charset = USER_MAP;
2119 if (vc->vc_charset == 0)
2120 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2121 vc->vc_state = ESnormal;
2122 return;
2123 case ESsetG1:
2124 if (c == '0')
2125 vc->vc_G1_charset = GRAF_MAP;
2126 else if (c == 'B')
2127 vc->vc_G1_charset = LAT1_MAP;
2128 else if (c == 'U')
2129 vc->vc_G1_charset = IBMPC_MAP;
2130 else if (c == 'K')
2131 vc->vc_G1_charset = USER_MAP;
2132 if (vc->vc_charset == 1)
2133 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2134 vc->vc_state = ESnormal;
2135 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002136 case ESosc:
2137 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 default:
2139 vc->vc_state = ESnormal;
2140 }
2141}
2142
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002143/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002144 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002145 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2146 */
2147struct interval {
2148 uint32_t first;
2149 uint32_t last;
2150};
2151
2152static int bisearch(uint32_t ucs, const struct interval *table, int max)
2153{
2154 int min = 0;
2155 int mid;
2156
2157 if (ucs < table[0].first || ucs > table[max].last)
2158 return 0;
2159 while (max >= min) {
2160 mid = (min + max) / 2;
2161 if (ucs > table[mid].last)
2162 min = mid + 1;
2163 else if (ucs < table[mid].first)
2164 max = mid - 1;
2165 else
2166 return 1;
2167 }
2168 return 0;
2169}
2170
2171static int is_double_width(uint32_t ucs)
2172{
2173 static const struct interval double_width[] = {
2174 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2175 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002176 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2177 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002178 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002179 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002180}
2181
Jiri Slabyd711ea82016-06-23 13:34:30 +02002182static void con_flush(struct vc_data *vc, unsigned long draw_from,
2183 unsigned long draw_to, int *draw_x)
2184{
2185 if (*draw_x < 0)
2186 return;
2187
2188 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2189 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2190 *draw_x = -1;
2191}
2192
Torben Hohnac751ef2011-01-25 15:07:35 -08002193/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2195{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 int c, tc, ok, n = 0, draw_x = -1;
2197 unsigned int currcons;
2198 unsigned long draw_from = 0, draw_to = 0;
2199 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002200 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002201 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002202 uint8_t rescan;
2203 uint8_t inverse;
2204 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 if (in_interrupt())
2208 return count;
2209
2210 might_sleep();
2211
Torben Hohnac751ef2011-01-25 15:07:35 -08002212 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 vc = tty->driver_data;
2214 if (vc == NULL) {
2215 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002216 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return 0;
2218 }
2219
2220 currcons = vc->vc_num;
2221 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002222 /* could this happen? */
2223 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2224 console_unlock();
2225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 himask = vc->vc_hi_font_mask;
2229 charmask = himask ? 0x1ff : 0xff;
2230
2231 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002232 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 hide_cursor(vc);
2234
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002235 param.vc = vc;
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 while (!tty->stopped && count) {
2238 int orig = *buf;
2239 c = orig;
2240 buf++;
2241 n++;
2242 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002243 rescan = 0;
2244 inverse = 0;
2245 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
2247 /* Do no translation at all in control states */
2248 if (vc->vc_state != ESnormal) {
2249 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002250 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002251 /* Combine UTF-8 into Unicode in vc_utf_char.
2252 * vc_utf_count is the number of continuation bytes still
2253 * expected to arrive.
2254 * vc_npar is the number of continuation bytes arrived so
2255 * far
2256 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002257rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002258 if ((c & 0xc0) == 0x80) {
2259 /* Continuation byte received */
2260 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002261 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002262 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2263 vc->vc_npar++;
2264 if (--vc->vc_utf_count) {
2265 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002267 }
2268 /* Got a whole character */
2269 c = vc->vc_utf_char;
2270 /* Reject overlong sequences */
2271 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2272 c > utf8_length_changes[vc->vc_npar])
2273 c = 0xfffd;
2274 } else {
2275 /* Unexpected continuation byte */
2276 vc->vc_utf_count = 0;
2277 c = 0xfffd;
2278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002280 /* Single ASCII byte or first byte of a sequence received */
2281 if (vc->vc_utf_count) {
2282 /* Continuation byte expected */
2283 rescan = 1;
2284 vc->vc_utf_count = 0;
2285 c = 0xfffd;
2286 } else if (c > 0x7f) {
2287 /* First byte of a multibyte sequence received */
2288 vc->vc_npar = 0;
2289 if ((c & 0xe0) == 0xc0) {
2290 vc->vc_utf_count = 1;
2291 vc->vc_utf_char = (c & 0x1f);
2292 } else if ((c & 0xf0) == 0xe0) {
2293 vc->vc_utf_count = 2;
2294 vc->vc_utf_char = (c & 0x0f);
2295 } else if ((c & 0xf8) == 0xf0) {
2296 vc->vc_utf_count = 3;
2297 vc->vc_utf_char = (c & 0x07);
2298 } else if ((c & 0xfc) == 0xf8) {
2299 vc->vc_utf_count = 4;
2300 vc->vc_utf_char = (c & 0x03);
2301 } else if ((c & 0xfe) == 0xfc) {
2302 vc->vc_utf_count = 5;
2303 vc->vc_utf_char = (c & 0x01);
2304 } else {
2305 /* 254 and 255 are invalid */
2306 c = 0xfffd;
2307 }
2308 if (vc->vc_utf_count) {
2309 /* Still need some bytes */
2310 continue;
2311 }
2312 }
2313 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002315 /* End of UTF-8 decoding. */
2316 /* c is the received character, or U+FFFD for invalid sequences. */
2317 /* Replace invalid Unicode code points with U+FFFD too */
2318 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2319 c = 0xfffd;
2320 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002321 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002322 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 }
2324
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002325 param.c = tc;
2326 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2327 &param) == NOTIFY_STOP)
2328 continue;
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 /* If the original code was a control character we
2331 * only allow a glyph to be displayed if the code is
2332 * not normally used (such as for cursor movement) or
2333 * if the disp_ctrl mode has been explicitly enabled.
2334 * Certain characters (as given by the CTRL_ALWAYS
2335 * bitmap) are always displayed as control characters,
2336 * as the console would be pretty useless without
2337 * them; to display an arbitrary font position use the
2338 * direct-to-font zone in UTF-8 mode.
2339 */
2340 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002341 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2342 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 && (c != 127 || vc->vc_disp_ctrl)
2344 && (c != 128+27);
2345
2346 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002347 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2348 if (is_double_width(c))
2349 width = 2;
2350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* Now try to find out how to display it */
2352 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002353 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002354 if (tc == -1 || tc == -2) {
2355 continue; /* nothing to display */
2356 }
2357 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002358 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002359 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002360 This would make absolutely no sense with Unicode in mind,
2361 but do this for ASCII characters since a font may lack
2362 Unicode mapping info and we don't want to end up with
2363 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002364 tc = c;
2365 } else {
2366 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2367 tc = conv_uni_to_pc(vc, 0xfffd);
2368 if (tc < 0) {
2369 inverse = 1;
2370 tc = conv_uni_to_pc(vc, '?');
2371 if (tc < 0) tc = '?';
2372 }
2373 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002374 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002376 if (!inverse) {
2377 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002379 /* invert vc_attr */
2380 if (!vc->vc_can_do_color) {
2381 vc_attr = (vc->vc_attr) ^ 0x08;
2382 } else if (vc->vc_hi_font_mask == 0x100) {
2383 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2384 } else {
2385 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002386 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002387 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002388 }
2389
2390 while (1) {
2391 if (vc->vc_need_wrap || vc->vc_decim)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002392 con_flush(vc, draw_from, draw_to,
2393 &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002394 if (vc->vc_need_wrap) {
2395 cr(vc);
2396 lf(vc);
2397 }
2398 if (vc->vc_decim)
2399 insert_char(vc, 1);
2400 scr_writew(himask ?
2401 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2402 (vc_attr << 8) + tc,
2403 (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002404 if (con_should_update(vc) && draw_x < 0) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002405 draw_x = vc->vc_x;
2406 draw_from = vc->vc_pos;
2407 }
2408 if (vc->vc_x == vc->vc_cols - 1) {
2409 vc->vc_need_wrap = vc->vc_decawm;
2410 draw_to = vc->vc_pos + 2;
2411 } else {
2412 vc->vc_x++;
2413 draw_to = (vc->vc_pos += 2);
2414 }
2415
2416 if (!--width) break;
2417
2418 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2419 if (tc < 0) tc = ' ';
2420 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002421 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002422
Jiri Slabyd711ea82016-06-23 13:34:30 +02002423 if (inverse)
2424 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002425
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002426 if (rescan) {
2427 rescan = 0;
2428 inverse = 0;
2429 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002430 c = orig;
2431 goto rescan_last_byte;
2432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 continue;
2434 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002435 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 do_con_trol(tty, vc, orig);
2437 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002438 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002440 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002441 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443}
2444
2445/*
2446 * This is the console switching callback.
2447 *
2448 * Doing console switching in a process context allows
2449 * us to do the switches asynchronously (needed when we want
2450 * to switch due to a keyboard interrupt). Synchronization
2451 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002452 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 */
David Howells65f27f32006-11-22 14:55:48 +00002454static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455{
Torben Hohnac751ef2011-01-25 15:07:35 -08002456 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 if (want_console >= 0) {
2459 if (want_console != fg_console &&
2460 vc_cons_allocated(want_console)) {
2461 hide_cursor(vc_cons[fg_console].d);
2462 change_console(vc_cons[want_console].d);
2463 /* we only changed when the console had already
2464 been allocated - a new console is not created
2465 in an interrupt routine */
2466 }
2467 want_console = -1;
2468 }
2469 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2470 do_poke_blanked_console = 0;
2471 poke_blanked_console();
2472 }
2473 if (scrollback_delta) {
2474 struct vc_data *vc = vc_cons[fg_console].d;
2475 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002476 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2478 scrollback_delta = 0;
2479 }
2480 if (blank_timer_expired) {
2481 do_blank_screen(0);
2482 blank_timer_expired = 0;
2483 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002484 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Torben Hohnac751ef2011-01-25 15:07:35 -08002486 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487}
2488
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002489int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002491 struct vc_data *vc = vc_cons[fg_console].d;
2492
2493 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2494 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2495
2496 /*
2497 * Console switch will fail in console_callback() or
2498 * change_console() so there is no point scheduling
2499 * the callback
2500 *
2501 * Existing set_console() users don't check the return
2502 * value so this shouldn't break anything
2503 */
2504 return -EINVAL;
2505 }
2506
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 want_console = nr;
2508 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002509
2510 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511}
2512
2513struct tty_driver *console_driver;
2514
2515#ifdef CONFIG_VT_CONSOLE
2516
Bernhard Walle5ada9182009-12-14 18:00:43 -08002517/**
2518 * vt_kmsg_redirect() - Sets/gets the kernel message console
2519 * @new: The new virtual terminal number or -1 if the console should stay
2520 * unchanged
2521 *
2522 * By default, the kernel messages are always printed on the current virtual
2523 * console. However, the user may modify that default with the
2524 * TIOCL_SETKMSGREDIRECT ioctl call.
2525 *
2526 * This function sets the kernel message console to be @new. It returns the old
2527 * virtual console number. The virtual terminal number 0 (both as parameter and
2528 * return value) means no redirection (i.e. always printed on the currently
2529 * active console).
2530 *
2531 * The parameter -1 means that only the current console is returned, but the
2532 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2533 * case to make the code more understandable.
2534 *
2535 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2536 * the parameter and always returns 0.
2537 */
2538int vt_kmsg_redirect(int new)
2539{
2540 static int kmsg_con;
2541
2542 if (new != -1)
2543 return xchg(&kmsg_con, new);
2544 else
2545 return kmsg_con;
2546}
2547
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548/*
2549 * Console on virtual terminal
2550 *
2551 * The console must be locked when we get here.
2552 */
2553
2554static void vt_console_print(struct console *co, const char *b, unsigned count)
2555{
2556 struct vc_data *vc = vc_cons[fg_console].d;
2557 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002558 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 const ushort *start;
2560 ushort cnt = 0;
2561 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002562 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
2564 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002565 if (!printable)
2566 return;
2567 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 return;
2569
Bernhard Walle5ada9182009-12-14 18:00:43 -08002570 kmsg_console = vt_get_kmsg_redirect();
2571 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2572 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
2574 /* read `x' only after setting currcons properly (otherwise
2575 the `x' macro will read the x of the foreground console). */
2576 myx = vc->vc_x;
2577
2578 if (!vc_cons_allocated(fg_console)) {
2579 /* impossible */
2580 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2581 goto quit;
2582 }
2583
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002584 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 goto quit;
2586
2587 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002588 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 hide_cursor(vc);
2590
2591 start = (ushort *)vc->vc_pos;
2592
2593 /* Contrived structure to try to emulate original need_wrap behaviour
2594 * Problems caused when we have need_wrap set on '\n' character */
2595 while (count--) {
2596 c = *b++;
2597 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2598 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002599 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2601 vc->vc_x += cnt;
2602 if (vc->vc_need_wrap)
2603 vc->vc_x--;
2604 cnt = 0;
2605 }
2606 if (c == 8) { /* backspace */
2607 bs(vc);
2608 start = (ushort *)vc->vc_pos;
2609 myx = vc->vc_x;
2610 continue;
2611 }
2612 if (c != 13)
2613 lf(vc);
2614 cr(vc);
2615 start = (ushort *)vc->vc_pos;
2616 myx = vc->vc_x;
2617 if (c == 10 || c == 13)
2618 continue;
2619 }
2620 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002621 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 cnt++;
2623 if (myx == vc->vc_cols - 1) {
2624 vc->vc_need_wrap = 1;
2625 continue;
2626 }
2627 vc->vc_pos += 2;
2628 myx++;
2629 }
2630 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002631 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2633 vc->vc_x += cnt;
2634 if (vc->vc_x == vc->vc_cols) {
2635 vc->vc_x--;
2636 vc->vc_need_wrap = 1;
2637 }
2638 }
2639 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002640 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641
2642quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002643 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
2646static struct tty_driver *vt_console_device(struct console *c, int *index)
2647{
2648 *index = c->index ? c->index-1 : fg_console;
2649 return console_driver;
2650}
2651
2652static struct console vt_console_driver = {
2653 .name = "tty",
2654 .write = vt_console_print,
2655 .device = vt_console_device,
2656 .unblank = unblank_screen,
2657 .flags = CON_PRINTBUFFER,
2658 .index = -1,
2659};
2660#endif
2661
2662/*
2663 * Handling of Linux-specific VC ioctls
2664 */
2665
2666/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002667 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 *
2669 * There are some functions which don't need it.
2670 *
2671 * There are some functions which can sleep for arbitrary periods
2672 * (paste_selection) but we don't need the lock there anyway.
2673 *
2674 * set_selection has locking, and definitely needs it
2675 */
2676
2677int tioclinux(struct tty_struct *tty, unsigned long arg)
2678{
2679 char type, data;
2680 char __user *p = (char __user *)arg;
2681 int lines;
2682 int ret;
2683
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2685 return -EPERM;
2686 if (get_user(type, p))
2687 return -EFAULT;
2688 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002689
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 switch (type)
2691 {
2692 case TIOCL_SETSEL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
2695 case TIOCL_PASTESEL:
2696 ret = paste_selection(tty);
2697 break;
2698 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002699 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002701 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 break;
2703 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00002704 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00002706 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 break;
2708 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002709
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 /*
2711 * Make it possible to react to Shift+Mousebutton.
2712 * Note that 'shift_state' is an undocumented
2713 * kernel-internal variable; programs not closely
2714 * related to the kernel should not use this.
2715 */
Alan Cox079c9532012-02-28 14:49:23 +00002716 data = vt_get_shift_state();
Adam Borowski63c634c2017-06-03 09:35:06 +02002717 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 break;
2719 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00002720 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00002722 console_unlock();
Adam Borowski63c634c2017-06-03 09:35:06 +02002723 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 break;
2725 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00002726 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002727 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00002728 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002730 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002731 data = vt_get_kmsg_redirect();
Adam Borowski63c634c2017-06-03 09:35:06 +02002732 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002733 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 case TIOCL_SETKMSGREDIRECT:
2735 if (!capable(CAP_SYS_ADMIN)) {
2736 ret = -EPERM;
2737 } else {
2738 if (get_user(data, p+1))
2739 ret = -EFAULT;
2740 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002741 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 }
2743 break;
2744 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00002745 /* No locking needed as this is a transiently
2746 correct return anyway if the caller hasn't
2747 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 ret = fg_console;
2749 break;
2750 case TIOCL_SCROLLCONSOLE:
2751 if (get_user(lines, (s32 __user *)(p+4))) {
2752 ret = -EFAULT;
2753 } else {
Alan Cox20f62572012-03-02 14:59:37 +00002754 /* Need the console lock here. Note that lots
2755 of other calls need fixing before the lock
2756 is actually useful ! */
2757 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00002759 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 ret = 0;
2761 }
2762 break;
2763 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08002764 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 ignore_poke = 1;
2766 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08002767 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 break;
2769 case TIOCL_BLANKEDSCREEN:
2770 ret = console_blanked;
2771 break;
2772 default:
2773 ret = -EINVAL;
2774 break;
2775 }
2776 return ret;
2777}
2778
2779/*
2780 * /dev/ttyN handling
2781 */
2782
2783static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2784{
2785 int retval;
2786
2787 retval = do_con_write(tty, buf, count);
2788 con_flush_chars(tty);
2789
2790 return retval;
2791}
2792
Alan Cox5d19f542008-04-30 00:54:08 -07002793static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794{
2795 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002796 return 0; /* n_r3964 calls put_char() from interrupt context */
2797 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798}
2799
2800static int con_write_room(struct tty_struct *tty)
2801{
2802 if (tty->stopped)
2803 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002804 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805}
2806
2807static int con_chars_in_buffer(struct tty_struct *tty)
2808{
2809 return 0; /* we're not buffering */
2810}
2811
2812/*
2813 * con_throttle and con_unthrottle are only used for
2814 * paste_selection(), which has to stuff in a large number of
2815 * characters...
2816 */
2817static void con_throttle(struct tty_struct *tty)
2818{
2819}
2820
2821static void con_unthrottle(struct tty_struct *tty)
2822{
2823 struct vc_data *vc = tty->driver_data;
2824
2825 wake_up_interruptible(&vc->paste_wait);
2826}
2827
2828/*
2829 * Turn the Scroll-Lock LED on when the tty is stopped
2830 */
2831static void con_stop(struct tty_struct *tty)
2832{
2833 int console_num;
2834 if (!tty)
2835 return;
2836 console_num = tty->index;
2837 if (!vc_cons_allocated(console_num))
2838 return;
Alan Cox079c9532012-02-28 14:49:23 +00002839 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
2842/*
2843 * Turn the Scroll-Lock LED off when the console is started
2844 */
2845static void con_start(struct tty_struct *tty)
2846{
2847 int console_num;
2848 if (!tty)
2849 return;
2850 console_num = tty->index;
2851 if (!vc_cons_allocated(console_num))
2852 return;
Alan Cox079c9532012-02-28 14:49:23 +00002853 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
2856static void con_flush_chars(struct tty_struct *tty)
2857{
2858 struct vc_data *vc;
2859
2860 if (in_interrupt()) /* from flush_to_ldisc */
2861 return;
2862
2863 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08002864 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 vc = tty->driver_data;
2866 if (vc)
2867 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08002868 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869}
2870
2871/*
2872 * Allocate the console screen memory.
2873 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002874static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
2876 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002877 struct vc_data *vc;
2878 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Torben Hohnac751ef2011-01-25 15:07:35 -08002880 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002881 ret = vc_allocate(currcons);
2882 if (ret)
2883 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01002884
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002885 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002887 /* Still being freed */
2888 if (vc->port.tty) {
2889 ret = -ERESTARTSYS;
2890 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002892
2893 ret = tty_port_install(&vc->port, driver, tty);
2894 if (ret)
2895 goto unlock;
2896
2897 tty->driver_data = vc;
2898 vc->port.tty = tty;
2899
2900 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2901 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2902 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2903 }
2904 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01002905 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002906 else
Alan Coxadc8d742012-07-14 15:31:47 +01002907 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002908unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08002909 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 return ret;
2911}
2912
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002913static int con_open(struct tty_struct *tty, struct file *filp)
2914{
2915 /* everything done in install */
2916 return 0;
2917}
2918
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920static void con_close(struct tty_struct *tty, struct file *filp)
2921{
Alan Coxfeebed62008-10-13 10:41:30 +01002922 /* Nothing to do - we defer to shutdown */
2923}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Alan Coxfeebed62008-10-13 10:41:30 +01002925static void con_shutdown(struct tty_struct *tty)
2926{
2927 struct vc_data *vc = tty->driver_data;
2928 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08002929 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02002930 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08002931 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002934static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002935static int default_italic_color = 2; // green (ASCII)
2936static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002937module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002938module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2939module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941static void vc_init(struct vc_data *vc, unsigned int rows,
2942 unsigned int cols, int do_clear)
2943{
2944 int j, k ;
2945
2946 vc->vc_cols = cols;
2947 vc->vc_rows = rows;
2948 vc->vc_size_row = cols << 1;
2949 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2950
2951 set_origin(vc);
2952 vc->vc_pos = vc->vc_origin;
2953 reset_vc(vc);
2954 for (j=k=0; j<16; j++) {
2955 vc->vc_palette[k++] = default_red[j] ;
2956 vc->vc_palette[k++] = default_grn[j] ;
2957 vc->vc_palette[k++] = default_blu[j] ;
2958 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002959 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002960 vc->vc_ulcolor = default_underline_color;
2961 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 vc->vc_halfcolor = 0x08; /* grey */
2963 init_waitqueue_head(&vc->paste_wait);
2964 reset_terminal(vc, do_clear);
2965}
2966
2967/*
2968 * This routine initializes console interrupts, and does nothing
2969 * else. If you want the screen to clear, call tty_write with
2970 * the appropriate escape-sequence.
2971 */
2972
2973static int __init con_init(void)
2974{
2975 const char *display_desc = NULL;
2976 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002977 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Torben Hohnac751ef2011-01-25 15:07:35 -08002979 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 if (conswitchp)
2982 display_desc = conswitchp->con_startup();
2983 if (!display_desc) {
2984 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002985 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 return 0;
2987 }
2988
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002989 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2990 struct con_driver *con_driver = &registered_con_driver[i];
2991
2992 if (con_driver->con == NULL) {
2993 con_driver->con = conswitchp;
2994 con_driver->desc = display_desc;
2995 con_driver->flag = CON_DRIVER_FLAG_INIT;
2996 con_driver->first = 0;
2997 con_driver->last = MAX_NR_CONSOLES - 1;
2998 break;
2999 }
3000 }
3001
3002 for (i = 0; i < MAX_NR_CONSOLES; i++)
3003 con_driver_map[i] = conswitchp;
3004
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 if (blankinterval) {
3006 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003007 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
3009
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003011 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003012 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003013 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003015 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 vc_init(vc, vc->vc_rows, vc->vc_cols,
3017 currcons || !vc->vc_sw->con_save_screen);
3018 }
3019 currcons = fg_console = 0;
3020 master_display_fg = vc = vc_cons[currcons].d;
3021 set_origin(vc);
3022 save_screen(vc);
3023 gotoxy(vc, vc->vc_x, vc->vc_y);
3024 csi_J(vc, 0);
3025 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003026 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 vc->vc_can_do_color ? "colour" : "mono",
3028 display_desc, vc->vc_cols, vc->vc_rows);
3029 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
Torben Hohnac751ef2011-01-25 15:07:35 -08003031 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
3033#ifdef CONFIG_VT_CONSOLE
3034 register_console(&vt_console_driver);
3035#endif
3036 return 0;
3037}
3038console_initcall(con_init);
3039
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003040static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003041 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .open = con_open,
3043 .close = con_close,
3044 .write = con_write,
3045 .write_room = con_write_room,
3046 .put_char = con_put_char,
3047 .flush_chars = con_flush_chars,
3048 .chars_in_buffer = con_chars_in_buffer,
3049 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003050#ifdef CONFIG_COMPAT
3051 .compat_ioctl = vt_compat_ioctl,
3052#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 .stop = con_stop,
3054 .start = con_start,
3055 .throttle = con_throttle,
3056 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003057 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01003058 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059};
3060
Alan Coxd81ed102008-10-13 10:41:42 +01003061static struct cdev vc0_cdev;
3062
Kay Sieversfbc92a32010-12-01 18:51:05 +01003063static ssize_t show_tty_active(struct device *dev,
3064 struct device_attribute *attr, char *buf)
3065{
3066 return sprintf(buf, "tty%d\n", fg_console + 1);
3067}
3068static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3069
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003070static struct attribute *vt_dev_attrs[] = {
3071 &dev_attr_active.attr,
3072 NULL
3073};
3074
3075ATTRIBUTE_GROUPS(vt_dev);
3076
Alan Coxd81ed102008-10-13 10:41:42 +01003077int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078{
Alan Coxd81ed102008-10-13 10:41:42 +01003079 cdev_init(&vc0_cdev, console_fops);
3080 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3081 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3082 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003083 tty0dev = device_create_with_groups(tty_class, NULL,
3084 MKDEV(TTY_MAJOR, 0), NULL,
3085 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003086 if (IS_ERR(tty0dev))
3087 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 vcs_init();
3090
3091 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3092 if (!console_driver)
3093 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 console_driver->name = "tty";
3096 console_driver->name_base = 1;
3097 console_driver->major = TTY_MAJOR;
3098 console_driver->minor_start = 1;
3099 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3100 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003101 if (default_utf8)
3102 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3104 tty_set_operations(console_driver, &con_ops);
3105 if (tty_register_driver(console_driver))
3106 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 kbd_init();
3108 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109#ifdef CONFIG_MDA_CONSOLE
3110 mda_console_init();
3111#endif
3112 return 0;
3113}
3114
3115#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003116
3117static struct class *vtconsole_class;
3118
Alan Cox50e244c2013-01-25 10:28:15 +10003119static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003120 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003122 struct module *owner = csw->owner;
3123 const char *desc = NULL;
3124 struct con_driver *con_driver;
3125 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (!try_module_get(owner))
3128 return -ENODEV;
3129
Alan Cox50e244c2013-01-25 10:28:15 +10003130 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003131
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003132 /* check if driver is registered */
3133 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3134 con_driver = &registered_con_driver[i];
3135
3136 if (con_driver->con == csw) {
3137 desc = con_driver->desc;
3138 retval = 0;
3139 break;
3140 }
3141 }
3142
3143 if (retval)
3144 goto err;
3145
3146 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3147 csw->con_startup();
3148 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (deflt) {
3152 if (conswitchp)
3153 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003154
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 __module_get(owner);
3156 conswitchp = csw;
3157 }
3158
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003159 first = max(first, con_driver->first);
3160 last = min(last, con_driver->last);
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 for (i = first; i <= last; i++) {
3163 int old_was_color;
3164 struct vc_data *vc = vc_cons[i].d;
3165
3166 if (con_driver_map[i])
3167 module_put(con_driver_map[i]->owner);
3168 __module_get(owner);
3169 con_driver_map[i] = csw;
3170
3171 if (!vc || !vc->vc_sw)
3172 continue;
3173
3174 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003175
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003176 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003177 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003179 }
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 old_was_color = vc->vc_can_do_color;
3182 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003183 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003185 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 update_attr(vc);
3187
3188 /* If the console changed between mono <-> color, then
3189 * the attributes in the screenbuf will be wrong. The
3190 * following resets all attributes to something sane.
3191 */
3192 if (old_was_color != vc->vc_can_do_color)
3193 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003195
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003196 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if (!deflt)
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003198 printk(KERN_CONT "consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003199 if (j >= 0) {
3200 struct vc_data *vc = vc_cons[j].d;
3201
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003202 printk(KERN_CONT "to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003203 vc->vc_can_do_color ? "colour" : "mono",
3204 desc, vc->vc_cols, vc->vc_rows);
3205
3206 if (k >= 0) {
3207 vc = vc_cons[k].d;
3208 update_screen(vc);
3209 }
3210 } else
Linus Torvalds4bcc5952016-10-08 20:32:40 -07003211 printk(KERN_CONT "to %s\n", desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003213 retval = 0;
3214err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003216 return retval;
3217};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Alan Cox50e244c2013-01-25 10:28:15 +10003219
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003220#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003221/* unlocked version of unbind_con_driver() */
3222int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3223{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003224 struct module *owner = csw->owner;
3225 const struct consw *defcsw = NULL;
3226 struct con_driver *con_driver = NULL, *con_back = NULL;
3227 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003229 if (!try_module_get(owner))
3230 return -ENODEV;
3231
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003232 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003233
3234 /* check if driver is registered and if it is unbindable */
3235 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3236 con_driver = &registered_con_driver[i];
3237
3238 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003239 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003240 retval = 0;
3241 break;
3242 }
3243 }
3244
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003245 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003246 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003247
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003248 retval = -ENODEV;
3249
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003250 /* check if backup driver exists */
3251 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3252 con_back = &registered_con_driver[i];
3253
Daniel Vetter249f7b32014-06-05 16:24:47 +02003254 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003255 defcsw = con_back->con;
3256 retval = 0;
3257 break;
3258 }
3259 }
3260
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003261 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003262 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003263
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003264 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003265 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003266
3267 first = max(first, con_driver->first);
3268 last = min(last, con_driver->last);
3269
3270 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 if (con_driver_map[i] == csw) {
3272 module_put(csw->owner);
3273 con_driver_map[i] = NULL;
3274 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003275 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003276
3277 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003278 const struct consw *defconsw = conswitchp;
3279
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003280 defcsw->con_startup();
3281 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003282 /*
3283 * vgacon may change the default driver to point
3284 * to dummycon, we restore it here...
3285 */
3286 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003287 }
3288
3289 if (!con_is_bound(csw))
3290 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3291
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003292 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003293 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003294err:
3295 module_put(owner);
3296 return retval;
3297
3298}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003299EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003300
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003301static int vt_bind(struct con_driver *con)
3302{
3303 const struct consw *defcsw = NULL, *csw = NULL;
3304 int i, more = 1, first = -1, last = -1, deflt = 0;
3305
Daniel Vetter77232f72015-04-13 11:16:21 +02003306 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003307 goto err;
3308
3309 csw = con->con;
3310
3311 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3312 struct con_driver *con = &registered_con_driver[i];
3313
3314 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3315 defcsw = con->con;
3316 break;
3317 }
3318 }
3319
3320 if (!defcsw)
3321 goto err;
3322
3323 while (more) {
3324 more = 0;
3325
3326 for (i = con->first; i <= con->last; i++) {
3327 if (con_driver_map[i] == defcsw) {
3328 if (first == -1)
3329 first = i;
3330 last = i;
3331 more = 1;
3332 } else if (first != -1)
3333 break;
3334 }
3335
3336 if (first == 0 && last == MAX_NR_CONSOLES -1)
3337 deflt = 1;
3338
Imre Deak4c215fe2014-12-16 00:16:00 +02003339 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003340 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003341
3342 first = -1;
3343 last = -1;
3344 deflt = 0;
3345 }
3346
3347err:
3348 return 0;
3349}
3350
3351static int vt_unbind(struct con_driver *con)
3352{
3353 const struct consw *csw = NULL;
3354 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003355 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003356
Daniel Vetter77232f72015-04-13 11:16:21 +02003357 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003358 goto err;
3359
3360 csw = con->con;
3361
3362 while (more) {
3363 more = 0;
3364
3365 for (i = con->first; i <= con->last; i++) {
3366 if (con_driver_map[i] == csw) {
3367 if (first == -1)
3368 first = i;
3369 last = i;
3370 more = 1;
3371 } else if (first != -1)
3372 break;
3373 }
3374
3375 if (first == 0 && last == MAX_NR_CONSOLES -1)
3376 deflt = 1;
3377
Wang YanQing618f2b92013-05-09 02:14:07 +08003378 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003379 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003380 if (ret != 0)
3381 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003382 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003383
3384 first = -1;
3385 last = -1;
3386 deflt = 0;
3387 }
3388
3389err:
3390 return 0;
3391}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003392#else
3393static inline int vt_bind(struct con_driver *con)
3394{
3395 return 0;
3396}
3397static inline int vt_unbind(struct con_driver *con)
3398{
3399 return 0;
3400}
3401#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003402
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003403static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003404 const char *buf, size_t count)
3405{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003406 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003407 int bind = simple_strtoul(buf, NULL, 0);
3408
Imre Deak4c215fe2014-12-16 00:16:00 +02003409 console_lock();
3410
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003411 if (bind)
3412 vt_bind(con);
3413 else
3414 vt_unbind(con);
3415
Imre Deak4c215fe2014-12-16 00:16:00 +02003416 console_unlock();
3417
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003418 return count;
3419}
3420
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003421static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3422 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003423{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003424 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003425 int bind = con_is_bound(con->con);
3426
3427 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3428}
3429
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003430static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3431 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003432{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003433 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003434
3435 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3436 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3437 con->desc);
3438
3439}
3440
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003441static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3442static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3443
3444static struct attribute *con_dev_attrs[] = {
3445 &dev_attr_bind.attr,
3446 &dev_attr_name.attr,
3447 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003448};
3449
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003450ATTRIBUTE_GROUPS(con_dev);
3451
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003452static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003453{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003454 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003455 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003456}
3457
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003458static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003459{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003460 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003461}
3462
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003463/**
3464 * con_is_bound - checks if driver is bound to the console
3465 * @csw: console driver
3466 *
3467 * RETURNS: zero if unbound, nonzero if bound
3468 *
3469 * Drivers can call this and if zero, they should release
3470 * all resources allocated on con_startup()
3471 */
3472int con_is_bound(const struct consw *csw)
3473{
3474 int i, bound = 0;
3475
3476 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3477 if (con_driver_map[i] == csw) {
3478 bound = 1;
3479 break;
3480 }
3481 }
3482
3483 return bound;
3484}
3485EXPORT_SYMBOL(con_is_bound);
3486
3487/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003488 * con_debug_enter - prepare the console for the kernel debugger
3489 * @sw: console driver
3490 *
3491 * Called when the console is taken over by the kernel debugger, this
3492 * function needs to save the current console state, then put the console
3493 * into a state suitable for the kernel debugger.
3494 *
3495 * RETURNS:
3496 * Zero on success, nonzero if a failure occurred when trying to prepare
3497 * the console for the debugger.
3498 */
3499int con_debug_enter(struct vc_data *vc)
3500{
3501 int ret = 0;
3502
3503 saved_fg_console = fg_console;
3504 saved_last_console = last_console;
3505 saved_want_console = want_console;
3506 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003507 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003508 vc->vc_mode = KD_TEXT;
3509 console_blanked = 0;
3510 if (vc->vc_sw->con_debug_enter)
3511 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003512#ifdef CONFIG_KGDB_KDB
3513 /* Set the initial LINES variable if it is not already set */
3514 if (vc->vc_rows < 999) {
3515 int linecount;
3516 char lns[4];
3517 const char *setargs[3] = {
3518 "set",
3519 "LINES",
3520 lns,
3521 };
3522 if (kdbgetintenv(setargs[0], &linecount)) {
3523 snprintf(lns, 4, "%i", vc->vc_rows);
3524 kdb_set(2, setargs);
3525 }
3526 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003527 if (vc->vc_cols < 999) {
3528 int colcount;
3529 char cols[4];
3530 const char *setargs[3] = {
3531 "set",
3532 "COLUMNS",
3533 cols,
3534 };
3535 if (kdbgetintenv(setargs[0], &colcount)) {
3536 snprintf(cols, 4, "%i", vc->vc_cols);
3537 kdb_set(2, setargs);
3538 }
3539 }
Jason Wessel81d44502010-08-05 09:22:30 -05003540#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003541 return ret;
3542}
3543EXPORT_SYMBOL_GPL(con_debug_enter);
3544
3545/**
3546 * con_debug_leave - restore console state
3547 * @sw: console driver
3548 *
3549 * Restore the console state to what it was before the kernel debugger
3550 * was invoked.
3551 *
3552 * RETURNS:
3553 * Zero on success, nonzero if a failure occurred when trying to restore
3554 * the console.
3555 */
3556int con_debug_leave(void)
3557{
3558 struct vc_data *vc;
3559 int ret = 0;
3560
3561 fg_console = saved_fg_console;
3562 last_console = saved_last_console;
3563 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003564 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003565 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3566
3567 vc = vc_cons[fg_console].d;
3568 if (vc->vc_sw->con_debug_leave)
3569 ret = vc->vc_sw->con_debug_leave(vc);
3570 return ret;
3571}
3572EXPORT_SYMBOL_GPL(con_debug_leave);
3573
Alan Cox50e244c2013-01-25 10:28:15 +10003574static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003575{
3576 struct module *owner = csw->owner;
3577 struct con_driver *con_driver;
3578 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02003579 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003580
Alan Cox50e244c2013-01-25 10:28:15 +10003581 WARN_CONSOLE_UNLOCKED();
3582
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003583 if (!try_module_get(owner))
3584 return -ENODEV;
3585
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003586 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3587 con_driver = &registered_con_driver[i];
3588
3589 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02003590 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10003591 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02003592 goto err;
3593 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003594 }
3595
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003596 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02003597 if (!desc) {
3598 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003599 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02003600 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003601
3602 retval = -EINVAL;
3603
3604 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3605 con_driver = &registered_con_driver[i];
3606
Imre Deakd364b5c2015-04-01 21:06:16 +03003607 if (con_driver->con == NULL &&
3608 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003609 con_driver->con = csw;
3610 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003611 con_driver->node = i;
3612 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003613 CON_DRIVER_FLAG_INIT;
3614 con_driver->first = first;
3615 con_driver->last = last;
3616 retval = 0;
3617 break;
3618 }
3619 }
3620
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003621 if (retval)
3622 goto err;
3623
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003624 con_driver->dev =
3625 device_create_with_groups(vtconsole_class, NULL,
3626 MKDEV(0, con_driver->node),
3627 con_driver, con_dev_groups,
3628 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003629 if (IS_ERR(con_driver->dev)) {
3630 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003631 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003632 PTR_ERR(con_driver->dev));
3633 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003634 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003635 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003636 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003637
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003638err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003639 module_put(owner);
3640 return retval;
3641}
Alan Cox50e244c2013-01-25 10:28:15 +10003642
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003643
3644/**
Wang YanQing50539dd2013-05-09 02:14:44 +08003645 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003646 * @csw: console driver
3647 *
3648 * DESCRIPTION: All drivers that registers to the console layer must
3649 * call this function upon exit, or if the console driver is in a state
3650 * where it won't be able to handle console services, such as the
3651 * framebuffer console without loaded framebuffer drivers.
3652 *
3653 * The driver must unbind first prior to unregistration.
3654 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003655int do_unregister_con_driver(const struct consw *csw)
3656{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003657 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003658
3659 /* cannot unregister a bound driver */
3660 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003661 return -EBUSY;
3662
3663 if (csw == conswitchp)
3664 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003665
3666 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3667 struct con_driver *con_driver = &registered_con_driver[i];
3668
Imre Deak2cf30f72014-12-16 00:15:59 +02003669 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03003670 /*
3671 * Defer the removal of the sysfs entries since that
3672 * will acquire the kernfs s_active lock and we can't
3673 * acquire this lock while holding the console lock:
3674 * the unbind sysfs entry imposes already the opposite
3675 * order. Reset con already here to prevent any later
3676 * lookup to succeed and mark this slot as zombie, so
3677 * it won't get reused until we complete the removal
3678 * in the deferred work.
3679 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003680 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03003681 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
3682 schedule_work(&con_driver_unregister_work);
3683
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003684 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003685 }
3686 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003687
3688 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003689}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003690EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003691
Imre Deakd364b5c2015-04-01 21:06:16 +03003692static void con_driver_unregister_callback(struct work_struct *ignored)
3693{
3694 int i;
3695
3696 console_lock();
3697
3698 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3699 struct con_driver *con_driver = &registered_con_driver[i];
3700
3701 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
3702 continue;
3703
3704 console_unlock();
3705
3706 vtconsole_deinit_device(con_driver);
3707 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
3708
3709 console_lock();
3710
3711 if (WARN_ON_ONCE(con_driver->con))
3712 con_driver->con = NULL;
3713 con_driver->desc = NULL;
3714 con_driver->dev = NULL;
3715 con_driver->node = 0;
3716 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
3717 con_driver->flag = 0;
3718 con_driver->first = 0;
3719 con_driver->last = 0;
3720 }
3721
3722 console_unlock();
3723}
3724
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003725/*
3726 * If we support more console drivers, this function is used
3727 * when a driver wants to take over some existing consoles
3728 * and become default driver for newly opened ones.
3729 *
Wang YanQing155957f2013-05-21 13:15:12 +08003730 * do_take_over_console is basically a register followed by unbind
Alan Cox50e244c2013-01-25 10:28:15 +10003731 */
3732int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3733{
3734 int err;
3735
3736 err = do_register_con_driver(csw, first, last);
3737 /*
3738 * If we get an busy error we still want to bind the console driver
3739 * and return success, as we may have unbound the console driver
3740 * but not unregistered it.
3741 */
3742 if (err == -EBUSY)
3743 err = 0;
3744 if (!err)
3745 do_bind_con_driver(csw, first, last, deflt);
3746
3747 return err;
3748}
3749EXPORT_SYMBOL_GPL(do_take_over_console);
3750
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003751
3752/*
3753 * give_up_console is a wrapper to unregister_con_driver. It will only
3754 * work if driver is fully unbound.
3755 */
3756void give_up_console(const struct consw *csw)
3757{
Wang YanQing70125e72013-05-09 02:14:39 +08003758 console_lock();
3759 do_unregister_con_driver(csw);
3760 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003761}
3762
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003763static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003764{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003765 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003766
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003767 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3768 if (IS_ERR(vtconsole_class)) {
3769 printk(KERN_WARNING "Unable to create vt console class; "
3770 "errno = %ld\n", PTR_ERR(vtconsole_class));
3771 vtconsole_class = NULL;
3772 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003773
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003774 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003775 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3776 struct con_driver *con = &registered_con_driver[i];
3777
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003778 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003779 con->dev =
3780 device_create_with_groups(vtconsole_class, NULL,
3781 MKDEV(0, con->node),
3782 con, con_dev_groups,
3783 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003784
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003785 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003786 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003787 "device for %s; errno = %ld\n",
3788 con->desc, PTR_ERR(con->dev));
3789 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003790 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003791 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003792 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003793 }
3794 }
3795
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003796 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003797}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003798postcore_initcall(vtconsole_class_init);
3799
3800#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003801
3802/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 * Screen blanking
3804 */
3805
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003806static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003808 unsigned int mode;
3809
3810 if (get_user(mode, p + 1))
3811 return -EFAULT;
3812
3813 vesa_blank_mode = (mode < 4) ? mode : 0;
3814 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815}
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817void do_blank_screen(int entering_gfx)
3818{
3819 struct vc_data *vc = vc_cons[fg_console].d;
3820 int i;
3821
3822 WARN_CONSOLE_UNLOCKED();
3823
3824 if (console_blanked) {
3825 if (blank_state == blank_vesa_wait) {
3826 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003827 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 }
3829 return;
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831
3832 /* entering graphics mode? */
3833 if (entering_gfx) {
3834 hide_cursor(vc);
3835 save_screen(vc);
3836 vc->vc_sw->con_blank(vc, -1, 1);
3837 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003838 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 set_origin(vc);
3840 return;
3841 }
3842
izumib6e8f002007-07-17 04:05:49 -07003843 if (blank_state != blank_normal_wait)
3844 return;
3845 blank_state = blank_off;
3846
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847 /* don't blank graphics */
3848 if (vc->vc_mode != KD_TEXT) {
3849 console_blanked = fg_console + 1;
3850 return;
3851 }
3852
3853 hide_cursor(vc);
3854 del_timer_sync(&console_timer);
3855 blank_timer_expired = 0;
3856
3857 save_screen(vc);
3858 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003859 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 console_blanked = fg_console + 1;
3861 if (i)
3862 set_origin(vc);
3863
3864 if (console_blank_hook && console_blank_hook(1))
3865 return;
3866
Ville Syrjalad060a322006-01-09 20:53:49 -08003867 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003868 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 mod_timer(&console_timer, jiffies + vesa_off_interval);
3870 }
Alan Cox8b92e872009-09-19 13:13:24 -07003871 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872}
3873EXPORT_SYMBOL(do_blank_screen);
3874
3875/*
3876 * Called by timer as well as from vt_console_driver
3877 */
3878void do_unblank_screen(int leaving_gfx)
3879{
3880 struct vc_data *vc;
3881
3882 /* This should now always be called from a "sane" (read: can schedule)
3883 * context for the sake of the low level drivers, except in the special
3884 * case of oops_in_progress
3885 */
3886 if (!oops_in_progress)
3887 might_sleep();
3888
3889 WARN_CONSOLE_UNLOCKED();
3890
3891 ignore_poke = 0;
3892 if (!console_blanked)
3893 return;
3894 if (!vc_cons_allocated(fg_console)) {
3895 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08003896 pr_warn("unblank_screen: tty %d not allocated ??\n",
3897 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 return;
3899 }
3900 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003901 /* Try to unblank in oops case too */
3902 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return; /* but leave console_blanked != 0 */
3904
3905 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003906 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 blank_state = blank_normal_wait;
3908 }
3909
3910 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003911 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 /* Low-level driver cannot restore -> do it ourselves */
3913 update_screen(vc);
3914 if (console_blank_hook)
3915 console_blank_hook(0);
3916 set_palette(vc);
3917 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003918 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919}
3920EXPORT_SYMBOL(do_unblank_screen);
3921
3922/*
3923 * This is called by the outside world to cause a forced unblank, mostly for
3924 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3925 * call it with 1 as an argument and so force a mode restore... that may kill
3926 * X or at least garbage the screen but would also make the Oops visible...
3927 */
3928void unblank_screen(void)
3929{
3930 do_unblank_screen(0);
3931}
3932
3933/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003934 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003936 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 */
3938static void blank_screen_t(unsigned long dummy)
3939{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003940 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003941 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003942 return;
3943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 blank_timer_expired = 1;
3945 schedule_work(&console_work);
3946}
3947
3948void poke_blanked_console(void)
3949{
3950 WARN_CONSOLE_UNLOCKED();
3951
3952 /* Add this so we quickly catch whoever might call us in a non
3953 * safe context. Nowadays, unblank_screen() isn't to be called in
3954 * atomic contexts and is allowed to schedule (with the special case
3955 * of oops_in_progress, but that isn't of any concern for this
3956 * function. --BenH.
3957 */
3958 might_sleep();
3959
3960 /* This isn't perfectly race free, but a race here would be mostly harmless,
3961 * at worse, we'll do a spurrious blank and it's unlikely
3962 */
3963 del_timer(&console_timer);
3964 blank_timer_expired = 0;
3965
3966 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3967 return;
3968 if (console_blanked)
3969 unblank_screen();
3970 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003971 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 blank_state = blank_normal_wait;
3973 }
3974}
3975
3976/*
3977 * Palettes
3978 */
3979
3980static void set_palette(struct vc_data *vc)
3981{
3982 WARN_CONSOLE_UNLOCKED();
3983
Jiri Slaby709280d2016-06-23 13:34:27 +02003984 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 vc->vc_sw->con_set_palette(vc, color_table);
3986}
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988/*
3989 * Load palette into the DAC registers. arg points to a colour
3990 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3991 */
3992
3993int con_set_cmap(unsigned char __user *arg)
3994{
Michael Gehring871bdea2012-03-21 01:26:45 +01003995 int i, j, k;
3996 unsigned char colormap[3*16];
3997
3998 if (copy_from_user(colormap, arg, sizeof(colormap)))
3999 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
Torben Hohnac751ef2011-01-25 15:07:35 -08004001 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004002 for (i = k = 0; i < 16; i++) {
4003 default_red[i] = colormap[k++];
4004 default_grn[i] = colormap[k++];
4005 default_blu[i] = colormap[k++];
4006 }
4007 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4008 if (!vc_cons_allocated(i))
4009 continue;
4010 for (j = k = 0; j < 16; j++) {
4011 vc_cons[i].d->vc_palette[k++] = default_red[j];
4012 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4013 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4014 }
4015 set_palette(vc_cons[i].d);
4016 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004017 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Michael Gehring871bdea2012-03-21 01:26:45 +01004019 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020}
4021
4022int con_get_cmap(unsigned char __user *arg)
4023{
Michael Gehring871bdea2012-03-21 01:26:45 +01004024 int i, k;
4025 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Torben Hohnac751ef2011-01-25 15:07:35 -08004027 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004028 for (i = k = 0; i < 16; i++) {
4029 colormap[k++] = default_red[i];
4030 colormap[k++] = default_grn[i];
4031 colormap[k++] = default_blu[i];
4032 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004033 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Michael Gehring871bdea2012-03-21 01:26:45 +01004035 if (copy_to_user(arg, colormap, sizeof(colormap)))
4036 return -EFAULT;
4037
4038 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
4041void reset_palette(struct vc_data *vc)
4042{
4043 int j, k;
4044 for (j=k=0; j<16; j++) {
4045 vc->vc_palette[k++] = default_red[j];
4046 vc->vc_palette[k++] = default_grn[j];
4047 vc->vc_palette[k++] = default_blu[j];
4048 }
4049 set_palette(vc);
4050}
4051
4052/*
4053 * Font switching
4054 *
4055 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4056 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4057 * depending on width) reserved for each character which is kinda wasty, but
4058 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004059 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 * format (maybe we should add a `fontoffset' field to the `display'
4061 * structure so we won't have to convert the fontdata all the time.
4062 * /Jes
4063 */
4064
4065#define max_font_size 65536
4066
4067static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4068{
4069 struct console_font font;
4070 int rc = -EINVAL;
4071 int c;
4072
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 if (op->data) {
4074 font.data = kmalloc(max_font_size, GFP_KERNEL);
4075 if (!font.data)
4076 return -ENOMEM;
4077 } else
4078 font.data = NULL;
4079
Torben Hohnac751ef2011-01-25 15:07:35 -08004080 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004081 if (vc->vc_mode != KD_TEXT)
4082 rc = -EINVAL;
4083 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 rc = vc->vc_sw->con_font_get(vc, &font);
4085 else
4086 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004087 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089 if (rc)
4090 goto out;
4091
4092 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 if (op->data && font.charcount > op->charcount)
4095 rc = -ENOSPC;
4096 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4097 if (font.width > op->width || font.height > op->height)
4098 rc = -ENOSPC;
4099 } else {
4100 if (font.width != 8)
4101 rc = -EIO;
4102 else if ((op->height && font.height > op->height) ||
4103 font.height > 32)
4104 rc = -ENOSPC;
4105 }
4106 if (rc)
4107 goto out;
4108
4109 op->height = font.height;
4110 op->width = font.width;
4111 op->charcount = font.charcount;
4112
4113 if (op->data && copy_to_user(op->data, font.data, c))
4114 rc = -EFAULT;
4115
4116out:
4117 kfree(font.data);
4118 return rc;
4119}
4120
4121static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4122{
4123 struct console_font font;
4124 int rc = -EINVAL;
4125 int size;
4126
4127 if (vc->vc_mode != KD_TEXT)
4128 return -EINVAL;
4129 if (!op->data)
4130 return -EINVAL;
4131 if (op->charcount > 512)
4132 return -EINVAL;
4133 if (!op->height) { /* Need to guess font height [compat] */
4134 int h, i;
4135 u8 __user *charmap = op->data;
4136 u8 tmp;
4137
4138 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4139 so that we can get rid of this soon */
4140 if (!(op->flags & KD_FONT_FLAG_OLD))
4141 return -EINVAL;
4142 for (h = 32; h > 0; h--)
4143 for (i = 0; i < op->charcount; i++) {
4144 if (get_user(tmp, &charmap[32*i+h-1]))
4145 return -EFAULT;
4146 if (tmp)
4147 goto nonzero;
4148 }
4149 return -EINVAL;
4150 nonzero:
4151 op->height = h;
4152 }
4153 if (op->width <= 0 || op->width > 32 || op->height > 32)
4154 return -EINVAL;
4155 size = (op->width+7)/8 * 32 * op->charcount;
4156 if (size > max_font_size)
4157 return -ENOSPC;
4158 font.charcount = op->charcount;
4159 font.height = op->height;
4160 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004161 font.data = memdup_user(op->data, size);
4162 if (IS_ERR(font.data))
4163 return PTR_ERR(font.data);
Torben Hohnac751ef2011-01-25 15:07:35 -08004164 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004165 if (vc->vc_mode != KD_TEXT)
4166 rc = -EINVAL;
4167 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4169 else
4170 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004171 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 kfree(font.data);
4173 return rc;
4174}
4175
4176static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4177{
4178 struct console_font font = {.width = op->width, .height = op->height};
4179 char name[MAX_FONT_NAME];
4180 char *s = name;
4181 int rc;
4182
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 if (!op->data)
4185 s = NULL;
4186 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4187 return -EFAULT;
4188 else
4189 name[MAX_FONT_NAME - 1] = 0;
4190
Torben Hohnac751ef2011-01-25 15:07:35 -08004191 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004192 if (vc->vc_mode != KD_TEXT) {
4193 console_unlock();
4194 return -EINVAL;
4195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 if (vc->vc_sw->con_font_default)
4197 rc = vc->vc_sw->con_font_default(vc, &font, s);
4198 else
4199 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004200 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 if (!rc) {
4202 op->width = font.width;
4203 op->height = font.height;
4204 }
4205 return rc;
4206}
4207
4208static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4209{
4210 int con = op->height;
4211 int rc;
4212
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Torben Hohnac751ef2011-01-25 15:07:35 -08004214 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004215 if (vc->vc_mode != KD_TEXT)
4216 rc = -EINVAL;
4217 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 rc = -ENOSYS;
4219 else if (con < 0 || !vc_cons_allocated(con))
4220 rc = -ENOTTY;
4221 else if (con == vc->vc_num) /* nothing to do */
4222 rc = 0;
4223 else
4224 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004225 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 return rc;
4227}
4228
4229int con_font_op(struct vc_data *vc, struct console_font_op *op)
4230{
4231 switch (op->op) {
4232 case KD_FONT_OP_SET:
4233 return con_font_set(vc, op);
4234 case KD_FONT_OP_GET:
4235 return con_font_get(vc, op);
4236 case KD_FONT_OP_SET_DEFAULT:
4237 return con_font_default(vc, op);
4238 case KD_FONT_OP_COPY:
4239 return con_font_copy(vc, op);
4240 }
4241 return -ENOSYS;
4242}
4243
4244/*
4245 * Interface exported to selection and vcs.
4246 */
4247
4248/* used by selection */
4249u16 screen_glyph(struct vc_data *vc, int offset)
4250{
4251 u16 w = scr_readw(screenpos(vc, offset, 1));
4252 u16 c = w & 0xff;
4253
4254 if (w & vc->vc_hi_font_mask)
4255 c |= 0x100;
4256 return c;
4257}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004258EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
4260/* used by vcs - note the word offset */
4261unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4262{
4263 return screenpos(vc, 2 * w_offset, viewed);
4264}
Samuel Thibault88867e32016-01-25 01:32:08 +01004265EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
4267void getconsxy(struct vc_data *vc, unsigned char *p)
4268{
4269 p[0] = vc->vc_x;
4270 p[1] = vc->vc_y;
4271}
4272
4273void putconsxy(struct vc_data *vc, unsigned char *p)
4274{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004275 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 gotoxy(vc, p[0], p[1]);
4277 set_cursor(vc);
4278}
4279
4280u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4281{
4282 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4283 return softcursor_original;
4284 return scr_readw(org);
4285}
4286
4287void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4288{
4289 scr_writew(val, org);
4290 if ((unsigned long)org == vc->vc_pos) {
4291 softcursor_original = -1;
4292 add_softcursor(vc);
4293 }
4294}
4295
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004296void vcs_scr_updated(struct vc_data *vc)
4297{
4298 notify_update(vc);
4299}
4300
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301/*
4302 * Visible symbols for modules
4303 */
4304
4305EXPORT_SYMBOL(color_table);
4306EXPORT_SYMBOL(default_red);
4307EXPORT_SYMBOL(default_grn);
4308EXPORT_SYMBOL(default_blu);
4309EXPORT_SYMBOL(update_region);
4310EXPORT_SYMBOL(redraw_screen);
4311EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312EXPORT_SYMBOL(fg_console);
4313EXPORT_SYMBOL(console_blank_hook);
4314EXPORT_SYMBOL(console_blanked);
4315EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004316EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318EXPORT_SYMBOL(give_up_console);
4319#endif