blob: 5004242d601bd8b28961f1d29ce87cda7570742e [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>
Nicolas Pitre9a98e7a2020-03-28 22:25:11 -040084#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070089#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/vt_kern.h>
91#include <linux/selection.h>
92#include <linux/tiocl.h>
93#include <linux/kbd_kern.h>
94#include <linux/consolemap.h>
95#include <linux/timer.h>
96#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/pm.h>
99#include <linux/font.h>
100#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700101#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100102#include <linux/device.h>
103#include <linux/io.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100104#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500105#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300106#include <linux/ctype.h>
Thomas Meyerf0a8d842017-09-16 10:03:05 +0200107#include <linux/bsearch.h>
Nicolas Pitred541ae42018-07-19 00:05:25 -0400108#include <linux/gcd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700110#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700112#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700113#define CON_DRIVER_FLAG_INIT 2
114#define CON_DRIVER_FLAG_ATTR 4
Imre Deakd364b5c2015-04-01 21:06:16 +0300115#define CON_DRIVER_FLAG_ZOMBIE 8
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700116
117struct con_driver {
118 const struct consw *con;
119 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700120 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700121 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700122 int first;
123 int last;
124 int flag;
125};
126
127static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128const struct consw *conswitchp;
129
130/* A bitmap for codes <32. A bit of 1 indicates that the code
131 * corresponding to that bit number invokes some special action
132 * (such as cursor movement) and should not be displayed as a
133 * glyph unless the disp_ctrl mode is explicitly enabled.
134 */
135#define CTRL_ACTION 0x0d00ff81
136#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
137
138/*
139 * Here is the default bell parameters: 750HZ, 1/8th of a second
140 */
141#define DEFAULT_BELL_PITCH 750
142#define DEFAULT_BELL_DURATION (HZ/8)
Scot Doylebd633642015-03-26 13:54:39 +0000143#define DEFAULT_CURSOR_BLINK_MS 200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145struct vc vc_cons [MAX_NR_CONSOLES];
146
147#ifndef VT_SINGLE_DRIVER
148static const struct consw *con_driver_map[MAX_NR_CONSOLES];
149#endif
150
151static int con_open(struct tty_struct *, struct file *);
152static void vc_init(struct vc_data *vc, unsigned int rows,
153 unsigned int cols, int do_clear);
154static void gotoxy(struct vc_data *vc, int new_x, int new_y);
155static void save_cur(struct vc_data *vc);
156static void reset_terminal(struct vc_data *vc, int do_clear);
157static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800158static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void set_cursor(struct vc_data *vc);
160static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000161static void console_callback(struct work_struct *ignored);
Imre Deakd364b5c2015-04-01 21:06:16 +0300162static void con_driver_unregister_callback(struct work_struct *ignored);
Kees Cook24ed9602017-08-28 11:28:21 -0700163static void blank_screen_t(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164static void set_palette(struct vc_data *vc);
165
Peter Hurley52c40fc2014-11-11 11:20:56 -0500166#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700169int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700170module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500171int global_cursor_default = -1;
172module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800174static int cur_default = CUR_DEFAULT;
175module_param(cur_default, int, S_IRUGO | S_IWUSR);
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
178 * ignore_poke: don't unblank the screen when things are typed. This is
179 * mainly for the privacy of braille terminal users.
180 */
181static int ignore_poke;
182
183int do_poke_blanked_console;
184int console_blanked;
185
186static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187static int vesa_off_interval;
Tim Gardnera4199f52017-03-22 09:07:20 -0600188static int blankinterval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700189core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
David Howells65f27f32006-11-22 14:55:48 +0000191static DECLARE_WORK(console_work, console_callback);
Imre Deakd364b5c2015-04-01 21:06:16 +0300192static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194/*
195 * fg_console is the current virtual console,
196 * last_console is the last used one,
197 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500198 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 */
200int fg_console;
201int last_console;
202int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500203static int saved_fg_console;
204static int saved_last_console;
205static int saved_want_console;
206static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500207static int saved_console_blanked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/*
210 * For each existing display, we have a pointer to console currently visible
211 * on that display, allowing consoles other than fg_console to be refreshed
212 * appropriately. Unless the low-level driver supplies its own display_fg
213 * variable, we use this one for the "master display".
214 */
215static struct vc_data *master_display_fg;
216
217/*
218 * Unfortunately, we need to delay tty echo when we're currently writing to the
219 * console since the code is (and always was) not re-entrant, so we schedule
220 * all flip requests to process context with schedule-task() and run it from
221 * console_callback().
222 */
223
224/*
225 * For the same reason, we defer scrollback to the console callback.
226 */
227static int scrollback_delta;
228
229/*
230 * Hook so that the power management routines can (un)blank
231 * the console on our behalf.
232 */
233int (*console_blank_hook)(int);
234
Kees Cook1d27e3e2017-10-04 16:27:04 -0700235static DEFINE_TIMER(console_timer, blank_screen_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236static int blank_state;
237static int blank_timer_expired;
238enum {
239 blank_off = 0,
240 blank_normal_wait,
241 blank_vesa_wait,
242};
243
244/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100245 * /sys/class/tty/tty0/
246 *
247 * the attribute 'active' contains the name of the current vc
248 * console and it supports poll() to detect vc switches
249 */
250static struct device *tty0dev;
251
252/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700253 * Notifier list for console events.
254 */
255static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
256
257int register_vt_notifier(struct notifier_block *nb)
258{
259 return atomic_notifier_chain_register(&vt_notifier_list, nb);
260}
261EXPORT_SYMBOL_GPL(register_vt_notifier);
262
263int unregister_vt_notifier(struct notifier_block *nb)
264{
265 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
266}
267EXPORT_SYMBOL_GPL(unregister_vt_notifier);
268
269static void notify_write(struct vc_data *vc, unsigned int unicode)
270{
Mathias Krause502fcb72011-07-30 14:01:59 +0200271 struct vt_notifier_param param = { .vc = vc, .c = unicode };
Samuel Thibaultb293d752007-10-18 23:39:17 -0700272 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
273}
274
275static void notify_update(struct vc_data *vc)
276{
277 struct vt_notifier_param param = { .vc = vc };
278 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
279}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700280/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * Low-Level Functions
282 */
283
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200284static inline bool con_is_fg(const struct vc_data *vc)
285{
286 return vc->vc_num == fg_console;
287}
288
289static inline bool con_should_update(const struct vc_data *vc)
290{
291 return con_is_visible(vc) && !console_blanked;
292}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
295{
296 unsigned short *p;
297
298 if (!viewed)
299 p = (unsigned short *)(vc->vc_origin + offset);
300 else if (!vc->vc_sw->con_screen_pos)
301 p = (unsigned short *)(vc->vc_visible_origin + offset);
302 else
303 p = vc->vc_sw->con_screen_pos(vc, offset);
304 return p;
305}
306
Alan Coxe33ac1c2010-06-01 22:52:54 +0200307/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308static inline void scrolldelta(int lines)
309{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200310 /* FIXME */
311 /* scrolldelta needs some kind of consistency lock, but the BKL was
312 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 scrollback_delta += lines;
314 schedule_console_callback();
315}
316
317void schedule_console_callback(void)
318{
319 schedule_work(&console_work);
320}
321
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400322/*
323 * Code to manage unicode-based screen buffers
324 */
325
326#ifdef NO_VC_UNI_SCREEN
327/* this disables and optimizes related code away at compile time */
328#define get_vc_uniscr(vc) NULL
329#else
330#define get_vc_uniscr(vc) vc->vc_uni_screen
331#endif
332
Nicolas Pitre02240802018-07-17 21:02:41 -0400333#define VC_UNI_SCREEN_DEBUG 0
334
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400335typedef uint32_t char32_t;
336
337/*
338 * Our screen buffer is preceded by an array of line pointers so that
339 * scrolling only implies some pointer shuffling.
340 */
341struct uni_screen {
342 char32_t *lines[0];
343};
344
345static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
346{
347 struct uni_screen *uniscr;
348 void *p;
349 unsigned int memsize, i;
350
351 /* allocate everything in one go */
352 memsize = cols * rows * sizeof(char32_t);
353 memsize += rows * sizeof(char32_t *);
Nicolas Pitre9a98e7a2020-03-28 22:25:11 -0400354 p = vmalloc(memsize);
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400355 if (!p)
356 return NULL;
357
358 /* initial line pointers */
359 uniscr = p;
360 p = uniscr->lines + rows;
361 for (i = 0; i < rows; i++) {
362 uniscr->lines[i] = p;
363 p += cols * sizeof(char32_t);
364 }
365 return uniscr;
366}
367
Nicolas Pitre57d38f22020-05-02 11:01:07 -0400368static void vc_uniscr_free(struct uni_screen *uniscr)
369{
370 vfree(uniscr);
371}
372
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400373static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
374{
Nicolas Pitre57d38f22020-05-02 11:01:07 -0400375 vc_uniscr_free(vc->vc_uni_screen);
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400376 vc->vc_uni_screen = new_uniscr;
377}
378
379static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
380{
381 struct uni_screen *uniscr = get_vc_uniscr(vc);
382
383 if (uniscr)
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200384 uniscr->lines[vc->state.y][vc->state.x] = uc;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400385}
386
387static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
388{
389 struct uni_screen *uniscr = get_vc_uniscr(vc);
390
391 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200392 char32_t *ln = uniscr->lines[vc->state.y];
393 unsigned int x = vc->state.x, cols = vc->vc_cols;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400394
395 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
396 memset32(&ln[x], ' ', nr);
397 }
398}
399
400static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
401{
402 struct uni_screen *uniscr = get_vc_uniscr(vc);
403
404 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200405 char32_t *ln = uniscr->lines[vc->state.y];
406 unsigned int x = vc->state.x, cols = vc->vc_cols;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400407
408 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
409 memset32(&ln[cols - nr], ' ', nr);
410 }
411}
412
413static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
414 unsigned int nr)
415{
416 struct uni_screen *uniscr = get_vc_uniscr(vc);
417
418 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200419 char32_t *ln = uniscr->lines[vc->state.y];
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400420
421 memset32(&ln[x], ' ', nr);
422 }
423}
424
425static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
426 unsigned int nr)
427{
428 struct uni_screen *uniscr = get_vc_uniscr(vc);
429
430 if (uniscr) {
431 unsigned int cols = vc->vc_cols;
432
433 while (nr--)
434 memset32(uniscr->lines[y++], ' ', cols);
435 }
436}
437
438static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
439 enum con_scroll dir, unsigned int nr)
440{
441 struct uni_screen *uniscr = get_vc_uniscr(vc);
442
443 if (uniscr) {
Nicolas Pitred541ae42018-07-19 00:05:25 -0400444 unsigned int i, j, k, sz, d, clear;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400445
Nicolas Pitred541ae42018-07-19 00:05:25 -0400446 sz = b - t;
447 clear = b - nr;
448 d = nr;
449 if (dir == SM_DOWN) {
450 clear = t;
451 d = sz - nr;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400452 }
Nicolas Pitred541ae42018-07-19 00:05:25 -0400453 for (i = 0; i < gcd(d, sz); i++) {
454 char32_t *tmp = uniscr->lines[t + i];
455 j = i;
456 while (1) {
457 k = j + d;
458 if (k >= sz)
459 k -= sz;
460 if (k == i)
461 break;
462 uniscr->lines[t + j] = uniscr->lines[t + k];
463 j = k;
464 }
465 uniscr->lines[t + j] = tmp;
466 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400467 vc_uniscr_clear_lines(vc, clear, nr);
468 }
469}
470
471static void vc_uniscr_copy_area(struct uni_screen *dst,
472 unsigned int dst_cols,
473 unsigned int dst_rows,
474 struct uni_screen *src,
475 unsigned int src_cols,
476 unsigned int src_top_row,
477 unsigned int src_bot_row)
478{
479 unsigned int dst_row = 0;
480
481 if (!dst)
482 return;
483
484 while (src_top_row < src_bot_row) {
485 char32_t *src_line = src->lines[src_top_row];
486 char32_t *dst_line = dst->lines[dst_row];
487
488 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
489 if (dst_cols - src_cols)
490 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
491 src_top_row++;
492 dst_row++;
493 }
494 while (dst_row < dst_rows) {
495 char32_t *dst_line = dst->lines[dst_row];
496
497 memset32(dst_line, ' ', dst_cols);
498 dst_row++;
499 }
500}
501
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400502/*
503 * Called from vcs_read() to make sure unicode screen retrieval is possible.
504 * This will initialize the unicode screen buffer if not already done.
505 * This returns 0 if OK, or a negative error code otherwise.
506 * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
507 */
508int vc_uniscr_check(struct vc_data *vc)
509{
510 struct uni_screen *uniscr;
511 unsigned short *p;
512 int x, y, mask;
513
514 if (__is_defined(NO_VC_UNI_SCREEN))
515 return -EOPNOTSUPP;
516
517 WARN_CONSOLE_UNLOCKED();
518
519 if (!vc->vc_utf)
520 return -ENODATA;
521
522 if (vc->vc_uni_screen)
523 return 0;
524
525 uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
526 if (!uniscr)
527 return -ENOMEM;
528
529 /*
530 * Let's populate it initially with (imperfect) reverse translation.
531 * This is the next best thing we can do short of having it enabled
532 * from the start even when no users rely on this functionality. True
533 * unicode content will be available after a complete screen refresh.
534 */
535 p = (unsigned short *)vc->vc_origin;
536 mask = vc->vc_hi_font_mask | 0xff;
537 for (y = 0; y < vc->vc_rows; y++) {
538 char32_t *line = uniscr->lines[y];
539 for (x = 0; x < vc->vc_cols; x++) {
540 u16 glyph = scr_readw(p++) & mask;
541 line[x] = inverse_translate(vc, glyph, true);
542 }
543 }
544
545 vc->vc_uni_screen = uniscr;
546 return 0;
547}
548
549/*
550 * Called from vcs_read() to get the unicode data from the screen.
551 * This must be preceded by a successful call to vc_uniscr_check() once
552 * the console lock has been taken.
553 */
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400554void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400555 unsigned int row, unsigned int col, unsigned int nr)
556{
557 struct uni_screen *uniscr = get_vc_uniscr(vc);
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400558 int offset = row * vc->vc_size_row + col * 2;
559 unsigned long pos;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400560
561 BUG_ON(!uniscr);
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400562
563 pos = (unsigned long)screenpos(vc, offset, viewed);
564 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
565 /*
566 * Desired position falls in the main screen buffer.
567 * However the actual row/col might be different if
568 * scrollback is active.
569 */
570 row = (pos - vc->vc_origin) / vc->vc_size_row;
571 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
572 memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
573 } else {
574 /*
575 * Scrollback is active. For now let's simply backtranslate
576 * the screen glyphs until the unicode screen buffer does
577 * synchronize with console display drivers for a scrollback
578 * buffer of its own.
579 */
580 u16 *p = (u16 *)pos;
581 int mask = vc->vc_hi_font_mask | 0xff;
582 char32_t *uni_buf = dest;
583 while (nr--) {
584 u16 glyph = scr_readw(p++) & mask;
585 *uni_buf++ = inverse_translate(vc, glyph, true);
586 }
587 }
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400588}
589
Nicolas Pitre02240802018-07-17 21:02:41 -0400590/* this is for validation and debugging only */
591static void vc_uniscr_debug_check(struct vc_data *vc)
592{
593 struct uni_screen *uniscr = get_vc_uniscr(vc);
594 unsigned short *p;
595 int x, y, mask;
596
597 if (!VC_UNI_SCREEN_DEBUG || !uniscr)
598 return;
599
600 WARN_CONSOLE_UNLOCKED();
601
602 /*
603 * Make sure our unicode screen translates into the same glyphs
604 * as the actual screen. This is brutal indeed.
605 */
606 p = (unsigned short *)vc->vc_origin;
607 mask = vc->vc_hi_font_mask | 0xff;
608 for (y = 0; y < vc->vc_rows; y++) {
609 char32_t *line = uniscr->lines[y];
610 for (x = 0; x < vc->vc_cols; x++) {
611 u16 glyph = scr_readw(p++) & mask;
612 char32_t uc = line[x];
613 int tc = conv_uni_to_pc(vc, uc);
614 if (tc == -4)
615 tc = conv_uni_to_pc(vc, 0xfffd);
616 if (tc == -4)
617 tc = conv_uni_to_pc(vc, '?');
618 if (tc != glyph)
619 pr_err_ratelimited(
620 "%s: mismatch at %d,%d: glyph=%#x tc=%#x\n",
621 __func__, x, y, glyph, tc);
622 }
623 }
624}
625
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400626
Jiri Slaby89765b92016-10-03 11:18:34 +0200627static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
628 enum con_scroll dir, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Jiri Slaby89765b92016-10-03 11:18:34 +0200630 u16 *clear, *d, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Jiri Slaby89765b92016-10-03 11:18:34 +0200632 if (t + nr >= b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 nr = b - t - 1;
634 if (b > vc->vc_rows || t >= b || nr < 1)
635 return;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400636 vc_uniscr_scroll(vc, t, b, dir, nr);
Jiri Slaby89765b92016-10-03 11:18:34 +0200637 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 return;
Jiri Slaby89765b92016-10-03 11:18:34 +0200639
640 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
641 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
642
643 if (dir == SM_UP) {
644 clear = s + (b - t - nr) * vc->vc_cols;
645 swap(s, d);
646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Jiri Slaby89765b92016-10-03 11:18:34 +0200648 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649}
650
651static void do_update_region(struct vc_data *vc, unsigned long start, int count)
652{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 unsigned int xx, yy, offset;
654 u16 *p;
655
656 p = (u16 *) start;
657 if (!vc->vc_sw->con_getxy) {
658 offset = (start - vc->vc_origin) / 2;
659 xx = offset % vc->vc_cols;
660 yy = offset / vc->vc_cols;
661 } else {
662 int nxx, nyy;
663 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
664 xx = nxx; yy = nyy;
665 }
666 for(;;) {
667 u16 attrib = scr_readw(p) & 0xff00;
668 int startx = xx;
669 u16 *q = p;
670 while (xx < vc->vc_cols && count) {
671 if (attrib != (scr_readw(p) & 0xff00)) {
672 if (p > q)
673 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
674 startx = xx;
675 q = p;
676 attrib = scr_readw(p) & 0xff00;
677 }
678 p++;
679 xx++;
680 count--;
681 }
682 if (p > q)
683 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
684 if (!count)
685 break;
686 xx = 0;
687 yy++;
688 if (vc->vc_sw->con_getxy) {
689 p = (u16 *)start;
690 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
691 }
692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
695void update_region(struct vc_data *vc, unsigned long start, int count)
696{
697 WARN_CONSOLE_UNLOCKED();
698
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200699 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 hide_cursor(vc);
701 do_update_region(vc, start, count);
702 set_cursor(vc);
703 }
704}
705
706/* Structure of attributes is hardware-dependent */
707
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200708static u8 build_attr(struct vc_data *vc, u8 _color,
Jiri Slaby77bc14f2020-06-15 09:48:35 +0200709 enum vc_intensity _intensity, bool _blink, bool _underline,
710 bool _reverse, bool _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700713 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
714 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716/*
717 * ++roman: I completely changed the attribute format for monochrome
718 * mode (!can_do_color). The formerly used MDA (monochrome display
719 * adapter) format didn't allow the combination of certain effects.
720 * Now the attribute is just a bit vector:
721 * Bit 0..1: intensity (0..2)
722 * Bit 2 : underline
723 * Bit 3 : reverse
724 * Bit 7 : blink
725 */
726 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700727 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 if (!vc->vc_can_do_color)
729 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700730 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 (_underline ? 4 : 0) |
732 (_reverse ? 8 : 0) |
733 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700734 if (_italic)
735 a = (a & 0xF0) | vc->vc_itcolor;
736 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 a = (a & 0xf0) | vc->vc_ulcolor;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200738 else if (_intensity == VCI_HALF_BRIGHT)
Adam Borowski1c65a872017-06-03 09:08:25 +0200739 a = (a & 0xf0) | vc->vc_halfcolor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (_reverse)
741 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
742 if (_blink)
743 a ^= 0x80;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200744 if (_intensity == VCI_BOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 a ^= 0x08;
746 if (vc->vc_hi_font_mask == 0x100)
747 a <<= 1;
748 return a;
749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752static void update_attr(struct vc_data *vc)
753{
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200754 vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity,
755 vc->state.blink, vc->state.underline,
756 vc->state.reverse ^ vc->vc_decscnm, vc->state.italic);
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200757 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color,
Jiri Slaby77bc14f2020-06-15 09:48:35 +0200758 VCI_NORMAL, vc->state.blink, false,
759 vc->vc_decscnm, false) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760}
761
762/* Note: inverting the screen twice should revert to the original state */
763void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
764{
765 unsigned short *p;
766
767 WARN_CONSOLE_UNLOCKED();
768
769 count /= 2;
770 p = screenpos(vc, offset, viewed);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200771 if (vc->vc_sw->con_invert_region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 vc->vc_sw->con_invert_region(vc, p, count);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200773 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 u16 *q = p;
775 int cnt = count;
776 u16 a;
777
778 if (!vc->vc_can_do_color) {
779 while (cnt--) {
780 a = scr_readw(q);
781 a ^= 0x0800;
782 scr_writew(a, q);
783 q++;
784 }
785 } else if (vc->vc_hi_font_mask == 0x100) {
786 while (cnt--) {
787 a = scr_readw(q);
788 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
789 scr_writew(a, q);
790 q++;
791 }
792 } else {
793 while (cnt--) {
794 a = scr_readw(q);
795 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
796 scr_writew(a, q);
797 q++;
798 }
799 }
800 }
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200801
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200802 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 do_update_region(vc, (unsigned long) p, count);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500804 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
807/* used by selection: complement pointer position */
808void complement_pos(struct vc_data *vc, int offset)
809{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700810 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 static unsigned short old;
812 static unsigned short oldx, oldy;
813
814 WARN_CONSOLE_UNLOCKED();
815
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700816 if (old_offset != -1 && old_offset >= 0 &&
817 old_offset < vc->vc_screenbuf_size) {
818 scr_writew(old, screenpos(vc, old_offset, 1));
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200819 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 vc->vc_sw->con_putc(vc, old, oldy, oldx);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500821 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700823
824 old_offset = offset;
825
826 if (offset != -1 && offset >= 0 &&
827 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700829 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 p = screenpos(vc, offset, 1);
831 old = scr_readw(p);
832 new = old ^ vc->vc_complement_mask;
833 scr_writew(new, p);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200834 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 oldx = (offset >> 1) % vc->vc_cols;
836 oldy = (offset >> 1) / vc->vc_cols;
837 vc->vc_sw->con_putc(vc, new, oldy, oldx);
838 }
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500839 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841}
842
843static void insert_char(struct vc_data *vc, unsigned int nr)
844{
Jean-François Moine81732c32012-09-06 19:24:13 +0200845 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400847 vc_uniscr_insert(vc, nr);
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200848 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200849 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200851 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200852 do_update_region(vc, (unsigned long) p,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200853 vc->vc_cols - vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856static void delete_char(struct vc_data *vc, unsigned int nr)
857{
Jean-François Moine81732c32012-09-06 19:24:13 +0200858 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400860 vc_uniscr_delete(vc, nr);
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200861 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
862 scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
Jean-François Moine81732c32012-09-06 19:24:13 +0200863 nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200865 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200866 do_update_region(vc, (unsigned long) p,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200867 vc->vc_cols - vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868}
869
Melchior FRANZe882f712015-11-01 19:48:18 +0100870static int softcursor_original = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872static void add_softcursor(struct vc_data *vc)
873{
874 int i = scr_readw((u16 *) vc->vc_pos);
875 u32 type = vc->vc_cursor_type;
876
877 if (! (type & 0x10)) return;
878 if (softcursor_original != -1) return;
879 softcursor_original = i;
880 i |= ((type >> 8) & 0xff00 );
881 i ^= ((type) & 0xff00 );
882 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
883 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
884 scr_writew(i, (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200885 if (con_should_update(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200886 vc->vc_sw->con_putc(vc, i, vc->state.y, vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887}
888
889static void hide_softcursor(struct vc_data *vc)
890{
891 if (softcursor_original != -1) {
892 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200893 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 vc->vc_sw->con_putc(vc, softcursor_original,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200895 vc->state.y, vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 softcursor_original = -1;
897 }
898}
899
900static void hide_cursor(struct vc_data *vc)
901{
Jiri Slabydce05aa2020-02-19 08:39:43 +0100902 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 clear_selection();
Jiri Slabydce05aa2020-02-19 08:39:43 +0100904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 vc->vc_sw->con_cursor(vc, CM_ERASE);
906 hide_softcursor(vc);
907}
908
909static void set_cursor(struct vc_data *vc)
910{
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200911 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return;
913 if (vc->vc_deccm) {
Jiri Slabydce05aa2020-02-19 08:39:43 +0100914 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 clear_selection();
916 add_softcursor(vc);
917 if ((vc->vc_cursor_type & 0x0f) != 1)
918 vc->vc_sw->con_cursor(vc, CM_DRAW);
919 } else
920 hide_cursor(vc);
921}
922
923static void set_origin(struct vc_data *vc)
924{
925 WARN_CONSOLE_UNLOCKED();
926
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200927 if (!con_is_visible(vc) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 !vc->vc_sw->con_set_origin ||
929 !vc->vc_sw->con_set_origin(vc))
930 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
931 vc->vc_visible_origin = vc->vc_origin;
932 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200933 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y +
934 2 * vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Denys Vlasenkoc0f160a2015-10-27 18:46:47 +0100937static void save_screen(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938{
939 WARN_CONSOLE_UNLOCKED();
940
941 if (vc->vc_sw->con_save_screen)
942 vc->vc_sw->con_save_screen(vc);
943}
944
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100945static void flush_scrollback(struct vc_data *vc)
946{
947 WARN_CONSOLE_UNLOCKED();
948
Nicolas Pitrea6dbe442019-02-11 19:36:41 -0500949 set_origin(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500950 if (vc->vc_sw->con_flush_scrollback) {
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100951 vc->vc_sw->con_flush_scrollback(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500952 } else if (con_is_visible(vc)) {
953 /*
954 * When no con_flush_scrollback method is provided then the
955 * legacy way for flushing the scrollback buffer is to use
956 * a side effect of the con_switch method. We do it only on
957 * the foreground console as background consoles have no
958 * scrollback buffers in that case and we obviously don't
959 * want to switch to them.
960 */
961 hide_cursor(vc);
Nicolas Pitrea6dbe442019-02-11 19:36:41 -0500962 vc->vc_sw->con_switch(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500963 set_cursor(vc);
964 }
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100965}
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/*
968 * Redrawing of screen
969 */
970
Dave Airlie2a248302013-01-24 14:14:19 +1000971void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972{
973 unsigned short *p = (unsigned short *)vc->vc_origin;
974 int count = vc->vc_screenbuf_size / 2;
975 int mask = vc->vc_hi_font_mask | 0xff;
976
977 for (; count > 0; count--, p++) {
978 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
979 }
980}
981
982void redraw_screen(struct vc_data *vc, int is_switch)
983{
984 int redraw = 0;
985
986 WARN_CONSOLE_UNLOCKED();
987
988 if (!vc) {
989 /* strange ... */
990 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
991 return;
992 }
993
994 if (is_switch) {
995 struct vc_data *old_vc = vc_cons[fg_console].d;
996 if (old_vc == vc)
997 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200998 if (!con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 redraw = 1;
1000 *vc->vc_display_fg = vc;
1001 fg_console = vc->vc_num;
1002 hide_cursor(old_vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001003 if (!con_is_visible(old_vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 save_screen(old_vc);
1005 set_origin(old_vc);
1006 }
Kay Sieversfbc92a32010-12-01 18:51:05 +01001007 if (tty0dev)
1008 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 } else {
1010 hide_cursor(vc);
1011 redraw = 1;
1012 }
1013
1014 if (redraw) {
1015 int update;
1016 int old_was_color = vc->vc_can_do_color;
1017
1018 set_origin(vc);
1019 update = vc->vc_sw->con_switch(vc);
1020 set_palette(vc);
1021 /*
1022 * If console changed from mono<->color, the best we can do
1023 * is to clear the buffer attributes. As it currently stands,
1024 * rebuilding new attributes from the old buffer is not doable
1025 * without overly complex code.
1026 */
1027 if (old_was_color != vc->vc_can_do_color) {
1028 update_attr(vc);
1029 clear_buffer_attributes(vc);
1030 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07001031
Daniel Vetter8d7fc292018-08-22 10:54:03 +02001032 if (update && vc->vc_mode != KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
1034 }
1035 set_cursor(vc);
1036 if (is_switch) {
1037 set_leds();
1038 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -08001039 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041}
1042
1043/*
1044 * Allocation, freeing and resizing of VTs.
1045 */
1046
1047int vc_cons_allocated(unsigned int i)
1048{
1049 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
1050}
1051
1052static void visual_init(struct vc_data *vc, int num, int init)
1053{
1054 /* ++Geert: vc->vc_sw->con_init determines console size */
1055 if (vc->vc_sw)
1056 module_put(vc->vc_sw->owner);
1057 vc->vc_sw = conswitchp;
1058#ifndef VT_SINGLE_DRIVER
1059 if (con_driver_map[num])
1060 vc->vc_sw = con_driver_map[num];
1061#endif
1062 __module_get(vc->vc_sw->owner);
1063 vc->vc_num = num;
1064 vc->vc_display_fg = &master_display_fg;
Dongxing Zhang08b33242015-06-10 15:21:10 +08001065 if (vc->vc_uni_pagedir_loc)
1066 con_free_unimap(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +02001068 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 vc->vc_hi_font_mask = 0;
1070 vc->vc_complement_mask = 0;
1071 vc->vc_can_do_color = 0;
David Daney1b459962016-05-17 11:41:04 -07001072 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 vc->vc_sw->con_init(vc, init);
1074 if (!vc->vc_complement_mask)
1075 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1076 vc->vc_s_complement_mask = vc->vc_complement_mask;
1077 vc->vc_size_row = vc->vc_cols << 1;
1078 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
1079}
1080
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001081
1082static void visual_deinit(struct vc_data *vc)
1083{
1084 vc->vc_sw->con_deinit(vc);
1085 module_put(vc->vc_sw->owner);
1086}
1087
Eric Biggersca4463b2020-03-21 20:43:04 -07001088static void vc_port_destruct(struct tty_port *port)
1089{
1090 struct vc_data *vc = container_of(port, struct vc_data, port);
1091
1092 kfree(vc);
1093}
1094
1095static const struct tty_port_operations vc_port_ops = {
1096 .destruct = vc_port_destruct,
1097};
1098
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099int vc_allocate(unsigned int currcons) /* return 0 on success */
1100{
Jiri Slaby34902b72016-03-31 10:08:15 +02001101 struct vt_notifier_param param;
1102 struct vc_data *vc;
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 WARN_CONSOLE_UNLOCKED();
1105
1106 if (currcons >= MAX_NR_CONSOLES)
1107 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
Jiri Slaby34902b72016-03-31 10:08:15 +02001109 if (vc_cons[currcons].d)
1110 return 0;
1111
1112 /* due to the granularity of kmalloc, we waste some memory here */
1113 /* the alloc is done in two steps, to optimize the common situation
1114 of a 25x80 console (structsize=216, screenbuf_size=4000) */
1115 /* although the numbers above are not valid since long ago, the
1116 point is still up-to-date and the comment still has its value
1117 even if only as a historical artifact. --mj, July 1998 */
1118 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
1119 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 return -ENOMEM;
Jiri Slaby34902b72016-03-31 10:08:15 +02001121
1122 vc_cons[currcons].d = vc;
1123 tty_port_init(&vc->port);
Eric Biggersca4463b2020-03-21 20:43:04 -07001124 vc->port.ops = &vc_port_ops;
Jiri Slaby34902b72016-03-31 10:08:15 +02001125 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1126
1127 visual_init(vc, currcons, 1);
1128
1129 if (!*vc->vc_uni_pagedir_loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 con_set_default_unimap(vc);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001131
Alexander Potapenko21eff692018-06-14 12:23:09 +02001132 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Jiri Slaby34902b72016-03-31 10:08:15 +02001133 if (!vc->vc_screenbuf)
1134 goto err_free;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001135
Jiri Slaby34902b72016-03-31 10:08:15 +02001136 /* If no drivers have overridden us and the user didn't pass a
1137 boot option, default to displaying the cursor */
1138 if (global_cursor_default == -1)
1139 global_cursor_default = 1;
1140
1141 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
1142 vcs_make_sysfs(currcons);
1143 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
Jiri Slaby34902b72016-03-31 10:08:15 +02001146err_free:
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001147 visual_deinit(vc);
Jiri Slaby34902b72016-03-31 10:08:15 +02001148 kfree(vc);
1149 vc_cons[currcons].d = NULL;
1150 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151}
1152
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001153static inline int resize_screen(struct vc_data *vc, int width, int height,
1154 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155{
1156 /* Resizes the resolution of the display adapater */
1157 int err = 0;
1158
1159 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001160 err = vc->vc_sw->con_resize(vc, width, height, user);
1161
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 return err;
1163}
1164
1165/*
1166 * Change # of rows and columns (0 means unchanged/the size of fg_console)
1167 * [this is to be used together with some user program
1168 * like resize that changes the hardware videomode]
1169 */
1170#define VC_RESIZE_MAXCOL (32767)
1171#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001172
1173/**
1174 * vc_do_resize - resizing method for the tty
1175 * @tty: tty being resized
1176 * @real_tty: real tty (different to tty if a pty/tty pair)
1177 * @vc: virtual console private data
1178 * @cols: columns
1179 * @lines: lines
1180 *
1181 * Resize a virtual console, clipping according to the actual constraints.
1182 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08001183 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001184 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001185 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001186 * ctrl_lock of the tty IFF a tty is passed.
1187 */
1188
Alan Coxfc6f6232009-01-02 13:43:17 +00001189static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1190 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
1192 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -07001193 unsigned long end;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001194 unsigned int old_rows, old_row_size, first_copied_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -07001196 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 unsigned short *newscreen;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001198 struct uni_screen *new_uniscr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 WARN_CONSOLE_UNLOCKED();
1201
1202 if (!vc)
1203 return -ENXIO;
1204
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001205 user = vc->vc_resize_user;
1206 vc->vc_resize_user = 0;
1207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1209 return -EINVAL;
1210
1211 new_cols = (cols ? cols : vc->vc_cols);
1212 new_rows = (lines ? lines : vc->vc_rows);
1213 new_row_size = new_cols << 1;
1214 new_screen_size = new_row_size * new_rows;
1215
1216 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1217 return 0;
1218
Nicolas Pitre27177692020-03-28 17:32:42 -04001219 if (new_screen_size > KMALLOC_MAX_SIZE)
Dmitry Vyukov32b29212016-10-14 15:18:28 +02001220 return -EINVAL;
Alexander Potapenko21eff692018-06-14 12:23:09 +02001221 newscreen = kzalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (!newscreen)
1223 return -ENOMEM;
1224
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001225 if (get_vc_uniscr(vc)) {
1226 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1227 if (!new_uniscr) {
1228 kfree(newscreen);
1229 return -ENOMEM;
1230 }
1231 }
1232
Jiri Slabydce05aa2020-02-19 08:39:43 +01001233 if (vc_is_sel(vc))
Scot Doyle009e39a2016-10-13 12:12:43 -05001234 clear_selection();
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001239 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (err) {
1241 kfree(newscreen);
Nicolas Pitre57d38f22020-05-02 11:01:07 -04001242 vc_uniscr_free(new_uniscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return err;
1244 }
1245
1246 vc->vc_rows = new_rows;
1247 vc->vc_cols = new_cols;
1248 vc->vc_size_row = new_row_size;
1249 vc->vc_screenbuf_size = new_screen_size;
1250
1251 rlth = min(old_row_size, new_row_size);
1252 rrem = new_row_size - rlth;
1253 old_origin = vc->vc_origin;
1254 new_origin = (long) newscreen;
1255 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001256
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001257 if (vc->state.y > new_rows) {
1258 if (old_rows - vc->state.y < new_rows) {
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001259 /*
1260 * Cursor near the bottom, copy contents from the
1261 * bottom of buffer
1262 */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001263 first_copied_row = (old_rows - new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001264 } else {
1265 /*
1266 * Cursor is in no man's land, copy 1/2 screenful
1267 * from the top and bottom of cursor position
1268 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001269 first_copied_row = (vc->state.y - new_rows/2);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001270 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001271 old_origin += first_copied_row * old_row_size;
1272 } else
1273 first_copied_row = 0;
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02001274 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001276 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1277 get_vc_uniscr(vc), rlth/2, first_copied_row,
1278 min(old_rows, new_rows));
1279 vc_uniscr_set(vc, new_uniscr);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 update_attr(vc);
1282
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001283 while (old_origin < end) {
1284 scr_memcpyw((unsigned short *) new_origin,
1285 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001287 scr_memsetw((void *)(new_origin + rlth),
1288 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 old_origin += old_row_size;
1290 new_origin += new_row_size;
1291 }
1292 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001293 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1294 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001295 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 vc->vc_screenbuf_size = new_screen_size;
1298 set_origin(vc);
1299
1300 /* do part of a reset_terminal() */
1301 vc->vc_top = 0;
1302 vc->vc_bottom = vc->vc_rows;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001303 gotoxy(vc, vc->state.x, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 save_cur(vc);
1305
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001306 if (tty) {
1307 /* Rewrite the requested winsize data with the actual
1308 resulting sizes */
1309 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 memset(&ws, 0, sizeof(ws));
1311 ws.ws_row = vc->vc_rows;
1312 ws.ws_col = vc->vc_cols;
1313 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +00001314 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
1316
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001317 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07001319 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Nicolas Pitre0c9b1962019-01-08 22:55:01 -05001320 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 return err;
1322}
1323
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001324/**
1325 * vc_resize - resize a VT
1326 * @vc: virtual console
1327 * @cols: columns
1328 * @rows: rows
1329 *
1330 * Resize a virtual console as seen from the console end of things. We
1331 * use the common vc_do_resize methods to update the structures. The
1332 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +02001333 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001334 */
1335
1336int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -07001337{
Alan Cox8ce73262010-06-01 22:52:56 +02001338 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001339}
1340
1341/**
1342 * vt_resize - resize a VT
1343 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001344 * @ws: winsize attributes
1345 *
1346 * Resize a virtual terminal. This is called by the tty layer as we
1347 * register our own handler for resizing. The mutual helper does all
1348 * the actual work.
1349 *
1350 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -04001351 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001352 */
Roel Kluinda2bdf92009-01-07 18:09:15 -08001353static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001354{
1355 struct vc_data *vc = tty->driver_data;
1356 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357
Torben Hohnac751ef2011-01-25 15:07:35 -08001358 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001359 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001360 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001361 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001362}
1363
Peter Hurley421b40a2013-05-17 12:41:03 -04001364struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365{
Peter Hurley421b40a2013-05-17 12:41:03 -04001366 struct vc_data *vc = NULL;
1367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 WARN_CONSOLE_UNLOCKED();
1369
1370 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001371 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001372
Peter Hurley421b40a2013-05-17 12:41:03 -04001373 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001374 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001375 vcs_remove_sysfs(currcons);
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001376 visual_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001377 put_pid(vc->vt_pid);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001378 vc_uniscr_set(vc, NULL);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001379 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 vc_cons[currcons].d = NULL;
1381 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001382 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
1385/*
1386 * VT102 emulator
1387 */
1388
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01001389enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
1390
Alan Cox079c9532012-02-28 14:49:23 +00001391#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1392#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1393#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395#define decarm VC_REPEAT
1396#define decckm VC_CKMODE
1397#define kbdapplic VC_APPLIC
1398#define lnm VC_CRLF
1399
Jiri Slaby8ede5cc2016-03-31 10:08:16 +02001400const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 8,12,10,14, 9,13,11,15 };
1402
1403/* the default colour table, for VGA+ colour systems */
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001404unsigned char default_red[] = {
1405 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1406 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1407};
1408module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001410unsigned char default_grn[] = {
1411 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1412 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1413};
1414module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1415
1416unsigned char default_blu[] = {
1417 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1418 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1419};
1420module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001421
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422/*
1423 * gotoxy() must verify all boundaries, because the arguments
1424 * might also be negative. If the given position is out of
1425 * bounds, the cursor is placed at the nearest margin.
1426 */
1427static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1428{
1429 int min_y, max_y;
1430
1431 if (new_x < 0)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001432 vc->state.x = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 else {
1434 if (new_x >= vc->vc_cols)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001435 vc->state.x = vc->vc_cols - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 else
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001437 vc->state.x = new_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 }
1439
1440 if (vc->vc_decom) {
1441 min_y = vc->vc_top;
1442 max_y = vc->vc_bottom;
1443 } else {
1444 min_y = 0;
1445 max_y = vc->vc_rows;
1446 }
1447 if (new_y < min_y)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001448 vc->state.y = min_y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 else if (new_y >= max_y)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001450 vc->state.y = max_y - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 else
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001452 vc->state.y = new_y;
1453 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row +
1454 (vc->state.x << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 vc->vc_need_wrap = 0;
1456}
1457
1458/* for absolute user moves, when decom is set */
1459static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1460{
1461 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1462}
1463
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001464void scrollback(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465{
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001466 scrolldelta(-(vc->vc_rows / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469void scrollfront(struct vc_data *vc, int lines)
1470{
1471 if (!lines)
1472 lines = vc->vc_rows / 2;
1473 scrolldelta(lines);
1474}
1475
1476static void lf(struct vc_data *vc)
1477{
1478 /* don't scroll if above bottom of scrolling region, or
1479 * if below scrolling region
1480 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001481 if (vc->state.y + 1 == vc->vc_bottom)
Jiri Slaby89765b92016-10-03 11:18:34 +02001482 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001483 else if (vc->state.y < vc->vc_rows - 1) {
1484 vc->state.y++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 vc->vc_pos += vc->vc_size_row;
1486 }
1487 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001488 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491static void ri(struct vc_data *vc)
1492{
1493 /* don't scroll if below top of scrolling region, or
1494 * if above scrolling region
1495 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001496 if (vc->state.y == vc->vc_top)
Jiri Slaby89765b92016-10-03 11:18:34 +02001497 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001498 else if (vc->state.y > 0) {
1499 vc->state.y--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 vc->vc_pos -= vc->vc_size_row;
1501 }
1502 vc->vc_need_wrap = 0;
1503}
1504
1505static inline void cr(struct vc_data *vc)
1506{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001507 vc->vc_pos -= vc->state.x << 1;
1508 vc->vc_need_wrap = vc->state.x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001509 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512static inline void bs(struct vc_data *vc)
1513{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001514 if (vc->state.x) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 vc->vc_pos -= 2;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001516 vc->state.x--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001518 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 }
1520}
1521
1522static inline void del(struct vc_data *vc)
1523{
1524 /* ignored */
1525}
1526
1527static void csi_J(struct vc_data *vc, int vpar)
1528{
1529 unsigned int count;
1530 unsigned short * start;
1531
1532 switch (vpar) {
1533 case 0: /* erase from cursor to end of display */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001534 vc_uniscr_clear_line(vc, vc->state.x,
1535 vc->vc_cols - vc->state.x);
1536 vc_uniscr_clear_lines(vc, vc->state.y + 1,
1537 vc->vc_rows - vc->state.y - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1539 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 break;
1541 case 1: /* erase from start to cursor */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001542 vc_uniscr_clear_line(vc, 0, vc->state.x + 1);
1543 vc_uniscr_clear_lines(vc, 0, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1545 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 break;
Nicolas Pitrea6dbe442019-02-11 19:36:41 -05001547 case 3: /* include scrollback */
1548 flush_scrollback(vc);
1549 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 case 2: /* erase whole display */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001551 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 count = vc->vc_cols * vc->vc_rows;
1553 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 break;
1555 default:
1556 return;
1557 }
1558 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Mikulas Patockab2ecf002019-04-04 20:53:28 -04001559 if (con_should_update(vc))
1560 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 vc->vc_need_wrap = 0;
1562}
1563
1564static void csi_K(struct vc_data *vc, int vpar)
1565{
1566 unsigned int count;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001567 unsigned short *start = (unsigned short *)vc->vc_pos;
1568 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 switch (vpar) {
1571 case 0: /* erase from cursor to end of line */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001572 offset = 0;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001573 count = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 case 1: /* erase from start of line to cursor */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001576 offset = -vc->state.x;
1577 count = vc->state.x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 break;
1579 case 2: /* erase whole line */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001580 offset = -vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 break;
1583 default:
1584 return;
1585 }
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001586 vc_uniscr_clear_line(vc, vc->state.x + offset, count);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001587 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001589 if (con_should_update(vc))
Mikulas Patocka943210b2018-10-23 11:28:28 -04001590 do_update_region(vc, (unsigned long)(start + offset), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
1593static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1594{ /* not vt100? */
1595 int count;
1596
1597 if (!vpar)
1598 vpar++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001599 count = (vpar > vc->vc_cols - vc->state.x) ? (vc->vc_cols - vc->state.x) : vpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001601 vc_uniscr_clear_line(vc, vc->state.x, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001603 if (con_should_update(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001604 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 vc->vc_need_wrap = 0;
1606}
1607
1608static void default_attr(struct vc_data *vc)
1609{
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001610 vc->state.intensity = VCI_NORMAL;
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001611 vc->state.italic = false;
1612 vc->state.underline = false;
1613 vc->state.reverse = false;
1614 vc->state.blink = false;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001615 vc->state.color = vc->vc_def_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616}
1617
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001618struct rgb { u8 r; u8 g; u8 b; };
1619
Jiri Slaby0f91e142016-06-23 13:34:32 +02001620static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001621{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001622 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001623 c->r = i&1 ? 0xaa : 0x00;
1624 c->g = i&2 ? 0xaa : 0x00;
1625 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001626 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001627 c->r = i&1 ? 0xff : 0x55;
1628 c->g = i&2 ? 0xff : 0x55;
1629 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001630 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001631 c->r = (i - 16) / 36 * 85 / 2;
1632 c->g = (i - 16) / 6 % 6 * 85 / 2;
1633 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001634 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001635 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001636}
1637
Jiri Slaby0f91e142016-06-23 13:34:32 +02001638static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001639{
Jiri Slaby193df022016-06-23 13:34:33 +02001640 u8 hue = 0, max = max3(c->r, c->g, c->b);
1641
1642 if (c->r > max / 2)
1643 hue |= 4;
1644 if (c->g > max / 2)
1645 hue |= 2;
1646 if (c->b > max / 2)
1647 hue |= 1;
1648
1649 if (hue == 7 && max <= 0x55) {
1650 hue = 0;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001651 vc->state.intensity = VCI_BOLD;
Jiri Slaby193df022016-06-23 13:34:33 +02001652 } else if (max > 0xaa)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001653 vc->state.intensity = VCI_BOLD;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001654 else
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001655 vc->state.intensity = VCI_NORMAL;
Jiri Slaby193df022016-06-23 13:34:33 +02001656
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001657 vc->state.color = (vc->state.color & 0xf0) | hue;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001658}
1659
Jiri Slaby0f91e142016-06-23 13:34:32 +02001660static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001661{
1662 /* For backgrounds, err on the dark side. */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001663 vc->state.color = (vc->state.color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001664 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001665}
1666
Jiri Slabye05ab232016-06-23 13:34:31 +02001667/*
1668 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
Martin Hostettler04afcd32018-12-15 15:34:23 +01001669 * and thus need to be detected and ignored by hand. That standard also
1670 * wants : rather than ; as separators but sequences containing : are currently
1671 * completely ignored by the parser.
Jiri Slabye05ab232016-06-23 13:34:31 +02001672 *
1673 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1674 * supporting them.
1675 */
1676static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001677 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001678{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001679 struct rgb c;
1680
Jiri Slabye05ab232016-06-23 13:34:31 +02001681 i++;
1682 if (i > vc->vc_npar)
1683 return i;
1684
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001685 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001686 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001687 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001688 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001689 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001690 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001691 c.r = vc->vc_par[i + 1];
1692 c.g = vc->vc_par[i + 2];
1693 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001694 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001695 } else
1696 return i;
1697
1698 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001699
1700 return i;
1701}
1702
Torben Hohnac751ef2011-01-25 15:07:35 -08001703/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704static void csi_m(struct vc_data *vc)
1705{
1706 int i;
1707
1708 for (i = 0; i <= vc->vc_npar; i++)
1709 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001710 case 0: /* all attributes off */
1711 default_attr(vc);
1712 break;
1713 case 1:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001714 vc->state.intensity = VCI_BOLD;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001715 break;
1716 case 2:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001717 vc->state.intensity = VCI_HALF_BRIGHT;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001718 break;
1719 case 3:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001720 vc->state.italic = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001721 break;
Mike Frysinger65d99822018-01-29 17:08:21 -05001722 case 21:
1723 /*
1724 * No console drivers support double underline, so
1725 * convert it to a single underline.
1726 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001727 case 4:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001728 vc->state.underline = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001729 break;
1730 case 5:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001731 vc->state.blink = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001732 break;
1733 case 7:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001734 vc->state.reverse = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001735 break;
1736 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1737 * Select primary font, don't display control chars if
1738 * defined, don't set bit 8 on output.
1739 */
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02001740 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001741 vc->vc_disp_ctrl = 0;
1742 vc->vc_toggle_meta = 0;
1743 break;
1744 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1745 * Select first alternate font, lets chars < 32 be
1746 * displayed as ROM chars.
1747 */
1748 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1749 vc->vc_disp_ctrl = 1;
1750 vc->vc_toggle_meta = 0;
1751 break;
1752 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1753 * Select second alternate font, toggle high bit
1754 * before displaying as ROM char.
1755 */
1756 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1757 vc->vc_disp_ctrl = 1;
1758 vc->vc_toggle_meta = 1;
1759 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001760 case 22:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001761 vc->state.intensity = VCI_NORMAL;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001762 break;
1763 case 23:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001764 vc->state.italic = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001765 break;
1766 case 24:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001767 vc->state.underline = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001768 break;
1769 case 25:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001770 vc->state.blink = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001771 break;
1772 case 27:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001773 vc->state.reverse = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001774 break;
1775 case 38:
1776 i = vc_t416_color(vc, i, rgb_foreground);
1777 break;
1778 case 48:
1779 i = vc_t416_color(vc, i, rgb_background);
1780 break;
1781 case 39:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001782 vc->state.color = (vc->vc_def_color & 0x0f) |
1783 (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001784 break;
1785 case 49:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001786 vc->state.color = (vc->vc_def_color & 0xf0) |
1787 (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001788 break;
1789 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001790 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1791 if (vc->vc_par[i] < 100)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001792 vc->state.intensity = VCI_BOLD;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001793 vc->vc_par[i] -= 60;
1794 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001795 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001796 vc->state.color = color_table[vc->vc_par[i] - 30]
1797 | (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001798 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001799 vc->state.color = (color_table[vc->vc_par[i] - 40] << 4)
1800 | (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001801 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
1803 update_attr(vc);
1804}
1805
Jiri Slabyde53ce02020-06-15 09:48:40 +02001806static void respond_string(const char *p, size_t len, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001808 tty_insert_flip_string(port, p, len);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001809 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1813{
1814 char buf[40];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001815 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
Jiri Slabyde53ce02020-06-15 09:48:40 +02001817 len = sprintf(buf, "\033[%d;%dR", vc->state.y +
1818 (vc->vc_decom ? vc->vc_top + 1 : 1),
1819 vc->state.x + 1);
1820 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823static inline void status_report(struct tty_struct *tty)
1824{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001825 static const char teminal_ok[] = "\033[0n";
1826
1827 respond_string(teminal_ok, strlen(teminal_ok), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001830static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831{
Jiri Slaby9a6f72d2020-06-15 09:48:41 +02001832 /* terminal answer to an ESC-Z or csi0c query. */
1833 static const char vt102_id[] = "\033[?6c";
1834
1835 respond_string(vt102_id, strlen(vt102_id), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
1837
1838void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1839{
1840 char buf[8];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001841 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842
Jiri Slabyde53ce02020-06-15 09:48:40 +02001843 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt),
1844 (char)('!' + mrx), (char)('!' + mry));
1845 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Okash Khawaja496124e2019-04-17 13:21:13 +01001848/* invoked via ioctl(TIOCLINUX) and through set_selection_user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849int mouse_reporting(void)
1850{
1851 return vc_cons[fg_console].d->vc_report_mouse;
1852}
1853
Torben Hohnac751ef2011-01-25 15:07:35 -08001854/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855static void set_mode(struct vc_data *vc, int on_off)
1856{
1857 int i;
1858
1859 for (i = 0; i <= vc->vc_npar; i++)
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01001860 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1862 case 1: /* Cursor keys send ^[Ox/^[[x */
1863 if (on_off)
1864 set_kbd(vc, decckm);
1865 else
1866 clr_kbd(vc, decckm);
1867 break;
1868 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869#if 0
1870 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1871 /* this alone does not suffice; some user mode
1872 utility has to change the hardware regs */
1873#endif
1874 break;
1875 case 5: /* Inverted screen on/off */
1876 if (vc->vc_decscnm != on_off) {
1877 vc->vc_decscnm = on_off;
1878 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1879 update_attr(vc);
1880 }
1881 break;
1882 case 6: /* Origin relative/absolute */
1883 vc->vc_decom = on_off;
1884 gotoxay(vc, 0, 0);
1885 break;
1886 case 7: /* Autowrap on/off */
1887 vc->vc_decawm = on_off;
1888 break;
1889 case 8: /* Autorepeat on/off */
1890 if (on_off)
1891 set_kbd(vc, decarm);
1892 else
1893 clr_kbd(vc, decarm);
1894 break;
1895 case 9:
1896 vc->vc_report_mouse = on_off ? 1 : 0;
1897 break;
1898 case 25: /* Cursor on/off */
1899 vc->vc_deccm = on_off;
1900 break;
1901 case 1000:
1902 vc->vc_report_mouse = on_off ? 2 : 0;
1903 break;
1904 }
1905 } else {
1906 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1907 case 3: /* Monitor (display ctrls) */
1908 vc->vc_disp_ctrl = on_off;
1909 break;
1910 case 4: /* Insert Mode on/off */
1911 vc->vc_decim = on_off;
1912 break;
1913 case 20: /* Lf, Enter == CrLf/Lf */
1914 if (on_off)
1915 set_kbd(vc, lnm);
1916 else
1917 clr_kbd(vc, lnm);
1918 break;
1919 }
1920 }
1921}
1922
Torben Hohnac751ef2011-01-25 15:07:35 -08001923/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924static void setterm_command(struct vc_data *vc)
1925{
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001926 switch (vc->vc_par[0]) {
1927 case 1: /* set color for underline mode */
1928 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1929 vc->vc_ulcolor = color_table[vc->vc_par[1]];
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001930 if (vc->state.underline)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001931 update_attr(vc);
1932 }
1933 break;
1934 case 2: /* set color for half intensity mode */
1935 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1936 vc->vc_halfcolor = color_table[vc->vc_par[1]];
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001937 if (vc->state.intensity == VCI_HALF_BRIGHT)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001938 update_attr(vc);
1939 }
1940 break;
1941 case 8: /* store colors as defaults */
1942 vc->vc_def_color = vc->vc_attr;
1943 if (vc->vc_hi_font_mask == 0x100)
1944 vc->vc_def_color >>= 1;
1945 default_attr(vc);
1946 update_attr(vc);
1947 break;
1948 case 9: /* set blanking interval */
1949 blankinterval = min(vc->vc_par[1], 60U) * 60;
1950 poke_blanked_console();
1951 break;
1952 case 10: /* set bell frequency in Hz */
1953 if (vc->vc_npar >= 1)
1954 vc->vc_bell_pitch = vc->vc_par[1];
1955 else
1956 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1957 break;
1958 case 11: /* set bell duration in msec */
1959 if (vc->vc_npar >= 1)
1960 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1961 msecs_to_jiffies(vc->vc_par[1]) : 0;
1962 else
1963 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1964 break;
1965 case 12: /* bring specified console to the front */
1966 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1967 set_console(vc->vc_par[1] - 1);
1968 break;
1969 case 13: /* unblank the screen */
1970 poke_blanked_console();
1971 break;
1972 case 14: /* set vesa powerdown interval */
1973 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1974 break;
1975 case 15: /* activate the previous console */
1976 set_console(last_console);
1977 break;
1978 case 16: /* set cursor blink duration in msec */
1979 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1980 vc->vc_par[1] <= USHRT_MAX)
1981 vc->vc_cur_blink_ms = vc->vc_par[1];
1982 else
1983 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 }
1986}
1987
Torben Hohnac751ef2011-01-25 15:07:35 -08001988/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989static void csi_at(struct vc_data *vc, unsigned int nr)
1990{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001991 if (nr > vc->vc_cols - vc->state.x)
1992 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 else if (!nr)
1994 nr = 1;
1995 insert_char(vc, nr);
1996}
1997
Torben Hohnac751ef2011-01-25 15:07:35 -08001998/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999static void csi_L(struct vc_data *vc, unsigned int nr)
2000{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002001 if (nr > vc->vc_rows - vc->state.y)
2002 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 else if (!nr)
2004 nr = 1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002005 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 vc->vc_need_wrap = 0;
2007}
2008
Torben Hohnac751ef2011-01-25 15:07:35 -08002009/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010static void csi_P(struct vc_data *vc, unsigned int nr)
2011{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002012 if (nr > vc->vc_cols - vc->state.x)
2013 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 else if (!nr)
2015 nr = 1;
2016 delete_char(vc, nr);
2017}
2018
Torben Hohnac751ef2011-01-25 15:07:35 -08002019/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020static void csi_M(struct vc_data *vc, unsigned int nr)
2021{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002022 if (nr > vc->vc_rows - vc->state.y)
2023 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 else if (!nr)
2025 nr=1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002026 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 vc->vc_need_wrap = 0;
2028}
2029
Torben Hohnac751ef2011-01-25 15:07:35 -08002030/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031static void save_cur(struct vc_data *vc)
2032{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002033 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
Torben Hohnac751ef2011-01-25 15:07:35 -08002036/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037static void restore_cur(struct vc_data *vc)
2038{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002039 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
2040
2041 gotoxy(vc, vc->state.x, vc->state.y);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002042 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset],
2043 vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 update_attr(vc);
2045 vc->vc_need_wrap = 0;
2046}
2047
Adam Borowskib290af62014-02-19 01:40:16 +01002048enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Martin Hostettler7a995652018-12-15 15:34:22 +01002049 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01002050 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
Torben Hohnac751ef2011-01-25 15:07:35 -08002052/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053static void reset_terminal(struct vc_data *vc, int do_clear)
2054{
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002055 unsigned int i;
2056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 vc->vc_top = 0;
2058 vc->vc_bottom = vc->vc_rows;
2059 vc->vc_state = ESnormal;
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002060 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 vc->vc_translate = set_translate(LAT1_MAP, vc);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002062 vc->state.Gx_charset[0] = LAT1_MAP;
2063 vc->state.Gx_charset[1] = GRAF_MAP;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002064 vc->state.charset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 vc->vc_need_wrap = 0;
2066 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07002067 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 vc->vc_utf_count = 0;
2069
2070 vc->vc_disp_ctrl = 0;
2071 vc->vc_toggle_meta = 0;
2072
2073 vc->vc_decscnm = 0;
2074 vc->vc_decom = 0;
2075 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05002076 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 vc->vc_decim = 0;
2078
Alan Cox079c9532012-02-28 14:49:23 +00002079 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002081 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 vc->vc_complement_mask = vc->vc_s_complement_mask;
2083
2084 default_attr(vc);
2085 update_attr(vc);
2086
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002087 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
2088 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8)
2089 set_bit(i, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2092 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00002093 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 gotoxy(vc, 0, 0);
2096 save_cur(vc);
2097 if (do_clear)
2098 csi_J(vc, 2);
2099}
2100
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002101static void vc_setGx(struct vc_data *vc, unsigned int which, int c)
2102{
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002103 unsigned char *charset = &vc->state.Gx_charset[which];
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002104
2105 switch (c) {
2106 case '0':
2107 *charset = GRAF_MAP;
2108 break;
2109 case 'B':
2110 *charset = LAT1_MAP;
2111 break;
2112 case 'U':
2113 *charset = IBMPC_MAP;
2114 break;
2115 case 'K':
2116 *charset = USER_MAP;
2117 break;
2118 }
2119
2120 if (vc->state.charset == which)
2121 vc->vc_translate = set_translate(*charset, vc);
2122}
2123
Torben Hohnac751ef2011-01-25 15:07:35 -08002124/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2126{
2127 /*
2128 * Control characters can be used in the _middle_
2129 * of an escape sequence.
2130 */
Adam Borowski63f3a162014-02-19 01:38:04 +01002131 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 switch (c) {
2134 case 0:
2135 return;
2136 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01002137 if (vc->vc_state == ESosc)
2138 vc->vc_state = ESnormal;
2139 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2141 return;
2142 case 8:
2143 bs(vc);
2144 return;
2145 case 9:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002146 vc->vc_pos -= (vc->state.x << 1);
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002147
2148 vc->state.x = find_next_bit(vc->vc_tab_stop,
2149 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT),
2150 vc->state.x + 1);
2151 if (vc->state.x >= VC_TABSTOPS_COUNT)
2152 vc->state.x = vc->vc_cols - 1;
2153
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002154 vc->vc_pos += (vc->state.x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002155 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return;
2157 case 10: case 11: case 12:
2158 lf(vc);
2159 if (!is_kbd(vc, lnm))
2160 return;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002161 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 case 13:
2163 cr(vc);
2164 return;
2165 case 14:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002166 vc->state.charset = 1;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002167 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 vc->vc_disp_ctrl = 1;
2169 return;
2170 case 15:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002171 vc->state.charset = 0;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002172 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 vc->vc_disp_ctrl = 0;
2174 return;
2175 case 24: case 26:
2176 vc->vc_state = ESnormal;
2177 return;
2178 case 27:
2179 vc->vc_state = ESesc;
2180 return;
2181 case 127:
2182 del(vc);
2183 return;
2184 case 128+27:
2185 vc->vc_state = ESsquare;
2186 return;
2187 }
2188 switch(vc->vc_state) {
2189 case ESesc:
2190 vc->vc_state = ESnormal;
2191 switch (c) {
2192 case '[':
2193 vc->vc_state = ESsquare;
2194 return;
2195 case ']':
2196 vc->vc_state = ESnonstd;
2197 return;
2198 case '%':
2199 vc->vc_state = ESpercent;
2200 return;
2201 case 'E':
2202 cr(vc);
2203 lf(vc);
2204 return;
2205 case 'M':
2206 ri(vc);
2207 return;
2208 case 'D':
2209 lf(vc);
2210 return;
2211 case 'H':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002212 if (vc->state.x < VC_TABSTOPS_COUNT)
2213 set_bit(vc->state.x, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 return;
2215 case 'Z':
2216 respond_ID(tty);
2217 return;
2218 case '7':
2219 save_cur(vc);
2220 return;
2221 case '8':
2222 restore_cur(vc);
2223 return;
2224 case '(':
2225 vc->vc_state = ESsetG0;
2226 return;
2227 case ')':
2228 vc->vc_state = ESsetG1;
2229 return;
2230 case '#':
2231 vc->vc_state = EShash;
2232 return;
2233 case 'c':
2234 reset_terminal(vc, 1);
2235 return;
2236 case '>': /* Numeric keypad */
2237 clr_kbd(vc, kbdapplic);
2238 return;
2239 case '=': /* Appl. keypad */
2240 set_kbd(vc, kbdapplic);
2241 return;
2242 }
2243 return;
2244 case ESnonstd:
2245 if (c=='P') { /* palette escape sequence */
2246 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2247 vc->vc_par[vc->vc_npar] = 0;
2248 vc->vc_npar = 0;
2249 vc->vc_state = ESpalette;
2250 return;
2251 } else if (c=='R') { /* reset palette */
2252 reset_palette(vc);
2253 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01002254 } else if (c>='0' && c<='9')
2255 vc->vc_state = ESosc;
2256 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 vc->vc_state = ESnormal;
2258 return;
2259 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03002260 if (isxdigit(c)) {
2261 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (vc->vc_npar == 7) {
2263 int i = vc->vc_par[0] * 3, j = 1;
2264 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2265 vc->vc_palette[i++] += vc->vc_par[j++];
2266 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2267 vc->vc_palette[i++] += vc->vc_par[j++];
2268 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2269 vc->vc_palette[i] += vc->vc_par[j];
2270 set_palette(vc);
2271 vc->vc_state = ESnormal;
2272 }
2273 } else
2274 vc->vc_state = ESnormal;
2275 return;
2276 case ESsquare:
2277 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2278 vc->vc_par[vc->vc_npar] = 0;
2279 vc->vc_npar = 0;
2280 vc->vc_state = ESgetpars;
2281 if (c == '[') { /* Function key */
2282 vc->vc_state=ESfunckey;
2283 return;
2284 }
Martin Hostettler54454472018-12-15 15:34:21 +01002285 switch (c) {
2286 case '?':
2287 vc->vc_priv = EPdec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 return;
Martin Hostettler54454472018-12-15 15:34:21 +01002289 case '>':
2290 vc->vc_priv = EPgt;
2291 return;
2292 case '=':
2293 vc->vc_priv = EPeq;
2294 return;
2295 case '<':
2296 vc->vc_priv = EPlt;
2297 return;
2298 }
2299 vc->vc_priv = EPecma;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002300 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 case ESgetpars:
2302 if (c == ';' && vc->vc_npar < NPAR - 1) {
2303 vc->vc_npar++;
2304 return;
2305 } else if (c>='0' && c<='9') {
2306 vc->vc_par[vc->vc_npar] *= 10;
2307 vc->vc_par[vc->vc_npar] += c - '0';
2308 return;
Adam Borowskib290af62014-02-19 01:40:16 +01002309 }
Martin Hostettler04afcd32018-12-15 15:34:23 +01002310 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
Martin Hostettler7a995652018-12-15 15:34:22 +01002311 vc->vc_state = EScsiignore;
2312 return;
2313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 vc->vc_state = ESnormal;
2315 switch(c) {
2316 case 'h':
Martin Hostettler54454472018-12-15 15:34:21 +01002317 if (vc->vc_priv <= EPdec)
2318 set_mode(vc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return;
2320 case 'l':
Martin Hostettler54454472018-12-15 15:34:21 +01002321 if (vc->vc_priv <= EPdec)
2322 set_mode(vc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return;
2324 case 'c':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002325 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (vc->vc_par[0])
2327 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2328 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002329 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return;
2331 }
2332 break;
2333 case 'm':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002334 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 clear_selection();
2336 if (vc->vc_par[0])
2337 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2338 else
2339 vc->vc_complement_mask = vc->vc_s_complement_mask;
2340 return;
2341 }
2342 break;
2343 case 'n':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002344 if (vc->vc_priv == EPecma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 if (vc->vc_par[0] == 5)
2346 status_report(tty);
2347 else if (vc->vc_par[0] == 6)
2348 cursor_report(vc, tty);
2349 }
2350 return;
2351 }
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002352 if (vc->vc_priv != EPecma) {
2353 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 return;
2355 }
2356 switch(c) {
2357 case 'G': case '`':
2358 if (vc->vc_par[0])
2359 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002360 gotoxy(vc, vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return;
2362 case 'A':
2363 if (!vc->vc_par[0])
2364 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002365 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return;
2367 case 'B': case 'e':
2368 if (!vc->vc_par[0])
2369 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002370 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 return;
2372 case 'C': case 'a':
2373 if (!vc->vc_par[0])
2374 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002375 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return;
2377 case 'D':
2378 if (!vc->vc_par[0])
2379 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002380 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return;
2382 case 'E':
2383 if (!vc->vc_par[0])
2384 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002385 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 return;
2387 case 'F':
2388 if (!vc->vc_par[0])
2389 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002390 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return;
2392 case 'd':
2393 if (vc->vc_par[0])
2394 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002395 gotoxay(vc, vc->state.x ,vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 return;
2397 case 'H': case 'f':
2398 if (vc->vc_par[0])
2399 vc->vc_par[0]--;
2400 if (vc->vc_par[1])
2401 vc->vc_par[1]--;
2402 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2403 return;
2404 case 'J':
2405 csi_J(vc, vc->vc_par[0]);
2406 return;
2407 case 'K':
2408 csi_K(vc, vc->vc_par[0]);
2409 return;
2410 case 'L':
2411 csi_L(vc, vc->vc_par[0]);
2412 return;
2413 case 'M':
2414 csi_M(vc, vc->vc_par[0]);
2415 return;
2416 case 'P':
2417 csi_P(vc, vc->vc_par[0]);
2418 return;
2419 case 'c':
2420 if (!vc->vc_par[0])
2421 respond_ID(tty);
2422 return;
2423 case 'g':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002424 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT)
2425 set_bit(vc->state.x, vc->vc_tab_stop);
2426 else if (vc->vc_par[0] == 3)
2427 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 return;
2429 case 'm':
2430 csi_m(vc);
2431 return;
2432 case 'q': /* DECLL - but only 3 leds */
2433 /* map 0,1,2,3 to 0,1,2,4 */
2434 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002435 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2437 return;
2438 case 'r':
2439 if (!vc->vc_par[0])
2440 vc->vc_par[0]++;
2441 if (!vc->vc_par[1])
2442 vc->vc_par[1] = vc->vc_rows;
2443 /* Minimum allowed region is 2 lines */
2444 if (vc->vc_par[0] < vc->vc_par[1] &&
2445 vc->vc_par[1] <= vc->vc_rows) {
2446 vc->vc_top = vc->vc_par[0] - 1;
2447 vc->vc_bottom = vc->vc_par[1];
2448 gotoxay(vc, 0, 0);
2449 }
2450 return;
2451 case 's':
2452 save_cur(vc);
2453 return;
2454 case 'u':
2455 restore_cur(vc);
2456 return;
2457 case 'X':
2458 csi_X(vc, vc->vc_par[0]);
2459 return;
2460 case '@':
2461 csi_at(vc, vc->vc_par[0]);
2462 return;
2463 case ']': /* setterm functions */
2464 setterm_command(vc);
2465 return;
2466 }
2467 return;
Martin Hostettler7a995652018-12-15 15:34:22 +01002468 case EScsiignore:
2469 if (c >= 20 && c <= 0x3f)
2470 return;
2471 vc->vc_state = ESnormal;
2472 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 case ESpercent:
2474 vc->vc_state = ESnormal;
2475 switch (c) {
2476 case '@': /* defined in ISO 2022 */
2477 vc->vc_utf = 0;
2478 return;
2479 case 'G': /* prelim official escape code */
2480 case '8': /* retained for compatibility */
2481 vc->vc_utf = 1;
2482 return;
2483 }
2484 return;
2485 case ESfunckey:
2486 vc->vc_state = ESnormal;
2487 return;
2488 case EShash:
2489 vc->vc_state = ESnormal;
2490 if (c == '8') {
2491 /* DEC screen alignment test. kludge :-) */
2492 vc->vc_video_erase_char =
2493 (vc->vc_video_erase_char & 0xff00) | 'E';
2494 csi_J(vc, 2);
2495 vc->vc_video_erase_char =
2496 (vc->vc_video_erase_char & 0xff00) | ' ';
2497 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2498 }
2499 return;
2500 case ESsetG0:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002501 vc_setGx(vc, 0, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 vc->vc_state = ESnormal;
2503 return;
2504 case ESsetG1:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002505 vc_setGx(vc, 1, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 vc->vc_state = ESnormal;
2507 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002508 case ESosc:
2509 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 default:
2511 vc->vc_state = ESnormal;
2512 }
2513}
2514
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002515/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002516 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002517 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2518 */
2519struct interval {
2520 uint32_t first;
2521 uint32_t last;
2522};
2523
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002524static int ucs_cmp(const void *key, const void *elt)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002525{
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002526 uint32_t ucs = *(uint32_t *)key;
2527 struct interval e = *(struct interval *) elt;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002528
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002529 if (ucs > e.last)
2530 return 1;
2531 else if (ucs < e.first)
2532 return -1;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002533 return 0;
2534}
2535
2536static int is_double_width(uint32_t ucs)
2537{
2538 static const struct interval double_width[] = {
2539 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2540 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002541 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2542 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002543 };
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002544 if (ucs < double_width[0].first ||
2545 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2546 return 0;
2547
2548 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2549 sizeof(struct interval), ucs_cmp) != NULL;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002550}
2551
Jiri Slabyd711ea82016-06-23 13:34:30 +02002552static void con_flush(struct vc_data *vc, unsigned long draw_from,
2553 unsigned long draw_to, int *draw_x)
2554{
2555 if (*draw_x < 0)
2556 return;
2557
2558 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002559 (u16 *)draw_to - (u16 *)draw_from, vc->state.y, *draw_x);
Jiri Slabyd711ea82016-06-23 13:34:30 +02002560 *draw_x = -1;
2561}
2562
Jiri Slabya0181802020-06-15 09:48:42 +02002563static inline int vc_translate_ascii(const struct vc_data *vc, int c)
2564{
2565 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) {
2566 if (vc->vc_toggle_meta)
2567 c |= 0x80;
2568
2569 return vc->vc_translate[c];
2570 }
2571
2572 return c;
2573}
2574
Jiri Slabyede98d12020-06-15 09:48:45 +02002575
2576/**
2577 * vc_sanitize_unicode -- Replace invalid Unicode code points with U+FFFD
2578 * @c: the received character, or U+FFFD for invalid sequences.
2579 */
2580static inline int vc_sanitize_unicode(const int c)
2581{
2582 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2583 return 0xfffd;
2584
2585 return c;
2586}
2587
Jiri Slaby694d8a42020-06-15 09:48:44 +02002588/**
2589 * vc_translate_unicode -- Combine UTF-8 into Unicode in @vc_utf_char
2590 *
2591 * @vc_utf_char is the being-constructed unicode character.
2592 * @vc_utf_count is the number of continuation bytes still expected to arrive.
2593 * @vc_npar is the number of continuation bytes arrived so far.
2594 */
2595static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
2596{
2597 static const u32 utf8_length_changes[] = {
2598 0x0000007f, 0x000007ff, 0x0000ffff,
2599 0x001fffff, 0x03ffffff, 0x7fffffff
2600 };
2601
Jiri Slabyede98d12020-06-15 09:48:45 +02002602 /* Continuation byte received */
Jiri Slaby694d8a42020-06-15 09:48:44 +02002603 if ((c & 0xc0) == 0x80) {
Jiri Slabyede98d12020-06-15 09:48:45 +02002604 /* Unexpected continuation byte? */
2605 if (!vc->vc_utf_count)
Jiri Slaby694d8a42020-06-15 09:48:44 +02002606 return 0xfffd;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002607
Jiri Slabyede98d12020-06-15 09:48:45 +02002608 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2609 vc->vc_npar++;
2610 if (--vc->vc_utf_count)
2611 goto need_more_bytes;
2612
2613 /* Got a whole character */
2614 c = vc->vc_utf_char;
2615 /* Reject overlong sequences */
2616 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2617 c > utf8_length_changes[vc->vc_npar])
2618 return 0xfffd;
2619
2620 return vc_sanitize_unicode(c);
2621 }
2622
2623 /* Single ASCII byte or first byte of a sequence received */
2624 if (vc->vc_utf_count) {
2625 /* Continuation byte expected */
2626 *rescan = true;
2627 vc->vc_utf_count = 0;
2628 return 0xfffd;
2629 }
2630
2631 /* Nothing to do if an ASCII byte was received */
2632 if (c <= 0x7f)
2633 return c;
2634
2635 /* First byte of a multibyte sequence received */
2636 vc->vc_npar = 0;
2637 if ((c & 0xe0) == 0xc0) {
2638 vc->vc_utf_count = 1;
2639 vc->vc_utf_char = (c & 0x1f);
2640 } else if ((c & 0xf0) == 0xe0) {
2641 vc->vc_utf_count = 2;
2642 vc->vc_utf_char = (c & 0x0f);
2643 } else if ((c & 0xf8) == 0xf0) {
2644 vc->vc_utf_count = 3;
2645 vc->vc_utf_char = (c & 0x07);
2646 } else if ((c & 0xfc) == 0xf8) {
2647 vc->vc_utf_count = 4;
2648 vc->vc_utf_char = (c & 0x03);
2649 } else if ((c & 0xfe) == 0xfc) {
2650 vc->vc_utf_count = 5;
2651 vc->vc_utf_char = (c & 0x01);
2652 } else {
2653 /* 254 and 255 are invalid */
2654 return 0xfffd;
2655 }
2656
2657need_more_bytes:
2658 return -1;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002659}
2660
Torben Hohnac751ef2011-01-25 15:07:35 -08002661/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2663{
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002664 int c, next_c, tc, ok, n = 0, draw_x = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 unsigned int currcons;
2666 unsigned long draw_from = 0, draw_to = 0;
2667 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002668 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002669 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 u16 himask, charmask;
Jiri Slabyda823b22020-06-15 09:48:43 +02002671 u8 width;
2672 bool rescan;
2673 bool inverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 if (in_interrupt())
2676 return count;
2677
Torben Hohnac751ef2011-01-25 15:07:35 -08002678 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 vc = tty->driver_data;
2680 if (vc == NULL) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07002681 pr_err("vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002682 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
2684 }
2685
2686 currcons = vc->vc_num;
2687 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002688 /* could this happen? */
2689 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2690 console_unlock();
2691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 himask = vc->vc_hi_font_mask;
2695 charmask = himask ? 0x1ff : 0xff;
2696
2697 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002698 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 hide_cursor(vc);
2700
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002701 param.vc = vc;
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 while (!tty->stopped && count) {
2704 int orig = *buf;
2705 c = orig;
2706 buf++;
2707 n++;
2708 count--;
Jiri Slabyda823b22020-06-15 09:48:43 +02002709 rescan = false;
2710 inverse = false;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002711 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
2713 /* Do no translation at all in control states */
2714 if (vc->vc_state != ESnormal) {
2715 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002716 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Adam Tlalkad4328b42006-09-29 01:59:53 -07002717rescan_last_byte:
Jiri Slaby694d8a42020-06-15 09:48:44 +02002718 tc = c = vc_translate_unicode(vc, c, &rescan);
2719 if (tc == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 continue;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002721 } else { /* no utf or alternate charset mode */
Jiri Slabya0181802020-06-15 09:48:42 +02002722 tc = vc_translate_ascii(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
2724
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002725 param.c = tc;
2726 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2727 &param) == NOTIFY_STOP)
2728 continue;
2729
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 /* If the original code was a control character we
2731 * only allow a glyph to be displayed if the code is
2732 * not normally used (such as for cursor movement) or
2733 * if the disp_ctrl mode has been explicitly enabled.
2734 * Certain characters (as given by the CTRL_ALWAYS
2735 * bitmap) are always displayed as control characters,
2736 * as the console would be pretty useless without
2737 * them; to display an arbitrary font position use the
2738 * direct-to-font zone in UTF-8 mode.
2739 */
2740 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002741 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2742 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 && (c != 127 || vc->vc_disp_ctrl)
2744 && (c != 128+27);
2745
2746 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002747 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2748 if (is_double_width(c))
2749 width = 2;
2750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 /* Now try to find out how to display it */
2752 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002753 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002754 if (tc == -1 || tc == -2) {
2755 continue; /* nothing to display */
2756 }
2757 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002758 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002759 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002760 This would make absolutely no sense with Unicode in mind,
2761 but do this for ASCII characters since a font may lack
2762 Unicode mapping info and we don't want to end up with
2763 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002764 tc = c;
2765 } else {
2766 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2767 tc = conv_uni_to_pc(vc, 0xfffd);
2768 if (tc < 0) {
Jiri Slabyda823b22020-06-15 09:48:43 +02002769 inverse = true;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002770 tc = conv_uni_to_pc(vc, '?');
2771 if (tc < 0) tc = '?';
2772 }
2773 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002776 if (!inverse) {
2777 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002779 /* invert vc_attr */
2780 if (!vc->vc_can_do_color) {
2781 vc_attr = (vc->vc_attr) ^ 0x08;
2782 } else if (vc->vc_hi_font_mask == 0x100) {
2783 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2784 } else {
2785 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002786 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002787 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002788 }
2789
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002790 next_c = c;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002791 while (1) {
2792 if (vc->vc_need_wrap || vc->vc_decim)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002793 con_flush(vc, draw_from, draw_to,
2794 &draw_x);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002795 if (vc->vc_need_wrap) {
2796 cr(vc);
2797 lf(vc);
2798 }
2799 if (vc->vc_decim)
2800 insert_char(vc, 1);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002801 vc_uniscr_putc(vc, next_c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002802 scr_writew(himask ?
2803 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2804 (vc_attr << 8) + tc,
2805 (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002806 if (con_should_update(vc) && draw_x < 0) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002807 draw_x = vc->state.x;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002808 draw_from = vc->vc_pos;
2809 }
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002810 if (vc->state.x == vc->vc_cols - 1) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002811 vc->vc_need_wrap = vc->vc_decawm;
2812 draw_to = vc->vc_pos + 2;
2813 } else {
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002814 vc->state.x++;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002815 draw_to = (vc->vc_pos += 2);
2816 }
2817
2818 if (!--width) break;
2819
2820 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2821 if (tc < 0) tc = ' ';
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002822 next_c = ' ';
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002823 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002824 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002825
Jiri Slabyd711ea82016-06-23 13:34:30 +02002826 if (inverse)
2827 con_flush(vc, draw_from, draw_to, &draw_x);
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002828
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002829 if (rescan) {
Jiri Slabyda823b22020-06-15 09:48:43 +02002830 rescan = false;
2831 inverse = false;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002832 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002833 c = orig;
2834 goto rescan_last_byte;
2835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 continue;
2837 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002838 con_flush(vc, draw_from, draw_to, &draw_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 do_con_trol(tty, vc, orig);
2840 }
Jiri Slabyd711ea82016-06-23 13:34:30 +02002841 con_flush(vc, draw_from, draw_to, &draw_x);
Nicolas Pitre02240802018-07-17 21:02:41 -04002842 vc_uniscr_debug_check(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 console_conditional_schedule();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002844 notify_update(vc);
Nicolas Pitre7e1d2262019-01-08 22:55:00 -05002845 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
2849/*
2850 * This is the console switching callback.
2851 *
2852 * Doing console switching in a process context allows
2853 * us to do the switches asynchronously (needed when we want
2854 * to switch due to a keyboard interrupt). Synchronization
2855 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002856 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
David Howells65f27f32006-11-22 14:55:48 +00002858static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Torben Hohnac751ef2011-01-25 15:07:35 -08002860 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 if (want_console >= 0) {
2863 if (want_console != fg_console &&
2864 vc_cons_allocated(want_console)) {
2865 hide_cursor(vc_cons[fg_console].d);
2866 change_console(vc_cons[want_console].d);
2867 /* we only changed when the console had already
2868 been allocated - a new console is not created
2869 in an interrupt routine */
2870 }
2871 want_console = -1;
2872 }
2873 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2874 do_poke_blanked_console = 0;
2875 poke_blanked_console();
2876 }
2877 if (scrollback_delta) {
2878 struct vc_data *vc = vc_cons[fg_console].d;
2879 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002880 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2882 scrollback_delta = 0;
2883 }
2884 if (blank_timer_expired) {
2885 do_blank_screen(0);
2886 blank_timer_expired = 0;
2887 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002888 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
Torben Hohnac751ef2011-01-25 15:07:35 -08002890 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891}
2892
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002893int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002895 struct vc_data *vc = vc_cons[fg_console].d;
2896
2897 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2898 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2899
2900 /*
2901 * Console switch will fail in console_callback() or
2902 * change_console() so there is no point scheduling
2903 * the callback
2904 *
2905 * Existing set_console() users don't check the return
2906 * value so this shouldn't break anything
2907 */
2908 return -EINVAL;
2909 }
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 want_console = nr;
2912 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002913
2914 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
2917struct tty_driver *console_driver;
2918
2919#ifdef CONFIG_VT_CONSOLE
2920
Bernhard Walle5ada9182009-12-14 18:00:43 -08002921/**
2922 * vt_kmsg_redirect() - Sets/gets the kernel message console
2923 * @new: The new virtual terminal number or -1 if the console should stay
2924 * unchanged
2925 *
2926 * By default, the kernel messages are always printed on the current virtual
2927 * console. However, the user may modify that default with the
2928 * TIOCL_SETKMSGREDIRECT ioctl call.
2929 *
2930 * This function sets the kernel message console to be @new. It returns the old
2931 * virtual console number. The virtual terminal number 0 (both as parameter and
2932 * return value) means no redirection (i.e. always printed on the currently
2933 * active console).
2934 *
2935 * The parameter -1 means that only the current console is returned, but the
2936 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2937 * case to make the code more understandable.
2938 *
2939 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2940 * the parameter and always returns 0.
2941 */
2942int vt_kmsg_redirect(int new)
2943{
2944 static int kmsg_con;
2945
2946 if (new != -1)
2947 return xchg(&kmsg_con, new);
2948 else
2949 return kmsg_con;
2950}
2951
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952/*
2953 * Console on virtual terminal
2954 *
2955 * The console must be locked when we get here.
2956 */
2957
2958static void vt_console_print(struct console *co, const char *b, unsigned count)
2959{
2960 struct vc_data *vc = vc_cons[fg_console].d;
2961 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002962 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 const ushort *start;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05002964 ushort start_x, cnt;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002965 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002968 if (!printable)
2969 return;
2970 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 return;
2972
Bernhard Walle5ada9182009-12-14 18:00:43 -08002973 kmsg_console = vt_get_kmsg_redirect();
2974 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2975 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (!vc_cons_allocated(fg_console)) {
2978 /* impossible */
2979 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2980 goto quit;
2981 }
2982
Daniel Vetter8d7fc292018-08-22 10:54:03 +02002983 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 goto quit;
2985
2986 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002987 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 hide_cursor(vc);
2989
2990 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002991 start_x = vc->state.x;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05002992 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 while (count--) {
2994 c = *b++;
2995 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
Nicolas Pitre6609cff2019-01-08 22:54:59 -05002996 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002997 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Nicolas Pitre6609cff2019-01-08 22:54:59 -05002998 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 if (c == 8) { /* backspace */
3000 bs(vc);
3001 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003002 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 continue;
3004 }
3005 if (c != 13)
3006 lf(vc);
3007 cr(vc);
3008 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003009 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 if (c == 10 || c == 13)
3011 continue;
3012 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003013 vc_uniscr_putc(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003015 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 cnt++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003017 if (vc->state.x == vc->vc_cols - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 vc->vc_need_wrap = 1;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003019 } else {
3020 vc->vc_pos += 2;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003021 vc->state.x++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003024 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003025 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003027 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029quit:
Nick Pigginb0940002008-02-06 01:37:04 -08003030 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031}
3032
3033static struct tty_driver *vt_console_device(struct console *c, int *index)
3034{
3035 *index = c->index ? c->index-1 : fg_console;
3036 return console_driver;
3037}
3038
3039static struct console vt_console_driver = {
3040 .name = "tty",
3041 .write = vt_console_print,
3042 .device = vt_console_device,
3043 .unblank = unblank_screen,
3044 .flags = CON_PRINTBUFFER,
3045 .index = -1,
3046};
3047#endif
3048
3049/*
3050 * Handling of Linux-specific VC ioctls
3051 */
3052
3053/*
Torben Hohnac751ef2011-01-25 15:07:35 -08003054 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 *
3056 * There are some functions which don't need it.
3057 *
3058 * There are some functions which can sleep for arbitrary periods
3059 * (paste_selection) but we don't need the lock there anyway.
3060 *
Okash Khawaja496124e2019-04-17 13:21:13 +01003061 * set_selection_user has locking, and definitely needs it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 */
3063
3064int tioclinux(struct tty_struct *tty, unsigned long arg)
3065{
3066 char type, data;
3067 char __user *p = (char __user *)arg;
3068 int lines;
3069 int ret;
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3072 return -EPERM;
3073 if (get_user(type, p))
3074 return -EFAULT;
3075 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07003076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 switch (type)
3078 {
3079 case TIOCL_SETSEL:
Okash Khawaja496124e2019-04-17 13:21:13 +01003080 ret = set_selection_user((struct tiocl_selection
3081 __user *)(p+1), tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 break;
3083 case TIOCL_PASTESEL:
3084 ret = paste_selection(tty);
3085 break;
3086 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08003087 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08003089 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 break;
3091 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00003092 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00003094 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 break;
3096 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07003097
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 /*
3099 * Make it possible to react to Shift+Mousebutton.
3100 * Note that 'shift_state' is an undocumented
3101 * kernel-internal variable; programs not closely
3102 * related to the kernel should not use this.
3103 */
Alan Cox079c9532012-02-28 14:49:23 +00003104 data = vt_get_shift_state();
Adam Borowski6987dc82017-06-03 09:35:06 +02003105 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 break;
3107 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00003108 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00003110 console_unlock();
Adam Borowski6987dc82017-06-03 09:35:06 +02003111 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 break;
3113 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00003114 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003115 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00003116 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003118 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08003119 data = vt_get_kmsg_redirect();
Adam Borowski6987dc82017-06-03 09:35:06 +02003120 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003121 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 case TIOCL_SETKMSGREDIRECT:
3123 if (!capable(CAP_SYS_ADMIN)) {
3124 ret = -EPERM;
3125 } else {
3126 if (get_user(data, p+1))
3127 ret = -EFAULT;
3128 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08003129 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 }
3131 break;
3132 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00003133 /* No locking needed as this is a transiently
3134 correct return anyway if the caller hasn't
3135 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 ret = fg_console;
3137 break;
3138 case TIOCL_SCROLLCONSOLE:
3139 if (get_user(lines, (s32 __user *)(p+4))) {
3140 ret = -EFAULT;
3141 } else {
Alan Cox20f62572012-03-02 14:59:37 +00003142 /* Need the console lock here. Note that lots
3143 of other calls need fixing before the lock
3144 is actually useful ! */
3145 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00003147 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 ret = 0;
3149 }
3150 break;
3151 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08003152 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 ignore_poke = 1;
3154 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003155 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 break;
3157 case TIOCL_BLANKEDSCREEN:
3158 ret = console_blanked;
3159 break;
3160 default:
3161 ret = -EINVAL;
3162 break;
3163 }
3164 return ret;
3165}
3166
3167/*
3168 * /dev/ttyN handling
3169 */
3170
3171static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3172{
3173 int retval;
3174
3175 retval = do_con_write(tty, buf, count);
3176 con_flush_chars(tty);
3177
3178 return retval;
3179}
3180
Alan Cox5d19f542008-04-30 00:54:08 -07003181static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
3183 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07003184 return 0; /* n_r3964 calls put_char() from interrupt context */
3185 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186}
3187
3188static int con_write_room(struct tty_struct *tty)
3189{
3190 if (tty->stopped)
3191 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00003192 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
3195static int con_chars_in_buffer(struct tty_struct *tty)
3196{
3197 return 0; /* we're not buffering */
3198}
3199
3200/*
3201 * con_throttle and con_unthrottle are only used for
3202 * paste_selection(), which has to stuff in a large number of
3203 * characters...
3204 */
3205static void con_throttle(struct tty_struct *tty)
3206{
3207}
3208
3209static void con_unthrottle(struct tty_struct *tty)
3210{
3211 struct vc_data *vc = tty->driver_data;
3212
3213 wake_up_interruptible(&vc->paste_wait);
3214}
3215
3216/*
3217 * Turn the Scroll-Lock LED on when the tty is stopped
3218 */
3219static void con_stop(struct tty_struct *tty)
3220{
3221 int console_num;
3222 if (!tty)
3223 return;
3224 console_num = tty->index;
3225 if (!vc_cons_allocated(console_num))
3226 return;
Alan Cox079c9532012-02-28 14:49:23 +00003227 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
3230/*
3231 * Turn the Scroll-Lock LED off when the console is started
3232 */
3233static void con_start(struct tty_struct *tty)
3234{
3235 int console_num;
3236 if (!tty)
3237 return;
3238 console_num = tty->index;
3239 if (!vc_cons_allocated(console_num))
3240 return;
Alan Cox079c9532012-02-28 14:49:23 +00003241 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244static void con_flush_chars(struct tty_struct *tty)
3245{
3246 struct vc_data *vc;
3247
3248 if (in_interrupt()) /* from flush_to_ldisc */
3249 return;
3250
3251 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08003252 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 vc = tty->driver_data;
3254 if (vc)
3255 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08003256 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257}
3258
3259/*
3260 * Allocate the console screen memory.
3261 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003262static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
3264 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003265 struct vc_data *vc;
3266 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
Torben Hohnac751ef2011-01-25 15:07:35 -08003268 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003269 ret = vc_allocate(currcons);
3270 if (ret)
3271 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01003272
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003273 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003275 /* Still being freed */
3276 if (vc->port.tty) {
3277 ret = -ERESTARTSYS;
3278 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003280
3281 ret = tty_port_install(&vc->port, driver, tty);
3282 if (ret)
3283 goto unlock;
3284
3285 tty->driver_data = vc;
3286 vc->port.tty = tty;
Eric Biggersca4463b2020-03-21 20:43:04 -07003287 tty_port_get(&vc->port);
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003288
3289 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3290 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3291 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3292 }
3293 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01003294 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003295 else
Alan Coxadc8d742012-07-14 15:31:47 +01003296 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003297unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08003298 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 return ret;
3300}
3301
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003302static int con_open(struct tty_struct *tty, struct file *filp)
3303{
3304 /* everything done in install */
3305 return 0;
3306}
3307
3308
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309static void con_close(struct tty_struct *tty, struct file *filp)
3310{
Alan Coxfeebed62008-10-13 10:41:30 +01003311 /* Nothing to do - we defer to shutdown */
3312}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
Alan Coxfeebed62008-10-13 10:41:30 +01003314static void con_shutdown(struct tty_struct *tty)
3315{
3316 struct vc_data *vc = tty->driver_data;
3317 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08003318 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02003319 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08003320 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
Eric Biggersca4463b2020-03-21 20:43:04 -07003323static void con_cleanup(struct tty_struct *tty)
3324{
3325 struct vc_data *vc = tty->driver_data;
3326
3327 tty_port_put(&vc->port);
3328}
3329
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003330static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003331static int default_italic_color = 2; // green (ASCII)
3332static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003333module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003334module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3335module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3336
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337static void vc_init(struct vc_data *vc, unsigned int rows,
3338 unsigned int cols, int do_clear)
3339{
3340 int j, k ;
3341
3342 vc->vc_cols = cols;
3343 vc->vc_rows = rows;
3344 vc->vc_size_row = cols << 1;
3345 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3346
3347 set_origin(vc);
3348 vc->vc_pos = vc->vc_origin;
3349 reset_vc(vc);
3350 for (j=k=0; j<16; j++) {
3351 vc->vc_palette[k++] = default_red[j] ;
3352 vc->vc_palette[k++] = default_grn[j] ;
3353 vc->vc_palette[k++] = default_blu[j] ;
3354 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003355 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003356 vc->vc_ulcolor = default_underline_color;
3357 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 vc->vc_halfcolor = 0x08; /* grey */
3359 init_waitqueue_head(&vc->paste_wait);
3360 reset_terminal(vc, do_clear);
3361}
3362
3363/*
3364 * This routine initializes console interrupts, and does nothing
3365 * else. If you want the screen to clear, call tty_write with
3366 * the appropriate escape-sequence.
3367 */
3368
3369static int __init con_init(void)
3370{
3371 const char *display_desc = NULL;
3372 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003373 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
Torben Hohnac751ef2011-01-25 15:07:35 -08003375 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376
Arvind Sankar805ece22019-12-18 16:44:44 -05003377 if (!conswitchp)
3378 conswitchp = &dummy_con;
3379 display_desc = conswitchp->con_startup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (!display_desc) {
3381 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08003382 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return 0;
3384 }
3385
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003386 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3387 struct con_driver *con_driver = &registered_con_driver[i];
3388
3389 if (con_driver->con == NULL) {
3390 con_driver->con = conswitchp;
3391 con_driver->desc = display_desc;
3392 con_driver->flag = CON_DRIVER_FLAG_INIT;
3393 con_driver->first = 0;
3394 con_driver->last = MAX_NR_CONSOLES - 1;
3395 break;
3396 }
3397 }
3398
3399 for (i = 0; i < MAX_NR_CONSOLES; i++)
3400 con_driver_map[i] = conswitchp;
3401
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if (blankinterval) {
3403 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003404 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 }
3406
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003408 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003409 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003410 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003412 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 vc_init(vc, vc->vc_rows, vc->vc_cols,
3414 currcons || !vc->vc_sw->con_save_screen);
3415 }
3416 currcons = fg_console = 0;
3417 master_display_fg = vc = vc_cons[currcons].d;
3418 set_origin(vc);
3419 save_screen(vc);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003420 gotoxy(vc, vc->state.x, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 csi_J(vc, 0);
3422 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003423 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 vc->vc_can_do_color ? "colour" : "mono",
3425 display_desc, vc->vc_cols, vc->vc_rows);
3426 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003427
Torben Hohnac751ef2011-01-25 15:07:35 -08003428 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
3430#ifdef CONFIG_VT_CONSOLE
3431 register_console(&vt_console_driver);
3432#endif
3433 return 0;
3434}
3435console_initcall(con_init);
3436
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003437static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003438 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 .open = con_open,
3440 .close = con_close,
3441 .write = con_write,
3442 .write_room = con_write_room,
3443 .put_char = con_put_char,
3444 .flush_chars = con_flush_chars,
3445 .chars_in_buffer = con_chars_in_buffer,
3446 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003447#ifdef CONFIG_COMPAT
3448 .compat_ioctl = vt_compat_ioctl,
3449#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 .stop = con_stop,
3451 .start = con_start,
3452 .throttle = con_throttle,
3453 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003454 .resize = vt_resize,
Eric Biggersca4463b2020-03-21 20:43:04 -07003455 .shutdown = con_shutdown,
3456 .cleanup = con_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457};
3458
Alan Coxd81ed102008-10-13 10:41:42 +01003459static struct cdev vc0_cdev;
3460
Kay Sieversfbc92a32010-12-01 18:51:05 +01003461static ssize_t show_tty_active(struct device *dev,
3462 struct device_attribute *attr, char *buf)
3463{
3464 return sprintf(buf, "tty%d\n", fg_console + 1);
3465}
3466static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3467
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003468static struct attribute *vt_dev_attrs[] = {
3469 &dev_attr_active.attr,
3470 NULL
3471};
3472
3473ATTRIBUTE_GROUPS(vt_dev);
3474
Alan Coxd81ed102008-10-13 10:41:42 +01003475int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476{
Alan Coxd81ed102008-10-13 10:41:42 +01003477 cdev_init(&vc0_cdev, console_fops);
3478 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3479 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3480 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003481 tty0dev = device_create_with_groups(tty_class, NULL,
3482 MKDEV(TTY_MAJOR, 0), NULL,
3483 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003484 if (IS_ERR(tty0dev))
3485 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003486
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 vcs_init();
3488
3489 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3490 if (!console_driver)
3491 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003492
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 console_driver->name = "tty";
3494 console_driver->name_base = 1;
3495 console_driver->major = TTY_MAJOR;
3496 console_driver->minor_start = 1;
3497 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3498 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003499 if (default_utf8)
3500 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3502 tty_set_operations(console_driver, &con_ops);
3503 if (tty_register_driver(console_driver))
3504 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 kbd_init();
3506 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507#ifdef CONFIG_MDA_CONSOLE
3508 mda_console_init();
3509#endif
3510 return 0;
3511}
3512
3513#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003514
3515static struct class *vtconsole_class;
3516
Alan Cox50e244c2013-01-25 10:28:15 +10003517static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003518 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003520 struct module *owner = csw->owner;
3521 const char *desc = NULL;
3522 struct con_driver *con_driver;
3523 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 if (!try_module_get(owner))
3526 return -ENODEV;
3527
Alan Cox50e244c2013-01-25 10:28:15 +10003528 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003529
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003530 /* check if driver is registered */
3531 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3532 con_driver = &registered_con_driver[i];
3533
3534 if (con_driver->con == csw) {
3535 desc = con_driver->desc;
3536 retval = 0;
3537 break;
3538 }
3539 }
3540
3541 if (retval)
3542 goto err;
3543
3544 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3545 csw->con_startup();
3546 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 if (deflt) {
3550 if (conswitchp)
3551 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 __module_get(owner);
3554 conswitchp = csw;
3555 }
3556
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003557 first = max(first, con_driver->first);
3558 last = min(last, con_driver->last);
3559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 for (i = first; i <= last; i++) {
3561 int old_was_color;
3562 struct vc_data *vc = vc_cons[i].d;
3563
3564 if (con_driver_map[i])
3565 module_put(con_driver_map[i]->owner);
3566 __module_get(owner);
3567 con_driver_map[i] = csw;
3568
3569 if (!vc || !vc->vc_sw)
3570 continue;
3571
3572 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003573
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003574 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003575 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003577 }
3578
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 old_was_color = vc->vc_can_do_color;
3580 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003581 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003583 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 update_attr(vc);
3585
3586 /* If the console changed between mono <-> color, then
3587 * the attributes in the screenbuf will be wrong. The
3588 * following resets all attributes to something sane.
3589 */
3590 if (old_was_color != vc->vc_can_do_color)
3591 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003593
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003594 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 if (!deflt)
Joe Perchesbccf1da2017-10-02 08:48:55 -07003596 pr_cont("consoles %d-%d ", first + 1, last + 1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003597 if (j >= 0) {
3598 struct vc_data *vc = vc_cons[j].d;
3599
Joe Perchesbccf1da2017-10-02 08:48:55 -07003600 pr_cont("to %s %s %dx%d\n",
3601 vc->vc_can_do_color ? "colour" : "mono",
3602 desc, vc->vc_cols, vc->vc_rows);
David Hollister4ee1acc2006-06-26 00:26:41 -07003603
3604 if (k >= 0) {
3605 vc = vc_cons[k].d;
3606 update_screen(vc);
3607 }
Joe Perchesbccf1da2017-10-02 08:48:55 -07003608 } else {
3609 pr_cont("to %s\n", desc);
3610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003612 retval = 0;
3613err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003615 return retval;
3616};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Alan Cox50e244c2013-01-25 10:28:15 +10003618
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003619#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003620int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3621{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003622 struct module *owner = csw->owner;
3623 const struct consw *defcsw = NULL;
3624 struct con_driver *con_driver = NULL, *con_back = NULL;
3625 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003627 if (!try_module_get(owner))
3628 return -ENODEV;
3629
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003630 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003631
3632 /* check if driver is registered and if it is unbindable */
3633 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3634 con_driver = &registered_con_driver[i];
3635
3636 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003637 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003638 retval = 0;
3639 break;
3640 }
3641 }
3642
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003643 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003644 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003645
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003646 retval = -ENODEV;
3647
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003648 /* check if backup driver exists */
3649 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3650 con_back = &registered_con_driver[i];
3651
Daniel Vetter249f7b32014-06-05 16:24:47 +02003652 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003653 defcsw = con_back->con;
3654 retval = 0;
3655 break;
3656 }
3657 }
3658
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003659 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003660 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003661
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003662 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003663 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003664
3665 first = max(first, con_driver->first);
3666 last = min(last, con_driver->last);
3667
3668 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 if (con_driver_map[i] == csw) {
3670 module_put(csw->owner);
3671 con_driver_map[i] = NULL;
3672 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003673 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003674
3675 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003676 const struct consw *defconsw = conswitchp;
3677
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003678 defcsw->con_startup();
3679 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003680 /*
3681 * vgacon may change the default driver to point
3682 * to dummycon, we restore it here...
3683 */
3684 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003685 }
3686
3687 if (!con_is_bound(csw))
3688 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3689
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003690 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003691 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003692err:
3693 module_put(owner);
3694 return retval;
3695
3696}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003697EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003698
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003699static int vt_bind(struct con_driver *con)
3700{
3701 const struct consw *defcsw = NULL, *csw = NULL;
3702 int i, more = 1, first = -1, last = -1, deflt = 0;
3703
Daniel Vetter77232f72015-04-13 11:16:21 +02003704 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003705 goto err;
3706
3707 csw = con->con;
3708
3709 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3710 struct con_driver *con = &registered_con_driver[i];
3711
3712 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3713 defcsw = con->con;
3714 break;
3715 }
3716 }
3717
3718 if (!defcsw)
3719 goto err;
3720
3721 while (more) {
3722 more = 0;
3723
3724 for (i = con->first; i <= con->last; i++) {
3725 if (con_driver_map[i] == defcsw) {
3726 if (first == -1)
3727 first = i;
3728 last = i;
3729 more = 1;
3730 } else if (first != -1)
3731 break;
3732 }
3733
3734 if (first == 0 && last == MAX_NR_CONSOLES -1)
3735 deflt = 1;
3736
Imre Deak4c215fe2014-12-16 00:16:00 +02003737 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003738 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003739
3740 first = -1;
3741 last = -1;
3742 deflt = 0;
3743 }
3744
3745err:
3746 return 0;
3747}
3748
3749static int vt_unbind(struct con_driver *con)
3750{
3751 const struct consw *csw = NULL;
3752 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003753 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003754
Daniel Vetter77232f72015-04-13 11:16:21 +02003755 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003756 goto err;
3757
3758 csw = con->con;
3759
3760 while (more) {
3761 more = 0;
3762
3763 for (i = con->first; i <= con->last; i++) {
3764 if (con_driver_map[i] == csw) {
3765 if (first == -1)
3766 first = i;
3767 last = i;
3768 more = 1;
3769 } else if (first != -1)
3770 break;
3771 }
3772
3773 if (first == 0 && last == MAX_NR_CONSOLES -1)
3774 deflt = 1;
3775
Wang YanQing618f2b92013-05-09 02:14:07 +08003776 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003777 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003778 if (ret != 0)
3779 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003780 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003781
3782 first = -1;
3783 last = -1;
3784 deflt = 0;
3785 }
3786
3787err:
3788 return 0;
3789}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003790#else
3791static inline int vt_bind(struct con_driver *con)
3792{
3793 return 0;
3794}
3795static inline int vt_unbind(struct con_driver *con)
3796{
3797 return 0;
3798}
3799#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003800
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003801static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003802 const char *buf, size_t count)
3803{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003804 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003805 int bind = simple_strtoul(buf, NULL, 0);
3806
Imre Deak4c215fe2014-12-16 00:16:00 +02003807 console_lock();
3808
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003809 if (bind)
3810 vt_bind(con);
3811 else
3812 vt_unbind(con);
3813
Imre Deak4c215fe2014-12-16 00:16:00 +02003814 console_unlock();
3815
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003816 return count;
3817}
3818
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003819static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3820 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003821{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003822 struct con_driver *con = dev_get_drvdata(dev);
Daniel Vetter61d51452019-07-18 10:09:03 +02003823 int bind;
3824
3825 console_lock();
3826 bind = con_is_bound(con->con);
3827 console_unlock();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003828
3829 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3830}
3831
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003832static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3833 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003834{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003835 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003836
3837 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3838 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3839 con->desc);
3840
3841}
3842
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003843static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3844static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3845
3846static struct attribute *con_dev_attrs[] = {
3847 &dev_attr_bind.attr,
3848 &dev_attr_name.attr,
3849 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003850};
3851
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003852ATTRIBUTE_GROUPS(con_dev);
3853
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003854static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003855{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003856 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003857 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003858}
3859
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003860static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003861{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003862 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003863}
3864
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003865/**
3866 * con_is_bound - checks if driver is bound to the console
3867 * @csw: console driver
3868 *
3869 * RETURNS: zero if unbound, nonzero if bound
3870 *
3871 * Drivers can call this and if zero, they should release
3872 * all resources allocated on con_startup()
3873 */
3874int con_is_bound(const struct consw *csw)
3875{
3876 int i, bound = 0;
3877
Daniel Vetterddde3c12019-05-28 11:02:35 +02003878 WARN_CONSOLE_UNLOCKED();
3879
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003880 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3881 if (con_driver_map[i] == csw) {
3882 bound = 1;
3883 break;
3884 }
3885 }
3886
3887 return bound;
3888}
3889EXPORT_SYMBOL(con_is_bound);
3890
3891/**
Daniel Vetterddde3c12019-05-28 11:02:35 +02003892 * con_is_visible - checks whether the current console is visible
3893 * @vc: virtual console
3894 *
3895 * RETURNS: zero if not visible, nonzero if visible
3896 */
3897bool con_is_visible(const struct vc_data *vc)
3898{
3899 WARN_CONSOLE_UNLOCKED();
3900
3901 return *vc->vc_display_fg == vc;
3902}
3903EXPORT_SYMBOL(con_is_visible);
3904
3905/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003906 * con_debug_enter - prepare the console for the kernel debugger
3907 * @sw: console driver
3908 *
3909 * Called when the console is taken over by the kernel debugger, this
3910 * function needs to save the current console state, then put the console
3911 * into a state suitable for the kernel debugger.
3912 *
3913 * RETURNS:
3914 * Zero on success, nonzero if a failure occurred when trying to prepare
3915 * the console for the debugger.
3916 */
3917int con_debug_enter(struct vc_data *vc)
3918{
3919 int ret = 0;
3920
3921 saved_fg_console = fg_console;
3922 saved_last_console = last_console;
3923 saved_want_console = want_console;
3924 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003925 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003926 vc->vc_mode = KD_TEXT;
3927 console_blanked = 0;
3928 if (vc->vc_sw->con_debug_enter)
3929 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003930#ifdef CONFIG_KGDB_KDB
3931 /* Set the initial LINES variable if it is not already set */
3932 if (vc->vc_rows < 999) {
3933 int linecount;
3934 char lns[4];
3935 const char *setargs[3] = {
3936 "set",
3937 "LINES",
3938 lns,
3939 };
3940 if (kdbgetintenv(setargs[0], &linecount)) {
3941 snprintf(lns, 4, "%i", vc->vc_rows);
3942 kdb_set(2, setargs);
3943 }
3944 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003945 if (vc->vc_cols < 999) {
3946 int colcount;
3947 char cols[4];
3948 const char *setargs[3] = {
3949 "set",
3950 "COLUMNS",
3951 cols,
3952 };
3953 if (kdbgetintenv(setargs[0], &colcount)) {
3954 snprintf(cols, 4, "%i", vc->vc_cols);
3955 kdb_set(2, setargs);
3956 }
3957 }
Jason Wessel81d44502010-08-05 09:22:30 -05003958#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003959 return ret;
3960}
3961EXPORT_SYMBOL_GPL(con_debug_enter);
3962
3963/**
3964 * con_debug_leave - restore console state
3965 * @sw: console driver
3966 *
3967 * Restore the console state to what it was before the kernel debugger
3968 * was invoked.
3969 *
3970 * RETURNS:
3971 * Zero on success, nonzero if a failure occurred when trying to restore
3972 * the console.
3973 */
3974int con_debug_leave(void)
3975{
3976 struct vc_data *vc;
3977 int ret = 0;
3978
3979 fg_console = saved_fg_console;
3980 last_console = saved_last_console;
3981 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003982 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003983 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3984
3985 vc = vc_cons[fg_console].d;
3986 if (vc->vc_sw->con_debug_leave)
3987 ret = vc->vc_sw->con_debug_leave(vc);
3988 return ret;
3989}
3990EXPORT_SYMBOL_GPL(con_debug_leave);
3991
Alan Cox50e244c2013-01-25 10:28:15 +10003992static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003993{
3994 struct module *owner = csw->owner;
3995 struct con_driver *con_driver;
3996 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02003997 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003998
Alan Cox50e244c2013-01-25 10:28:15 +10003999 WARN_CONSOLE_UNLOCKED();
4000
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004001 if (!try_module_get(owner))
4002 return -ENODEV;
4003
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004004 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4005 con_driver = &registered_con_driver[i];
4006
4007 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02004008 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10004009 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02004010 goto err;
4011 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004012 }
4013
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004014 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02004015 if (!desc) {
4016 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004017 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02004018 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004019
4020 retval = -EINVAL;
4021
4022 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4023 con_driver = &registered_con_driver[i];
4024
Imre Deakd364b5c2015-04-01 21:06:16 +03004025 if (con_driver->con == NULL &&
4026 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004027 con_driver->con = csw;
4028 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004029 con_driver->node = i;
4030 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004031 CON_DRIVER_FLAG_INIT;
4032 con_driver->first = first;
4033 con_driver->last = last;
4034 retval = 0;
4035 break;
4036 }
4037 }
4038
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004039 if (retval)
4040 goto err;
4041
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004042 con_driver->dev =
4043 device_create_with_groups(vtconsole_class, NULL,
4044 MKDEV(0, con_driver->node),
4045 con_driver, con_dev_groups,
4046 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004047 if (IS_ERR(con_driver->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004048 pr_warn("Unable to create device for %s; errno = %ld\n",
4049 con_driver->desc, PTR_ERR(con_driver->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004050 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004051 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004052 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004053 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07004054
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004055err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004056 module_put(owner);
4057 return retval;
4058}
Alan Cox50e244c2013-01-25 10:28:15 +10004059
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004060
4061/**
Wang YanQing50539dd2013-05-09 02:14:44 +08004062 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004063 * @csw: console driver
4064 *
4065 * DESCRIPTION: All drivers that registers to the console layer must
4066 * call this function upon exit, or if the console driver is in a state
4067 * where it won't be able to handle console services, such as the
4068 * framebuffer console without loaded framebuffer drivers.
4069 *
4070 * The driver must unbind first prior to unregistration.
4071 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004072int do_unregister_con_driver(const struct consw *csw)
4073{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004074 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004075
4076 /* cannot unregister a bound driver */
4077 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004078 return -EBUSY;
4079
4080 if (csw == conswitchp)
4081 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004082
4083 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4084 struct con_driver *con_driver = &registered_con_driver[i];
4085
Imre Deak2cf30f72014-12-16 00:15:59 +02004086 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03004087 /*
4088 * Defer the removal of the sysfs entries since that
4089 * will acquire the kernfs s_active lock and we can't
4090 * acquire this lock while holding the console lock:
4091 * the unbind sysfs entry imposes already the opposite
4092 * order. Reset con already here to prevent any later
4093 * lookup to succeed and mark this slot as zombie, so
4094 * it won't get reused until we complete the removal
4095 * in the deferred work.
4096 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004097 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03004098 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4099 schedule_work(&con_driver_unregister_work);
4100
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004101 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004102 }
4103 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004104
4105 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004106}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004107EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004108
Imre Deakd364b5c2015-04-01 21:06:16 +03004109static void con_driver_unregister_callback(struct work_struct *ignored)
4110{
4111 int i;
4112
4113 console_lock();
4114
4115 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4116 struct con_driver *con_driver = &registered_con_driver[i];
4117
4118 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4119 continue;
4120
4121 console_unlock();
4122
4123 vtconsole_deinit_device(con_driver);
4124 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4125
4126 console_lock();
4127
4128 if (WARN_ON_ONCE(con_driver->con))
4129 con_driver->con = NULL;
4130 con_driver->desc = NULL;
4131 con_driver->dev = NULL;
4132 con_driver->node = 0;
4133 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4134 con_driver->flag = 0;
4135 con_driver->first = 0;
4136 con_driver->last = 0;
4137 }
4138
4139 console_unlock();
4140}
4141
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004142/*
4143 * If we support more console drivers, this function is used
4144 * when a driver wants to take over some existing consoles
4145 * and become default driver for newly opened ones.
4146 *
Lukas Wunner0095ab42020-01-09 13:59:21 +01004147 * do_take_over_console is basically a register followed by bind
Alan Cox50e244c2013-01-25 10:28:15 +10004148 */
4149int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4150{
4151 int err;
4152
4153 err = do_register_con_driver(csw, first, last);
4154 /*
4155 * If we get an busy error we still want to bind the console driver
4156 * and return success, as we may have unbound the console driver
4157 * but not unregistered it.
4158 */
4159 if (err == -EBUSY)
4160 err = 0;
4161 if (!err)
4162 do_bind_con_driver(csw, first, last, deflt);
4163
4164 return err;
4165}
4166EXPORT_SYMBOL_GPL(do_take_over_console);
4167
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004168
4169/*
4170 * give_up_console is a wrapper to unregister_con_driver. It will only
4171 * work if driver is fully unbound.
4172 */
4173void give_up_console(const struct consw *csw)
4174{
Wang YanQing70125e72013-05-09 02:14:39 +08004175 console_lock();
4176 do_unregister_con_driver(csw);
4177 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004178}
4179
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004180static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004181{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004182 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004183
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004184 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4185 if (IS_ERR(vtconsole_class)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004186 pr_warn("Unable to create vt console class; errno = %ld\n",
4187 PTR_ERR(vtconsole_class));
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004188 vtconsole_class = NULL;
4189 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004190
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004191 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004192 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4193 struct con_driver *con = &registered_con_driver[i];
4194
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004195 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004196 con->dev =
4197 device_create_with_groups(vtconsole_class, NULL,
4198 MKDEV(0, con->node),
4199 con, con_dev_groups,
4200 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004201
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004202 if (IS_ERR(con->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004203 pr_warn("Unable to create device for %s; errno = %ld\n",
4204 con->desc, PTR_ERR(con->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004205 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004206 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004207 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004208 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004209 }
4210 }
4211
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004212 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004213}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004214postcore_initcall(vtconsole_class_init);
4215
4216#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004217
4218/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 * Screen blanking
4220 */
4221
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004222static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004224 unsigned int mode;
4225
4226 if (get_user(mode, p + 1))
4227 return -EFAULT;
4228
4229 vesa_blank_mode = (mode < 4) ? mode : 0;
4230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231}
4232
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233void do_blank_screen(int entering_gfx)
4234{
4235 struct vc_data *vc = vc_cons[fg_console].d;
4236 int i;
4237
Daniel Vettera1355132019-05-28 11:02:34 +02004238 might_sleep();
4239
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240 WARN_CONSOLE_UNLOCKED();
4241
4242 if (console_blanked) {
4243 if (blank_state == blank_vesa_wait) {
4244 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08004245 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 }
4247 return;
4248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /* entering graphics mode? */
4251 if (entering_gfx) {
4252 hide_cursor(vc);
4253 save_screen(vc);
4254 vc->vc_sw->con_blank(vc, -1, 1);
4255 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07004256 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257 set_origin(vc);
4258 return;
4259 }
4260
izumib6e8f002007-07-17 04:05:49 -07004261 blank_state = blank_off;
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 /* don't blank graphics */
4264 if (vc->vc_mode != KD_TEXT) {
4265 console_blanked = fg_console + 1;
4266 return;
4267 }
4268
4269 hide_cursor(vc);
4270 del_timer_sync(&console_timer);
4271 blank_timer_expired = 0;
4272
4273 save_screen(vc);
4274 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08004275 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 console_blanked = fg_console + 1;
4277 if (i)
4278 set_origin(vc);
4279
4280 if (console_blank_hook && console_blank_hook(1))
4281 return;
4282
Ville Syrjalad060a322006-01-09 20:53:49 -08004283 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07004284 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 mod_timer(&console_timer, jiffies + vesa_off_interval);
4286 }
Alan Cox8b92e872009-09-19 13:13:24 -07004287 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288}
4289EXPORT_SYMBOL(do_blank_screen);
4290
4291/*
4292 * Called by timer as well as from vt_console_driver
4293 */
4294void do_unblank_screen(int leaving_gfx)
4295{
4296 struct vc_data *vc;
4297
4298 /* This should now always be called from a "sane" (read: can schedule)
4299 * context for the sake of the low level drivers, except in the special
4300 * case of oops_in_progress
4301 */
4302 if (!oops_in_progress)
4303 might_sleep();
4304
4305 WARN_CONSOLE_UNLOCKED();
4306
4307 ignore_poke = 0;
4308 if (!console_blanked)
4309 return;
4310 if (!vc_cons_allocated(fg_console)) {
4311 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08004312 pr_warn("unblank_screen: tty %d not allocated ??\n",
4313 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 return;
4315 }
4316 vc = vc_cons[fg_console].d;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004317 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 return; /* but leave console_blanked != 0 */
4319
4320 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004321 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 blank_state = blank_normal_wait;
4323 }
4324
4325 console_blanked = 0;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004326 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 /* Low-level driver cannot restore -> do it ourselves */
4328 update_screen(vc);
4329 if (console_blank_hook)
4330 console_blank_hook(0);
4331 set_palette(vc);
4332 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07004333 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334}
4335EXPORT_SYMBOL(do_unblank_screen);
4336
4337/*
4338 * This is called by the outside world to cause a forced unblank, mostly for
4339 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4340 * call it with 1 as an argument and so force a mode restore... that may kill
4341 * X or at least garbage the screen but would also make the Oops visible...
4342 */
4343void unblank_screen(void)
4344{
4345 do_unblank_screen(0);
4346}
4347
4348/*
Ingo Molnar70522e12006-03-23 03:00:31 -08004349 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08004351 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 */
Kees Cook24ed9602017-08-28 11:28:21 -07004353static void blank_screen_t(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 blank_timer_expired = 1;
4356 schedule_work(&console_work);
4357}
4358
4359void poke_blanked_console(void)
4360{
4361 WARN_CONSOLE_UNLOCKED();
4362
4363 /* Add this so we quickly catch whoever might call us in a non
4364 * safe context. Nowadays, unblank_screen() isn't to be called in
4365 * atomic contexts and is allowed to schedule (with the special case
4366 * of oops_in_progress, but that isn't of any concern for this
4367 * function. --BenH.
4368 */
4369 might_sleep();
4370
4371 /* This isn't perfectly race free, but a race here would be mostly harmless,
4372 * at worse, we'll do a spurrious blank and it's unlikely
4373 */
4374 del_timer(&console_timer);
4375 blank_timer_expired = 0;
4376
4377 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4378 return;
4379 if (console_blanked)
4380 unblank_screen();
4381 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004382 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 blank_state = blank_normal_wait;
4384 }
4385}
4386
4387/*
4388 * Palettes
4389 */
4390
4391static void set_palette(struct vc_data *vc)
4392{
4393 WARN_CONSOLE_UNLOCKED();
4394
Jiri Slaby709280d2016-06-23 13:34:27 +02004395 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 vc->vc_sw->con_set_palette(vc, color_table);
4397}
4398
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399/*
4400 * Load palette into the DAC registers. arg points to a colour
4401 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4402 */
4403
4404int con_set_cmap(unsigned char __user *arg)
4405{
Michael Gehring871bdea2012-03-21 01:26:45 +01004406 int i, j, k;
4407 unsigned char colormap[3*16];
4408
4409 if (copy_from_user(colormap, arg, sizeof(colormap)))
4410 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411
Torben Hohnac751ef2011-01-25 15:07:35 -08004412 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004413 for (i = k = 0; i < 16; i++) {
4414 default_red[i] = colormap[k++];
4415 default_grn[i] = colormap[k++];
4416 default_blu[i] = colormap[k++];
4417 }
4418 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4419 if (!vc_cons_allocated(i))
4420 continue;
4421 for (j = k = 0; j < 16; j++) {
4422 vc_cons[i].d->vc_palette[k++] = default_red[j];
4423 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4424 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4425 }
4426 set_palette(vc_cons[i].d);
4427 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004428 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429
Michael Gehring871bdea2012-03-21 01:26:45 +01004430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431}
4432
4433int con_get_cmap(unsigned char __user *arg)
4434{
Michael Gehring871bdea2012-03-21 01:26:45 +01004435 int i, k;
4436 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437
Torben Hohnac751ef2011-01-25 15:07:35 -08004438 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004439 for (i = k = 0; i < 16; i++) {
4440 colormap[k++] = default_red[i];
4441 colormap[k++] = default_grn[i];
4442 colormap[k++] = default_blu[i];
4443 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004444 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Michael Gehring871bdea2012-03-21 01:26:45 +01004446 if (copy_to_user(arg, colormap, sizeof(colormap)))
4447 return -EFAULT;
4448
4449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450}
4451
4452void reset_palette(struct vc_data *vc)
4453{
4454 int j, k;
4455 for (j=k=0; j<16; j++) {
4456 vc->vc_palette[k++] = default_red[j];
4457 vc->vc_palette[k++] = default_grn[j];
4458 vc->vc_palette[k++] = default_blu[j];
4459 }
4460 set_palette(vc);
4461}
4462
4463/*
4464 * Font switching
4465 *
4466 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4467 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4468 * depending on width) reserved for each character which is kinda wasty, but
4469 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004470 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 * format (maybe we should add a `fontoffset' field to the `display'
4472 * structure so we won't have to convert the fontdata all the time.
4473 * /Jes
4474 */
4475
4476#define max_font_size 65536
4477
4478static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4479{
4480 struct console_font font;
4481 int rc = -EINVAL;
4482 int c;
4483
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 if (op->data) {
4485 font.data = kmalloc(max_font_size, GFP_KERNEL);
4486 if (!font.data)
4487 return -ENOMEM;
4488 } else
4489 font.data = NULL;
4490
Torben Hohnac751ef2011-01-25 15:07:35 -08004491 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004492 if (vc->vc_mode != KD_TEXT)
4493 rc = -EINVAL;
4494 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 rc = vc->vc_sw->con_font_get(vc, &font);
4496 else
4497 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004498 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499
4500 if (rc)
4501 goto out;
4502
4503 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004504
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 if (op->data && font.charcount > op->charcount)
4506 rc = -ENOSPC;
4507 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4508 if (font.width > op->width || font.height > op->height)
4509 rc = -ENOSPC;
4510 } else {
4511 if (font.width != 8)
4512 rc = -EIO;
4513 else if ((op->height && font.height > op->height) ||
4514 font.height > 32)
4515 rc = -ENOSPC;
4516 }
4517 if (rc)
4518 goto out;
4519
4520 op->height = font.height;
4521 op->width = font.width;
4522 op->charcount = font.charcount;
4523
4524 if (op->data && copy_to_user(op->data, font.data, c))
4525 rc = -EFAULT;
4526
4527out:
4528 kfree(font.data);
4529 return rc;
4530}
4531
4532static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4533{
4534 struct console_font font;
4535 int rc = -EINVAL;
4536 int size;
4537
4538 if (vc->vc_mode != KD_TEXT)
4539 return -EINVAL;
4540 if (!op->data)
4541 return -EINVAL;
4542 if (op->charcount > 512)
4543 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 if (op->width <= 0 || op->width > 32 || op->height > 32)
4545 return -EINVAL;
4546 size = (op->width+7)/8 * 32 * op->charcount;
4547 if (size > max_font_size)
4548 return -ENOSPC;
Meng Xu8ffb8202017-10-04 10:38:37 -04004549
Julia Lawall9b71ca22010-05-26 14:42:11 -07004550 font.data = memdup_user(op->data, size);
4551 if (IS_ERR(font.data))
4552 return PTR_ERR(font.data);
Meng Xu8ffb8202017-10-04 10:38:37 -04004553
4554 if (!op->height) { /* Need to guess font height [compat] */
4555 int h, i;
4556 u8 *charmap = font.data;
4557
4558 /*
4559 * If from KDFONTOP ioctl, don't allow things which can be done
4560 * in userland,so that we can get rid of this soon
4561 */
4562 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4563 kfree(font.data);
4564 return -EINVAL;
4565 }
4566
4567 for (h = 32; h > 0; h--)
4568 for (i = 0; i < op->charcount; i++)
4569 if (charmap[32*i+h-1])
4570 goto nonzero;
4571
4572 kfree(font.data);
4573 return -EINVAL;
4574
4575 nonzero:
4576 op->height = h;
4577 }
4578
4579 font.charcount = op->charcount;
4580 font.width = op->width;
4581 font.height = op->height;
4582
Torben Hohnac751ef2011-01-25 15:07:35 -08004583 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004584 if (vc->vc_mode != KD_TEXT)
4585 rc = -EINVAL;
4586 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4588 else
4589 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004590 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 kfree(font.data);
4592 return rc;
4593}
4594
4595static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4596{
4597 struct console_font font = {.width = op->width, .height = op->height};
4598 char name[MAX_FONT_NAME];
4599 char *s = name;
4600 int rc;
4601
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603 if (!op->data)
4604 s = NULL;
4605 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4606 return -EFAULT;
4607 else
4608 name[MAX_FONT_NAME - 1] = 0;
4609
Torben Hohnac751ef2011-01-25 15:07:35 -08004610 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004611 if (vc->vc_mode != KD_TEXT) {
4612 console_unlock();
4613 return -EINVAL;
4614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615 if (vc->vc_sw->con_font_default)
4616 rc = vc->vc_sw->con_font_default(vc, &font, s);
4617 else
4618 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004619 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (!rc) {
4621 op->width = font.width;
4622 op->height = font.height;
4623 }
4624 return rc;
4625}
4626
4627static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4628{
4629 int con = op->height;
4630 int rc;
4631
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632
Torben Hohnac751ef2011-01-25 15:07:35 -08004633 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004634 if (vc->vc_mode != KD_TEXT)
4635 rc = -EINVAL;
4636 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 rc = -ENOSYS;
4638 else if (con < 0 || !vc_cons_allocated(con))
4639 rc = -ENOTTY;
4640 else if (con == vc->vc_num) /* nothing to do */
4641 rc = 0;
4642 else
4643 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004644 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 return rc;
4646}
4647
4648int con_font_op(struct vc_data *vc, struct console_font_op *op)
4649{
4650 switch (op->op) {
4651 case KD_FONT_OP_SET:
4652 return con_font_set(vc, op);
4653 case KD_FONT_OP_GET:
4654 return con_font_get(vc, op);
4655 case KD_FONT_OP_SET_DEFAULT:
4656 return con_font_default(vc, op);
4657 case KD_FONT_OP_COPY:
4658 return con_font_copy(vc, op);
4659 }
4660 return -ENOSYS;
4661}
4662
4663/*
4664 * Interface exported to selection and vcs.
4665 */
4666
4667/* used by selection */
4668u16 screen_glyph(struct vc_data *vc, int offset)
4669{
4670 u16 w = scr_readw(screenpos(vc, offset, 1));
4671 u16 c = w & 0xff;
4672
4673 if (w & vc->vc_hi_font_mask)
4674 c |= 0x100;
4675 return c;
4676}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004677EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678
Adam Borowski9bfdc262018-07-18 04:10:44 +02004679u32 screen_glyph_unicode(struct vc_data *vc, int n)
4680{
4681 struct uni_screen *uniscr = get_vc_uniscr(vc);
4682
4683 if (uniscr)
4684 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4685 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4686}
4687EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4688
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689/* used by vcs - note the word offset */
4690unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4691{
4692 return screenpos(vc, 2 * w_offset, viewed);
4693}
Samuel Thibault88867e32016-01-25 01:32:08 +01004694EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
4696void getconsxy(struct vc_data *vc, unsigned char *p)
4697{
Nicolas Pitre8a085492019-01-08 22:55:02 -05004698 /* clamp values if they don't fit */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02004699 p[0] = min(vc->state.x, 0xFFu);
4700 p[1] = min(vc->state.y, 0xFFu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701}
4702
4703void putconsxy(struct vc_data *vc, unsigned char *p)
4704{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004705 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 gotoxy(vc, p[0], p[1]);
4707 set_cursor(vc);
4708}
4709
4710u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4711{
4712 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4713 return softcursor_original;
4714 return scr_readw(org);
4715}
4716
4717void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4718{
4719 scr_writew(val, org);
4720 if ((unsigned long)org == vc->vc_pos) {
4721 softcursor_original = -1;
4722 add_softcursor(vc);
4723 }
4724}
4725
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004726void vcs_scr_updated(struct vc_data *vc)
4727{
4728 notify_update(vc);
4729}
4730
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004731void vc_scrolldelta_helper(struct vc_data *c, int lines,
4732 unsigned int rolled_over, void *base, unsigned int size)
4733{
4734 unsigned long ubase = (unsigned long)base;
Jiri Slaby210fd742016-10-03 11:18:36 +02004735 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4736 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4737 ptrdiff_t origin = (void *)c->vc_origin - base;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004738 int margin = c->vc_size_row * 4;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004739 int from, wrap, from_off, avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004740
4741 /* Turn scrollback off */
4742 if (!lines) {
4743 c->vc_visible_origin = c->vc_origin;
4744 return;
4745 }
4746
4747 /* Do we have already enough to allow jumping from 0 to the end? */
Jiri Slaby210fd742016-10-03 11:18:36 +02004748 if (rolled_over > scr_end + margin) {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004749 from = scr_end;
4750 wrap = rolled_over + c->vc_size_row;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004751 } else {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004752 from = 0;
4753 wrap = size;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004754 }
4755
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004756 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4757 avail = (origin - from + wrap) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004758
4759 /* Only a little piece would be left? Show all incl. the piece! */
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004760 if (avail < 2 * margin)
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004761 margin = 0;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004762 if (from_off < margin)
4763 from_off = 0;
4764 if (from_off > avail - margin)
4765 from_off = avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004766
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004767 c->vc_visible_origin = ubase + (from + from_off) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004768}
4769EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4770
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771/*
4772 * Visible symbols for modules
4773 */
4774
4775EXPORT_SYMBOL(color_table);
4776EXPORT_SYMBOL(default_red);
4777EXPORT_SYMBOL(default_grn);
4778EXPORT_SYMBOL(default_blu);
4779EXPORT_SYMBOL(update_region);
4780EXPORT_SYMBOL(redraw_screen);
4781EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782EXPORT_SYMBOL(fg_console);
4783EXPORT_SYMBOL(console_blank_hook);
4784EXPORT_SYMBOL(console_blanked);
4785EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004786EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788EXPORT_SYMBOL(give_up_console);
4789#endif