vc: extract detecting control characters from do_con_write
[linux-2.6-block.git] / drivers / tty / vt / vt.c
CommitLineData
e3b3d0f5 1// SPDX-License-Identifier: GPL-2.0
1da177e4 2/*
1da177e4
LT
3 * 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
e1f8e874 61 * deletion SMP-safe. 17Jun00, Andrew Morton
1da177e4
LT
62 *
63 * Removed console_lock, enabled interrupts across all console operations
64 * 13 March 2001, Andrew Morton
d4328b40
AT
65 *
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
1da177e4
LT
72 */
73
74#include <linux/module.h>
75#include <linux/types.h>
3f07c014 76#include <linux/sched/signal.h>
1da177e4
LT
77#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>
9a98e7a8 84#include <linux/vmalloc.h>
1da177e4
LT
85#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
c831c338 89#include <linux/mutex.h>
1da177e4
LT
90#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>
1da177e4 97#include <linux/workqueue.h>
1da177e4
LT
98#include <linux/pm.h>
99#include <linux/font.h>
100#include <linux/bitops.h>
b293d758 101#include <linux/notifier.h>
d81ed103
AC
102#include <linux/device.h>
103#include <linux/io.h>
d81ed103 104#include <linux/uaccess.h>
81d44507 105#include <linux/kdb.h>
74c807ce 106#include <linux/ctype.h>
f0a8d84f 107#include <linux/bsearch.h>
d541ae4e 108#include <linux/gcd.h>
1da177e4 109
3e795de7 110#define MAX_NR_CON_DRIVER 16
1da177e4 111
6db4063c 112#define CON_DRIVER_FLAG_MODULE 1
928e964f
AD
113#define CON_DRIVER_FLAG_INIT 2
114#define CON_DRIVER_FLAG_ATTR 4
d364b5c3 115#define CON_DRIVER_FLAG_ZOMBIE 8
3e795de7
AD
116
117struct con_driver {
118 const struct consw *con;
119 const char *desc;
805952a8 120 struct device *dev;
6db4063c 121 int node;
3e795de7
AD
122 int first;
123 int last;
124 int flag;
125};
126
127static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
1da177e4
LT
128const struct consw *conswitchp;
129
1da177e4
LT
130/*
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)
bd63364c 135#define DEFAULT_CURSOR_BLINK_MS 200
1da177e4 136
1da177e4
LT
137struct 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);
403aac96 150static int set_vesa_blanking(char __user *p);
1da177e4
LT
151static void set_cursor(struct vc_data *vc);
152static void hide_cursor(struct vc_data *vc);
65f27f38 153static void console_callback(struct work_struct *ignored);
d364b5c3 154static void con_driver_unregister_callback(struct work_struct *ignored);
24ed960a 155static void blank_screen_t(struct timer_list *unused);
1da177e4
LT
156static void set_palette(struct vc_data *vc);
157
52c40fca
PH
158#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
159
1da177e4 160static int printable; /* Is console ready for printing? */
77bf2bab 161int default_utf8 = true;
042f10ec 162module_param(default_utf8, int, S_IRUGO | S_IWUSR);
f6c06b68
MG
163int global_cursor_default = -1;
164module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
1da177e4 165
9ea9a886
CL
166static int cur_default = CUR_DEFAULT;
167module_param(cur_default, int, S_IRUGO | S_IWUSR);
168
1da177e4
LT
169/*
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 */
1da177e4 179static int vesa_off_interval;
a4199f5e 180static int blankinterval;
f324edc8 181core_param(consoleblank, blankinterval, int, 0444);
1da177e4 182
65f27f38 183static DECLARE_WORK(console_work, console_callback);
d364b5c3 184static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
1da177e4
LT
185
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,
b45cfba4 190 * saved_* variants are for save/restore around kernel debugger enter/leave
1da177e4
LT
191 */
192int fg_console;
193int last_console;
194int want_console = -1;
fed891c8
JW
195static int saved_fg_console;
196static int saved_last_console;
197static int saved_want_console;
198static int saved_vc_mode;
beed5336 199static int saved_console_blanked;
1da177e4
LT
200
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
1d27e3e2 227static DEFINE_TIMER(console_timer, blank_screen_t);
1da177e4
LT
228static int blank_state;
229static int blank_timer_expired;
230enum {
231 blank_off = 0,
232 blank_normal_wait,
233 blank_vesa_wait,
234};
235
fbc92a34
KS
236/*
237 * /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
b293d758
ST
244/*
245 * 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{
502fcb79 263 struct vt_notifier_param param = { .vc = vc, .c = unicode };
b293d758
ST
264 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}
1da177e4
LT
272/*
273 * Low-Level Functions
274 */
275
6ca8dfd7
JS
276static 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}
1da177e4
LT
285
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
e33ac1c1 299/* Called from the keyboard irq path.. */
1da177e4
LT
300static inline void scrolldelta(int lines)
301{
e33ac1c1
AC
302 /* FIXME */
303 /* scrolldelta needs some kind of consistency lock, but the BKL was
304 and still is not protecting versus the scheduled back end */
1da177e4
LT
305 scrollback_delta += lines;
306 schedule_console_callback();
307}
308
309void schedule_console_callback(void)
310{
311 schedule_work(&console_work);
312}
313
d8ae7242
NP
314/*
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
0224080f
NP
325#define VC_UNI_SCREEN_DEBUG 0
326
d8ae7242
NP
327typedef 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 *);
9a98e7a8 346 p = vmalloc(memsize);
d8ae7242
NP
347 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
57d38f26
NP
360static void vc_uniscr_free(struct uni_screen *uniscr)
361{
362 vfree(uniscr);
363}
364
d8ae7242
NP
365static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
366{
57d38f26 367 vc_uniscr_free(vc->vc_uni_screen);
d8ae7242
NP
368 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)
28bc24fc 376 uniscr->lines[vc->state.y][vc->state.x] = uc;
d8ae7242
NP
377}
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) {
28bc24fc
JS
384 char32_t *ln = uniscr->lines[vc->state.y];
385 unsigned int x = vc->state.x, cols = vc->vc_cols;
d8ae7242
NP
386
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) {
28bc24fc
JS
397 char32_t *ln = uniscr->lines[vc->state.y];
398 unsigned int x = vc->state.x, cols = vc->vc_cols;
d8ae7242
NP
399
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) {
28bc24fc 411 char32_t *ln = uniscr->lines[vc->state.y];
d8ae7242
NP
412
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) {
d541ae4e
NP
436 unsigned int i, j, k, sz, d, clear;
437
438 sz = b - t;
439 clear = b - nr;
440 d = nr;
441 if (dir == SM_DOWN) {
442 clear = t;
443 d = sz - nr;
444 }
445 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;
d8ae7242 458 }
d8ae7242
NP
459 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
d21b0be2
NP
494/*
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 */
708d0bff 546void vc_uniscr_copy_line(struct vc_data *vc, void *dest, int viewed,
d21b0be2
NP
547 unsigned int row, unsigned int col, unsigned int nr)
548{
549 struct uni_screen *uniscr = get_vc_uniscr(vc);
708d0bff
NP
550 int offset = row * vc->vc_size_row + col * 2;
551 unsigned long pos;
d21b0be2
NP
552
553 BUG_ON(!uniscr);
708d0bff
NP
554
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 }
d21b0be2
NP
580}
581
0224080f
NP
582/* 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
d8ae7242 618
89765b94
JS
619static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
620 enum con_scroll dir, unsigned int nr)
1da177e4 621{
89765b94 622 u16 *clear, *d, *s;
1da177e4 623
89765b94 624 if (t + nr >= b)
1da177e4
LT
625 nr = b - t - 1;
626 if (b > vc->vc_rows || t >= b || nr < 1)
627 return;
d8ae7242 628 vc_uniscr_scroll(vc, t, b, dir, nr);
89765b94 629 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
1da177e4 630 return;
1da177e4 631
89765b94
JS
632 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
633 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
1da177e4 634
89765b94
JS
635 if (dir == SM_UP) {
636 clear = s + (b - t - nr) * vc->vc_cols;
637 swap(s, d);
638 }
639 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
640 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
1da177e4
LT
641}
642
643static void do_update_region(struct vc_data *vc, unsigned long start, int count)
644{
1da177e4
LT
645 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 }
1da177e4
LT
685}
686
687void update_region(struct vc_data *vc, unsigned long start, int count)
688{
689 WARN_CONSOLE_UNLOCKED();
690
6ca8dfd7 691 if (con_should_update(vc)) {
1da177e4
LT
692 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
b84ae3dc 700static u8 build_attr(struct vc_data *vc, u8 _color,
77bc14f2
JS
701 enum vc_intensity _intensity, bool _blink, bool _underline,
702 bool _reverse, bool _italic)
1da177e4
LT
703{
704 if (vc->vc_sw->con_build_attr)
fa6ce9ab
JE
705 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
706 _blink, _underline, _reverse, _italic);
1da177e4 707
1da177e4
LT
708/*
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 {
c9e587ab 719 u8 a = _color;
1da177e4
LT
720 if (!vc->vc_can_do_color)
721 return _intensity |
fa6ce9ab 722 (_italic ? 2 : 0) |
1da177e4
LT
723 (_underline ? 4 : 0) |
724 (_reverse ? 8 : 0) |
725 (_blink ? 0x80 : 0);
fa6ce9ab
JE
726 if (_italic)
727 a = (a & 0xF0) | vc->vc_itcolor;
728 else if (_underline)
1da177e4 729 a = (a & 0xf0) | vc->vc_ulcolor;
b84ae3dc 730 else if (_intensity == VCI_HALF_BRIGHT)
1c65a879 731 a = (a & 0xf0) | vc->vc_halfcolor;
1da177e4
LT
732 if (_reverse)
733 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
734 if (_blink)
735 a ^= 0x80;
b84ae3dc 736 if (_intensity == VCI_BOLD)
1da177e4
LT
737 a ^= 0x08;
738 if (vc->vc_hi_font_mask == 0x100)
739 a <<= 1;
740 return a;
741 }
1da177e4
LT
742}
743
744static void update_attr(struct vc_data *vc)
745{
28bc24fc
JS
746 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);
b84ae3dc 749 vc->vc_video_erase_char = ' ' | (build_attr(vc, vc->state.color,
77bc14f2
JS
750 VCI_NORMAL, vc->state.blink, false,
751 vc->vc_decscnm, false) << 8);
1da177e4
LT
752}
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);
6af39ed3 763 if (vc->vc_sw->con_invert_region) {
1da177e4 764 vc->vc_sw->con_invert_region(vc, p, count);
6af39ed3 765 } else {
1da177e4
LT
766 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 }
6af39ed3 793
6ca8dfd7 794 if (con_should_update(vc))
1da177e4 795 do_update_region(vc, (unsigned long) p, count);
19e3ae6b 796 notify_update(vc);
1da177e4
LT
797}
798
799/* used by selection: complement pointer position */
800void complement_pos(struct vc_data *vc, int offset)
801{
414edcd3 802 static int old_offset = -1;
1da177e4
LT
803 static unsigned short old;
804 static unsigned short oldx, oldy;
805
806 WARN_CONSOLE_UNLOCKED();
807
414edcd3
AD
808 if (old_offset != -1 && old_offset >= 0 &&
809 old_offset < vc->vc_screenbuf_size) {
810 scr_writew(old, screenpos(vc, old_offset, 1));
6ca8dfd7 811 if (con_should_update(vc))
1da177e4 812 vc->vc_sw->con_putc(vc, old, oldy, oldx);
19e3ae6b 813 notify_update(vc);
1da177e4 814 }
414edcd3
AD
815
816 old_offset = offset;
817
818 if (offset != -1 && offset >= 0 &&
819 offset < vc->vc_screenbuf_size) {
1da177e4 820 unsigned short new;
414edcd3 821 unsigned short *p;
1da177e4
LT
822 p = screenpos(vc, offset, 1);
823 old = scr_readw(p);
824 new = old ^ vc->vc_complement_mask;
825 scr_writew(new, p);
6ca8dfd7 826 if (con_should_update(vc)) {
1da177e4
LT
827 oldx = (offset >> 1) % vc->vc_cols;
828 oldy = (offset >> 1) / vc->vc_cols;
829 vc->vc_sw->con_putc(vc, new, oldy, oldx);
830 }
19e3ae6b 831 notify_update(vc);
1da177e4
LT
832 }
833}
834
835static void insert_char(struct vc_data *vc, unsigned int nr)
836{
81732c3b 837 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 838
d8ae7242 839 vc_uniscr_insert(vc, nr);
28bc24fc 840 scr_memmovew(p + nr, p, (vc->vc_cols - vc->state.x - nr) * 2);
81732c3b 841 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
1da177e4 842 vc->vc_need_wrap = 0;
6ca8dfd7 843 if (con_should_update(vc))
81732c3b 844 do_update_region(vc, (unsigned long) p,
28bc24fc 845 vc->vc_cols - vc->state.x);
1da177e4
LT
846}
847
848static void delete_char(struct vc_data *vc, unsigned int nr)
849{
81732c3b 850 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 851
d8ae7242 852 vc_uniscr_delete(vc, nr);
28bc24fc
JS
853 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,
81732c3b 855 nr * 2);
1da177e4 856 vc->vc_need_wrap = 0;
6ca8dfd7 857 if (con_should_update(vc))
81732c3b 858 do_update_region(vc, (unsigned long) p,
28bc24fc 859 vc->vc_cols - vc->state.x);
1da177e4
LT
860}
861
e882f715 862static int softcursor_original = -1;
1da177e4
LT
863
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);
6ca8dfd7 877 if (con_should_update(vc))
28bc24fc 878 vc->vc_sw->con_putc(vc, i, vc->state.y, vc->state.x);
1da177e4
LT
879}
880
881static void hide_softcursor(struct vc_data *vc)
882{
883 if (softcursor_original != -1) {
884 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
6ca8dfd7 885 if (con_should_update(vc))
1da177e4 886 vc->vc_sw->con_putc(vc, softcursor_original,
28bc24fc 887 vc->state.y, vc->state.x);
1da177e4
LT
888 softcursor_original = -1;
889 }
890}
891
892static void hide_cursor(struct vc_data *vc)
893{
dce05aa6 894 if (vc_is_sel(vc))
1da177e4 895 clear_selection();
dce05aa6 896
1da177e4
LT
897 vc->vc_sw->con_cursor(vc, CM_ERASE);
898 hide_softcursor(vc);
899}
900
901static void set_cursor(struct vc_data *vc)
902{
6ca8dfd7 903 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
1da177e4
LT
904 return;
905 if (vc->vc_deccm) {
dce05aa6 906 if (vc_is_sel(vc))
1da177e4
LT
907 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
6ca8dfd7 919 if (!con_is_visible(vc) ||
1da177e4
LT
920 !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;
28bc24fc
JS
925 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->state.y +
926 2 * vc->state.x;
1da177e4
LT
927}
928
c0f160a7 929static void save_screen(struct vc_data *vc)
1da177e4
LT
930{
931 WARN_CONSOLE_UNLOCKED();
932
933 if (vc->vc_sw->con_save_screen)
934 vc->vc_sw->con_save_screen(vc);
935}
936
bcd375f7
MS
937static void flush_scrollback(struct vc_data *vc)
938{
939 WARN_CONSOLE_UNLOCKED();
940
a6dbe442 941 set_origin(vc);
3f4ef485 942 if (vc->vc_sw->con_flush_scrollback) {
bcd375f7 943 vc->vc_sw->con_flush_scrollback(vc);
3f4ef485
NP
944 } 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);
a6dbe442 954 vc->vc_sw->con_switch(vc);
3f4ef485
NP
955 set_cursor(vc);
956 }
bcd375f7
MS
957}
958
1da177e4
LT
959/*
960 * Redrawing of screen
961 */
962
2a248307 963void clear_buffer_attributes(struct vc_data *vc)
1da177e4
LT
964{
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;
6ca8dfd7 990 if (!con_is_visible(vc))
1da177e4
LT
991 redraw = 1;
992 *vc->vc_display_fg = vc;
993 fg_console = vc->vc_num;
994 hide_cursor(old_vc);
6ca8dfd7 995 if (!con_is_visible(old_vc)) {
1da177e4
LT
996 save_screen(old_vc);
997 set_origin(old_vc);
998 }
fbc92a34
KS
999 if (tty0dev)
1000 sysfs_notify(&tty0dev->kobj, NULL, "active");
1da177e4
LT
1001 } 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 }
8fd4bd22 1023
8d7fc299 1024 if (update && vc->vc_mode != KD_GRAPHICS)
1da177e4
LT
1025 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();
8182ec49 1031 notify_update(vc);
1da177e4
LT
1032 }
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;
08b33249
DZ
1057 if (vc->vc_uni_pagedir_loc)
1058 con_free_unimap(vc);
1da177e4 1059 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
e4bdab70 1060 vc->vc_uni_pagedir = NULL;
1da177e4
LT
1061 vc->vc_hi_font_mask = 0;
1062 vc->vc_complement_mask = 0;
1063 vc->vc_can_do_color = 0;
1b45996d 1064 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
1065 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
a1ad1cc9
GH
1073
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
ca4463bf
EB
1080static 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
1da177e4
LT
1091int vc_allocate(unsigned int currcons) /* return 0 on success */
1092{
34902b7f
JS
1093 struct vt_notifier_param param;
1094 struct vc_data *vc;
1095
1da177e4
LT
1096 WARN_CONSOLE_UNLOCKED();
1097
1098 if (currcons >= MAX_NR_CONSOLES)
1099 return -ENXIO;
34902b7f
JS
1100
1101 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)
1da177e4 1112 return -ENOMEM;
34902b7f
JS
1113
1114 vc_cons[currcons].d = vc;
1115 tty_port_init(&vc->port);
ca4463bf 1116 vc->port.ops = &vc_port_ops;
34902b7f
JS
1117 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
1118
1119 visual_init(vc, currcons, 1);
1120
1121 if (!*vc->vc_uni_pagedir_loc)
1da177e4 1122 con_set_default_unimap(vc);
f6c06b68 1123
21eff69a 1124 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
34902b7f
JS
1125 if (!vc->vc_screenbuf)
1126 goto err_free;
1127
1128 /* 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);
f6c06b68 1136
1da177e4 1137 return 0;
34902b7f 1138err_free:
a1ad1cc9 1139 visual_deinit(vc);
34902b7f
JS
1140 kfree(vc);
1141 vc_cons[currcons].d = NULL;
1142 return -ENOMEM;
1da177e4
LT
1143}
1144
e400b6ec
AD
1145static inline int resize_screen(struct vc_data *vc, int width, int height,
1146 int user)
1da177e4
LT
1147{
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)
e400b6ec
AD
1152 err = vc->vc_sw->con_resize(vc, width, height, user);
1153
1da177e4
LT
1154 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)
8c9a9dd0
AC
1164
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
3ad2f3fb 1175 * information and perform any necessary signal handling.
8c9a9dd0 1176 *
6a1c0680 1177 * Caller must hold the console semaphore. Takes the termios rwsem and
8c9a9dd0
AC
1178 * ctrl_lock of the tty IFF a tty is passed.
1179 */
1180
fc6f6238
AC
1181static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1182 unsigned int cols, unsigned int lines)
1da177e4
LT
1183{
1184 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
9e0ba741 1185 unsigned long end;
d8ae7242 1186 unsigned int old_rows, old_row_size, first_copied_row;
1da177e4 1187 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
9e0ba741 1188 unsigned int user;
1da177e4 1189 unsigned short *newscreen;
d8ae7242 1190 struct uni_screen *new_uniscr = NULL;
1da177e4
LT
1191
1192 WARN_CONSOLE_UNLOCKED();
1193
1194 if (!vc)
1195 return -ENXIO;
1196
e400b6ec
AD
1197 user = vc->vc_resize_user;
1198 vc->vc_resize_user = 0;
1199
1da177e4
LT
1200 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
2717769e 1211 if (new_screen_size > KMALLOC_MAX_SIZE)
32b2921e 1212 return -EINVAL;
21eff69a 1213 newscreen = kzalloc(new_screen_size, GFP_USER);
1da177e4
LT
1214 if (!newscreen)
1215 return -ENOMEM;
1216
d8ae7242
NP
1217 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
dce05aa6 1225 if (vc_is_sel(vc))
009e39ae
SD
1226 clear_selection();
1227
1da177e4 1228 old_rows = vc->vc_rows;
1da177e4 1229 old_row_size = vc->vc_size_row;
1da177e4 1230
e400b6ec 1231 err = resize_screen(vc, new_cols, new_rows, user);
1da177e4
LT
1232 if (err) {
1233 kfree(newscreen);
57d38f26 1234 vc_uniscr_free(new_uniscr);
1da177e4
LT
1235 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;
3b41dc1a 1248
28bc24fc
JS
1249 if (vc->state.y > new_rows) {
1250 if (old_rows - vc->state.y < new_rows) {
3b41dc1a
AD
1251 /*
1252 * Cursor near the bottom, copy contents from the
1253 * bottom of buffer
1254 */
d8ae7242 1255 first_copied_row = (old_rows - new_rows);
3b41dc1a
AD
1256 } else {
1257 /*
1258 * Cursor is in no man's land, copy 1/2 screenful
1259 * from the top and bottom of cursor position
1260 */
28bc24fc 1261 first_copied_row = (vc->state.y - new_rows/2);
3b41dc1a 1262 }
d8ae7242
NP
1263 old_origin += first_copied_row * old_row_size;
1264 } else
1265 first_copied_row = 0;
9fc2b2d0 1266 end = old_origin + old_row_size * min(old_rows, new_rows);
1da177e4 1267
d8ae7242
NP
1268 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
1da177e4
LT
1273 update_attr(vc);
1274
3b41dc1a
AD
1275 while (old_origin < end) {
1276 scr_memcpyw((unsigned short *) new_origin,
1277 (unsigned short *) old_origin, rlth);
1da177e4 1278 if (rrem)
3b41dc1a
AD
1279 scr_memsetw((void *)(new_origin + rlth),
1280 vc->vc_video_erase_char, rrem);
1da177e4
LT
1281 old_origin += old_row_size;
1282 new_origin += new_row_size;
1283 }
1284 if (new_scr_end > new_origin)
3b41dc1a
AD
1285 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1286 new_scr_end - new_origin);
5c9228f0 1287 kfree(vc->vc_screenbuf);
1da177e4 1288 vc->vc_screenbuf = newscreen;
1da177e4
LT
1289 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;
28bc24fc 1295 gotoxy(vc, vc->state.x, vc->state.y);
1da177e4
LT
1296 save_cur(vc);
1297
8c9a9dd0
AC
1298 if (tty) {
1299 /* Rewrite the requested winsize data with the actual
1300 resulting sizes */
1301 struct winsize ws;
1da177e4
LT
1302 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;
fc6f6238 1306 tty_do_resize(tty, &ws);
1da177e4
LT
1307 }
1308
6ca8dfd7 1309 if (con_is_visible(vc))
1da177e4 1310 update_screen(vc);
8b92e87d 1311 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
0c9b1965 1312 notify_update(vc);
1da177e4
LT
1313 return err;
1314}
1315
8c9a9dd0
AC
1316/**
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
8ce73264 1325 * vc->port.tty
8c9a9dd0
AC
1326 */
1327
1328int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
1329{
8ce73264 1330 return vc_do_resize(vc->port.tty, vc, cols, rows);
8c9a9dd0
AC
1331}
1332
1333/**
1334 * vt_resize - resize a VT
1335 * @tty: tty to resize
8c9a9dd0
AC
1336 * @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
6a1c0680 1343 * termios_rwsem and the tty ctrl_lock in that order.
8c9a9dd0 1344 */
da2bdf9a 1345static int vt_resize(struct tty_struct *tty, struct winsize *ws)
ca9bda00 1346{
8c9a9dd0
AC
1347 struct vc_data *vc = tty->driver_data;
1348 int ret;
ca9bda00 1349
ac751efa 1350 console_lock();
fc6f6238 1351 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
ac751efa 1352 console_unlock();
8c9a9dd0 1353 return ret;
ca9bda00 1354}
1da177e4 1355
421b40a6 1356struct vc_data *vc_deallocate(unsigned int currcons)
1da177e4 1357{
421b40a6
PH
1358 struct vc_data *vc = NULL;
1359
1da177e4
LT
1360 WARN_CONSOLE_UNLOCKED();
1361
1362 if (vc_cons_allocated(currcons)) {
421b40a6 1363 struct vt_notifier_param param;
4995f8ef 1364
421b40a6 1365 param.vc = vc = vc_cons[currcons].d;
b293d758 1366 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
4995f8ef 1367 vcs_remove_sysfs(currcons);
a1ad1cc9 1368 visual_deinit(vc);
bde0d2c9 1369 put_pid(vc->vt_pid);
d8ae7242 1370 vc_uniscr_set(vc, NULL);
5c9228f0 1371 kfree(vc->vc_screenbuf);
1da177e4
LT
1372 vc_cons[currcons].d = NULL;
1373 }
421b40a6 1374 return vc;
1da177e4
LT
1375}
1376
1377/*
1378 * VT102 emulator
1379 */
1380
2ff5c5a1
MH
1381enum { EPecma = 0, EPdec, EPeq, EPgt, EPlt};
1382
079c9534
AC
1383#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))
1da177e4
LT
1386
1387#define decarm VC_REPEAT
1388#define decckm VC_CKMODE
1389#define kbdapplic VC_APPLIC
1390#define lnm VC_CRLF
1391
8ede5cce 1392const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1da177e4
LT
1393 8,12,10,14, 9,13,11,15 };
1394
1395/* the default colour table, for VGA+ colour systems */
91e74ca5
JS
1396unsigned 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);
1401
1402unsigned 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);
1c2bbe6a 1413
1da177e4
LT
1414/*
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)
28bc24fc 1424 vc->state.x = 0;
1da177e4
LT
1425 else {
1426 if (new_x >= vc->vc_cols)
28bc24fc 1427 vc->state.x = vc->vc_cols - 1;
1da177e4 1428 else
28bc24fc 1429 vc->state.x = new_x;
1da177e4
LT
1430 }
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)
28bc24fc 1440 vc->state.y = min_y;
1da177e4 1441 else if (new_y >= max_y)
28bc24fc 1442 vc->state.y = max_y - 1;
1da177e4 1443 else
28bc24fc
JS
1444 vc->state.y = new_y;
1445 vc->vc_pos = vc->vc_origin + vc->state.y * vc->vc_size_row +
1446 (vc->state.x << 1);
1da177e4
LT
1447 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
1b0ec88a 1456void scrollback(struct vc_data *vc)
1da177e4 1457{
1b0ec88a 1458 scrolldelta(-(vc->vc_rows / 2));
1da177e4
LT
1459}
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 */
28bc24fc 1473 if (vc->state.y + 1 == vc->vc_bottom)
89765b94 1474 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
28bc24fc
JS
1475 else if (vc->state.y < vc->vc_rows - 1) {
1476 vc->state.y++;
1da177e4
LT
1477 vc->vc_pos += vc->vc_size_row;
1478 }
1479 vc->vc_need_wrap = 0;
b293d758 1480 notify_write(vc, '\n');
1da177e4
LT
1481}
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 */
28bc24fc 1488 if (vc->state.y == vc->vc_top)
89765b94 1489 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
28bc24fc
JS
1490 else if (vc->state.y > 0) {
1491 vc->state.y--;
1da177e4
LT
1492 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{
28bc24fc
JS
1499 vc->vc_pos -= vc->state.x << 1;
1500 vc->vc_need_wrap = vc->state.x = 0;
b293d758 1501 notify_write(vc, '\r');
1da177e4
LT
1502}
1503
1504static inline void bs(struct vc_data *vc)
1505{
28bc24fc 1506 if (vc->state.x) {
1da177e4 1507 vc->vc_pos -= 2;
28bc24fc 1508 vc->state.x--;
1da177e4 1509 vc->vc_need_wrap = 0;
b293d758 1510 notify_write(vc, '\b');
1da177e4
LT
1511 }
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 */
28bc24fc
JS
1526 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);
1da177e4
LT
1530 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1531 start = (unsigned short *)vc->vc_pos;
1da177e4
LT
1532 break;
1533 case 1: /* erase from start to cursor */
28bc24fc
JS
1534 vc_uniscr_clear_line(vc, 0, vc->state.x + 1);
1535 vc_uniscr_clear_lines(vc, 0, vc->state.y);
1da177e4
LT
1536 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1537 start = (unsigned short *)vc->vc_origin;
1da177e4 1538 break;
a6dbe442
NP
1539 case 3: /* include scrollback */
1540 flush_scrollback(vc);
1541 /* fallthrough */
1da177e4 1542 case 2: /* erase whole display */
d8ae7242 1543 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
1da177e4
LT
1544 count = vc->vc_cols * vc->vc_rows;
1545 start = (unsigned short *)vc->vc_origin;
1da177e4
LT
1546 break;
1547 default:
1548 return;
1549 }
1550 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
b2ecf006
MP
1551 if (con_should_update(vc))
1552 do_update_region(vc, (unsigned long) start, count);
1da177e4
LT
1553 vc->vc_need_wrap = 0;
1554}
1555
1556static void csi_K(struct vc_data *vc, int vpar)
1557{
1558 unsigned int count;
d8ae7242
NP
1559 unsigned short *start = (unsigned short *)vc->vc_pos;
1560 int offset;
1da177e4
LT
1561
1562 switch (vpar) {
1563 case 0: /* erase from cursor to end of line */
d8ae7242 1564 offset = 0;
28bc24fc 1565 count = vc->vc_cols - vc->state.x;
1da177e4
LT
1566 break;
1567 case 1: /* erase from start of line to cursor */
28bc24fc
JS
1568 offset = -vc->state.x;
1569 count = vc->state.x + 1;
1da177e4
LT
1570 break;
1571 case 2: /* erase whole line */
28bc24fc 1572 offset = -vc->state.x;
1da177e4 1573 count = vc->vc_cols;
1da177e4
LT
1574 break;
1575 default:
1576 return;
1577 }
28bc24fc 1578 vc_uniscr_clear_line(vc, vc->state.x + offset, count);
d8ae7242 1579 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
1da177e4 1580 vc->vc_need_wrap = 0;
6ca8dfd7 1581 if (con_should_update(vc))
943210ba 1582 do_update_region(vc, (unsigned long)(start + offset), count);
1da177e4
LT
1583}
1584
1585static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1586{ /* not vt100? */
1587 int count;
1588
1589 if (!vpar)
1590 vpar++;
28bc24fc 1591 count = (vpar > vc->vc_cols - vc->state.x) ? (vc->vc_cols - vc->state.x) : vpar;
1da177e4 1592
28bc24fc 1593 vc_uniscr_clear_line(vc, vc->state.x, count);
1da177e4 1594 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
6ca8dfd7 1595 if (con_should_update(vc))
28bc24fc 1596 vc->vc_sw->con_clear(vc, vc->state.y, vc->state.x, 1, count);
1da177e4
LT
1597 vc->vc_need_wrap = 0;
1598}
1599
1600static void default_attr(struct vc_data *vc)
1601{
b84ae3dc 1602 vc->state.intensity = VCI_NORMAL;
77bc14f2
JS
1603 vc->state.italic = false;
1604 vc->state.underline = false;
1605 vc->state.reverse = false;
1606 vc->state.blink = false;
28bc24fc 1607 vc->state.color = vc->vc_def_color;
1da177e4
LT
1608}
1609
cec5b2a9
AB
1610struct rgb { u8 r; u8 g; u8 b; };
1611
0f91e142 1612static void rgb_from_256(int i, struct rgb *c)
cec5b2a9 1613{
cec5b2a9 1614 if (i < 8) { /* Standard colours. */
0f91e142
JS
1615 c->r = i&1 ? 0xaa : 0x00;
1616 c->g = i&2 ? 0xaa : 0x00;
1617 c->b = i&4 ? 0xaa : 0x00;
cec5b2a9 1618 } else if (i < 16) {
0f91e142
JS
1619 c->r = i&1 ? 0xff : 0x55;
1620 c->g = i&2 ? 0xff : 0x55;
1621 c->b = i&4 ? 0xff : 0x55;
cec5b2a9 1622 } else if (i < 232) { /* 6x6x6 colour cube. */
0f91e142
JS
1623 c->r = (i - 16) / 36 * 85 / 2;
1624 c->g = (i - 16) / 6 % 6 * 85 / 2;
1625 c->b = (i - 16) % 6 * 85 / 2;
cec5b2a9 1626 } else /* Grayscale ramp. */
0f91e142 1627 c->r = c->g = c->b = i * 10 - 2312;
cec5b2a9
AB
1628}
1629
0f91e142 1630static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
cec5b2a9 1631{
193df022
JS
1632 u8 hue = 0, max = max3(c->r, c->g, c->b);
1633
1634 if (c->r > max / 2)
1635 hue |= 4;
1636 if (c->g > max / 2)
1637 hue |= 2;
1638 if (c->b > max / 2)
1639 hue |= 1;
1640
1641 if (hue == 7 && max <= 0x55) {
1642 hue = 0;
b84ae3dc 1643 vc->state.intensity = VCI_BOLD;
193df022 1644 } else if (max > 0xaa)
b84ae3dc 1645 vc->state.intensity = VCI_BOLD;
cec5b2a9 1646 else
b84ae3dc 1647 vc->state.intensity = VCI_NORMAL;
193df022 1648
28bc24fc 1649 vc->state.color = (vc->state.color & 0xf0) | hue;
cec5b2a9
AB
1650}
1651
0f91e142 1652static void rgb_background(struct vc_data *vc, const struct rgb *c)
cec5b2a9
AB
1653{
1654 /* For backgrounds, err on the dark side. */
28bc24fc 1655 vc->state.color = (vc->state.color & 0x0f)
0f91e142 1656 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
cec5b2a9
AB
1657}
1658
e05ab238
JS
1659/*
1660 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
04afcd3a
MH
1661 * and thus need to be detected and ignored by hand. That standard also
1662 * wants : rather than ; as separators but sequences containing : are currently
1663 * completely ignored by the parser.
e05ab238
JS
1664 *
1665 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1666 * supporting them.
1667 */
1668static int vc_t416_color(struct vc_data *vc, int i,
0f91e142 1669 void(*set_color)(struct vc_data *vc, const struct rgb *c))
e05ab238 1670{
0f91e142
JS
1671 struct rgb c;
1672
e05ab238
JS
1673 i++;
1674 if (i > vc->vc_npar)
1675 return i;
1676
0bf1f4a8 1677 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
3e7ec4a0 1678 /* 256 colours */
e05ab238 1679 i++;
0f91e142 1680 rgb_from_256(vc->vc_par[i], &c);
669e0a51 1681 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
3e7ec4a0 1682 /* 24 bit */
0f91e142
JS
1683 c.r = vc->vc_par[i + 1];
1684 c.g = vc->vc_par[i + 2];
1685 c.b = vc->vc_par[i + 3];
e05ab238 1686 i += 3;
0f91e142
JS
1687 } else
1688 return i;
1689
1690 set_color(vc, &c);
e05ab238
JS
1691
1692 return i;
1693}
1694
ac751efa 1695/* console_lock is held */
1da177e4
LT
1696static void csi_m(struct vc_data *vc)
1697{
1698 int i;
1699
1700 for (i = 0; i <= vc->vc_npar; i++)
1701 switch (vc->vc_par[i]) {
aada0a34
JS
1702 case 0: /* all attributes off */
1703 default_attr(vc);
1704 break;
1705 case 1:
b84ae3dc 1706 vc->state.intensity = VCI_BOLD;
aada0a34
JS
1707 break;
1708 case 2:
b84ae3dc 1709 vc->state.intensity = VCI_HALF_BRIGHT;
aada0a34
JS
1710 break;
1711 case 3:
77bc14f2 1712 vc->state.italic = true;
aada0a34 1713 break;
65d9982d
MF
1714 case 21:
1715 /*
1716 * No console drivers support double underline, so
1717 * convert it to a single underline.
1718 */
aada0a34 1719 case 4:
77bc14f2 1720 vc->state.underline = true;
aada0a34
JS
1721 break;
1722 case 5:
77bc14f2 1723 vc->state.blink = true;
aada0a34
JS
1724 break;
1725 case 7:
77bc14f2 1726 vc->state.reverse = true;
aada0a34
JS
1727 break;
1728 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1729 * Select primary font, don't display control chars if
1730 * defined, don't set bit 8 on output.
1731 */
b70ec4d9 1732 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset], vc);
aada0a34
JS
1733 vc->vc_disp_ctrl = 0;
1734 vc->vc_toggle_meta = 0;
1735 break;
1736 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1737 * Select first alternate font, lets chars < 32 be
1738 * displayed as ROM chars.
1739 */
1740 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1741 vc->vc_disp_ctrl = 1;
1742 vc->vc_toggle_meta = 0;
1743 break;
1744 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1745 * Select second alternate font, toggle high bit
1746 * before displaying as ROM char.
1747 */
1748 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1749 vc->vc_disp_ctrl = 1;
1750 vc->vc_toggle_meta = 1;
1751 break;
aada0a34 1752 case 22:
b84ae3dc 1753 vc->state.intensity = VCI_NORMAL;
aada0a34
JS
1754 break;
1755 case 23:
77bc14f2 1756 vc->state.italic = false;
aada0a34
JS
1757 break;
1758 case 24:
77bc14f2 1759 vc->state.underline = false;
aada0a34
JS
1760 break;
1761 case 25:
77bc14f2 1762 vc->state.blink = false;
aada0a34
JS
1763 break;
1764 case 27:
77bc14f2 1765 vc->state.reverse = false;
aada0a34
JS
1766 break;
1767 case 38:
1768 i = vc_t416_color(vc, i, rgb_foreground);
1769 break;
1770 case 48:
1771 i = vc_t416_color(vc, i, rgb_background);
1772 break;
1773 case 39:
28bc24fc
JS
1774 vc->state.color = (vc->vc_def_color & 0x0f) |
1775 (vc->state.color & 0xf0);
aada0a34
JS
1776 break;
1777 case 49:
28bc24fc
JS
1778 vc->state.color = (vc->vc_def_color & 0xf0) |
1779 (vc->state.color & 0x0f);
aada0a34
JS
1780 break;
1781 default:
fadb4244
AB
1782 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1783 if (vc->vc_par[i] < 100)
b84ae3dc 1784 vc->state.intensity = VCI_BOLD;
cc67dc28
AB
1785 vc->vc_par[i] -= 60;
1786 }
aada0a34 1787 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
28bc24fc
JS
1788 vc->state.color = color_table[vc->vc_par[i] - 30]
1789 | (vc->state.color & 0xf0);
aada0a34 1790 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
28bc24fc
JS
1791 vc->state.color = (color_table[vc->vc_par[i] - 40] << 4)
1792 | (vc->state.color & 0x0f);
aada0a34 1793 break;
1da177e4
LT
1794 }
1795 update_attr(vc);
1796}
1797
de53ce04 1798static void respond_string(const char *p, size_t len, struct tty_port *port)
1da177e4 1799{
de53ce04 1800 tty_insert_flip_string(port, p, len);
6732c8bb 1801 tty_schedule_flip(port);
1da177e4
LT
1802}
1803
1804static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1805{
1806 char buf[40];
de53ce04 1807 int len;
1da177e4 1808
de53ce04
JS
1809 len = sprintf(buf, "\033[%d;%dR", vc->state.y +
1810 (vc->vc_decom ? vc->vc_top + 1 : 1),
1811 vc->state.x + 1);
1812 respond_string(buf, len, tty->port);
1da177e4
LT
1813}
1814
1815static inline void status_report(struct tty_struct *tty)
1816{
de53ce04
JS
1817 static const char teminal_ok[] = "\033[0n";
1818
1819 respond_string(teminal_ok, strlen(teminal_ok), tty->port);
1da177e4
LT
1820}
1821
6732c8bb 1822static inline void respond_ID(struct tty_struct *tty)
1da177e4 1823{
9a6f72d9
JS
1824 /* terminal answer to an ESC-Z or csi0c query. */
1825 static const char vt102_id[] = "\033[?6c";
1826
1827 respond_string(vt102_id, strlen(vt102_id), tty->port);
1da177e4
LT
1828}
1829
1830void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1831{
1832 char buf[8];
de53ce04 1833 int len;
1da177e4 1834
de53ce04
JS
1835 len = sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt),
1836 (char)('!' + mrx), (char)('!' + mry));
1837 respond_string(buf, len, tty->port);
1da177e4
LT
1838}
1839
496124e5 1840/* invoked via ioctl(TIOCLINUX) and through set_selection_user */
1da177e4
LT
1841int mouse_reporting(void)
1842{
1843 return vc_cons[fg_console].d->vc_report_mouse;
1844}
1845
ac751efa 1846/* console_lock is held */
1da177e4
LT
1847static void set_mode(struct vc_data *vc, int on_off)
1848{
1849 int i;
1850
1851 for (i = 0; i <= vc->vc_npar; i++)
2ff5c5a1 1852 if (vc->vc_priv == EPdec) {
1da177e4
LT
1853 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1854 case 1: /* Cursor keys send ^[Ox/^[[x */
1855 if (on_off)
1856 set_kbd(vc, decckm);
1857 else
1858 clr_kbd(vc, decckm);
1859 break;
1860 case 3: /* 80/132 mode switch unimplemented */
1da177e4
LT
1861#if 0
1862 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1863 /* this alone does not suffice; some user mode
1864 utility has to change the hardware regs */
1865#endif
1866 break;
1867 case 5: /* Inverted screen on/off */
1868 if (vc->vc_decscnm != on_off) {
1869 vc->vc_decscnm = on_off;
1870 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1871 update_attr(vc);
1872 }
1873 break;
1874 case 6: /* Origin relative/absolute */
1875 vc->vc_decom = on_off;
1876 gotoxay(vc, 0, 0);
1877 break;
1878 case 7: /* Autowrap on/off */
1879 vc->vc_decawm = on_off;
1880 break;
1881 case 8: /* Autorepeat on/off */
1882 if (on_off)
1883 set_kbd(vc, decarm);
1884 else
1885 clr_kbd(vc, decarm);
1886 break;
1887 case 9:
1888 vc->vc_report_mouse = on_off ? 1 : 0;
1889 break;
1890 case 25: /* Cursor on/off */
1891 vc->vc_deccm = on_off;
1892 break;
1893 case 1000:
1894 vc->vc_report_mouse = on_off ? 2 : 0;
1895 break;
1896 }
1897 } else {
1898 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1899 case 3: /* Monitor (display ctrls) */
1900 vc->vc_disp_ctrl = on_off;
1901 break;
1902 case 4: /* Insert Mode on/off */
1903 vc->vc_decim = on_off;
1904 break;
1905 case 20: /* Lf, Enter == CrLf/Lf */
1906 if (on_off)
1907 set_kbd(vc, lnm);
1908 else
1909 clr_kbd(vc, lnm);
1910 break;
1911 }
1912 }
1913}
1914
ac751efa 1915/* console_lock is held */
1da177e4
LT
1916static void setterm_command(struct vc_data *vc)
1917{
c3a834e8
JS
1918 switch (vc->vc_par[0]) {
1919 case 1: /* set color for underline mode */
1920 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1921 vc->vc_ulcolor = color_table[vc->vc_par[1]];
28bc24fc 1922 if (vc->state.underline)
c3a834e8
JS
1923 update_attr(vc);
1924 }
1925 break;
1926 case 2: /* set color for half intensity mode */
1927 if (vc->vc_can_do_color && vc->vc_par[1] < 16) {
1928 vc->vc_halfcolor = color_table[vc->vc_par[1]];
b84ae3dc 1929 if (vc->state.intensity == VCI_HALF_BRIGHT)
c3a834e8
JS
1930 update_attr(vc);
1931 }
1932 break;
1933 case 8: /* store colors as defaults */
1934 vc->vc_def_color = vc->vc_attr;
1935 if (vc->vc_hi_font_mask == 0x100)
1936 vc->vc_def_color >>= 1;
1937 default_attr(vc);
1938 update_attr(vc);
1939 break;
1940 case 9: /* set blanking interval */
1941 blankinterval = min(vc->vc_par[1], 60U) * 60;
1942 poke_blanked_console();
1943 break;
1944 case 10: /* set bell frequency in Hz */
1945 if (vc->vc_npar >= 1)
1946 vc->vc_bell_pitch = vc->vc_par[1];
1947 else
1948 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1949 break;
1950 case 11: /* set bell duration in msec */
1951 if (vc->vc_npar >= 1)
1952 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1953 msecs_to_jiffies(vc->vc_par[1]) : 0;
1954 else
1955 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1956 break;
1957 case 12: /* bring specified console to the front */
1958 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1959 set_console(vc->vc_par[1] - 1);
1960 break;
1961 case 13: /* unblank the screen */
1962 poke_blanked_console();
1963 break;
1964 case 14: /* set vesa powerdown interval */
1965 vesa_off_interval = min(vc->vc_par[1], 60U) * 60 * HZ;
1966 break;
1967 case 15: /* activate the previous console */
1968 set_console(last_console);
1969 break;
1970 case 16: /* set cursor blink duration in msec */
1971 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1972 vc->vc_par[1] <= USHRT_MAX)
1973 vc->vc_cur_blink_ms = vc->vc_par[1];
1974 else
1975 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1976 break;
1da177e4
LT
1977 }
1978}
1979
ac751efa 1980/* console_lock is held */
1da177e4
LT
1981static void csi_at(struct vc_data *vc, unsigned int nr)
1982{
28bc24fc
JS
1983 if (nr > vc->vc_cols - vc->state.x)
1984 nr = vc->vc_cols - vc->state.x;
1da177e4
LT
1985 else if (!nr)
1986 nr = 1;
1987 insert_char(vc, nr);
1988}
1989
ac751efa 1990/* console_lock is held */
1da177e4
LT
1991static void csi_L(struct vc_data *vc, unsigned int nr)
1992{
28bc24fc
JS
1993 if (nr > vc->vc_rows - vc->state.y)
1994 nr = vc->vc_rows - vc->state.y;
1da177e4
LT
1995 else if (!nr)
1996 nr = 1;
28bc24fc 1997 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_DOWN, nr);
1da177e4
LT
1998 vc->vc_need_wrap = 0;
1999}
2000
ac751efa 2001/* console_lock is held */
1da177e4
LT
2002static void csi_P(struct vc_data *vc, unsigned int nr)
2003{
28bc24fc
JS
2004 if (nr > vc->vc_cols - vc->state.x)
2005 nr = vc->vc_cols - vc->state.x;
1da177e4
LT
2006 else if (!nr)
2007 nr = 1;
2008 delete_char(vc, nr);
2009}
2010
ac751efa 2011/* console_lock is held */
1da177e4
LT
2012static void csi_M(struct vc_data *vc, unsigned int nr)
2013{
28bc24fc
JS
2014 if (nr > vc->vc_rows - vc->state.y)
2015 nr = vc->vc_rows - vc->state.y;
1da177e4
LT
2016 else if (!nr)
2017 nr=1;
28bc24fc 2018 con_scroll(vc, vc->state.y, vc->vc_bottom, SM_UP, nr);
1da177e4
LT
2019 vc->vc_need_wrap = 0;
2020}
2021
ac751efa 2022/* console_lock is held (except via vc_init->reset_terminal */
1da177e4
LT
2023static void save_cur(struct vc_data *vc)
2024{
28bc24fc 2025 memcpy(&vc->saved_state, &vc->state, sizeof(vc->state));
1da177e4
LT
2026}
2027
ac751efa 2028/* console_lock is held */
1da177e4
LT
2029static void restore_cur(struct vc_data *vc)
2030{
28bc24fc
JS
2031 memcpy(&vc->state, &vc->saved_state, sizeof(vc->state));
2032
2033 gotoxy(vc, vc->state.x, vc->state.y);
b70ec4d9
JS
2034 vc->vc_translate = set_translate(vc->state.Gx_charset[vc->state.charset],
2035 vc);
1da177e4
LT
2036 update_attr(vc);
2037 vc->vc_need_wrap = 0;
2038}
2039
b290af68 2040enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
7a99565f 2041 EShash, ESsetG0, ESsetG1, ESpercent, EScsiignore, ESnonstd,
63f3a16d 2042 ESpalette, ESosc };
1da177e4 2043
ac751efa 2044/* console_lock is held (except via vc_init()) */
1da177e4
LT
2045static void reset_terminal(struct vc_data *vc, int do_clear)
2046{
dbee4cff
JS
2047 unsigned int i;
2048
1da177e4
LT
2049 vc->vc_top = 0;
2050 vc->vc_bottom = vc->vc_rows;
2051 vc->vc_state = ESnormal;
2ff5c5a1 2052 vc->vc_priv = EPecma;
1da177e4 2053 vc->vc_translate = set_translate(LAT1_MAP, vc);
b70ec4d9
JS
2054 vc->state.Gx_charset[0] = LAT1_MAP;
2055 vc->state.Gx_charset[1] = GRAF_MAP;
28bc24fc 2056 vc->state.charset = 0;
1da177e4
LT
2057 vc->vc_need_wrap = 0;
2058 vc->vc_report_mouse = 0;
042f10ec 2059 vc->vc_utf = default_utf8;
1da177e4
LT
2060 vc->vc_utf_count = 0;
2061
2062 vc->vc_disp_ctrl = 0;
2063 vc->vc_toggle_meta = 0;
2064
2065 vc->vc_decscnm = 0;
2066 vc->vc_decom = 0;
2067 vc->vc_decawm = 1;
f6c06b68 2068 vc->vc_deccm = global_cursor_default;
1da177e4
LT
2069 vc->vc_decim = 0;
2070
079c9534 2071 vt_reset_keyboard(vc->vc_num);
1da177e4 2072
9ea9a886 2073 vc->vc_cursor_type = cur_default;
1da177e4
LT
2074 vc->vc_complement_mask = vc->vc_s_complement_mask;
2075
2076 default_attr(vc);
2077 update_attr(vc);
2078
dbee4cff
JS
2079 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
2080 for (i = 0; i < VC_TABSTOPS_COUNT; i += 8)
2081 set_bit(i, vc->vc_tab_stop);
1da177e4
LT
2082
2083 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
2084 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
bd63364c 2085 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
2086
2087 gotoxy(vc, 0, 0);
2088 save_cur(vc);
2089 if (do_clear)
2090 csi_J(vc, 2);
2091}
2092
b4d92b65
JS
2093static void vc_setGx(struct vc_data *vc, unsigned int which, int c)
2094{
b70ec4d9 2095 unsigned char *charset = &vc->state.Gx_charset[which];
b4d92b65
JS
2096
2097 switch (c) {
2098 case '0':
2099 *charset = GRAF_MAP;
2100 break;
2101 case 'B':
2102 *charset = LAT1_MAP;
2103 break;
2104 case 'U':
2105 *charset = IBMPC_MAP;
2106 break;
2107 case 'K':
2108 *charset = USER_MAP;
2109 break;
2110 }
2111
2112 if (vc->state.charset == which)
2113 vc->vc_translate = set_translate(*charset, vc);
2114}
2115
ac751efa 2116/* console_lock is held */
1da177e4
LT
2117static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
2118{
2119 /*
2120 * Control characters can be used in the _middle_
2121 * of an escape sequence.
2122 */
63f3a16d
AB
2123 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
2124 return;
1da177e4
LT
2125 switch (c) {
2126 case 0:
2127 return;
2128 case 7:
63f3a16d
AB
2129 if (vc->vc_state == ESosc)
2130 vc->vc_state = ESnormal;
2131 else if (vc->vc_bell_duration)
1da177e4
LT
2132 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
2133 return;
2134 case 8:
2135 bs(vc);
2136 return;
2137 case 9:
28bc24fc 2138 vc->vc_pos -= (vc->state.x << 1);
dbee4cff
JS
2139
2140 vc->state.x = find_next_bit(vc->vc_tab_stop,
2141 min(vc->vc_cols - 1, VC_TABSTOPS_COUNT),
2142 vc->state.x + 1);
2143 if (vc->state.x >= VC_TABSTOPS_COUNT)
2144 vc->state.x = vc->vc_cols - 1;
2145
28bc24fc 2146 vc->vc_pos += (vc->state.x << 1);
b293d758 2147 notify_write(vc, '\t');
1da177e4
LT
2148 return;
2149 case 10: case 11: case 12:
2150 lf(vc);
2151 if (!is_kbd(vc, lnm))
2152 return;
17504fd7 2153 /* fall through */
1da177e4
LT
2154 case 13:
2155 cr(vc);
2156 return;
2157 case 14:
28bc24fc 2158 vc->state.charset = 1;
b70ec4d9 2159 vc->vc_translate = set_translate(vc->state.Gx_charset[1], vc);
1da177e4
LT
2160 vc->vc_disp_ctrl = 1;
2161 return;
2162 case 15:
28bc24fc 2163 vc->state.charset = 0;
b70ec4d9 2164 vc->vc_translate = set_translate(vc->state.Gx_charset[0], vc);
1da177e4
LT
2165 vc->vc_disp_ctrl = 0;
2166 return;
2167 case 24: case 26:
2168 vc->vc_state = ESnormal;
2169 return;
2170 case 27:
2171 vc->vc_state = ESesc;
2172 return;
2173 case 127:
2174 del(vc);
2175 return;
2176 case 128+27:
2177 vc->vc_state = ESsquare;
2178 return;
2179 }
2180 switch(vc->vc_state) {
2181 case ESesc:
2182 vc->vc_state = ESnormal;
2183 switch (c) {
2184 case '[':
2185 vc->vc_state = ESsquare;
2186 return;
2187 case ']':
2188 vc->vc_state = ESnonstd;
2189 return;
2190 case '%':
2191 vc->vc_state = ESpercent;
2192 return;
2193 case 'E':
2194 cr(vc);
2195 lf(vc);
2196 return;
2197 case 'M':
2198 ri(vc);
2199 return;
2200 case 'D':
2201 lf(vc);
2202 return;
2203 case 'H':
dbee4cff
JS
2204 if (vc->state.x < VC_TABSTOPS_COUNT)
2205 set_bit(vc->state.x, vc->vc_tab_stop);
1da177e4
LT
2206 return;
2207 case 'Z':
2208 respond_ID(tty);
2209 return;
2210 case '7':
2211 save_cur(vc);
2212 return;
2213 case '8':
2214 restore_cur(vc);
2215 return;
2216 case '(':
2217 vc->vc_state = ESsetG0;
2218 return;
2219 case ')':
2220 vc->vc_state = ESsetG1;
2221 return;
2222 case '#':
2223 vc->vc_state = EShash;
2224 return;
2225 case 'c':
2226 reset_terminal(vc, 1);
2227 return;
2228 case '>': /* Numeric keypad */
2229 clr_kbd(vc, kbdapplic);
2230 return;
2231 case '=': /* Appl. keypad */
2232 set_kbd(vc, kbdapplic);
2233 return;
2234 }
2235 return;
2236 case ESnonstd:
2237 if (c=='P') { /* palette escape sequence */
2238 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2239 vc->vc_par[vc->vc_npar] = 0;
2240 vc->vc_npar = 0;
2241 vc->vc_state = ESpalette;
2242 return;
2243 } else if (c=='R') { /* reset palette */
2244 reset_palette(vc);
2245 vc->vc_state = ESnormal;
63f3a16d
AB
2246 } else if (c>='0' && c<='9')
2247 vc->vc_state = ESosc;
2248 else
1da177e4
LT
2249 vc->vc_state = ESnormal;
2250 return;
2251 case ESpalette:
74c807ce
AS
2252 if (isxdigit(c)) {
2253 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1da177e4
LT
2254 if (vc->vc_npar == 7) {
2255 int i = vc->vc_par[0] * 3, j = 1;
2256 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2257 vc->vc_palette[i++] += vc->vc_par[j++];
2258 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2259 vc->vc_palette[i++] += vc->vc_par[j++];
2260 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2261 vc->vc_palette[i] += vc->vc_par[j];
2262 set_palette(vc);
2263 vc->vc_state = ESnormal;
2264 }
2265 } else
2266 vc->vc_state = ESnormal;
2267 return;
2268 case ESsquare:
2269 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2270 vc->vc_par[vc->vc_npar] = 0;
2271 vc->vc_npar = 0;
2272 vc->vc_state = ESgetpars;
2273 if (c == '[') { /* Function key */
2274 vc->vc_state=ESfunckey;
2275 return;
2276 }
5445447b
MH
2277 switch (c) {
2278 case '?':
2279 vc->vc_priv = EPdec;
2280 return;
2281 case '>':
2282 vc->vc_priv = EPgt;
2283 return;
2284 case '=':
2285 vc->vc_priv = EPeq;
1da177e4 2286 return;
5445447b
MH
2287 case '<':
2288 vc->vc_priv = EPlt;
2289 return;
2290 }
2291 vc->vc_priv = EPecma;
17504fd7 2292 /* fall through */
1da177e4
LT
2293 case ESgetpars:
2294 if (c == ';' && vc->vc_npar < NPAR - 1) {
2295 vc->vc_npar++;
2296 return;
2297 } else if (c>='0' && c<='9') {
2298 vc->vc_par[vc->vc_npar] *= 10;
2299 vc->vc_par[vc->vc_npar] += c - '0';
2300 return;
b290af68 2301 }
04afcd3a 2302 if (c >= 0x20 && c <= 0x3f) { /* 0x2x, 0x3a and 0x3c - 0x3f */
7a99565f
MH
2303 vc->vc_state = EScsiignore;
2304 return;
2305 }
1da177e4
LT
2306 vc->vc_state = ESnormal;
2307 switch(c) {
2308 case 'h':
5445447b
MH
2309 if (vc->vc_priv <= EPdec)
2310 set_mode(vc, 1);
1da177e4
LT
2311 return;
2312 case 'l':
5445447b
MH
2313 if (vc->vc_priv <= EPdec)
2314 set_mode(vc, 0);
1da177e4
LT
2315 return;
2316 case 'c':
2ff5c5a1 2317 if (vc->vc_priv == EPdec) {
1da177e4
LT
2318 if (vc->vc_par[0])
2319 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2320 else
9ea9a886 2321 vc->vc_cursor_type = cur_default;
1da177e4
LT
2322 return;
2323 }
2324 break;
2325 case 'm':
2ff5c5a1 2326 if (vc->vc_priv == EPdec) {
1da177e4
LT
2327 clear_selection();
2328 if (vc->vc_par[0])
2329 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2330 else
2331 vc->vc_complement_mask = vc->vc_s_complement_mask;
2332 return;
2333 }
2334 break;
2335 case 'n':
2ff5c5a1 2336 if (vc->vc_priv == EPecma) {
1da177e4
LT
2337 if (vc->vc_par[0] == 5)
2338 status_report(tty);
2339 else if (vc->vc_par[0] == 6)
2340 cursor_report(vc, tty);
2341 }
2342 return;
2343 }
2ff5c5a1
MH
2344 if (vc->vc_priv != EPecma) {
2345 vc->vc_priv = EPecma;
1da177e4
LT
2346 return;
2347 }
2348 switch(c) {
2349 case 'G': case '`':
2350 if (vc->vc_par[0])
2351 vc->vc_par[0]--;
28bc24fc 2352 gotoxy(vc, vc->vc_par[0], vc->state.y);
1da177e4
LT
2353 return;
2354 case 'A':
2355 if (!vc->vc_par[0])
2356 vc->vc_par[0]++;
28bc24fc 2357 gotoxy(vc, vc->state.x, vc->state.y - vc->vc_par[0]);
1da177e4
LT
2358 return;
2359 case 'B': case 'e':
2360 if (!vc->vc_par[0])
2361 vc->vc_par[0]++;
28bc24fc 2362 gotoxy(vc, vc->state.x, vc->state.y + vc->vc_par[0]);
1da177e4
LT
2363 return;
2364 case 'C': case 'a':
2365 if (!vc->vc_par[0])
2366 vc->vc_par[0]++;
28bc24fc 2367 gotoxy(vc, vc->state.x + vc->vc_par[0], vc->state.y);
1da177e4
LT
2368 return;
2369 case 'D':
2370 if (!vc->vc_par[0])
2371 vc->vc_par[0]++;
28bc24fc 2372 gotoxy(vc, vc->state.x - vc->vc_par[0], vc->state.y);
1da177e4
LT
2373 return;
2374 case 'E':
2375 if (!vc->vc_par[0])
2376 vc->vc_par[0]++;
28bc24fc 2377 gotoxy(vc, 0, vc->state.y + vc->vc_par[0]);
1da177e4
LT
2378 return;
2379 case 'F':
2380 if (!vc->vc_par[0])
2381 vc->vc_par[0]++;
28bc24fc 2382 gotoxy(vc, 0, vc->state.y - vc->vc_par[0]);
1da177e4
LT
2383 return;
2384 case 'd':
2385 if (vc->vc_par[0])
2386 vc->vc_par[0]--;
28bc24fc 2387 gotoxay(vc, vc->state.x ,vc->vc_par[0]);
1da177e4
LT
2388 return;
2389 case 'H': case 'f':
2390 if (vc->vc_par[0])
2391 vc->vc_par[0]--;
2392 if (vc->vc_par[1])
2393 vc->vc_par[1]--;
2394 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2395 return;
2396 case 'J':
2397 csi_J(vc, vc->vc_par[0]);
2398 return;
2399 case 'K':
2400 csi_K(vc, vc->vc_par[0]);
2401 return;
2402 case 'L':
2403 csi_L(vc, vc->vc_par[0]);
2404 return;
2405 case 'M':
2406 csi_M(vc, vc->vc_par[0]);
2407 return;
2408 case 'P':
2409 csi_P(vc, vc->vc_par[0]);
2410 return;
2411 case 'c':
2412 if (!vc->vc_par[0])
2413 respond_ID(tty);
2414 return;
2415 case 'g':
dbee4cff
JS
2416 if (!vc->vc_par[0] && vc->state.x < VC_TABSTOPS_COUNT)
2417 set_bit(vc->state.x, vc->vc_tab_stop);
2418 else if (vc->vc_par[0] == 3)
2419 bitmap_zero(vc->vc_tab_stop, VC_TABSTOPS_COUNT);
1da177e4
LT
2420 return;
2421 case 'm':
2422 csi_m(vc);
2423 return;
2424 case 'q': /* DECLL - but only 3 leds */
2425 /* map 0,1,2,3 to 0,1,2,4 */
2426 if (vc->vc_par[0] < 4)
079c9534 2427 vt_set_led_state(vc->vc_num,
1da177e4
LT
2428 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2429 return;
2430 case 'r':
2431 if (!vc->vc_par[0])
2432 vc->vc_par[0]++;
2433 if (!vc->vc_par[1])
2434 vc->vc_par[1] = vc->vc_rows;
2435 /* Minimum allowed region is 2 lines */
2436 if (vc->vc_par[0] < vc->vc_par[1] &&
2437 vc->vc_par[1] <= vc->vc_rows) {
2438 vc->vc_top = vc->vc_par[0] - 1;
2439 vc->vc_bottom = vc->vc_par[1];
2440 gotoxay(vc, 0, 0);
2441 }
2442 return;
2443 case 's':
2444 save_cur(vc);
2445 return;
2446 case 'u':
2447 restore_cur(vc);
2448 return;
2449 case 'X':
2450 csi_X(vc, vc->vc_par[0]);
2451 return;
2452 case '@':
2453 csi_at(vc, vc->vc_par[0]);
2454 return;
2455 case ']': /* setterm functions */
2456 setterm_command(vc);
2457 return;
2458 }
2459 return;
7a99565f
MH
2460 case EScsiignore:
2461 if (c >= 20 && c <= 0x3f)
2462 return;
2463 vc->vc_state = ESnormal;
2464 return;
1da177e4
LT
2465 case ESpercent:
2466 vc->vc_state = ESnormal;
2467 switch (c) {
2468 case '@': /* defined in ISO 2022 */
2469 vc->vc_utf = 0;
2470 return;
2471 case 'G': /* prelim official escape code */
2472 case '8': /* retained for compatibility */
2473 vc->vc_utf = 1;
2474 return;
2475 }
2476 return;
2477 case ESfunckey:
2478 vc->vc_state = ESnormal;
2479 return;
2480 case EShash:
2481 vc->vc_state = ESnormal;
2482 if (c == '8') {
2483 /* DEC screen alignment test. kludge :-) */
2484 vc->vc_video_erase_char =
2485 (vc->vc_video_erase_char & 0xff00) | 'E';
2486 csi_J(vc, 2);
2487 vc->vc_video_erase_char =
2488 (vc->vc_video_erase_char & 0xff00) | ' ';
2489 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2490 }
2491 return;
2492 case ESsetG0:
b4d92b65 2493 vc_setGx(vc, 0, c);
1da177e4
LT
2494 vc->vc_state = ESnormal;
2495 return;
2496 case ESsetG1:
b4d92b65 2497 vc_setGx(vc, 1, c);
1da177e4
LT
2498 vc->vc_state = ESnormal;
2499 return;
63f3a16d
AB
2500 case ESosc:
2501 return;
1da177e4
LT
2502 default:
2503 vc->vc_state = ESnormal;
2504 }
2505}
2506
2f1a2ccb 2507/* is_double_width() is based on the wcwidth() implementation by
1ed8a2b3 2508 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2f1a2ccb
EK
2509 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2510 */
2511struct interval {
2512 uint32_t first;
2513 uint32_t last;
2514};
2515
f0a8d84f 2516static int ucs_cmp(const void *key, const void *elt)
2f1a2ccb 2517{
f0a8d84f
TM
2518 uint32_t ucs = *(uint32_t *)key;
2519 struct interval e = *(struct interval *) elt;
2f1a2ccb 2520
f0a8d84f
TM
2521 if (ucs > e.last)
2522 return 1;
2523 else if (ucs < e.first)
2524 return -1;
2f1a2ccb
EK
2525 return 0;
2526}
2527
2528static int is_double_width(uint32_t ucs)
2529{
2530 static const struct interval double_width[] = {
2531 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2532 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
1ed8a2b3
EK
2533 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2534 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2f1a2ccb 2535 };
f0a8d84f
TM
2536 if (ucs < double_width[0].first ||
2537 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2538 return 0;
2539
2540 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2541 sizeof(struct interval), ucs_cmp) != NULL;
2f1a2ccb
EK
2542}
2543
917ae1a9
JS
2544struct vc_draw_region {
2545 unsigned long from, to;
2546 int x;
2547};
2548
2549static void con_flush(struct vc_data *vc, struct vc_draw_region *draw)
d711ea8f 2550{
917ae1a9 2551 if (draw->x < 0)
d711ea8f
JS
2552 return;
2553
917ae1a9
JS
2554 vc->vc_sw->con_putcs(vc, (u16 *)draw->from,
2555 (u16 *)draw->to - (u16 *)draw->from, vc->state.y,
2556 draw->x);
2557 draw->x = -1;
d711ea8f
JS
2558}
2559
a018180c
JS
2560static inline int vc_translate_ascii(const struct vc_data *vc, int c)
2561{
2562 if (IS_ENABLED(CONFIG_CONSOLE_TRANSLATIONS)) {
2563 if (vc->vc_toggle_meta)
2564 c |= 0x80;
2565
2566 return vc->vc_translate[c];
2567 }
2568
2569 return c;
2570}
2571
ede98d12
JS
2572
2573/**
2574 * vc_sanitize_unicode -- Replace invalid Unicode code points with U+FFFD
2575 * @c: the received character, or U+FFFD for invalid sequences.
2576 */
2577static inline int vc_sanitize_unicode(const int c)
2578{
2579 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2580 return 0xfffd;
2581
2582 return c;
2583}
2584
694d8a48
JS
2585/**
2586 * vc_translate_unicode -- Combine UTF-8 into Unicode in @vc_utf_char
2587 *
2588 * @vc_utf_char is the being-constructed unicode character.
2589 * @vc_utf_count is the number of continuation bytes still expected to arrive.
2590 * @vc_npar is the number of continuation bytes arrived so far.
2591 */
2592static int vc_translate_unicode(struct vc_data *vc, int c, bool *rescan)
2593{
2594 static const u32 utf8_length_changes[] = {
2595 0x0000007f, 0x000007ff, 0x0000ffff,
2596 0x001fffff, 0x03ffffff, 0x7fffffff
2597 };
2598
ede98d12 2599 /* Continuation byte received */
694d8a48 2600 if ((c & 0xc0) == 0x80) {
ede98d12
JS
2601 /* Unexpected continuation byte? */
2602 if (!vc->vc_utf_count)
694d8a48 2603 return 0xfffd;
ede98d12
JS
2604
2605 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2606 vc->vc_npar++;
2607 if (--vc->vc_utf_count)
2608 goto need_more_bytes;
2609
2610 /* Got a whole character */
2611 c = vc->vc_utf_char;
2612 /* Reject overlong sequences */
2613 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2614 c > utf8_length_changes[vc->vc_npar])
694d8a48 2615 return 0xfffd;
ede98d12
JS
2616
2617 return vc_sanitize_unicode(c);
694d8a48 2618 }
ede98d12
JS
2619
2620 /* Single ASCII byte or first byte of a sequence received */
2621 if (vc->vc_utf_count) {
2622 /* Continuation byte expected */
2623 *rescan = true;
2624 vc->vc_utf_count = 0;
694d8a48 2625 return 0xfffd;
ede98d12 2626 }
694d8a48 2627
ede98d12
JS
2628 /* Nothing to do if an ASCII byte was received */
2629 if (c <= 0x7f)
2630 return c;
2631
2632 /* First byte of a multibyte sequence received */
2633 vc->vc_npar = 0;
2634 if ((c & 0xe0) == 0xc0) {
2635 vc->vc_utf_count = 1;
2636 vc->vc_utf_char = (c & 0x1f);
2637 } else if ((c & 0xf0) == 0xe0) {
2638 vc->vc_utf_count = 2;
2639 vc->vc_utf_char = (c & 0x0f);
2640 } else if ((c & 0xf8) == 0xf0) {
2641 vc->vc_utf_count = 3;
2642 vc->vc_utf_char = (c & 0x07);
2643 } else if ((c & 0xfc) == 0xf8) {
2644 vc->vc_utf_count = 4;
2645 vc->vc_utf_char = (c & 0x03);
2646 } else if ((c & 0xfe) == 0xfc) {
2647 vc->vc_utf_count = 5;
2648 vc->vc_utf_char = (c & 0x01);
2649 } else {
2650 /* 254 and 255 are invalid */
2651 return 0xfffd;
2652 }
2653
2654need_more_bytes:
2655 return -1;
694d8a48
JS
2656}
2657
a4236348
JS
2658static int vc_translate(struct vc_data *vc, int *c, bool *rescan)
2659{
2660 /* Do no translation at all in control states */
2661 if (vc->vc_state != ESnormal)
2662 return *c;
2663
2664 if (vc->vc_utf && !vc->vc_disp_ctrl)
2665 return *c = vc_translate_unicode(vc, *c, rescan);
2666
2667 /* no utf or alternate charset mode */
2668 return vc_translate_ascii(vc, *c);
2669}
2670
881c8783
JS
2671static inline unsigned char vc_invert_attr(const struct vc_data *vc)
2672{
2673 if (!vc->vc_can_do_color)
2674 return vc->vc_attr ^ 0x08;
2675
2676 if (vc->vc_hi_font_mask == 0x100)
2677 return (vc->vc_attr & 0x11) |
2678 ((vc->vc_attr & 0xe0) >> 4) |
2679 ((vc->vc_attr & 0x0e) << 4);
2680
2681 return (vc->vc_attr & 0x88) |
2682 ((vc->vc_attr & 0x70) >> 4) |
2683 ((vc->vc_attr & 0x07) << 4);
2684}
2685
754bda98
JS
2686static bool vc_is_control(struct vc_data *vc, int tc, int c)
2687{
2688 /*
2689 * A bitmap for codes <32. A bit of 1 indicates that the code
2690 * corresponding to that bit number invokes some special action (such
2691 * as cursor movement) and should not be displayed as a glyph unless
2692 * the disp_ctrl mode is explicitly enabled.
2693 */
2694 static const u32 CTRL_ACTION = 0x0d00ff81;
2695 /* Cannot be overridden by disp_ctrl */
2696 static const u32 CTRL_ALWAYS = 0x0800f501;
2697
2698 if (vc->vc_state != ESnormal)
2699 return true;
2700
2701 if (!tc)
2702 return true;
2703
2704 /*
2705 * If the original code was a control character we only allow a glyph
2706 * to be displayed if the code is not normally used (such as for cursor
2707 * movement) or if the disp_ctrl mode has been explicitly enabled.
2708 * Certain characters (as given by the CTRL_ALWAYS bitmap) are always
2709 * displayed as control characters, as the console would be pretty
2710 * useless without them; to display an arbitrary font position use the
2711 * direct-to-font zone in UTF-8 mode.
2712 */
2713 if (c < 32) {
2714 if (vc->vc_disp_ctrl)
2715 return CTRL_ALWAYS & BIT(c);
2716 else
2717 return vc->vc_utf || (CTRL_ACTION & BIT(c));
2718 }
2719
2720 if (c == 127 && !vc->vc_disp_ctrl)
2721 return true;
2722
2723 if (c == 128 + 27)
2724 return true;
2725
2726 return false;
2727}
2728
ac751efa 2729/* acquires console_lock */
1da177e4
LT
2730static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2731{
917ae1a9
JS
2732 struct vc_draw_region draw = {
2733 .x = -1,
2734 };
754bda98 2735 int c, next_c, tc, n = 0;
1da177e4 2736 unsigned int currcons;
1da177e4 2737 struct vc_data *vc;
2f1a2ccb 2738 unsigned char vc_attr;
0341a4d0 2739 struct vt_notifier_param param;
1da177e4 2740 u16 himask, charmask;
da823b2d
JS
2741 u8 width;
2742 bool rescan;
2743 bool inverse;
1da177e4
LT
2744
2745 if (in_interrupt())
2746 return count;
2747
ac751efa 2748 console_lock();
1da177e4
LT
2749 vc = tty->driver_data;
2750 if (vc == NULL) {
bccf1da3 2751 pr_err("vt: argh, driver_data is NULL !\n");
ac751efa 2752 console_unlock();
1da177e4
LT
2753 return 0;
2754 }
2755
2756 currcons = vc->vc_num;
2757 if (!vc_cons_allocated(currcons)) {
1ffdda95
MSB
2758 /* could this happen? */
2759 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2760 console_unlock();
2761 return 0;
1da177e4 2762 }
1da177e4 2763
1da177e4
LT
2764 himask = vc->vc_hi_font_mask;
2765 charmask = himask ? 0x1ff : 0xff;
2766
2767 /* undraw cursor first */
6ca8dfd7 2768 if (con_is_fg(vc))
1da177e4
LT
2769 hide_cursor(vc);
2770
0341a4d0
KD
2771 param.vc = vc;
2772
1da177e4
LT
2773 while (!tty->stopped && count) {
2774 int orig = *buf;
1da177e4
LT
2775 buf++;
2776 n++;
2777 count--;
0d1ffef5
JS
2778rescan_last_byte:
2779 c = orig;
da823b2d
JS
2780 rescan = false;
2781 inverse = false;
2f1a2ccb 2782 width = 1;
1da177e4 2783
a4236348
JS
2784 tc = vc_translate(vc, &c, &rescan);
2785 if (tc == -1)
2786 continue;
1da177e4 2787
0341a4d0
KD
2788 param.c = tc;
2789 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2790 &param) == NOTIFY_STOP)
2791 continue;
2792
754bda98 2793 if (!vc_is_control(vc, tc, c)) {
2f1a2ccb
EK
2794 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2795 if (is_double_width(c))
2796 width = 2;
2797 }
1da177e4
LT
2798 /* Now try to find out how to display it */
2799 tc = conv_uni_to_pc(vc, tc);
d4328b40 2800 if (tc & ~charmask) {
2f1a2ccb
EK
2801 if (tc == -1 || tc == -2) {
2802 continue; /* nothing to display */
2803 }
2804 /* Glyph not found */
c0b79882 2805 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2f1a2ccb 2806 /* In legacy mode use the glyph we get by a 1:1 mapping.
1ed8a2b3
EK
2807 This would make absolutely no sense with Unicode in mind,
2808 but do this for ASCII characters since a font may lack
2809 Unicode mapping info and we don't want to end up with
2810 having question marks only. */
2f1a2ccb
EK
2811 tc = c;
2812 } else {
2813 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2814 tc = conv_uni_to_pc(vc, 0xfffd);
2815 if (tc < 0) {
da823b2d 2816 inverse = true;
2f1a2ccb
EK
2817 tc = conv_uni_to_pc(vc, '?');
2818 if (tc < 0) tc = '?';
2819 }
2820 }
d4328b40 2821 }
1da177e4 2822
2f1a2ccb
EK
2823 if (!inverse) {
2824 vc_attr = vc->vc_attr;
1da177e4 2825 } else {
881c8783 2826 vc_attr = vc_invert_attr(vc);
917ae1a9 2827 con_flush(vc, &draw);
1da177e4 2828 }
2f1a2ccb 2829
d8ae7242 2830 next_c = c;
2f1a2ccb
EK
2831 while (1) {
2832 if (vc->vc_need_wrap || vc->vc_decim)
917ae1a9 2833 con_flush(vc, &draw);
2f1a2ccb
EK
2834 if (vc->vc_need_wrap) {
2835 cr(vc);
2836 lf(vc);
2837 }
2838 if (vc->vc_decim)
2839 insert_char(vc, 1);
d8ae7242 2840 vc_uniscr_putc(vc, next_c);
2f1a2ccb
EK
2841 scr_writew(himask ?
2842 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2843 (vc_attr << 8) + tc,
2844 (u16 *) vc->vc_pos);
917ae1a9
JS
2845 if (con_should_update(vc) && draw.x < 0) {
2846 draw.x = vc->state.x;
2847 draw.from = vc->vc_pos;
2f1a2ccb 2848 }
28bc24fc 2849 if (vc->state.x == vc->vc_cols - 1) {
2f1a2ccb 2850 vc->vc_need_wrap = vc->vc_decawm;
917ae1a9 2851 draw.to = vc->vc_pos + 2;
2f1a2ccb 2852 } else {
28bc24fc 2853 vc->state.x++;
917ae1a9 2854 draw.to = (vc->vc_pos += 2);
d4328b40 2855 }
2f1a2ccb
EK
2856
2857 if (!--width) break;
2858
2859 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2860 if (tc < 0) tc = ' ';
d8ae7242 2861 next_c = ' ';
2f1a2ccb 2862 }
b293d758 2863 notify_write(vc, c);
2f1a2ccb 2864
d711ea8f 2865 if (inverse)
917ae1a9 2866 con_flush(vc, &draw);
1ed8a2b3 2867
0d1ffef5 2868 if (rescan)
d4328b40 2869 goto rescan_last_byte;
0d1ffef5 2870
1da177e4
LT
2871 continue;
2872 }
917ae1a9 2873 con_flush(vc, &draw);
1da177e4
LT
2874 do_con_trol(tty, vc, orig);
2875 }
917ae1a9 2876 con_flush(vc, &draw);
0224080f 2877 vc_uniscr_debug_check(vc);
1da177e4 2878 console_conditional_schedule();
b293d758 2879 notify_update(vc);
7e1d2263 2880 console_unlock();
1da177e4 2881 return n;
1da177e4
LT
2882}
2883
2884/*
2885 * This is the console switching callback.
2886 *
2887 * Doing console switching in a process context allows
2888 * us to do the switches asynchronously (needed when we want
2889 * to switch due to a keyboard interrupt). Synchronization
2890 * with other console code and prevention of re-entrancy is
ac751efa 2891 * ensured with console_lock.
1da177e4 2892 */
65f27f38 2893static void console_callback(struct work_struct *ignored)
1da177e4 2894{
ac751efa 2895 console_lock();
1da177e4
LT
2896
2897 if (want_console >= 0) {
2898 if (want_console != fg_console &&
2899 vc_cons_allocated(want_console)) {
2900 hide_cursor(vc_cons[fg_console].d);
2901 change_console(vc_cons[want_console].d);
2902 /* we only changed when the console had already
2903 been allocated - a new console is not created
2904 in an interrupt routine */
2905 }
2906 want_console = -1;
2907 }
2908 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2909 do_poke_blanked_console = 0;
2910 poke_blanked_console();
2911 }
2912 if (scrollback_delta) {
2913 struct vc_data *vc = vc_cons[fg_console].d;
2914 clear_selection();
97293de9 2915 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
1da177e4
LT
2916 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2917 scrollback_delta = 0;
2918 }
2919 if (blank_timer_expired) {
2920 do_blank_screen(0);
2921 blank_timer_expired = 0;
2922 }
b293d758 2923 notify_update(vc_cons[fg_console].d);
1da177e4 2924
ac751efa 2925 console_unlock();
1da177e4
LT
2926}
2927
b257bc05 2928int set_console(int nr)
1da177e4 2929{
b257bc05
AJ
2930 struct vc_data *vc = vc_cons[fg_console].d;
2931
2932 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2933 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2934
2935 /*
2936 * Console switch will fail in console_callback() or
2937 * change_console() so there is no point scheduling
2938 * the callback
2939 *
2940 * Existing set_console() users don't check the return
2941 * value so this shouldn't break anything
2942 */
2943 return -EINVAL;
2944 }
2945
1da177e4
LT
2946 want_console = nr;
2947 schedule_console_callback();
b257bc05
AJ
2948
2949 return 0;
1da177e4
LT
2950}
2951
2952struct tty_driver *console_driver;
2953
2954#ifdef CONFIG_VT_CONSOLE
2955
5ada918b
BW
2956/**
2957 * vt_kmsg_redirect() - Sets/gets the kernel message console
2958 * @new: The new virtual terminal number or -1 if the console should stay
2959 * unchanged
2960 *
2961 * By default, the kernel messages are always printed on the current virtual
2962 * console. However, the user may modify that default with the
2963 * TIOCL_SETKMSGREDIRECT ioctl call.
2964 *
2965 * This function sets the kernel message console to be @new. It returns the old
2966 * virtual console number. The virtual terminal number 0 (both as parameter and
2967 * return value) means no redirection (i.e. always printed on the currently
2968 * active console).
2969 *
2970 * The parameter -1 means that only the current console is returned, but the
2971 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2972 * case to make the code more understandable.
2973 *
2974 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2975 * the parameter and always returns 0.
2976 */
2977int vt_kmsg_redirect(int new)
2978{
2979 static int kmsg_con;
2980
2981 if (new != -1)
2982 return xchg(&kmsg_con, new);
2983 else
2984 return kmsg_con;
2985}
2986
1da177e4
LT
2987/*
2988 * Console on virtual terminal
2989 *
2990 * The console must be locked when we get here.
2991 */
2992
2993static void vt_console_print(struct console *co, const char *b, unsigned count)
2994{
2995 struct vc_data *vc = vc_cons[fg_console].d;
2996 unsigned char c;
b0940003 2997 static DEFINE_SPINLOCK(printing_lock);
1da177e4 2998 const ushort *start;
6609cff6 2999 ushort start_x, cnt;
5ada918b 3000 int kmsg_console;
1da177e4
LT
3001
3002 /* console busy or not yet initialized */
b0940003
NP
3003 if (!printable)
3004 return;
3005 if (!spin_trylock(&printing_lock))
1da177e4
LT
3006 return;
3007
5ada918b
BW
3008 kmsg_console = vt_get_kmsg_redirect();
3009 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
3010 vc = vc_cons[kmsg_console - 1].d;
1da177e4 3011
1da177e4
LT
3012 if (!vc_cons_allocated(fg_console)) {
3013 /* impossible */
3014 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
3015 goto quit;
3016 }
3017
8d7fc299 3018 if (vc->vc_mode != KD_TEXT)
1da177e4
LT
3019 goto quit;
3020
3021 /* undraw cursor first */
6ca8dfd7 3022 if (con_is_fg(vc))
1da177e4
LT
3023 hide_cursor(vc);
3024
3025 start = (ushort *)vc->vc_pos;
28bc24fc 3026 start_x = vc->state.x;
6609cff6 3027 cnt = 0;
1da177e4
LT
3028 while (count--) {
3029 c = *b++;
3030 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
6609cff6 3031 if (cnt && con_is_visible(vc))
28bc24fc 3032 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
6609cff6 3033 cnt = 0;
1da177e4
LT
3034 if (c == 8) { /* backspace */
3035 bs(vc);
3036 start = (ushort *)vc->vc_pos;
28bc24fc 3037 start_x = vc->state.x;
1da177e4
LT
3038 continue;
3039 }
3040 if (c != 13)
3041 lf(vc);
3042 cr(vc);
3043 start = (ushort *)vc->vc_pos;
28bc24fc 3044 start_x = vc->state.x;
1da177e4
LT
3045 if (c == 10 || c == 13)
3046 continue;
3047 }
6609cff6 3048 vc_uniscr_putc(vc, c);
1da177e4 3049 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
b293d758 3050 notify_write(vc, c);
1da177e4 3051 cnt++;
28bc24fc 3052 if (vc->state.x == vc->vc_cols - 1) {
1da177e4 3053 vc->vc_need_wrap = 1;
6609cff6
NP
3054 } else {
3055 vc->vc_pos += 2;
28bc24fc 3056 vc->state.x++;
1da177e4
LT
3057 }
3058 }
6609cff6 3059 if (cnt && con_is_visible(vc))
28bc24fc 3060 vc->vc_sw->con_putcs(vc, start, cnt, vc->state.y, start_x);
1da177e4 3061 set_cursor(vc);
b293d758 3062 notify_update(vc);
1da177e4
LT
3063
3064quit:
b0940003 3065 spin_unlock(&printing_lock);
1da177e4
LT
3066}
3067
3068static struct tty_driver *vt_console_device(struct console *c, int *index)
3069{
3070 *index = c->index ? c->index-1 : fg_console;
3071 return console_driver;
3072}
3073
3074static struct console vt_console_driver = {
3075 .name = "tty",
3076 .write = vt_console_print,
3077 .device = vt_console_device,
3078 .unblank = unblank_screen,
3079 .flags = CON_PRINTBUFFER,
3080 .index = -1,
3081};
3082#endif
3083
3084/*
3085 * Handling of Linux-specific VC ioctls
3086 */
3087
3088/*
ac751efa 3089 * Generally a bit racy with respect to console_lock();.
1da177e4
LT
3090 *
3091 * There are some functions which don't need it.
3092 *
3093 * There are some functions which can sleep for arbitrary periods
3094 * (paste_selection) but we don't need the lock there anyway.
3095 *
496124e5 3096 * set_selection_user has locking, and definitely needs it
1da177e4
LT
3097 */
3098
3099int tioclinux(struct tty_struct *tty, unsigned long arg)
3100{
3101 char type, data;
3102 char __user *p = (char __user *)arg;
3103 int lines;
3104 int ret;
3105
1da177e4
LT
3106 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
3107 return -EPERM;
3108 if (get_user(type, p))
3109 return -EFAULT;
3110 ret = 0;
04f378b1 3111
1da177e4
LT
3112 switch (type)
3113 {
3114 case TIOCL_SETSEL:
496124e5
OK
3115 ret = set_selection_user((struct tiocl_selection
3116 __user *)(p+1), tty);
1da177e4
LT
3117 break;
3118 case TIOCL_PASTESEL:
3119 ret = paste_selection(tty);
3120 break;
3121 case TIOCL_UNBLANKSCREEN:
ac751efa 3122 console_lock();
1da177e4 3123 unblank_screen();
ac751efa 3124 console_unlock();
1da177e4
LT
3125 break;
3126 case TIOCL_SELLOADLUT:
5289475d 3127 console_lock();
1da177e4 3128 ret = sel_loadlut(p);
5289475d 3129 console_unlock();
1da177e4
LT
3130 break;
3131 case TIOCL_GETSHIFTSTATE:
04f378b1 3132
1da177e4
LT
3133 /*
3134 * Make it possible to react to Shift+Mousebutton.
3135 * Note that 'shift_state' is an undocumented
3136 * kernel-internal variable; programs not closely
3137 * related to the kernel should not use this.
3138 */
079c9534 3139 data = vt_get_shift_state();
6987dc8a 3140 ret = put_user(data, p);
1da177e4
LT
3141 break;
3142 case TIOCL_GETMOUSEREPORTING:
20f62579 3143 console_lock(); /* May be overkill */
1da177e4 3144 data = mouse_reporting();
20f62579 3145 console_unlock();
6987dc8a 3146 ret = put_user(data, p);
1da177e4
LT
3147 break;
3148 case TIOCL_SETVESABLANK:
20f62579 3149 console_lock();
403aac96 3150 ret = set_vesa_blanking(p);
20f62579 3151 console_unlock();
1da177e4 3152 break;
0ca07731 3153 case TIOCL_GETKMSGREDIRECT:
5ada918b 3154 data = vt_get_kmsg_redirect();
6987dc8a 3155 ret = put_user(data, p);
0ca07731 3156 break;
1da177e4
LT
3157 case TIOCL_SETKMSGREDIRECT:
3158 if (!capable(CAP_SYS_ADMIN)) {
3159 ret = -EPERM;
3160 } else {
3161 if (get_user(data, p+1))
3162 ret = -EFAULT;
3163 else
5ada918b 3164 vt_kmsg_redirect(data);
1da177e4
LT
3165 }
3166 break;
3167 case TIOCL_GETFGCONSOLE:
20f62579
AC
3168 /* No locking needed as this is a transiently
3169 correct return anyway if the caller hasn't
3170 disabled switching */
1da177e4
LT
3171 ret = fg_console;
3172 break;
3173 case TIOCL_SCROLLCONSOLE:
3174 if (get_user(lines, (s32 __user *)(p+4))) {
3175 ret = -EFAULT;
3176 } else {
20f62579
AC
3177 /* Need the console lock here. Note that lots
3178 of other calls need fixing before the lock
3179 is actually useful ! */
3180 console_lock();
1da177e4 3181 scrollfront(vc_cons[fg_console].d, lines);
20f62579 3182 console_unlock();
1da177e4
LT
3183 ret = 0;
3184 }
3185 break;
3186 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
ac751efa 3187 console_lock();
1da177e4
LT
3188 ignore_poke = 1;
3189 do_blank_screen(0);
ac751efa 3190 console_unlock();
1da177e4
LT
3191 break;
3192 case TIOCL_BLANKEDSCREEN:
3193 ret = console_blanked;
3194 break;
3195 default:
3196 ret = -EINVAL;
3197 break;
3198 }
3199 return ret;
3200}
3201
3202/*
3203 * /dev/ttyN handling
3204 */
3205
3206static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
3207{
3208 int retval;
3209
3210 retval = do_con_write(tty, buf, count);
3211 con_flush_chars(tty);
3212
3213 return retval;
3214}
3215
5d19f546 3216static int con_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
3217{
3218 if (in_interrupt())
5d19f546
AC
3219 return 0; /* n_r3964 calls put_char() from interrupt context */
3220 return do_con_write(tty, &ch, 1);
1da177e4
LT
3221}
3222
3223static int con_write_room(struct tty_struct *tty)
3224{
3225 if (tty->stopped)
3226 return 0;
a88a69c9 3227 return 32768; /* No limit, really; we're not buffering */
1da177e4
LT
3228}
3229
3230static int con_chars_in_buffer(struct tty_struct *tty)
3231{
3232 return 0; /* we're not buffering */
3233}
3234
3235/*
3236 * con_throttle and con_unthrottle are only used for
3237 * paste_selection(), which has to stuff in a large number of
3238 * characters...
3239 */
3240static void con_throttle(struct tty_struct *tty)
3241{
3242}
3243
3244static void con_unthrottle(struct tty_struct *tty)
3245{
3246 struct vc_data *vc = tty->driver_data;
3247
3248 wake_up_interruptible(&vc->paste_wait);
3249}
3250
3251/*
3252 * Turn the Scroll-Lock LED on when the tty is stopped
3253 */
3254static void con_stop(struct tty_struct *tty)
3255{
3256 int console_num;
3257 if (!tty)
3258 return;
3259 console_num = tty->index;
3260 if (!vc_cons_allocated(console_num))
3261 return;
079c9534 3262 vt_kbd_con_stop(console_num);
1da177e4
LT
3263}
3264
3265/*
3266 * Turn the Scroll-Lock LED off when the console is started
3267 */
3268static void con_start(struct tty_struct *tty)
3269{
3270 int console_num;
3271 if (!tty)
3272 return;
3273 console_num = tty->index;
3274 if (!vc_cons_allocated(console_num))
3275 return;
079c9534 3276 vt_kbd_con_start(console_num);
1da177e4
LT
3277}
3278
3279static void con_flush_chars(struct tty_struct *tty)
3280{
3281 struct vc_data *vc;
3282
3283 if (in_interrupt()) /* from flush_to_ldisc */
3284 return;
3285
3286 /* if we race with con_close(), vt may be null */
ac751efa 3287 console_lock();
1da177e4
LT
3288 vc = tty->driver_data;
3289 if (vc)
3290 set_cursor(vc);
ac751efa 3291 console_unlock();
1da177e4
LT
3292}
3293
3294/*
3295 * Allocate the console screen memory.
3296 */
bc1e99d9 3297static int con_install(struct tty_driver *driver, struct tty_struct *tty)
1da177e4
LT
3298{
3299 unsigned int currcons = tty->index;
bc1e99d9
JS
3300 struct vc_data *vc;
3301 int ret;
1da177e4 3302
ac751efa 3303 console_lock();
bc1e99d9
JS
3304 ret = vc_allocate(currcons);
3305 if (ret)
3306 goto unlock;
feebed65 3307
bc1e99d9 3308 vc = vc_cons[currcons].d;
1da177e4 3309
bc1e99d9
JS
3310 /* Still being freed */
3311 if (vc->port.tty) {
3312 ret = -ERESTARTSYS;
3313 goto unlock;
3314 }
3315
3316 ret = tty_port_install(&vc->port, driver, tty);
3317 if (ret)
3318 goto unlock;
3319
3320 tty->driver_data = vc;
3321 vc->port.tty = tty;
ca4463bf 3322 tty_port_get(&vc->port);
bc1e99d9
JS
3323
3324 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3325 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3326 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
1da177e4 3327 }
bc1e99d9 3328 if (vc->vc_utf)
adc8d746 3329 tty->termios.c_iflag |= IUTF8;
bc1e99d9 3330 else
adc8d746 3331 tty->termios.c_iflag &= ~IUTF8;
bc1e99d9 3332unlock:
ac751efa 3333 console_unlock();
1da177e4
LT
3334 return ret;
3335}
3336
bc1e99d9
JS
3337static int con_open(struct tty_struct *tty, struct file *filp)
3338{
3339 /* everything done in install */
3340 return 0;
3341}
3342
3343
1da177e4
LT
3344static void con_close(struct tty_struct *tty, struct file *filp)
3345{
feebed65
AC
3346 /* Nothing to do - we defer to shutdown */
3347}
1da177e4 3348
feebed65
AC
3349static void con_shutdown(struct tty_struct *tty)
3350{
3351 struct vc_data *vc = tty->driver_data;
3352 BUG_ON(vc == NULL);
ac751efa 3353 console_lock();
8ce73264 3354 vc->port.tty = NULL;
ac751efa 3355 console_unlock();
1da177e4
LT
3356}
3357
ca4463bf
EB
3358static void con_cleanup(struct tty_struct *tty)
3359{
3360 struct vc_data *vc = tty->driver_data;
3361
3362 tty_port_put(&vc->port);
3363}
3364
3855ae1c 3365static int default_color = 7; /* white */
fa6ce9ab
JE
3366static int default_italic_color = 2; // green (ASCII)
3367static int default_underline_color = 3; // cyan (ASCII)
3855ae1c 3368module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
fa6ce9ab
JE
3369module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3370module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3371
1da177e4
LT
3372static void vc_init(struct vc_data *vc, unsigned int rows,
3373 unsigned int cols, int do_clear)
3374{
3375 int j, k ;
3376
3377 vc->vc_cols = cols;
3378 vc->vc_rows = rows;
3379 vc->vc_size_row = cols << 1;
3380 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3381
3382 set_origin(vc);
3383 vc->vc_pos = vc->vc_origin;
3384 reset_vc(vc);
3385 for (j=k=0; j<16; j++) {
3386 vc->vc_palette[k++] = default_red[j] ;
3387 vc->vc_palette[k++] = default_grn[j] ;
3388 vc->vc_palette[k++] = default_blu[j] ;
3389 }
3855ae1c 3390 vc->vc_def_color = default_color;
fa6ce9ab
JE
3391 vc->vc_ulcolor = default_underline_color;
3392 vc->vc_itcolor = default_italic_color;
1da177e4
LT
3393 vc->vc_halfcolor = 0x08; /* grey */
3394 init_waitqueue_head(&vc->paste_wait);
3395 reset_terminal(vc, do_clear);
3396}
3397
3398/*
3399 * This routine initializes console interrupts, and does nothing
3400 * else. If you want the screen to clear, call tty_write with
3401 * the appropriate escape-sequence.
3402 */
3403
3404static int __init con_init(void)
3405{
3406 const char *display_desc = NULL;
3407 struct vc_data *vc;
3e795de7 3408 unsigned int currcons = 0, i;
1da177e4 3409
ac751efa 3410 console_lock();
1da177e4 3411
805ece2a
AS
3412 if (!conswitchp)
3413 conswitchp = &dummy_con;
3414 display_desc = conswitchp->con_startup();
1da177e4
LT
3415 if (!display_desc) {
3416 fg_console = 0;
ac751efa 3417 console_unlock();
1da177e4
LT
3418 return 0;
3419 }
3420
3e795de7
AD
3421 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3422 struct con_driver *con_driver = &registered_con_driver[i];
3423
3424 if (con_driver->con == NULL) {
3425 con_driver->con = conswitchp;
3426 con_driver->desc = display_desc;
3427 con_driver->flag = CON_DRIVER_FLAG_INIT;
3428 con_driver->first = 0;
3429 con_driver->last = MAX_NR_CONSOLES - 1;
3430 break;
3431 }
3432 }
3433
3434 for (i = 0; i < MAX_NR_CONSOLES; i++)
3435 con_driver_map[i] = conswitchp;
3436
1da177e4
LT
3437 if (blankinterval) {
3438 blank_state = blank_normal_wait;
f324edc8 3439 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
3440 }
3441
1da177e4 3442 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
a5f4f52e 3443 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
7f1f86a0 3444 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
ff917ba4 3445 tty_port_init(&vc->port);
1da177e4 3446 visual_init(vc, currcons, 1);
a5f4f52e 3447 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
1da177e4
LT
3448 vc_init(vc, vc->vc_rows, vc->vc_cols,
3449 currcons || !vc->vc_sw->con_save_screen);
3450 }
3451 currcons = fg_console = 0;
3452 master_display_fg = vc = vc_cons[currcons].d;
3453 set_origin(vc);
3454 save_screen(vc);
28bc24fc 3455 gotoxy(vc, vc->state.x, vc->state.y);
1da177e4
LT
3456 csi_J(vc, 0);
3457 update_screen(vc);
5da527aa 3458 pr_info("Console: %s %s %dx%d\n",
1da177e4
LT
3459 vc->vc_can_do_color ? "colour" : "mono",
3460 display_desc, vc->vc_cols, vc->vc_rows);
3461 printable = 1;
1da177e4 3462
ac751efa 3463 console_unlock();
1da177e4
LT
3464
3465#ifdef CONFIG_VT_CONSOLE
3466 register_console(&vt_console_driver);
3467#endif
3468 return 0;
3469}
3470console_initcall(con_init);
3471
b68e31d0 3472static const struct tty_operations con_ops = {
bc1e99d9 3473 .install = con_install,
1da177e4
LT
3474 .open = con_open,
3475 .close = con_close,
3476 .write = con_write,
3477 .write_room = con_write_room,
3478 .put_char = con_put_char,
3479 .flush_chars = con_flush_chars,
3480 .chars_in_buffer = con_chars_in_buffer,
3481 .ioctl = vt_ioctl,
e9216651
AB
3482#ifdef CONFIG_COMPAT
3483 .compat_ioctl = vt_compat_ioctl,
3484#endif
1da177e4
LT
3485 .stop = con_stop,
3486 .start = con_start,
3487 .throttle = con_throttle,
3488 .unthrottle = con_unthrottle,
8c9a9dd0 3489 .resize = vt_resize,
ca4463bf
EB
3490 .shutdown = con_shutdown,
3491 .cleanup = con_cleanup,
1da177e4
LT
3492};
3493
d81ed103
AC
3494static struct cdev vc0_cdev;
3495
fbc92a34
KS
3496static ssize_t show_tty_active(struct device *dev,
3497 struct device_attribute *attr, char *buf)
3498{
3499 return sprintf(buf, "tty%d\n", fg_console + 1);
3500}
3501static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3502
1083a7be
TI
3503static struct attribute *vt_dev_attrs[] = {
3504 &dev_attr_active.attr,
3505 NULL
3506};
3507
3508ATTRIBUTE_GROUPS(vt_dev);
3509
d81ed103 3510int __init vty_init(const struct file_operations *console_fops)
1da177e4 3511{
d81ed103
AC
3512 cdev_init(&vc0_cdev, console_fops);
3513 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3514 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3515 panic("Couldn't register /dev/tty0 driver\n");
1083a7be
TI
3516 tty0dev = device_create_with_groups(tty_class, NULL,
3517 MKDEV(TTY_MAJOR, 0), NULL,
3518 vt_dev_groups, "tty0");
fbc92a34
KS
3519 if (IS_ERR(tty0dev))
3520 tty0dev = NULL;
d81ed103 3521
1da177e4
LT
3522 vcs_init();
3523
3524 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3525 if (!console_driver)
3526 panic("Couldn't allocate console driver\n");
2f16669d 3527
1da177e4
LT
3528 console_driver->name = "tty";
3529 console_driver->name_base = 1;
3530 console_driver->major = TTY_MAJOR;
3531 console_driver->minor_start = 1;
3532 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3533 console_driver->init_termios = tty_std_termios;
c1236d31
ST
3534 if (default_utf8)
3535 console_driver->init_termios.c_iflag |= IUTF8;
1da177e4
LT
3536 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3537 tty_set_operations(console_driver, &con_ops);
3538 if (tty_register_driver(console_driver))
3539 panic("Couldn't register console driver\n");
1da177e4
LT
3540 kbd_init();
3541 console_map_init();
1da177e4
LT
3542#ifdef CONFIG_MDA_CONSOLE
3543 mda_console_init();
3544#endif
3545 return 0;
3546}
3547
3548#ifndef VT_SINGLE_DRIVER
6db4063c
AD
3549
3550static struct class *vtconsole_class;
3551
50e244cc 3552static int do_bind_con_driver(const struct consw *csw, int first, int last,
b7269dd2 3553 int deflt)
1da177e4 3554{
3e795de7
AD
3555 struct module *owner = csw->owner;
3556 const char *desc = NULL;
3557 struct con_driver *con_driver;
3558 int i, j = -1, k = -1, retval = -ENODEV;
1da177e4 3559
1da177e4
LT
3560 if (!try_module_get(owner))
3561 return -ENODEV;
3562
50e244cc 3563 WARN_CONSOLE_UNLOCKED();
1c8ce271 3564
3e795de7
AD
3565 /* check if driver is registered */
3566 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3567 con_driver = &registered_con_driver[i];
3568
3569 if (con_driver->con == csw) {
3570 desc = con_driver->desc;
3571 retval = 0;
3572 break;
3573 }
3574 }
3575
3576 if (retval)
3577 goto err;
3578
3579 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3580 csw->con_startup();
3581 con_driver->flag |= CON_DRIVER_FLAG_INIT;
1da177e4 3582 }
1c8ce271 3583
1da177e4
LT
3584 if (deflt) {
3585 if (conswitchp)
3586 module_put(conswitchp->owner);
1c8ce271 3587
1da177e4
LT
3588 __module_get(owner);
3589 conswitchp = csw;
3590 }
3591
3e795de7
AD
3592 first = max(first, con_driver->first);
3593 last = min(last, con_driver->last);
3594
1da177e4
LT
3595 for (i = first; i <= last; i++) {
3596 int old_was_color;
3597 struct vc_data *vc = vc_cons[i].d;
3598
3599 if (con_driver_map[i])
3600 module_put(con_driver_map[i]->owner);
3601 __module_get(owner);
3602 con_driver_map[i] = csw;
3603
3604 if (!vc || !vc->vc_sw)
3605 continue;
3606
3607 j = i;
1c8ce271 3608
6ca8dfd7 3609 if (con_is_visible(vc)) {
4ee1acce 3610 k = i;
1da177e4 3611 save_screen(vc);
4ee1acce
DH
3612 }
3613
1da177e4
LT
3614 old_was_color = vc->vc_can_do_color;
3615 vc->vc_sw->con_deinit(vc);
9fc2b2d0 3616 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
1da177e4 3617 visual_init(vc, i, 0);
1c8ce271 3618 set_origin(vc);
1da177e4
LT
3619 update_attr(vc);
3620
3621 /* If the console changed between mono <-> color, then
3622 * the attributes in the screenbuf will be wrong. The
3623 * following resets all attributes to something sane.
3624 */
3625 if (old_was_color != vc->vc_can_do_color)
3626 clear_buffer_attributes(vc);
1da177e4 3627 }
4ee1acce 3628
1ffdda95 3629 pr_info("Console: switching ");
1da177e4 3630 if (!deflt)
bccf1da3 3631 pr_cont("consoles %d-%d ", first + 1, last + 1);
4ee1acce
DH
3632 if (j >= 0) {
3633 struct vc_data *vc = vc_cons[j].d;
3634
bccf1da3
JP
3635 pr_cont("to %s %s %dx%d\n",
3636 vc->vc_can_do_color ? "colour" : "mono",
3637 desc, vc->vc_cols, vc->vc_rows);
4ee1acce
DH
3638
3639 if (k >= 0) {
3640 vc = vc_cons[k].d;
3641 update_screen(vc);
3642 }
bccf1da3
JP
3643 } else {
3644 pr_cont("to %s\n", desc);
3645 }
1da177e4 3646
3e795de7
AD
3647 retval = 0;
3648err:
1da177e4 3649 module_put(owner);
3e795de7
AD
3650 return retval;
3651};
1da177e4 3652
50e244cc 3653
13ae6645 3654#ifdef CONFIG_VT_HW_CONSOLE_BINDING
e93a9a86 3655int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
1da177e4 3656{
3e795de7
AD
3657 struct module *owner = csw->owner;
3658 const struct consw *defcsw = NULL;
3659 struct con_driver *con_driver = NULL, *con_back = NULL;
3660 int i, retval = -ENODEV;
1da177e4 3661
3e795de7
AD
3662 if (!try_module_get(owner))
3663 return -ENODEV;
3664
e93a9a86 3665 WARN_CONSOLE_UNLOCKED();
3e795de7
AD
3666
3667 /* check if driver is registered and if it is unbindable */
3668 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3669 con_driver = &registered_con_driver[i];
3670
3671 if (con_driver->con == csw &&
6db4063c 3672 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3e795de7
AD
3673 retval = 0;
3674 break;
3675 }
3676 }
3677
e93a9a86 3678 if (retval)
3e795de7 3679 goto err;
3e795de7 3680
6db4063c
AD
3681 retval = -ENODEV;
3682
3e795de7
AD
3683 /* check if backup driver exists */
3684 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3685 con_back = &registered_con_driver[i];
3686
249f7b3e 3687 if (con_back->con && con_back->con != csw) {
3e795de7
AD
3688 defcsw = con_back->con;
3689 retval = 0;
3690 break;
3691 }
3692 }
3693
e93a9a86 3694 if (retval)
3e795de7 3695 goto err;
3e795de7 3696
e93a9a86 3697 if (!con_is_bound(csw))
3e795de7 3698 goto err;
3e795de7
AD
3699
3700 first = max(first, con_driver->first);
3701 last = min(last, con_driver->last);
3702
3703 for (i = first; i <= last; i++) {
1da177e4
LT
3704 if (con_driver_map[i] == csw) {
3705 module_put(csw->owner);
3706 con_driver_map[i] = NULL;
3707 }
3e795de7
AD
3708 }
3709
3710 if (!con_is_bound(defcsw)) {
6db4063c
AD
3711 const struct consw *defconsw = conswitchp;
3712
3e795de7
AD
3713 defcsw->con_startup();
3714 con_back->flag |= CON_DRIVER_FLAG_INIT;
6db4063c
AD
3715 /*
3716 * vgacon may change the default driver to point
3717 * to dummycon, we restore it here...
3718 */
3719 conswitchp = defconsw;
3e795de7
AD
3720 }
3721
3722 if (!con_is_bound(csw))
3723 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3724
6db4063c 3725 /* ignore return value, binding should not fail */
50e244cc 3726 do_bind_con_driver(defcsw, first, last, deflt);
3e795de7
AD
3727err:
3728 module_put(owner);
3729 return retval;
3730
3731}
e93a9a86 3732EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3e795de7 3733
6db4063c
AD
3734static int vt_bind(struct con_driver *con)
3735{
3736 const struct consw *defcsw = NULL, *csw = NULL;
3737 int i, more = 1, first = -1, last = -1, deflt = 0;
3738
77232f79 3739 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3740 goto err;
3741
3742 csw = con->con;
3743
3744 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3745 struct con_driver *con = &registered_con_driver[i];
3746
3747 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3748 defcsw = con->con;
3749 break;
3750 }
3751 }
3752
3753 if (!defcsw)
3754 goto err;
3755
3756 while (more) {
3757 more = 0;
3758
3759 for (i = con->first; i <= con->last; i++) {
3760 if (con_driver_map[i] == defcsw) {
3761 if (first == -1)
3762 first = i;
3763 last = i;
3764 more = 1;
3765 } else if (first != -1)
3766 break;
3767 }
3768
3769 if (first == 0 && last == MAX_NR_CONSOLES -1)
3770 deflt = 1;
3771
4c215fe8 3772 if (first != -1)
c62a1e57 3773 do_bind_con_driver(csw, first, last, deflt);
6db4063c
AD
3774
3775 first = -1;
3776 last = -1;
3777 deflt = 0;
3778 }
3779
3780err:
3781 return 0;
3782}
3783
3784static int vt_unbind(struct con_driver *con)
3785{
3786 const struct consw *csw = NULL;
3787 int i, more = 1, first = -1, last = -1, deflt = 0;
f418f2ec 3788 int ret;
6db4063c 3789
77232f79 3790 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3791 goto err;
3792
3793 csw = con->con;
3794
3795 while (more) {
3796 more = 0;
3797
3798 for (i = con->first; i <= con->last; i++) {
3799 if (con_driver_map[i] == csw) {
3800 if (first == -1)
3801 first = i;
3802 last = i;
3803 more = 1;
3804 } else if (first != -1)
3805 break;
3806 }
3807
3808 if (first == 0 && last == MAX_NR_CONSOLES -1)
3809 deflt = 1;
3810
618f2b90 3811 if (first != -1) {
f418f2ec 3812 ret = do_unbind_con_driver(csw, first, last, deflt);
f418f2ec
DV
3813 if (ret != 0)
3814 return ret;
618f2b90 3815 }
6db4063c
AD
3816
3817 first = -1;
3818 last = -1;
3819 deflt = 0;
3820 }
3821
3822err:
3823 return 0;
3824}
13ae6645
AD
3825#else
3826static inline int vt_bind(struct con_driver *con)
3827{
3828 return 0;
3829}
3830static inline int vt_unbind(struct con_driver *con)
3831{
3832 return 0;
3833}
3834#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
6db4063c 3835
805952a8 3836static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
6db4063c
AD
3837 const char *buf, size_t count)
3838{
805952a8 3839 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3840 int bind = simple_strtoul(buf, NULL, 0);
3841
4c215fe8
ID
3842 console_lock();
3843
6db4063c
AD
3844 if (bind)
3845 vt_bind(con);
3846 else
3847 vt_unbind(con);
3848
4c215fe8
ID
3849 console_unlock();
3850
6db4063c
AD
3851 return count;
3852}
3853
805952a8
GKH
3854static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3855 char *buf)
6db4063c 3856{
805952a8 3857 struct con_driver *con = dev_get_drvdata(dev);
61d51456
DV
3858 int bind;
3859
3860 console_lock();
3861 bind = con_is_bound(con->con);
3862 console_unlock();
6db4063c
AD
3863
3864 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3865}
3866
805952a8
GKH
3867static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3868 char *buf)
6db4063c 3869{
805952a8 3870 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3871
3872 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3873 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3874 con->desc);
3875
3876}
3877
1083a7be
TI
3878static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3879static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3880
3881static struct attribute *con_dev_attrs[] = {
3882 &dev_attr_bind.attr,
3883 &dev_attr_name.attr,
3884 NULL
6db4063c
AD
3885};
3886
1083a7be
TI
3887ATTRIBUTE_GROUPS(con_dev);
3888
805952a8 3889static int vtconsole_init_device(struct con_driver *con)
6db4063c 3890{
928e964f 3891 con->flag |= CON_DRIVER_FLAG_ATTR;
1083a7be 3892 return 0;
6db4063c
AD
3893}
3894
805952a8 3895static void vtconsole_deinit_device(struct con_driver *con)
6db4063c 3896{
1083a7be 3897 con->flag &= ~CON_DRIVER_FLAG_ATTR;
6db4063c
AD
3898}
3899
3e795de7
AD
3900/**
3901 * con_is_bound - checks if driver is bound to the console
3902 * @csw: console driver
3903 *
3904 * RETURNS: zero if unbound, nonzero if bound
3905 *
3906 * Drivers can call this and if zero, they should release
3907 * all resources allocated on con_startup()
3908 */
3909int con_is_bound(const struct consw *csw)
3910{
3911 int i, bound = 0;
3912
ddde3c18
DV
3913 WARN_CONSOLE_UNLOCKED();
3914
3e795de7
AD
3915 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3916 if (con_driver_map[i] == csw) {
3917 bound = 1;
3918 break;
3919 }
3920 }
3921
3922 return bound;
3923}
3924EXPORT_SYMBOL(con_is_bound);
3925
ddde3c18
DV
3926/**
3927 * con_is_visible - checks whether the current console is visible
3928 * @vc: virtual console
3929 *
3930 * RETURNS: zero if not visible, nonzero if visible
3931 */
3932bool con_is_visible(const struct vc_data *vc)
3933{
3934 WARN_CONSOLE_UNLOCKED();
3935
3936 return *vc->vc_display_fg == vc;
3937}
3938EXPORT_SYMBOL(con_is_visible);
3939
b45cfba4
JB
3940/**
3941 * con_debug_enter - prepare the console for the kernel debugger
3942 * @sw: console driver
3943 *
3944 * Called when the console is taken over by the kernel debugger, this
3945 * function needs to save the current console state, then put the console
3946 * into a state suitable for the kernel debugger.
3947 *
3948 * RETURNS:
3949 * Zero on success, nonzero if a failure occurred when trying to prepare
3950 * the console for the debugger.
3951 */
3952int con_debug_enter(struct vc_data *vc)
3953{
3954 int ret = 0;
3955
3956 saved_fg_console = fg_console;
3957 saved_last_console = last_console;
3958 saved_want_console = want_console;
3959 saved_vc_mode = vc->vc_mode;
beed5336 3960 saved_console_blanked = console_blanked;
b45cfba4
JB
3961 vc->vc_mode = KD_TEXT;
3962 console_blanked = 0;
3963 if (vc->vc_sw->con_debug_enter)
3964 ret = vc->vc_sw->con_debug_enter(vc);
81d44507
JW
3965#ifdef CONFIG_KGDB_KDB
3966 /* Set the initial LINES variable if it is not already set */
3967 if (vc->vc_rows < 999) {
3968 int linecount;
3969 char lns[4];
3970 const char *setargs[3] = {
3971 "set",
3972 "LINES",
3973 lns,
3974 };
3975 if (kdbgetintenv(setargs[0], &linecount)) {
3976 snprintf(lns, 4, "%i", vc->vc_rows);
3977 kdb_set(2, setargs);
3978 }
3979 }
17b572e8
JW
3980 if (vc->vc_cols < 999) {
3981 int colcount;
3982 char cols[4];
3983 const char *setargs[3] = {
3984 "set",
3985 "COLUMNS",
3986 cols,
3987 };
3988 if (kdbgetintenv(setargs[0], &colcount)) {
3989 snprintf(cols, 4, "%i", vc->vc_cols);
3990 kdb_set(2, setargs);
3991 }
3992 }
81d44507 3993#endif /* CONFIG_KGDB_KDB */
b45cfba4
JB
3994 return ret;
3995}
3996EXPORT_SYMBOL_GPL(con_debug_enter);
3997
3998/**
3999 * con_debug_leave - restore console state
4000 * @sw: console driver
4001 *
4002 * Restore the console state to what it was before the kernel debugger
4003 * was invoked.
4004 *
4005 * RETURNS:
4006 * Zero on success, nonzero if a failure occurred when trying to restore
4007 * the console.
4008 */
4009int con_debug_leave(void)
4010{
4011 struct vc_data *vc;
4012 int ret = 0;
4013
4014 fg_console = saved_fg_console;
4015 last_console = saved_last_console;
4016 want_console = saved_want_console;
beed5336 4017 console_blanked = saved_console_blanked;
b45cfba4
JB
4018 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
4019
4020 vc = vc_cons[fg_console].d;
4021 if (vc->vc_sw->con_debug_leave)
4022 ret = vc->vc_sw->con_debug_leave(vc);
4023 return ret;
4024}
4025EXPORT_SYMBOL_GPL(con_debug_leave);
4026
50e244cc 4027static int do_register_con_driver(const struct consw *csw, int first, int last)
3e795de7
AD
4028{
4029 struct module *owner = csw->owner;
4030 struct con_driver *con_driver;
4031 const char *desc;
96317e9e 4032 int i, retval;
3e795de7 4033
50e244cc
AC
4034 WARN_CONSOLE_UNLOCKED();
4035
3e795de7
AD
4036 if (!try_module_get(owner))
4037 return -ENODEV;
4038
3e795de7
AD
4039 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4040 con_driver = &registered_con_driver[i];
4041
4042 /* already registered */
96317e9e 4043 if (con_driver->con == csw) {
c55c63c6 4044 retval = -EBUSY;
96317e9e
JS
4045 goto err;
4046 }
3e795de7
AD
4047 }
4048
3e795de7 4049 desc = csw->con_startup();
6798df4c
JS
4050 if (!desc) {
4051 retval = -ENODEV;
3e795de7 4052 goto err;
6798df4c 4053 }
3e795de7
AD
4054
4055 retval = -EINVAL;
4056
4057 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4058 con_driver = &registered_con_driver[i];
4059
d364b5c3
ID
4060 if (con_driver->con == NULL &&
4061 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
3e795de7
AD
4062 con_driver->con = csw;
4063 con_driver->desc = desc;
6db4063c
AD
4064 con_driver->node = i;
4065 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3e795de7
AD
4066 CON_DRIVER_FLAG_INIT;
4067 con_driver->first = first;
4068 con_driver->last = last;
4069 retval = 0;
4070 break;
4071 }
4072 }
4073
6db4063c
AD
4074 if (retval)
4075 goto err;
4076
1083a7be
TI
4077 con_driver->dev =
4078 device_create_with_groups(vtconsole_class, NULL,
4079 MKDEV(0, con_driver->node),
4080 con_driver, con_dev_groups,
4081 "vtcon%i", con_driver->node);
805952a8 4082 if (IS_ERR(con_driver->dev)) {
bccf1da3
JP
4083 pr_warn("Unable to create device for %s; errno = %ld\n",
4084 con_driver->desc, PTR_ERR(con_driver->dev));
805952a8 4085 con_driver->dev = NULL;
6db4063c 4086 } else {
805952a8 4087 vtconsole_init_device(con_driver);
6db4063c 4088 }
928e964f 4089
3e795de7 4090err:
3e795de7
AD
4091 module_put(owner);
4092 return retval;
4093}
50e244cc 4094
3e795de7
AD
4095
4096/**
50539dd4 4097 * do_unregister_con_driver - unregister console driver from console layer
3e795de7
AD
4098 * @csw: console driver
4099 *
4100 * DESCRIPTION: All drivers that registers to the console layer must
4101 * call this function upon exit, or if the console driver is in a state
4102 * where it won't be able to handle console services, such as the
4103 * framebuffer console without loaded framebuffer drivers.
4104 *
4105 * The driver must unbind first prior to unregistration.
4106 */
e93a9a86
TI
4107int do_unregister_con_driver(const struct consw *csw)
4108{
d9c660e7 4109 int i;
3e795de7
AD
4110
4111 /* cannot unregister a bound driver */
4112 if (con_is_bound(csw))
d9c660e7
DV
4113 return -EBUSY;
4114
4115 if (csw == conswitchp)
4116 return -EINVAL;
3e795de7
AD
4117
4118 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4119 struct con_driver *con_driver = &registered_con_driver[i];
4120
2cf30f75 4121 if (con_driver->con == csw) {
d364b5c3
ID
4122 /*
4123 * Defer the removal of the sysfs entries since that
4124 * will acquire the kernfs s_active lock and we can't
4125 * acquire this lock while holding the console lock:
4126 * the unbind sysfs entry imposes already the opposite
4127 * order. Reset con already here to prevent any later
4128 * lookup to succeed and mark this slot as zombie, so
4129 * it won't get reused until we complete the removal
4130 * in the deferred work.
4131 */
3e795de7 4132 con_driver->con = NULL;
d364b5c3
ID
4133 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
4134 schedule_work(&con_driver_unregister_work);
4135
d9c660e7 4136 return 0;
3e795de7
AD
4137 }
4138 }
d9c660e7
DV
4139
4140 return -ENODEV;
3e795de7 4141}
e93a9a86 4142EXPORT_SYMBOL_GPL(do_unregister_con_driver);
3e795de7 4143
d364b5c3
ID
4144static void con_driver_unregister_callback(struct work_struct *ignored)
4145{
4146 int i;
4147
4148 console_lock();
4149
4150 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4151 struct con_driver *con_driver = &registered_con_driver[i];
4152
4153 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
4154 continue;
4155
4156 console_unlock();
4157
4158 vtconsole_deinit_device(con_driver);
4159 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
4160
4161 console_lock();
4162
4163 if (WARN_ON_ONCE(con_driver->con))
4164 con_driver->con = NULL;
4165 con_driver->desc = NULL;
4166 con_driver->dev = NULL;
4167 con_driver->node = 0;
4168 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
4169 con_driver->flag = 0;
4170 con_driver->first = 0;
4171 con_driver->last = 0;
4172 }
4173
4174 console_unlock();
4175}
4176
3e795de7
AD
4177/*
4178 * If we support more console drivers, this function is used
4179 * when a driver wants to take over some existing consoles
4180 * and become default driver for newly opened ones.
4181 *
0095ab42 4182 * do_take_over_console is basically a register followed by bind
50e244cc
AC
4183 */
4184int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
4185{
4186 int err;
4187
4188 err = do_register_con_driver(csw, first, last);
4189 /*
4190 * If we get an busy error we still want to bind the console driver
4191 * and return success, as we may have unbound the console driver
4192 * but not unregistered it.
4193 */
4194 if (err == -EBUSY)
4195 err = 0;
4196 if (!err)
4197 do_bind_con_driver(csw, first, last, deflt);
4198
4199 return err;
4200}
4201EXPORT_SYMBOL_GPL(do_take_over_console);
4202
3e795de7
AD
4203
4204/*
4205 * give_up_console is a wrapper to unregister_con_driver. It will only
4206 * work if driver is fully unbound.
4207 */
4208void give_up_console(const struct consw *csw)
4209{
70125e76
WY
4210 console_lock();
4211 do_unregister_con_driver(csw);
4212 console_unlock();
3e795de7
AD
4213}
4214
6db4063c 4215static int __init vtconsole_class_init(void)
3e795de7 4216{
6db4063c 4217 int i;
3e795de7 4218
6db4063c
AD
4219 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
4220 if (IS_ERR(vtconsole_class)) {
bccf1da3
JP
4221 pr_warn("Unable to create vt console class; errno = %ld\n",
4222 PTR_ERR(vtconsole_class));
6db4063c
AD
4223 vtconsole_class = NULL;
4224 }
3e795de7 4225
6db4063c 4226 /* Add system drivers to sysfs */
3e795de7
AD
4227 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
4228 struct con_driver *con = &registered_con_driver[i];
4229
805952a8 4230 if (con->con && !con->dev) {
1083a7be
TI
4231 con->dev =
4232 device_create_with_groups(vtconsole_class, NULL,
4233 MKDEV(0, con->node),
4234 con, con_dev_groups,
4235 "vtcon%i", con->node);
6db4063c 4236
805952a8 4237 if (IS_ERR(con->dev)) {
bccf1da3
JP
4238 pr_warn("Unable to create device for %s; errno = %ld\n",
4239 con->desc, PTR_ERR(con->dev));
805952a8 4240 con->dev = NULL;
6db4063c 4241 } else {
805952a8 4242 vtconsole_init_device(con);
6db4063c 4243 }
3e795de7 4244 }
3e795de7
AD
4245 }
4246
3e795de7
AD
4247 return 0;
4248}
6db4063c 4249postcore_initcall(vtconsole_class_init);
3e795de7 4250
1da177e4
LT
4251#endif
4252
4253/*
4254 * Screen blanking
4255 */
4256
403aac96 4257static int set_vesa_blanking(char __user *p)
1da177e4 4258{
403aac96
YY
4259 unsigned int mode;
4260
4261 if (get_user(mode, p + 1))
4262 return -EFAULT;
4263
4264 vesa_blank_mode = (mode < 4) ? mode : 0;
4265 return 0;
1da177e4
LT
4266}
4267
1da177e4
LT
4268void do_blank_screen(int entering_gfx)
4269{
4270 struct vc_data *vc = vc_cons[fg_console].d;
4271 int i;
4272
a135513c
DV
4273 might_sleep();
4274
1da177e4
LT
4275 WARN_CONSOLE_UNLOCKED();
4276
4277 if (console_blanked) {
4278 if (blank_state == blank_vesa_wait) {
4279 blank_state = blank_off;
d060a321 4280 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
1da177e4
LT
4281 }
4282 return;
4283 }
1da177e4
LT
4284
4285 /* entering graphics mode? */
4286 if (entering_gfx) {
4287 hide_cursor(vc);
4288 save_screen(vc);
4289 vc->vc_sw->con_blank(vc, -1, 1);
4290 console_blanked = fg_console + 1;
b6e8f00f 4291 blank_state = blank_off;
1da177e4
LT
4292 set_origin(vc);
4293 return;
4294 }
4295
b6e8f00f 4296 blank_state = blank_off;
4297
1da177e4
LT
4298 /* don't blank graphics */
4299 if (vc->vc_mode != KD_TEXT) {
4300 console_blanked = fg_console + 1;
4301 return;
4302 }
4303
4304 hide_cursor(vc);
4305 del_timer_sync(&console_timer);
4306 blank_timer_expired = 0;
4307
4308 save_screen(vc);
4309 /* In case we need to reset origin, blanking hook returns 1 */
d060a321 4310 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
1da177e4
LT
4311 console_blanked = fg_console + 1;
4312 if (i)
4313 set_origin(vc);
4314
4315 if (console_blank_hook && console_blank_hook(1))
4316 return;
4317
d060a321 4318 if (vesa_off_interval && vesa_blank_mode) {
030babac 4319 blank_state = blank_vesa_wait;
1da177e4
LT
4320 mod_timer(&console_timer, jiffies + vesa_off_interval);
4321 }
8b92e87d 4322 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
4323}
4324EXPORT_SYMBOL(do_blank_screen);
4325
4326/*
4327 * Called by timer as well as from vt_console_driver
4328 */
4329void do_unblank_screen(int leaving_gfx)
4330{
4331 struct vc_data *vc;
4332
4333 /* This should now always be called from a "sane" (read: can schedule)
4334 * context for the sake of the low level drivers, except in the special
4335 * case of oops_in_progress
4336 */
4337 if (!oops_in_progress)
4338 might_sleep();
4339
4340 WARN_CONSOLE_UNLOCKED();
4341
4342 ignore_poke = 0;
4343 if (!console_blanked)
4344 return;
4345 if (!vc_cons_allocated(fg_console)) {
4346 /* impossible */
e620e548
JP
4347 pr_warn("unblank_screen: tty %d not allocated ??\n",
4348 fg_console + 1);
1da177e4
LT
4349 return;
4350 }
4351 vc = vc_cons[fg_console].d;
8d7fc299 4352 if (vc->vc_mode != KD_TEXT)
1da177e4
LT
4353 return; /* but leave console_blanked != 0 */
4354
4355 if (blankinterval) {
f324edc8 4356 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
4357 blank_state = blank_normal_wait;
4358 }
4359
4360 console_blanked = 0;
8d7fc299 4361 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
1da177e4
LT
4362 /* Low-level driver cannot restore -> do it ourselves */
4363 update_screen(vc);
4364 if (console_blank_hook)
4365 console_blank_hook(0);
4366 set_palette(vc);
4367 set_cursor(vc);
8b92e87d 4368 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
4369}
4370EXPORT_SYMBOL(do_unblank_screen);
4371
4372/*
4373 * This is called by the outside world to cause a forced unblank, mostly for
4374 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4375 * call it with 1 as an argument and so force a mode restore... that may kill
4376 * X or at least garbage the screen but would also make the Oops visible...
4377 */
4378void unblank_screen(void)
4379{
4380 do_unblank_screen(0);
4381}
4382
4383/*
70522e12 4384 * We defer the timer blanking to work queue so it can take the console mutex
1da177e4 4385 * (console operations can still happen at irq time, but only from printk which
70522e12 4386 * has the console mutex. Not perfect yet, but better than no locking
1da177e4 4387 */
24ed960a 4388static void blank_screen_t(struct timer_list *unused)
1da177e4
LT
4389{
4390 blank_timer_expired = 1;
4391 schedule_work(&console_work);
4392}
4393
4394void poke_blanked_console(void)
4395{
4396 WARN_CONSOLE_UNLOCKED();
4397
4398 /* Add this so we quickly catch whoever might call us in a non
4399 * safe context. Nowadays, unblank_screen() isn't to be called in
4400 * atomic contexts and is allowed to schedule (with the special case
4401 * of oops_in_progress, but that isn't of any concern for this
4402 * function. --BenH.
4403 */
4404 might_sleep();
4405
4406 /* This isn't perfectly race free, but a race here would be mostly harmless,
4407 * at worse, we'll do a spurrious blank and it's unlikely
4408 */
4409 del_timer(&console_timer);
4410 blank_timer_expired = 0;
4411
4412 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4413 return;
4414 if (console_blanked)
4415 unblank_screen();
4416 else if (blankinterval) {
f324edc8 4417 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
4418 blank_state = blank_normal_wait;
4419 }
4420}
4421
4422/*
4423 * Palettes
4424 */
4425
4426static void set_palette(struct vc_data *vc)
4427{
4428 WARN_CONSOLE_UNLOCKED();
4429
709280da 4430 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
1da177e4
LT
4431 vc->vc_sw->con_set_palette(vc, color_table);
4432}
4433
1da177e4
LT
4434/*
4435 * Load palette into the DAC registers. arg points to a colour
4436 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4437 */
4438
4439int con_set_cmap(unsigned char __user *arg)
4440{
871bdea6
MG
4441 int i, j, k;
4442 unsigned char colormap[3*16];
4443
4444 if (copy_from_user(colormap, arg, sizeof(colormap)))
4445 return -EFAULT;
1da177e4 4446
ac751efa 4447 console_lock();
871bdea6
MG
4448 for (i = k = 0; i < 16; i++) {
4449 default_red[i] = colormap[k++];
4450 default_grn[i] = colormap[k++];
4451 default_blu[i] = colormap[k++];
4452 }
4453 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4454 if (!vc_cons_allocated(i))
4455 continue;
4456 for (j = k = 0; j < 16; j++) {
4457 vc_cons[i].d->vc_palette[k++] = default_red[j];
4458 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4459 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4460 }
4461 set_palette(vc_cons[i].d);
4462 }
ac751efa 4463 console_unlock();
1da177e4 4464
871bdea6 4465 return 0;
1da177e4
LT
4466}
4467
4468int con_get_cmap(unsigned char __user *arg)
4469{
871bdea6
MG
4470 int i, k;
4471 unsigned char colormap[3*16];
1da177e4 4472
ac751efa 4473 console_lock();
871bdea6
MG
4474 for (i = k = 0; i < 16; i++) {
4475 colormap[k++] = default_red[i];
4476 colormap[k++] = default_grn[i];
4477 colormap[k++] = default_blu[i];
4478 }
ac751efa 4479 console_unlock();
1da177e4 4480
871bdea6
MG
4481 if (copy_to_user(arg, colormap, sizeof(colormap)))
4482 return -EFAULT;
4483
4484 return 0;
1da177e4
LT
4485}
4486
4487void reset_palette(struct vc_data *vc)
4488{
4489 int j, k;
4490 for (j=k=0; j<16; j++) {
4491 vc->vc_palette[k++] = default_red[j];
4492 vc->vc_palette[k++] = default_grn[j];
4493 vc->vc_palette[k++] = default_blu[j];
4494 }
4495 set_palette(vc);
4496}
4497
4498/*
4499 * Font switching
4500 *
4501 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4502 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4503 * depending on width) reserved for each character which is kinda wasty, but
4504 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
25985edc 4505 * is up to the actual low-level console-driver convert data into its favorite
1da177e4
LT
4506 * format (maybe we should add a `fontoffset' field to the `display'
4507 * structure so we won't have to convert the fontdata all the time.
4508 * /Jes
4509 */
4510
4511#define max_font_size 65536
4512
4513static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4514{
4515 struct console_font font;
4516 int rc = -EINVAL;
4517 int c;
4518
1da177e4
LT
4519 if (op->data) {
4520 font.data = kmalloc(max_font_size, GFP_KERNEL);
4521 if (!font.data)
4522 return -ENOMEM;
4523 } else
4524 font.data = NULL;
4525
ac751efa 4526 console_lock();
edab558f
AC
4527 if (vc->vc_mode != KD_TEXT)
4528 rc = -EINVAL;
4529 else if (vc->vc_sw->con_font_get)
1da177e4
LT
4530 rc = vc->vc_sw->con_font_get(vc, &font);
4531 else
4532 rc = -ENOSYS;
ac751efa 4533 console_unlock();
1da177e4
LT
4534
4535 if (rc)
4536 goto out;
4537
4538 c = (font.width+7)/8 * 32 * font.charcount;
04f378b1 4539
1da177e4
LT
4540 if (op->data && font.charcount > op->charcount)
4541 rc = -ENOSPC;
4542 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4543 if (font.width > op->width || font.height > op->height)
4544 rc = -ENOSPC;
4545 } else {
4546 if (font.width != 8)
4547 rc = -EIO;
4548 else if ((op->height && font.height > op->height) ||
4549 font.height > 32)
4550 rc = -ENOSPC;
4551 }
4552 if (rc)
4553 goto out;
4554
4555 op->height = font.height;
4556 op->width = font.width;
4557 op->charcount = font.charcount;
4558
4559 if (op->data && copy_to_user(op->data, font.data, c))
4560 rc = -EFAULT;
4561
4562out:
4563 kfree(font.data);
4564 return rc;
4565}
4566
4567static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4568{
4569 struct console_font font;
4570 int rc = -EINVAL;
4571 int size;
4572
4573 if (vc->vc_mode != KD_TEXT)
4574 return -EINVAL;
4575 if (!op->data)
4576 return -EINVAL;
4577 if (op->charcount > 512)
4578 return -EINVAL;
8ffb8209
MX
4579 if (op->width <= 0 || op->width > 32 || op->height > 32)
4580 return -EINVAL;
4581 size = (op->width+7)/8 * 32 * op->charcount;
4582 if (size > max_font_size)
4583 return -ENOSPC;
4584
4585 font.data = memdup_user(op->data, size);
4586 if (IS_ERR(font.data))
4587 return PTR_ERR(font.data);
4588
1da177e4
LT
4589 if (!op->height) { /* Need to guess font height [compat] */
4590 int h, i;
8ffb8209
MX
4591 u8 *charmap = font.data;
4592
4593 /*
4594 * If from KDFONTOP ioctl, don't allow things which can be done
4595 * in userland,so that we can get rid of this soon
4596 */
4597 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4598 kfree(font.data);
1da177e4 4599 return -EINVAL;
8ffb8209
MX
4600 }
4601
1da177e4 4602 for (h = 32; h > 0; h--)
8ffb8209
MX
4603 for (i = 0; i < op->charcount; i++)
4604 if (charmap[32*i+h-1])
1da177e4 4605 goto nonzero;
8ffb8209
MX
4606
4607 kfree(font.data);
1da177e4 4608 return -EINVAL;
8ffb8209 4609
1da177e4
LT
4610 nonzero:
4611 op->height = h;
4612 }
8ffb8209 4613
1da177e4 4614 font.charcount = op->charcount;
1da177e4 4615 font.width = op->width;
8ffb8209
MX
4616 font.height = op->height;
4617
ac751efa 4618 console_lock();
edab558f
AC
4619 if (vc->vc_mode != KD_TEXT)
4620 rc = -EINVAL;
4621 else if (vc->vc_sw->con_font_set)
1da177e4
LT
4622 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4623 else
4624 rc = -ENOSYS;
ac751efa 4625 console_unlock();
1da177e4
LT
4626 kfree(font.data);
4627 return rc;
4628}
4629
4630static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4631{
4632 struct console_font font = {.width = op->width, .height = op->height};
4633 char name[MAX_FONT_NAME];
4634 char *s = name;
4635 int rc;
4636
1da177e4
LT
4637
4638 if (!op->data)
4639 s = NULL;
4640 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4641 return -EFAULT;
4642 else
4643 name[MAX_FONT_NAME - 1] = 0;
4644
ac751efa 4645 console_lock();
edab558f
AC
4646 if (vc->vc_mode != KD_TEXT) {
4647 console_unlock();
4648 return -EINVAL;
4649 }
1da177e4
LT
4650 if (vc->vc_sw->con_font_default)
4651 rc = vc->vc_sw->con_font_default(vc, &font, s);
4652 else
4653 rc = -ENOSYS;
ac751efa 4654 console_unlock();
1da177e4
LT
4655 if (!rc) {
4656 op->width = font.width;
4657 op->height = font.height;
4658 }
4659 return rc;
4660}
4661
4662static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4663{
4664 int con = op->height;
4665 int rc;
4666
1da177e4 4667
ac751efa 4668 console_lock();
edab558f
AC
4669 if (vc->vc_mode != KD_TEXT)
4670 rc = -EINVAL;
4671 else if (!vc->vc_sw->con_font_copy)
1da177e4
LT
4672 rc = -ENOSYS;
4673 else if (con < 0 || !vc_cons_allocated(con))
4674 rc = -ENOTTY;
4675 else if (con == vc->vc_num) /* nothing to do */
4676 rc = 0;
4677 else
4678 rc = vc->vc_sw->con_font_copy(vc, con);
ac751efa 4679 console_unlock();
1da177e4
LT
4680 return rc;
4681}
4682
4683int con_font_op(struct vc_data *vc, struct console_font_op *op)
4684{
4685 switch (op->op) {
4686 case KD_FONT_OP_SET:
4687 return con_font_set(vc, op);
4688 case KD_FONT_OP_GET:
4689 return con_font_get(vc, op);
4690 case KD_FONT_OP_SET_DEFAULT:
4691 return con_font_default(vc, op);
4692 case KD_FONT_OP_COPY:
4693 return con_font_copy(vc, op);
4694 }
4695 return -ENOSYS;
4696}
4697
4698/*
4699 * Interface exported to selection and vcs.
4700 */
4701
4702/* used by selection */
4703u16 screen_glyph(struct vc_data *vc, int offset)
4704{
4705 u16 w = scr_readw(screenpos(vc, offset, 1));
4706 u16 c = w & 0xff;
4707
4708 if (w & vc->vc_hi_font_mask)
4709 c |= 0x100;
4710 return c;
4711}
f7511d5f 4712EXPORT_SYMBOL_GPL(screen_glyph);
1da177e4 4713
9bfdc261
AB
4714u32 screen_glyph_unicode(struct vc_data *vc, int n)
4715{
4716 struct uni_screen *uniscr = get_vc_uniscr(vc);
4717
4718 if (uniscr)
4719 return uniscr->lines[n / vc->vc_cols][n % vc->vc_cols];
4720 return inverse_translate(vc, screen_glyph(vc, n * 2), 1);
4721}
4722EXPORT_SYMBOL_GPL(screen_glyph_unicode);
4723
1da177e4
LT
4724/* used by vcs - note the word offset */
4725unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4726{
4727 return screenpos(vc, 2 * w_offset, viewed);
4728}
88867e3d 4729EXPORT_SYMBOL_GPL(screen_pos);
1da177e4
LT
4730
4731void getconsxy(struct vc_data *vc, unsigned char *p)
4732{
8a085494 4733 /* clamp values if they don't fit */
28bc24fc
JS
4734 p[0] = min(vc->state.x, 0xFFu);
4735 p[1] = min(vc->state.y, 0xFFu);
1da177e4
LT
4736}
4737
4738void putconsxy(struct vc_data *vc, unsigned char *p)
4739{
9477e260 4740 hide_cursor(vc);
1da177e4
LT
4741 gotoxy(vc, p[0], p[1]);
4742 set_cursor(vc);
4743}
4744
4745u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4746{
4747 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4748 return softcursor_original;
4749 return scr_readw(org);
4750}
4751
4752void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4753{
4754 scr_writew(val, org);
4755 if ((unsigned long)org == vc->vc_pos) {
4756 softcursor_original = -1;
4757 add_softcursor(vc);
4758 }
4759}
4760
432c9ed2
NP
4761void vcs_scr_updated(struct vc_data *vc)
4762{
4763 notify_update(vc);
4764}
4765
35cc56f9
JS
4766void vc_scrolldelta_helper(struct vc_data *c, int lines,
4767 unsigned int rolled_over, void *base, unsigned int size)
4768{
4769 unsigned long ubase = (unsigned long)base;
210fd746
JS
4770 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4771 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4772 ptrdiff_t origin = (void *)c->vc_origin - base;
35cc56f9 4773 int margin = c->vc_size_row * 4;
7c918cdc 4774 int from, wrap, from_off, avail;
35cc56f9
JS
4775
4776 /* Turn scrollback off */
4777 if (!lines) {
4778 c->vc_visible_origin = c->vc_origin;
4779 return;
4780 }
4781
4782 /* Do we have already enough to allow jumping from 0 to the end? */
210fd746 4783 if (rolled_over > scr_end + margin) {
7c918cdc
JS
4784 from = scr_end;
4785 wrap = rolled_over + c->vc_size_row;
35cc56f9 4786 } else {
7c918cdc
JS
4787 from = 0;
4788 wrap = size;
35cc56f9
JS
4789 }
4790
7c918cdc
JS
4791 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4792 avail = (origin - from + wrap) % wrap;
35cc56f9
JS
4793
4794 /* Only a little piece would be left? Show all incl. the piece! */
7c918cdc 4795 if (avail < 2 * margin)
35cc56f9 4796 margin = 0;
7c918cdc
JS
4797 if (from_off < margin)
4798 from_off = 0;
4799 if (from_off > avail - margin)
4800 from_off = avail;
35cc56f9 4801
7c918cdc 4802 c->vc_visible_origin = ubase + (from + from_off) % wrap;
35cc56f9
JS
4803}
4804EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4805
1da177e4
LT
4806/*
4807 * Visible symbols for modules
4808 */
4809
4810EXPORT_SYMBOL(color_table);
4811EXPORT_SYMBOL(default_red);
4812EXPORT_SYMBOL(default_grn);
4813EXPORT_SYMBOL(default_blu);
4814EXPORT_SYMBOL(update_region);
4815EXPORT_SYMBOL(redraw_screen);
4816EXPORT_SYMBOL(vc_resize);
4817EXPORT_SYMBOL(fg_console);
4818EXPORT_SYMBOL(console_blank_hook);
4819EXPORT_SYMBOL(console_blanked);
4820EXPORT_SYMBOL(vc_cons);
f6c06b68 4821EXPORT_SYMBOL(global_cursor_default);
1da177e4 4822#ifndef VT_SINGLE_DRIVER
1da177e4
LT
4823EXPORT_SYMBOL(give_up_console);
4824#endif