blob: 062ce6be7957dd185ded5b9317c5cb961ffefd05 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
4 */
5
6/*
7 * Hopefully this will be a rather complete VT102 implementation.
8 *
9 * Beeping thanks to John T Kohl.
10 *
11 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
12 * Chars, and VT100 enhancements by Peter MacDonald.
13 *
14 * Copy and paste function by Andrew Haylett,
15 * some enhancements by Alessandro Rubini.
16 *
17 * Code to check for different video-cards mostly by Galen Hunt,
18 * <g-hunt@ee.utah.edu>
19 *
20 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
21 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
22 *
23 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
24 * Resizing of consoles, aeb, 940926
25 *
26 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
27 * <poe@daimi.aau.dk>
28 *
29 * User-defined bell sound, new setterm control sequences and printk
30 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
31 *
32 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
33 *
34 * Merge with the abstract console driver by Geert Uytterhoeven
35 * <geert@linux-m68k.org>, Jan 1997.
36 *
37 * Original m68k console driver modifications by
38 *
39 * - Arno Griffioen <arno@usn.nl>
40 * - David Carter <carter@cs.bris.ac.uk>
41 *
42 * The abstract console driver provides a generic interface for a text
43 * console. It supports VGA text mode, frame buffer based graphical consoles
44 * and special graphics processors that are only accessible through some
45 * registers (e.g. a TMS340x0 GSP).
46 *
47 * The interface to the hardware is specified using a special structure
48 * (struct consw) which contains function pointers to console operations
49 * (see <linux/console.h> for more information).
50 *
51 * Support for changeable cursor shape
52 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
53 *
54 * Ported to i386 and con_scrolldelta fixed
55 * by Emmanuel Marty <core@ggi-project.org>, April 1998
56 *
57 * Resurrected character buffers in videoram plus lots of other trickery
58 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
59 *
60 * Removed old-style timers, introduced console_timer, made timer
Francois Camie1f8e872008-10-15 22:01:59 -070061 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * Removed console_lock, enabled interrupts across all console operations
64 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070065 *
66 * Fixed UTF-8 mode so alternate charset modes always work according
67 * to control sequences interpreted in do_con_trol function
68 * preserving backward VT100 semigraphics compatibility,
69 * malformed UTF sequences represented as sequences of replacement glyphs,
70 * original codes or '?' as a last resort if replacement glyph is undefined
71 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73
74#include <linux/module.h>
75#include <linux/types.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010076#include <linux/sched/signal.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#include <linux/tty.h>
78#include <linux/tty_flip.h>
79#include <linux/kernel.h>
80#include <linux/string.h>
81#include <linux/errno.h>
82#include <linux/kd.h>
83#include <linux/slab.h>
84#include <linux/major.h>
85#include <linux/mm.h>
86#include <linux/console.h>
87#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070088#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/vt_kern.h>
90#include <linux/selection.h>
91#include <linux/tiocl.h>
92#include <linux/kbd_kern.h>
93#include <linux/consolemap.h>
94#include <linux/timer.h>
95#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/pm.h>
98#include <linux/font.h>
99#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700100#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100101#include <linux/device.h>
102#include <linux/io.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100103#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500104#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300105#include <linux/ctype.h>
Thomas Meyerf0a8d842017-09-16 10:03:05 +0200106#include <linux/bsearch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700108#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700110#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700111#define CON_DRIVER_FLAG_INIT 2
112#define CON_DRIVER_FLAG_ATTR 4
Imre Deakd364b5c2015-04-01 21:06:16 +0300113#define CON_DRIVER_FLAG_ZOMBIE 8
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700114
115struct con_driver {
116 const struct consw *con;
117 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700118 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700119 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700120 int first;
121 int last;
122 int flag;
123};
124
125static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126const struct consw *conswitchp;
127
128/* A bitmap for codes <32. A bit of 1 indicates that the code
129 * corresponding to that bit number invokes some special action
130 * (such as cursor movement) and should not be displayed as a
131 * glyph unless the disp_ctrl mode is explicitly enabled.
132 */
133#define CTRL_ACTION 0x0d00ff81
134#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
135
136/*
137 * Here is the default bell parameters: 750HZ, 1/8th of a second
138 */
139#define DEFAULT_BELL_PITCH 750
140#define DEFAULT_BELL_DURATION (HZ/8)
Scot Doylebd633642015-03-26 13:54:39 +0000141#define DEFAULT_CURSOR_BLINK_MS 200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143struct vc vc_cons [MAX_NR_CONSOLES];
144
145#ifndef VT_SINGLE_DRIVER
146static const struct consw *con_driver_map[MAX_NR_CONSOLES];
147#endif
148
149static int con_open(struct tty_struct *, struct file *);
150static void vc_init(struct vc_data *vc, unsigned int rows,
151 unsigned int cols, int do_clear);
152static void gotoxy(struct vc_data *vc, int new_x, int new_y);
153static void save_cur(struct vc_data *vc);
154static void reset_terminal(struct vc_data *vc, int do_clear);
155static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800156static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void set_cursor(struct vc_data *vc);
158static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000159static void console_callback(struct work_struct *ignored);
Imre Deakd364b5c2015-04-01 21:06:16 +0300160static void con_driver_unregister_callback(struct work_struct *ignored);
Kees Cook24ed9602017-08-28 11:28:21 -0700161static void blank_screen_t(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static void set_palette(struct vc_data *vc);
163
Peter Hurley52c40fc2014-11-11 11:20:56 -0500164#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700167int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700168module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500169int global_cursor_default = -1;
170module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800172static int cur_default = CUR_DEFAULT;
173module_param(cur_default, int, S_IRUGO | S_IWUSR);
174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/*
176 * ignore_poke: don't unblank the screen when things are typed. This is
177 * mainly for the privacy of braille terminal users.
178 */
179static int ignore_poke;
180
181int do_poke_blanked_console;
182int console_blanked;
183
184static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185static int vesa_off_interval;
Tim Gardnera4199f52017-03-22 09:07:20 -0600186static int blankinterval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700187core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
David Howells65f27f32006-11-22 14:55:48 +0000189static DECLARE_WORK(console_work, console_callback);
Imre Deakd364b5c2015-04-01 21:06:16 +0300190static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/*
193 * fg_console is the current virtual console,
194 * last_console is the last used one,
195 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500196 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 */
198int fg_console;
199int last_console;
200int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500201static int saved_fg_console;
202static int saved_last_console;
203static int saved_want_console;
204static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500205static int saved_console_blanked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
207/*
208 * For each existing display, we have a pointer to console currently visible
209 * on that display, allowing consoles other than fg_console to be refreshed
210 * appropriately. Unless the low-level driver supplies its own display_fg
211 * variable, we use this one for the "master display".
212 */
213static struct vc_data *master_display_fg;
214
215/*
216 * Unfortunately, we need to delay tty echo when we're currently writing to the
217 * console since the code is (and always was) not re-entrant, so we schedule
218 * all flip requests to process context with schedule-task() and run it from
219 * console_callback().
220 */
221
222/*
223 * For the same reason, we defer scrollback to the console callback.
224 */
225static int scrollback_delta;
226
227/*
228 * Hook so that the power management routines can (un)blank
229 * the console on our behalf.
230 */
231int (*console_blank_hook)(int);
232
Kees Cook1d27e3e2017-10-04 16:27:04 -0700233static DEFINE_TIMER(console_timer, blank_screen_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static int blank_state;
235static int blank_timer_expired;
236enum {
237 blank_off = 0,
238 blank_normal_wait,
239 blank_vesa_wait,
240};
241
242/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100243 * /sys/class/tty/tty0/
244 *
245 * the attribute 'active' contains the name of the current vc
246 * console and it supports poll() to detect vc switches
247 */
248static struct device *tty0dev;
249
250/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700251 * Notifier list for console events.
252 */
253static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
254
255int register_vt_notifier(struct notifier_block *nb)
256{
257 return atomic_notifier_chain_register(&vt_notifier_list, nb);
258}
259EXPORT_SYMBOL_GPL(register_vt_notifier);
260
261int unregister_vt_notifier(struct notifier_block *nb)
262{
263 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
264}
265EXPORT_SYMBOL_GPL(unregister_vt_notifier);
266
267static void notify_write(struct vc_data *vc, unsigned int unicode)
268{
Mathias Krause502fcb72011-07-30 14:01:59 +0200269 struct vt_notifier_param param = { .vc = vc, .c = unicode };
Samuel Thibaultb293d752007-10-18 23:39:17 -0700270 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
271}
272
273static void notify_update(struct vc_data *vc)
274{
275 struct vt_notifier_param param = { .vc = vc };
276 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
277}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700278/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * Low-Level Functions
280 */
281
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200282static inline bool con_is_fg(const struct vc_data *vc)
283{
284 return vc->vc_num == fg_console;
285}
286
287static inline bool con_should_update(const struct vc_data *vc)
288{
289 return con_is_visible(vc) && !console_blanked;
290}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
293{
294 unsigned short *p;
295
296 if (!viewed)
297 p = (unsigned short *)(vc->vc_origin + offset);
298 else if (!vc->vc_sw->con_screen_pos)
299 p = (unsigned short *)(vc->vc_visible_origin + offset);
300 else
301 p = vc->vc_sw->con_screen_pos(vc, offset);
302 return p;
303}
304
Alan Coxe33ac1c2010-06-01 22:52:54 +0200305/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static inline void scrolldelta(int lines)
307{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200308 /* FIXME */
309 /* scrolldelta needs some kind of consistency lock, but the BKL was
310 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 scrollback_delta += lines;
312 schedule_console_callback();
313}
314
315void schedule_console_callback(void)
316{
317 schedule_work(&console_work);
318}
319
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400320/*
321 * Code to manage unicode-based screen buffers
322 */
323
324#ifdef NO_VC_UNI_SCREEN
325/* this disables and optimizes related code away at compile time */
326#define get_vc_uniscr(vc) NULL
327#else
328#define get_vc_uniscr(vc) vc->vc_uni_screen
329#endif
330
331typedef uint32_t char32_t;
332
333/*
334 * Our screen buffer is preceded by an array of line pointers so that
335 * scrolling only implies some pointer shuffling.
336 */
337struct uni_screen {
338 char32_t *lines[0];
339};
340
341static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
342{
343 struct uni_screen *uniscr;
344 void *p;
345 unsigned int memsize, i;
346
347 /* allocate everything in one go */
348 memsize = cols * rows * sizeof(char32_t);
349 memsize += rows * sizeof(char32_t *);
350 p = kmalloc(memsize, GFP_KERNEL);
351 if (!p)
352 return NULL;
353
354 /* initial line pointers */
355 uniscr = p;
356 p = uniscr->lines + rows;
357 for (i = 0; i < rows; i++) {
358 uniscr->lines[i] = p;
359 p += cols * sizeof(char32_t);
360 }
361 return uniscr;
362}
363
364static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
365{
366 kfree(vc->vc_uni_screen);
367 vc->vc_uni_screen = new_uniscr;
368}
369
370static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
371{
372 struct uni_screen *uniscr = get_vc_uniscr(vc);
373
374 if (uniscr)
375 uniscr->lines[vc->vc_y][vc->vc_x] = uc;
376}
377
378static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
379{
380 struct uni_screen *uniscr = get_vc_uniscr(vc);
381
382 if (uniscr) {
383 char32_t *ln = uniscr->lines[vc->vc_y];
384 unsigned int x = vc->vc_x, cols = vc->vc_cols;
385
386 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
387 memset32(&ln[x], ' ', nr);
388 }
389}
390
391static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
392{
393 struct uni_screen *uniscr = get_vc_uniscr(vc);
394
395 if (uniscr) {
396 char32_t *ln = uniscr->lines[vc->vc_y];
397 unsigned int x = vc->vc_x, cols = vc->vc_cols;
398
399 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
400 memset32(&ln[cols - nr], ' ', nr);
401 }
402}
403
404static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
405 unsigned int nr)
406{
407 struct uni_screen *uniscr = get_vc_uniscr(vc);
408
409 if (uniscr) {
410 char32_t *ln = uniscr->lines[vc->vc_y];
411
412 memset32(&ln[x], ' ', nr);
413 }
414}
415
416static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
417 unsigned int nr)
418{
419 struct uni_screen *uniscr = get_vc_uniscr(vc);
420
421 if (uniscr) {
422 unsigned int cols = vc->vc_cols;
423
424 while (nr--)
425 memset32(uniscr->lines[y++], ' ', cols);
426 }
427}
428
429static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
430 enum con_scroll dir, unsigned int nr)
431{
432 struct uni_screen *uniscr = get_vc_uniscr(vc);
433
434 if (uniscr) {
435 unsigned int s, d, rescue, clear;
436 char32_t *save[nr];
437
438 s = clear = t;
439 d = t + nr;
440 rescue = b - nr;
441 if (dir == SM_UP) {
442 swap(s, d);
443 swap(clear, rescue);
444 }
445 memcpy(save, uniscr->lines + rescue, nr * sizeof(*save));
446 memmove(uniscr->lines + d, uniscr->lines + s,
447 (b - t - nr) * sizeof(*uniscr->lines));
448 memcpy(uniscr->lines + clear, save, nr * sizeof(*save));
449 vc_uniscr_clear_lines(vc, clear, nr);
450 }
451}
452
453static void vc_uniscr_copy_area(struct uni_screen *dst,
454 unsigned int dst_cols,
455 unsigned int dst_rows,
456 struct uni_screen *src,
457 unsigned int src_cols,
458 unsigned int src_top_row,
459 unsigned int src_bot_row)
460{
461 unsigned int dst_row = 0;
462
463 if (!dst)
464 return;
465
466 while (src_top_row < src_bot_row) {
467 char32_t *src_line = src->lines[src_top_row];
468 char32_t *dst_line = dst->lines[dst_row];
469
470 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
471 if (dst_cols - src_cols)
472 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
473 src_top_row++;
474 dst_row++;
475 }
476 while (dst_row < dst_rows) {
477 char32_t *dst_line = dst->lines[dst_row];
478
479 memset32(dst_line, ' ', dst_cols);
480 dst_row++;
481 }
482}
483
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400484/*
485 * Called from vcs_read() to make sure unicode screen retrieval is possible.
486 * This will initialize the unicode screen buffer if not already done.
487 * This returns 0 if OK, or a negative error code otherwise.
488 * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
489 */
490int vc_uniscr_check(struct vc_data *vc)
491{
492 struct uni_screen *uniscr;
493 unsigned short *p;
494 int x, y, mask;
495
496 if (__is_defined(NO_VC_UNI_SCREEN))
497 return -EOPNOTSUPP;
498
499 WARN_CONSOLE_UNLOCKED();
500
501 if (!vc->vc_utf)
502 return -ENODATA;
503
504 if (vc->vc_uni_screen)
505 return 0;
506
507 uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
508 if (!uniscr)
509 return -ENOMEM;
510
511 /*
512 * Let's populate it initially with (imperfect) reverse translation.
513 * This is the next best thing we can do short of having it enabled
514 * from the start even when no users rely on this functionality. True
515 * unicode content will be available after a complete screen refresh.
516 */
517 p = (unsigned short *)vc->vc_origin;
518 mask = vc->vc_hi_font_mask | 0xff;
519 for (y = 0; y < vc->vc_rows; y++) {
520 char32_t *line = uniscr->lines[y];
521 for (x = 0; x < vc->vc_cols; x++) {
522 u16 glyph = scr_readw(p++) & mask;
523 line[x] = inverse_translate(vc, glyph, true);
524 }
525 }
526
527 vc->vc_uni_screen = uniscr;
528 return 0;
529}
530
531/*
532 * Called from vcs_read() to get the unicode data from the screen.
533 * This must be preceded by a successful call to vc_uniscr_check() once
534 * the console lock has been taken.
535 */
536void vc_uniscr_copy_line(struct vc_data *vc, void *dest,
537 unsigned int row, unsigned int col, unsigned int nr)
538{
539 struct uni_screen *uniscr = get_vc_uniscr(vc);
540
541 BUG_ON(!uniscr);
542 memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
543}
544
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400545
Jiri Slaby89765b92016-10-03 11:18:34 +0200546static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
547 enum con_scroll dir, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
Jiri Slaby89765b92016-10-03 11:18:34 +0200549 u16 *clear, *d, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Jiri Slaby89765b92016-10-03 11:18:34 +0200551 if (t + nr >= b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 nr = b - t - 1;
553 if (b > vc->vc_rows || t >= b || nr < 1)
554 return;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400555 vc_uniscr_scroll(vc, t, b, dir, nr);
Jiri Slaby89765b92016-10-03 11:18:34 +0200556 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return;
Jiri Slaby89765b92016-10-03 11:18:34 +0200558
559 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
560 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
561
562 if (dir == SM_UP) {
563 clear = s + (b - t - nr) * vc->vc_cols;
564 swap(s, d);
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Jiri Slaby89765b92016-10-03 11:18:34 +0200567 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
570static void do_update_region(struct vc_data *vc, unsigned long start, int count)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 unsigned int xx, yy, offset;
573 u16 *p;
574
575 p = (u16 *) start;
576 if (!vc->vc_sw->con_getxy) {
577 offset = (start - vc->vc_origin) / 2;
578 xx = offset % vc->vc_cols;
579 yy = offset / vc->vc_cols;
580 } else {
581 int nxx, nyy;
582 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
583 xx = nxx; yy = nyy;
584 }
585 for(;;) {
586 u16 attrib = scr_readw(p) & 0xff00;
587 int startx = xx;
588 u16 *q = p;
589 while (xx < vc->vc_cols && count) {
590 if (attrib != (scr_readw(p) & 0xff00)) {
591 if (p > q)
592 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
593 startx = xx;
594 q = p;
595 attrib = scr_readw(p) & 0xff00;
596 }
597 p++;
598 xx++;
599 count--;
600 }
601 if (p > q)
602 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
603 if (!count)
604 break;
605 xx = 0;
606 yy++;
607 if (vc->vc_sw->con_getxy) {
608 p = (u16 *)start;
609 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
610 }
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614void update_region(struct vc_data *vc, unsigned long start, int count)
615{
616 WARN_CONSOLE_UNLOCKED();
617
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200618 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 hide_cursor(vc);
620 do_update_region(vc, start, count);
621 set_cursor(vc);
622 }
623}
624
625/* Structure of attributes is hardware-dependent */
626
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700627static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
628 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700631 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
632 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634/*
635 * ++roman: I completely changed the attribute format for monochrome
636 * mode (!can_do_color). The formerly used MDA (monochrome display
637 * adapter) format didn't allow the combination of certain effects.
638 * Now the attribute is just a bit vector:
639 * Bit 0..1: intensity (0..2)
640 * Bit 2 : underline
641 * Bit 3 : reverse
642 * Bit 7 : blink
643 */
644 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700645 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 if (!vc->vc_can_do_color)
647 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700648 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 (_underline ? 4 : 0) |
650 (_reverse ? 8 : 0) |
651 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700652 if (_italic)
653 a = (a & 0xF0) | vc->vc_itcolor;
654 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 a = (a & 0xf0) | vc->vc_ulcolor;
656 else if (_intensity == 0)
Adam Borowski1c65a872017-06-03 09:08:25 +0200657 a = (a & 0xf0) | vc->vc_halfcolor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 if (_reverse)
659 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
660 if (_blink)
661 a ^= 0x80;
662 if (_intensity == 2)
663 a ^= 0x08;
664 if (vc->vc_hi_font_mask == 0x100)
665 a <<= 1;
666 return a;
667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
670static void update_attr(struct vc_data *vc)
671{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700672 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
673 vc->vc_blink, vc->vc_underline,
674 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
675 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
678/* Note: inverting the screen twice should revert to the original state */
679void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
680{
681 unsigned short *p;
682
683 WARN_CONSOLE_UNLOCKED();
684
685 count /= 2;
686 p = screenpos(vc, offset, viewed);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200687 if (vc->vc_sw->con_invert_region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 vc->vc_sw->con_invert_region(vc, p, count);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200689 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 u16 *q = p;
691 int cnt = count;
692 u16 a;
693
694 if (!vc->vc_can_do_color) {
695 while (cnt--) {
696 a = scr_readw(q);
697 a ^= 0x0800;
698 scr_writew(a, q);
699 q++;
700 }
701 } else if (vc->vc_hi_font_mask == 0x100) {
702 while (cnt--) {
703 a = scr_readw(q);
704 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
705 scr_writew(a, q);
706 q++;
707 }
708 } else {
709 while (cnt--) {
710 a = scr_readw(q);
711 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
712 scr_writew(a, q);
713 q++;
714 }
715 }
716 }
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200717
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200718 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 do_update_region(vc, (unsigned long) p, count);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500720 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723/* used by selection: complement pointer position */
724void complement_pos(struct vc_data *vc, int offset)
725{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700726 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 static unsigned short old;
728 static unsigned short oldx, oldy;
729
730 WARN_CONSOLE_UNLOCKED();
731
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700732 if (old_offset != -1 && old_offset >= 0 &&
733 old_offset < vc->vc_screenbuf_size) {
734 scr_writew(old, screenpos(vc, old_offset, 1));
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200735 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 vc->vc_sw->con_putc(vc, old, oldy, oldx);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500737 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700739
740 old_offset = offset;
741
742 if (offset != -1 && offset >= 0 &&
743 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700745 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 p = screenpos(vc, offset, 1);
747 old = scr_readw(p);
748 new = old ^ vc->vc_complement_mask;
749 scr_writew(new, p);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200750 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 oldx = (offset >> 1) % vc->vc_cols;
752 oldy = (offset >> 1) / vc->vc_cols;
753 vc->vc_sw->con_putc(vc, new, oldy, oldx);
754 }
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500755 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757}
758
759static void insert_char(struct vc_data *vc, unsigned int nr)
760{
Jean-François Moine81732c32012-09-06 19:24:13 +0200761 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400763 vc_uniscr_insert(vc, nr);
Nicolas Pitrea883b702013-02-24 20:06:09 -0500764 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200765 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200767 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200768 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100769 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770}
771
772static void delete_char(struct vc_data *vc, unsigned int nr)
773{
Jean-François Moine81732c32012-09-06 19:24:13 +0200774 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400776 vc_uniscr_delete(vc, nr);
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100777 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200778 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
779 nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200781 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200782 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100783 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784}
785
Melchior FRANZe882f712015-11-01 19:48:18 +0100786static int softcursor_original = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788static void add_softcursor(struct vc_data *vc)
789{
790 int i = scr_readw((u16 *) vc->vc_pos);
791 u32 type = vc->vc_cursor_type;
792
793 if (! (type & 0x10)) return;
794 if (softcursor_original != -1) return;
795 softcursor_original = i;
796 i |= ((type >> 8) & 0xff00 );
797 i ^= ((type) & 0xff00 );
798 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
799 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
800 scr_writew(i, (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200801 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
803}
804
805static void hide_softcursor(struct vc_data *vc)
806{
807 if (softcursor_original != -1) {
808 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200809 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 vc->vc_sw->con_putc(vc, softcursor_original,
811 vc->vc_y, vc->vc_x);
812 softcursor_original = -1;
813 }
814}
815
816static void hide_cursor(struct vc_data *vc)
817{
818 if (vc == sel_cons)
819 clear_selection();
820 vc->vc_sw->con_cursor(vc, CM_ERASE);
821 hide_softcursor(vc);
822}
823
824static void set_cursor(struct vc_data *vc)
825{
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200826 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return;
828 if (vc->vc_deccm) {
829 if (vc == sel_cons)
830 clear_selection();
831 add_softcursor(vc);
832 if ((vc->vc_cursor_type & 0x0f) != 1)
833 vc->vc_sw->con_cursor(vc, CM_DRAW);
834 } else
835 hide_cursor(vc);
836}
837
838static void set_origin(struct vc_data *vc)
839{
840 WARN_CONSOLE_UNLOCKED();
841
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200842 if (!con_is_visible(vc) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 !vc->vc_sw->con_set_origin ||
844 !vc->vc_sw->con_set_origin(vc))
845 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
846 vc->vc_visible_origin = vc->vc_origin;
847 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
848 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
849}
850
Denys Vlasenkoc0f160a2015-10-27 18:46:47 +0100851static void save_screen(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 WARN_CONSOLE_UNLOCKED();
854
855 if (vc->vc_sw->con_save_screen)
856 vc->vc_sw->con_save_screen(vc);
857}
858
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100859static void flush_scrollback(struct vc_data *vc)
860{
861 WARN_CONSOLE_UNLOCKED();
862
863 if (vc->vc_sw->con_flush_scrollback)
864 vc->vc_sw->con_flush_scrollback(vc);
865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/*
868 * Redrawing of screen
869 */
870
Dave Airlie2a248302013-01-24 14:14:19 +1000871void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 unsigned short *p = (unsigned short *)vc->vc_origin;
874 int count = vc->vc_screenbuf_size / 2;
875 int mask = vc->vc_hi_font_mask | 0xff;
876
877 for (; count > 0; count--, p++) {
878 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
879 }
880}
881
882void redraw_screen(struct vc_data *vc, int is_switch)
883{
884 int redraw = 0;
885
886 WARN_CONSOLE_UNLOCKED();
887
888 if (!vc) {
889 /* strange ... */
890 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
891 return;
892 }
893
894 if (is_switch) {
895 struct vc_data *old_vc = vc_cons[fg_console].d;
896 if (old_vc == vc)
897 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200898 if (!con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 redraw = 1;
900 *vc->vc_display_fg = vc;
901 fg_console = vc->vc_num;
902 hide_cursor(old_vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200903 if (!con_is_visible(old_vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 save_screen(old_vc);
905 set_origin(old_vc);
906 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100907 if (tty0dev)
908 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 } else {
910 hide_cursor(vc);
911 redraw = 1;
912 }
913
914 if (redraw) {
915 int update;
916 int old_was_color = vc->vc_can_do_color;
917
918 set_origin(vc);
919 update = vc->vc_sw->con_switch(vc);
920 set_palette(vc);
921 /*
922 * If console changed from mono<->color, the best we can do
923 * is to clear the buffer attributes. As it currently stands,
924 * rebuilding new attributes from the old buffer is not doable
925 * without overly complex code.
926 */
927 if (old_was_color != vc->vc_can_do_color) {
928 update_attr(vc);
929 clear_buffer_attributes(vc);
930 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700931
932 /* Forcibly update if we're panicing */
933 if ((update && vc->vc_mode != KD_GRAPHICS) ||
934 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
936 }
937 set_cursor(vc);
938 if (is_switch) {
939 set_leds();
940 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800941 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 }
943}
944
945/*
946 * Allocation, freeing and resizing of VTs.
947 */
948
949int vc_cons_allocated(unsigned int i)
950{
951 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
952}
953
954static void visual_init(struct vc_data *vc, int num, int init)
955{
956 /* ++Geert: vc->vc_sw->con_init determines console size */
957 if (vc->vc_sw)
958 module_put(vc->vc_sw->owner);
959 vc->vc_sw = conswitchp;
960#ifndef VT_SINGLE_DRIVER
961 if (con_driver_map[num])
962 vc->vc_sw = con_driver_map[num];
963#endif
964 __module_get(vc->vc_sw->owner);
965 vc->vc_num = num;
966 vc->vc_display_fg = &master_display_fg;
Dongxing Zhang08b33242015-06-10 15:21:10 +0800967 if (vc->vc_uni_pagedir_loc)
968 con_free_unimap(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +0200970 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 vc->vc_hi_font_mask = 0;
972 vc->vc_complement_mask = 0;
973 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700974 vc->vc_panic_force_write = false;
David Daney1b459962016-05-17 11:41:04 -0700975 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 vc->vc_sw->con_init(vc, init);
977 if (!vc->vc_complement_mask)
978 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
979 vc->vc_s_complement_mask = vc->vc_complement_mask;
980 vc->vc_size_row = vc->vc_cols << 1;
981 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
982}
983
984int vc_allocate(unsigned int currcons) /* return 0 on success */
985{
Jiri Slaby34902b72016-03-31 10:08:15 +0200986 struct vt_notifier_param param;
987 struct vc_data *vc;
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 WARN_CONSOLE_UNLOCKED();
990
991 if (currcons >= MAX_NR_CONSOLES)
992 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Jiri Slaby34902b72016-03-31 10:08:15 +0200994 if (vc_cons[currcons].d)
995 return 0;
996
997 /* due to the granularity of kmalloc, we waste some memory here */
998 /* the alloc is done in two steps, to optimize the common situation
999 of a 25x80 console (structsize=216, screenbuf_size=4000) */
1000 /* although the numbers above are not valid since long ago, the
1001 point is still up-to-date and the comment still has its value
1002 even if only as a historical artifact. --mj, July 1998 */
1003 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
1004 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return -ENOMEM;
Jiri Slaby34902b72016-03-31 10:08:15 +02001006
1007 vc_cons[currcons].d = vc;
1008 tty_port_init(&vc->port);
1009 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1010
1011 visual_init(vc, currcons, 1);
1012
1013 if (!*vc->vc_uni_pagedir_loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 con_set_default_unimap(vc);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001015
Jiri Slaby34902b72016-03-31 10:08:15 +02001016 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
1017 if (!vc->vc_screenbuf)
1018 goto err_free;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001019
Jiri Slaby34902b72016-03-31 10:08:15 +02001020 /* If no drivers have overridden us and the user didn't pass a
1021 boot option, default to displaying the cursor */
1022 if (global_cursor_default == -1)
1023 global_cursor_default = 1;
1024
1025 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
1026 vcs_make_sysfs(currcons);
1027 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 return 0;
Jiri Slaby34902b72016-03-31 10:08:15 +02001030err_free:
1031 kfree(vc);
1032 vc_cons[currcons].d = NULL;
1033 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034}
1035
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001036static inline int resize_screen(struct vc_data *vc, int width, int height,
1037 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038{
1039 /* Resizes the resolution of the display adapater */
1040 int err = 0;
1041
1042 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001043 err = vc->vc_sw->con_resize(vc, width, height, user);
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 return err;
1046}
1047
1048/*
1049 * Change # of rows and columns (0 means unchanged/the size of fg_console)
1050 * [this is to be used together with some user program
1051 * like resize that changes the hardware videomode]
1052 */
1053#define VC_RESIZE_MAXCOL (32767)
1054#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001055
1056/**
1057 * vc_do_resize - resizing method for the tty
1058 * @tty: tty being resized
1059 * @real_tty: real tty (different to tty if a pty/tty pair)
1060 * @vc: virtual console private data
1061 * @cols: columns
1062 * @lines: lines
1063 *
1064 * Resize a virtual console, clipping according to the actual constraints.
1065 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +08001066 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001067 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001068 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001069 * ctrl_lock of the tty IFF a tty is passed.
1070 */
1071
Alan Coxfc6f6232009-01-02 13:43:17 +00001072static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1073 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -07001076 unsigned long end;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001077 unsigned int old_rows, old_row_size, first_copied_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -07001079 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 unsigned short *newscreen;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001081 struct uni_screen *new_uniscr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 WARN_CONSOLE_UNLOCKED();
1084
1085 if (!vc)
1086 return -ENXIO;
1087
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001088 user = vc->vc_resize_user;
1089 vc->vc_resize_user = 0;
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1092 return -EINVAL;
1093
1094 new_cols = (cols ? cols : vc->vc_cols);
1095 new_rows = (lines ? lines : vc->vc_rows);
1096 new_row_size = new_cols << 1;
1097 new_screen_size = new_row_size * new_rows;
1098
1099 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1100 return 0;
1101
Dmitry Vyukov32b29212016-10-14 15:18:28 +02001102 if (new_screen_size > (4 << 20))
1103 return -EINVAL;
Robert P. J. Day5cbded52006-12-13 00:35:56 -08001104 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (!newscreen)
1106 return -ENOMEM;
1107
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001108 if (get_vc_uniscr(vc)) {
1109 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1110 if (!new_uniscr) {
1111 kfree(newscreen);
1112 return -ENOMEM;
1113 }
1114 }
1115
Scot Doyle009e39a2016-10-13 12:12:43 -05001116 if (vc == sel_cons)
1117 clear_selection();
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001122 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 if (err) {
1124 kfree(newscreen);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001125 kfree(new_uniscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return err;
1127 }
1128
1129 vc->vc_rows = new_rows;
1130 vc->vc_cols = new_cols;
1131 vc->vc_size_row = new_row_size;
1132 vc->vc_screenbuf_size = new_screen_size;
1133
1134 rlth = min(old_row_size, new_row_size);
1135 rrem = new_row_size - rlth;
1136 old_origin = vc->vc_origin;
1137 new_origin = (long) newscreen;
1138 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001139
1140 if (vc->vc_y > new_rows) {
1141 if (old_rows - vc->vc_y < new_rows) {
1142 /*
1143 * Cursor near the bottom, copy contents from the
1144 * bottom of buffer
1145 */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001146 first_copied_row = (old_rows - new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001147 } else {
1148 /*
1149 * Cursor is in no man's land, copy 1/2 screenful
1150 * from the top and bottom of cursor position
1151 */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001152 first_copied_row = (vc->vc_y - new_rows/2);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001153 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001154 old_origin += first_copied_row * old_row_size;
1155 } else
1156 first_copied_row = 0;
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02001157 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001159 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1160 get_vc_uniscr(vc), rlth/2, first_copied_row,
1161 min(old_rows, new_rows));
1162 vc_uniscr_set(vc, new_uniscr);
1163
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 update_attr(vc);
1165
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001166 while (old_origin < end) {
1167 scr_memcpyw((unsigned short *) new_origin,
1168 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001170 scr_memsetw((void *)(new_origin + rlth),
1171 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 old_origin += old_row_size;
1173 new_origin += new_row_size;
1174 }
1175 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001176 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1177 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001178 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 vc->vc_screenbuf_size = new_screen_size;
1181 set_origin(vc);
1182
1183 /* do part of a reset_terminal() */
1184 vc->vc_top = 0;
1185 vc->vc_bottom = vc->vc_rows;
1186 gotoxy(vc, vc->vc_x, vc->vc_y);
1187 save_cur(vc);
1188
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001189 if (tty) {
1190 /* Rewrite the requested winsize data with the actual
1191 resulting sizes */
1192 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 memset(&ws, 0, sizeof(ws));
1194 ws.ws_row = vc->vc_rows;
1195 ws.ws_col = vc->vc_cols;
1196 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +00001197 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001200 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07001202 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 return err;
1204}
1205
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001206/**
1207 * vc_resize - resize a VT
1208 * @vc: virtual console
1209 * @cols: columns
1210 * @rows: rows
1211 *
1212 * Resize a virtual console as seen from the console end of things. We
1213 * use the common vc_do_resize methods to update the structures. The
1214 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +02001215 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001216 */
1217
1218int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -07001219{
Alan Cox8ce73262010-06-01 22:52:56 +02001220 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001221}
1222
1223/**
1224 * vt_resize - resize a VT
1225 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001226 * @ws: winsize attributes
1227 *
1228 * Resize a virtual terminal. This is called by the tty layer as we
1229 * register our own handler for resizing. The mutual helper does all
1230 * the actual work.
1231 *
1232 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -04001233 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001234 */
Roel Kluinda2bdf92009-01-07 18:09:15 -08001235static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001236{
1237 struct vc_data *vc = tty->driver_data;
1238 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
Torben Hohnac751ef2011-01-25 15:07:35 -08001240 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001241 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001242 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001243 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001244}
1245
Peter Hurley421b40a2013-05-17 12:41:03 -04001246struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247{
Peter Hurley421b40a2013-05-17 12:41:03 -04001248 struct vc_data *vc = NULL;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 WARN_CONSOLE_UNLOCKED();
1251
1252 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001253 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001254
Peter Hurley421b40a2013-05-17 12:41:03 -04001255 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001256 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001257 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001259 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001260 module_put(vc->vc_sw->owner);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001261 vc_uniscr_set(vc, NULL);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001262 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 vc_cons[currcons].d = NULL;
1264 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001265 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266}
1267
1268/*
1269 * VT102 emulator
1270 */
1271
Alan Cox079c9532012-02-28 14:49:23 +00001272#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1273#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1274#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276#define decarm VC_REPEAT
1277#define decckm VC_CKMODE
1278#define kbdapplic VC_APPLIC
1279#define lnm VC_CRLF
1280
1281/*
1282 * this is what the terminal answers to a ESC-Z or csi0c query.
1283 */
1284#define VT100ID "\033[?1;2c"
1285#define VT102ID "\033[?6c"
1286
Jiri Slaby8ede5cc2016-03-31 10:08:16 +02001287const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 8,12,10,14, 9,13,11,15 };
1289
1290/* the default colour table, for VGA+ colour systems */
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001291unsigned char default_red[] = {
1292 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1293 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1294};
1295module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001297unsigned char default_grn[] = {
1298 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1299 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1300};
1301module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1302
1303unsigned char default_blu[] = {
1304 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1305 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1306};
1307module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309/*
1310 * gotoxy() must verify all boundaries, because the arguments
1311 * might also be negative. If the given position is out of
1312 * bounds, the cursor is placed at the nearest margin.
1313 */
1314static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1315{
1316 int min_y, max_y;
1317
1318 if (new_x < 0)
1319 vc->vc_x = 0;
1320 else {
1321 if (new_x >= vc->vc_cols)
1322 vc->vc_x = vc->vc_cols - 1;
1323 else
1324 vc->vc_x = new_x;
1325 }
1326
1327 if (vc->vc_decom) {
1328 min_y = vc->vc_top;
1329 max_y = vc->vc_bottom;
1330 } else {
1331 min_y = 0;
1332 max_y = vc->vc_rows;
1333 }
1334 if (new_y < min_y)
1335 vc->vc_y = min_y;
1336 else if (new_y >= max_y)
1337 vc->vc_y = max_y - 1;
1338 else
1339 vc->vc_y = new_y;
1340 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1341 vc->vc_need_wrap = 0;
1342}
1343
1344/* for absolute user moves, when decom is set */
1345static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1346{
1347 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1348}
1349
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001350void scrollback(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001352 scrolldelta(-(vc->vc_rows / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355void scrollfront(struct vc_data *vc, int lines)
1356{
1357 if (!lines)
1358 lines = vc->vc_rows / 2;
1359 scrolldelta(lines);
1360}
1361
1362static void lf(struct vc_data *vc)
1363{
1364 /* don't scroll if above bottom of scrolling region, or
1365 * if below scrolling region
1366 */
1367 if (vc->vc_y + 1 == vc->vc_bottom)
Jiri Slaby89765b92016-10-03 11:18:34 +02001368 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 else if (vc->vc_y < vc->vc_rows - 1) {
1370 vc->vc_y++;
1371 vc->vc_pos += vc->vc_size_row;
1372 }
1373 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001374 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377static void ri(struct vc_data *vc)
1378{
1379 /* don't scroll if below top of scrolling region, or
1380 * if above scrolling region
1381 */
1382 if (vc->vc_y == vc->vc_top)
Jiri Slaby89765b92016-10-03 11:18:34 +02001383 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 else if (vc->vc_y > 0) {
1385 vc->vc_y--;
1386 vc->vc_pos -= vc->vc_size_row;
1387 }
1388 vc->vc_need_wrap = 0;
1389}
1390
1391static inline void cr(struct vc_data *vc)
1392{
1393 vc->vc_pos -= vc->vc_x << 1;
1394 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001395 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
1398static inline void bs(struct vc_data *vc)
1399{
1400 if (vc->vc_x) {
1401 vc->vc_pos -= 2;
1402 vc->vc_x--;
1403 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001404 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406}
1407
1408static inline void del(struct vc_data *vc)
1409{
1410 /* ignored */
1411}
1412
1413static void csi_J(struct vc_data *vc, int vpar)
1414{
1415 unsigned int count;
1416 unsigned short * start;
1417
1418 switch (vpar) {
1419 case 0: /* erase from cursor to end of display */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001420 vc_uniscr_clear_line(vc, vc->vc_x,
1421 vc->vc_cols - vc->vc_x);
1422 vc_uniscr_clear_lines(vc, vc->vc_y + 1,
1423 vc->vc_rows - vc->vc_y - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1425 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
1427 case 1: /* erase from start to cursor */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001428 vc_uniscr_clear_line(vc, 0, vc->vc_x + 1);
1429 vc_uniscr_clear_lines(vc, 0, vc->vc_y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1431 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 case 2: /* erase whole display */
Nicolas Pitre4b4ecd92018-05-14 11:53:04 -04001434 case 3: /* (and scrollback buffer later) */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001435 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 count = vc->vc_cols * vc->vc_rows;
1437 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 break;
1439 default:
1440 return;
1441 }
1442 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Nicolas Pitre4b4ecd92018-05-14 11:53:04 -04001443 if (vpar == 3) {
1444 set_origin(vc);
1445 flush_scrollback(vc);
1446 if (con_is_visible(vc))
1447 update_screen(vc);
1448 } else if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001449 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 vc->vc_need_wrap = 0;
1451}
1452
1453static void csi_K(struct vc_data *vc, int vpar)
1454{
1455 unsigned int count;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001456 unsigned short *start = (unsigned short *)vc->vc_pos;
1457 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
1459 switch (vpar) {
1460 case 0: /* erase from cursor to end of line */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001461 offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 count = vc->vc_cols - vc->vc_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 break;
1464 case 1: /* erase from start of line to cursor */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001465 offset = -vc->vc_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 count = vc->vc_x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 break;
1468 case 2: /* erase whole line */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001469 offset = -vc->vc_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 break;
1472 default:
1473 return;
1474 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001475 vc_uniscr_clear_line(vc, vc->vc_x + offset, count);
1476 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001478 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +02001479 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480}
1481
1482static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1483{ /* not vt100? */
1484 int count;
1485
1486 if (!vpar)
1487 vpar++;
1488 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1489
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001490 vc_uniscr_clear_line(vc, vc->vc_x, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001492 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1494 vc->vc_need_wrap = 0;
1495}
1496
1497static void default_attr(struct vc_data *vc)
1498{
1499 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001500 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 vc->vc_underline = 0;
1502 vc->vc_reverse = 0;
1503 vc->vc_blink = 0;
1504 vc->vc_color = vc->vc_def_color;
1505}
1506
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001507struct rgb { u8 r; u8 g; u8 b; };
1508
Jiri Slaby0f91e142016-06-23 13:34:32 +02001509static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001510{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001511 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001512 c->r = i&1 ? 0xaa : 0x00;
1513 c->g = i&2 ? 0xaa : 0x00;
1514 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001515 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001516 c->r = i&1 ? 0xff : 0x55;
1517 c->g = i&2 ? 0xff : 0x55;
1518 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001519 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001520 c->r = (i - 16) / 36 * 85 / 2;
1521 c->g = (i - 16) / 6 % 6 * 85 / 2;
1522 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001523 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001524 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001525}
1526
Jiri Slaby0f91e142016-06-23 13:34:32 +02001527static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001528{
Jiri Slaby193df022016-06-23 13:34:33 +02001529 u8 hue = 0, max = max3(c->r, c->g, c->b);
1530
1531 if (c->r > max / 2)
1532 hue |= 4;
1533 if (c->g > max / 2)
1534 hue |= 2;
1535 if (c->b > max / 2)
1536 hue |= 1;
1537
1538 if (hue == 7 && max <= 0x55) {
1539 hue = 0;
1540 vc->vc_intensity = 2;
1541 } else if (max > 0xaa)
1542 vc->vc_intensity = 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001543 else
Jiri Slaby193df022016-06-23 13:34:33 +02001544 vc->vc_intensity = 1;
1545
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001546 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1547}
1548
Jiri Slaby0f91e142016-06-23 13:34:32 +02001549static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001550{
1551 /* For backgrounds, err on the dark side. */
1552 vc->vc_color = (vc->vc_color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001553 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001554}
1555
Jiri Slabye05ab232016-06-23 13:34:31 +02001556/*
1557 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1558 * and thus need to be detected and ignored by hand. Strictly speaking, that
1559 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1560 * no one produces such codes and almost no one accepts them.
1561 *
1562 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1563 * supporting them.
1564 */
1565static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001566 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001567{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001568 struct rgb c;
1569
Jiri Slabye05ab232016-06-23 13:34:31 +02001570 i++;
1571 if (i > vc->vc_npar)
1572 return i;
1573
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001574 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001575 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001576 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001577 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001578 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001579 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001580 c.r = vc->vc_par[i + 1];
1581 c.g = vc->vc_par[i + 2];
1582 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001583 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001584 } else
1585 return i;
1586
1587 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001588
1589 return i;
1590}
1591
Torben Hohnac751ef2011-01-25 15:07:35 -08001592/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static void csi_m(struct vc_data *vc)
1594{
1595 int i;
1596
1597 for (i = 0; i <= vc->vc_npar; i++)
1598 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001599 case 0: /* all attributes off */
1600 default_attr(vc);
1601 break;
1602 case 1:
1603 vc->vc_intensity = 2;
1604 break;
1605 case 2:
1606 vc->vc_intensity = 0;
1607 break;
1608 case 3:
1609 vc->vc_italic = 1;
1610 break;
Mike Frysinger65d99822018-01-29 17:08:21 -05001611 case 21:
1612 /*
1613 * No console drivers support double underline, so
1614 * convert it to a single underline.
1615 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001616 case 4:
1617 vc->vc_underline = 1;
1618 break;
1619 case 5:
1620 vc->vc_blink = 1;
1621 break;
1622 case 7:
1623 vc->vc_reverse = 1;
1624 break;
1625 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1626 * Select primary font, don't display control chars if
1627 * defined, don't set bit 8 on output.
1628 */
1629 vc->vc_translate = set_translate(vc->vc_charset == 0
1630 ? vc->vc_G0_charset
1631 : vc->vc_G1_charset, vc);
1632 vc->vc_disp_ctrl = 0;
1633 vc->vc_toggle_meta = 0;
1634 break;
1635 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1636 * Select first alternate font, lets chars < 32 be
1637 * displayed as ROM chars.
1638 */
1639 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1640 vc->vc_disp_ctrl = 1;
1641 vc->vc_toggle_meta = 0;
1642 break;
1643 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1644 * Select second alternate font, toggle high bit
1645 * before displaying as ROM char.
1646 */
1647 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1648 vc->vc_disp_ctrl = 1;
1649 vc->vc_toggle_meta = 1;
1650 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001651 case 22:
1652 vc->vc_intensity = 1;
1653 break;
1654 case 23:
1655 vc->vc_italic = 0;
1656 break;
1657 case 24:
1658 vc->vc_underline = 0;
1659 break;
1660 case 25:
1661 vc->vc_blink = 0;
1662 break;
1663 case 27:
1664 vc->vc_reverse = 0;
1665 break;
1666 case 38:
1667 i = vc_t416_color(vc, i, rgb_foreground);
1668 break;
1669 case 48:
1670 i = vc_t416_color(vc, i, rgb_background);
1671 break;
1672 case 39:
1673 vc->vc_color = (vc->vc_def_color & 0x0f) |
1674 (vc->vc_color & 0xf0);
1675 break;
1676 case 49:
1677 vc->vc_color = (vc->vc_def_color & 0xf0) |
1678 (vc->vc_color & 0x0f);
1679 break;
1680 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001681 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1682 if (vc->vc_par[i] < 100)
1683 vc->vc_intensity = 2;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001684 vc->vc_par[i] -= 60;
1685 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001686 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1687 vc->vc_color = color_table[vc->vc_par[i] - 30]
1688 | (vc->vc_color & 0xf0);
1689 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1690 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1691 | (vc->vc_color & 0x0f);
1692 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 }
1694 update_attr(vc);
1695}
1696
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001697static void respond_string(const char *p, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698{
1699 while (*p) {
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001700 tty_insert_flip_char(port, *p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 p++;
1702 }
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001703 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
1706static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1707{
1708 char buf[40];
1709
1710 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001711 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714static inline void status_report(struct tty_struct *tty)
1715{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001716 respond_string("\033[0n", tty->port); /* Terminal ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717}
1718
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001719static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001721 respond_string(VT102ID, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
1724void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1725{
1726 char buf[8];
1727
1728 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1729 (char)('!' + mry));
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001730 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731}
1732
1733/* invoked via ioctl(TIOCLINUX) and through set_selection */
1734int mouse_reporting(void)
1735{
1736 return vc_cons[fg_console].d->vc_report_mouse;
1737}
1738
Torben Hohnac751ef2011-01-25 15:07:35 -08001739/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740static void set_mode(struct vc_data *vc, int on_off)
1741{
1742 int i;
1743
1744 for (i = 0; i <= vc->vc_npar; i++)
1745 if (vc->vc_ques) {
1746 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1747 case 1: /* Cursor keys send ^[Ox/^[[x */
1748 if (on_off)
1749 set_kbd(vc, decckm);
1750 else
1751 clr_kbd(vc, decckm);
1752 break;
1753 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#if 0
1755 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1756 /* this alone does not suffice; some user mode
1757 utility has to change the hardware regs */
1758#endif
1759 break;
1760 case 5: /* Inverted screen on/off */
1761 if (vc->vc_decscnm != on_off) {
1762 vc->vc_decscnm = on_off;
1763 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1764 update_attr(vc);
1765 }
1766 break;
1767 case 6: /* Origin relative/absolute */
1768 vc->vc_decom = on_off;
1769 gotoxay(vc, 0, 0);
1770 break;
1771 case 7: /* Autowrap on/off */
1772 vc->vc_decawm = on_off;
1773 break;
1774 case 8: /* Autorepeat on/off */
1775 if (on_off)
1776 set_kbd(vc, decarm);
1777 else
1778 clr_kbd(vc, decarm);
1779 break;
1780 case 9:
1781 vc->vc_report_mouse = on_off ? 1 : 0;
1782 break;
1783 case 25: /* Cursor on/off */
1784 vc->vc_deccm = on_off;
1785 break;
1786 case 1000:
1787 vc->vc_report_mouse = on_off ? 2 : 0;
1788 break;
1789 }
1790 } else {
1791 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1792 case 3: /* Monitor (display ctrls) */
1793 vc->vc_disp_ctrl = on_off;
1794 break;
1795 case 4: /* Insert Mode on/off */
1796 vc->vc_decim = on_off;
1797 break;
1798 case 20: /* Lf, Enter == CrLf/Lf */
1799 if (on_off)
1800 set_kbd(vc, lnm);
1801 else
1802 clr_kbd(vc, lnm);
1803 break;
1804 }
1805 }
1806}
1807
Torben Hohnac751ef2011-01-25 15:07:35 -08001808/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static void setterm_command(struct vc_data *vc)
1810{
1811 switch(vc->vc_par[0]) {
1812 case 1: /* set color for underline mode */
1813 if (vc->vc_can_do_color &&
1814 vc->vc_par[1] < 16) {
1815 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1816 if (vc->vc_underline)
1817 update_attr(vc);
1818 }
1819 break;
1820 case 2: /* set color for half intensity mode */
1821 if (vc->vc_can_do_color &&
1822 vc->vc_par[1] < 16) {
1823 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1824 if (vc->vc_intensity == 0)
1825 update_attr(vc);
1826 }
1827 break;
1828 case 8: /* store colors as defaults */
1829 vc->vc_def_color = vc->vc_attr;
1830 if (vc->vc_hi_font_mask == 0x100)
1831 vc->vc_def_color >>= 1;
1832 default_attr(vc);
1833 update_attr(vc);
1834 break;
1835 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001836 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 poke_blanked_console();
1838 break;
1839 case 10: /* set bell frequency in Hz */
1840 if (vc->vc_npar >= 1)
1841 vc->vc_bell_pitch = vc->vc_par[1];
1842 else
1843 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1844 break;
1845 case 11: /* set bell duration in msec */
1846 if (vc->vc_npar >= 1)
1847 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
Nicholas Mc Guire3372ec22015-02-09 10:54:10 -05001848 msecs_to_jiffies(vc->vc_par[1]) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 else
1850 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1851 break;
1852 case 12: /* bring specified console to the front */
1853 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1854 set_console(vc->vc_par[1] - 1);
1855 break;
1856 case 13: /* unblank the screen */
1857 poke_blanked_console();
1858 break;
1859 case 14: /* set vesa powerdown interval */
1860 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1861 break;
1862 case 15: /* activate the previous console */
1863 set_console(last_console);
1864 break;
Scot Doylebd633642015-03-26 13:54:39 +00001865 case 16: /* set cursor blink duration in msec */
1866 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1867 vc->vc_par[1] <= USHRT_MAX)
1868 vc->vc_cur_blink_ms = vc->vc_par[1];
1869 else
1870 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1871 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 }
1873}
1874
Torben Hohnac751ef2011-01-25 15:07:35 -08001875/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876static void csi_at(struct vc_data *vc, unsigned int nr)
1877{
1878 if (nr > vc->vc_cols - vc->vc_x)
1879 nr = vc->vc_cols - vc->vc_x;
1880 else if (!nr)
1881 nr = 1;
1882 insert_char(vc, nr);
1883}
1884
Torben Hohnac751ef2011-01-25 15:07:35 -08001885/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886static void csi_L(struct vc_data *vc, unsigned int nr)
1887{
1888 if (nr > vc->vc_rows - vc->vc_y)
1889 nr = vc->vc_rows - vc->vc_y;
1890 else if (!nr)
1891 nr = 1;
Jiri Slaby89765b92016-10-03 11:18:34 +02001892 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_DOWN, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 vc->vc_need_wrap = 0;
1894}
1895
Torben Hohnac751ef2011-01-25 15:07:35 -08001896/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897static void csi_P(struct vc_data *vc, unsigned int nr)
1898{
1899 if (nr > vc->vc_cols - vc->vc_x)
1900 nr = vc->vc_cols - vc->vc_x;
1901 else if (!nr)
1902 nr = 1;
1903 delete_char(vc, nr);
1904}
1905
Torben Hohnac751ef2011-01-25 15:07:35 -08001906/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static void csi_M(struct vc_data *vc, unsigned int nr)
1908{
1909 if (nr > vc->vc_rows - vc->vc_y)
1910 nr = vc->vc_rows - vc->vc_y;
1911 else if (!nr)
1912 nr=1;
Jiri Slaby89765b92016-10-03 11:18:34 +02001913 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_UP, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 vc->vc_need_wrap = 0;
1915}
1916
Torben Hohnac751ef2011-01-25 15:07:35 -08001917/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static void save_cur(struct vc_data *vc)
1919{
1920 vc->vc_saved_x = vc->vc_x;
1921 vc->vc_saved_y = vc->vc_y;
1922 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001923 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 vc->vc_s_underline = vc->vc_underline;
1925 vc->vc_s_blink = vc->vc_blink;
1926 vc->vc_s_reverse = vc->vc_reverse;
1927 vc->vc_s_charset = vc->vc_charset;
1928 vc->vc_s_color = vc->vc_color;
1929 vc->vc_saved_G0 = vc->vc_G0_charset;
1930 vc->vc_saved_G1 = vc->vc_G1_charset;
1931}
1932
Torben Hohnac751ef2011-01-25 15:07:35 -08001933/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934static void restore_cur(struct vc_data *vc)
1935{
1936 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1937 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001938 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 vc->vc_underline = vc->vc_s_underline;
1940 vc->vc_blink = vc->vc_s_blink;
1941 vc->vc_reverse = vc->vc_s_reverse;
1942 vc->vc_charset = vc->vc_s_charset;
1943 vc->vc_color = vc->vc_s_color;
1944 vc->vc_G0_charset = vc->vc_saved_G0;
1945 vc->vc_G1_charset = vc->vc_saved_G1;
1946 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1947 update_attr(vc);
1948 vc->vc_need_wrap = 0;
1949}
1950
Adam Borowskib290af62014-02-19 01:40:16 +01001951enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01001953 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
Torben Hohnac751ef2011-01-25 15:07:35 -08001955/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956static void reset_terminal(struct vc_data *vc, int do_clear)
1957{
1958 vc->vc_top = 0;
1959 vc->vc_bottom = vc->vc_rows;
1960 vc->vc_state = ESnormal;
1961 vc->vc_ques = 0;
1962 vc->vc_translate = set_translate(LAT1_MAP, vc);
1963 vc->vc_G0_charset = LAT1_MAP;
1964 vc->vc_G1_charset = GRAF_MAP;
1965 vc->vc_charset = 0;
1966 vc->vc_need_wrap = 0;
1967 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001968 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 vc->vc_utf_count = 0;
1970
1971 vc->vc_disp_ctrl = 0;
1972 vc->vc_toggle_meta = 0;
1973
1974 vc->vc_decscnm = 0;
1975 vc->vc_decom = 0;
1976 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001977 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 vc->vc_decim = 0;
1979
Alan Cox079c9532012-02-28 14:49:23 +00001980 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001982 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 vc->vc_complement_mask = vc->vc_s_complement_mask;
1984
1985 default_attr(vc);
1986 update_attr(vc);
1987
Linus Torvaldsf1869a82018-03-24 10:43:26 +01001988 vc->vc_tab_stop[0] =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 vc->vc_tab_stop[1] =
1990 vc->vc_tab_stop[2] =
1991 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001992 vc->vc_tab_stop[4] =
1993 vc->vc_tab_stop[5] =
1994 vc->vc_tab_stop[6] =
1995 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
1997 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1998 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00001999 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000
2001 gotoxy(vc, 0, 0);
2002 save_cur(vc);
2003 if (do_clear)
2004 csi_J(vc, 2);
2005}
2006
Torben Hohnac751ef2011-01-25 15:07:35 -08002007/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2009{
2010 /*
2011 * Control characters can be used in the _middle_
2012 * of an escape sequence.
2013 */
Adam Borowski63f3a162014-02-19 01:38:04 +01002014 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2015 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 switch (c) {
2017 case 0:
2018 return;
2019 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01002020 if (vc->vc_state == ESosc)
2021 vc->vc_state = ESnormal;
2022 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2024 return;
2025 case 8:
2026 bs(vc);
2027 return;
2028 case 9:
2029 vc->vc_pos -= (vc->vc_x << 1);
2030 while (vc->vc_x < vc->vc_cols - 1) {
2031 vc->vc_x++;
Linus Torvaldsf1869a82018-03-24 10:43:26 +01002032 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 break;
2034 }
2035 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002036 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return;
2038 case 10: case 11: case 12:
2039 lf(vc);
2040 if (!is_kbd(vc, lnm))
2041 return;
2042 case 13:
2043 cr(vc);
2044 return;
2045 case 14:
2046 vc->vc_charset = 1;
2047 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2048 vc->vc_disp_ctrl = 1;
2049 return;
2050 case 15:
2051 vc->vc_charset = 0;
2052 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2053 vc->vc_disp_ctrl = 0;
2054 return;
2055 case 24: case 26:
2056 vc->vc_state = ESnormal;
2057 return;
2058 case 27:
2059 vc->vc_state = ESesc;
2060 return;
2061 case 127:
2062 del(vc);
2063 return;
2064 case 128+27:
2065 vc->vc_state = ESsquare;
2066 return;
2067 }
2068 switch(vc->vc_state) {
2069 case ESesc:
2070 vc->vc_state = ESnormal;
2071 switch (c) {
2072 case '[':
2073 vc->vc_state = ESsquare;
2074 return;
2075 case ']':
2076 vc->vc_state = ESnonstd;
2077 return;
2078 case '%':
2079 vc->vc_state = ESpercent;
2080 return;
2081 case 'E':
2082 cr(vc);
2083 lf(vc);
2084 return;
2085 case 'M':
2086 ri(vc);
2087 return;
2088 case 'D':
2089 lf(vc);
2090 return;
2091 case 'H':
Linus Torvaldsf1869a82018-03-24 10:43:26 +01002092 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return;
2094 case 'Z':
2095 respond_ID(tty);
2096 return;
2097 case '7':
2098 save_cur(vc);
2099 return;
2100 case '8':
2101 restore_cur(vc);
2102 return;
2103 case '(':
2104 vc->vc_state = ESsetG0;
2105 return;
2106 case ')':
2107 vc->vc_state = ESsetG1;
2108 return;
2109 case '#':
2110 vc->vc_state = EShash;
2111 return;
2112 case 'c':
2113 reset_terminal(vc, 1);
2114 return;
2115 case '>': /* Numeric keypad */
2116 clr_kbd(vc, kbdapplic);
2117 return;
2118 case '=': /* Appl. keypad */
2119 set_kbd(vc, kbdapplic);
2120 return;
2121 }
2122 return;
2123 case ESnonstd:
2124 if (c=='P') { /* palette escape sequence */
2125 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2126 vc->vc_par[vc->vc_npar] = 0;
2127 vc->vc_npar = 0;
2128 vc->vc_state = ESpalette;
2129 return;
2130 } else if (c=='R') { /* reset palette */
2131 reset_palette(vc);
2132 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01002133 } else if (c>='0' && c<='9')
2134 vc->vc_state = ESosc;
2135 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 vc->vc_state = ESnormal;
2137 return;
2138 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03002139 if (isxdigit(c)) {
2140 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if (vc->vc_npar == 7) {
2142 int i = vc->vc_par[0] * 3, j = 1;
2143 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2144 vc->vc_palette[i++] += vc->vc_par[j++];
2145 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2146 vc->vc_palette[i++] += vc->vc_par[j++];
2147 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2148 vc->vc_palette[i] += vc->vc_par[j];
2149 set_palette(vc);
2150 vc->vc_state = ESnormal;
2151 }
2152 } else
2153 vc->vc_state = ESnormal;
2154 return;
2155 case ESsquare:
2156 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2157 vc->vc_par[vc->vc_npar] = 0;
2158 vc->vc_npar = 0;
2159 vc->vc_state = ESgetpars;
2160 if (c == '[') { /* Function key */
2161 vc->vc_state=ESfunckey;
2162 return;
2163 }
2164 vc->vc_ques = (c == '?');
2165 if (vc->vc_ques)
2166 return;
2167 case ESgetpars:
2168 if (c == ';' && vc->vc_npar < NPAR - 1) {
2169 vc->vc_npar++;
2170 return;
2171 } else if (c>='0' && c<='9') {
2172 vc->vc_par[vc->vc_npar] *= 10;
2173 vc->vc_par[vc->vc_npar] += c - '0';
2174 return;
Adam Borowskib290af62014-02-19 01:40:16 +01002175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 vc->vc_state = ESnormal;
2177 switch(c) {
2178 case 'h':
2179 set_mode(vc, 1);
2180 return;
2181 case 'l':
2182 set_mode(vc, 0);
2183 return;
2184 case 'c':
2185 if (vc->vc_ques) {
2186 if (vc->vc_par[0])
2187 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2188 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002189 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 return;
2191 }
2192 break;
2193 case 'm':
2194 if (vc->vc_ques) {
2195 clear_selection();
2196 if (vc->vc_par[0])
2197 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2198 else
2199 vc->vc_complement_mask = vc->vc_s_complement_mask;
2200 return;
2201 }
2202 break;
2203 case 'n':
2204 if (!vc->vc_ques) {
2205 if (vc->vc_par[0] == 5)
2206 status_report(tty);
2207 else if (vc->vc_par[0] == 6)
2208 cursor_report(vc, tty);
2209 }
2210 return;
2211 }
2212 if (vc->vc_ques) {
2213 vc->vc_ques = 0;
2214 return;
2215 }
2216 switch(c) {
2217 case 'G': case '`':
2218 if (vc->vc_par[0])
2219 vc->vc_par[0]--;
2220 gotoxy(vc, vc->vc_par[0], vc->vc_y);
2221 return;
2222 case 'A':
2223 if (!vc->vc_par[0])
2224 vc->vc_par[0]++;
2225 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
2226 return;
2227 case 'B': case 'e':
2228 if (!vc->vc_par[0])
2229 vc->vc_par[0]++;
2230 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
2231 return;
2232 case 'C': case 'a':
2233 if (!vc->vc_par[0])
2234 vc->vc_par[0]++;
2235 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
2236 return;
2237 case 'D':
2238 if (!vc->vc_par[0])
2239 vc->vc_par[0]++;
2240 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
2241 return;
2242 case 'E':
2243 if (!vc->vc_par[0])
2244 vc->vc_par[0]++;
2245 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
2246 return;
2247 case 'F':
2248 if (!vc->vc_par[0])
2249 vc->vc_par[0]++;
2250 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
2251 return;
2252 case 'd':
2253 if (vc->vc_par[0])
2254 vc->vc_par[0]--;
2255 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2256 return;
2257 case 'H': case 'f':
2258 if (vc->vc_par[0])
2259 vc->vc_par[0]--;
2260 if (vc->vc_par[1])
2261 vc->vc_par[1]--;
2262 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2263 return;
2264 case 'J':
2265 csi_J(vc, vc->vc_par[0]);
2266 return;
2267 case 'K':
2268 csi_K(vc, vc->vc_par[0]);
2269 return;
2270 case 'L':
2271 csi_L(vc, vc->vc_par[0]);
2272 return;
2273 case 'M':
2274 csi_M(vc, vc->vc_par[0]);
2275 return;
2276 case 'P':
2277 csi_P(vc, vc->vc_par[0]);
2278 return;
2279 case 'c':
2280 if (!vc->vc_par[0])
2281 respond_ID(tty);
2282 return;
2283 case 'g':
2284 if (!vc->vc_par[0])
Linus Torvaldsf1869a82018-03-24 10:43:26 +01002285 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 else if (vc->vc_par[0] == 3) {
2287 vc->vc_tab_stop[0] =
2288 vc->vc_tab_stop[1] =
2289 vc->vc_tab_stop[2] =
2290 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08002291 vc->vc_tab_stop[4] =
2292 vc->vc_tab_stop[5] =
2293 vc->vc_tab_stop[6] =
2294 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 }
2296 return;
2297 case 'm':
2298 csi_m(vc);
2299 return;
2300 case 'q': /* DECLL - but only 3 leds */
2301 /* map 0,1,2,3 to 0,1,2,4 */
2302 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002303 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2305 return;
2306 case 'r':
2307 if (!vc->vc_par[0])
2308 vc->vc_par[0]++;
2309 if (!vc->vc_par[1])
2310 vc->vc_par[1] = vc->vc_rows;
2311 /* Minimum allowed region is 2 lines */
2312 if (vc->vc_par[0] < vc->vc_par[1] &&
2313 vc->vc_par[1] <= vc->vc_rows) {
2314 vc->vc_top = vc->vc_par[0] - 1;
2315 vc->vc_bottom = vc->vc_par[1];
2316 gotoxay(vc, 0, 0);
2317 }
2318 return;
2319 case 's':
2320 save_cur(vc);
2321 return;
2322 case 'u':
2323 restore_cur(vc);
2324 return;
2325 case 'X':
2326 csi_X(vc, vc->vc_par[0]);
2327 return;
2328 case '@':
2329 csi_at(vc, vc->vc_par[0]);
2330 return;
2331 case ']': /* setterm functions */
2332 setterm_command(vc);
2333 return;
2334 }
2335 return;
2336 case ESpercent:
2337 vc->vc_state = ESnormal;
2338 switch (c) {
2339 case '@': /* defined in ISO 2022 */
2340 vc->vc_utf = 0;
2341 return;
2342 case 'G': /* prelim official escape code */
2343 case '8': /* retained for compatibility */
2344 vc->vc_utf = 1;
2345 return;
2346 }
2347 return;
2348 case ESfunckey:
2349 vc->vc_state = ESnormal;
2350 return;
2351 case EShash:
2352 vc->vc_state = ESnormal;
2353 if (c == '8') {
2354 /* DEC screen alignment test. kludge :-) */
2355 vc->vc_video_erase_char =
2356 (vc->vc_video_erase_char & 0xff00) | 'E';
2357 csi_J(vc, 2);
2358 vc->vc_video_erase_char =
2359 (vc->vc_video_erase_char & 0xff00) | ' ';
2360 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2361 }
2362 return;
2363 case ESsetG0:
2364 if (c == '0')
2365 vc->vc_G0_charset = GRAF_MAP;
2366 else if (c == 'B')
2367 vc->vc_G0_charset = LAT1_MAP;
2368 else if (c == 'U')
2369 vc->vc_G0_charset = IBMPC_MAP;
2370 else if (c == 'K')
2371 vc->vc_G0_charset = USER_MAP;
2372 if (vc->vc_charset == 0)
2373 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2374 vc->vc_state = ESnormal;
2375 return;
2376 case ESsetG1:
2377 if (c == '0')
2378 vc->vc_G1_charset = GRAF_MAP;
2379 else if (c == 'B')
2380 vc->vc_G1_charset = LAT1_MAP;
2381 else if (c == 'U')
2382 vc->vc_G1_charset = IBMPC_MAP;
2383 else if (c == 'K')
2384 vc->vc_G1_charset = USER_MAP;
2385 if (vc->vc_charset == 1)
2386 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2387 vc->vc_state = ESnormal;
2388 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002389 case ESosc:
2390 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 default:
2392 vc->vc_state = ESnormal;
2393 }
2394}
2395
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002396/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002397 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002398 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2399 */
2400struct interval {
2401 uint32_t first;
2402 uint32_t last;
2403};
2404
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002405static int ucs_cmp(const void *key, const void *elt)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002406{
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002407 uint32_t ucs = *(uint32_t *)key;
2408 struct interval e = *(struct interval *) elt;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002409
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002410 if (ucs > e.last)
2411 return 1;
2412 else if (ucs < e.first)
2413 return -1;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002414 return 0;
2415}
2416
2417static int is_double_width(uint32_t ucs)
2418{
2419 static const struct interval double_width[] = {
2420 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2421 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002422 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2423 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002424 };
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002425 if (ucs < double_width[0].first ||
2426 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2427 return 0;
2428
2429 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2430 sizeof(struct interval), ucs_cmp) != NULL;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002431}
2432
Jiri Slabyd711ea82016-06-23 13:34:30 +02002433static void con_flush(struct vc_data *vc, unsigned long draw_from,
2434 unsigned long draw_to, int *draw_x)
2435{
2436 if (*draw_x < 0)
2437 return;
2438
2439 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2440 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2441 *draw_x = -1;
2442}
2443
Torben Hohnac751ef2011-01-25 15:07:35 -08002444/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2446{
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002447 int c, next_c, tc, ok, n = 0, draw_x = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 unsigned int currcons;
2449 unsigned long draw_from = 0, draw_to = 0;
2450 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002451 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002452 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002453 uint8_t rescan;
2454 uint8_t inverse;
2455 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
2458 if (in_interrupt())
2459 return count;
2460
2461 might_sleep();
2462
Torben Hohnac751ef2011-01-25 15:07:35 -08002463 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 vc = tty->driver_data;
2465 if (vc == NULL) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07002466 pr_err("vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002467 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 return 0;
2469 }
2470
2471 currcons = vc->vc_num;
2472 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002473 /* could this happen? */
2474 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2475 console_unlock();
2476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 himask = vc->vc_hi_font_mask;
2480 charmask = himask ? 0x1ff : 0xff;
2481
2482 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002483 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 hide_cursor(vc);
2485
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002486 param.vc = vc;
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 while (!tty->stopped && count) {
2489 int orig = *buf;
2490 c = orig;
2491 buf++;
2492 n++;
2493 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002494 rescan = 0;
2495 inverse = 0;
2496 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* Do no translation at all in control states */
2499 if (vc->vc_state != ESnormal) {
2500 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002501 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002502 /* Combine UTF-8 into Unicode in vc_utf_char.
2503 * vc_utf_count is the number of continuation bytes still
2504 * expected to arrive.
2505 * vc_npar is the number of continuation bytes arrived so
2506 * far
2507 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002508rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002509 if ((c & 0xc0) == 0x80) {
2510 /* Continuation byte received */
2511 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002512 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002513 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2514 vc->vc_npar++;
2515 if (--vc->vc_utf_count) {
2516 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002518 }
2519 /* Got a whole character */
2520 c = vc->vc_utf_char;
2521 /* Reject overlong sequences */
2522 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2523 c > utf8_length_changes[vc->vc_npar])
2524 c = 0xfffd;
2525 } else {
2526 /* Unexpected continuation byte */
2527 vc->vc_utf_count = 0;
2528 c = 0xfffd;
2529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002531 /* Single ASCII byte or first byte of a sequence received */
2532 if (vc->vc_utf_count) {
2533 /* Continuation byte expected */
2534 rescan = 1;
2535 vc->vc_utf_count = 0;
2536 c = 0xfffd;
2537 } else if (c > 0x7f) {
2538 /* First byte of a multibyte sequence received */
2539 vc->vc_npar = 0;
2540 if ((c & 0xe0) == 0xc0) {
2541 vc->vc_utf_count = 1;
2542 vc->vc_utf_char = (c & 0x1f);
2543 } else if ((c & 0xf0) == 0xe0) {
2544 vc->vc_utf_count = 2;
2545 vc->vc_utf_char = (c & 0x0f);
2546 } else if ((c & 0xf8) == 0xf0) {
2547 vc->vc_utf_count = 3;
2548 vc->vc_utf_char = (c & 0x07);
2549 } else if ((c & 0xfc) == 0xf8) {
2550 vc->vc_utf_count = 4;
2551 vc->vc_utf_char = (c & 0x03);
2552 } else if ((c & 0xfe) == 0xfc) {
2553 vc->vc_utf_count = 5;
2554 vc->vc_utf_char = (c & 0x01);
2555 } else {
2556 /* 254 and 255 are invalid */
2557 c = 0xfffd;
2558 }
2559 if (vc->vc_utf_count) {
2560 /* Still need some bytes */
2561 continue;
2562 }
2563 }
2564 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002566 /* End of UTF-8 decoding. */
2567 /* c is the received character, or U+FFFD for invalid sequences. */
2568 /* Replace invalid Unicode code points with U+FFFD too */
2569 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2570 c = 0xfffd;
2571 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002572 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002573 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 }
2575
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002576 param.c = tc;
2577 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2578 &param) == NOTIFY_STOP)
2579 continue;
2580
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 /* If the original code was a control character we
2582 * only allow a glyph to be displayed if the code is
2583 * not normally used (such as for cursor movement) or
2584 * if the disp_ctrl mode has been explicitly enabled.
2585 * Certain characters (as given by the CTRL_ALWAYS
2586 * bitmap) are always displayed as control characters,
2587 * as the console would be pretty useless without
2588 * them; to display an arbitrary font position use the
2589 * direct-to-font zone in UTF-8 mode.
2590 */
2591 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002592 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2593 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 && (c != 127 || vc->vc_disp_ctrl)
2595 && (c != 128+27);
2596
2597 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002598 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2599 if (is_double_width(c))
2600 width = 2;
2601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 /* Now try to find out how to display it */
2603 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002604 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002605 if (tc == -1 || tc == -2) {
2606 continue; /* nothing to display */
2607 }
2608 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002609 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002610 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002611 This would make absolutely no sense with Unicode in mind,
2612 but do this for ASCII characters since a font may lack
2613 Unicode mapping info and we don't want to end up with
2614 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002615 tc = c;
2616 } else {
2617 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2618 tc = conv_uni_to_pc(vc, 0xfffd);
2619 if (tc < 0) {
2620 inverse = 1;
2621 tc = conv_uni_to_pc(vc, '?');
2622 if (tc < 0) tc = '?';
2623 }
2624 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002627 if (!inverse) {
2628 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002630 /* invert vc_attr */
2631 if (!vc->vc_can_do_color) {
2632 vc_attr = (vc->vc_attr) ^ 0x08;
2633 } else if (vc->vc_hi_font_mask == 0x100) {
2634 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2635 } else {
2636 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002637 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002638 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002639 }
2640
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002641 next_c = c;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002642 while (1) {
2643 if (vc->vc_need_wrap || vc->vc_decim)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002644 con_flush(vc, draw_from, draw_to,
2645 &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002646 if (vc->vc_need_wrap) {
2647 cr(vc);
2648 lf(vc);
2649 }
2650 if (vc->vc_decim)
2651 insert_char(vc, 1);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002652 vc_uniscr_putc(vc, next_c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002653 scr_writew(himask ?
2654 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2655 (vc_attr << 8) + tc,
2656 (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002657 if (con_should_update(vc) && draw_x < 0) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002658 draw_x = vc->vc_x;
2659 draw_from = vc->vc_pos;
2660 }
2661 if (vc->vc_x == vc->vc_cols - 1) {
2662 vc->vc_need_wrap = vc->vc_decawm;
2663 draw_to = vc->vc_pos + 2;
2664 } else {
2665 vc->vc_x++;
2666 draw_to = (vc->vc_pos += 2);
2667 }
2668
2669 if (!--width) break;
2670
2671 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2672 if (tc < 0) tc = ' ';
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002673 next_c = ' ';
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002674 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002675 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002676
Jiri Slabyd711ea82016-06-23 13:34:30 +02002677 if (inverse)
2678 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002679
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002680 if (rescan) {
2681 rescan = 0;
2682 inverse = 0;
2683 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002684 c = orig;
2685 goto rescan_last_byte;
2686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 continue;
2688 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002689 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 do_con_trol(tty, vc, orig);
2691 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002692 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002694 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002695 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697}
2698
2699/*
2700 * This is the console switching callback.
2701 *
2702 * Doing console switching in a process context allows
2703 * us to do the switches asynchronously (needed when we want
2704 * to switch due to a keyboard interrupt). Synchronization
2705 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002706 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 */
David Howells65f27f32006-11-22 14:55:48 +00002708static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709{
Torben Hohnac751ef2011-01-25 15:07:35 -08002710 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
2712 if (want_console >= 0) {
2713 if (want_console != fg_console &&
2714 vc_cons_allocated(want_console)) {
2715 hide_cursor(vc_cons[fg_console].d);
2716 change_console(vc_cons[want_console].d);
2717 /* we only changed when the console had already
2718 been allocated - a new console is not created
2719 in an interrupt routine */
2720 }
2721 want_console = -1;
2722 }
2723 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2724 do_poke_blanked_console = 0;
2725 poke_blanked_console();
2726 }
2727 if (scrollback_delta) {
2728 struct vc_data *vc = vc_cons[fg_console].d;
2729 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002730 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2732 scrollback_delta = 0;
2733 }
2734 if (blank_timer_expired) {
2735 do_blank_screen(0);
2736 blank_timer_expired = 0;
2737 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002738 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Torben Hohnac751ef2011-01-25 15:07:35 -08002740 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741}
2742
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002743int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002745 struct vc_data *vc = vc_cons[fg_console].d;
2746
2747 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2748 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2749
2750 /*
2751 * Console switch will fail in console_callback() or
2752 * change_console() so there is no point scheduling
2753 * the callback
2754 *
2755 * Existing set_console() users don't check the return
2756 * value so this shouldn't break anything
2757 */
2758 return -EINVAL;
2759 }
2760
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 want_console = nr;
2762 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002763
2764 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
2767struct tty_driver *console_driver;
2768
2769#ifdef CONFIG_VT_CONSOLE
2770
Bernhard Walle5ada9182009-12-14 18:00:43 -08002771/**
2772 * vt_kmsg_redirect() - Sets/gets the kernel message console
2773 * @new: The new virtual terminal number or -1 if the console should stay
2774 * unchanged
2775 *
2776 * By default, the kernel messages are always printed on the current virtual
2777 * console. However, the user may modify that default with the
2778 * TIOCL_SETKMSGREDIRECT ioctl call.
2779 *
2780 * This function sets the kernel message console to be @new. It returns the old
2781 * virtual console number. The virtual terminal number 0 (both as parameter and
2782 * return value) means no redirection (i.e. always printed on the currently
2783 * active console).
2784 *
2785 * The parameter -1 means that only the current console is returned, but the
2786 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2787 * case to make the code more understandable.
2788 *
2789 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2790 * the parameter and always returns 0.
2791 */
2792int vt_kmsg_redirect(int new)
2793{
2794 static int kmsg_con;
2795
2796 if (new != -1)
2797 return xchg(&kmsg_con, new);
2798 else
2799 return kmsg_con;
2800}
2801
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802/*
2803 * Console on virtual terminal
2804 *
2805 * The console must be locked when we get here.
2806 */
2807
2808static void vt_console_print(struct console *co, const char *b, unsigned count)
2809{
2810 struct vc_data *vc = vc_cons[fg_console].d;
2811 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002812 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 const ushort *start;
2814 ushort cnt = 0;
2815 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002816 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002819 if (!printable)
2820 return;
2821 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 return;
2823
Bernhard Walle5ada9182009-12-14 18:00:43 -08002824 kmsg_console = vt_get_kmsg_redirect();
2825 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2826 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828 /* read `x' only after setting currcons properly (otherwise
2829 the `x' macro will read the x of the foreground console). */
2830 myx = vc->vc_x;
2831
2832 if (!vc_cons_allocated(fg_console)) {
2833 /* impossible */
2834 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2835 goto quit;
2836 }
2837
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002838 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 goto quit;
2840
2841 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002842 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 hide_cursor(vc);
2844
2845 start = (ushort *)vc->vc_pos;
2846
2847 /* Contrived structure to try to emulate original need_wrap behaviour
2848 * Problems caused when we have need_wrap set on '\n' character */
2849 while (count--) {
2850 c = *b++;
2851 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2852 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002853 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2855 vc->vc_x += cnt;
2856 if (vc->vc_need_wrap)
2857 vc->vc_x--;
2858 cnt = 0;
2859 }
2860 if (c == 8) { /* backspace */
2861 bs(vc);
2862 start = (ushort *)vc->vc_pos;
2863 myx = vc->vc_x;
2864 continue;
2865 }
2866 if (c != 13)
2867 lf(vc);
2868 cr(vc);
2869 start = (ushort *)vc->vc_pos;
2870 myx = vc->vc_x;
2871 if (c == 10 || c == 13)
2872 continue;
2873 }
2874 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002875 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 cnt++;
2877 if (myx == vc->vc_cols - 1) {
2878 vc->vc_need_wrap = 1;
2879 continue;
2880 }
2881 vc->vc_pos += 2;
2882 myx++;
2883 }
2884 if (cnt > 0) {
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002885 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2887 vc->vc_x += cnt;
2888 if (vc->vc_x == vc->vc_cols) {
2889 vc->vc_x--;
2890 vc->vc_need_wrap = 1;
2891 }
2892 }
2893 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002894 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002897 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898}
2899
2900static struct tty_driver *vt_console_device(struct console *c, int *index)
2901{
2902 *index = c->index ? c->index-1 : fg_console;
2903 return console_driver;
2904}
2905
2906static struct console vt_console_driver = {
2907 .name = "tty",
2908 .write = vt_console_print,
2909 .device = vt_console_device,
2910 .unblank = unblank_screen,
2911 .flags = CON_PRINTBUFFER,
2912 .index = -1,
2913};
2914#endif
2915
2916/*
2917 * Handling of Linux-specific VC ioctls
2918 */
2919
2920/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002921 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 *
2923 * There are some functions which don't need it.
2924 *
2925 * There are some functions which can sleep for arbitrary periods
2926 * (paste_selection) but we don't need the lock there anyway.
2927 *
2928 * set_selection has locking, and definitely needs it
2929 */
2930
2931int tioclinux(struct tty_struct *tty, unsigned long arg)
2932{
2933 char type, data;
2934 char __user *p = (char __user *)arg;
2935 int lines;
2936 int ret;
2937
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2939 return -EPERM;
2940 if (get_user(type, p))
2941 return -EFAULT;
2942 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 switch (type)
2945 {
2946 case TIOCL_SETSEL:
Torben Hohnac751ef2011-01-25 15:07:35 -08002947 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Torben Hohnac751ef2011-01-25 15:07:35 -08002949 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 break;
2951 case TIOCL_PASTESEL:
2952 ret = paste_selection(tty);
2953 break;
2954 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002955 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002957 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 break;
2959 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00002960 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00002962 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 break;
2964 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002965
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /*
2967 * Make it possible to react to Shift+Mousebutton.
2968 * Note that 'shift_state' is an undocumented
2969 * kernel-internal variable; programs not closely
2970 * related to the kernel should not use this.
2971 */
Alan Cox079c9532012-02-28 14:49:23 +00002972 data = vt_get_shift_state();
Adam Borowski6987dc82017-06-03 09:35:06 +02002973 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 break;
2975 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00002976 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00002978 console_unlock();
Adam Borowski6987dc82017-06-03 09:35:06 +02002979 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 break;
2981 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00002982 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002983 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00002984 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002986 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002987 data = vt_get_kmsg_redirect();
Adam Borowski6987dc82017-06-03 09:35:06 +02002988 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002989 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 case TIOCL_SETKMSGREDIRECT:
2991 if (!capable(CAP_SYS_ADMIN)) {
2992 ret = -EPERM;
2993 } else {
2994 if (get_user(data, p+1))
2995 ret = -EFAULT;
2996 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002997 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 }
2999 break;
3000 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00003001 /* No locking needed as this is a transiently
3002 correct return anyway if the caller hasn't
3003 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 ret = fg_console;
3005 break;
3006 case TIOCL_SCROLLCONSOLE:
3007 if (get_user(lines, (s32 __user *)(p+4))) {
3008 ret = -EFAULT;
3009 } else {
Alan Cox20f62572012-03-02 14:59:37 +00003010 /* Need the console lock here. Note that lots
3011 of other calls need fixing before the lock
3012 is actually useful ! */
3013 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00003015 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 ret = 0;
3017 }
3018 break;
3019 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08003020 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 ignore_poke = 1;
3022 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003023 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 break;
3025 case TIOCL_BLANKEDSCREEN:
3026 ret = console_blanked;
3027 break;
3028 default:
3029 ret = -EINVAL;
3030 break;
3031 }
3032 return ret;
3033}
3034
3035/*
3036 * /dev/ttyN handling
3037 */
3038
3039static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3040{
3041 int retval;
3042
3043 retval = do_con_write(tty, buf, count);
3044 con_flush_chars(tty);
3045
3046 return retval;
3047}
3048
Alan Cox5d19f542008-04-30 00:54:08 -07003049static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
3051 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07003052 return 0; /* n_r3964 calls put_char() from interrupt context */
3053 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054}
3055
3056static int con_write_room(struct tty_struct *tty)
3057{
3058 if (tty->stopped)
3059 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00003060 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061}
3062
3063static int con_chars_in_buffer(struct tty_struct *tty)
3064{
3065 return 0; /* we're not buffering */
3066}
3067
3068/*
3069 * con_throttle and con_unthrottle are only used for
3070 * paste_selection(), which has to stuff in a large number of
3071 * characters...
3072 */
3073static void con_throttle(struct tty_struct *tty)
3074{
3075}
3076
3077static void con_unthrottle(struct tty_struct *tty)
3078{
3079 struct vc_data *vc = tty->driver_data;
3080
3081 wake_up_interruptible(&vc->paste_wait);
3082}
3083
3084/*
3085 * Turn the Scroll-Lock LED on when the tty is stopped
3086 */
3087static void con_stop(struct tty_struct *tty)
3088{
3089 int console_num;
3090 if (!tty)
3091 return;
3092 console_num = tty->index;
3093 if (!vc_cons_allocated(console_num))
3094 return;
Alan Cox079c9532012-02-28 14:49:23 +00003095 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096}
3097
3098/*
3099 * Turn the Scroll-Lock LED off when the console is started
3100 */
3101static void con_start(struct tty_struct *tty)
3102{
3103 int console_num;
3104 if (!tty)
3105 return;
3106 console_num = tty->index;
3107 if (!vc_cons_allocated(console_num))
3108 return;
Alan Cox079c9532012-02-28 14:49:23 +00003109 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
3112static void con_flush_chars(struct tty_struct *tty)
3113{
3114 struct vc_data *vc;
3115
3116 if (in_interrupt()) /* from flush_to_ldisc */
3117 return;
3118
3119 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08003120 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 vc = tty->driver_data;
3122 if (vc)
3123 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08003124 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125}
3126
3127/*
3128 * Allocate the console screen memory.
3129 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003130static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131{
3132 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003133 struct vc_data *vc;
3134 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
Torben Hohnac751ef2011-01-25 15:07:35 -08003136 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003137 ret = vc_allocate(currcons);
3138 if (ret)
3139 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01003140
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003141 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003143 /* Still being freed */
3144 if (vc->port.tty) {
3145 ret = -ERESTARTSYS;
3146 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003148
3149 ret = tty_port_install(&vc->port, driver, tty);
3150 if (ret)
3151 goto unlock;
3152
3153 tty->driver_data = vc;
3154 vc->port.tty = tty;
3155
3156 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3157 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3158 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3159 }
3160 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01003161 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003162 else
Alan Coxadc8d742012-07-14 15:31:47 +01003163 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003164unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08003165 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 return ret;
3167}
3168
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003169static int con_open(struct tty_struct *tty, struct file *filp)
3170{
3171 /* everything done in install */
3172 return 0;
3173}
3174
3175
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176static void con_close(struct tty_struct *tty, struct file *filp)
3177{
Alan Coxfeebed62008-10-13 10:41:30 +01003178 /* Nothing to do - we defer to shutdown */
3179}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
Alan Coxfeebed62008-10-13 10:41:30 +01003181static void con_shutdown(struct tty_struct *tty)
3182{
3183 struct vc_data *vc = tty->driver_data;
3184 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08003185 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02003186 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08003187 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188}
3189
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003190static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003191static int default_italic_color = 2; // green (ASCII)
3192static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003193module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003194module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3195module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3196
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197static void vc_init(struct vc_data *vc, unsigned int rows,
3198 unsigned int cols, int do_clear)
3199{
3200 int j, k ;
3201
3202 vc->vc_cols = cols;
3203 vc->vc_rows = rows;
3204 vc->vc_size_row = cols << 1;
3205 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3206
3207 set_origin(vc);
3208 vc->vc_pos = vc->vc_origin;
3209 reset_vc(vc);
3210 for (j=k=0; j<16; j++) {
3211 vc->vc_palette[k++] = default_red[j] ;
3212 vc->vc_palette[k++] = default_grn[j] ;
3213 vc->vc_palette[k++] = default_blu[j] ;
3214 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003215 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003216 vc->vc_ulcolor = default_underline_color;
3217 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 vc->vc_halfcolor = 0x08; /* grey */
3219 init_waitqueue_head(&vc->paste_wait);
3220 reset_terminal(vc, do_clear);
3221}
3222
3223/*
3224 * This routine initializes console interrupts, and does nothing
3225 * else. If you want the screen to clear, call tty_write with
3226 * the appropriate escape-sequence.
3227 */
3228
3229static int __init con_init(void)
3230{
3231 const char *display_desc = NULL;
3232 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003233 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234
Torben Hohnac751ef2011-01-25 15:07:35 -08003235 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236
3237 if (conswitchp)
3238 display_desc = conswitchp->con_startup();
3239 if (!display_desc) {
3240 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08003241 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 return 0;
3243 }
3244
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003245 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3246 struct con_driver *con_driver = &registered_con_driver[i];
3247
3248 if (con_driver->con == NULL) {
3249 con_driver->con = conswitchp;
3250 con_driver->desc = display_desc;
3251 con_driver->flag = CON_DRIVER_FLAG_INIT;
3252 con_driver->first = 0;
3253 con_driver->last = MAX_NR_CONSOLES - 1;
3254 break;
3255 }
3256 }
3257
3258 for (i = 0; i < MAX_NR_CONSOLES; i++)
3259 con_driver_map[i] = conswitchp;
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 if (blankinterval) {
3262 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003263 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 }
3265
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003267 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003268 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003269 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003271 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 vc_init(vc, vc->vc_rows, vc->vc_cols,
3273 currcons || !vc->vc_sw->con_save_screen);
3274 }
3275 currcons = fg_console = 0;
3276 master_display_fg = vc = vc_cons[currcons].d;
3277 set_origin(vc);
3278 save_screen(vc);
3279 gotoxy(vc, vc->vc_x, vc->vc_y);
3280 csi_J(vc, 0);
3281 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003282 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 vc->vc_can_do_color ? "colour" : "mono",
3284 display_desc, vc->vc_cols, vc->vc_rows);
3285 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Torben Hohnac751ef2011-01-25 15:07:35 -08003287 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289#ifdef CONFIG_VT_CONSOLE
3290 register_console(&vt_console_driver);
3291#endif
3292 return 0;
3293}
3294console_initcall(con_init);
3295
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003296static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003297 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 .open = con_open,
3299 .close = con_close,
3300 .write = con_write,
3301 .write_room = con_write_room,
3302 .put_char = con_put_char,
3303 .flush_chars = con_flush_chars,
3304 .chars_in_buffer = con_chars_in_buffer,
3305 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003306#ifdef CONFIG_COMPAT
3307 .compat_ioctl = vt_compat_ioctl,
3308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 .stop = con_stop,
3310 .start = con_start,
3311 .throttle = con_throttle,
3312 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003313 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01003314 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315};
3316
Alan Coxd81ed102008-10-13 10:41:42 +01003317static struct cdev vc0_cdev;
3318
Kay Sieversfbc92a32010-12-01 18:51:05 +01003319static ssize_t show_tty_active(struct device *dev,
3320 struct device_attribute *attr, char *buf)
3321{
3322 return sprintf(buf, "tty%d\n", fg_console + 1);
3323}
3324static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3325
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003326static struct attribute *vt_dev_attrs[] = {
3327 &dev_attr_active.attr,
3328 NULL
3329};
3330
3331ATTRIBUTE_GROUPS(vt_dev);
3332
Alan Coxd81ed102008-10-13 10:41:42 +01003333int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Alan Coxd81ed102008-10-13 10:41:42 +01003335 cdev_init(&vc0_cdev, console_fops);
3336 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3337 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3338 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003339 tty0dev = device_create_with_groups(tty_class, NULL,
3340 MKDEV(TTY_MAJOR, 0), NULL,
3341 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003342 if (IS_ERR(tty0dev))
3343 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003344
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345 vcs_init();
3346
3347 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3348 if (!console_driver)
3349 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003350
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 console_driver->name = "tty";
3352 console_driver->name_base = 1;
3353 console_driver->major = TTY_MAJOR;
3354 console_driver->minor_start = 1;
3355 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3356 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003357 if (default_utf8)
3358 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3360 tty_set_operations(console_driver, &con_ops);
3361 if (tty_register_driver(console_driver))
3362 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 kbd_init();
3364 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365#ifdef CONFIG_MDA_CONSOLE
3366 mda_console_init();
3367#endif
3368 return 0;
3369}
3370
3371#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003372
3373static struct class *vtconsole_class;
3374
Alan Cox50e244c2013-01-25 10:28:15 +10003375static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003376 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003378 struct module *owner = csw->owner;
3379 const char *desc = NULL;
3380 struct con_driver *con_driver;
3381 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 if (!try_module_get(owner))
3384 return -ENODEV;
3385
Alan Cox50e244c2013-01-25 10:28:15 +10003386 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003387
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003388 /* check if driver is registered */
3389 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3390 con_driver = &registered_con_driver[i];
3391
3392 if (con_driver->con == csw) {
3393 desc = con_driver->desc;
3394 retval = 0;
3395 break;
3396 }
3397 }
3398
3399 if (retval)
3400 goto err;
3401
3402 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3403 csw->con_startup();
3404 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 if (deflt) {
3408 if (conswitchp)
3409 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003410
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 __module_get(owner);
3412 conswitchp = csw;
3413 }
3414
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003415 first = max(first, con_driver->first);
3416 last = min(last, con_driver->last);
3417
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 for (i = first; i <= last; i++) {
3419 int old_was_color;
3420 struct vc_data *vc = vc_cons[i].d;
3421
3422 if (con_driver_map[i])
3423 module_put(con_driver_map[i]->owner);
3424 __module_get(owner);
3425 con_driver_map[i] = csw;
3426
3427 if (!vc || !vc->vc_sw)
3428 continue;
3429
3430 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003431
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003432 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003433 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003435 }
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 old_was_color = vc->vc_can_do_color;
3438 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003439 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003441 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 update_attr(vc);
3443
3444 /* If the console changed between mono <-> color, then
3445 * the attributes in the screenbuf will be wrong. The
3446 * following resets all attributes to something sane.
3447 */
3448 if (old_was_color != vc->vc_can_do_color)
3449 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003451
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003452 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 if (!deflt)
Joe Perchesbccf1da2017-10-02 08:48:55 -07003454 pr_cont("consoles %d-%d ", first + 1, last + 1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003455 if (j >= 0) {
3456 struct vc_data *vc = vc_cons[j].d;
3457
Joe Perchesbccf1da2017-10-02 08:48:55 -07003458 pr_cont("to %s %s %dx%d\n",
3459 vc->vc_can_do_color ? "colour" : "mono",
3460 desc, vc->vc_cols, vc->vc_rows);
David Hollister4ee1acc2006-06-26 00:26:41 -07003461
3462 if (k >= 0) {
3463 vc = vc_cons[k].d;
3464 update_screen(vc);
3465 }
Joe Perchesbccf1da2017-10-02 08:48:55 -07003466 } else {
3467 pr_cont("to %s\n", desc);
3468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003470 retval = 0;
3471err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003473 return retval;
3474};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475
Alan Cox50e244c2013-01-25 10:28:15 +10003476
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003477#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003478/* unlocked version of unbind_con_driver() */
3479int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3480{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003481 struct module *owner = csw->owner;
3482 const struct consw *defcsw = NULL;
3483 struct con_driver *con_driver = NULL, *con_back = NULL;
3484 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003486 if (!try_module_get(owner))
3487 return -ENODEV;
3488
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003489 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003490
3491 /* check if driver is registered and if it is unbindable */
3492 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3493 con_driver = &registered_con_driver[i];
3494
3495 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003496 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003497 retval = 0;
3498 break;
3499 }
3500 }
3501
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003502 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003503 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003504
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003505 retval = -ENODEV;
3506
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003507 /* check if backup driver exists */
3508 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3509 con_back = &registered_con_driver[i];
3510
Daniel Vetter249f7b32014-06-05 16:24:47 +02003511 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003512 defcsw = con_back->con;
3513 retval = 0;
3514 break;
3515 }
3516 }
3517
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003518 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003519 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003520
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003521 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003522 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003523
3524 first = max(first, con_driver->first);
3525 last = min(last, con_driver->last);
3526
3527 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 if (con_driver_map[i] == csw) {
3529 module_put(csw->owner);
3530 con_driver_map[i] = NULL;
3531 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003532 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003533
3534 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003535 const struct consw *defconsw = conswitchp;
3536
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003537 defcsw->con_startup();
3538 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003539 /*
3540 * vgacon may change the default driver to point
3541 * to dummycon, we restore it here...
3542 */
3543 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003544 }
3545
3546 if (!con_is_bound(csw))
3547 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3548
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003549 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003550 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003551err:
3552 module_put(owner);
3553 return retval;
3554
3555}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003556EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003557
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003558static int vt_bind(struct con_driver *con)
3559{
3560 const struct consw *defcsw = NULL, *csw = NULL;
3561 int i, more = 1, first = -1, last = -1, deflt = 0;
3562
Daniel Vetter77232f72015-04-13 11:16:21 +02003563 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003564 goto err;
3565
3566 csw = con->con;
3567
3568 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3569 struct con_driver *con = &registered_con_driver[i];
3570
3571 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3572 defcsw = con->con;
3573 break;
3574 }
3575 }
3576
3577 if (!defcsw)
3578 goto err;
3579
3580 while (more) {
3581 more = 0;
3582
3583 for (i = con->first; i <= con->last; i++) {
3584 if (con_driver_map[i] == defcsw) {
3585 if (first == -1)
3586 first = i;
3587 last = i;
3588 more = 1;
3589 } else if (first != -1)
3590 break;
3591 }
3592
3593 if (first == 0 && last == MAX_NR_CONSOLES -1)
3594 deflt = 1;
3595
Imre Deak4c215fe2014-12-16 00:16:00 +02003596 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003597 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003598
3599 first = -1;
3600 last = -1;
3601 deflt = 0;
3602 }
3603
3604err:
3605 return 0;
3606}
3607
3608static int vt_unbind(struct con_driver *con)
3609{
3610 const struct consw *csw = NULL;
3611 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003612 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003613
Daniel Vetter77232f72015-04-13 11:16:21 +02003614 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003615 goto err;
3616
3617 csw = con->con;
3618
3619 while (more) {
3620 more = 0;
3621
3622 for (i = con->first; i <= con->last; i++) {
3623 if (con_driver_map[i] == csw) {
3624 if (first == -1)
3625 first = i;
3626 last = i;
3627 more = 1;
3628 } else if (first != -1)
3629 break;
3630 }
3631
3632 if (first == 0 && last == MAX_NR_CONSOLES -1)
3633 deflt = 1;
3634
Wang YanQing618f2b92013-05-09 02:14:07 +08003635 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003636 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003637 if (ret != 0)
3638 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003639 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003640
3641 first = -1;
3642 last = -1;
3643 deflt = 0;
3644 }
3645
3646err:
3647 return 0;
3648}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003649#else
3650static inline int vt_bind(struct con_driver *con)
3651{
3652 return 0;
3653}
3654static inline int vt_unbind(struct con_driver *con)
3655{
3656 return 0;
3657}
3658#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003659
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003660static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003661 const char *buf, size_t count)
3662{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003663 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003664 int bind = simple_strtoul(buf, NULL, 0);
3665
Imre Deak4c215fe2014-12-16 00:16:00 +02003666 console_lock();
3667
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003668 if (bind)
3669 vt_bind(con);
3670 else
3671 vt_unbind(con);
3672
Imre Deak4c215fe2014-12-16 00:16:00 +02003673 console_unlock();
3674
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003675 return count;
3676}
3677
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003678static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3679 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003680{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003681 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003682 int bind = con_is_bound(con->con);
3683
3684 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3685}
3686
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003687static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3688 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003689{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003690 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003691
3692 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3693 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3694 con->desc);
3695
3696}
3697
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003698static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3699static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3700
3701static struct attribute *con_dev_attrs[] = {
3702 &dev_attr_bind.attr,
3703 &dev_attr_name.attr,
3704 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003705};
3706
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003707ATTRIBUTE_GROUPS(con_dev);
3708
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003709static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003710{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003711 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003712 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003713}
3714
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003715static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003716{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003717 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003718}
3719
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003720/**
3721 * con_is_bound - checks if driver is bound to the console
3722 * @csw: console driver
3723 *
3724 * RETURNS: zero if unbound, nonzero if bound
3725 *
3726 * Drivers can call this and if zero, they should release
3727 * all resources allocated on con_startup()
3728 */
3729int con_is_bound(const struct consw *csw)
3730{
3731 int i, bound = 0;
3732
3733 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3734 if (con_driver_map[i] == csw) {
3735 bound = 1;
3736 break;
3737 }
3738 }
3739
3740 return bound;
3741}
3742EXPORT_SYMBOL(con_is_bound);
3743
3744/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003745 * con_debug_enter - prepare the console for the kernel debugger
3746 * @sw: console driver
3747 *
3748 * Called when the console is taken over by the kernel debugger, this
3749 * function needs to save the current console state, then put the console
3750 * into a state suitable for the kernel debugger.
3751 *
3752 * RETURNS:
3753 * Zero on success, nonzero if a failure occurred when trying to prepare
3754 * the console for the debugger.
3755 */
3756int con_debug_enter(struct vc_data *vc)
3757{
3758 int ret = 0;
3759
3760 saved_fg_console = fg_console;
3761 saved_last_console = last_console;
3762 saved_want_console = want_console;
3763 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003764 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003765 vc->vc_mode = KD_TEXT;
3766 console_blanked = 0;
3767 if (vc->vc_sw->con_debug_enter)
3768 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003769#ifdef CONFIG_KGDB_KDB
3770 /* Set the initial LINES variable if it is not already set */
3771 if (vc->vc_rows < 999) {
3772 int linecount;
3773 char lns[4];
3774 const char *setargs[3] = {
3775 "set",
3776 "LINES",
3777 lns,
3778 };
3779 if (kdbgetintenv(setargs[0], &linecount)) {
3780 snprintf(lns, 4, "%i", vc->vc_rows);
3781 kdb_set(2, setargs);
3782 }
3783 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003784 if (vc->vc_cols < 999) {
3785 int colcount;
3786 char cols[4];
3787 const char *setargs[3] = {
3788 "set",
3789 "COLUMNS",
3790 cols,
3791 };
3792 if (kdbgetintenv(setargs[0], &colcount)) {
3793 snprintf(cols, 4, "%i", vc->vc_cols);
3794 kdb_set(2, setargs);
3795 }
3796 }
Jason Wessel81d44502010-08-05 09:22:30 -05003797#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003798 return ret;
3799}
3800EXPORT_SYMBOL_GPL(con_debug_enter);
3801
3802/**
3803 * con_debug_leave - restore console state
3804 * @sw: console driver
3805 *
3806 * Restore the console state to what it was before the kernel debugger
3807 * was invoked.
3808 *
3809 * RETURNS:
3810 * Zero on success, nonzero if a failure occurred when trying to restore
3811 * the console.
3812 */
3813int con_debug_leave(void)
3814{
3815 struct vc_data *vc;
3816 int ret = 0;
3817
3818 fg_console = saved_fg_console;
3819 last_console = saved_last_console;
3820 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003821 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003822 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3823
3824 vc = vc_cons[fg_console].d;
3825 if (vc->vc_sw->con_debug_leave)
3826 ret = vc->vc_sw->con_debug_leave(vc);
3827 return ret;
3828}
3829EXPORT_SYMBOL_GPL(con_debug_leave);
3830
Alan Cox50e244c2013-01-25 10:28:15 +10003831static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003832{
3833 struct module *owner = csw->owner;
3834 struct con_driver *con_driver;
3835 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02003836 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003837
Alan Cox50e244c2013-01-25 10:28:15 +10003838 WARN_CONSOLE_UNLOCKED();
3839
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003840 if (!try_module_get(owner))
3841 return -ENODEV;
3842
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003843 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3844 con_driver = &registered_con_driver[i];
3845
3846 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02003847 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10003848 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02003849 goto err;
3850 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003851 }
3852
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003853 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02003854 if (!desc) {
3855 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003856 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02003857 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003858
3859 retval = -EINVAL;
3860
3861 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3862 con_driver = &registered_con_driver[i];
3863
Imre Deakd364b5c2015-04-01 21:06:16 +03003864 if (con_driver->con == NULL &&
3865 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003866 con_driver->con = csw;
3867 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003868 con_driver->node = i;
3869 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003870 CON_DRIVER_FLAG_INIT;
3871 con_driver->first = first;
3872 con_driver->last = last;
3873 retval = 0;
3874 break;
3875 }
3876 }
3877
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003878 if (retval)
3879 goto err;
3880
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003881 con_driver->dev =
3882 device_create_with_groups(vtconsole_class, NULL,
3883 MKDEV(0, con_driver->node),
3884 con_driver, con_dev_groups,
3885 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003886 if (IS_ERR(con_driver->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07003887 pr_warn("Unable to create device for %s; errno = %ld\n",
3888 con_driver->desc, PTR_ERR(con_driver->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003889 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003890 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003891 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003892 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003893
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003894err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003895 module_put(owner);
3896 return retval;
3897}
Alan Cox50e244c2013-01-25 10:28:15 +10003898
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003899
3900/**
Wang YanQing50539dd2013-05-09 02:14:44 +08003901 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003902 * @csw: console driver
3903 *
3904 * DESCRIPTION: All drivers that registers to the console layer must
3905 * call this function upon exit, or if the console driver is in a state
3906 * where it won't be able to handle console services, such as the
3907 * framebuffer console without loaded framebuffer drivers.
3908 *
3909 * The driver must unbind first prior to unregistration.
3910 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003911int do_unregister_con_driver(const struct consw *csw)
3912{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003913 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003914
3915 /* cannot unregister a bound driver */
3916 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003917 return -EBUSY;
3918
3919 if (csw == conswitchp)
3920 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003921
3922 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3923 struct con_driver *con_driver = &registered_con_driver[i];
3924
Imre Deak2cf30f72014-12-16 00:15:59 +02003925 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03003926 /*
3927 * Defer the removal of the sysfs entries since that
3928 * will acquire the kernfs s_active lock and we can't
3929 * acquire this lock while holding the console lock:
3930 * the unbind sysfs entry imposes already the opposite
3931 * order. Reset con already here to prevent any later
3932 * lookup to succeed and mark this slot as zombie, so
3933 * it won't get reused until we complete the removal
3934 * in the deferred work.
3935 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003936 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03003937 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
3938 schedule_work(&con_driver_unregister_work);
3939
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003940 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003941 }
3942 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003943
3944 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003945}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003946EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003947
Imre Deakd364b5c2015-04-01 21:06:16 +03003948static void con_driver_unregister_callback(struct work_struct *ignored)
3949{
3950 int i;
3951
3952 console_lock();
3953
3954 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3955 struct con_driver *con_driver = &registered_con_driver[i];
3956
3957 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
3958 continue;
3959
3960 console_unlock();
3961
3962 vtconsole_deinit_device(con_driver);
3963 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
3964
3965 console_lock();
3966
3967 if (WARN_ON_ONCE(con_driver->con))
3968 con_driver->con = NULL;
3969 con_driver->desc = NULL;
3970 con_driver->dev = NULL;
3971 con_driver->node = 0;
3972 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
3973 con_driver->flag = 0;
3974 con_driver->first = 0;
3975 con_driver->last = 0;
3976 }
3977
3978 console_unlock();
3979}
3980
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003981/*
3982 * If we support more console drivers, this function is used
3983 * when a driver wants to take over some existing consoles
3984 * and become default driver for newly opened ones.
3985 *
Wang YanQing155957f2013-05-21 13:15:12 +08003986 * do_take_over_console is basically a register followed by unbind
Alan Cox50e244c2013-01-25 10:28:15 +10003987 */
3988int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3989{
3990 int err;
3991
3992 err = do_register_con_driver(csw, first, last);
3993 /*
3994 * If we get an busy error we still want to bind the console driver
3995 * and return success, as we may have unbound the console driver
3996 * but not unregistered it.
3997 */
3998 if (err == -EBUSY)
3999 err = 0;
4000 if (!err)
4001 do_bind_con_driver(csw, first, last, deflt);
4002
4003 return err;
4004}
4005EXPORT_SYMBOL_GPL(do_take_over_console);
4006
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004007
4008/*
4009 * give_up_console is a wrapper to unregister_con_driver. It will only
4010 * work if driver is fully unbound.
4011 */
4012void give_up_console(const struct consw *csw)
4013{
Wang YanQing70125e72013-05-09 02:14:39 +08004014 console_lock();
4015 do_unregister_con_driver(csw);
4016 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004017}
4018
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004019static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004020{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004021 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004022
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004023 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4024 if (IS_ERR(vtconsole_class)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004025 pr_warn("Unable to create vt console class; errno = %ld\n",
4026 PTR_ERR(vtconsole_class));
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004027 vtconsole_class = NULL;
4028 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004029
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004030 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004031 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4032 struct con_driver *con = &registered_con_driver[i];
4033
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004034 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004035 con->dev =
4036 device_create_with_groups(vtconsole_class, NULL,
4037 MKDEV(0, con->node),
4038 con, con_dev_groups,
4039 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004040
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004041 if (IS_ERR(con->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004042 pr_warn("Unable to create device for %s; errno = %ld\n",
4043 con->desc, PTR_ERR(con->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004044 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004045 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004046 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004047 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004048 }
4049 }
4050
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004051 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004052}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004053postcore_initcall(vtconsole_class_init);
4054
4055#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004056
4057/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 * Screen blanking
4059 */
4060
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004061static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004063 unsigned int mode;
4064
4065 if (get_user(mode, p + 1))
4066 return -EFAULT;
4067
4068 vesa_blank_mode = (mode < 4) ? mode : 0;
4069 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072void do_blank_screen(int entering_gfx)
4073{
4074 struct vc_data *vc = vc_cons[fg_console].d;
4075 int i;
4076
4077 WARN_CONSOLE_UNLOCKED();
4078
4079 if (console_blanked) {
4080 if (blank_state == blank_vesa_wait) {
4081 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08004082 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 }
4084 return;
4085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
4087 /* entering graphics mode? */
4088 if (entering_gfx) {
4089 hide_cursor(vc);
4090 save_screen(vc);
4091 vc->vc_sw->con_blank(vc, -1, 1);
4092 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07004093 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 set_origin(vc);
4095 return;
4096 }
4097
izumib6e8f002007-07-17 04:05:49 -07004098 if (blank_state != blank_normal_wait)
4099 return;
4100 blank_state = blank_off;
4101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 /* don't blank graphics */
4103 if (vc->vc_mode != KD_TEXT) {
4104 console_blanked = fg_console + 1;
4105 return;
4106 }
4107
4108 hide_cursor(vc);
4109 del_timer_sync(&console_timer);
4110 blank_timer_expired = 0;
4111
4112 save_screen(vc);
4113 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08004114 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 console_blanked = fg_console + 1;
4116 if (i)
4117 set_origin(vc);
4118
4119 if (console_blank_hook && console_blank_hook(1))
4120 return;
4121
Ville Syrjalad060a322006-01-09 20:53:49 -08004122 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07004123 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 mod_timer(&console_timer, jiffies + vesa_off_interval);
4125 }
Alan Cox8b92e872009-09-19 13:13:24 -07004126 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127}
4128EXPORT_SYMBOL(do_blank_screen);
4129
4130/*
4131 * Called by timer as well as from vt_console_driver
4132 */
4133void do_unblank_screen(int leaving_gfx)
4134{
4135 struct vc_data *vc;
4136
4137 /* This should now always be called from a "sane" (read: can schedule)
4138 * context for the sake of the low level drivers, except in the special
4139 * case of oops_in_progress
4140 */
4141 if (!oops_in_progress)
4142 might_sleep();
4143
4144 WARN_CONSOLE_UNLOCKED();
4145
4146 ignore_poke = 0;
4147 if (!console_blanked)
4148 return;
4149 if (!vc_cons_allocated(fg_console)) {
4150 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08004151 pr_warn("unblank_screen: tty %d not allocated ??\n",
4152 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 return;
4154 }
4155 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07004156 /* Try to unblank in oops case too */
4157 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 return; /* but leave console_blanked != 0 */
4159
4160 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004161 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 blank_state = blank_normal_wait;
4163 }
4164
4165 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07004166 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 /* Low-level driver cannot restore -> do it ourselves */
4168 update_screen(vc);
4169 if (console_blank_hook)
4170 console_blank_hook(0);
4171 set_palette(vc);
4172 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07004173 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174}
4175EXPORT_SYMBOL(do_unblank_screen);
4176
4177/*
4178 * This is called by the outside world to cause a forced unblank, mostly for
4179 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4180 * call it with 1 as an argument and so force a mode restore... that may kill
4181 * X or at least garbage the screen but would also make the Oops visible...
4182 */
4183void unblank_screen(void)
4184{
4185 do_unblank_screen(0);
4186}
4187
4188/*
Ingo Molnar70522e12006-03-23 03:00:31 -08004189 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08004191 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 */
Kees Cook24ed9602017-08-28 11:28:21 -07004193static void blank_screen_t(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 blank_timer_expired = 1;
4196 schedule_work(&console_work);
4197}
4198
4199void poke_blanked_console(void)
4200{
4201 WARN_CONSOLE_UNLOCKED();
4202
4203 /* Add this so we quickly catch whoever might call us in a non
4204 * safe context. Nowadays, unblank_screen() isn't to be called in
4205 * atomic contexts and is allowed to schedule (with the special case
4206 * of oops_in_progress, but that isn't of any concern for this
4207 * function. --BenH.
4208 */
4209 might_sleep();
4210
4211 /* This isn't perfectly race free, but a race here would be mostly harmless,
4212 * at worse, we'll do a spurrious blank and it's unlikely
4213 */
4214 del_timer(&console_timer);
4215 blank_timer_expired = 0;
4216
4217 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4218 return;
4219 if (console_blanked)
4220 unblank_screen();
4221 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004222 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 blank_state = blank_normal_wait;
4224 }
4225}
4226
4227/*
4228 * Palettes
4229 */
4230
4231static void set_palette(struct vc_data *vc)
4232{
4233 WARN_CONSOLE_UNLOCKED();
4234
Jiri Slaby709280d2016-06-23 13:34:27 +02004235 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 vc->vc_sw->con_set_palette(vc, color_table);
4237}
4238
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239/*
4240 * Load palette into the DAC registers. arg points to a colour
4241 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4242 */
4243
4244int con_set_cmap(unsigned char __user *arg)
4245{
Michael Gehring871bdea2012-03-21 01:26:45 +01004246 int i, j, k;
4247 unsigned char colormap[3*16];
4248
4249 if (copy_from_user(colormap, arg, sizeof(colormap)))
4250 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251
Torben Hohnac751ef2011-01-25 15:07:35 -08004252 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004253 for (i = k = 0; i < 16; i++) {
4254 default_red[i] = colormap[k++];
4255 default_grn[i] = colormap[k++];
4256 default_blu[i] = colormap[k++];
4257 }
4258 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4259 if (!vc_cons_allocated(i))
4260 continue;
4261 for (j = k = 0; j < 16; j++) {
4262 vc_cons[i].d->vc_palette[k++] = default_red[j];
4263 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4264 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4265 }
4266 set_palette(vc_cons[i].d);
4267 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004268 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269
Michael Gehring871bdea2012-03-21 01:26:45 +01004270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
4273int con_get_cmap(unsigned char __user *arg)
4274{
Michael Gehring871bdea2012-03-21 01:26:45 +01004275 int i, k;
4276 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Torben Hohnac751ef2011-01-25 15:07:35 -08004278 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004279 for (i = k = 0; i < 16; i++) {
4280 colormap[k++] = default_red[i];
4281 colormap[k++] = default_grn[i];
4282 colormap[k++] = default_blu[i];
4283 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004284 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Michael Gehring871bdea2012-03-21 01:26:45 +01004286 if (copy_to_user(arg, colormap, sizeof(colormap)))
4287 return -EFAULT;
4288
4289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
4292void reset_palette(struct vc_data *vc)
4293{
4294 int j, k;
4295 for (j=k=0; j<16; j++) {
4296 vc->vc_palette[k++] = default_red[j];
4297 vc->vc_palette[k++] = default_grn[j];
4298 vc->vc_palette[k++] = default_blu[j];
4299 }
4300 set_palette(vc);
4301}
4302
4303/*
4304 * Font switching
4305 *
4306 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4307 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4308 * depending on width) reserved for each character which is kinda wasty, but
4309 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004310 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * format (maybe we should add a `fontoffset' field to the `display'
4312 * structure so we won't have to convert the fontdata all the time.
4313 * /Jes
4314 */
4315
4316#define max_font_size 65536
4317
4318static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4319{
4320 struct console_font font;
4321 int rc = -EINVAL;
4322 int c;
4323
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 if (op->data) {
4325 font.data = kmalloc(max_font_size, GFP_KERNEL);
4326 if (!font.data)
4327 return -ENOMEM;
4328 } else
4329 font.data = NULL;
4330
Torben Hohnac751ef2011-01-25 15:07:35 -08004331 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004332 if (vc->vc_mode != KD_TEXT)
4333 rc = -EINVAL;
4334 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 rc = vc->vc_sw->con_font_get(vc, &font);
4336 else
4337 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004338 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 if (rc)
4341 goto out;
4342
4343 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004344
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 if (op->data && font.charcount > op->charcount)
4346 rc = -ENOSPC;
4347 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4348 if (font.width > op->width || font.height > op->height)
4349 rc = -ENOSPC;
4350 } else {
4351 if (font.width != 8)
4352 rc = -EIO;
4353 else if ((op->height && font.height > op->height) ||
4354 font.height > 32)
4355 rc = -ENOSPC;
4356 }
4357 if (rc)
4358 goto out;
4359
4360 op->height = font.height;
4361 op->width = font.width;
4362 op->charcount = font.charcount;
4363
4364 if (op->data && copy_to_user(op->data, font.data, c))
4365 rc = -EFAULT;
4366
4367out:
4368 kfree(font.data);
4369 return rc;
4370}
4371
4372static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4373{
4374 struct console_font font;
4375 int rc = -EINVAL;
4376 int size;
4377
4378 if (vc->vc_mode != KD_TEXT)
4379 return -EINVAL;
4380 if (!op->data)
4381 return -EINVAL;
4382 if (op->charcount > 512)
4383 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 if (op->width <= 0 || op->width > 32 || op->height > 32)
4385 return -EINVAL;
4386 size = (op->width+7)/8 * 32 * op->charcount;
4387 if (size > max_font_size)
4388 return -ENOSPC;
Meng Xu8ffb8202017-10-04 10:38:37 -04004389
Julia Lawall9b71ca22010-05-26 14:42:11 -07004390 font.data = memdup_user(op->data, size);
4391 if (IS_ERR(font.data))
4392 return PTR_ERR(font.data);
Meng Xu8ffb8202017-10-04 10:38:37 -04004393
4394 if (!op->height) { /* Need to guess font height [compat] */
4395 int h, i;
4396 u8 *charmap = font.data;
4397
4398 /*
4399 * If from KDFONTOP ioctl, don't allow things which can be done
4400 * in userland,so that we can get rid of this soon
4401 */
4402 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4403 kfree(font.data);
4404 return -EINVAL;
4405 }
4406
4407 for (h = 32; h > 0; h--)
4408 for (i = 0; i < op->charcount; i++)
4409 if (charmap[32*i+h-1])
4410 goto nonzero;
4411
4412 kfree(font.data);
4413 return -EINVAL;
4414
4415 nonzero:
4416 op->height = h;
4417 }
4418
4419 font.charcount = op->charcount;
4420 font.width = op->width;
4421 font.height = op->height;
4422
Torben Hohnac751ef2011-01-25 15:07:35 -08004423 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004424 if (vc->vc_mode != KD_TEXT)
4425 rc = -EINVAL;
4426 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4428 else
4429 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004430 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 kfree(font.data);
4432 return rc;
4433}
4434
4435static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4436{
4437 struct console_font font = {.width = op->width, .height = op->height};
4438 char name[MAX_FONT_NAME];
4439 char *s = name;
4440 int rc;
4441
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
4443 if (!op->data)
4444 s = NULL;
4445 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4446 return -EFAULT;
4447 else
4448 name[MAX_FONT_NAME - 1] = 0;
4449
Torben Hohnac751ef2011-01-25 15:07:35 -08004450 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004451 if (vc->vc_mode != KD_TEXT) {
4452 console_unlock();
4453 return -EINVAL;
4454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 if (vc->vc_sw->con_font_default)
4456 rc = vc->vc_sw->con_font_default(vc, &font, s);
4457 else
4458 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004459 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 if (!rc) {
4461 op->width = font.width;
4462 op->height = font.height;
4463 }
4464 return rc;
4465}
4466
4467static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4468{
4469 int con = op->height;
4470 int rc;
4471
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Torben Hohnac751ef2011-01-25 15:07:35 -08004473 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004474 if (vc->vc_mode != KD_TEXT)
4475 rc = -EINVAL;
4476 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 rc = -ENOSYS;
4478 else if (con < 0 || !vc_cons_allocated(con))
4479 rc = -ENOTTY;
4480 else if (con == vc->vc_num) /* nothing to do */
4481 rc = 0;
4482 else
4483 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004484 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return rc;
4486}
4487
4488int con_font_op(struct vc_data *vc, struct console_font_op *op)
4489{
4490 switch (op->op) {
4491 case KD_FONT_OP_SET:
4492 return con_font_set(vc, op);
4493 case KD_FONT_OP_GET:
4494 return con_font_get(vc, op);
4495 case KD_FONT_OP_SET_DEFAULT:
4496 return con_font_default(vc, op);
4497 case KD_FONT_OP_COPY:
4498 return con_font_copy(vc, op);
4499 }
4500 return -ENOSYS;
4501}
4502
4503/*
4504 * Interface exported to selection and vcs.
4505 */
4506
4507/* used by selection */
4508u16 screen_glyph(struct vc_data *vc, int offset)
4509{
4510 u16 w = scr_readw(screenpos(vc, offset, 1));
4511 u16 c = w & 0xff;
4512
4513 if (w & vc->vc_hi_font_mask)
4514 c |= 0x100;
4515 return c;
4516}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004517EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518
4519/* used by vcs - note the word offset */
4520unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4521{
4522 return screenpos(vc, 2 * w_offset, viewed);
4523}
Samuel Thibault88867e32016-01-25 01:32:08 +01004524EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525
4526void getconsxy(struct vc_data *vc, unsigned char *p)
4527{
4528 p[0] = vc->vc_x;
4529 p[1] = vc->vc_y;
4530}
4531
4532void putconsxy(struct vc_data *vc, unsigned char *p)
4533{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004534 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 gotoxy(vc, p[0], p[1]);
4536 set_cursor(vc);
4537}
4538
4539u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4540{
4541 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4542 return softcursor_original;
4543 return scr_readw(org);
4544}
4545
4546void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4547{
4548 scr_writew(val, org);
4549 if ((unsigned long)org == vc->vc_pos) {
4550 softcursor_original = -1;
4551 add_softcursor(vc);
4552 }
4553}
4554
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004555void vcs_scr_updated(struct vc_data *vc)
4556{
4557 notify_update(vc);
4558}
4559
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004560void vc_scrolldelta_helper(struct vc_data *c, int lines,
4561 unsigned int rolled_over, void *base, unsigned int size)
4562{
4563 unsigned long ubase = (unsigned long)base;
Jiri Slaby210fd742016-10-03 11:18:36 +02004564 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4565 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4566 ptrdiff_t origin = (void *)c->vc_origin - base;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004567 int margin = c->vc_size_row * 4;
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004568 int from, wrap, from_off, avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004569
4570 /* Turn scrollback off */
4571 if (!lines) {
4572 c->vc_visible_origin = c->vc_origin;
4573 return;
4574 }
4575
4576 /* Do we have already enough to allow jumping from 0 to the end? */
Jiri Slaby210fd742016-10-03 11:18:36 +02004577 if (rolled_over > scr_end + margin) {
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004578 from = scr_end;
4579 wrap = rolled_over + c->vc_size_row;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004580 } else {
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004581 from = 0;
4582 wrap = size;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004583 }
4584
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004585 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4586 avail = (origin - from + wrap) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004587
4588 /* Only a little piece would be left? Show all incl. the piece! */
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004589 if (avail < 2 * margin)
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004590 margin = 0;
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004591 if (from_off < margin)
4592 from_off = 0;
4593 if (from_off > avail - margin)
4594 from_off = avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004595
Jiri Slaby7c918cd2016-10-03 11:18:37 +02004596 c->vc_visible_origin = ubase + (from + from_off) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004597}
4598EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4599
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600/*
4601 * Visible symbols for modules
4602 */
4603
4604EXPORT_SYMBOL(color_table);
4605EXPORT_SYMBOL(default_red);
4606EXPORT_SYMBOL(default_grn);
4607EXPORT_SYMBOL(default_blu);
4608EXPORT_SYMBOL(update_region);
4609EXPORT_SYMBOL(redraw_screen);
4610EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611EXPORT_SYMBOL(fg_console);
4612EXPORT_SYMBOL(console_blank_hook);
4613EXPORT_SYMBOL(console_blanked);
4614EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004615EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617EXPORT_SYMBOL(give_up_console);
4618#endif