blob: 45d32844e61b15476ba2994ade73a1c5dfa977b4 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130/*
131 * Here is the default bell parameters: 750HZ, 1/8th of a second
132 */
133#define DEFAULT_BELL_PITCH 750
134#define DEFAULT_BELL_DURATION (HZ/8)
Scot Doylebd633642015-03-26 13:54:39 +0000135#define DEFAULT_CURSOR_BLINK_MS 200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137struct vc vc_cons [MAX_NR_CONSOLES];
138
139#ifndef VT_SINGLE_DRIVER
140static const struct consw *con_driver_map[MAX_NR_CONSOLES];
141#endif
142
143static int con_open(struct tty_struct *, struct file *);
144static void vc_init(struct vc_data *vc, unsigned int rows,
145 unsigned int cols, int do_clear);
146static void gotoxy(struct vc_data *vc, int new_x, int new_y);
147static void save_cur(struct vc_data *vc);
148static void reset_terminal(struct vc_data *vc, int do_clear);
149static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800150static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151static void set_cursor(struct vc_data *vc);
152static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000153static void console_callback(struct work_struct *ignored);
Imre Deakd364b5c2015-04-01 21:06:16 +0300154static void con_driver_unregister_callback(struct work_struct *ignored);
Kees Cook24ed9602017-08-28 11:28:21 -0700155static void blank_screen_t(struct timer_list *unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void set_palette(struct vc_data *vc);
157
Peter Hurley52c40fc2014-11-11 11:20:56 -0500158#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700161int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700162module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500163int global_cursor_default = -1;
164module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800166static int cur_default = CUR_DEFAULT;
167module_param(cur_default, int, S_IRUGO | S_IWUSR);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * ignore_poke: don't unblank the screen when things are typed. This is
171 * mainly for the privacy of braille terminal users.
172 */
173static int ignore_poke;
174
175int do_poke_blanked_console;
176int console_blanked;
177
178static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static int vesa_off_interval;
Tim Gardnera4199f52017-03-22 09:07:20 -0600180static int blankinterval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700181core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
David Howells65f27f32006-11-22 14:55:48 +0000183static DECLARE_WORK(console_work, console_callback);
Imre Deakd364b5c2015-04-01 21:06:16 +0300184static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186/*
187 * fg_console is the current virtual console,
188 * last_console is the last used one,
189 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500190 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 */
192int fg_console;
193int last_console;
194int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500195static int saved_fg_console;
196static int saved_last_console;
197static int saved_want_console;
198static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500199static int saved_console_blanked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201/*
202 * For each existing display, we have a pointer to console currently visible
203 * on that display, allowing consoles other than fg_console to be refreshed
204 * appropriately. Unless the low-level driver supplies its own display_fg
205 * variable, we use this one for the "master display".
206 */
207static struct vc_data *master_display_fg;
208
209/*
210 * Unfortunately, we need to delay tty echo when we're currently writing to the
211 * console since the code is (and always was) not re-entrant, so we schedule
212 * all flip requests to process context with schedule-task() and run it from
213 * console_callback().
214 */
215
216/*
217 * For the same reason, we defer scrollback to the console callback.
218 */
219static int scrollback_delta;
220
221/*
222 * Hook so that the power management routines can (un)blank
223 * the console on our behalf.
224 */
225int (*console_blank_hook)(int);
226
Kees Cook1d27e3e2017-10-04 16:27:04 -0700227static DEFINE_TIMER(console_timer, blank_screen_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static int blank_state;
229static int blank_timer_expired;
230enum {
231 blank_off = 0,
232 blank_normal_wait,
233 blank_vesa_wait,
234};
235
236/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100237 * /sys/class/tty/tty0/
238 *
239 * the attribute 'active' contains the name of the current vc
240 * console and it supports poll() to detect vc switches
241 */
242static struct device *tty0dev;
243
244/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700245 * Notifier list for console events.
246 */
247static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
248
249int register_vt_notifier(struct notifier_block *nb)
250{
251 return atomic_notifier_chain_register(&vt_notifier_list, nb);
252}
253EXPORT_SYMBOL_GPL(register_vt_notifier);
254
255int unregister_vt_notifier(struct notifier_block *nb)
256{
257 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
258}
259EXPORT_SYMBOL_GPL(unregister_vt_notifier);
260
261static void notify_write(struct vc_data *vc, unsigned int unicode)
262{
Mathias Krause502fcb72011-07-30 14:01:59 +0200263 struct vt_notifier_param param = { .vc = vc, .c = unicode };
Samuel Thibaultb293d752007-10-18 23:39:17 -0700264 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
265}
266
267static void notify_update(struct vc_data *vc)
268{
269 struct vt_notifier_param param = { .vc = vc };
270 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
271}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700272/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 * Low-Level Functions
274 */
275
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200276static inline bool con_is_fg(const struct vc_data *vc)
277{
278 return vc->vc_num == fg_console;
279}
280
281static inline bool con_should_update(const struct vc_data *vc)
282{
283 return con_is_visible(vc) && !console_blanked;
284}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285
286static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
287{
288 unsigned short *p;
289
290 if (!viewed)
291 p = (unsigned short *)(vc->vc_origin + offset);
292 else if (!vc->vc_sw->con_screen_pos)
293 p = (unsigned short *)(vc->vc_visible_origin + offset);
294 else
295 p = vc->vc_sw->con_screen_pos(vc, offset);
296 return p;
297}
298
Alan Coxe33ac1c2010-06-01 22:52:54 +0200299/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static inline void scrolldelta(int lines)
301{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200302 /* FIXME */
303 /* scrolldelta needs some kind of consistency lock, but the BKL was
304 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 scrollback_delta += lines;
306 schedule_console_callback();
307}
308
309void schedule_console_callback(void)
310{
311 schedule_work(&console_work);
312}
313
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400314/*
315 * Code to manage unicode-based screen buffers
316 */
317
318#ifdef NO_VC_UNI_SCREEN
319/* this disables and optimizes related code away at compile time */
320#define get_vc_uniscr(vc) NULL
321#else
322#define get_vc_uniscr(vc) vc->vc_uni_screen
323#endif
324
Nicolas Pitre02240802018-07-17 21:02:41 -0400325#define VC_UNI_SCREEN_DEBUG 0
326
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400327typedef uint32_t char32_t;
328
329/*
330 * Our screen buffer is preceded by an array of line pointers so that
331 * scrolling only implies some pointer shuffling.
332 */
333struct uni_screen {
334 char32_t *lines[0];
335};
336
337static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
338{
339 struct uni_screen *uniscr;
340 void *p;
341 unsigned int memsize, i;
342
343 /* allocate everything in one go */
344 memsize = cols * rows * sizeof(char32_t);
345 memsize += rows * sizeof(char32_t *);
Nicolas Pitre9a98e7a2020-03-28 22:25:11 -0400346 p = vmalloc(memsize);
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400347 if (!p)
348 return NULL;
349
350 /* initial line pointers */
351 uniscr = p;
352 p = uniscr->lines + rows;
353 for (i = 0; i < rows; i++) {
354 uniscr->lines[i] = p;
355 p += cols * sizeof(char32_t);
356 }
357 return uniscr;
358}
359
Nicolas Pitre57d38f22020-05-02 11:01:07 -0400360static void vc_uniscr_free(struct uni_screen *uniscr)
361{
362 vfree(uniscr);
363}
364
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400365static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
366{
Nicolas Pitre57d38f22020-05-02 11:01:07 -0400367 vc_uniscr_free(vc->vc_uni_screen);
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400368 vc->vc_uni_screen = new_uniscr;
369}
370
371static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
372{
373 struct uni_screen *uniscr = get_vc_uniscr(vc);
374
375 if (uniscr)
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200376 uniscr->lines[vc->state.y][vc->state.x] = uc;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400377}
378
379static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
380{
381 struct uni_screen *uniscr = get_vc_uniscr(vc);
382
383 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200384 char32_t *ln = uniscr->lines[vc->state.y];
385 unsigned int x = vc->state.x, cols = vc->vc_cols;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400386
387 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
388 memset32(&ln[x], ' ', nr);
389 }
390}
391
392static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
393{
394 struct uni_screen *uniscr = get_vc_uniscr(vc);
395
396 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200397 char32_t *ln = uniscr->lines[vc->state.y];
398 unsigned int x = vc->state.x, cols = vc->vc_cols;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400399
400 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
401 memset32(&ln[cols - nr], ' ', nr);
402 }
403}
404
405static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
406 unsigned int nr)
407{
408 struct uni_screen *uniscr = get_vc_uniscr(vc);
409
410 if (uniscr) {
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200411 char32_t *ln = uniscr->lines[vc->state.y];
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400412
413 memset32(&ln[x], ' ', nr);
414 }
415}
416
417static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
418 unsigned int nr)
419{
420 struct uni_screen *uniscr = get_vc_uniscr(vc);
421
422 if (uniscr) {
423 unsigned int cols = vc->vc_cols;
424
425 while (nr--)
426 memset32(uniscr->lines[y++], ' ', cols);
427 }
428}
429
430static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
431 enum con_scroll dir, unsigned int nr)
432{
433 struct uni_screen *uniscr = get_vc_uniscr(vc);
434
435 if (uniscr) {
Nicolas Pitred541ae42018-07-19 00:05:25 -0400436 unsigned int i, j, k, sz, d, clear;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400437
Nicolas Pitred541ae42018-07-19 00:05:25 -0400438 sz = b - t;
439 clear = b - nr;
440 d = nr;
441 if (dir == SM_DOWN) {
442 clear = t;
443 d = sz - nr;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400444 }
Nicolas Pitred541ae42018-07-19 00:05:25 -0400445 for (i = 0; i < gcd(d, sz); i++) {
446 char32_t *tmp = uniscr->lines[t + i];
447 j = i;
448 while (1) {
449 k = j + d;
450 if (k >= sz)
451 k -= sz;
452 if (k == i)
453 break;
454 uniscr->lines[t + j] = uniscr->lines[t + k];
455 j = k;
456 }
457 uniscr->lines[t + j] = tmp;
458 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400459 vc_uniscr_clear_lines(vc, clear, nr);
460 }
461}
462
463static void vc_uniscr_copy_area(struct uni_screen *dst,
464 unsigned int dst_cols,
465 unsigned int dst_rows,
466 struct uni_screen *src,
467 unsigned int src_cols,
468 unsigned int src_top_row,
469 unsigned int src_bot_row)
470{
471 unsigned int dst_row = 0;
472
473 if (!dst)
474 return;
475
476 while (src_top_row < src_bot_row) {
477 char32_t *src_line = src->lines[src_top_row];
478 char32_t *dst_line = dst->lines[dst_row];
479
480 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
481 if (dst_cols - src_cols)
482 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
483 src_top_row++;
484 dst_row++;
485 }
486 while (dst_row < dst_rows) {
487 char32_t *dst_line = dst->lines[dst_row];
488
489 memset32(dst_line, ' ', dst_cols);
490 dst_row++;
491 }
492}
493
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400494/*
495 * Called from vcs_read() to make sure unicode screen retrieval is possible.
496 * This will initialize the unicode screen buffer if not already done.
497 * This returns 0 if OK, or a negative error code otherwise.
498 * In particular, -ENODATA is returned if the console is not in UTF-8 mode.
499 */
500int vc_uniscr_check(struct vc_data *vc)
501{
502 struct uni_screen *uniscr;
503 unsigned short *p;
504 int x, y, mask;
505
506 if (__is_defined(NO_VC_UNI_SCREEN))
507 return -EOPNOTSUPP;
508
509 WARN_CONSOLE_UNLOCKED();
510
511 if (!vc->vc_utf)
512 return -ENODATA;
513
514 if (vc->vc_uni_screen)
515 return 0;
516
517 uniscr = vc_uniscr_alloc(vc->vc_cols, vc->vc_rows);
518 if (!uniscr)
519 return -ENOMEM;
520
521 /*
522 * Let's populate it initially with (imperfect) reverse translation.
523 * This is the next best thing we can do short of having it enabled
524 * from the start even when no users rely on this functionality. True
525 * unicode content will be available after a complete screen refresh.
526 */
527 p = (unsigned short *)vc->vc_origin;
528 mask = vc->vc_hi_font_mask | 0xff;
529 for (y = 0; y < vc->vc_rows; y++) {
530 char32_t *line = uniscr->lines[y];
531 for (x = 0; x < vc->vc_cols; x++) {
532 u16 glyph = scr_readw(p++) & mask;
533 line[x] = inverse_translate(vc, glyph, true);
534 }
535 }
536
537 vc->vc_uni_screen = uniscr;
538 return 0;
539}
540
541/*
542 * Called from vcs_read() to get the unicode data from the screen.
543 * This must be preceded by a successful call to vc_uniscr_check() once
544 * the console lock has been taken.
545 */
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400546void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400547 unsigned int row, unsigned int col, unsigned int nr)
548{
549 struct uni_screen *uniscr = get_vc_uniscr(vc);
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400550 int offset = row * vc->vc_size_row + col * 2;
551 unsigned long pos;
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400552
553 BUG_ON(!uniscr);
Nicolas Pitre708d0bf2018-06-26 23:56:42 -0400554
555 pos = (unsigned long)screenpos(vc, offset, viewed);
556 if (pos >= vc->vc_origin && pos < vc->vc_scr_end) {
557 /*
558 * Desired position falls in the main screen buffer.
559 * However the actual row/col might be different if
560 * scrollback is active.
561 */
562 row = (pos - vc->vc_origin) / vc->vc_size_row;
563 col = ((pos - vc->vc_origin) % vc->vc_size_row) / 2;
564 memcpy(dest, &uniscr->lines[row][col], nr * sizeof(char32_t));
565 } else {
566 /*
567 * Scrollback is active. For now let's simply backtranslate
568 * the screen glyphs until the unicode screen buffer does
569 * synchronize with console display drivers for a scrollback
570 * buffer of its own.
571 */
572 u16 *p = (u16 *)pos;
573 int mask = vc->vc_hi_font_mask | 0xff;
574 char32_t *uni_buf = dest;
575 while (nr--) {
576 u16 glyph = scr_readw(p++) & mask;
577 *uni_buf++ = inverse_translate(vc, glyph, true);
578 }
579 }
Nicolas Pitred21b0be2018-06-26 23:56:41 -0400580}
581
Nicolas Pitre02240802018-07-17 21:02:41 -0400582/* this is for validation and debugging only */
583static void vc_uniscr_debug_check(struct vc_data *vc)
584{
585 struct uni_screen *uniscr = get_vc_uniscr(vc);
586 unsigned short *p;
587 int x, y, mask;
588
589 if (!VC_UNI_SCREEN_DEBUG || !uniscr)
590 return;
591
592 WARN_CONSOLE_UNLOCKED();
593
594 /*
595 * Make sure our unicode screen translates into the same glyphs
596 * as the actual screen. This is brutal indeed.
597 */
598 p = (unsigned short *)vc->vc_origin;
599 mask = vc->vc_hi_font_mask | 0xff;
600 for (y = 0; y < vc->vc_rows; y++) {
601 char32_t *line = uniscr->lines[y];
602 for (x = 0; x < vc->vc_cols; x++) {
603 u16 glyph = scr_readw(p++) & mask;
604 char32_t uc = line[x];
605 int tc = conv_uni_to_pc(vc, uc);
606 if (tc == -4)
607 tc = conv_uni_to_pc(vc, 0xfffd);
608 if (tc == -4)
609 tc = conv_uni_to_pc(vc, '?');
610 if (tc != glyph)
611 pr_err_ratelimited(
612 "%s: mismatch at %d,%d: glyph=%#x tc=%#x\n",
613 __func__, x, y, glyph, tc);
614 }
615 }
616}
617
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400618
Jiri Slaby89765b92016-10-03 11:18:34 +0200619static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
620 enum con_scroll dir, unsigned int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Jiri Slaby89765b92016-10-03 11:18:34 +0200622 u16 *clear, *d, *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Jiri Slaby89765b92016-10-03 11:18:34 +0200624 if (t + nr >= b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 nr = b - t - 1;
626 if (b > vc->vc_rows || t >= b || nr < 1)
627 return;
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400628 vc_uniscr_scroll(vc, t, b, dir, nr);
Jiri Slaby89765b92016-10-03 11:18:34 +0200629 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return;
Jiri Slaby89765b92016-10-03 11:18:34 +0200631
632 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
633 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
634
635 if (dir == SM_UP) {
636 clear = s + (b - t - nr) * vc->vc_cols;
637 swap(s, d);
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Jiri Slaby89765b92016-10-03 11:18:34 +0200640 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
643static void do_update_region(struct vc_data *vc, unsigned long start, int count)
644{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 unsigned int xx, yy, offset;
646 u16 *p;
647
648 p = (u16 *) start;
649 if (!vc->vc_sw->con_getxy) {
650 offset = (start - vc->vc_origin) / 2;
651 xx = offset % vc->vc_cols;
652 yy = offset / vc->vc_cols;
653 } else {
654 int nxx, nyy;
655 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
656 xx = nxx; yy = nyy;
657 }
658 for(;;) {
659 u16 attrib = scr_readw(p) & 0xff00;
660 int startx = xx;
661 u16 *q = p;
662 while (xx < vc->vc_cols && count) {
663 if (attrib != (scr_readw(p) & 0xff00)) {
664 if (p > q)
665 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
666 startx = xx;
667 q = p;
668 attrib = scr_readw(p) & 0xff00;
669 }
670 p++;
671 xx++;
672 count--;
673 }
674 if (p > q)
675 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
676 if (!count)
677 break;
678 xx = 0;
679 yy++;
680 if (vc->vc_sw->con_getxy) {
681 p = (u16 *)start;
682 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
683 }
684 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685}
686
687void update_region(struct vc_data *vc, unsigned long start, int count)
688{
689 WARN_CONSOLE_UNLOCKED();
690
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200691 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 hide_cursor(vc);
693 do_update_region(vc, start, count);
694 set_cursor(vc);
695 }
696}
697
698/* Structure of attributes is hardware-dependent */
699
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200700static u8 build_attr(struct vc_data *vc, u8 _color,
Jiri Slaby77bc14f2020-06-15 09:48:35 +0200701 enum vc_intensity _intensity, bool _blink, bool _underline,
702 bool _reverse, bool _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
704 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700705 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
706 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/*
709 * ++roman: I completely changed the attribute format for monochrome
710 * mode (!can_do_color). The formerly used MDA (monochrome display
711 * adapter) format didn't allow the combination of certain effects.
712 * Now the attribute is just a bit vector:
713 * Bit 0..1: intensity (0..2)
714 * Bit 2 : underline
715 * Bit 3 : reverse
716 * Bit 7 : blink
717 */
718 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700719 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (!vc->vc_can_do_color)
721 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700722 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 (_underline ? 4 : 0) |
724 (_reverse ? 8 : 0) |
725 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700726 if (_italic)
727 a = (a & 0xF0) | vc->vc_itcolor;
728 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 a = (a & 0xf0) | vc->vc_ulcolor;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200730 else if (_intensity == VCI_HALF_BRIGHT)
Adam Borowski1c65a872017-06-03 09:08:25 +0200731 a = (a & 0xf0) | vc->vc_halfcolor;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (_reverse)
733 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
734 if (_blink)
735 a ^= 0x80;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200736 if (_intensity == VCI_BOLD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 a ^= 0x08;
738 if (vc->vc_hi_font_mask == 0x100)
739 a <<= 1;
740 return a;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
744static void update_attr(struct vc_data *vc)
745{
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200746 vc->vc_attr = build_attr(vc, vc->state.color, vc->state.intensity,
747 vc->state.blink, vc->state.underline,
748 vc->state.reverse ^ vc->vc_decscnm, vc->state.italic);
Jiri Slabyb84ae3d2020-06-15 09:48:34 +0200749 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color,
Jiri Slaby77bc14f2020-06-15 09:48:35 +0200750 VCI_NORMAL, vc->state.blink, false,
751 vc->vc_decscnm, false) << 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
754/* Note: inverting the screen twice should revert to the original state */
755void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
756{
757 unsigned short *p;
758
759 WARN_CONSOLE_UNLOCKED();
760
761 count /= 2;
762 p = screenpos(vc, offset, viewed);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200763 if (vc->vc_sw->con_invert_region) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 vc->vc_sw->con_invert_region(vc, p, count);
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 u16 *q = p;
767 int cnt = count;
768 u16 a;
769
770 if (!vc->vc_can_do_color) {
771 while (cnt--) {
772 a = scr_readw(q);
773 a ^= 0x0800;
774 scr_writew(a, q);
775 q++;
776 }
777 } else if (vc->vc_hi_font_mask == 0x100) {
778 while (cnt--) {
779 a = scr_readw(q);
780 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
781 scr_writew(a, q);
782 q++;
783 }
784 } else {
785 while (cnt--) {
786 a = scr_readw(q);
787 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
788 scr_writew(a, q);
789 q++;
790 }
791 }
792 }
Jiri Slaby6af39ed2016-06-23 13:34:29 +0200793
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200794 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 do_update_region(vc, (unsigned long) p, count);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500796 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
799/* used by selection: complement pointer position */
800void complement_pos(struct vc_data *vc, int offset)
801{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700802 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 static unsigned short old;
804 static unsigned short oldx, oldy;
805
806 WARN_CONSOLE_UNLOCKED();
807
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700808 if (old_offset != -1 && old_offset >= 0 &&
809 old_offset < vc->vc_screenbuf_size) {
810 scr_writew(old, screenpos(vc, old_offset, 1));
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200811 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 vc->vc_sw->con_putc(vc, old, oldy, oldx);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500813 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700815
816 old_offset = offset;
817
818 if (offset != -1 && offset >= 0 &&
819 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700821 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 p = screenpos(vc, offset, 1);
823 old = scr_readw(p);
824 new = old ^ vc->vc_complement_mask;
825 scr_writew(new, p);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200826 if (con_should_update(vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 oldx = (offset >> 1) % vc->vc_cols;
828 oldy = (offset >> 1) / vc->vc_cols;
829 vc->vc_sw->con_putc(vc, new, oldy, oldx);
830 }
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500831 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 }
833}
834
835static void insert_char(struct vc_data *vc, unsigned int nr)
836{
Jean-François Moine81732c32012-09-06 19:24:13 +0200837 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400839 vc_uniscr_insert(vc, nr);
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200840 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200841 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200843 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200844 do_update_region(vc, (unsigned long) p,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200845 vc->vc_cols - vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
848static void delete_char(struct vc_data *vc, unsigned int nr)
849{
Jean-François Moine81732c32012-09-06 19:24:13 +0200850 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Nicolas Pitred8ae7242018-06-26 23:56:40 -0400852 vc_uniscr_delete(vc, nr);
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200853 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->state.x - nr) * 2);
854 scr_memsetw(p + vc->vc_cols - vc->state.x - nr, vc->vc_video_erase_char,
Jean-François Moine81732c32012-09-06 19:24:13 +0200855 nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200857 if (con_should_update(vc))
Jean-François Moine81732c32012-09-06 19:24:13 +0200858 do_update_region(vc, (unsigned long) p,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200859 vc->vc_cols - vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
Melchior FRANZe882f712015-11-01 19:48:18 +0100862static int softcursor_original = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864static void add_softcursor(struct vc_data *vc)
865{
866 int i = scr_readw((u16 *) vc->vc_pos);
867 u32 type = vc->vc_cursor_type;
868
869 if (! (type & 0x10)) return;
870 if (softcursor_original != -1) return;
871 softcursor_original = i;
872 i |= ((type >> 8) & 0xff00 );
873 i ^= ((type) & 0xff00 );
874 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
875 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
876 scr_writew(i, (u16 *) vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200877 if (con_should_update(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200878 vc->vc_sw->con_putc(vc, i, vc->state.y, vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881static void hide_softcursor(struct vc_data *vc)
882{
883 if (softcursor_original != -1) {
884 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200885 if (con_should_update(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 vc->vc_sw->con_putc(vc, softcursor_original,
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200887 vc->state.y, vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 softcursor_original = -1;
889 }
890}
891
892static void hide_cursor(struct vc_data *vc)
893{
Jiri Slabydce05aa2020-02-19 08:39:43 +0100894 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 clear_selection();
Jiri Slabydce05aa2020-02-19 08:39:43 +0100896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 vc->vc_sw->con_cursor(vc, CM_ERASE);
898 hide_softcursor(vc);
899}
900
901static void set_cursor(struct vc_data *vc)
902{
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200903 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return;
905 if (vc->vc_deccm) {
Jiri Slabydce05aa2020-02-19 08:39:43 +0100906 if (vc_is_sel(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 clear_selection();
908 add_softcursor(vc);
909 if ((vc->vc_cursor_type & 0x0f) != 1)
910 vc->vc_sw->con_cursor(vc, CM_DRAW);
911 } else
912 hide_cursor(vc);
913}
914
915static void set_origin(struct vc_data *vc)
916{
917 WARN_CONSOLE_UNLOCKED();
918
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200919 if (!con_is_visible(vc) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 !vc->vc_sw->con_set_origin ||
921 !vc->vc_sw->con_set_origin(vc))
922 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
923 vc->vc_visible_origin = vc->vc_origin;
924 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
Jiri Slaby28bc24f2020-06-15 09:48:33 +0200925 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y +
926 2 * vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927}
928
Denys Vlasenkoc0f160a2015-10-27 18:46:47 +0100929static void save_screen(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
931 WARN_CONSOLE_UNLOCKED();
932
933 if (vc->vc_sw->con_save_screen)
934 vc->vc_sw->con_save_screen(vc);
935}
936
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100937static void flush_scrollback(struct vc_data *vc)
938{
939 WARN_CONSOLE_UNLOCKED();
940
Nicolas Pitrea6dbe442019-02-11 19:36:41 -0500941 set_origin(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500942 if (vc->vc_sw->con_flush_scrollback) {
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100943 vc->vc_sw->con_flush_scrollback(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500944 } else if (con_is_visible(vc)) {
945 /*
946 * When no con_flush_scrollback method is provided then the
947 * legacy way for flushing the scrollback buffer is to use
948 * a side effect of the con_switch method. We do it only on
949 * the foreground console as background consoles have no
950 * scrollback buffers in that case and we obviously don't
951 * want to switch to them.
952 */
953 hide_cursor(vc);
Nicolas Pitrea6dbe442019-02-11 19:36:41 -0500954 vc->vc_sw->con_switch(vc);
Nicolas Pitre3f4ef482020-01-28 12:50:33 -0500955 set_cursor(vc);
956 }
Manuel Schöllingbcd375f2017-01-13 21:07:56 +0100957}
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * Redrawing of screen
961 */
962
Dave Airlie2a248302013-01-24 14:14:19 +1000963void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
965 unsigned short *p = (unsigned short *)vc->vc_origin;
966 int count = vc->vc_screenbuf_size / 2;
967 int mask = vc->vc_hi_font_mask | 0xff;
968
969 for (; count > 0; count--, p++) {
970 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
971 }
972}
973
974void redraw_screen(struct vc_data *vc, int is_switch)
975{
976 int redraw = 0;
977
978 WARN_CONSOLE_UNLOCKED();
979
980 if (!vc) {
981 /* strange ... */
982 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
983 return;
984 }
985
986 if (is_switch) {
987 struct vc_data *old_vc = vc_cons[fg_console].d;
988 if (old_vc == vc)
989 return;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200990 if (!con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 redraw = 1;
992 *vc->vc_display_fg = vc;
993 fg_console = vc->vc_num;
994 hide_cursor(old_vc);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +0200995 if (!con_is_visible(old_vc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 save_screen(old_vc);
997 set_origin(old_vc);
998 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100999 if (tty0dev)
1000 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 } else {
1002 hide_cursor(vc);
1003 redraw = 1;
1004 }
1005
1006 if (redraw) {
1007 int update;
1008 int old_was_color = vc->vc_can_do_color;
1009
1010 set_origin(vc);
1011 update = vc->vc_sw->con_switch(vc);
1012 set_palette(vc);
1013 /*
1014 * If console changed from mono<->color, the best we can do
1015 * is to clear the buffer attributes. As it currently stands,
1016 * rebuilding new attributes from the old buffer is not doable
1017 * without overly complex code.
1018 */
1019 if (old_was_color != vc->vc_can_do_color) {
1020 update_attr(vc);
1021 clear_buffer_attributes(vc);
1022 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07001023
Daniel Vetter8d7fc292018-08-22 10:54:03 +02001024 if (update && vc->vc_mode != KD_GRAPHICS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
1026 }
1027 set_cursor(vc);
1028 if (is_switch) {
1029 set_leds();
1030 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -08001031 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033}
1034
1035/*
1036 * Allocation, freeing and resizing of VTs.
1037 */
1038
1039int vc_cons_allocated(unsigned int i)
1040{
1041 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
1042}
1043
1044static void visual_init(struct vc_data *vc, int num, int init)
1045{
1046 /* ++Geert: vc->vc_sw->con_init determines console size */
1047 if (vc->vc_sw)
1048 module_put(vc->vc_sw->owner);
1049 vc->vc_sw = conswitchp;
1050#ifndef VT_SINGLE_DRIVER
1051 if (con_driver_map[num])
1052 vc->vc_sw = con_driver_map[num];
1053#endif
1054 __module_get(vc->vc_sw->owner);
1055 vc->vc_num = num;
1056 vc->vc_display_fg = &master_display_fg;
Dongxing Zhang08b33242015-06-10 15:21:10 +08001057 if (vc->vc_uni_pagedir_loc)
1058 con_free_unimap(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +02001060 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 vc->vc_hi_font_mask = 0;
1062 vc->vc_complement_mask = 0;
1063 vc->vc_can_do_color = 0;
David Daney1b459962016-05-17 11:41:04 -07001064 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 vc->vc_sw->con_init(vc, init);
1066 if (!vc->vc_complement_mask)
1067 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
1068 vc->vc_s_complement_mask = vc->vc_complement_mask;
1069 vc->vc_size_row = vc->vc_cols << 1;
1070 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
1071}
1072
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001073
1074static void visual_deinit(struct vc_data *vc)
1075{
1076 vc->vc_sw->con_deinit(vc);
1077 module_put(vc->vc_sw->owner);
1078}
1079
Eric Biggersca4463b2020-03-21 20:43:04 -07001080static void vc_port_destruct(struct tty_port *port)
1081{
1082 struct vc_data *vc = container_of(port, struct vc_data, port);
1083
1084 kfree(vc);
1085}
1086
1087static const struct tty_port_operations vc_port_ops = {
1088 .destruct = vc_port_destruct,
1089};
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091int vc_allocate(unsigned int currcons) /* return 0 on success */
1092{
Jiri Slaby34902b72016-03-31 10:08:15 +02001093 struct vt_notifier_param param;
1094 struct vc_data *vc;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 WARN_CONSOLE_UNLOCKED();
1097
1098 if (currcons >= MAX_NR_CONSOLES)
1099 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jiri Slaby34902b72016-03-31 10:08:15 +02001101 if (vc_cons[currcons].d)
1102 return 0;
1103
1104 /* due to the granularity of kmalloc, we waste some memory here */
1105 /* the alloc is done in two steps, to optimize the common situation
1106 of a 25x80 console (structsize=216, screenbuf_size=4000) */
1107 /* although the numbers above are not valid since long ago, the
1108 point is still up-to-date and the comment still has its value
1109 even if only as a historical artifact. --mj, July 1998 */
1110 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
1111 if (!vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 return -ENOMEM;
Jiri Slaby34902b72016-03-31 10:08:15 +02001113
1114 vc_cons[currcons].d = vc;
1115 tty_port_init(&vc->port);
Eric Biggersca4463b2020-03-21 20:43:04 -07001116 vc->port.ops = &vc_port_ops;
Jiri Slaby34902b72016-03-31 10:08:15 +02001117 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1118
1119 visual_init(vc, currcons, 1);
1120
1121 if (!*vc->vc_uni_pagedir_loc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 con_set_default_unimap(vc);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001123
Alexander Potapenko21eff692018-06-14 12:23:09 +02001124 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Jiri Slaby34902b72016-03-31 10:08:15 +02001125 if (!vc->vc_screenbuf)
1126 goto err_free;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001127
Jiri Slaby34902b72016-03-31 10:08:15 +02001128 /* If no drivers have overridden us and the user didn't pass a
1129 boot option, default to displaying the cursor */
1130 if (global_cursor_default == -1)
1131 global_cursor_default = 1;
1132
1133 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
1134 vcs_make_sysfs(currcons);
1135 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 return 0;
Jiri Slaby34902b72016-03-31 10:08:15 +02001138err_free:
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001139 visual_deinit(vc);
Jiri Slaby34902b72016-03-31 10:08:15 +02001140 kfree(vc);
1141 vc_cons[currcons].d = NULL;
1142 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001145static inline int resize_screen(struct vc_data *vc, int width, int height,
1146 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 /* Resizes the resolution of the display adapater */
1149 int err = 0;
1150
1151 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001152 err = vc->vc_sw->con_resize(vc, width, height, user);
1153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return err;
1155}
1156
1157/*
1158 * Change # of rows and columns (0 means unchanged/the size of fg_console)
1159 * [this is to be used together with some user program
1160 * like resize that changes the hardware videomode]
1161 */
1162#define VC_RESIZE_MAXCOL (32767)
1163#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001164
1165/**
1166 * vc_do_resize - resizing method for the tty
1167 * @tty: tty being resized
1168 * @real_tty: real tty (different to tty if a pty/tty pair)
1169 * @vc: virtual console private data
1170 * @cols: columns
1171 * @lines: lines
1172 *
1173 * Resize a virtual console, clipping according to the actual constraints.
1174 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +08001175 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001176 *
Peter Hurley6a1c0682013-06-15 09:14:23 -04001177 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001178 * ctrl_lock of the tty IFF a tty is passed.
1179 */
1180
Alan Coxfc6f6232009-01-02 13:43:17 +00001181static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1182 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -07001185 unsigned long end;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001186 unsigned int old_rows, old_row_size, first_copied_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -07001188 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 unsigned short *newscreen;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001190 struct uni_screen *new_uniscr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 WARN_CONSOLE_UNLOCKED();
1193
1194 if (!vc)
1195 return -ENXIO;
1196
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001197 user = vc->vc_resize_user;
1198 vc->vc_resize_user = 0;
1199
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1201 return -EINVAL;
1202
1203 new_cols = (cols ? cols : vc->vc_cols);
1204 new_rows = (lines ? lines : vc->vc_rows);
1205 new_row_size = new_cols << 1;
1206 new_screen_size = new_row_size * new_rows;
1207
1208 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1209 return 0;
1210
Nicolas Pitre27177692020-03-28 17:32:42 -04001211 if (new_screen_size > KMALLOC_MAX_SIZE)
Dmitry Vyukov32b29212016-10-14 15:18:28 +02001212 return -EINVAL;
Alexander Potapenko21eff692018-06-14 12:23:09 +02001213 newscreen = kzalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 if (!newscreen)
1215 return -ENOMEM;
1216
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001217 if (get_vc_uniscr(vc)) {
1218 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1219 if (!new_uniscr) {
1220 kfree(newscreen);
1221 return -ENOMEM;
1222 }
1223 }
1224
Jiri Slabydce05aa2020-02-19 08:39:43 +01001225 if (vc_is_sel(vc))
Scot Doyle009e39a2016-10-13 12:12:43 -05001226 clear_selection();
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Antonino A. Daplase400b6e2007-10-16 01:29:35 -07001231 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 if (err) {
1233 kfree(newscreen);
Nicolas Pitre57d38f22020-05-02 11:01:07 -04001234 vc_uniscr_free(new_uniscr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return err;
1236 }
1237
1238 vc->vc_rows = new_rows;
1239 vc->vc_cols = new_cols;
1240 vc->vc_size_row = new_row_size;
1241 vc->vc_screenbuf_size = new_screen_size;
1242
1243 rlth = min(old_row_size, new_row_size);
1244 rrem = new_row_size - rlth;
1245 old_origin = vc->vc_origin;
1246 new_origin = (long) newscreen;
1247 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001248
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001249 if (vc->state.y > new_rows) {
1250 if (old_rows - vc->state.y < new_rows) {
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001251 /*
1252 * Cursor near the bottom, copy contents from the
1253 * bottom of buffer
1254 */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001255 first_copied_row = (old_rows - new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001256 } else {
1257 /*
1258 * Cursor is in no man's land, copy 1/2 screenful
1259 * from the top and bottom of cursor position
1260 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001261 first_copied_row = (vc->state.y - new_rows/2);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001262 }
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001263 old_origin += first_copied_row * old_row_size;
1264 } else
1265 first_copied_row = 0;
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02001266 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001268 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1269 get_vc_uniscr(vc), rlth/2, first_copied_row,
1270 min(old_rows, new_rows));
1271 vc_uniscr_set(vc, new_uniscr);
1272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 update_attr(vc);
1274
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001275 while (old_origin < end) {
1276 scr_memcpyw((unsigned short *) new_origin,
1277 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001279 scr_memsetw((void *)(new_origin + rlth),
1280 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 old_origin += old_row_size;
1282 new_origin += new_row_size;
1283 }
1284 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -07001285 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1286 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001287 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 vc->vc_screenbuf_size = new_screen_size;
1290 set_origin(vc);
1291
1292 /* do part of a reset_terminal() */
1293 vc->vc_top = 0;
1294 vc->vc_bottom = vc->vc_rows;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001295 gotoxy(vc, vc->state.x, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 save_cur(vc);
1297
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001298 if (tty) {
1299 /* Rewrite the requested winsize data with the actual
1300 resulting sizes */
1301 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 memset(&ws, 0, sizeof(ws));
1303 ws.ws_row = vc->vc_rows;
1304 ws.ws_col = vc->vc_cols;
1305 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +00001306 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001309 if (con_is_visible(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07001311 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Nicolas Pitre0c9b1962019-01-08 22:55:01 -05001312 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return err;
1314}
1315
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001316/**
1317 * vc_resize - resize a VT
1318 * @vc: virtual console
1319 * @cols: columns
1320 * @rows: rows
1321 *
1322 * Resize a virtual console as seen from the console end of things. We
1323 * use the common vc_do_resize methods to update the structures. The
1324 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +02001325 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001326 */
1327
1328int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -07001329{
Alan Cox8ce73262010-06-01 22:52:56 +02001330 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001331}
1332
1333/**
1334 * vt_resize - resize a VT
1335 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001336 * @ws: winsize attributes
1337 *
1338 * Resize a virtual terminal. This is called by the tty layer as we
1339 * register our own handler for resizing. The mutual helper does all
1340 * the actual work.
1341 *
1342 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -04001343 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001344 */
Roel Kluinda2bdf92009-01-07 18:09:15 -08001345static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001346{
1347 struct vc_data *vc = tty->driver_data;
1348 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Torben Hohnac751ef2011-01-25 15:07:35 -08001350 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001351 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001352 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001353 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001354}
1355
Peter Hurley421b40a2013-05-17 12:41:03 -04001356struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Peter Hurley421b40a2013-05-17 12:41:03 -04001358 struct vc_data *vc = NULL;
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 WARN_CONSOLE_UNLOCKED();
1361
1362 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001363 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001364
Peter Hurley421b40a2013-05-17 12:41:03 -04001365 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001366 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001367 vcs_remove_sysfs(currcons);
Grzegorz Halata1ad1cc2019-04-26 16:59:46 +02001368 visual_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001369 put_pid(vc->vt_pid);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001370 vc_uniscr_set(vc, NULL);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001371 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 vc_cons[currcons].d = NULL;
1373 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001374 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375}
1376
1377/*
1378 * VT102 emulator
1379 */
1380
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01001381enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
1382
Alan Cox079c9532012-02-28 14:49:23 +00001383#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1384#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1385#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387#define decarm VC_REPEAT
1388#define decckm VC_CKMODE
1389#define kbdapplic VC_APPLIC
1390#define lnm VC_CRLF
1391
Jiri Slaby8ede5cc2016-03-31 10:08:16 +02001392const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 8,12,10,14, 9,13,11,15 };
1394
1395/* the default colour table, for VGA+ colour systems */
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001396unsigned char default_red[] = {
1397 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1398 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1399};
1400module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Jiri Slaby91e74ca2016-03-31 10:08:17 +02001402unsigned char default_grn[] = {
1403 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1404 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1405};
1406module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1407
1408unsigned char default_blu[] = {
1409 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1410 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1411};
1412module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414/*
1415 * gotoxy() must verify all boundaries, because the arguments
1416 * might also be negative. If the given position is out of
1417 * bounds, the cursor is placed at the nearest margin.
1418 */
1419static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1420{
1421 int min_y, max_y;
1422
1423 if (new_x < 0)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001424 vc->state.x = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 else {
1426 if (new_x >= vc->vc_cols)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001427 vc->state.x = vc->vc_cols - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 else
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001429 vc->state.x = new_x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 if (vc->vc_decom) {
1433 min_y = vc->vc_top;
1434 max_y = vc->vc_bottom;
1435 } else {
1436 min_y = 0;
1437 max_y = vc->vc_rows;
1438 }
1439 if (new_y < min_y)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001440 vc->state.y = min_y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 else if (new_y >= max_y)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001442 vc->state.y = max_y - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 else
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001444 vc->state.y = new_y;
1445 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row +
1446 (vc->state.x << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 vc->vc_need_wrap = 0;
1448}
1449
1450/* for absolute user moves, when decom is set */
1451static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1452{
1453 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1454}
1455
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001456void scrollback(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
Jiri Slaby1b0ec882016-06-23 13:34:23 +02001458 scrolldelta(-(vc->vc_rows / 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461void scrollfront(struct vc_data *vc, int lines)
1462{
1463 if (!lines)
1464 lines = vc->vc_rows / 2;
1465 scrolldelta(lines);
1466}
1467
1468static void lf(struct vc_data *vc)
1469{
1470 /* don't scroll if above bottom of scrolling region, or
1471 * if below scrolling region
1472 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001473 if (vc->state.y + 1 == vc->vc_bottom)
Jiri Slaby89765b92016-10-03 11:18:34 +02001474 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001475 else if (vc->state.y < vc->vc_rows - 1) {
1476 vc->state.y++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477 vc->vc_pos += vc->vc_size_row;
1478 }
1479 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001480 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481}
1482
1483static void ri(struct vc_data *vc)
1484{
1485 /* don't scroll if below top of scrolling region, or
1486 * if above scrolling region
1487 */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001488 if (vc->state.y == vc->vc_top)
Jiri Slaby89765b92016-10-03 11:18:34 +02001489 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001490 else if (vc->state.y > 0) {
1491 vc->state.y--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 vc->vc_pos -= vc->vc_size_row;
1493 }
1494 vc->vc_need_wrap = 0;
1495}
1496
1497static inline void cr(struct vc_data *vc)
1498{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001499 vc->vc_pos -= vc->state.x << 1;
1500 vc->vc_need_wrap = vc->state.x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001501 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502}
1503
1504static inline void bs(struct vc_data *vc)
1505{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001506 if (vc->state.x) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 vc->vc_pos -= 2;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001508 vc->state.x--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001510 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512}
1513
1514static inline void del(struct vc_data *vc)
1515{
1516 /* ignored */
1517}
1518
1519static void csi_J(struct vc_data *vc, int vpar)
1520{
1521 unsigned int count;
1522 unsigned short * start;
1523
1524 switch (vpar) {
1525 case 0: /* erase from cursor to end of display */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001526 vc_uniscr_clear_line(vc, vc->state.x,
1527 vc->vc_cols - vc->state.x);
1528 vc_uniscr_clear_lines(vc, vc->state.y + 1,
1529 vc->vc_rows - vc->state.y - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1531 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 break;
1533 case 1: /* erase from start to cursor */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001534 vc_uniscr_clear_line(vc, 0, vc->state.x + 1);
1535 vc_uniscr_clear_lines(vc, 0, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1537 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 break;
Nicolas Pitrea6dbe442019-02-11 19:36:41 -05001539 case 3: /* include scrollback */
1540 flush_scrollback(vc);
1541 /* fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 case 2: /* erase whole display */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001543 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 count = vc->vc_cols * vc->vc_rows;
1545 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 break;
1547 default:
1548 return;
1549 }
1550 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Mikulas Patockab2ecf002019-04-04 20:53:28 -04001551 if (con_should_update(vc))
1552 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 vc->vc_need_wrap = 0;
1554}
1555
1556static void csi_K(struct vc_data *vc, int vpar)
1557{
1558 unsigned int count;
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001559 unsigned short *start = (unsigned short *)vc->vc_pos;
1560 int offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 switch (vpar) {
1563 case 0: /* erase from cursor to end of line */
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001564 offset = 0;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001565 count = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 break;
1567 case 1: /* erase from start of line to cursor */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001568 offset = -vc->state.x;
1569 count = vc->state.x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 break;
1571 case 2: /* erase whole line */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001572 offset = -vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 break;
1575 default:
1576 return;
1577 }
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001578 vc_uniscr_clear_line(vc, vc->state.x + offset, count);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04001579 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 vc->vc_need_wrap = 0;
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001581 if (con_should_update(vc))
Mikulas Patocka943210b2018-10-23 11:28:28 -04001582 do_update_region(vc, (unsigned long)(start + offset), count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583}
1584
1585static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1586{ /* not vt100? */
1587 int count;
1588
1589 if (!vpar)
1590 vpar++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001591 count = (vpar > vc->vc_cols - vc->state.x) ? (vc->vc_cols - vc->state.x) : vpar;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001593 vc_uniscr_clear_line(vc, vc->state.x, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001595 if (con_should_update(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001596 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 vc->vc_need_wrap = 0;
1598}
1599
1600static void default_attr(struct vc_data *vc)
1601{
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001602 vc->state.intensity = VCI_NORMAL;
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001603 vc->state.italic = false;
1604 vc->state.underline = false;
1605 vc->state.reverse = false;
1606 vc->state.blink = false;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001607 vc->state.color = vc->vc_def_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001610struct rgb { u8 r; u8 g; u8 b; };
1611
Jiri Slaby0f91e142016-06-23 13:34:32 +02001612static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001613{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001614 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001615 c->r = i&1 ? 0xaa : 0x00;
1616 c->g = i&2 ? 0xaa : 0x00;
1617 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001618 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001619 c->r = i&1 ? 0xff : 0x55;
1620 c->g = i&2 ? 0xff : 0x55;
1621 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001622 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001623 c->r = (i - 16) / 36 * 85 / 2;
1624 c->g = (i - 16) / 6 % 6 * 85 / 2;
1625 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001626 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001627 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001628}
1629
Jiri Slaby0f91e142016-06-23 13:34:32 +02001630static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001631{
Jiri Slaby193df022016-06-23 13:34:33 +02001632 u8 hue = 0, max = max3(c->r, c->g, c->b);
1633
1634 if (c->r > max / 2)
1635 hue |= 4;
1636 if (c->g > max / 2)
1637 hue |= 2;
1638 if (c->b > max / 2)
1639 hue |= 1;
1640
1641 if (hue == 7 && max <= 0x55) {
1642 hue = 0;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001643 vc->state.intensity = VCI_BOLD;
Jiri Slaby193df022016-06-23 13:34:33 +02001644 } else if (max > 0xaa)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001645 vc->state.intensity = VCI_BOLD;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001646 else
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001647 vc->state.intensity = VCI_NORMAL;
Jiri Slaby193df022016-06-23 13:34:33 +02001648
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001649 vc->state.color = (vc->state.color & 0xf0) | hue;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001650}
1651
Jiri Slaby0f91e142016-06-23 13:34:32 +02001652static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001653{
1654 /* For backgrounds, err on the dark side. */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001655 vc->state.color = (vc->state.color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001656 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001657}
1658
Jiri Slabye05ab232016-06-23 13:34:31 +02001659/*
1660 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
Martin Hostettler04afcd32018-12-15 15:34:23 +01001661 * and thus need to be detected and ignored by hand. That standard also
1662 * wants : rather than ; as separators but sequences containing : are currently
1663 * completely ignored by the parser.
Jiri Slabye05ab232016-06-23 13:34:31 +02001664 *
1665 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1666 * supporting them.
1667 */
1668static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001669 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001670{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001671 struct rgb c;
1672
Jiri Slabye05ab232016-06-23 13:34:31 +02001673 i++;
1674 if (i > vc->vc_npar)
1675 return i;
1676
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001677 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001678 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001679 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001680 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001681 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001682 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001683 c.r = vc->vc_par[i + 1];
1684 c.g = vc->vc_par[i + 2];
1685 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001686 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001687 } else
1688 return i;
1689
1690 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001691
1692 return i;
1693}
1694
Torben Hohnac751ef2011-01-25 15:07:35 -08001695/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696static void csi_m(struct vc_data *vc)
1697{
1698 int i;
1699
1700 for (i = 0; i <= vc->vc_npar; i++)
1701 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001702 case 0: /* all attributes off */
1703 default_attr(vc);
1704 break;
1705 case 1:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001706 vc->state.intensity = VCI_BOLD;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001707 break;
1708 case 2:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001709 vc->state.intensity = VCI_HALF_BRIGHT;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001710 break;
1711 case 3:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001712 vc->state.italic = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001713 break;
Mike Frysinger65d99822018-01-29 17:08:21 -05001714 case 21:
1715 /*
1716 * No console drivers support double underline, so
1717 * convert it to a single underline.
1718 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001719 case 4:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001720 vc->state.underline = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001721 break;
1722 case 5:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001723 vc->state.blink = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001724 break;
1725 case 7:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001726 vc->state.reverse = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001727 break;
1728 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1729 * Select primary font, don't display control chars if
1730 * defined, don't set bit 8 on output.
1731 */
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02001732 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001733 vc->vc_disp_ctrl = 0;
1734 vc->vc_toggle_meta = 0;
1735 break;
1736 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1737 * Select first alternate font, lets chars < 32 be
1738 * displayed as ROM chars.
1739 */
1740 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1741 vc->vc_disp_ctrl = 1;
1742 vc->vc_toggle_meta = 0;
1743 break;
1744 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1745 * Select second alternate font, toggle high bit
1746 * before displaying as ROM char.
1747 */
1748 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1749 vc->vc_disp_ctrl = 1;
1750 vc->vc_toggle_meta = 1;
1751 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001752 case 22:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001753 vc->state.intensity = VCI_NORMAL;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001754 break;
1755 case 23:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001756 vc->state.italic = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001757 break;
1758 case 24:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001759 vc->state.underline = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001760 break;
1761 case 25:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001762 vc->state.blink = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001763 break;
1764 case 27:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001765 vc->state.reverse = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001766 break;
1767 case 38:
1768 i = vc_t416_color(vc, i, rgb_foreground);
1769 break;
1770 case 48:
1771 i = vc_t416_color(vc, i, rgb_background);
1772 break;
1773 case 39:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001774 vc->state.color = (vc->vc_def_color & 0x0f) |
1775 (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001776 break;
1777 case 49:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001778 vc->state.color = (vc->vc_def_color & 0xf0) |
1779 (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001780 break;
1781 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001782 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1783 if (vc->vc_par[i] < 100)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001784 vc->state.intensity = VCI_BOLD;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001785 vc->vc_par[i] -= 60;
1786 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001787 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001788 vc->state.color = color_table[vc->vc_par[i] - 30]
1789 | (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001790 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001791 vc->state.color = (color_table[vc->vc_par[i] - 40] << 4)
1792 | (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001793 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 }
1795 update_attr(vc);
1796}
1797
Jiri Slabyde53ce02020-06-15 09:48:40 +02001798static void respond_string(const char *p, size_t len, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001800 tty_insert_flip_string(port, p, len);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001801 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
1804static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1805{
1806 char buf[40];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001807 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Jiri Slabyde53ce02020-06-15 09:48:40 +02001809 len = sprintf(buf, "\033[%d;%dR", vc->state.y +
1810 (vc->vc_decom ? vc->vc_top + 1 : 1),
1811 vc->state.x + 1);
1812 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
1815static inline void status_report(struct tty_struct *tty)
1816{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001817 static const char teminal_ok[] = "\033[0n";
1818
1819 respond_string(teminal_ok, strlen(teminal_ok), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001822static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Jiri Slaby9a6f72d2020-06-15 09:48:41 +02001824 /* terminal answer to an ESC-Z or csi0c query. */
1825 static const char vt102_id[] = "\033[?6c";
1826
1827 respond_string(vt102_id, strlen(vt102_id), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1831{
1832 char buf[8];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001833 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
Jiri Slabyde53ce02020-06-15 09:48:40 +02001835 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt),
1836 (char)('!' + mrx), (char)('!' + mry));
1837 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
Okash Khawaja496124e2019-04-17 13:21:13 +01001840/* invoked via ioctl(TIOCLINUX) and through set_selection_user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841int mouse_reporting(void)
1842{
1843 return vc_cons[fg_console].d->vc_report_mouse;
1844}
1845
Torben Hohnac751ef2011-01-25 15:07:35 -08001846/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847static void set_mode(struct vc_data *vc, int on_off)
1848{
1849 int i;
1850
1851 for (i = 0; i <= vc->vc_npar; i++)
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01001852 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1854 case 1: /* Cursor keys send ^[Ox/^[[x */
1855 if (on_off)
1856 set_kbd(vc, decckm);
1857 else
1858 clr_kbd(vc, decckm);
1859 break;
1860 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861#if 0
1862 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1863 /* this alone does not suffice; some user mode
1864 utility has to change the hardware regs */
1865#endif
1866 break;
1867 case 5: /* Inverted screen on/off */
1868 if (vc->vc_decscnm != on_off) {
1869 vc->vc_decscnm = on_off;
1870 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1871 update_attr(vc);
1872 }
1873 break;
1874 case 6: /* Origin relative/absolute */
1875 vc->vc_decom = on_off;
1876 gotoxay(vc, 0, 0);
1877 break;
1878 case 7: /* Autowrap on/off */
1879 vc->vc_decawm = on_off;
1880 break;
1881 case 8: /* Autorepeat on/off */
1882 if (on_off)
1883 set_kbd(vc, decarm);
1884 else
1885 clr_kbd(vc, decarm);
1886 break;
1887 case 9:
1888 vc->vc_report_mouse = on_off ? 1 : 0;
1889 break;
1890 case 25: /* Cursor on/off */
1891 vc->vc_deccm = on_off;
1892 break;
1893 case 1000:
1894 vc->vc_report_mouse = on_off ? 2 : 0;
1895 break;
1896 }
1897 } else {
1898 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1899 case 3: /* Monitor (display ctrls) */
1900 vc->vc_disp_ctrl = on_off;
1901 break;
1902 case 4: /* Insert Mode on/off */
1903 vc->vc_decim = on_off;
1904 break;
1905 case 20: /* Lf, Enter == CrLf/Lf */
1906 if (on_off)
1907 set_kbd(vc, lnm);
1908 else
1909 clr_kbd(vc, lnm);
1910 break;
1911 }
1912 }
1913}
1914
Torben Hohnac751ef2011-01-25 15:07:35 -08001915/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916static void setterm_command(struct vc_data *vc)
1917{
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001918 switch (vc->vc_par[0]) {
1919 case 1: /* set color for underline mode */
1920 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1921 vc->vc_ulcolor = color_table[vc->vc_par[1]];
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001922 if (vc->state.underline)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001923 update_attr(vc);
1924 }
1925 break;
1926 case 2: /* set color for half intensity mode */
1927 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1928 vc->vc_halfcolor = color_table[vc->vc_par[1]];
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001929 if (vc->state.intensity == VCI_HALF_BRIGHT)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001930 update_attr(vc);
1931 }
1932 break;
1933 case 8: /* store colors as defaults */
1934 vc->vc_def_color = vc->vc_attr;
1935 if (vc->vc_hi_font_mask == 0x100)
1936 vc->vc_def_color >>= 1;
1937 default_attr(vc);
1938 update_attr(vc);
1939 break;
1940 case 9: /* set blanking interval */
1941 blankinterval = min(vc->vc_par[1], 60U) * 60;
1942 poke_blanked_console();
1943 break;
1944 case 10: /* set bell frequency in Hz */
1945 if (vc->vc_npar >= 1)
1946 vc->vc_bell_pitch = vc->vc_par[1];
1947 else
1948 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1949 break;
1950 case 11: /* set bell duration in msec */
1951 if (vc->vc_npar >= 1)
1952 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1953 msecs_to_jiffies(vc->vc_par[1]) : 0;
1954 else
1955 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1956 break;
1957 case 12: /* bring specified console to the front */
1958 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1959 set_console(vc->vc_par[1] - 1);
1960 break;
1961 case 13: /* unblank the screen */
1962 poke_blanked_console();
1963 break;
1964 case 14: /* set vesa powerdown interval */
1965 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1966 break;
1967 case 15: /* activate the previous console */
1968 set_console(last_console);
1969 break;
1970 case 16: /* set cursor blink duration in msec */
1971 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1972 vc->vc_par[1] <= USHRT_MAX)
1973 vc->vc_cur_blink_ms = vc->vc_par[1];
1974 else
1975 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1976 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 }
1978}
1979
Torben Hohnac751ef2011-01-25 15:07:35 -08001980/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981static void csi_at(struct vc_data *vc, unsigned int nr)
1982{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001983 if (nr > vc->vc_cols - vc->state.x)
1984 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 else if (!nr)
1986 nr = 1;
1987 insert_char(vc, nr);
1988}
1989
Torben Hohnac751ef2011-01-25 15:07:35 -08001990/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991static void csi_L(struct vc_data *vc, unsigned int nr)
1992{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001993 if (nr > vc->vc_rows - vc->state.y)
1994 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 else if (!nr)
1996 nr = 1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001997 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 vc->vc_need_wrap = 0;
1999}
2000
Torben Hohnac751ef2011-01-25 15:07:35 -08002001/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002static void csi_P(struct vc_data *vc, unsigned int nr)
2003{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002004 if (nr > vc->vc_cols - vc->state.x)
2005 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 else if (!nr)
2007 nr = 1;
2008 delete_char(vc, nr);
2009}
2010
Torben Hohnac751ef2011-01-25 15:07:35 -08002011/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static void csi_M(struct vc_data *vc, unsigned int nr)
2013{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002014 if (nr > vc->vc_rows - vc->state.y)
2015 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 else if (!nr)
2017 nr=1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002018 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 vc->vc_need_wrap = 0;
2020}
2021
Torben Hohnac751ef2011-01-25 15:07:35 -08002022/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023static void save_cur(struct vc_data *vc)
2024{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002025 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026}
2027
Torben Hohnac751ef2011-01-25 15:07:35 -08002028/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029static void restore_cur(struct vc_data *vc)
2030{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002031 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
2032
2033 gotoxy(vc, vc->state.x, vc->state.y);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002034 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset],
2035 vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 update_attr(vc);
2037 vc->vc_need_wrap = 0;
2038}
2039
Adam Borowskib290af62014-02-19 01:40:16 +01002040enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Martin Hostettler7a995652018-12-15 15:34:22 +01002041 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01002042 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
Torben Hohnac751ef2011-01-25 15:07:35 -08002044/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045static void reset_terminal(struct vc_data *vc, int do_clear)
2046{
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002047 unsigned int i;
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 vc->vc_top = 0;
2050 vc->vc_bottom = vc->vc_rows;
2051 vc->vc_state = ESnormal;
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002052 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 vc->vc_translate = set_translate(LAT1_MAP, vc);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002054 vc->state.Gx_charset[0] = LAT1_MAP;
2055 vc->state.Gx_charset[1] = GRAF_MAP;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002056 vc->state.charset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 vc->vc_need_wrap = 0;
2058 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07002059 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 vc->vc_utf_count = 0;
2061
2062 vc->vc_disp_ctrl = 0;
2063 vc->vc_toggle_meta = 0;
2064
2065 vc->vc_decscnm = 0;
2066 vc->vc_decom = 0;
2067 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05002068 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 vc->vc_decim = 0;
2070
Alan Cox079c9532012-02-28 14:49:23 +00002071 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002073 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 vc->vc_complement_mask = vc->vc_s_complement_mask;
2075
2076 default_attr(vc);
2077 update_attr(vc);
2078
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002079 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
2080 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8)
2081 set_bit(i, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2084 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00002085 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 gotoxy(vc, 0, 0);
2088 save_cur(vc);
2089 if (do_clear)
2090 csi_J(vc, 2);
2091}
2092
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002093static void vc_setGx(struct vc_data *vc, unsigned int which, int c)
2094{
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002095 unsigned char *charset = &vc->state.Gx_charset[which];
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002096
2097 switch (c) {
2098 case '0':
2099 *charset = GRAF_MAP;
2100 break;
2101 case 'B':
2102 *charset = LAT1_MAP;
2103 break;
2104 case 'U':
2105 *charset = IBMPC_MAP;
2106 break;
2107 case 'K':
2108 *charset = USER_MAP;
2109 break;
2110 }
2111
2112 if (vc->state.charset == which)
2113 vc->vc_translate = set_translate(*charset, vc);
2114}
2115
Torben Hohnac751ef2011-01-25 15:07:35 -08002116/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2118{
2119 /*
2120 * Control characters can be used in the _middle_
2121 * of an escape sequence.
2122 */
Adam Borowski63f3a162014-02-19 01:38:04 +01002123 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2124 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 switch (c) {
2126 case 0:
2127 return;
2128 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01002129 if (vc->vc_state == ESosc)
2130 vc->vc_state = ESnormal;
2131 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2133 return;
2134 case 8:
2135 bs(vc);
2136 return;
2137 case 9:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002138 vc->vc_pos -= (vc->state.x << 1);
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002139
2140 vc->state.x = find_next_bit(vc->vc_tab_stop,
2141 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT),
2142 vc->state.x + 1);
2143 if (vc->state.x >= VC_TABSTOPS_COUNT)
2144 vc->state.x = vc->vc_cols - 1;
2145
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002146 vc->vc_pos += (vc->state.x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002147 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 return;
2149 case 10: case 11: case 12:
2150 lf(vc);
2151 if (!is_kbd(vc, lnm))
2152 return;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002153 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 case 13:
2155 cr(vc);
2156 return;
2157 case 14:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002158 vc->state.charset = 1;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002159 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 vc->vc_disp_ctrl = 1;
2161 return;
2162 case 15:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002163 vc->state.charset = 0;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002164 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 vc->vc_disp_ctrl = 0;
2166 return;
2167 case 24: case 26:
2168 vc->vc_state = ESnormal;
2169 return;
2170 case 27:
2171 vc->vc_state = ESesc;
2172 return;
2173 case 127:
2174 del(vc);
2175 return;
2176 case 128+27:
2177 vc->vc_state = ESsquare;
2178 return;
2179 }
2180 switch(vc->vc_state) {
2181 case ESesc:
2182 vc->vc_state = ESnormal;
2183 switch (c) {
2184 case '[':
2185 vc->vc_state = ESsquare;
2186 return;
2187 case ']':
2188 vc->vc_state = ESnonstd;
2189 return;
2190 case '%':
2191 vc->vc_state = ESpercent;
2192 return;
2193 case 'E':
2194 cr(vc);
2195 lf(vc);
2196 return;
2197 case 'M':
2198 ri(vc);
2199 return;
2200 case 'D':
2201 lf(vc);
2202 return;
2203 case 'H':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002204 if (vc->state.x < VC_TABSTOPS_COUNT)
2205 set_bit(vc->state.x, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return;
2207 case 'Z':
2208 respond_ID(tty);
2209 return;
2210 case '7':
2211 save_cur(vc);
2212 return;
2213 case '8':
2214 restore_cur(vc);
2215 return;
2216 case '(':
2217 vc->vc_state = ESsetG0;
2218 return;
2219 case ')':
2220 vc->vc_state = ESsetG1;
2221 return;
2222 case '#':
2223 vc->vc_state = EShash;
2224 return;
2225 case 'c':
2226 reset_terminal(vc, 1);
2227 return;
2228 case '>': /* Numeric keypad */
2229 clr_kbd(vc, kbdapplic);
2230 return;
2231 case '=': /* Appl. keypad */
2232 set_kbd(vc, kbdapplic);
2233 return;
2234 }
2235 return;
2236 case ESnonstd:
2237 if (c=='P') { /* palette escape sequence */
2238 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2239 vc->vc_par[vc->vc_npar] = 0;
2240 vc->vc_npar = 0;
2241 vc->vc_state = ESpalette;
2242 return;
2243 } else if (c=='R') { /* reset palette */
2244 reset_palette(vc);
2245 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01002246 } else if (c>='0' && c<='9')
2247 vc->vc_state = ESosc;
2248 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 vc->vc_state = ESnormal;
2250 return;
2251 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03002252 if (isxdigit(c)) {
2253 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (vc->vc_npar == 7) {
2255 int i = vc->vc_par[0] * 3, j = 1;
2256 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2257 vc->vc_palette[i++] += vc->vc_par[j++];
2258 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2259 vc->vc_palette[i++] += vc->vc_par[j++];
2260 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2261 vc->vc_palette[i] += vc->vc_par[j];
2262 set_palette(vc);
2263 vc->vc_state = ESnormal;
2264 }
2265 } else
2266 vc->vc_state = ESnormal;
2267 return;
2268 case ESsquare:
2269 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2270 vc->vc_par[vc->vc_npar] = 0;
2271 vc->vc_npar = 0;
2272 vc->vc_state = ESgetpars;
2273 if (c == '[') { /* Function key */
2274 vc->vc_state=ESfunckey;
2275 return;
2276 }
Martin Hostettler54454472018-12-15 15:34:21 +01002277 switch (c) {
2278 case '?':
2279 vc->vc_priv = EPdec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 return;
Martin Hostettler54454472018-12-15 15:34:21 +01002281 case '>':
2282 vc->vc_priv = EPgt;
2283 return;
2284 case '=':
2285 vc->vc_priv = EPeq;
2286 return;
2287 case '<':
2288 vc->vc_priv = EPlt;
2289 return;
2290 }
2291 vc->vc_priv = EPecma;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002292 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 case ESgetpars:
2294 if (c == ';' && vc->vc_npar < NPAR - 1) {
2295 vc->vc_npar++;
2296 return;
2297 } else if (c>='0' && c<='9') {
2298 vc->vc_par[vc->vc_npar] *= 10;
2299 vc->vc_par[vc->vc_npar] += c - '0';
2300 return;
Adam Borowskib290af62014-02-19 01:40:16 +01002301 }
Martin Hostettler04afcd32018-12-15 15:34:23 +01002302 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
Martin Hostettler7a995652018-12-15 15:34:22 +01002303 vc->vc_state = EScsiignore;
2304 return;
2305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 vc->vc_state = ESnormal;
2307 switch(c) {
2308 case 'h':
Martin Hostettler54454472018-12-15 15:34:21 +01002309 if (vc->vc_priv <= EPdec)
2310 set_mode(vc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 return;
2312 case 'l':
Martin Hostettler54454472018-12-15 15:34:21 +01002313 if (vc->vc_priv <= EPdec)
2314 set_mode(vc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return;
2316 case 'c':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002317 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (vc->vc_par[0])
2319 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2320 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002321 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return;
2323 }
2324 break;
2325 case 'm':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002326 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 clear_selection();
2328 if (vc->vc_par[0])
2329 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2330 else
2331 vc->vc_complement_mask = vc->vc_s_complement_mask;
2332 return;
2333 }
2334 break;
2335 case 'n':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002336 if (vc->vc_priv == EPecma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (vc->vc_par[0] == 5)
2338 status_report(tty);
2339 else if (vc->vc_par[0] == 6)
2340 cursor_report(vc, tty);
2341 }
2342 return;
2343 }
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002344 if (vc->vc_priv != EPecma) {
2345 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 return;
2347 }
2348 switch(c) {
2349 case 'G': case '`':
2350 if (vc->vc_par[0])
2351 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002352 gotoxy(vc, vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 return;
2354 case 'A':
2355 if (!vc->vc_par[0])
2356 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002357 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return;
2359 case 'B': case 'e':
2360 if (!vc->vc_par[0])
2361 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002362 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return;
2364 case 'C': case 'a':
2365 if (!vc->vc_par[0])
2366 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002367 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return;
2369 case 'D':
2370 if (!vc->vc_par[0])
2371 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002372 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return;
2374 case 'E':
2375 if (!vc->vc_par[0])
2376 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002377 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 return;
2379 case 'F':
2380 if (!vc->vc_par[0])
2381 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002382 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 return;
2384 case 'd':
2385 if (vc->vc_par[0])
2386 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002387 gotoxay(vc, vc->state.x ,vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 return;
2389 case 'H': case 'f':
2390 if (vc->vc_par[0])
2391 vc->vc_par[0]--;
2392 if (vc->vc_par[1])
2393 vc->vc_par[1]--;
2394 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2395 return;
2396 case 'J':
2397 csi_J(vc, vc->vc_par[0]);
2398 return;
2399 case 'K':
2400 csi_K(vc, vc->vc_par[0]);
2401 return;
2402 case 'L':
2403 csi_L(vc, vc->vc_par[0]);
2404 return;
2405 case 'M':
2406 csi_M(vc, vc->vc_par[0]);
2407 return;
2408 case 'P':
2409 csi_P(vc, vc->vc_par[0]);
2410 return;
2411 case 'c':
2412 if (!vc->vc_par[0])
2413 respond_ID(tty);
2414 return;
2415 case 'g':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002416 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT)
2417 set_bit(vc->state.x, vc->vc_tab_stop);
2418 else if (vc->vc_par[0] == 3)
2419 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 return;
2421 case 'm':
2422 csi_m(vc);
2423 return;
2424 case 'q': /* DECLL - but only 3 leds */
2425 /* map 0,1,2,3 to 0,1,2,4 */
2426 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002427 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2429 return;
2430 case 'r':
2431 if (!vc->vc_par[0])
2432 vc->vc_par[0]++;
2433 if (!vc->vc_par[1])
2434 vc->vc_par[1] = vc->vc_rows;
2435 /* Minimum allowed region is 2 lines */
2436 if (vc->vc_par[0] < vc->vc_par[1] &&
2437 vc->vc_par[1] <= vc->vc_rows) {
2438 vc->vc_top = vc->vc_par[0] - 1;
2439 vc->vc_bottom = vc->vc_par[1];
2440 gotoxay(vc, 0, 0);
2441 }
2442 return;
2443 case 's':
2444 save_cur(vc);
2445 return;
2446 case 'u':
2447 restore_cur(vc);
2448 return;
2449 case 'X':
2450 csi_X(vc, vc->vc_par[0]);
2451 return;
2452 case '@':
2453 csi_at(vc, vc->vc_par[0]);
2454 return;
2455 case ']': /* setterm functions */
2456 setterm_command(vc);
2457 return;
2458 }
2459 return;
Martin Hostettler7a995652018-12-15 15:34:22 +01002460 case EScsiignore:
2461 if (c >= 20 && c <= 0x3f)
2462 return;
2463 vc->vc_state = ESnormal;
2464 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 case ESpercent:
2466 vc->vc_state = ESnormal;
2467 switch (c) {
2468 case '@': /* defined in ISO 2022 */
2469 vc->vc_utf = 0;
2470 return;
2471 case 'G': /* prelim official escape code */
2472 case '8': /* retained for compatibility */
2473 vc->vc_utf = 1;
2474 return;
2475 }
2476 return;
2477 case ESfunckey:
2478 vc->vc_state = ESnormal;
2479 return;
2480 case EShash:
2481 vc->vc_state = ESnormal;
2482 if (c == '8') {
2483 /* DEC screen alignment test. kludge :-) */
2484 vc->vc_video_erase_char =
2485 (vc->vc_video_erase_char & 0xff00) | 'E';
2486 csi_J(vc, 2);
2487 vc->vc_video_erase_char =
2488 (vc->vc_video_erase_char & 0xff00) | ' ';
2489 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2490 }
2491 return;
2492 case ESsetG0:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002493 vc_setGx(vc, 0, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 vc->vc_state = ESnormal;
2495 return;
2496 case ESsetG1:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002497 vc_setGx(vc, 1, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 vc->vc_state = ESnormal;
2499 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002500 case ESosc:
2501 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502 default:
2503 vc->vc_state = ESnormal;
2504 }
2505}
2506
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002507/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002508 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002509 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2510 */
2511struct interval {
2512 uint32_t first;
2513 uint32_t last;
2514};
2515
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002516static int ucs_cmp(const void *key, const void *elt)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002517{
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002518 uint32_t ucs = *(uint32_t *)key;
2519 struct interval e = *(struct interval *) elt;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002520
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002521 if (ucs > e.last)
2522 return 1;
2523 else if (ucs < e.first)
2524 return -1;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002525 return 0;
2526}
2527
2528static int is_double_width(uint32_t ucs)
2529{
2530 static const struct interval double_width[] = {
2531 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2532 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002533 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2534 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002535 };
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002536 if (ucs < double_width[0].first ||
2537 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2538 return 0;
2539
2540 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2541 sizeof(struct interval), ucs_cmp) != NULL;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002542}
2543
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002544struct vc_draw_region {
2545 unsigned long from, to;
2546 int x;
2547};
2548
2549static void con_flush(struct vc_data *vc, struct vc_draw_region *draw)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002550{
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002551 if (draw->x < 0)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002552 return;
2553
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002554 vc->vc_sw->con_putcs(vc, (u16 *)draw->from,
2555 (u16 *)draw->to - (u16 *)draw->from, vc->state.y,
2556 draw->x);
2557 draw->x = -1;
Jiri Slabyd711ea82016-06-23 13:34:30 +02002558}
2559
Jiri Slabya0181802020-06-15 09:48:42 +02002560static inline int vc_translate_ascii(const struct vc_data *vc, int c)
2561{
2562 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) {
2563 if (vc->vc_toggle_meta)
2564 c |= 0x80;
2565
2566 return vc->vc_translate[c];
2567 }
2568
2569 return c;
2570}
2571
Jiri Slabyede98d12020-06-15 09:48:45 +02002572
2573/**
2574 * vc_sanitize_unicode -- Replace invalid Unicode code points with U+FFFD
2575 * @c: the received character, or U+FFFD for invalid sequences.
2576 */
2577static inline int vc_sanitize_unicode(const int c)
2578{
2579 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2580 return 0xfffd;
2581
2582 return c;
2583}
2584
Jiri Slaby694d8a42020-06-15 09:48:44 +02002585/**
2586 * vc_translate_unicode -- Combine UTF-8 into Unicode in @vc_utf_char
2587 *
2588 * @vc_utf_char is the being-constructed unicode character.
2589 * @vc_utf_count is the number of continuation bytes still expected to arrive.
2590 * @vc_npar is the number of continuation bytes arrived so far.
2591 */
2592static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
2593{
2594 static const u32 utf8_length_changes[] = {
2595 0x0000007f, 0x000007ff, 0x0000ffff,
2596 0x001fffff, 0x03ffffff, 0x7fffffff
2597 };
2598
Jiri Slabyede98d12020-06-15 09:48:45 +02002599 /* Continuation byte received */
Jiri Slaby694d8a42020-06-15 09:48:44 +02002600 if ((c & 0xc0) == 0x80) {
Jiri Slabyede98d12020-06-15 09:48:45 +02002601 /* Unexpected continuation byte? */
2602 if (!vc->vc_utf_count)
Jiri Slaby694d8a42020-06-15 09:48:44 +02002603 return 0xfffd;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002604
Jiri Slabyede98d12020-06-15 09:48:45 +02002605 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2606 vc->vc_npar++;
2607 if (--vc->vc_utf_count)
2608 goto need_more_bytes;
2609
2610 /* Got a whole character */
2611 c = vc->vc_utf_char;
2612 /* Reject overlong sequences */
2613 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2614 c > utf8_length_changes[vc->vc_npar])
2615 return 0xfffd;
2616
2617 return vc_sanitize_unicode(c);
2618 }
2619
2620 /* Single ASCII byte or first byte of a sequence received */
2621 if (vc->vc_utf_count) {
2622 /* Continuation byte expected */
2623 *rescan = true;
2624 vc->vc_utf_count = 0;
2625 return 0xfffd;
2626 }
2627
2628 /* Nothing to do if an ASCII byte was received */
2629 if (c <= 0x7f)
2630 return c;
2631
2632 /* First byte of a multibyte sequence received */
2633 vc->vc_npar = 0;
2634 if ((c & 0xe0) == 0xc0) {
2635 vc->vc_utf_count = 1;
2636 vc->vc_utf_char = (c & 0x1f);
2637 } else if ((c & 0xf0) == 0xe0) {
2638 vc->vc_utf_count = 2;
2639 vc->vc_utf_char = (c & 0x0f);
2640 } else if ((c & 0xf8) == 0xf0) {
2641 vc->vc_utf_count = 3;
2642 vc->vc_utf_char = (c & 0x07);
2643 } else if ((c & 0xfc) == 0xf8) {
2644 vc->vc_utf_count = 4;
2645 vc->vc_utf_char = (c & 0x03);
2646 } else if ((c & 0xfe) == 0xfc) {
2647 vc->vc_utf_count = 5;
2648 vc->vc_utf_char = (c & 0x01);
2649 } else {
2650 /* 254 and 255 are invalid */
2651 return 0xfffd;
2652 }
2653
2654need_more_bytes:
2655 return -1;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002656}
2657
Jiri Slabya4236342020-06-15 09:48:48 +02002658static int vc_translate(struct vc_data *vc, int *c, bool *rescan)
2659{
2660 /* Do no translation at all in control states */
2661 if (vc->vc_state != ESnormal)
2662 return *c;
2663
2664 if (vc->vc_utf && !vc->vc_disp_ctrl)
2665 return *c = vc_translate_unicode(vc, *c, rescan);
2666
2667 /* no utf or alternate charset mode */
2668 return vc_translate_ascii(vc, *c);
2669}
2670
Jiri Slaby881c8782020-06-15 09:48:46 +02002671static inline unsigned char vc_invert_attr(const struct vc_data *vc)
2672{
2673 if (!vc->vc_can_do_color)
2674 return vc->vc_attr ^ 0x08;
2675
2676 if (vc->vc_hi_font_mask == 0x100)
2677 return (vc->vc_attr & 0x11) |
2678 ((vc->vc_attr & 0xe0) >> 4) |
2679 ((vc->vc_attr & 0x0e) << 4);
2680
2681 return (vc->vc_attr & 0x88) |
2682 ((vc->vc_attr & 0x70) >> 4) |
2683 ((vc->vc_attr & 0x07) << 4);
2684}
2685
Jiri Slaby754bda92020-06-15 09:48:50 +02002686static bool vc_is_control(struct vc_data *vc, int tc, int c)
2687{
2688 /*
2689 * A bitmap for codes <32. A bit of 1 indicates that the code
2690 * corresponding to that bit number invokes some special action (such
2691 * as cursor movement) and should not be displayed as a glyph unless
2692 * the disp_ctrl mode is explicitly enabled.
2693 */
2694 static const u32 CTRL_ACTION = 0x0d00ff81;
2695 /* Cannot be overridden by disp_ctrl */
2696 static const u32 CTRL_ALWAYS = 0x0800f501;
2697
2698 if (vc->vc_state != ESnormal)
2699 return true;
2700
2701 if (!tc)
2702 return true;
2703
2704 /*
2705 * If the original code was a control character we only allow a glyph
2706 * to be displayed if the code is not normally used (such as for cursor
2707 * movement) or if the disp_ctrl mode has been explicitly enabled.
2708 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always
2709 * displayed as control characters, as the console would be pretty
2710 * useless without them; to display an arbitrary font position use the
2711 * direct-to-font zone in UTF-8 mode.
2712 */
2713 if (c < 32) {
2714 if (vc->vc_disp_ctrl)
2715 return CTRL_ALWAYS & BIT(c);
2716 else
2717 return vc->vc_utf || (CTRL_ACTION & BIT(c));
2718 }
2719
2720 if (c == 127 && !vc->vc_disp_ctrl)
2721 return true;
2722
2723 if (c == 128 + 27)
2724 return true;
2725
2726 return false;
2727}
2728
Torben Hohnac751ef2011-01-25 15:07:35 -08002729/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2731{
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002732 struct vc_draw_region draw = {
2733 .x = -1,
2734 };
Jiri Slaby754bda92020-06-15 09:48:50 +02002735 int c, next_c, tc, n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 unsigned int currcons;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002738 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002739 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 u16 himask, charmask;
Jiri Slabyda823b22020-06-15 09:48:43 +02002741 u8 width;
2742 bool rescan;
2743 bool inverse;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 if (in_interrupt())
2746 return count;
2747
Torben Hohnac751ef2011-01-25 15:07:35 -08002748 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 vc = tty->driver_data;
2750 if (vc == NULL) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07002751 pr_err("vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002752 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 return 0;
2754 }
2755
2756 currcons = vc->vc_num;
2757 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002758 /* could this happen? */
2759 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2760 console_unlock();
2761 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 himask = vc->vc_hi_font_mask;
2765 charmask = himask ? 0x1ff : 0xff;
2766
2767 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002768 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 hide_cursor(vc);
2770
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002771 param.vc = vc;
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 while (!tty->stopped && count) {
2774 int orig = *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 buf++;
2776 n++;
2777 count--;
Jiri Slaby0d1ffef2020-06-15 09:48:47 +02002778rescan_last_byte:
2779 c = orig;
Jiri Slabyda823b22020-06-15 09:48:43 +02002780 rescan = false;
2781 inverse = false;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002782 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Jiri Slabya4236342020-06-15 09:48:48 +02002784 tc = vc_translate(vc, &c, &rescan);
2785 if (tc == -1)
2786 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002788 param.c = tc;
2789 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2790 &param) == NOTIFY_STOP)
2791 continue;
2792
Jiri Slaby754bda92020-06-15 09:48:50 +02002793 if (!vc_is_control(vc, tc, c)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002794 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2795 if (is_double_width(c))
2796 width = 2;
2797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 /* Now try to find out how to display it */
2799 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002800 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002801 if (tc == -1 || tc == -2) {
2802 continue; /* nothing to display */
2803 }
2804 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002805 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002806 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002807 This would make absolutely no sense with Unicode in mind,
2808 but do this for ASCII characters since a font may lack
2809 Unicode mapping info and we don't want to end up with
2810 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002811 tc = c;
2812 } else {
2813 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2814 tc = conv_uni_to_pc(vc, 0xfffd);
2815 if (tc < 0) {
Jiri Slabyda823b22020-06-15 09:48:43 +02002816 inverse = true;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002817 tc = conv_uni_to_pc(vc, '?');
2818 if (tc < 0) tc = '?';
2819 }
2820 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002823 if (!inverse) {
2824 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 } else {
Jiri Slaby881c8782020-06-15 09:48:46 +02002826 vc_attr = vc_invert_attr(vc);
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002827 con_flush(vc, &draw);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002828 }
2829
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002830 next_c = c;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002831 while (1) {
2832 if (vc->vc_need_wrap || vc->vc_decim)
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002833 con_flush(vc, &draw);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002834 if (vc->vc_need_wrap) {
2835 cr(vc);
2836 lf(vc);
2837 }
2838 if (vc->vc_decim)
2839 insert_char(vc, 1);
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002840 vc_uniscr_putc(vc, next_c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002841 scr_writew(himask ?
2842 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2843 (vc_attr << 8) + tc,
2844 (u16 *) vc->vc_pos);
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002845 if (con_should_update(vc) && draw.x < 0) {
2846 draw.x = vc->state.x;
2847 draw.from = vc->vc_pos;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002848 }
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002849 if (vc->state.x == vc->vc_cols - 1) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002850 vc->vc_need_wrap = vc->vc_decawm;
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002851 draw.to = vc->vc_pos + 2;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002852 } else {
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002853 vc->state.x++;
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002854 draw.to = (vc->vc_pos += 2);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002855 }
2856
2857 if (!--width) break;
2858
2859 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2860 if (tc < 0) tc = ' ';
Nicolas Pitred8ae7242018-06-26 23:56:40 -04002861 next_c = ' ';
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002862 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002863 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002864
Jiri Slabyd711ea82016-06-23 13:34:30 +02002865 if (inverse)
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002866 con_flush(vc, &draw);
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002867
Jiri Slaby0d1ffef2020-06-15 09:48:47 +02002868 if (rescan)
Adam Tlalkad4328b42006-09-29 01:59:53 -07002869 goto rescan_last_byte;
Jiri Slaby0d1ffef2020-06-15 09:48:47 +02002870
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 continue;
2872 }
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002873 con_flush(vc, &draw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 do_con_trol(tty, vc, orig);
2875 }
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002876 con_flush(vc, &draw);
Nicolas Pitre02240802018-07-17 21:02:41 -04002877 vc_uniscr_debug_check(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 console_conditional_schedule();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002879 notify_update(vc);
Nicolas Pitre7e1d2262019-01-08 22:55:00 -05002880 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882}
2883
2884/*
2885 * This is the console switching callback.
2886 *
2887 * Doing console switching in a process context allows
2888 * us to do the switches asynchronously (needed when we want
2889 * to switch due to a keyboard interrupt). Synchronization
2890 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002891 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
David Howells65f27f32006-11-22 14:55:48 +00002893static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
Torben Hohnac751ef2011-01-25 15:07:35 -08002895 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 if (want_console >= 0) {
2898 if (want_console != fg_console &&
2899 vc_cons_allocated(want_console)) {
2900 hide_cursor(vc_cons[fg_console].d);
2901 change_console(vc_cons[want_console].d);
2902 /* we only changed when the console had already
2903 been allocated - a new console is not created
2904 in an interrupt routine */
2905 }
2906 want_console = -1;
2907 }
2908 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2909 do_poke_blanked_console = 0;
2910 poke_blanked_console();
2911 }
2912 if (scrollback_delta) {
2913 struct vc_data *vc = vc_cons[fg_console].d;
2914 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002915 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2917 scrollback_delta = 0;
2918 }
2919 if (blank_timer_expired) {
2920 do_blank_screen(0);
2921 blank_timer_expired = 0;
2922 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002923 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Torben Hohnac751ef2011-01-25 15:07:35 -08002925 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002928int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002930 struct vc_data *vc = vc_cons[fg_console].d;
2931
2932 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2933 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2934
2935 /*
2936 * Console switch will fail in console_callback() or
2937 * change_console() so there is no point scheduling
2938 * the callback
2939 *
2940 * Existing set_console() users don't check the return
2941 * value so this shouldn't break anything
2942 */
2943 return -EINVAL;
2944 }
2945
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 want_console = nr;
2947 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002948
2949 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
2952struct tty_driver *console_driver;
2953
2954#ifdef CONFIG_VT_CONSOLE
2955
Bernhard Walle5ada9182009-12-14 18:00:43 -08002956/**
2957 * vt_kmsg_redirect() - Sets/gets the kernel message console
2958 * @new: The new virtual terminal number or -1 if the console should stay
2959 * unchanged
2960 *
2961 * By default, the kernel messages are always printed on the current virtual
2962 * console. However, the user may modify that default with the
2963 * TIOCL_SETKMSGREDIRECT ioctl call.
2964 *
2965 * This function sets the kernel message console to be @new. It returns the old
2966 * virtual console number. The virtual terminal number 0 (both as parameter and
2967 * return value) means no redirection (i.e. always printed on the currently
2968 * active console).
2969 *
2970 * The parameter -1 means that only the current console is returned, but the
2971 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2972 * case to make the code more understandable.
2973 *
2974 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2975 * the parameter and always returns 0.
2976 */
2977int vt_kmsg_redirect(int new)
2978{
2979 static int kmsg_con;
2980
2981 if (new != -1)
2982 return xchg(&kmsg_con, new);
2983 else
2984 return kmsg_con;
2985}
2986
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987/*
2988 * Console on virtual terminal
2989 *
2990 * The console must be locked when we get here.
2991 */
2992
2993static void vt_console_print(struct console *co, const char *b, unsigned count)
2994{
2995 struct vc_data *vc = vc_cons[fg_console].d;
2996 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002997 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 const ushort *start;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05002999 ushort start_x, cnt;
Bernhard Walle5ada9182009-12-14 18:00:43 -08003000 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
3002 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08003003 if (!printable)
3004 return;
3005 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return;
3007
Bernhard Walle5ada9182009-12-14 18:00:43 -08003008 kmsg_console = vt_get_kmsg_redirect();
3009 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
3010 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 if (!vc_cons_allocated(fg_console)) {
3013 /* impossible */
3014 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
3015 goto quit;
3016 }
3017
Daniel Vetter8d7fc292018-08-22 10:54:03 +02003018 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 goto quit;
3020
3021 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003022 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 hide_cursor(vc);
3024
3025 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003026 start_x = vc->state.x;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003027 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 while (count--) {
3029 c = *b++;
3030 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003031 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003032 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003033 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 if (c == 8) { /* backspace */
3035 bs(vc);
3036 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003037 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 continue;
3039 }
3040 if (c != 13)
3041 lf(vc);
3042 cr(vc);
3043 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003044 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 if (c == 10 || c == 13)
3046 continue;
3047 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003048 vc_uniscr_putc(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003050 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 cnt++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003052 if (vc->state.x == vc->vc_cols - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 vc->vc_need_wrap = 1;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003054 } else {
3055 vc->vc_pos += 2;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003056 vc->state.x++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 }
3058 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003059 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003060 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003062 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
3064quit:
Nick Pigginb0940002008-02-06 01:37:04 -08003065 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066}
3067
3068static struct tty_driver *vt_console_device(struct console *c, int *index)
3069{
3070 *index = c->index ? c->index-1 : fg_console;
3071 return console_driver;
3072}
3073
3074static struct console vt_console_driver = {
3075 .name = "tty",
3076 .write = vt_console_print,
3077 .device = vt_console_device,
3078 .unblank = unblank_screen,
3079 .flags = CON_PRINTBUFFER,
3080 .index = -1,
3081};
3082#endif
3083
3084/*
3085 * Handling of Linux-specific VC ioctls
3086 */
3087
3088/*
Torben Hohnac751ef2011-01-25 15:07:35 -08003089 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 *
3091 * There are some functions which don't need it.
3092 *
3093 * There are some functions which can sleep for arbitrary periods
3094 * (paste_selection) but we don't need the lock there anyway.
3095 *
Okash Khawaja496124e2019-04-17 13:21:13 +01003096 * set_selection_user has locking, and definitely needs it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 */
3098
3099int tioclinux(struct tty_struct *tty, unsigned long arg)
3100{
3101 char type, data;
3102 char __user *p = (char __user *)arg;
3103 int lines;
3104 int ret;
3105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3107 return -EPERM;
3108 if (get_user(type, p))
3109 return -EFAULT;
3110 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07003111
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 switch (type)
3113 {
3114 case TIOCL_SETSEL:
Okash Khawaja496124e2019-04-17 13:21:13 +01003115 ret = set_selection_user((struct tiocl_selection
3116 __user *)(p+1), tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 break;
3118 case TIOCL_PASTESEL:
3119 ret = paste_selection(tty);
3120 break;
3121 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08003122 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08003124 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 break;
3126 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00003127 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00003129 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 break;
3131 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 /*
3134 * Make it possible to react to Shift+Mousebutton.
3135 * Note that 'shift_state' is an undocumented
3136 * kernel-internal variable; programs not closely
3137 * related to the kernel should not use this.
3138 */
Alan Cox079c9532012-02-28 14:49:23 +00003139 data = vt_get_shift_state();
Adam Borowski6987dc82017-06-03 09:35:06 +02003140 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 break;
3142 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00003143 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00003145 console_unlock();
Adam Borowski6987dc82017-06-03 09:35:06 +02003146 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 break;
3148 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00003149 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003150 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00003151 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003153 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08003154 data = vt_get_kmsg_redirect();
Adam Borowski6987dc82017-06-03 09:35:06 +02003155 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003156 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 case TIOCL_SETKMSGREDIRECT:
3158 if (!capable(CAP_SYS_ADMIN)) {
3159 ret = -EPERM;
3160 } else {
3161 if (get_user(data, p+1))
3162 ret = -EFAULT;
3163 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08003164 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 }
3166 break;
3167 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00003168 /* No locking needed as this is a transiently
3169 correct return anyway if the caller hasn't
3170 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 ret = fg_console;
3172 break;
3173 case TIOCL_SCROLLCONSOLE:
3174 if (get_user(lines, (s32 __user *)(p+4))) {
3175 ret = -EFAULT;
3176 } else {
Alan Cox20f62572012-03-02 14:59:37 +00003177 /* Need the console lock here. Note that lots
3178 of other calls need fixing before the lock
3179 is actually useful ! */
3180 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00003182 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 ret = 0;
3184 }
3185 break;
3186 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08003187 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 ignore_poke = 1;
3189 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003190 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 break;
3192 case TIOCL_BLANKEDSCREEN:
3193 ret = console_blanked;
3194 break;
3195 default:
3196 ret = -EINVAL;
3197 break;
3198 }
3199 return ret;
3200}
3201
3202/*
3203 * /dev/ttyN handling
3204 */
3205
3206static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3207{
3208 int retval;
3209
3210 retval = do_con_write(tty, buf, count);
3211 con_flush_chars(tty);
3212
3213 return retval;
3214}
3215
Alan Cox5d19f542008-04-30 00:54:08 -07003216static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217{
3218 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07003219 return 0; /* n_r3964 calls put_char() from interrupt context */
3220 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221}
3222
3223static int con_write_room(struct tty_struct *tty)
3224{
3225 if (tty->stopped)
3226 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00003227 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228}
3229
3230static int con_chars_in_buffer(struct tty_struct *tty)
3231{
3232 return 0; /* we're not buffering */
3233}
3234
3235/*
3236 * con_throttle and con_unthrottle are only used for
3237 * paste_selection(), which has to stuff in a large number of
3238 * characters...
3239 */
3240static void con_throttle(struct tty_struct *tty)
3241{
3242}
3243
3244static void con_unthrottle(struct tty_struct *tty)
3245{
3246 struct vc_data *vc = tty->driver_data;
3247
3248 wake_up_interruptible(&vc->paste_wait);
3249}
3250
3251/*
3252 * Turn the Scroll-Lock LED on when the tty is stopped
3253 */
3254static void con_stop(struct tty_struct *tty)
3255{
3256 int console_num;
3257 if (!tty)
3258 return;
3259 console_num = tty->index;
3260 if (!vc_cons_allocated(console_num))
3261 return;
Alan Cox079c9532012-02-28 14:49:23 +00003262 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
3265/*
3266 * Turn the Scroll-Lock LED off when the console is started
3267 */
3268static void con_start(struct tty_struct *tty)
3269{
3270 int console_num;
3271 if (!tty)
3272 return;
3273 console_num = tty->index;
3274 if (!vc_cons_allocated(console_num))
3275 return;
Alan Cox079c9532012-02-28 14:49:23 +00003276 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277}
3278
3279static void con_flush_chars(struct tty_struct *tty)
3280{
3281 struct vc_data *vc;
3282
3283 if (in_interrupt()) /* from flush_to_ldisc */
3284 return;
3285
3286 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08003287 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 vc = tty->driver_data;
3289 if (vc)
3290 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08003291 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292}
3293
3294/*
3295 * Allocate the console screen memory.
3296 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003297static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298{
3299 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003300 struct vc_data *vc;
3301 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Torben Hohnac751ef2011-01-25 15:07:35 -08003303 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003304 ret = vc_allocate(currcons);
3305 if (ret)
3306 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01003307
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003308 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003310 /* Still being freed */
3311 if (vc->port.tty) {
3312 ret = -ERESTARTSYS;
3313 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003315
3316 ret = tty_port_install(&vc->port, driver, tty);
3317 if (ret)
3318 goto unlock;
3319
3320 tty->driver_data = vc;
3321 vc->port.tty = tty;
Eric Biggersca4463b2020-03-21 20:43:04 -07003322 tty_port_get(&vc->port);
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003323
3324 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3325 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3326 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3327 }
3328 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01003329 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003330 else
Alan Coxadc8d742012-07-14 15:31:47 +01003331 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003332unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08003333 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 return ret;
3335}
3336
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003337static int con_open(struct tty_struct *tty, struct file *filp)
3338{
3339 /* everything done in install */
3340 return 0;
3341}
3342
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344static void con_close(struct tty_struct *tty, struct file *filp)
3345{
Alan Coxfeebed62008-10-13 10:41:30 +01003346 /* Nothing to do - we defer to shutdown */
3347}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Alan Coxfeebed62008-10-13 10:41:30 +01003349static void con_shutdown(struct tty_struct *tty)
3350{
3351 struct vc_data *vc = tty->driver_data;
3352 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08003353 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02003354 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08003355 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
Eric Biggersca4463b2020-03-21 20:43:04 -07003358static void con_cleanup(struct tty_struct *tty)
3359{
3360 struct vc_data *vc = tty->driver_data;
3361
3362 tty_port_put(&vc->port);
3363}
3364
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003365static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003366static int default_italic_color = 2; // green (ASCII)
3367static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003368module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003369module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3370module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372static void vc_init(struct vc_data *vc, unsigned int rows,
3373 unsigned int cols, int do_clear)
3374{
3375 int j, k ;
3376
3377 vc->vc_cols = cols;
3378 vc->vc_rows = rows;
3379 vc->vc_size_row = cols << 1;
3380 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3381
3382 set_origin(vc);
3383 vc->vc_pos = vc->vc_origin;
3384 reset_vc(vc);
3385 for (j=k=0; j<16; j++) {
3386 vc->vc_palette[k++] = default_red[j] ;
3387 vc->vc_palette[k++] = default_grn[j] ;
3388 vc->vc_palette[k++] = default_blu[j] ;
3389 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003390 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003391 vc->vc_ulcolor = default_underline_color;
3392 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 vc->vc_halfcolor = 0x08; /* grey */
3394 init_waitqueue_head(&vc->paste_wait);
3395 reset_terminal(vc, do_clear);
3396}
3397
3398/*
3399 * This routine initializes console interrupts, and does nothing
3400 * else. If you want the screen to clear, call tty_write with
3401 * the appropriate escape-sequence.
3402 */
3403
3404static int __init con_init(void)
3405{
3406 const char *display_desc = NULL;
3407 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003408 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409
Torben Hohnac751ef2011-01-25 15:07:35 -08003410 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411
Arvind Sankar805ece22019-12-18 16:44:44 -05003412 if (!conswitchp)
3413 conswitchp = &dummy_con;
3414 display_desc = conswitchp->con_startup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415 if (!display_desc) {
3416 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08003417 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 return 0;
3419 }
3420
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003421 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3422 struct con_driver *con_driver = &registered_con_driver[i];
3423
3424 if (con_driver->con == NULL) {
3425 con_driver->con = conswitchp;
3426 con_driver->desc = display_desc;
3427 con_driver->flag = CON_DRIVER_FLAG_INIT;
3428 con_driver->first = 0;
3429 con_driver->last = MAX_NR_CONSOLES - 1;
3430 break;
3431 }
3432 }
3433
3434 for (i = 0; i < MAX_NR_CONSOLES; i++)
3435 con_driver_map[i] = conswitchp;
3436
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 if (blankinterval) {
3438 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003439 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 }
3441
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003443 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003444 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003445 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003447 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 vc_init(vc, vc->vc_rows, vc->vc_cols,
3449 currcons || !vc->vc_sw->con_save_screen);
3450 }
3451 currcons = fg_console = 0;
3452 master_display_fg = vc = vc_cons[currcons].d;
3453 set_origin(vc);
3454 save_screen(vc);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003455 gotoxy(vc, vc->state.x, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 csi_J(vc, 0);
3457 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003458 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 vc->vc_can_do_color ? "colour" : "mono",
3460 display_desc, vc->vc_cols, vc->vc_rows);
3461 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Torben Hohnac751ef2011-01-25 15:07:35 -08003463 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
3465#ifdef CONFIG_VT_CONSOLE
3466 register_console(&vt_console_driver);
3467#endif
3468 return 0;
3469}
3470console_initcall(con_init);
3471
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003472static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003473 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 .open = con_open,
3475 .close = con_close,
3476 .write = con_write,
3477 .write_room = con_write_room,
3478 .put_char = con_put_char,
3479 .flush_chars = con_flush_chars,
3480 .chars_in_buffer = con_chars_in_buffer,
3481 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003482#ifdef CONFIG_COMPAT
3483 .compat_ioctl = vt_compat_ioctl,
3484#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .stop = con_stop,
3486 .start = con_start,
3487 .throttle = con_throttle,
3488 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003489 .resize = vt_resize,
Eric Biggersca4463b2020-03-21 20:43:04 -07003490 .shutdown = con_shutdown,
3491 .cleanup = con_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492};
3493
Alan Coxd81ed102008-10-13 10:41:42 +01003494static struct cdev vc0_cdev;
3495
Kay Sieversfbc92a32010-12-01 18:51:05 +01003496static ssize_t show_tty_active(struct device *dev,
3497 struct device_attribute *attr, char *buf)
3498{
3499 return sprintf(buf, "tty%d\n", fg_console + 1);
3500}
3501static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3502
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003503static struct attribute *vt_dev_attrs[] = {
3504 &dev_attr_active.attr,
3505 NULL
3506};
3507
3508ATTRIBUTE_GROUPS(vt_dev);
3509
Alan Coxd81ed102008-10-13 10:41:42 +01003510int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511{
Alan Coxd81ed102008-10-13 10:41:42 +01003512 cdev_init(&vc0_cdev, console_fops);
3513 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3514 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3515 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003516 tty0dev = device_create_with_groups(tty_class, NULL,
3517 MKDEV(TTY_MAJOR, 0), NULL,
3518 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003519 if (IS_ERR(tty0dev))
3520 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 vcs_init();
3523
3524 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3525 if (!console_driver)
3526 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003527
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528 console_driver->name = "tty";
3529 console_driver->name_base = 1;
3530 console_driver->major = TTY_MAJOR;
3531 console_driver->minor_start = 1;
3532 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3533 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003534 if (default_utf8)
3535 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3537 tty_set_operations(console_driver, &con_ops);
3538 if (tty_register_driver(console_driver))
3539 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 kbd_init();
3541 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542#ifdef CONFIG_MDA_CONSOLE
3543 mda_console_init();
3544#endif
3545 return 0;
3546}
3547
3548#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003549
3550static struct class *vtconsole_class;
3551
Alan Cox50e244c2013-01-25 10:28:15 +10003552static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003553 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003555 struct module *owner = csw->owner;
3556 const char *desc = NULL;
3557 struct con_driver *con_driver;
3558 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 if (!try_module_get(owner))
3561 return -ENODEV;
3562
Alan Cox50e244c2013-01-25 10:28:15 +10003563 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003564
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003565 /* check if driver is registered */
3566 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3567 con_driver = &registered_con_driver[i];
3568
3569 if (con_driver->con == csw) {
3570 desc = con_driver->desc;
3571 retval = 0;
3572 break;
3573 }
3574 }
3575
3576 if (retval)
3577 goto err;
3578
3579 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3580 csw->con_startup();
3581 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 if (deflt) {
3585 if (conswitchp)
3586 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 __module_get(owner);
3589 conswitchp = csw;
3590 }
3591
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003592 first = max(first, con_driver->first);
3593 last = min(last, con_driver->last);
3594
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595 for (i = first; i <= last; i++) {
3596 int old_was_color;
3597 struct vc_data *vc = vc_cons[i].d;
3598
3599 if (con_driver_map[i])
3600 module_put(con_driver_map[i]->owner);
3601 __module_get(owner);
3602 con_driver_map[i] = csw;
3603
3604 if (!vc || !vc->vc_sw)
3605 continue;
3606
3607 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003608
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003609 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003610 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003612 }
3613
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 old_was_color = vc->vc_can_do_color;
3615 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003616 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003618 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 update_attr(vc);
3620
3621 /* If the console changed between mono <-> color, then
3622 * the attributes in the screenbuf will be wrong. The
3623 * following resets all attributes to something sane.
3624 */
3625 if (old_was_color != vc->vc_can_do_color)
3626 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003628
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003629 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 if (!deflt)
Joe Perchesbccf1da2017-10-02 08:48:55 -07003631 pr_cont("consoles %d-%d ", first + 1, last + 1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003632 if (j >= 0) {
3633 struct vc_data *vc = vc_cons[j].d;
3634
Joe Perchesbccf1da2017-10-02 08:48:55 -07003635 pr_cont("to %s %s %dx%d\n",
3636 vc->vc_can_do_color ? "colour" : "mono",
3637 desc, vc->vc_cols, vc->vc_rows);
David Hollister4ee1acc2006-06-26 00:26:41 -07003638
3639 if (k >= 0) {
3640 vc = vc_cons[k].d;
3641 update_screen(vc);
3642 }
Joe Perchesbccf1da2017-10-02 08:48:55 -07003643 } else {
3644 pr_cont("to %s\n", desc);
3645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003647 retval = 0;
3648err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003650 return retval;
3651};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652
Alan Cox50e244c2013-01-25 10:28:15 +10003653
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003654#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003655int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3656{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003657 struct module *owner = csw->owner;
3658 const struct consw *defcsw = NULL;
3659 struct con_driver *con_driver = NULL, *con_back = NULL;
3660 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003662 if (!try_module_get(owner))
3663 return -ENODEV;
3664
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003665 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003666
3667 /* check if driver is registered and if it is unbindable */
3668 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3669 con_driver = &registered_con_driver[i];
3670
3671 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003672 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003673 retval = 0;
3674 break;
3675 }
3676 }
3677
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003678 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003679 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003680
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003681 retval = -ENODEV;
3682
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003683 /* check if backup driver exists */
3684 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3685 con_back = &registered_con_driver[i];
3686
Daniel Vetter249f7b32014-06-05 16:24:47 +02003687 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003688 defcsw = con_back->con;
3689 retval = 0;
3690 break;
3691 }
3692 }
3693
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003694 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003695 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003696
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003697 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003698 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003699
3700 first = max(first, con_driver->first);
3701 last = min(last, con_driver->last);
3702
3703 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 if (con_driver_map[i] == csw) {
3705 module_put(csw->owner);
3706 con_driver_map[i] = NULL;
3707 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003708 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003709
3710 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003711 const struct consw *defconsw = conswitchp;
3712
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003713 defcsw->con_startup();
3714 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003715 /*
3716 * vgacon may change the default driver to point
3717 * to dummycon, we restore it here...
3718 */
3719 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003720 }
3721
3722 if (!con_is_bound(csw))
3723 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3724
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003725 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003726 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003727err:
3728 module_put(owner);
3729 return retval;
3730
3731}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003732EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003733
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003734static int vt_bind(struct con_driver *con)
3735{
3736 const struct consw *defcsw = NULL, *csw = NULL;
3737 int i, more = 1, first = -1, last = -1, deflt = 0;
3738
Daniel Vetter77232f72015-04-13 11:16:21 +02003739 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003740 goto err;
3741
3742 csw = con->con;
3743
3744 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3745 struct con_driver *con = &registered_con_driver[i];
3746
3747 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3748 defcsw = con->con;
3749 break;
3750 }
3751 }
3752
3753 if (!defcsw)
3754 goto err;
3755
3756 while (more) {
3757 more = 0;
3758
3759 for (i = con->first; i <= con->last; i++) {
3760 if (con_driver_map[i] == defcsw) {
3761 if (first == -1)
3762 first = i;
3763 last = i;
3764 more = 1;
3765 } else if (first != -1)
3766 break;
3767 }
3768
3769 if (first == 0 && last == MAX_NR_CONSOLES -1)
3770 deflt = 1;
3771
Imre Deak4c215fe2014-12-16 00:16:00 +02003772 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003773 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003774
3775 first = -1;
3776 last = -1;
3777 deflt = 0;
3778 }
3779
3780err:
3781 return 0;
3782}
3783
3784static int vt_unbind(struct con_driver *con)
3785{
3786 const struct consw *csw = NULL;
3787 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003788 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003789
Daniel Vetter77232f72015-04-13 11:16:21 +02003790 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003791 goto err;
3792
3793 csw = con->con;
3794
3795 while (more) {
3796 more = 0;
3797
3798 for (i = con->first; i <= con->last; i++) {
3799 if (con_driver_map[i] == csw) {
3800 if (first == -1)
3801 first = i;
3802 last = i;
3803 more = 1;
3804 } else if (first != -1)
3805 break;
3806 }
3807
3808 if (first == 0 && last == MAX_NR_CONSOLES -1)
3809 deflt = 1;
3810
Wang YanQing618f2b92013-05-09 02:14:07 +08003811 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003812 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003813 if (ret != 0)
3814 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003815 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003816
3817 first = -1;
3818 last = -1;
3819 deflt = 0;
3820 }
3821
3822err:
3823 return 0;
3824}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003825#else
3826static inline int vt_bind(struct con_driver *con)
3827{
3828 return 0;
3829}
3830static inline int vt_unbind(struct con_driver *con)
3831{
3832 return 0;
3833}
3834#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003835
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003836static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003837 const char *buf, size_t count)
3838{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003839 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003840 int bind = simple_strtoul(buf, NULL, 0);
3841
Imre Deak4c215fe2014-12-16 00:16:00 +02003842 console_lock();
3843
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003844 if (bind)
3845 vt_bind(con);
3846 else
3847 vt_unbind(con);
3848
Imre Deak4c215fe2014-12-16 00:16:00 +02003849 console_unlock();
3850
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003851 return count;
3852}
3853
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003854static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3855 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003856{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003857 struct con_driver *con = dev_get_drvdata(dev);
Daniel Vetter61d51452019-07-18 10:09:03 +02003858 int bind;
3859
3860 console_lock();
3861 bind = con_is_bound(con->con);
3862 console_unlock();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003863
3864 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3865}
3866
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003867static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3868 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003869{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003870 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003871
3872 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3873 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3874 con->desc);
3875
3876}
3877
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003878static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3879static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3880
3881static struct attribute *con_dev_attrs[] = {
3882 &dev_attr_bind.attr,
3883 &dev_attr_name.attr,
3884 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003885};
3886
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003887ATTRIBUTE_GROUPS(con_dev);
3888
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003889static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003890{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003891 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003892 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003893}
3894
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003895static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003896{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003897 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003898}
3899
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003900/**
3901 * con_is_bound - checks if driver is bound to the console
3902 * @csw: console driver
3903 *
3904 * RETURNS: zero if unbound, nonzero if bound
3905 *
3906 * Drivers can call this and if zero, they should release
3907 * all resources allocated on con_startup()
3908 */
3909int con_is_bound(const struct consw *csw)
3910{
3911 int i, bound = 0;
3912
Daniel Vetterddde3c12019-05-28 11:02:35 +02003913 WARN_CONSOLE_UNLOCKED();
3914
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003915 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3916 if (con_driver_map[i] == csw) {
3917 bound = 1;
3918 break;
3919 }
3920 }
3921
3922 return bound;
3923}
3924EXPORT_SYMBOL(con_is_bound);
3925
3926/**
Daniel Vetterddde3c12019-05-28 11:02:35 +02003927 * con_is_visible - checks whether the current console is visible
3928 * @vc: virtual console
3929 *
3930 * RETURNS: zero if not visible, nonzero if visible
3931 */
3932bool con_is_visible(const struct vc_data *vc)
3933{
3934 WARN_CONSOLE_UNLOCKED();
3935
3936 return *vc->vc_display_fg == vc;
3937}
3938EXPORT_SYMBOL(con_is_visible);
3939
3940/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003941 * con_debug_enter - prepare the console for the kernel debugger
3942 * @sw: console driver
3943 *
3944 * Called when the console is taken over by the kernel debugger, this
3945 * function needs to save the current console state, then put the console
3946 * into a state suitable for the kernel debugger.
3947 *
3948 * RETURNS:
3949 * Zero on success, nonzero if a failure occurred when trying to prepare
3950 * the console for the debugger.
3951 */
3952int con_debug_enter(struct vc_data *vc)
3953{
3954 int ret = 0;
3955
3956 saved_fg_console = fg_console;
3957 saved_last_console = last_console;
3958 saved_want_console = want_console;
3959 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003960 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003961 vc->vc_mode = KD_TEXT;
3962 console_blanked = 0;
3963 if (vc->vc_sw->con_debug_enter)
3964 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003965#ifdef CONFIG_KGDB_KDB
3966 /* Set the initial LINES variable if it is not already set */
3967 if (vc->vc_rows < 999) {
3968 int linecount;
3969 char lns[4];
3970 const char *setargs[3] = {
3971 "set",
3972 "LINES",
3973 lns,
3974 };
3975 if (kdbgetintenv(setargs[0], &linecount)) {
3976 snprintf(lns, 4, "%i", vc->vc_rows);
3977 kdb_set(2, setargs);
3978 }
3979 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003980 if (vc->vc_cols < 999) {
3981 int colcount;
3982 char cols[4];
3983 const char *setargs[3] = {
3984 "set",
3985 "COLUMNS",
3986 cols,
3987 };
3988 if (kdbgetintenv(setargs[0], &colcount)) {
3989 snprintf(cols, 4, "%i", vc->vc_cols);
3990 kdb_set(2, setargs);
3991 }
3992 }
Jason Wessel81d44502010-08-05 09:22:30 -05003993#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003994 return ret;
3995}
3996EXPORT_SYMBOL_GPL(con_debug_enter);
3997
3998/**
3999 * con_debug_leave - restore console state
4000 * @sw: console driver
4001 *
4002 * Restore the console state to what it was before the kernel debugger
4003 * was invoked.
4004 *
4005 * RETURNS:
4006 * Zero on success, nonzero if a failure occurred when trying to restore
4007 * the console.
4008 */
4009int con_debug_leave(void)
4010{
4011 struct vc_data *vc;
4012 int ret = 0;
4013
4014 fg_console = saved_fg_console;
4015 last_console = saved_last_console;
4016 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05004017 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05004018 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
4019
4020 vc = vc_cons[fg_console].d;
4021 if (vc->vc_sw->con_debug_leave)
4022 ret = vc->vc_sw->con_debug_leave(vc);
4023 return ret;
4024}
4025EXPORT_SYMBOL_GPL(con_debug_leave);
4026
Alan Cox50e244c2013-01-25 10:28:15 +10004027static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004028{
4029 struct module *owner = csw->owner;
4030 struct con_driver *con_driver;
4031 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02004032 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004033
Alan Cox50e244c2013-01-25 10:28:15 +10004034 WARN_CONSOLE_UNLOCKED();
4035
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004036 if (!try_module_get(owner))
4037 return -ENODEV;
4038
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004039 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4040 con_driver = &registered_con_driver[i];
4041
4042 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02004043 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10004044 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02004045 goto err;
4046 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004047 }
4048
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004049 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02004050 if (!desc) {
4051 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004052 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02004053 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004054
4055 retval = -EINVAL;
4056
4057 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4058 con_driver = &registered_con_driver[i];
4059
Imre Deakd364b5c2015-04-01 21:06:16 +03004060 if (con_driver->con == NULL &&
4061 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004062 con_driver->con = csw;
4063 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004064 con_driver->node = i;
4065 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004066 CON_DRIVER_FLAG_INIT;
4067 con_driver->first = first;
4068 con_driver->last = last;
4069 retval = 0;
4070 break;
4071 }
4072 }
4073
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004074 if (retval)
4075 goto err;
4076
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004077 con_driver->dev =
4078 device_create_with_groups(vtconsole_class, NULL,
4079 MKDEV(0, con_driver->node),
4080 con_driver, con_dev_groups,
4081 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004082 if (IS_ERR(con_driver->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004083 pr_warn("Unable to create device for %s; errno = %ld\n",
4084 con_driver->desc, PTR_ERR(con_driver->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004085 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004086 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004087 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004088 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07004089
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004090err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004091 module_put(owner);
4092 return retval;
4093}
Alan Cox50e244c2013-01-25 10:28:15 +10004094
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004095
4096/**
Wang YanQing50539dd2013-05-09 02:14:44 +08004097 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004098 * @csw: console driver
4099 *
4100 * DESCRIPTION: All drivers that registers to the console layer must
4101 * call this function upon exit, or if the console driver is in a state
4102 * where it won't be able to handle console services, such as the
4103 * framebuffer console without loaded framebuffer drivers.
4104 *
4105 * The driver must unbind first prior to unregistration.
4106 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004107int do_unregister_con_driver(const struct consw *csw)
4108{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004109 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004110
4111 /* cannot unregister a bound driver */
4112 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004113 return -EBUSY;
4114
4115 if (csw == conswitchp)
4116 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004117
4118 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4119 struct con_driver *con_driver = &registered_con_driver[i];
4120
Imre Deak2cf30f72014-12-16 00:15:59 +02004121 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03004122 /*
4123 * Defer the removal of the sysfs entries since that
4124 * will acquire the kernfs s_active lock and we can't
4125 * acquire this lock while holding the console lock:
4126 * the unbind sysfs entry imposes already the opposite
4127 * order. Reset con already here to prevent any later
4128 * lookup to succeed and mark this slot as zombie, so
4129 * it won't get reused until we complete the removal
4130 * in the deferred work.
4131 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004132 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03004133 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4134 schedule_work(&con_driver_unregister_work);
4135
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004136 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004137 }
4138 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004139
4140 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004141}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004142EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004143
Imre Deakd364b5c2015-04-01 21:06:16 +03004144static void con_driver_unregister_callback(struct work_struct *ignored)
4145{
4146 int i;
4147
4148 console_lock();
4149
4150 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4151 struct con_driver *con_driver = &registered_con_driver[i];
4152
4153 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4154 continue;
4155
4156 console_unlock();
4157
4158 vtconsole_deinit_device(con_driver);
4159 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4160
4161 console_lock();
4162
4163 if (WARN_ON_ONCE(con_driver->con))
4164 con_driver->con = NULL;
4165 con_driver->desc = NULL;
4166 con_driver->dev = NULL;
4167 con_driver->node = 0;
4168 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4169 con_driver->flag = 0;
4170 con_driver->first = 0;
4171 con_driver->last = 0;
4172 }
4173
4174 console_unlock();
4175}
4176
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004177/*
4178 * If we support more console drivers, this function is used
4179 * when a driver wants to take over some existing consoles
4180 * and become default driver for newly opened ones.
4181 *
Lukas Wunner0095ab42020-01-09 13:59:21 +01004182 * do_take_over_console is basically a register followed by bind
Alan Cox50e244c2013-01-25 10:28:15 +10004183 */
4184int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4185{
4186 int err;
4187
4188 err = do_register_con_driver(csw, first, last);
4189 /*
4190 * If we get an busy error we still want to bind the console driver
4191 * and return success, as we may have unbound the console driver
4192 * but not unregistered it.
4193 */
4194 if (err == -EBUSY)
4195 err = 0;
4196 if (!err)
4197 do_bind_con_driver(csw, first, last, deflt);
4198
4199 return err;
4200}
4201EXPORT_SYMBOL_GPL(do_take_over_console);
4202
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004203
4204/*
4205 * give_up_console is a wrapper to unregister_con_driver. It will only
4206 * work if driver is fully unbound.
4207 */
4208void give_up_console(const struct consw *csw)
4209{
Wang YanQing70125e72013-05-09 02:14:39 +08004210 console_lock();
4211 do_unregister_con_driver(csw);
4212 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004213}
4214
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004215static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004216{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004217 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004218
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004219 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4220 if (IS_ERR(vtconsole_class)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004221 pr_warn("Unable to create vt console class; errno = %ld\n",
4222 PTR_ERR(vtconsole_class));
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004223 vtconsole_class = NULL;
4224 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004225
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004226 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004227 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4228 struct con_driver *con = &registered_con_driver[i];
4229
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004230 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004231 con->dev =
4232 device_create_with_groups(vtconsole_class, NULL,
4233 MKDEV(0, con->node),
4234 con, con_dev_groups,
4235 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004236
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004237 if (IS_ERR(con->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004238 pr_warn("Unable to create device for %s; errno = %ld\n",
4239 con->desc, PTR_ERR(con->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004240 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004241 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004242 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004243 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004244 }
4245 }
4246
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004247 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004248}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004249postcore_initcall(vtconsole_class_init);
4250
4251#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004252
4253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 * Screen blanking
4255 */
4256
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004257static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004259 unsigned int mode;
4260
4261 if (get_user(mode, p + 1))
4262 return -EFAULT;
4263
4264 vesa_blank_mode = (mode < 4) ? mode : 0;
4265 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268void do_blank_screen(int entering_gfx)
4269{
4270 struct vc_data *vc = vc_cons[fg_console].d;
4271 int i;
4272
Daniel Vettera1355132019-05-28 11:02:34 +02004273 might_sleep();
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 WARN_CONSOLE_UNLOCKED();
4276
4277 if (console_blanked) {
4278 if (blank_state == blank_vesa_wait) {
4279 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08004280 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 }
4282 return;
4283 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
4285 /* entering graphics mode? */
4286 if (entering_gfx) {
4287 hide_cursor(vc);
4288 save_screen(vc);
4289 vc->vc_sw->con_blank(vc, -1, 1);
4290 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07004291 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 set_origin(vc);
4293 return;
4294 }
4295
izumib6e8f002007-07-17 04:05:49 -07004296 blank_state = blank_off;
4297
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 /* don't blank graphics */
4299 if (vc->vc_mode != KD_TEXT) {
4300 console_blanked = fg_console + 1;
4301 return;
4302 }
4303
4304 hide_cursor(vc);
4305 del_timer_sync(&console_timer);
4306 blank_timer_expired = 0;
4307
4308 save_screen(vc);
4309 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08004310 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 console_blanked = fg_console + 1;
4312 if (i)
4313 set_origin(vc);
4314
4315 if (console_blank_hook && console_blank_hook(1))
4316 return;
4317
Ville Syrjalad060a322006-01-09 20:53:49 -08004318 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07004319 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 mod_timer(&console_timer, jiffies + vesa_off_interval);
4321 }
Alan Cox8b92e872009-09-19 13:13:24 -07004322 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323}
4324EXPORT_SYMBOL(do_blank_screen);
4325
4326/*
4327 * Called by timer as well as from vt_console_driver
4328 */
4329void do_unblank_screen(int leaving_gfx)
4330{
4331 struct vc_data *vc;
4332
4333 /* This should now always be called from a "sane" (read: can schedule)
4334 * context for the sake of the low level drivers, except in the special
4335 * case of oops_in_progress
4336 */
4337 if (!oops_in_progress)
4338 might_sleep();
4339
4340 WARN_CONSOLE_UNLOCKED();
4341
4342 ignore_poke = 0;
4343 if (!console_blanked)
4344 return;
4345 if (!vc_cons_allocated(fg_console)) {
4346 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08004347 pr_warn("unblank_screen: tty %d not allocated ??\n",
4348 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 return;
4350 }
4351 vc = vc_cons[fg_console].d;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004352 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 return; /* but leave console_blanked != 0 */
4354
4355 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004356 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 blank_state = blank_normal_wait;
4358 }
4359
4360 console_blanked = 0;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004361 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 /* Low-level driver cannot restore -> do it ourselves */
4363 update_screen(vc);
4364 if (console_blank_hook)
4365 console_blank_hook(0);
4366 set_palette(vc);
4367 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07004368 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369}
4370EXPORT_SYMBOL(do_unblank_screen);
4371
4372/*
4373 * This is called by the outside world to cause a forced unblank, mostly for
4374 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4375 * call it with 1 as an argument and so force a mode restore... that may kill
4376 * X or at least garbage the screen but would also make the Oops visible...
4377 */
4378void unblank_screen(void)
4379{
4380 do_unblank_screen(0);
4381}
4382
4383/*
Ingo Molnar70522e12006-03-23 03:00:31 -08004384 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08004386 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 */
Kees Cook24ed9602017-08-28 11:28:21 -07004388static void blank_screen_t(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 blank_timer_expired = 1;
4391 schedule_work(&console_work);
4392}
4393
4394void poke_blanked_console(void)
4395{
4396 WARN_CONSOLE_UNLOCKED();
4397
4398 /* Add this so we quickly catch whoever might call us in a non
4399 * safe context. Nowadays, unblank_screen() isn't to be called in
4400 * atomic contexts and is allowed to schedule (with the special case
4401 * of oops_in_progress, but that isn't of any concern for this
4402 * function. --BenH.
4403 */
4404 might_sleep();
4405
4406 /* This isn't perfectly race free, but a race here would be mostly harmless,
4407 * at worse, we'll do a spurrious blank and it's unlikely
4408 */
4409 del_timer(&console_timer);
4410 blank_timer_expired = 0;
4411
4412 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4413 return;
4414 if (console_blanked)
4415 unblank_screen();
4416 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004417 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 blank_state = blank_normal_wait;
4419 }
4420}
4421
4422/*
4423 * Palettes
4424 */
4425
4426static void set_palette(struct vc_data *vc)
4427{
4428 WARN_CONSOLE_UNLOCKED();
4429
Jiri Slaby709280d2016-06-23 13:34:27 +02004430 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 vc->vc_sw->con_set_palette(vc, color_table);
4432}
4433
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434/*
4435 * Load palette into the DAC registers. arg points to a colour
4436 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4437 */
4438
4439int con_set_cmap(unsigned char __user *arg)
4440{
Michael Gehring871bdea2012-03-21 01:26:45 +01004441 int i, j, k;
4442 unsigned char colormap[3*16];
4443
4444 if (copy_from_user(colormap, arg, sizeof(colormap)))
4445 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Torben Hohnac751ef2011-01-25 15:07:35 -08004447 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004448 for (i = k = 0; i < 16; i++) {
4449 default_red[i] = colormap[k++];
4450 default_grn[i] = colormap[k++];
4451 default_blu[i] = colormap[k++];
4452 }
4453 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4454 if (!vc_cons_allocated(i))
4455 continue;
4456 for (j = k = 0; j < 16; j++) {
4457 vc_cons[i].d->vc_palette[k++] = default_red[j];
4458 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4459 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4460 }
4461 set_palette(vc_cons[i].d);
4462 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004463 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Michael Gehring871bdea2012-03-21 01:26:45 +01004465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004466}
4467
4468int con_get_cmap(unsigned char __user *arg)
4469{
Michael Gehring871bdea2012-03-21 01:26:45 +01004470 int i, k;
4471 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
Torben Hohnac751ef2011-01-25 15:07:35 -08004473 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004474 for (i = k = 0; i < 16; i++) {
4475 colormap[k++] = default_red[i];
4476 colormap[k++] = default_grn[i];
4477 colormap[k++] = default_blu[i];
4478 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004479 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480
Michael Gehring871bdea2012-03-21 01:26:45 +01004481 if (copy_to_user(arg, colormap, sizeof(colormap)))
4482 return -EFAULT;
4483
4484 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485}
4486
4487void reset_palette(struct vc_data *vc)
4488{
4489 int j, k;
4490 for (j=k=0; j<16; j++) {
4491 vc->vc_palette[k++] = default_red[j];
4492 vc->vc_palette[k++] = default_grn[j];
4493 vc->vc_palette[k++] = default_blu[j];
4494 }
4495 set_palette(vc);
4496}
4497
4498/*
4499 * Font switching
4500 *
4501 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4502 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4503 * depending on width) reserved for each character which is kinda wasty, but
4504 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004505 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 * format (maybe we should add a `fontoffset' field to the `display'
4507 * structure so we won't have to convert the fontdata all the time.
4508 * /Jes
4509 */
4510
4511#define max_font_size 65536
4512
4513static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4514{
4515 struct console_font font;
4516 int rc = -EINVAL;
4517 int c;
4518
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 if (op->data) {
4520 font.data = kmalloc(max_font_size, GFP_KERNEL);
4521 if (!font.data)
4522 return -ENOMEM;
4523 } else
4524 font.data = NULL;
4525
Torben Hohnac751ef2011-01-25 15:07:35 -08004526 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004527 if (vc->vc_mode != KD_TEXT)
4528 rc = -EINVAL;
4529 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 rc = vc->vc_sw->con_font_get(vc, &font);
4531 else
4532 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004533 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
4535 if (rc)
4536 goto out;
4537
4538 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (op->data && font.charcount > op->charcount)
4541 rc = -ENOSPC;
4542 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4543 if (font.width > op->width || font.height > op->height)
4544 rc = -ENOSPC;
4545 } else {
4546 if (font.width != 8)
4547 rc = -EIO;
4548 else if ((op->height && font.height > op->height) ||
4549 font.height > 32)
4550 rc = -ENOSPC;
4551 }
4552 if (rc)
4553 goto out;
4554
4555 op->height = font.height;
4556 op->width = font.width;
4557 op->charcount = font.charcount;
4558
4559 if (op->data && copy_to_user(op->data, font.data, c))
4560 rc = -EFAULT;
4561
4562out:
4563 kfree(font.data);
4564 return rc;
4565}
4566
4567static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4568{
4569 struct console_font font;
4570 int rc = -EINVAL;
4571 int size;
4572
4573 if (vc->vc_mode != KD_TEXT)
4574 return -EINVAL;
4575 if (!op->data)
4576 return -EINVAL;
4577 if (op->charcount > 512)
4578 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579 if (op->width <= 0 || op->width > 32 || op->height > 32)
4580 return -EINVAL;
4581 size = (op->width+7)/8 * 32 * op->charcount;
4582 if (size > max_font_size)
4583 return -ENOSPC;
Meng Xu8ffb8202017-10-04 10:38:37 -04004584
Julia Lawall9b71ca22010-05-26 14:42:11 -07004585 font.data = memdup_user(op->data, size);
4586 if (IS_ERR(font.data))
4587 return PTR_ERR(font.data);
Meng Xu8ffb8202017-10-04 10:38:37 -04004588
4589 if (!op->height) { /* Need to guess font height [compat] */
4590 int h, i;
4591 u8 *charmap = font.data;
4592
4593 /*
4594 * If from KDFONTOP ioctl, don't allow things which can be done
4595 * in userland,so that we can get rid of this soon
4596 */
4597 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4598 kfree(font.data);
4599 return -EINVAL;
4600 }
4601
4602 for (h = 32; h > 0; h--)
4603 for (i = 0; i < op->charcount; i++)
4604 if (charmap[32*i+h-1])
4605 goto nonzero;
4606
4607 kfree(font.data);
4608 return -EINVAL;
4609
4610 nonzero:
4611 op->height = h;
4612 }
4613
4614 font.charcount = op->charcount;
4615 font.width = op->width;
4616 font.height = op->height;
4617
Torben Hohnac751ef2011-01-25 15:07:35 -08004618 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004619 if (vc->vc_mode != KD_TEXT)
4620 rc = -EINVAL;
4621 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4623 else
4624 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004625 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 kfree(font.data);
4627 return rc;
4628}
4629
4630static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4631{
4632 struct console_font font = {.width = op->width, .height = op->height};
4633 char name[MAX_FONT_NAME];
4634 char *s = name;
4635 int rc;
4636
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
4638 if (!op->data)
4639 s = NULL;
4640 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4641 return -EFAULT;
4642 else
4643 name[MAX_FONT_NAME - 1] = 0;
4644
Torben Hohnac751ef2011-01-25 15:07:35 -08004645 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004646 if (vc->vc_mode != KD_TEXT) {
4647 console_unlock();
4648 return -EINVAL;
4649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 if (vc->vc_sw->con_font_default)
4651 rc = vc->vc_sw->con_font_default(vc, &font, s);
4652 else
4653 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004654 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 if (!rc) {
4656 op->width = font.width;
4657 op->height = font.height;
4658 }
4659 return rc;
4660}
4661
4662static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4663{
4664 int con = op->height;
4665 int rc;
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Torben Hohnac751ef2011-01-25 15:07:35 -08004668 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004669 if (vc->vc_mode != KD_TEXT)
4670 rc = -EINVAL;
4671 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 rc = -ENOSYS;
4673 else if (con < 0 || !vc_cons_allocated(con))
4674 rc = -ENOTTY;
4675 else if (con == vc->vc_num) /* nothing to do */
4676 rc = 0;
4677 else
4678 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004679 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 return rc;
4681}
4682
4683int con_font_op(struct vc_data *vc, struct console_font_op *op)
4684{
4685 switch (op->op) {
4686 case KD_FONT_OP_SET:
4687 return con_font_set(vc, op);
4688 case KD_FONT_OP_GET:
4689 return con_font_get(vc, op);
4690 case KD_FONT_OP_SET_DEFAULT:
4691 return con_font_default(vc, op);
4692 case KD_FONT_OP_COPY:
4693 return con_font_copy(vc, op);
4694 }
4695 return -ENOSYS;
4696}
4697
4698/*
4699 * Interface exported to selection and vcs.
4700 */
4701
4702/* used by selection */
4703u16 screen_glyph(struct vc_data *vc, int offset)
4704{
4705 u16 w = scr_readw(screenpos(vc, offset, 1));
4706 u16 c = w & 0xff;
4707
4708 if (w & vc->vc_hi_font_mask)
4709 c |= 0x100;
4710 return c;
4711}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004712EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713
Adam Borowski9bfdc262018-07-18 04:10:44 +02004714u32 screen_glyph_unicode(struct vc_data *vc, int n)
4715{
4716 struct uni_screen *uniscr = get_vc_uniscr(vc);
4717
4718 if (uniscr)
4719 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4720 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4721}
4722EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4723
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724/* used by vcs - note the word offset */
4725unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4726{
4727 return screenpos(vc, 2 * w_offset, viewed);
4728}
Samuel Thibault88867e32016-01-25 01:32:08 +01004729EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731void getconsxy(struct vc_data *vc, unsigned char *p)
4732{
Nicolas Pitre8a085492019-01-08 22:55:02 -05004733 /* clamp values if they don't fit */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02004734 p[0] = min(vc->state.x, 0xFFu);
4735 p[1] = min(vc->state.y, 0xFFu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736}
4737
4738void putconsxy(struct vc_data *vc, unsigned char *p)
4739{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004740 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 gotoxy(vc, p[0], p[1]);
4742 set_cursor(vc);
4743}
4744
4745u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4746{
4747 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4748 return softcursor_original;
4749 return scr_readw(org);
4750}
4751
4752void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4753{
4754 scr_writew(val, org);
4755 if ((unsigned long)org == vc->vc_pos) {
4756 softcursor_original = -1;
4757 add_softcursor(vc);
4758 }
4759}
4760
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004761void vcs_scr_updated(struct vc_data *vc)
4762{
4763 notify_update(vc);
4764}
4765
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004766void vc_scrolldelta_helper(struct vc_data *c, int lines,
4767 unsigned int rolled_over, void *base, unsigned int size)
4768{
4769 unsigned long ubase = (unsigned long)base;
Jiri Slaby210fd742016-10-03 11:18:36 +02004770 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4771 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4772 ptrdiff_t origin = (void *)c->vc_origin - base;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004773 int margin = c->vc_size_row * 4;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004774 int from, wrap, from_off, avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004775
4776 /* Turn scrollback off */
4777 if (!lines) {
4778 c->vc_visible_origin = c->vc_origin;
4779 return;
4780 }
4781
4782 /* Do we have already enough to allow jumping from 0 to the end? */
Jiri Slaby210fd742016-10-03 11:18:36 +02004783 if (rolled_over > scr_end + margin) {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004784 from = scr_end;
4785 wrap = rolled_over + c->vc_size_row;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004786 } else {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004787 from = 0;
4788 wrap = size;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004789 }
4790
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004791 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4792 avail = (origin - from + wrap) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004793
4794 /* Only a little piece would be left? Show all incl. the piece! */
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004795 if (avail < 2 * margin)
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004796 margin = 0;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004797 if (from_off < margin)
4798 from_off = 0;
4799 if (from_off > avail - margin)
4800 from_off = avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004801
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004802 c->vc_visible_origin = ubase + (from + from_off) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004803}
4804EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4805
Linus Torvalds1da177e2005-04-16 15:20:36 -07004806/*
4807 * Visible symbols for modules
4808 */
4809
4810EXPORT_SYMBOL(color_table);
4811EXPORT_SYMBOL(default_red);
4812EXPORT_SYMBOL(default_grn);
4813EXPORT_SYMBOL(default_blu);
4814EXPORT_SYMBOL(update_region);
4815EXPORT_SYMBOL(redraw_screen);
4816EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817EXPORT_SYMBOL(fg_console);
4818EXPORT_SYMBOL(console_blank_hook);
4819EXPORT_SYMBOL(console_blanked);
4820EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004821EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004822#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823EXPORT_SYMBOL(give_up_console);
4824#endif