blob: b1fdbf119755e6e3b83058a5253df64c4d4d8a27 [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
Jiri Slabye8548292020-06-15 09:48:55 +02001585/* erase the following vpar positions */
1586static void csi_X(struct vc_data *vc, unsigned int vpar)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587{ /* not vt100? */
Jiri Slabye8548292020-06-15 09:48:55 +02001588 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589
1590 if (!vpar)
1591 vpar++;
Jiri Slabye8548292020-06-15 09:48:55 +02001592
1593 count = min(vpar, vc->vc_cols - vc->state.x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001595 vc_uniscr_clear_line(vc, vc->state.x, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02001597 if (con_should_update(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001598 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 vc->vc_need_wrap = 0;
1600}
1601
1602static void default_attr(struct vc_data *vc)
1603{
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001604 vc->state.intensity = VCI_NORMAL;
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001605 vc->state.italic = false;
1606 vc->state.underline = false;
1607 vc->state.reverse = false;
1608 vc->state.blink = false;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001609 vc->state.color = vc->vc_def_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610}
1611
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001612struct rgb { u8 r; u8 g; u8 b; };
1613
Jiri Slaby0f91e142016-06-23 13:34:32 +02001614static void rgb_from_256(int i, struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001615{
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001616 if (i < 8) { /* Standard colours. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001617 c->r = i&1 ? 0xaa : 0x00;
1618 c->g = i&2 ? 0xaa : 0x00;
1619 c->b = i&4 ? 0xaa : 0x00;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001620 } else if (i < 16) {
Jiri Slaby0f91e142016-06-23 13:34:32 +02001621 c->r = i&1 ? 0xff : 0x55;
1622 c->g = i&2 ? 0xff : 0x55;
1623 c->b = i&4 ? 0xff : 0x55;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001624 } else if (i < 232) { /* 6x6x6 colour cube. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001625 c->r = (i - 16) / 36 * 85 / 2;
1626 c->g = (i - 16) / 6 % 6 * 85 / 2;
1627 c->b = (i - 16) % 6 * 85 / 2;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001628 } else /* Grayscale ramp. */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001629 c->r = c->g = c->b = i * 10 - 2312;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001630}
1631
Jiri Slaby0f91e142016-06-23 13:34:32 +02001632static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001633{
Jiri Slaby193df022016-06-23 13:34:33 +02001634 u8 hue = 0, max = max3(c->r, c->g, c->b);
1635
1636 if (c->r > max / 2)
1637 hue |= 4;
1638 if (c->g > max / 2)
1639 hue |= 2;
1640 if (c->b > max / 2)
1641 hue |= 1;
1642
1643 if (hue == 7 && max <= 0x55) {
1644 hue = 0;
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001645 vc->state.intensity = VCI_BOLD;
Jiri Slaby193df022016-06-23 13:34:33 +02001646 } else if (max > 0xaa)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001647 vc->state.intensity = VCI_BOLD;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001648 else
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001649 vc->state.intensity = VCI_NORMAL;
Jiri Slaby193df022016-06-23 13:34:33 +02001650
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001651 vc->state.color = (vc->state.color & 0xf0) | hue;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001652}
1653
Jiri Slaby0f91e142016-06-23 13:34:32 +02001654static void rgb_background(struct vc_data *vc, const struct rgb *c)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001655{
1656 /* For backgrounds, err on the dark side. */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001657 vc->state.color = (vc->state.color & 0x0f)
Jiri Slaby0f91e142016-06-23 13:34:32 +02001658 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001659}
1660
Jiri Slabye05ab232016-06-23 13:34:31 +02001661/*
1662 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
Martin Hostettler04afcd32018-12-15 15:34:23 +01001663 * and thus need to be detected and ignored by hand. That standard also
1664 * wants : rather than ; as separators but sequences containing : are currently
1665 * completely ignored by the parser.
Jiri Slabye05ab232016-06-23 13:34:31 +02001666 *
1667 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1668 * supporting them.
1669 */
1670static int vc_t416_color(struct vc_data *vc, int i,
Jiri Slaby0f91e142016-06-23 13:34:32 +02001671 void(*set_color)(struct vc_data *vc, const struct rgb *c))
Jiri Slabye05ab232016-06-23 13:34:31 +02001672{
Jiri Slaby0f91e142016-06-23 13:34:32 +02001673 struct rgb c;
1674
Jiri Slabye05ab232016-06-23 13:34:31 +02001675 i++;
1676 if (i > vc->vc_npar)
1677 return i;
1678
Adam Borowski0bf1f4a2016-09-15 16:47:10 +02001679 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001680 /* 256 colours */
Jiri Slabye05ab232016-06-23 13:34:31 +02001681 i++;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001682 rgb_from_256(vc->vc_par[i], &c);
Adam Borowski669e0a52016-09-15 16:47:09 +02001683 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
Adam Borowski3e7ec4a2016-09-15 16:47:11 +02001684 /* 24 bit */
Jiri Slaby0f91e142016-06-23 13:34:32 +02001685 c.r = vc->vc_par[i + 1];
1686 c.g = vc->vc_par[i + 2];
1687 c.b = vc->vc_par[i + 3];
Jiri Slabye05ab232016-06-23 13:34:31 +02001688 i += 3;
Jiri Slaby0f91e142016-06-23 13:34:32 +02001689 } else
1690 return i;
1691
1692 set_color(vc, &c);
Jiri Slabye05ab232016-06-23 13:34:31 +02001693
1694 return i;
1695}
1696
Torben Hohnac751ef2011-01-25 15:07:35 -08001697/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698static void csi_m(struct vc_data *vc)
1699{
1700 int i;
1701
1702 for (i = 0; i <= vc->vc_npar; i++)
1703 switch (vc->vc_par[i]) {
Jiri Slabyaada0a32016-06-23 13:34:34 +02001704 case 0: /* all attributes off */
1705 default_attr(vc);
1706 break;
1707 case 1:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001708 vc->state.intensity = VCI_BOLD;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001709 break;
1710 case 2:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001711 vc->state.intensity = VCI_HALF_BRIGHT;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001712 break;
1713 case 3:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001714 vc->state.italic = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001715 break;
Mike Frysinger65d99822018-01-29 17:08:21 -05001716 case 21:
1717 /*
1718 * No console drivers support double underline, so
1719 * convert it to a single underline.
1720 */
Jiri Slabyaada0a32016-06-23 13:34:34 +02001721 case 4:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001722 vc->state.underline = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001723 break;
1724 case 5:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001725 vc->state.blink = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001726 break;
1727 case 7:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001728 vc->state.reverse = true;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001729 break;
1730 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1731 * Select primary font, don't display control chars if
1732 * defined, don't set bit 8 on output.
1733 */
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02001734 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001735 vc->vc_disp_ctrl = 0;
1736 vc->vc_toggle_meta = 0;
1737 break;
1738 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1739 * Select first alternate font, lets chars < 32 be
1740 * displayed as ROM chars.
1741 */
1742 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1743 vc->vc_disp_ctrl = 1;
1744 vc->vc_toggle_meta = 0;
1745 break;
1746 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1747 * Select second alternate font, toggle high bit
1748 * before displaying as ROM char.
1749 */
1750 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1751 vc->vc_disp_ctrl = 1;
1752 vc->vc_toggle_meta = 1;
1753 break;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001754 case 22:
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001755 vc->state.intensity = VCI_NORMAL;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001756 break;
1757 case 23:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001758 vc->state.italic = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001759 break;
1760 case 24:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001761 vc->state.underline = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001762 break;
1763 case 25:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001764 vc->state.blink = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001765 break;
1766 case 27:
Jiri Slaby77bc14f2020-06-15 09:48:35 +02001767 vc->state.reverse = false;
Jiri Slabyaada0a32016-06-23 13:34:34 +02001768 break;
1769 case 38:
1770 i = vc_t416_color(vc, i, rgb_foreground);
1771 break;
1772 case 48:
1773 i = vc_t416_color(vc, i, rgb_background);
1774 break;
1775 case 39:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001776 vc->state.color = (vc->vc_def_color & 0x0f) |
1777 (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001778 break;
1779 case 49:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001780 vc->state.color = (vc->vc_def_color & 0xf0) |
1781 (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001782 break;
1783 default:
Adam Borowskifadb4242016-09-15 16:47:13 +02001784 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1785 if (vc->vc_par[i] < 100)
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001786 vc->state.intensity = VCI_BOLD;
Adam Borowskicc67dc22016-09-15 16:47:12 +02001787 vc->vc_par[i] -= 60;
1788 }
Jiri Slabyaada0a32016-06-23 13:34:34 +02001789 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001790 vc->state.color = color_table[vc->vc_par[i] - 30]
1791 | (vc->state.color & 0xf0);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001792 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001793 vc->state.color = (color_table[vc->vc_par[i] - 40] << 4)
1794 | (vc->state.color & 0x0f);
Jiri Slabyaada0a32016-06-23 13:34:34 +02001795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797 update_attr(vc);
1798}
1799
Jiri Slabyde53ce02020-06-15 09:48:40 +02001800static void respond_string(const char *p, size_t len, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001802 tty_insert_flip_string(port, p, len);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001803 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804}
1805
1806static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1807{
1808 char buf[40];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001809 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Jiri Slabyde53ce02020-06-15 09:48:40 +02001811 len = sprintf(buf, "\033[%d;%dR", vc->state.y +
1812 (vc->vc_decom ? vc->vc_top + 1 : 1),
1813 vc->state.x + 1);
1814 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815}
1816
1817static inline void status_report(struct tty_struct *tty)
1818{
Jiri Slabyde53ce02020-06-15 09:48:40 +02001819 static const char teminal_ok[] = "\033[0n";
1820
1821 respond_string(teminal_ok, strlen(teminal_ok), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001824static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Jiri Slaby9a6f72d2020-06-15 09:48:41 +02001826 /* terminal answer to an ESC-Z or csi0c query. */
1827 static const char vt102_id[] = "\033[?6c";
1828
1829 respond_string(vt102_id, strlen(vt102_id), tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830}
1831
1832void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1833{
1834 char buf[8];
Jiri Slabyde53ce02020-06-15 09:48:40 +02001835 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
Jiri Slabyde53ce02020-06-15 09:48:40 +02001837 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt),
1838 (char)('!' + mrx), (char)('!' + mry));
1839 respond_string(buf, len, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840}
1841
Okash Khawaja496124e2019-04-17 13:21:13 +01001842/* invoked via ioctl(TIOCLINUX) and through set_selection_user */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843int mouse_reporting(void)
1844{
1845 return vc_cons[fg_console].d->vc_report_mouse;
1846}
1847
Torben Hohnac751ef2011-01-25 15:07:35 -08001848/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849static void set_mode(struct vc_data *vc, int on_off)
1850{
1851 int i;
1852
1853 for (i = 0; i <= vc->vc_npar; i++)
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01001854 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1856 case 1: /* Cursor keys send ^[Ox/^[[x */
1857 if (on_off)
1858 set_kbd(vc, decckm);
1859 else
1860 clr_kbd(vc, decckm);
1861 break;
1862 case 3: /* 80/132 mode switch unimplemented */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863#if 0
1864 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1865 /* this alone does not suffice; some user mode
1866 utility has to change the hardware regs */
1867#endif
1868 break;
1869 case 5: /* Inverted screen on/off */
1870 if (vc->vc_decscnm != on_off) {
1871 vc->vc_decscnm = on_off;
1872 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1873 update_attr(vc);
1874 }
1875 break;
1876 case 6: /* Origin relative/absolute */
1877 vc->vc_decom = on_off;
1878 gotoxay(vc, 0, 0);
1879 break;
1880 case 7: /* Autowrap on/off */
1881 vc->vc_decawm = on_off;
1882 break;
1883 case 8: /* Autorepeat on/off */
1884 if (on_off)
1885 set_kbd(vc, decarm);
1886 else
1887 clr_kbd(vc, decarm);
1888 break;
1889 case 9:
1890 vc->vc_report_mouse = on_off ? 1 : 0;
1891 break;
1892 case 25: /* Cursor on/off */
1893 vc->vc_deccm = on_off;
1894 break;
1895 case 1000:
1896 vc->vc_report_mouse = on_off ? 2 : 0;
1897 break;
1898 }
1899 } else {
1900 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1901 case 3: /* Monitor (display ctrls) */
1902 vc->vc_disp_ctrl = on_off;
1903 break;
1904 case 4: /* Insert Mode on/off */
1905 vc->vc_decim = on_off;
1906 break;
1907 case 20: /* Lf, Enter == CrLf/Lf */
1908 if (on_off)
1909 set_kbd(vc, lnm);
1910 else
1911 clr_kbd(vc, lnm);
1912 break;
1913 }
1914 }
1915}
1916
Torben Hohnac751ef2011-01-25 15:07:35 -08001917/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918static void setterm_command(struct vc_data *vc)
1919{
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001920 switch (vc->vc_par[0]) {
1921 case 1: /* set color for underline mode */
1922 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1923 vc->vc_ulcolor = color_table[vc->vc_par[1]];
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001924 if (vc->state.underline)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001925 update_attr(vc);
1926 }
1927 break;
1928 case 2: /* set color for half intensity mode */
1929 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1930 vc->vc_halfcolor = color_table[vc->vc_par[1]];
Jiri Slabyb84ae3d2020-06-15 09:48:34 +02001931 if (vc->state.intensity == VCI_HALF_BRIGHT)
Jiri Slabyc3a834e2020-03-16 07:59:11 +01001932 update_attr(vc);
1933 }
1934 break;
1935 case 8: /* store colors as defaults */
1936 vc->vc_def_color = vc->vc_attr;
1937 if (vc->vc_hi_font_mask == 0x100)
1938 vc->vc_def_color >>= 1;
1939 default_attr(vc);
1940 update_attr(vc);
1941 break;
1942 case 9: /* set blanking interval */
1943 blankinterval = min(vc->vc_par[1], 60U) * 60;
1944 poke_blanked_console();
1945 break;
1946 case 10: /* set bell frequency in Hz */
1947 if (vc->vc_npar >= 1)
1948 vc->vc_bell_pitch = vc->vc_par[1];
1949 else
1950 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1951 break;
1952 case 11: /* set bell duration in msec */
1953 if (vc->vc_npar >= 1)
1954 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1955 msecs_to_jiffies(vc->vc_par[1]) : 0;
1956 else
1957 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1958 break;
1959 case 12: /* bring specified console to the front */
1960 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1961 set_console(vc->vc_par[1] - 1);
1962 break;
1963 case 13: /* unblank the screen */
1964 poke_blanked_console();
1965 break;
1966 case 14: /* set vesa powerdown interval */
1967 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1968 break;
1969 case 15: /* activate the previous console */
1970 set_console(last_console);
1971 break;
1972 case 16: /* set cursor blink duration in msec */
1973 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1974 vc->vc_par[1] <= USHRT_MAX)
1975 vc->vc_cur_blink_ms = vc->vc_par[1];
1976 else
1977 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1978 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
1980}
1981
Torben Hohnac751ef2011-01-25 15:07:35 -08001982/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983static void csi_at(struct vc_data *vc, unsigned int nr)
1984{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001985 if (nr > vc->vc_cols - vc->state.x)
1986 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 else if (!nr)
1988 nr = 1;
1989 insert_char(vc, nr);
1990}
1991
Torben Hohnac751ef2011-01-25 15:07:35 -08001992/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993static void csi_L(struct vc_data *vc, unsigned int nr)
1994{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001995 if (nr > vc->vc_rows - vc->state.y)
1996 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 else if (!nr)
1998 nr = 1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02001999 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 vc->vc_need_wrap = 0;
2001}
2002
Torben Hohnac751ef2011-01-25 15:07:35 -08002003/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004static void csi_P(struct vc_data *vc, unsigned int nr)
2005{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002006 if (nr > vc->vc_cols - vc->state.x)
2007 nr = vc->vc_cols - vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 else if (!nr)
2009 nr = 1;
2010 delete_char(vc, nr);
2011}
2012
Torben Hohnac751ef2011-01-25 15:07:35 -08002013/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014static void csi_M(struct vc_data *vc, unsigned int nr)
2015{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002016 if (nr > vc->vc_rows - vc->state.y)
2017 nr = vc->vc_rows - vc->state.y;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 else if (!nr)
2019 nr=1;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002020 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 vc->vc_need_wrap = 0;
2022}
2023
Torben Hohnac751ef2011-01-25 15:07:35 -08002024/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025static void save_cur(struct vc_data *vc)
2026{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002027 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
2029
Torben Hohnac751ef2011-01-25 15:07:35 -08002030/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031static void restore_cur(struct vc_data *vc)
2032{
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002033 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
2034
2035 gotoxy(vc, vc->state.x, vc->state.y);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002036 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset],
2037 vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 update_attr(vc);
2039 vc->vc_need_wrap = 0;
2040}
2041
Adam Borowskib290af62014-02-19 01:40:16 +01002042enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Martin Hostettler7a995652018-12-15 15:34:22 +01002043 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01002044 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
Torben Hohnac751ef2011-01-25 15:07:35 -08002046/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047static void reset_terminal(struct vc_data *vc, int do_clear)
2048{
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002049 unsigned int i;
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 vc->vc_top = 0;
2052 vc->vc_bottom = vc->vc_rows;
2053 vc->vc_state = ESnormal;
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002054 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 vc->vc_translate = set_translate(LAT1_MAP, vc);
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002056 vc->state.Gx_charset[0] = LAT1_MAP;
2057 vc->state.Gx_charset[1] = GRAF_MAP;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002058 vc->state.charset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 vc->vc_need_wrap = 0;
2060 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07002061 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 vc->vc_utf_count = 0;
2063
2064 vc->vc_disp_ctrl = 0;
2065 vc->vc_toggle_meta = 0;
2066
2067 vc->vc_decscnm = 0;
2068 vc->vc_decom = 0;
2069 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05002070 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 vc->vc_decim = 0;
2072
Alan Cox079c9532012-02-28 14:49:23 +00002073 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002075 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 vc->vc_complement_mask = vc->vc_s_complement_mask;
2077
2078 default_attr(vc);
2079 update_attr(vc);
2080
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002081 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
2082 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8)
2083 set_bit(i, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2086 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00002087 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088
2089 gotoxy(vc, 0, 0);
2090 save_cur(vc);
2091 if (do_clear)
2092 csi_J(vc, 2);
2093}
2094
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002095static void vc_setGx(struct vc_data *vc, unsigned int which, int c)
2096{
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002097 unsigned char *charset = &vc->state.Gx_charset[which];
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002098
2099 switch (c) {
2100 case '0':
2101 *charset = GRAF_MAP;
2102 break;
2103 case 'B':
2104 *charset = LAT1_MAP;
2105 break;
2106 case 'U':
2107 *charset = IBMPC_MAP;
2108 break;
2109 case 'K':
2110 *charset = USER_MAP;
2111 break;
2112 }
2113
2114 if (vc->state.charset == which)
2115 vc->vc_translate = set_translate(*charset, vc);
2116}
2117
Torben Hohnac751ef2011-01-25 15:07:35 -08002118/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2120{
2121 /*
2122 * Control characters can be used in the _middle_
2123 * of an escape sequence.
2124 */
Adam Borowski63f3a162014-02-19 01:38:04 +01002125 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2126 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 switch (c) {
2128 case 0:
2129 return;
2130 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01002131 if (vc->vc_state == ESosc)
2132 vc->vc_state = ESnormal;
2133 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2135 return;
2136 case 8:
2137 bs(vc);
2138 return;
2139 case 9:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002140 vc->vc_pos -= (vc->state.x << 1);
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002141
2142 vc->state.x = find_next_bit(vc->vc_tab_stop,
2143 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT),
2144 vc->state.x + 1);
2145 if (vc->state.x >= VC_TABSTOPS_COUNT)
2146 vc->state.x = vc->vc_cols - 1;
2147
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002148 vc->vc_pos += (vc->state.x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002149 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 return;
2151 case 10: case 11: case 12:
2152 lf(vc);
2153 if (!is_kbd(vc, lnm))
2154 return;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002155 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 case 13:
2157 cr(vc);
2158 return;
2159 case 14:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002160 vc->state.charset = 1;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002161 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 vc->vc_disp_ctrl = 1;
2163 return;
2164 case 15:
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002165 vc->state.charset = 0;
Jiri Slabyb70ec4d2020-06-15 09:48:37 +02002166 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 vc->vc_disp_ctrl = 0;
2168 return;
2169 case 24: case 26:
2170 vc->vc_state = ESnormal;
2171 return;
2172 case 27:
2173 vc->vc_state = ESesc;
2174 return;
2175 case 127:
2176 del(vc);
2177 return;
2178 case 128+27:
2179 vc->vc_state = ESsquare;
2180 return;
2181 }
2182 switch(vc->vc_state) {
2183 case ESesc:
2184 vc->vc_state = ESnormal;
2185 switch (c) {
2186 case '[':
2187 vc->vc_state = ESsquare;
2188 return;
2189 case ']':
2190 vc->vc_state = ESnonstd;
2191 return;
2192 case '%':
2193 vc->vc_state = ESpercent;
2194 return;
2195 case 'E':
2196 cr(vc);
2197 lf(vc);
2198 return;
2199 case 'M':
2200 ri(vc);
2201 return;
2202 case 'D':
2203 lf(vc);
2204 return;
2205 case 'H':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002206 if (vc->state.x < VC_TABSTOPS_COUNT)
2207 set_bit(vc->state.x, vc->vc_tab_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 return;
2209 case 'Z':
2210 respond_ID(tty);
2211 return;
2212 case '7':
2213 save_cur(vc);
2214 return;
2215 case '8':
2216 restore_cur(vc);
2217 return;
2218 case '(':
2219 vc->vc_state = ESsetG0;
2220 return;
2221 case ')':
2222 vc->vc_state = ESsetG1;
2223 return;
2224 case '#':
2225 vc->vc_state = EShash;
2226 return;
2227 case 'c':
2228 reset_terminal(vc, 1);
2229 return;
2230 case '>': /* Numeric keypad */
2231 clr_kbd(vc, kbdapplic);
2232 return;
2233 case '=': /* Appl. keypad */
2234 set_kbd(vc, kbdapplic);
2235 return;
2236 }
2237 return;
2238 case ESnonstd:
2239 if (c=='P') { /* palette escape sequence */
2240 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2241 vc->vc_par[vc->vc_npar] = 0;
2242 vc->vc_npar = 0;
2243 vc->vc_state = ESpalette;
2244 return;
2245 } else if (c=='R') { /* reset palette */
2246 reset_palette(vc);
2247 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01002248 } else if (c>='0' && c<='9')
2249 vc->vc_state = ESosc;
2250 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 vc->vc_state = ESnormal;
2252 return;
2253 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03002254 if (isxdigit(c)) {
2255 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (vc->vc_npar == 7) {
2257 int i = vc->vc_par[0] * 3, j = 1;
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 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2263 vc->vc_palette[i] += vc->vc_par[j];
2264 set_palette(vc);
2265 vc->vc_state = ESnormal;
2266 }
2267 } else
2268 vc->vc_state = ESnormal;
2269 return;
2270 case ESsquare:
2271 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2272 vc->vc_par[vc->vc_npar] = 0;
2273 vc->vc_npar = 0;
2274 vc->vc_state = ESgetpars;
2275 if (c == '[') { /* Function key */
2276 vc->vc_state=ESfunckey;
2277 return;
2278 }
Martin Hostettler54454472018-12-15 15:34:21 +01002279 switch (c) {
2280 case '?':
2281 vc->vc_priv = EPdec;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 return;
Martin Hostettler54454472018-12-15 15:34:21 +01002283 case '>':
2284 vc->vc_priv = EPgt;
2285 return;
2286 case '=':
2287 vc->vc_priv = EPeq;
2288 return;
2289 case '<':
2290 vc->vc_priv = EPlt;
2291 return;
2292 }
2293 vc->vc_priv = EPecma;
Mathieu Malaterre17504fd2019-01-14 21:41:13 +01002294 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 case ESgetpars:
2296 if (c == ';' && vc->vc_npar < NPAR - 1) {
2297 vc->vc_npar++;
2298 return;
2299 } else if (c>='0' && c<='9') {
2300 vc->vc_par[vc->vc_npar] *= 10;
2301 vc->vc_par[vc->vc_npar] += c - '0';
2302 return;
Adam Borowskib290af62014-02-19 01:40:16 +01002303 }
Martin Hostettler04afcd32018-12-15 15:34:23 +01002304 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
Martin Hostettler7a995652018-12-15 15:34:22 +01002305 vc->vc_state = EScsiignore;
2306 return;
2307 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 vc->vc_state = ESnormal;
2309 switch(c) {
2310 case 'h':
Martin Hostettler54454472018-12-15 15:34:21 +01002311 if (vc->vc_priv <= EPdec)
2312 set_mode(vc, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 return;
2314 case 'l':
Martin Hostettler54454472018-12-15 15:34:21 +01002315 if (vc->vc_priv <= EPdec)
2316 set_mode(vc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return;
2318 case 'c':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002319 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 if (vc->vc_par[0])
2321 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2322 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08002323 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 return;
2325 }
2326 break;
2327 case 'm':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002328 if (vc->vc_priv == EPdec) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 clear_selection();
2330 if (vc->vc_par[0])
2331 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2332 else
2333 vc->vc_complement_mask = vc->vc_s_complement_mask;
2334 return;
2335 }
2336 break;
2337 case 'n':
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002338 if (vc->vc_priv == EPecma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 if (vc->vc_par[0] == 5)
2340 status_report(tty);
2341 else if (vc->vc_par[0] == 6)
2342 cursor_report(vc, tty);
2343 }
2344 return;
2345 }
Martin Hostettler2ff5c5a2018-12-15 15:34:20 +01002346 if (vc->vc_priv != EPecma) {
2347 vc->vc_priv = EPecma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return;
2349 }
2350 switch(c) {
2351 case 'G': case '`':
2352 if (vc->vc_par[0])
2353 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002354 gotoxy(vc, vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 return;
2356 case 'A':
2357 if (!vc->vc_par[0])
2358 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002359 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 return;
2361 case 'B': case 'e':
2362 if (!vc->vc_par[0])
2363 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002364 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return;
2366 case 'C': case 'a':
2367 if (!vc->vc_par[0])
2368 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002369 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return;
2371 case 'D':
2372 if (!vc->vc_par[0])
2373 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002374 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 return;
2376 case 'E':
2377 if (!vc->vc_par[0])
2378 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002379 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 return;
2381 case 'F':
2382 if (!vc->vc_par[0])
2383 vc->vc_par[0]++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002384 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 return;
2386 case 'd':
2387 if (vc->vc_par[0])
2388 vc->vc_par[0]--;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02002389 gotoxay(vc, vc->state.x ,vc->vc_par[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return;
2391 case 'H': case 'f':
2392 if (vc->vc_par[0])
2393 vc->vc_par[0]--;
2394 if (vc->vc_par[1])
2395 vc->vc_par[1]--;
2396 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2397 return;
2398 case 'J':
2399 csi_J(vc, vc->vc_par[0]);
2400 return;
2401 case 'K':
2402 csi_K(vc, vc->vc_par[0]);
2403 return;
2404 case 'L':
2405 csi_L(vc, vc->vc_par[0]);
2406 return;
2407 case 'M':
2408 csi_M(vc, vc->vc_par[0]);
2409 return;
2410 case 'P':
2411 csi_P(vc, vc->vc_par[0]);
2412 return;
2413 case 'c':
2414 if (!vc->vc_par[0])
2415 respond_ID(tty);
2416 return;
2417 case 'g':
Jiri Slabydbee4cf2020-06-15 09:48:38 +02002418 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT)
2419 set_bit(vc->state.x, vc->vc_tab_stop);
2420 else if (vc->vc_par[0] == 3)
2421 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return;
2423 case 'm':
2424 csi_m(vc);
2425 return;
2426 case 'q': /* DECLL - but only 3 leds */
2427 /* map 0,1,2,3 to 0,1,2,4 */
2428 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002429 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2431 return;
2432 case 'r':
2433 if (!vc->vc_par[0])
2434 vc->vc_par[0]++;
2435 if (!vc->vc_par[1])
2436 vc->vc_par[1] = vc->vc_rows;
2437 /* Minimum allowed region is 2 lines */
2438 if (vc->vc_par[0] < vc->vc_par[1] &&
2439 vc->vc_par[1] <= vc->vc_rows) {
2440 vc->vc_top = vc->vc_par[0] - 1;
2441 vc->vc_bottom = vc->vc_par[1];
2442 gotoxay(vc, 0, 0);
2443 }
2444 return;
2445 case 's':
2446 save_cur(vc);
2447 return;
2448 case 'u':
2449 restore_cur(vc);
2450 return;
2451 case 'X':
2452 csi_X(vc, vc->vc_par[0]);
2453 return;
2454 case '@':
2455 csi_at(vc, vc->vc_par[0]);
2456 return;
2457 case ']': /* setterm functions */
2458 setterm_command(vc);
2459 return;
2460 }
2461 return;
Martin Hostettler7a995652018-12-15 15:34:22 +01002462 case EScsiignore:
2463 if (c >= 20 && c <= 0x3f)
2464 return;
2465 vc->vc_state = ESnormal;
2466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 case ESpercent:
2468 vc->vc_state = ESnormal;
2469 switch (c) {
2470 case '@': /* defined in ISO 2022 */
2471 vc->vc_utf = 0;
2472 return;
2473 case 'G': /* prelim official escape code */
2474 case '8': /* retained for compatibility */
2475 vc->vc_utf = 1;
2476 return;
2477 }
2478 return;
2479 case ESfunckey:
2480 vc->vc_state = ESnormal;
2481 return;
2482 case EShash:
2483 vc->vc_state = ESnormal;
2484 if (c == '8') {
2485 /* DEC screen alignment test. kludge :-) */
2486 vc->vc_video_erase_char =
2487 (vc->vc_video_erase_char & 0xff00) | 'E';
2488 csi_J(vc, 2);
2489 vc->vc_video_erase_char =
2490 (vc->vc_video_erase_char & 0xff00) | ' ';
2491 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2492 }
2493 return;
2494 case ESsetG0:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002495 vc_setGx(vc, 0, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 vc->vc_state = ESnormal;
2497 return;
2498 case ESsetG1:
Jiri Slabyb4d92b62020-06-15 09:48:36 +02002499 vc_setGx(vc, 1, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 vc->vc_state = ESnormal;
2501 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002502 case ESosc:
2503 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 default:
2505 vc->vc_state = ESnormal;
2506 }
2507}
2508
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002509/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002510 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002511 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2512 */
2513struct interval {
2514 uint32_t first;
2515 uint32_t last;
2516};
2517
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002518static int ucs_cmp(const void *key, const void *elt)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002519{
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002520 uint32_t ucs = *(uint32_t *)key;
2521 struct interval e = *(struct interval *) elt;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002522
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002523 if (ucs > e.last)
2524 return 1;
2525 else if (ucs < e.first)
2526 return -1;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002527 return 0;
2528}
2529
2530static int is_double_width(uint32_t ucs)
2531{
2532 static const struct interval double_width[] = {
2533 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2534 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002535 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2536 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002537 };
Thomas Meyerf0a8d842017-09-16 10:03:05 +02002538 if (ucs < double_width[0].first ||
2539 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2540 return 0;
2541
2542 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2543 sizeof(struct interval), ucs_cmp) != NULL;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002544}
2545
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002546struct vc_draw_region {
2547 unsigned long from, to;
2548 int x;
2549};
2550
2551static void con_flush(struct vc_data *vc, struct vc_draw_region *draw)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002552{
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002553 if (draw->x < 0)
Jiri Slabyd711ea82016-06-23 13:34:30 +02002554 return;
2555
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002556 vc->vc_sw->con_putcs(vc, (u16 *)draw->from,
2557 (u16 *)draw->to - (u16 *)draw->from, vc->state.y,
2558 draw->x);
2559 draw->x = -1;
Jiri Slabyd711ea82016-06-23 13:34:30 +02002560}
2561
Jiri Slabya0181802020-06-15 09:48:42 +02002562static inline int vc_translate_ascii(const struct vc_data *vc, int c)
2563{
2564 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) {
2565 if (vc->vc_toggle_meta)
2566 c |= 0x80;
2567
2568 return vc->vc_translate[c];
2569 }
2570
2571 return c;
2572}
2573
Jiri Slabyede98d12020-06-15 09:48:45 +02002574
2575/**
2576 * vc_sanitize_unicode -- Replace invalid Unicode code points with U+FFFD
2577 * @c: the received character, or U+FFFD for invalid sequences.
2578 */
2579static inline int vc_sanitize_unicode(const int c)
2580{
2581 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2582 return 0xfffd;
2583
2584 return c;
2585}
2586
Jiri Slaby694d8a42020-06-15 09:48:44 +02002587/**
2588 * vc_translate_unicode -- Combine UTF-8 into Unicode in @vc_utf_char
2589 *
2590 * @vc_utf_char is the being-constructed unicode character.
2591 * @vc_utf_count is the number of continuation bytes still expected to arrive.
2592 * @vc_npar is the number of continuation bytes arrived so far.
2593 */
2594static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
2595{
2596 static const u32 utf8_length_changes[] = {
2597 0x0000007f, 0x000007ff, 0x0000ffff,
2598 0x001fffff, 0x03ffffff, 0x7fffffff
2599 };
2600
Jiri Slabyede98d12020-06-15 09:48:45 +02002601 /* Continuation byte received */
Jiri Slaby694d8a42020-06-15 09:48:44 +02002602 if ((c & 0xc0) == 0x80) {
Jiri Slabyede98d12020-06-15 09:48:45 +02002603 /* Unexpected continuation byte? */
2604 if (!vc->vc_utf_count)
Jiri Slaby694d8a42020-06-15 09:48:44 +02002605 return 0xfffd;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002606
Jiri Slabyede98d12020-06-15 09:48:45 +02002607 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2608 vc->vc_npar++;
2609 if (--vc->vc_utf_count)
2610 goto need_more_bytes;
2611
2612 /* Got a whole character */
2613 c = vc->vc_utf_char;
2614 /* Reject overlong sequences */
2615 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2616 c > utf8_length_changes[vc->vc_npar])
2617 return 0xfffd;
2618
2619 return vc_sanitize_unicode(c);
2620 }
2621
2622 /* Single ASCII byte or first byte of a sequence received */
2623 if (vc->vc_utf_count) {
2624 /* Continuation byte expected */
2625 *rescan = true;
2626 vc->vc_utf_count = 0;
2627 return 0xfffd;
2628 }
2629
2630 /* Nothing to do if an ASCII byte was received */
2631 if (c <= 0x7f)
2632 return c;
2633
2634 /* First byte of a multibyte sequence received */
2635 vc->vc_npar = 0;
2636 if ((c & 0xe0) == 0xc0) {
2637 vc->vc_utf_count = 1;
2638 vc->vc_utf_char = (c & 0x1f);
2639 } else if ((c & 0xf0) == 0xe0) {
2640 vc->vc_utf_count = 2;
2641 vc->vc_utf_char = (c & 0x0f);
2642 } else if ((c & 0xf8) == 0xf0) {
2643 vc->vc_utf_count = 3;
2644 vc->vc_utf_char = (c & 0x07);
2645 } else if ((c & 0xfc) == 0xf8) {
2646 vc->vc_utf_count = 4;
2647 vc->vc_utf_char = (c & 0x03);
2648 } else if ((c & 0xfe) == 0xfc) {
2649 vc->vc_utf_count = 5;
2650 vc->vc_utf_char = (c & 0x01);
2651 } else {
2652 /* 254 and 255 are invalid */
2653 return 0xfffd;
2654 }
2655
2656need_more_bytes:
2657 return -1;
Jiri Slaby694d8a42020-06-15 09:48:44 +02002658}
2659
Jiri Slabya4236342020-06-15 09:48:48 +02002660static int vc_translate(struct vc_data *vc, int *c, bool *rescan)
2661{
2662 /* Do no translation at all in control states */
2663 if (vc->vc_state != ESnormal)
2664 return *c;
2665
2666 if (vc->vc_utf && !vc->vc_disp_ctrl)
2667 return *c = vc_translate_unicode(vc, *c, rescan);
2668
2669 /* no utf or alternate charset mode */
2670 return vc_translate_ascii(vc, *c);
2671}
2672
Jiri Slaby881c8782020-06-15 09:48:46 +02002673static inline unsigned char vc_invert_attr(const struct vc_data *vc)
2674{
2675 if (!vc->vc_can_do_color)
2676 return vc->vc_attr ^ 0x08;
2677
2678 if (vc->vc_hi_font_mask == 0x100)
2679 return (vc->vc_attr & 0x11) |
2680 ((vc->vc_attr & 0xe0) >> 4) |
2681 ((vc->vc_attr & 0x0e) << 4);
2682
2683 return (vc->vc_attr & 0x88) |
2684 ((vc->vc_attr & 0x70) >> 4) |
2685 ((vc->vc_attr & 0x07) << 4);
2686}
2687
Jiri Slaby754bda92020-06-15 09:48:50 +02002688static bool vc_is_control(struct vc_data *vc, int tc, int c)
2689{
2690 /*
2691 * A bitmap for codes <32. A bit of 1 indicates that the code
2692 * corresponding to that bit number invokes some special action (such
2693 * as cursor movement) and should not be displayed as a glyph unless
2694 * the disp_ctrl mode is explicitly enabled.
2695 */
2696 static const u32 CTRL_ACTION = 0x0d00ff81;
2697 /* Cannot be overridden by disp_ctrl */
2698 static const u32 CTRL_ALWAYS = 0x0800f501;
2699
2700 if (vc->vc_state != ESnormal)
2701 return true;
2702
2703 if (!tc)
2704 return true;
2705
2706 /*
2707 * If the original code was a control character we only allow a glyph
2708 * to be displayed if the code is not normally used (such as for cursor
2709 * movement) or if the disp_ctrl mode has been explicitly enabled.
2710 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always
2711 * displayed as control characters, as the console would be pretty
2712 * useless without them; to display an arbitrary font position use the
2713 * direct-to-font zone in UTF-8 mode.
2714 */
2715 if (c < 32) {
2716 if (vc->vc_disp_ctrl)
2717 return CTRL_ALWAYS & BIT(c);
2718 else
2719 return vc->vc_utf || (CTRL_ACTION & BIT(c));
2720 }
2721
2722 if (c == 127 && !vc->vc_disp_ctrl)
2723 return true;
2724
2725 if (c == 128 + 27)
2726 return true;
2727
2728 return false;
2729}
2730
Jiri Slaby391536a2020-06-15 09:48:51 +02002731static int vc_con_write_normal(struct vc_data *vc, int tc, int c,
2732 struct vc_draw_region *draw)
2733{
2734 int next_c;
Jiri Slabyf3205b22020-06-15 09:48:53 +02002735 unsigned char vc_attr = vc->vc_attr;
Jiri Slaby391536a2020-06-15 09:48:51 +02002736 u16 himask = vc->vc_hi_font_mask, charmask = himask ? 0x1ff : 0xff;
2737 u8 width = 1;
2738 bool inverse = false;
2739
2740 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2741 if (is_double_width(c))
2742 width = 2;
2743 }
2744
2745 /* Now try to find out how to display it */
2746 tc = conv_uni_to_pc(vc, tc);
2747 if (tc & ~charmask) {
2748 if (tc == -1 || tc == -2)
2749 return -1; /* nothing to display */
2750
2751 /* Glyph not found */
Jiri Slaby6923e2d2020-06-15 09:48:52 +02002752 if ((!vc->vc_utf || vc->vc_disp_ctrl || c < 128) &&
Jiri Slaby391536a2020-06-15 09:48:51 +02002753 !(c & ~charmask)) {
2754 /*
2755 * In legacy mode use the glyph we get by a 1:1
2756 * mapping.
2757 * This would make absolutely no sense with Unicode in
2758 * mind, but do this for ASCII characters since a font
2759 * may lack Unicode mapping info and we don't want to
2760 * end up with having question marks only.
2761 */
2762 tc = c;
2763 } else {
2764 /*
2765 * Display U+FFFD. If it's not found, display an inverse
2766 * question mark.
2767 */
2768 tc = conv_uni_to_pc(vc, 0xfffd);
2769 if (tc < 0) {
2770 inverse = true;
2771 tc = conv_uni_to_pc(vc, '?');
2772 if (tc < 0)
2773 tc = '?';
Jiri Slabyf3205b22020-06-15 09:48:53 +02002774
2775 vc_attr = vc_invert_attr(vc);
2776 con_flush(vc, draw);
Jiri Slaby391536a2020-06-15 09:48:51 +02002777 }
2778 }
2779 }
2780
Jiri Slaby391536a2020-06-15 09:48:51 +02002781 next_c = c;
2782 while (1) {
2783 if (vc->vc_need_wrap || vc->vc_decim)
2784 con_flush(vc, draw);
2785 if (vc->vc_need_wrap) {
2786 cr(vc);
2787 lf(vc);
2788 }
2789 if (vc->vc_decim)
2790 insert_char(vc, 1);
2791 vc_uniscr_putc(vc, next_c);
Jiri Slabyf1bcbe12020-06-15 09:48:54 +02002792
2793 if (himask)
2794 tc = ((tc & 0x100) ? himask : 0) |
2795 (tc & 0xff);
2796 tc |= (vc_attr << 8) & ~himask;
2797
2798 scr_writew(tc, (u16 *)vc->vc_pos);
2799
Jiri Slaby391536a2020-06-15 09:48:51 +02002800 if (con_should_update(vc) && draw->x < 0) {
2801 draw->x = vc->state.x;
2802 draw->from = vc->vc_pos;
2803 }
2804 if (vc->state.x == vc->vc_cols - 1) {
2805 vc->vc_need_wrap = vc->vc_decawm;
2806 draw->to = vc->vc_pos + 2;
2807 } else {
2808 vc->state.x++;
2809 draw->to = (vc->vc_pos += 2);
2810 }
2811
2812 if (!--width)
2813 break;
2814
2815 /* A space is printed in the second column */
2816 tc = conv_uni_to_pc(vc, ' ');
2817 if (tc < 0)
2818 tc = ' ';
2819 next_c = ' ';
2820 }
2821 notify_write(vc, c);
2822
2823 if (inverse)
2824 con_flush(vc, draw);
2825
2826 return 0;
2827}
2828
Torben Hohnac751ef2011-01-25 15:07:35 -08002829/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2831{
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002832 struct vc_draw_region draw = {
2833 .x = -1,
2834 };
Jiri Slaby391536a2020-06-15 09:48:51 +02002835 int c, tc, n = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 unsigned int currcons;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 struct vc_data *vc;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002838 struct vt_notifier_param param;
Jiri Slabyda823b22020-06-15 09:48:43 +02002839 bool rescan;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
2841 if (in_interrupt())
2842 return count;
2843
Torben Hohnac751ef2011-01-25 15:07:35 -08002844 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 vc = tty->driver_data;
2846 if (vc == NULL) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07002847 pr_err("vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002848 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 return 0;
2850 }
2851
2852 currcons = vc->vc_num;
2853 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002854 /* could this happen? */
2855 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2856 console_unlock();
2857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
2861 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02002862 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 hide_cursor(vc);
2864
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002865 param.vc = vc;
2866
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 while (!tty->stopped && count) {
2868 int orig = *buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 buf++;
2870 n++;
2871 count--;
Jiri Slaby0d1ffef2020-06-15 09:48:47 +02002872rescan_last_byte:
2873 c = orig;
Jiri Slabyda823b22020-06-15 09:48:43 +02002874 rescan = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Jiri Slabya4236342020-06-15 09:48:48 +02002876 tc = vc_translate(vc, &c, &rescan);
2877 if (tc == -1)
2878 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002880 param.c = tc;
2881 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2882 &param) == NOTIFY_STOP)
2883 continue;
2884
Jiri Slaby391536a2020-06-15 09:48:51 +02002885 if (vc_is_control(vc, tc, c)) {
2886 con_flush(vc, &draw);
2887 do_con_trol(tty, vc, orig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 continue;
2889 }
Jiri Slaby391536a2020-06-15 09:48:51 +02002890
2891 if (vc_con_write_normal(vc, tc, c, &draw) < 0)
2892 continue;
2893
2894 if (rescan)
2895 goto rescan_last_byte;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 }
Jiri Slaby917ae1a2020-06-15 09:48:49 +02002897 con_flush(vc, &draw);
Nicolas Pitre02240802018-07-17 21:02:41 -04002898 vc_uniscr_debug_check(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 console_conditional_schedule();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002900 notify_update(vc);
Nicolas Pitre7e1d2262019-01-08 22:55:00 -05002901 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903}
2904
2905/*
2906 * This is the console switching callback.
2907 *
2908 * Doing console switching in a process context allows
2909 * us to do the switches asynchronously (needed when we want
2910 * to switch due to a keyboard interrupt). Synchronization
2911 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002912 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 */
David Howells65f27f32006-11-22 14:55:48 +00002914static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Torben Hohnac751ef2011-01-25 15:07:35 -08002916 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 if (want_console >= 0) {
2919 if (want_console != fg_console &&
2920 vc_cons_allocated(want_console)) {
2921 hide_cursor(vc_cons[fg_console].d);
2922 change_console(vc_cons[want_console].d);
2923 /* we only changed when the console had already
2924 been allocated - a new console is not created
2925 in an interrupt routine */
2926 }
2927 want_console = -1;
2928 }
2929 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2930 do_poke_blanked_console = 0;
2931 poke_blanked_console();
2932 }
2933 if (scrollback_delta) {
2934 struct vc_data *vc = vc_cons[fg_console].d;
2935 clear_selection();
Jiri Slaby97293de2016-06-23 13:34:26 +02002936 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2938 scrollback_delta = 0;
2939 }
2940 if (blank_timer_expired) {
2941 do_blank_screen(0);
2942 blank_timer_expired = 0;
2943 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002944 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Torben Hohnac751ef2011-01-25 15:07:35 -08002946 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002949int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002951 struct vc_data *vc = vc_cons[fg_console].d;
2952
2953 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2954 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2955
2956 /*
2957 * Console switch will fail in console_callback() or
2958 * change_console() so there is no point scheduling
2959 * the callback
2960 *
2961 * Existing set_console() users don't check the return
2962 * value so this shouldn't break anything
2963 */
2964 return -EINVAL;
2965 }
2966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 want_console = nr;
2968 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002969
2970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971}
2972
2973struct tty_driver *console_driver;
2974
2975#ifdef CONFIG_VT_CONSOLE
2976
Bernhard Walle5ada9182009-12-14 18:00:43 -08002977/**
2978 * vt_kmsg_redirect() - Sets/gets the kernel message console
2979 * @new: The new virtual terminal number or -1 if the console should stay
2980 * unchanged
2981 *
2982 * By default, the kernel messages are always printed on the current virtual
2983 * console. However, the user may modify that default with the
2984 * TIOCL_SETKMSGREDIRECT ioctl call.
2985 *
2986 * This function sets the kernel message console to be @new. It returns the old
2987 * virtual console number. The virtual terminal number 0 (both as parameter and
2988 * return value) means no redirection (i.e. always printed on the currently
2989 * active console).
2990 *
2991 * The parameter -1 means that only the current console is returned, but the
2992 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2993 * case to make the code more understandable.
2994 *
2995 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2996 * the parameter and always returns 0.
2997 */
2998int vt_kmsg_redirect(int new)
2999{
3000 static int kmsg_con;
3001
3002 if (new != -1)
3003 return xchg(&kmsg_con, new);
3004 else
3005 return kmsg_con;
3006}
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008/*
3009 * Console on virtual terminal
3010 *
3011 * The console must be locked when we get here.
3012 */
3013
3014static void vt_console_print(struct console *co, const char *b, unsigned count)
3015{
3016 struct vc_data *vc = vc_cons[fg_console].d;
3017 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08003018 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 const ushort *start;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003020 ushort start_x, cnt;
Bernhard Walle5ada9182009-12-14 18:00:43 -08003021 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08003024 if (!printable)
3025 return;
3026 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 return;
3028
Bernhard Walle5ada9182009-12-14 18:00:43 -08003029 kmsg_console = vt_get_kmsg_redirect();
3030 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
3031 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 if (!vc_cons_allocated(fg_console)) {
3034 /* impossible */
3035 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
3036 goto quit;
3037 }
3038
Daniel Vetter8d7fc292018-08-22 10:54:03 +02003039 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 goto quit;
3041
3042 /* undraw cursor first */
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003043 if (con_is_fg(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 hide_cursor(vc);
3045
3046 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003047 start_x = vc->state.x;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003048 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 while (count--) {
3050 c = *b++;
3051 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003052 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003053 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003054 cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (c == 8) { /* backspace */
3056 bs(vc);
3057 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003058 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 continue;
3060 }
3061 if (c != 13)
3062 lf(vc);
3063 cr(vc);
3064 start = (ushort *)vc->vc_pos;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003065 start_x = vc->state.x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 if (c == 10 || c == 13)
3067 continue;
3068 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003069 vc_uniscr_putc(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003071 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 cnt++;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003073 if (vc->state.x == vc->vc_cols - 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 vc->vc_need_wrap = 1;
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003075 } else {
3076 vc->vc_pos += 2;
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003077 vc->state.x++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 }
Nicolas Pitre6609cff2019-01-08 22:54:59 -05003080 if (cnt && con_is_visible(vc))
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003081 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07003083 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085quit:
Nick Pigginb0940002008-02-06 01:37:04 -08003086 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087}
3088
3089static struct tty_driver *vt_console_device(struct console *c, int *index)
3090{
3091 *index = c->index ? c->index-1 : fg_console;
3092 return console_driver;
3093}
3094
3095static struct console vt_console_driver = {
3096 .name = "tty",
3097 .write = vt_console_print,
3098 .device = vt_console_device,
3099 .unblank = unblank_screen,
3100 .flags = CON_PRINTBUFFER,
3101 .index = -1,
3102};
3103#endif
3104
3105/*
3106 * Handling of Linux-specific VC ioctls
3107 */
3108
3109/*
Torben Hohnac751ef2011-01-25 15:07:35 -08003110 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 *
3112 * There are some functions which don't need it.
3113 *
3114 * There are some functions which can sleep for arbitrary periods
3115 * (paste_selection) but we don't need the lock there anyway.
3116 *
Okash Khawaja496124e2019-04-17 13:21:13 +01003117 * set_selection_user has locking, and definitely needs it
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 */
3119
3120int tioclinux(struct tty_struct *tty, unsigned long arg)
3121{
3122 char type, data;
3123 char __user *p = (char __user *)arg;
3124 int lines;
3125 int ret;
3126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3128 return -EPERM;
3129 if (get_user(type, p))
3130 return -EFAULT;
3131 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 switch (type)
3134 {
3135 case TIOCL_SETSEL:
Okash Khawaja496124e2019-04-17 13:21:13 +01003136 ret = set_selection_user((struct tiocl_selection
3137 __user *)(p+1), tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 break;
3139 case TIOCL_PASTESEL:
3140 ret = paste_selection(tty);
3141 break;
3142 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08003143 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08003145 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 break;
3147 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00003148 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00003150 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 break;
3152 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 /*
3155 * Make it possible to react to Shift+Mousebutton.
3156 * Note that 'shift_state' is an undocumented
3157 * kernel-internal variable; programs not closely
3158 * related to the kernel should not use this.
3159 */
Alan Cox079c9532012-02-28 14:49:23 +00003160 data = vt_get_shift_state();
Adam Borowski6987dc82017-06-03 09:35:06 +02003161 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 break;
3163 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00003164 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00003166 console_unlock();
Adam Borowski6987dc82017-06-03 09:35:06 +02003167 ret = put_user(data, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 break;
3169 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00003170 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003171 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00003172 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003174 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08003175 data = vt_get_kmsg_redirect();
Adam Borowski6987dc82017-06-03 09:35:06 +02003176 ret = put_user(data, p);
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08003177 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 case TIOCL_SETKMSGREDIRECT:
3179 if (!capable(CAP_SYS_ADMIN)) {
3180 ret = -EPERM;
3181 } else {
3182 if (get_user(data, p+1))
3183 ret = -EFAULT;
3184 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08003185 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187 break;
3188 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00003189 /* No locking needed as this is a transiently
3190 correct return anyway if the caller hasn't
3191 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 ret = fg_console;
3193 break;
3194 case TIOCL_SCROLLCONSOLE:
3195 if (get_user(lines, (s32 __user *)(p+4))) {
3196 ret = -EFAULT;
3197 } else {
Alan Cox20f62572012-03-02 14:59:37 +00003198 /* Need the console lock here. Note that lots
3199 of other calls need fixing before the lock
3200 is actually useful ! */
3201 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00003203 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 ret = 0;
3205 }
3206 break;
3207 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08003208 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 ignore_poke = 1;
3210 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003211 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 break;
3213 case TIOCL_BLANKEDSCREEN:
3214 ret = console_blanked;
3215 break;
3216 default:
3217 ret = -EINVAL;
3218 break;
3219 }
3220 return ret;
3221}
3222
3223/*
3224 * /dev/ttyN handling
3225 */
3226
3227static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3228{
3229 int retval;
3230
3231 retval = do_con_write(tty, buf, count);
3232 con_flush_chars(tty);
3233
3234 return retval;
3235}
3236
Alan Cox5d19f542008-04-30 00:54:08 -07003237static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238{
3239 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07003240 return 0; /* n_r3964 calls put_char() from interrupt context */
3241 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
3244static int con_write_room(struct tty_struct *tty)
3245{
3246 if (tty->stopped)
3247 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00003248 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
3251static int con_chars_in_buffer(struct tty_struct *tty)
3252{
3253 return 0; /* we're not buffering */
3254}
3255
3256/*
3257 * con_throttle and con_unthrottle are only used for
3258 * paste_selection(), which has to stuff in a large number of
3259 * characters...
3260 */
3261static void con_throttle(struct tty_struct *tty)
3262{
3263}
3264
3265static void con_unthrottle(struct tty_struct *tty)
3266{
3267 struct vc_data *vc = tty->driver_data;
3268
3269 wake_up_interruptible(&vc->paste_wait);
3270}
3271
3272/*
3273 * Turn the Scroll-Lock LED on when the tty is stopped
3274 */
3275static void con_stop(struct tty_struct *tty)
3276{
3277 int console_num;
3278 if (!tty)
3279 return;
3280 console_num = tty->index;
3281 if (!vc_cons_allocated(console_num))
3282 return;
Alan Cox079c9532012-02-28 14:49:23 +00003283 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284}
3285
3286/*
3287 * Turn the Scroll-Lock LED off when the console is started
3288 */
3289static void con_start(struct tty_struct *tty)
3290{
3291 int console_num;
3292 if (!tty)
3293 return;
3294 console_num = tty->index;
3295 if (!vc_cons_allocated(console_num))
3296 return;
Alan Cox079c9532012-02-28 14:49:23 +00003297 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298}
3299
3300static void con_flush_chars(struct tty_struct *tty)
3301{
3302 struct vc_data *vc;
3303
3304 if (in_interrupt()) /* from flush_to_ldisc */
3305 return;
3306
3307 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08003308 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 vc = tty->driver_data;
3310 if (vc)
3311 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08003312 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
3315/*
3316 * Allocate the console screen memory.
3317 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003318static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319{
3320 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003321 struct vc_data *vc;
3322 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Torben Hohnac751ef2011-01-25 15:07:35 -08003324 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003325 ret = vc_allocate(currcons);
3326 if (ret)
3327 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01003328
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003329 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003331 /* Still being freed */
3332 if (vc->port.tty) {
3333 ret = -ERESTARTSYS;
3334 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003336
3337 ret = tty_port_install(&vc->port, driver, tty);
3338 if (ret)
3339 goto unlock;
3340
3341 tty->driver_data = vc;
3342 vc->port.tty = tty;
Eric Biggersca4463b2020-03-21 20:43:04 -07003343 tty_port_get(&vc->port);
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003344
3345 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3346 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3347 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
3348 }
3349 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01003350 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003351 else
Alan Coxadc8d742012-07-14 15:31:47 +01003352 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003353unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08003354 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 return ret;
3356}
3357
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003358static int con_open(struct tty_struct *tty, struct file *filp)
3359{
3360 /* everything done in install */
3361 return 0;
3362}
3363
3364
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365static void con_close(struct tty_struct *tty, struct file *filp)
3366{
Alan Coxfeebed62008-10-13 10:41:30 +01003367 /* Nothing to do - we defer to shutdown */
3368}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
Alan Coxfeebed62008-10-13 10:41:30 +01003370static void con_shutdown(struct tty_struct *tty)
3371{
3372 struct vc_data *vc = tty->driver_data;
3373 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08003374 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02003375 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08003376 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377}
3378
Eric Biggersca4463b2020-03-21 20:43:04 -07003379static void con_cleanup(struct tty_struct *tty)
3380{
3381 struct vc_data *vc = tty->driver_data;
3382
3383 tty_port_put(&vc->port);
3384}
3385
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003386static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003387static int default_italic_color = 2; // green (ASCII)
3388static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003389module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003390module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3391module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3392
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393static void vc_init(struct vc_data *vc, unsigned int rows,
3394 unsigned int cols, int do_clear)
3395{
3396 int j, k ;
3397
3398 vc->vc_cols = cols;
3399 vc->vc_rows = rows;
3400 vc->vc_size_row = cols << 1;
3401 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3402
3403 set_origin(vc);
3404 vc->vc_pos = vc->vc_origin;
3405 reset_vc(vc);
3406 for (j=k=0; j<16; j++) {
3407 vc->vc_palette[k++] = default_red[j] ;
3408 vc->vc_palette[k++] = default_grn[j] ;
3409 vc->vc_palette[k++] = default_blu[j] ;
3410 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02003411 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07003412 vc->vc_ulcolor = default_underline_color;
3413 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 vc->vc_halfcolor = 0x08; /* grey */
3415 init_waitqueue_head(&vc->paste_wait);
3416 reset_terminal(vc, do_clear);
3417}
3418
3419/*
3420 * This routine initializes console interrupts, and does nothing
3421 * else. If you want the screen to clear, call tty_write with
3422 * the appropriate escape-sequence.
3423 */
3424
3425static int __init con_init(void)
3426{
3427 const char *display_desc = NULL;
3428 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003429 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Torben Hohnac751ef2011-01-25 15:07:35 -08003431 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Arvind Sankar805ece22019-12-18 16:44:44 -05003433 if (!conswitchp)
3434 conswitchp = &dummy_con;
3435 display_desc = conswitchp->con_startup();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 if (!display_desc) {
3437 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08003438 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 return 0;
3440 }
3441
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003442 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3443 struct con_driver *con_driver = &registered_con_driver[i];
3444
3445 if (con_driver->con == NULL) {
3446 con_driver->con = conswitchp;
3447 con_driver->desc = display_desc;
3448 con_driver->flag = CON_DRIVER_FLAG_INIT;
3449 con_driver->first = 0;
3450 con_driver->last = MAX_NR_CONSOLES - 1;
3451 break;
3452 }
3453 }
3454
3455 for (i = 0; i < MAX_NR_CONSOLES; i++)
3456 con_driver_map[i] = conswitchp;
3457
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 if (blankinterval) {
3459 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07003460 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 }
3462
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03003464 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07003465 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02003466 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03003468 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 vc_init(vc, vc->vc_rows, vc->vc_cols,
3470 currcons || !vc->vc_sw->con_save_screen);
3471 }
3472 currcons = fg_console = 0;
3473 master_display_fg = vc = vc_cons[currcons].d;
3474 set_origin(vc);
3475 save_screen(vc);
Jiri Slaby28bc24f2020-06-15 09:48:33 +02003476 gotoxy(vc, vc->state.x, vc->state.y);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 csi_J(vc, 0);
3478 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003479 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 vc->vc_can_do_color ? "colour" : "mono",
3481 display_desc, vc->vc_cols, vc->vc_rows);
3482 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Torben Hohnac751ef2011-01-25 15:07:35 -08003484 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485
3486#ifdef CONFIG_VT_CONSOLE
3487 register_console(&vt_console_driver);
3488#endif
3489 return 0;
3490}
3491console_initcall(con_init);
3492
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003493static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003494 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 .open = con_open,
3496 .close = con_close,
3497 .write = con_write,
3498 .write_room = con_write_room,
3499 .put_char = con_put_char,
3500 .flush_chars = con_flush_chars,
3501 .chars_in_buffer = con_chars_in_buffer,
3502 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003503#ifdef CONFIG_COMPAT
3504 .compat_ioctl = vt_compat_ioctl,
3505#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 .stop = con_stop,
3507 .start = con_start,
3508 .throttle = con_throttle,
3509 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003510 .resize = vt_resize,
Eric Biggersca4463b2020-03-21 20:43:04 -07003511 .shutdown = con_shutdown,
3512 .cleanup = con_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513};
3514
Alan Coxd81ed102008-10-13 10:41:42 +01003515static struct cdev vc0_cdev;
3516
Kay Sieversfbc92a32010-12-01 18:51:05 +01003517static ssize_t show_tty_active(struct device *dev,
3518 struct device_attribute *attr, char *buf)
3519{
3520 return sprintf(buf, "tty%d\n", fg_console + 1);
3521}
3522static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3523
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003524static struct attribute *vt_dev_attrs[] = {
3525 &dev_attr_active.attr,
3526 NULL
3527};
3528
3529ATTRIBUTE_GROUPS(vt_dev);
3530
Alan Coxd81ed102008-10-13 10:41:42 +01003531int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
Alan Coxd81ed102008-10-13 10:41:42 +01003533 cdev_init(&vc0_cdev, console_fops);
3534 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3535 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3536 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003537 tty0dev = device_create_with_groups(tty_class, NULL,
3538 MKDEV(TTY_MAJOR, 0), NULL,
3539 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003540 if (IS_ERR(tty0dev))
3541 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003542
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 vcs_init();
3544
3545 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3546 if (!console_driver)
3547 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003548
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 console_driver->name = "tty";
3550 console_driver->name_base = 1;
3551 console_driver->major = TTY_MAJOR;
3552 console_driver->minor_start = 1;
3553 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3554 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003555 if (default_utf8)
3556 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3558 tty_set_operations(console_driver, &con_ops);
3559 if (tty_register_driver(console_driver))
3560 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 kbd_init();
3562 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563#ifdef CONFIG_MDA_CONSOLE
3564 mda_console_init();
3565#endif
3566 return 0;
3567}
3568
3569#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003570
3571static struct class *vtconsole_class;
3572
Alan Cox50e244c2013-01-25 10:28:15 +10003573static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003574 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003576 struct module *owner = csw->owner;
3577 const char *desc = NULL;
3578 struct con_driver *con_driver;
3579 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 if (!try_module_get(owner))
3582 return -ENODEV;
3583
Alan Cox50e244c2013-01-25 10:28:15 +10003584 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003585
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003586 /* check if driver is registered */
3587 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3588 con_driver = &registered_con_driver[i];
3589
3590 if (con_driver->con == csw) {
3591 desc = con_driver->desc;
3592 retval = 0;
3593 break;
3594 }
3595 }
3596
3597 if (retval)
3598 goto err;
3599
3600 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3601 csw->con_startup();
3602 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003604
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 if (deflt) {
3606 if (conswitchp)
3607 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003608
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 __module_get(owner);
3610 conswitchp = csw;
3611 }
3612
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003613 first = max(first, con_driver->first);
3614 last = min(last, con_driver->last);
3615
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 for (i = first; i <= last; i++) {
3617 int old_was_color;
3618 struct vc_data *vc = vc_cons[i].d;
3619
3620 if (con_driver_map[i])
3621 module_put(con_driver_map[i]->owner);
3622 __module_get(owner);
3623 con_driver_map[i] = csw;
3624
3625 if (!vc || !vc->vc_sw)
3626 continue;
3627
3628 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003629
Jiri Slaby6ca8dfd2016-06-23 13:34:35 +02003630 if (con_is_visible(vc)) {
David Hollister4ee1acc2006-06-26 00:26:41 -07003631 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003633 }
3634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635 old_was_color = vc->vc_can_do_color;
3636 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003637 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003639 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 update_attr(vc);
3641
3642 /* If the console changed between mono <-> color, then
3643 * the attributes in the screenbuf will be wrong. The
3644 * following resets all attributes to something sane.
3645 */
3646 if (old_was_color != vc->vc_can_do_color)
3647 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003649
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003650 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 if (!deflt)
Joe Perchesbccf1da2017-10-02 08:48:55 -07003652 pr_cont("consoles %d-%d ", first + 1, last + 1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003653 if (j >= 0) {
3654 struct vc_data *vc = vc_cons[j].d;
3655
Joe Perchesbccf1da2017-10-02 08:48:55 -07003656 pr_cont("to %s %s %dx%d\n",
3657 vc->vc_can_do_color ? "colour" : "mono",
3658 desc, vc->vc_cols, vc->vc_rows);
David Hollister4ee1acc2006-06-26 00:26:41 -07003659
3660 if (k >= 0) {
3661 vc = vc_cons[k].d;
3662 update_screen(vc);
3663 }
Joe Perchesbccf1da2017-10-02 08:48:55 -07003664 } else {
3665 pr_cont("to %s\n", desc);
3666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003668 retval = 0;
3669err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003671 return retval;
3672};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
Alan Cox50e244c2013-01-25 10:28:15 +10003674
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003675#ifdef CONFIG_VT_HW_CONSOLE_BINDING
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003676int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3677{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003678 struct module *owner = csw->owner;
3679 const struct consw *defcsw = NULL;
3680 struct con_driver *con_driver = NULL, *con_back = NULL;
3681 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003683 if (!try_module_get(owner))
3684 return -ENODEV;
3685
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003686 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003687
3688 /* check if driver is registered and if it is unbindable */
3689 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3690 con_driver = &registered_con_driver[i];
3691
3692 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003693 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003694 retval = 0;
3695 break;
3696 }
3697 }
3698
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003699 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003700 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003701
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003702 retval = -ENODEV;
3703
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003704 /* check if backup driver exists */
3705 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3706 con_back = &registered_con_driver[i];
3707
Daniel Vetter249f7b32014-06-05 16:24:47 +02003708 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003709 defcsw = con_back->con;
3710 retval = 0;
3711 break;
3712 }
3713 }
3714
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003715 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003716 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003717
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003718 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003719 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003720
3721 first = max(first, con_driver->first);
3722 last = min(last, con_driver->last);
3723
3724 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 if (con_driver_map[i] == csw) {
3726 module_put(csw->owner);
3727 con_driver_map[i] = NULL;
3728 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003729 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003730
3731 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003732 const struct consw *defconsw = conswitchp;
3733
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003734 defcsw->con_startup();
3735 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003736 /*
3737 * vgacon may change the default driver to point
3738 * to dummycon, we restore it here...
3739 */
3740 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003741 }
3742
3743 if (!con_is_bound(csw))
3744 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3745
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003746 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003747 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003748err:
3749 module_put(owner);
3750 return retval;
3751
3752}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003753EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003754
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003755static int vt_bind(struct con_driver *con)
3756{
3757 const struct consw *defcsw = NULL, *csw = NULL;
3758 int i, more = 1, first = -1, last = -1, deflt = 0;
3759
Daniel Vetter77232f72015-04-13 11:16:21 +02003760 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003761 goto err;
3762
3763 csw = con->con;
3764
3765 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3766 struct con_driver *con = &registered_con_driver[i];
3767
3768 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3769 defcsw = con->con;
3770 break;
3771 }
3772 }
3773
3774 if (!defcsw)
3775 goto err;
3776
3777 while (more) {
3778 more = 0;
3779
3780 for (i = con->first; i <= con->last; i++) {
3781 if (con_driver_map[i] == defcsw) {
3782 if (first == -1)
3783 first = i;
3784 last = i;
3785 more = 1;
3786 } else if (first != -1)
3787 break;
3788 }
3789
3790 if (first == 0 && last == MAX_NR_CONSOLES -1)
3791 deflt = 1;
3792
Imre Deak4c215fe2014-12-16 00:16:00 +02003793 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003794 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003795
3796 first = -1;
3797 last = -1;
3798 deflt = 0;
3799 }
3800
3801err:
3802 return 0;
3803}
3804
3805static int vt_unbind(struct con_driver *con)
3806{
3807 const struct consw *csw = NULL;
3808 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003809 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003810
Daniel Vetter77232f72015-04-13 11:16:21 +02003811 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003812 goto err;
3813
3814 csw = con->con;
3815
3816 while (more) {
3817 more = 0;
3818
3819 for (i = con->first; i <= con->last; i++) {
3820 if (con_driver_map[i] == csw) {
3821 if (first == -1)
3822 first = i;
3823 last = i;
3824 more = 1;
3825 } else if (first != -1)
3826 break;
3827 }
3828
3829 if (first == 0 && last == MAX_NR_CONSOLES -1)
3830 deflt = 1;
3831
Wang YanQing618f2b92013-05-09 02:14:07 +08003832 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003833 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003834 if (ret != 0)
3835 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003836 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003837
3838 first = -1;
3839 last = -1;
3840 deflt = 0;
3841 }
3842
3843err:
3844 return 0;
3845}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003846#else
3847static inline int vt_bind(struct con_driver *con)
3848{
3849 return 0;
3850}
3851static inline int vt_unbind(struct con_driver *con)
3852{
3853 return 0;
3854}
3855#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003856
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003857static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003858 const char *buf, size_t count)
3859{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003860 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003861 int bind = simple_strtoul(buf, NULL, 0);
3862
Imre Deak4c215fe2014-12-16 00:16:00 +02003863 console_lock();
3864
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003865 if (bind)
3866 vt_bind(con);
3867 else
3868 vt_unbind(con);
3869
Imre Deak4c215fe2014-12-16 00:16:00 +02003870 console_unlock();
3871
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003872 return count;
3873}
3874
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003875static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3876 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003877{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003878 struct con_driver *con = dev_get_drvdata(dev);
Daniel Vetter61d51452019-07-18 10:09:03 +02003879 int bind;
3880
3881 console_lock();
3882 bind = con_is_bound(con->con);
3883 console_unlock();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003884
3885 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3886}
3887
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003888static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3889 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003890{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003891 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003892
3893 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3894 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3895 con->desc);
3896
3897}
3898
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003899static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3900static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3901
3902static struct attribute *con_dev_attrs[] = {
3903 &dev_attr_bind.attr,
3904 &dev_attr_name.attr,
3905 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003906};
3907
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003908ATTRIBUTE_GROUPS(con_dev);
3909
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003910static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003911{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003912 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003913 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003914}
3915
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003916static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003917{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003918 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003919}
3920
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003921/**
3922 * con_is_bound - checks if driver is bound to the console
3923 * @csw: console driver
3924 *
3925 * RETURNS: zero if unbound, nonzero if bound
3926 *
3927 * Drivers can call this and if zero, they should release
3928 * all resources allocated on con_startup()
3929 */
3930int con_is_bound(const struct consw *csw)
3931{
3932 int i, bound = 0;
3933
Daniel Vetterddde3c12019-05-28 11:02:35 +02003934 WARN_CONSOLE_UNLOCKED();
3935
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003936 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3937 if (con_driver_map[i] == csw) {
3938 bound = 1;
3939 break;
3940 }
3941 }
3942
3943 return bound;
3944}
3945EXPORT_SYMBOL(con_is_bound);
3946
3947/**
Daniel Vetterddde3c12019-05-28 11:02:35 +02003948 * con_is_visible - checks whether the current console is visible
3949 * @vc: virtual console
3950 *
3951 * RETURNS: zero if not visible, nonzero if visible
3952 */
3953bool con_is_visible(const struct vc_data *vc)
3954{
3955 WARN_CONSOLE_UNLOCKED();
3956
3957 return *vc->vc_display_fg == vc;
3958}
3959EXPORT_SYMBOL(con_is_visible);
3960
3961/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003962 * con_debug_enter - prepare the console for the kernel debugger
3963 * @sw: console driver
3964 *
3965 * Called when the console is taken over by the kernel debugger, this
3966 * function needs to save the current console state, then put the console
3967 * into a state suitable for the kernel debugger.
3968 *
3969 * RETURNS:
3970 * Zero on success, nonzero if a failure occurred when trying to prepare
3971 * the console for the debugger.
3972 */
3973int con_debug_enter(struct vc_data *vc)
3974{
3975 int ret = 0;
3976
3977 saved_fg_console = fg_console;
3978 saved_last_console = last_console;
3979 saved_want_console = want_console;
3980 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003981 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003982 vc->vc_mode = KD_TEXT;
3983 console_blanked = 0;
3984 if (vc->vc_sw->con_debug_enter)
3985 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003986#ifdef CONFIG_KGDB_KDB
3987 /* Set the initial LINES variable if it is not already set */
3988 if (vc->vc_rows < 999) {
3989 int linecount;
3990 char lns[4];
3991 const char *setargs[3] = {
3992 "set",
3993 "LINES",
3994 lns,
3995 };
3996 if (kdbgetintenv(setargs[0], &linecount)) {
3997 snprintf(lns, 4, "%i", vc->vc_rows);
3998 kdb_set(2, setargs);
3999 }
4000 }
Jason Wessel17b572e82012-08-26 22:37:03 -05004001 if (vc->vc_cols < 999) {
4002 int colcount;
4003 char cols[4];
4004 const char *setargs[3] = {
4005 "set",
4006 "COLUMNS",
4007 cols,
4008 };
4009 if (kdbgetintenv(setargs[0], &colcount)) {
4010 snprintf(cols, 4, "%i", vc->vc_cols);
4011 kdb_set(2, setargs);
4012 }
4013 }
Jason Wessel81d44502010-08-05 09:22:30 -05004014#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05004015 return ret;
4016}
4017EXPORT_SYMBOL_GPL(con_debug_enter);
4018
4019/**
4020 * con_debug_leave - restore console state
4021 * @sw: console driver
4022 *
4023 * Restore the console state to what it was before the kernel debugger
4024 * was invoked.
4025 *
4026 * RETURNS:
4027 * Zero on success, nonzero if a failure occurred when trying to restore
4028 * the console.
4029 */
4030int con_debug_leave(void)
4031{
4032 struct vc_data *vc;
4033 int ret = 0;
4034
4035 fg_console = saved_fg_console;
4036 last_console = saved_last_console;
4037 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05004038 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05004039 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
4040
4041 vc = vc_cons[fg_console].d;
4042 if (vc->vc_sw->con_debug_leave)
4043 ret = vc->vc_sw->con_debug_leave(vc);
4044 return ret;
4045}
4046EXPORT_SYMBOL_GPL(con_debug_leave);
4047
Alan Cox50e244c2013-01-25 10:28:15 +10004048static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004049{
4050 struct module *owner = csw->owner;
4051 struct con_driver *con_driver;
4052 const char *desc;
Jiri Slaby96317e92016-05-03 17:05:55 +02004053 int i, retval;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004054
Alan Cox50e244c2013-01-25 10:28:15 +10004055 WARN_CONSOLE_UNLOCKED();
4056
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004057 if (!try_module_get(owner))
4058 return -ENODEV;
4059
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004060 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4061 con_driver = &registered_con_driver[i];
4062
4063 /* already registered */
Jiri Slaby96317e92016-05-03 17:05:55 +02004064 if (con_driver->con == csw) {
Dave Airliec55c63c2011-01-07 09:57:41 +10004065 retval = -EBUSY;
Jiri Slaby96317e92016-05-03 17:05:55 +02004066 goto err;
4067 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004068 }
4069
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004070 desc = csw->con_startup();
Jiri Slaby6798df42016-05-03 17:05:54 +02004071 if (!desc) {
4072 retval = -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004073 goto err;
Jiri Slaby6798df42016-05-03 17:05:54 +02004074 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004075
4076 retval = -EINVAL;
4077
4078 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4079 con_driver = &registered_con_driver[i];
4080
Imre Deakd364b5c2015-04-01 21:06:16 +03004081 if (con_driver->con == NULL &&
4082 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004083 con_driver->con = csw;
4084 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004085 con_driver->node = i;
4086 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004087 CON_DRIVER_FLAG_INIT;
4088 con_driver->first = first;
4089 con_driver->last = last;
4090 retval = 0;
4091 break;
4092 }
4093 }
4094
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004095 if (retval)
4096 goto err;
4097
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004098 con_driver->dev =
4099 device_create_with_groups(vtconsole_class, NULL,
4100 MKDEV(0, con_driver->node),
4101 con_driver, con_dev_groups,
4102 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004103 if (IS_ERR(con_driver->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004104 pr_warn("Unable to create device for %s; errno = %ld\n",
4105 con_driver->desc, PTR_ERR(con_driver->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004106 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004107 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004108 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004109 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07004110
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004111err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004112 module_put(owner);
4113 return retval;
4114}
Alan Cox50e244c2013-01-25 10:28:15 +10004115
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004116
4117/**
Wang YanQing50539dd2013-05-09 02:14:44 +08004118 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004119 * @csw: console driver
4120 *
4121 * DESCRIPTION: All drivers that registers to the console layer must
4122 * call this function upon exit, or if the console driver is in a state
4123 * where it won't be able to handle console services, such as the
4124 * framebuffer console without loaded framebuffer drivers.
4125 *
4126 * The driver must unbind first prior to unregistration.
4127 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004128int do_unregister_con_driver(const struct consw *csw)
4129{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004130 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004131
4132 /* cannot unregister a bound driver */
4133 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004134 return -EBUSY;
4135
4136 if (csw == conswitchp)
4137 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004138
4139 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4140 struct con_driver *con_driver = &registered_con_driver[i];
4141
Imre Deak2cf30f72014-12-16 00:15:59 +02004142 if (con_driver->con == csw) {
Imre Deakd364b5c2015-04-01 21:06:16 +03004143 /*
4144 * Defer the removal of the sysfs entries since that
4145 * will acquire the kernfs s_active lock and we can't
4146 * acquire this lock while holding the console lock:
4147 * the unbind sysfs entry imposes already the opposite
4148 * order. Reset con already here to prevent any later
4149 * lookup to succeed and mark this slot as zombie, so
4150 * it won't get reused until we complete the removal
4151 * in the deferred work.
4152 */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004153 con_driver->con = NULL;
Imre Deakd364b5c2015-04-01 21:06:16 +03004154 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4155 schedule_work(&con_driver_unregister_work);
4156
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004157 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004158 }
4159 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02004160
4161 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004162}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10004163EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004164
Imre Deakd364b5c2015-04-01 21:06:16 +03004165static void con_driver_unregister_callback(struct work_struct *ignored)
4166{
4167 int i;
4168
4169 console_lock();
4170
4171 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4172 struct con_driver *con_driver = &registered_con_driver[i];
4173
4174 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4175 continue;
4176
4177 console_unlock();
4178
4179 vtconsole_deinit_device(con_driver);
4180 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4181
4182 console_lock();
4183
4184 if (WARN_ON_ONCE(con_driver->con))
4185 con_driver->con = NULL;
4186 con_driver->desc = NULL;
4187 con_driver->dev = NULL;
4188 con_driver->node = 0;
4189 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4190 con_driver->flag = 0;
4191 con_driver->first = 0;
4192 con_driver->last = 0;
4193 }
4194
4195 console_unlock();
4196}
4197
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004198/*
4199 * If we support more console drivers, this function is used
4200 * when a driver wants to take over some existing consoles
4201 * and become default driver for newly opened ones.
4202 *
Lukas Wunner0095ab42020-01-09 13:59:21 +01004203 * do_take_over_console is basically a register followed by bind
Alan Cox50e244c2013-01-25 10:28:15 +10004204 */
4205int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4206{
4207 int err;
4208
4209 err = do_register_con_driver(csw, first, last);
4210 /*
4211 * If we get an busy error we still want to bind the console driver
4212 * and return success, as we may have unbound the console driver
4213 * but not unregistered it.
4214 */
4215 if (err == -EBUSY)
4216 err = 0;
4217 if (!err)
4218 do_bind_con_driver(csw, first, last, deflt);
4219
4220 return err;
4221}
4222EXPORT_SYMBOL_GPL(do_take_over_console);
4223
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004224
4225/*
4226 * give_up_console is a wrapper to unregister_con_driver. It will only
4227 * work if driver is fully unbound.
4228 */
4229void give_up_console(const struct consw *csw)
4230{
Wang YanQing70125e72013-05-09 02:14:39 +08004231 console_lock();
4232 do_unregister_con_driver(csw);
4233 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004234}
4235
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004236static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004237{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004238 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004239
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004240 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4241 if (IS_ERR(vtconsole_class)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004242 pr_warn("Unable to create vt console class; errno = %ld\n",
4243 PTR_ERR(vtconsole_class));
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004244 vtconsole_class = NULL;
4245 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004246
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004247 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004248 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4249 struct con_driver *con = &registered_con_driver[i];
4250
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004251 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01004252 con->dev =
4253 device_create_with_groups(vtconsole_class, NULL,
4254 MKDEV(0, con->node),
4255 con, con_dev_groups,
4256 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004257
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004258 if (IS_ERR(con->dev)) {
Joe Perchesbccf1da2017-10-02 08:48:55 -07004259 pr_warn("Unable to create device for %s; errno = %ld\n",
4260 con->desc, PTR_ERR(con->dev));
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004261 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004262 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07004263 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004264 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004265 }
4266 }
4267
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004268 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004269}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07004270postcore_initcall(vtconsole_class_init);
4271
4272#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07004273
4274/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 * Screen blanking
4276 */
4277
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004278static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08004280 unsigned int mode;
4281
4282 if (get_user(mode, p + 1))
4283 return -EFAULT;
4284
4285 vesa_blank_mode = (mode < 4) ? mode : 0;
4286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287}
4288
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289void do_blank_screen(int entering_gfx)
4290{
4291 struct vc_data *vc = vc_cons[fg_console].d;
4292 int i;
4293
Daniel Vettera1355132019-05-28 11:02:34 +02004294 might_sleep();
4295
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296 WARN_CONSOLE_UNLOCKED();
4297
4298 if (console_blanked) {
4299 if (blank_state == blank_vesa_wait) {
4300 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08004301 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 }
4303 return;
4304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305
4306 /* entering graphics mode? */
4307 if (entering_gfx) {
4308 hide_cursor(vc);
4309 save_screen(vc);
4310 vc->vc_sw->con_blank(vc, -1, 1);
4311 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07004312 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 set_origin(vc);
4314 return;
4315 }
4316
izumib6e8f002007-07-17 04:05:49 -07004317 blank_state = blank_off;
4318
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 /* don't blank graphics */
4320 if (vc->vc_mode != KD_TEXT) {
4321 console_blanked = fg_console + 1;
4322 return;
4323 }
4324
4325 hide_cursor(vc);
4326 del_timer_sync(&console_timer);
4327 blank_timer_expired = 0;
4328
4329 save_screen(vc);
4330 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08004331 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 console_blanked = fg_console + 1;
4333 if (i)
4334 set_origin(vc);
4335
4336 if (console_blank_hook && console_blank_hook(1))
4337 return;
4338
Ville Syrjalad060a322006-01-09 20:53:49 -08004339 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07004340 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 mod_timer(&console_timer, jiffies + vesa_off_interval);
4342 }
Alan Cox8b92e872009-09-19 13:13:24 -07004343 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344}
4345EXPORT_SYMBOL(do_blank_screen);
4346
4347/*
4348 * Called by timer as well as from vt_console_driver
4349 */
4350void do_unblank_screen(int leaving_gfx)
4351{
4352 struct vc_data *vc;
4353
4354 /* This should now always be called from a "sane" (read: can schedule)
4355 * context for the sake of the low level drivers, except in the special
4356 * case of oops_in_progress
4357 */
4358 if (!oops_in_progress)
4359 might_sleep();
4360
4361 WARN_CONSOLE_UNLOCKED();
4362
4363 ignore_poke = 0;
4364 if (!console_blanked)
4365 return;
4366 if (!vc_cons_allocated(fg_console)) {
4367 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08004368 pr_warn("unblank_screen: tty %d not allocated ??\n",
4369 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 return;
4371 }
4372 vc = vc_cons[fg_console].d;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004373 if (vc->vc_mode != KD_TEXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return; /* but leave console_blanked != 0 */
4375
4376 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004377 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 blank_state = blank_normal_wait;
4379 }
4380
4381 console_blanked = 0;
Daniel Vetter8d7fc292018-08-22 10:54:03 +02004382 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 /* Low-level driver cannot restore -> do it ourselves */
4384 update_screen(vc);
4385 if (console_blank_hook)
4386 console_blank_hook(0);
4387 set_palette(vc);
4388 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07004389 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390}
4391EXPORT_SYMBOL(do_unblank_screen);
4392
4393/*
4394 * This is called by the outside world to cause a forced unblank, mostly for
4395 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4396 * call it with 1 as an argument and so force a mode restore... that may kill
4397 * X or at least garbage the screen but would also make the Oops visible...
4398 */
4399void unblank_screen(void)
4400{
4401 do_unblank_screen(0);
4402}
4403
4404/*
Ingo Molnar70522e12006-03-23 03:00:31 -08004405 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08004407 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408 */
Kees Cook24ed9602017-08-28 11:28:21 -07004409static void blank_screen_t(struct timer_list *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 blank_timer_expired = 1;
4412 schedule_work(&console_work);
4413}
4414
4415void poke_blanked_console(void)
4416{
4417 WARN_CONSOLE_UNLOCKED();
4418
4419 /* Add this so we quickly catch whoever might call us in a non
4420 * safe context. Nowadays, unblank_screen() isn't to be called in
4421 * atomic contexts and is allowed to schedule (with the special case
4422 * of oops_in_progress, but that isn't of any concern for this
4423 * function. --BenH.
4424 */
4425 might_sleep();
4426
4427 /* This isn't perfectly race free, but a race here would be mostly harmless,
4428 * at worse, we'll do a spurrious blank and it's unlikely
4429 */
4430 del_timer(&console_timer);
4431 blank_timer_expired = 0;
4432
4433 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4434 return;
4435 if (console_blanked)
4436 unblank_screen();
4437 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07004438 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 blank_state = blank_normal_wait;
4440 }
4441}
4442
4443/*
4444 * Palettes
4445 */
4446
4447static void set_palette(struct vc_data *vc)
4448{
4449 WARN_CONSOLE_UNLOCKED();
4450
Jiri Slaby709280d2016-06-23 13:34:27 +02004451 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 vc->vc_sw->con_set_palette(vc, color_table);
4453}
4454
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455/*
4456 * Load palette into the DAC registers. arg points to a colour
4457 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4458 */
4459
4460int con_set_cmap(unsigned char __user *arg)
4461{
Michael Gehring871bdea2012-03-21 01:26:45 +01004462 int i, j, k;
4463 unsigned char colormap[3*16];
4464
4465 if (copy_from_user(colormap, arg, sizeof(colormap)))
4466 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Torben Hohnac751ef2011-01-25 15:07:35 -08004468 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004469 for (i = k = 0; i < 16; i++) {
4470 default_red[i] = colormap[k++];
4471 default_grn[i] = colormap[k++];
4472 default_blu[i] = colormap[k++];
4473 }
4474 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4475 if (!vc_cons_allocated(i))
4476 continue;
4477 for (j = k = 0; j < 16; j++) {
4478 vc_cons[i].d->vc_palette[k++] = default_red[j];
4479 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4480 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4481 }
4482 set_palette(vc_cons[i].d);
4483 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004484 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
Michael Gehring871bdea2012-03-21 01:26:45 +01004486 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487}
4488
4489int con_get_cmap(unsigned char __user *arg)
4490{
Michael Gehring871bdea2012-03-21 01:26:45 +01004491 int i, k;
4492 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Torben Hohnac751ef2011-01-25 15:07:35 -08004494 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01004495 for (i = k = 0; i < 16; i++) {
4496 colormap[k++] = default_red[i];
4497 colormap[k++] = default_grn[i];
4498 colormap[k++] = default_blu[i];
4499 }
Torben Hohnac751ef2011-01-25 15:07:35 -08004500 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Michael Gehring871bdea2012-03-21 01:26:45 +01004502 if (copy_to_user(arg, colormap, sizeof(colormap)))
4503 return -EFAULT;
4504
4505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
4508void reset_palette(struct vc_data *vc)
4509{
4510 int j, k;
4511 for (j=k=0; j<16; j++) {
4512 vc->vc_palette[k++] = default_red[j];
4513 vc->vc_palette[k++] = default_grn[j];
4514 vc->vc_palette[k++] = default_blu[j];
4515 }
4516 set_palette(vc);
4517}
4518
4519/*
4520 * Font switching
4521 *
4522 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4523 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4524 * depending on width) reserved for each character which is kinda wasty, but
4525 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004526 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 * format (maybe we should add a `fontoffset' field to the `display'
4528 * structure so we won't have to convert the fontdata all the time.
4529 * /Jes
4530 */
4531
4532#define max_font_size 65536
4533
4534static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4535{
4536 struct console_font font;
4537 int rc = -EINVAL;
4538 int c;
4539
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 if (op->data) {
4541 font.data = kmalloc(max_font_size, GFP_KERNEL);
4542 if (!font.data)
4543 return -ENOMEM;
4544 } else
4545 font.data = NULL;
4546
Torben Hohnac751ef2011-01-25 15:07:35 -08004547 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004548 if (vc->vc_mode != KD_TEXT)
4549 rc = -EINVAL;
4550 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 rc = vc->vc_sw->con_font_get(vc, &font);
4552 else
4553 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004554 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
4556 if (rc)
4557 goto out;
4558
4559 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004560
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 if (op->data && font.charcount > op->charcount)
4562 rc = -ENOSPC;
4563 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4564 if (font.width > op->width || font.height > op->height)
4565 rc = -ENOSPC;
4566 } else {
4567 if (font.width != 8)
4568 rc = -EIO;
4569 else if ((op->height && font.height > op->height) ||
4570 font.height > 32)
4571 rc = -ENOSPC;
4572 }
4573 if (rc)
4574 goto out;
4575
4576 op->height = font.height;
4577 op->width = font.width;
4578 op->charcount = font.charcount;
4579
4580 if (op->data && copy_to_user(op->data, font.data, c))
4581 rc = -EFAULT;
4582
4583out:
4584 kfree(font.data);
4585 return rc;
4586}
4587
4588static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4589{
4590 struct console_font font;
4591 int rc = -EINVAL;
4592 int size;
4593
4594 if (vc->vc_mode != KD_TEXT)
4595 return -EINVAL;
4596 if (!op->data)
4597 return -EINVAL;
4598 if (op->charcount > 512)
4599 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 if (op->width <= 0 || op->width > 32 || op->height > 32)
4601 return -EINVAL;
4602 size = (op->width+7)/8 * 32 * op->charcount;
4603 if (size > max_font_size)
4604 return -ENOSPC;
Meng Xu8ffb8202017-10-04 10:38:37 -04004605
Julia Lawall9b71ca22010-05-26 14:42:11 -07004606 font.data = memdup_user(op->data, size);
4607 if (IS_ERR(font.data))
4608 return PTR_ERR(font.data);
Meng Xu8ffb8202017-10-04 10:38:37 -04004609
4610 if (!op->height) { /* Need to guess font height [compat] */
4611 int h, i;
4612 u8 *charmap = font.data;
4613
4614 /*
4615 * If from KDFONTOP ioctl, don't allow things which can be done
4616 * in userland,so that we can get rid of this soon
4617 */
4618 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4619 kfree(font.data);
4620 return -EINVAL;
4621 }
4622
4623 for (h = 32; h > 0; h--)
4624 for (i = 0; i < op->charcount; i++)
4625 if (charmap[32*i+h-1])
4626 goto nonzero;
4627
4628 kfree(font.data);
4629 return -EINVAL;
4630
4631 nonzero:
4632 op->height = h;
4633 }
4634
4635 font.charcount = op->charcount;
4636 font.width = op->width;
4637 font.height = op->height;
4638
Torben Hohnac751ef2011-01-25 15:07:35 -08004639 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004640 if (vc->vc_mode != KD_TEXT)
4641 rc = -EINVAL;
4642 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4644 else
4645 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004646 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 kfree(font.data);
4648 return rc;
4649}
4650
4651static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4652{
4653 struct console_font font = {.width = op->width, .height = op->height};
4654 char name[MAX_FONT_NAME];
4655 char *s = name;
4656 int rc;
4657
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658
4659 if (!op->data)
4660 s = NULL;
4661 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4662 return -EFAULT;
4663 else
4664 name[MAX_FONT_NAME - 1] = 0;
4665
Torben Hohnac751ef2011-01-25 15:07:35 -08004666 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004667 if (vc->vc_mode != KD_TEXT) {
4668 console_unlock();
4669 return -EINVAL;
4670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 if (vc->vc_sw->con_font_default)
4672 rc = vc->vc_sw->con_font_default(vc, &font, s);
4673 else
4674 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004675 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676 if (!rc) {
4677 op->width = font.width;
4678 op->height = font.height;
4679 }
4680 return rc;
4681}
4682
4683static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4684{
4685 int con = op->height;
4686 int rc;
4687
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688
Torben Hohnac751ef2011-01-25 15:07:35 -08004689 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004690 if (vc->vc_mode != KD_TEXT)
4691 rc = -EINVAL;
4692 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 rc = -ENOSYS;
4694 else if (con < 0 || !vc_cons_allocated(con))
4695 rc = -ENOTTY;
4696 else if (con == vc->vc_num) /* nothing to do */
4697 rc = 0;
4698 else
4699 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004700 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 return rc;
4702}
4703
4704int con_font_op(struct vc_data *vc, struct console_font_op *op)
4705{
4706 switch (op->op) {
4707 case KD_FONT_OP_SET:
4708 return con_font_set(vc, op);
4709 case KD_FONT_OP_GET:
4710 return con_font_get(vc, op);
4711 case KD_FONT_OP_SET_DEFAULT:
4712 return con_font_default(vc, op);
4713 case KD_FONT_OP_COPY:
4714 return con_font_copy(vc, op);
4715 }
4716 return -ENOSYS;
4717}
4718
4719/*
4720 * Interface exported to selection and vcs.
4721 */
4722
4723/* used by selection */
4724u16 screen_glyph(struct vc_data *vc, int offset)
4725{
4726 u16 w = scr_readw(screenpos(vc, offset, 1));
4727 u16 c = w & 0xff;
4728
4729 if (w & vc->vc_hi_font_mask)
4730 c |= 0x100;
4731 return c;
4732}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004733EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734
Adam Borowski9bfdc262018-07-18 04:10:44 +02004735u32 screen_glyph_unicode(struct vc_data *vc, int n)
4736{
4737 struct uni_screen *uniscr = get_vc_uniscr(vc);
4738
4739 if (uniscr)
4740 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4741 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4742}
4743EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4744
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745/* used by vcs - note the word offset */
4746unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4747{
4748 return screenpos(vc, 2 * w_offset, viewed);
4749}
Samuel Thibault88867e32016-01-25 01:32:08 +01004750EXPORT_SYMBOL_GPL(screen_pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
4752void getconsxy(struct vc_data *vc, unsigned char *p)
4753{
Nicolas Pitre8a085492019-01-08 22:55:02 -05004754 /* clamp values if they don't fit */
Jiri Slaby28bc24f2020-06-15 09:48:33 +02004755 p[0] = min(vc->state.x, 0xFFu);
4756 p[1] = min(vc->state.y, 0xFFu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757}
4758
4759void putconsxy(struct vc_data *vc, unsigned char *p)
4760{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004761 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 gotoxy(vc, p[0], p[1]);
4763 set_cursor(vc);
4764}
4765
4766u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4767{
4768 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4769 return softcursor_original;
4770 return scr_readw(org);
4771}
4772
4773void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4774{
4775 scr_writew(val, org);
4776 if ((unsigned long)org == vc->vc_pos) {
4777 softcursor_original = -1;
4778 add_softcursor(vc);
4779 }
4780}
4781
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004782void vcs_scr_updated(struct vc_data *vc)
4783{
4784 notify_update(vc);
4785}
4786
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004787void vc_scrolldelta_helper(struct vc_data *c, int lines,
4788 unsigned int rolled_over, void *base, unsigned int size)
4789{
4790 unsigned long ubase = (unsigned long)base;
Jiri Slaby210fd742016-10-03 11:18:36 +02004791 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4792 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4793 ptrdiff_t origin = (void *)c->vc_origin - base;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004794 int margin = c->vc_size_row * 4;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004795 int from, wrap, from_off, avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004796
4797 /* Turn scrollback off */
4798 if (!lines) {
4799 c->vc_visible_origin = c->vc_origin;
4800 return;
4801 }
4802
4803 /* Do we have already enough to allow jumping from 0 to the end? */
Jiri Slaby210fd742016-10-03 11:18:36 +02004804 if (rolled_over > scr_end + margin) {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004805 from = scr_end;
4806 wrap = rolled_over + c->vc_size_row;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004807 } else {
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004808 from = 0;
4809 wrap = size;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004810 }
4811
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004812 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4813 avail = (origin - from + wrap) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004814
4815 /* Only a little piece would be left? Show all incl. the piece! */
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004816 if (avail < 2 * margin)
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004817 margin = 0;
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004818 if (from_off < margin)
4819 from_off = 0;
4820 if (from_off > avail - margin)
4821 from_off = avail;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004822
Jiri Slaby7c918cdc2016-10-03 11:18:37 +02004823 c->vc_visible_origin = ubase + (from + from_off) % wrap;
Jiri Slaby35cc56f2016-10-03 11:18:35 +02004824}
4825EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827/*
4828 * Visible symbols for modules
4829 */
4830
4831EXPORT_SYMBOL(color_table);
4832EXPORT_SYMBOL(default_red);
4833EXPORT_SYMBOL(default_grn);
4834EXPORT_SYMBOL(default_blu);
4835EXPORT_SYMBOL(update_region);
4836EXPORT_SYMBOL(redraw_screen);
4837EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838EXPORT_SYMBOL(fg_console);
4839EXPORT_SYMBOL(console_blank_hook);
4840EXPORT_SYMBOL(console_blanked);
4841EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004842EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844EXPORT_SYMBOL(give_up_console);
4845#endif