]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/tty/vt/vt.c
vt: preserve unicode values corresponding to screen characters
[thirdparty/linux.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>
84#include <linux/major.h>
85#include <linux/mm.h>
86#include <linux/console.h>
87#include <linux/init.h>
c831c338 88#include <linux/mutex.h>
1da177e4
LT
89#include <linux/vt_kern.h>
90#include <linux/selection.h>
91#include <linux/tiocl.h>
92#include <linux/kbd_kern.h>
93#include <linux/consolemap.h>
94#include <linux/timer.h>
95#include <linux/interrupt.h>
1da177e4 96#include <linux/workqueue.h>
1da177e4
LT
97#include <linux/pm.h>
98#include <linux/font.h>
99#include <linux/bitops.h>
b293d758 100#include <linux/notifier.h>
d81ed103
AC
101#include <linux/device.h>
102#include <linux/io.h>
d81ed103 103#include <linux/uaccess.h>
81d44507 104#include <linux/kdb.h>
74c807ce 105#include <linux/ctype.h>
f0a8d84f 106#include <linux/bsearch.h>
1da177e4 107
3e795de7 108#define MAX_NR_CON_DRIVER 16
1da177e4 109
6db4063c 110#define CON_DRIVER_FLAG_MODULE 1
928e964f
AD
111#define CON_DRIVER_FLAG_INIT 2
112#define CON_DRIVER_FLAG_ATTR 4
d364b5c3 113#define CON_DRIVER_FLAG_ZOMBIE 8
3e795de7
AD
114
115struct con_driver {
116 const struct consw *con;
117 const char *desc;
805952a8 118 struct device *dev;
6db4063c 119 int node;
3e795de7
AD
120 int first;
121 int last;
122 int flag;
123};
124
125static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
1da177e4
LT
126const struct consw *conswitchp;
127
128/* A bitmap for codes <32. A bit of 1 indicates that the code
129 * corresponding to that bit number invokes some special action
130 * (such as cursor movement) and should not be displayed as a
131 * glyph unless the disp_ctrl mode is explicitly enabled.
132 */
133#define CTRL_ACTION 0x0d00ff81
134#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
135
136/*
137 * Here is the default bell parameters: 750HZ, 1/8th of a second
138 */
139#define DEFAULT_BELL_PITCH 750
140#define DEFAULT_BELL_DURATION (HZ/8)
bd63364c 141#define DEFAULT_CURSOR_BLINK_MS 200
1da177e4 142
1da177e4
LT
143struct vc vc_cons [MAX_NR_CONSOLES];
144
145#ifndef VT_SINGLE_DRIVER
146static const struct consw *con_driver_map[MAX_NR_CONSOLES];
147#endif
148
149static int con_open(struct tty_struct *, struct file *);
150static void vc_init(struct vc_data *vc, unsigned int rows,
151 unsigned int cols, int do_clear);
152static void gotoxy(struct vc_data *vc, int new_x, int new_y);
153static void save_cur(struct vc_data *vc);
154static void reset_terminal(struct vc_data *vc, int do_clear);
155static void con_flush_chars(struct tty_struct *tty);
403aac96 156static int set_vesa_blanking(char __user *p);
1da177e4
LT
157static void set_cursor(struct vc_data *vc);
158static void hide_cursor(struct vc_data *vc);
65f27f38 159static void console_callback(struct work_struct *ignored);
d364b5c3 160static void con_driver_unregister_callback(struct work_struct *ignored);
24ed960a 161static void blank_screen_t(struct timer_list *unused);
1da177e4
LT
162static void set_palette(struct vc_data *vc);
163
52c40fca
PH
164#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
165
1da177e4 166static int printable; /* Is console ready for printing? */
77bf2bab 167int default_utf8 = true;
042f10ec 168module_param(default_utf8, int, S_IRUGO | S_IWUSR);
f6c06b68
MG
169int global_cursor_default = -1;
170module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
1da177e4 171
9ea9a886
CL
172static int cur_default = CUR_DEFAULT;
173module_param(cur_default, int, S_IRUGO | S_IWUSR);
174
1da177e4
LT
175/*
176 * ignore_poke: don't unblank the screen when things are typed. This is
177 * mainly for the privacy of braille terminal users.
178 */
179static int ignore_poke;
180
181int do_poke_blanked_console;
182int console_blanked;
183
184static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
1da177e4 185static int vesa_off_interval;
a4199f5e 186static int blankinterval;
f324edc8 187core_param(consoleblank, blankinterval, int, 0444);
1da177e4 188
65f27f38 189static DECLARE_WORK(console_work, console_callback);
d364b5c3 190static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
1da177e4
LT
191
192/*
193 * fg_console is the current virtual console,
194 * last_console is the last used one,
195 * want_console is the console we want to switch to,
b45cfba4 196 * saved_* variants are for save/restore around kernel debugger enter/leave
1da177e4
LT
197 */
198int fg_console;
199int last_console;
200int want_console = -1;
fed891c8
JW
201static int saved_fg_console;
202static int saved_last_console;
203static int saved_want_console;
204static int saved_vc_mode;
beed5336 205static int saved_console_blanked;
1da177e4
LT
206
207/*
208 * For each existing display, we have a pointer to console currently visible
209 * on that display, allowing consoles other than fg_console to be refreshed
210 * appropriately. Unless the low-level driver supplies its own display_fg
211 * variable, we use this one for the "master display".
212 */
213static struct vc_data *master_display_fg;
214
215/*
216 * Unfortunately, we need to delay tty echo when we're currently writing to the
217 * console since the code is (and always was) not re-entrant, so we schedule
218 * all flip requests to process context with schedule-task() and run it from
219 * console_callback().
220 */
221
222/*
223 * For the same reason, we defer scrollback to the console callback.
224 */
225static int scrollback_delta;
226
227/*
228 * Hook so that the power management routines can (un)blank
229 * the console on our behalf.
230 */
231int (*console_blank_hook)(int);
232
1d27e3e2 233static DEFINE_TIMER(console_timer, blank_screen_t);
1da177e4
LT
234static int blank_state;
235static int blank_timer_expired;
236enum {
237 blank_off = 0,
238 blank_normal_wait,
239 blank_vesa_wait,
240};
241
fbc92a34
KS
242/*
243 * /sys/class/tty/tty0/
244 *
245 * the attribute 'active' contains the name of the current vc
246 * console and it supports poll() to detect vc switches
247 */
248static struct device *tty0dev;
249
b293d758
ST
250/*
251 * Notifier list for console events.
252 */
253static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
254
255int register_vt_notifier(struct notifier_block *nb)
256{
257 return atomic_notifier_chain_register(&vt_notifier_list, nb);
258}
259EXPORT_SYMBOL_GPL(register_vt_notifier);
260
261int unregister_vt_notifier(struct notifier_block *nb)
262{
263 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
264}
265EXPORT_SYMBOL_GPL(unregister_vt_notifier);
266
267static void notify_write(struct vc_data *vc, unsigned int unicode)
268{
502fcb79 269 struct vt_notifier_param param = { .vc = vc, .c = unicode };
b293d758
ST
270 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
271}
272
273static void notify_update(struct vc_data *vc)
274{
275 struct vt_notifier_param param = { .vc = vc };
276 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
277}
1da177e4
LT
278/*
279 * Low-Level Functions
280 */
281
6ca8dfd7
JS
282static inline bool con_is_fg(const struct vc_data *vc)
283{
284 return vc->vc_num == fg_console;
285}
286
287static inline bool con_should_update(const struct vc_data *vc)
288{
289 return con_is_visible(vc) && !console_blanked;
290}
1da177e4
LT
291
292static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
293{
294 unsigned short *p;
295
296 if (!viewed)
297 p = (unsigned short *)(vc->vc_origin + offset);
298 else if (!vc->vc_sw->con_screen_pos)
299 p = (unsigned short *)(vc->vc_visible_origin + offset);
300 else
301 p = vc->vc_sw->con_screen_pos(vc, offset);
302 return p;
303}
304
e33ac1c1 305/* Called from the keyboard irq path.. */
1da177e4
LT
306static inline void scrolldelta(int lines)
307{
e33ac1c1
AC
308 /* FIXME */
309 /* scrolldelta needs some kind of consistency lock, but the BKL was
310 and still is not protecting versus the scheduled back end */
1da177e4
LT
311 scrollback_delta += lines;
312 schedule_console_callback();
313}
314
315void schedule_console_callback(void)
316{
317 schedule_work(&console_work);
318}
319
d8ae7242
NP
320/*
321 * Code to manage unicode-based screen buffers
322 */
323
324#ifdef NO_VC_UNI_SCREEN
325/* this disables and optimizes related code away at compile time */
326#define get_vc_uniscr(vc) NULL
327#else
328#define get_vc_uniscr(vc) vc->vc_uni_screen
329#endif
330
331typedef uint32_t char32_t;
332
333/*
334 * Our screen buffer is preceded by an array of line pointers so that
335 * scrolling only implies some pointer shuffling.
336 */
337struct uni_screen {
338 char32_t *lines[0];
339};
340
341static struct uni_screen *vc_uniscr_alloc(unsigned int cols, unsigned int rows)
342{
343 struct uni_screen *uniscr;
344 void *p;
345 unsigned int memsize, i;
346
347 /* allocate everything in one go */
348 memsize = cols * rows * sizeof(char32_t);
349 memsize += rows * sizeof(char32_t *);
350 p = kmalloc(memsize, GFP_KERNEL);
351 if (!p)
352 return NULL;
353
354 /* initial line pointers */
355 uniscr = p;
356 p = uniscr->lines + rows;
357 for (i = 0; i < rows; i++) {
358 uniscr->lines[i] = p;
359 p += cols * sizeof(char32_t);
360 }
361 return uniscr;
362}
363
364static void vc_uniscr_set(struct vc_data *vc, struct uni_screen *new_uniscr)
365{
366 kfree(vc->vc_uni_screen);
367 vc->vc_uni_screen = new_uniscr;
368}
369
370static void vc_uniscr_putc(struct vc_data *vc, char32_t uc)
371{
372 struct uni_screen *uniscr = get_vc_uniscr(vc);
373
374 if (uniscr)
375 uniscr->lines[vc->vc_y][vc->vc_x] = uc;
376}
377
378static void vc_uniscr_insert(struct vc_data *vc, unsigned int nr)
379{
380 struct uni_screen *uniscr = get_vc_uniscr(vc);
381
382 if (uniscr) {
383 char32_t *ln = uniscr->lines[vc->vc_y];
384 unsigned int x = vc->vc_x, cols = vc->vc_cols;
385
386 memmove(&ln[x + nr], &ln[x], (cols - x - nr) * sizeof(*ln));
387 memset32(&ln[x], ' ', nr);
388 }
389}
390
391static void vc_uniscr_delete(struct vc_data *vc, unsigned int nr)
392{
393 struct uni_screen *uniscr = get_vc_uniscr(vc);
394
395 if (uniscr) {
396 char32_t *ln = uniscr->lines[vc->vc_y];
397 unsigned int x = vc->vc_x, cols = vc->vc_cols;
398
399 memcpy(&ln[x], &ln[x + nr], (cols - x - nr) * sizeof(*ln));
400 memset32(&ln[cols - nr], ' ', nr);
401 }
402}
403
404static void vc_uniscr_clear_line(struct vc_data *vc, unsigned int x,
405 unsigned int nr)
406{
407 struct uni_screen *uniscr = get_vc_uniscr(vc);
408
409 if (uniscr) {
410 char32_t *ln = uniscr->lines[vc->vc_y];
411
412 memset32(&ln[x], ' ', nr);
413 }
414}
415
416static void vc_uniscr_clear_lines(struct vc_data *vc, unsigned int y,
417 unsigned int nr)
418{
419 struct uni_screen *uniscr = get_vc_uniscr(vc);
420
421 if (uniscr) {
422 unsigned int cols = vc->vc_cols;
423
424 while (nr--)
425 memset32(uniscr->lines[y++], ' ', cols);
426 }
427}
428
429static void vc_uniscr_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
430 enum con_scroll dir, unsigned int nr)
431{
432 struct uni_screen *uniscr = get_vc_uniscr(vc);
433
434 if (uniscr) {
435 unsigned int s, d, rescue, clear;
436 char32_t *save[nr];
437
438 s = clear = t;
439 d = t + nr;
440 rescue = b - nr;
441 if (dir == SM_UP) {
442 swap(s, d);
443 swap(clear, rescue);
444 }
445 memcpy(save, uniscr->lines + rescue, nr * sizeof(*save));
446 memmove(uniscr->lines + d, uniscr->lines + s,
447 (b - t - nr) * sizeof(*uniscr->lines));
448 memcpy(uniscr->lines + clear, save, nr * sizeof(*save));
449 vc_uniscr_clear_lines(vc, clear, nr);
450 }
451}
452
453static void vc_uniscr_copy_area(struct uni_screen *dst,
454 unsigned int dst_cols,
455 unsigned int dst_rows,
456 struct uni_screen *src,
457 unsigned int src_cols,
458 unsigned int src_top_row,
459 unsigned int src_bot_row)
460{
461 unsigned int dst_row = 0;
462
463 if (!dst)
464 return;
465
466 while (src_top_row < src_bot_row) {
467 char32_t *src_line = src->lines[src_top_row];
468 char32_t *dst_line = dst->lines[dst_row];
469
470 memcpy(dst_line, src_line, src_cols * sizeof(char32_t));
471 if (dst_cols - src_cols)
472 memset32(dst_line + src_cols, ' ', dst_cols - src_cols);
473 src_top_row++;
474 dst_row++;
475 }
476 while (dst_row < dst_rows) {
477 char32_t *dst_line = dst->lines[dst_row];
478
479 memset32(dst_line, ' ', dst_cols);
480 dst_row++;
481 }
482}
483
484
89765b94
JS
485static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
486 enum con_scroll dir, unsigned int nr)
1da177e4 487{
89765b94 488 u16 *clear, *d, *s;
1da177e4 489
89765b94 490 if (t + nr >= b)
1da177e4
LT
491 nr = b - t - 1;
492 if (b > vc->vc_rows || t >= b || nr < 1)
493 return;
d8ae7242 494 vc_uniscr_scroll(vc, t, b, dir, nr);
89765b94 495 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, dir, nr))
1da177e4 496 return;
1da177e4 497
89765b94
JS
498 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
499 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
1da177e4 500
89765b94
JS
501 if (dir == SM_UP) {
502 clear = s + (b - t - nr) * vc->vc_cols;
503 swap(s, d);
504 }
505 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
506 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
1da177e4
LT
507}
508
509static void do_update_region(struct vc_data *vc, unsigned long start, int count)
510{
1da177e4
LT
511 unsigned int xx, yy, offset;
512 u16 *p;
513
514 p = (u16 *) start;
515 if (!vc->vc_sw->con_getxy) {
516 offset = (start - vc->vc_origin) / 2;
517 xx = offset % vc->vc_cols;
518 yy = offset / vc->vc_cols;
519 } else {
520 int nxx, nyy;
521 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
522 xx = nxx; yy = nyy;
523 }
524 for(;;) {
525 u16 attrib = scr_readw(p) & 0xff00;
526 int startx = xx;
527 u16 *q = p;
528 while (xx < vc->vc_cols && count) {
529 if (attrib != (scr_readw(p) & 0xff00)) {
530 if (p > q)
531 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
532 startx = xx;
533 q = p;
534 attrib = scr_readw(p) & 0xff00;
535 }
536 p++;
537 xx++;
538 count--;
539 }
540 if (p > q)
541 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
542 if (!count)
543 break;
544 xx = 0;
545 yy++;
546 if (vc->vc_sw->con_getxy) {
547 p = (u16 *)start;
548 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
549 }
550 }
1da177e4
LT
551}
552
553void update_region(struct vc_data *vc, unsigned long start, int count)
554{
555 WARN_CONSOLE_UNLOCKED();
556
6ca8dfd7 557 if (con_should_update(vc)) {
1da177e4
LT
558 hide_cursor(vc);
559 do_update_region(vc, start, count);
560 set_cursor(vc);
561 }
562}
563
564/* Structure of attributes is hardware-dependent */
565
fa6ce9ab
JE
566static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
567 u8 _underline, u8 _reverse, u8 _italic)
1da177e4
LT
568{
569 if (vc->vc_sw->con_build_attr)
fa6ce9ab
JE
570 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
571 _blink, _underline, _reverse, _italic);
1da177e4 572
1da177e4
LT
573/*
574 * ++roman: I completely changed the attribute format for monochrome
575 * mode (!can_do_color). The formerly used MDA (monochrome display
576 * adapter) format didn't allow the combination of certain effects.
577 * Now the attribute is just a bit vector:
578 * Bit 0..1: intensity (0..2)
579 * Bit 2 : underline
580 * Bit 3 : reverse
581 * Bit 7 : blink
582 */
583 {
c9e587ab 584 u8 a = _color;
1da177e4
LT
585 if (!vc->vc_can_do_color)
586 return _intensity |
fa6ce9ab 587 (_italic ? 2 : 0) |
1da177e4
LT
588 (_underline ? 4 : 0) |
589 (_reverse ? 8 : 0) |
590 (_blink ? 0x80 : 0);
fa6ce9ab
JE
591 if (_italic)
592 a = (a & 0xF0) | vc->vc_itcolor;
593 else if (_underline)
1da177e4
LT
594 a = (a & 0xf0) | vc->vc_ulcolor;
595 else if (_intensity == 0)
1c65a879 596 a = (a & 0xf0) | vc->vc_halfcolor;
1da177e4
LT
597 if (_reverse)
598 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
599 if (_blink)
600 a ^= 0x80;
601 if (_intensity == 2)
602 a ^= 0x08;
603 if (vc->vc_hi_font_mask == 0x100)
604 a <<= 1;
605 return a;
606 }
1da177e4
LT
607}
608
609static void update_attr(struct vc_data *vc)
610{
fa6ce9ab
JE
611 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
612 vc->vc_blink, vc->vc_underline,
613 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
614 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
1da177e4
LT
615}
616
617/* Note: inverting the screen twice should revert to the original state */
618void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
619{
620 unsigned short *p;
621
622 WARN_CONSOLE_UNLOCKED();
623
624 count /= 2;
625 p = screenpos(vc, offset, viewed);
6af39ed3 626 if (vc->vc_sw->con_invert_region) {
1da177e4 627 vc->vc_sw->con_invert_region(vc, p, count);
6af39ed3 628 } else {
1da177e4
LT
629 u16 *q = p;
630 int cnt = count;
631 u16 a;
632
633 if (!vc->vc_can_do_color) {
634 while (cnt--) {
635 a = scr_readw(q);
636 a ^= 0x0800;
637 scr_writew(a, q);
638 q++;
639 }
640 } else if (vc->vc_hi_font_mask == 0x100) {
641 while (cnt--) {
642 a = scr_readw(q);
643 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
644 scr_writew(a, q);
645 q++;
646 }
647 } else {
648 while (cnt--) {
649 a = scr_readw(q);
650 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
651 scr_writew(a, q);
652 q++;
653 }
654 }
655 }
6af39ed3 656
6ca8dfd7 657 if (con_should_update(vc))
1da177e4 658 do_update_region(vc, (unsigned long) p, count);
19e3ae6b 659 notify_update(vc);
1da177e4
LT
660}
661
662/* used by selection: complement pointer position */
663void complement_pos(struct vc_data *vc, int offset)
664{
414edcd3 665 static int old_offset = -1;
1da177e4
LT
666 static unsigned short old;
667 static unsigned short oldx, oldy;
668
669 WARN_CONSOLE_UNLOCKED();
670
414edcd3
AD
671 if (old_offset != -1 && old_offset >= 0 &&
672 old_offset < vc->vc_screenbuf_size) {
673 scr_writew(old, screenpos(vc, old_offset, 1));
6ca8dfd7 674 if (con_should_update(vc))
1da177e4 675 vc->vc_sw->con_putc(vc, old, oldy, oldx);
19e3ae6b 676 notify_update(vc);
1da177e4 677 }
414edcd3
AD
678
679 old_offset = offset;
680
681 if (offset != -1 && offset >= 0 &&
682 offset < vc->vc_screenbuf_size) {
1da177e4 683 unsigned short new;
414edcd3 684 unsigned short *p;
1da177e4
LT
685 p = screenpos(vc, offset, 1);
686 old = scr_readw(p);
687 new = old ^ vc->vc_complement_mask;
688 scr_writew(new, p);
6ca8dfd7 689 if (con_should_update(vc)) {
1da177e4
LT
690 oldx = (offset >> 1) % vc->vc_cols;
691 oldy = (offset >> 1) / vc->vc_cols;
692 vc->vc_sw->con_putc(vc, new, oldy, oldx);
693 }
19e3ae6b 694 notify_update(vc);
1da177e4
LT
695 }
696}
697
698static void insert_char(struct vc_data *vc, unsigned int nr)
699{
81732c3b 700 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 701
d8ae7242 702 vc_uniscr_insert(vc, nr);
a883b70d 703 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
81732c3b 704 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
1da177e4 705 vc->vc_need_wrap = 0;
6ca8dfd7 706 if (con_should_update(vc))
81732c3b 707 do_update_region(vc, (unsigned long) p,
b1a925f4 708 vc->vc_cols - vc->vc_x);
1da177e4
LT
709}
710
711static void delete_char(struct vc_data *vc, unsigned int nr)
712{
81732c3b 713 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 714
d8ae7242 715 vc_uniscr_delete(vc, nr);
b1a925f4 716 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
81732c3b
JFM
717 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
718 nr * 2);
1da177e4 719 vc->vc_need_wrap = 0;
6ca8dfd7 720 if (con_should_update(vc))
81732c3b 721 do_update_region(vc, (unsigned long) p,
b1a925f4 722 vc->vc_cols - vc->vc_x);
1da177e4
LT
723}
724
e882f715 725static int softcursor_original = -1;
1da177e4
LT
726
727static void add_softcursor(struct vc_data *vc)
728{
729 int i = scr_readw((u16 *) vc->vc_pos);
730 u32 type = vc->vc_cursor_type;
731
732 if (! (type & 0x10)) return;
733 if (softcursor_original != -1) return;
734 softcursor_original = i;
735 i |= ((type >> 8) & 0xff00 );
736 i ^= ((type) & 0xff00 );
737 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
738 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
739 scr_writew(i, (u16 *) vc->vc_pos);
6ca8dfd7 740 if (con_should_update(vc))
1da177e4
LT
741 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
742}
743
744static void hide_softcursor(struct vc_data *vc)
745{
746 if (softcursor_original != -1) {
747 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
6ca8dfd7 748 if (con_should_update(vc))
1da177e4
LT
749 vc->vc_sw->con_putc(vc, softcursor_original,
750 vc->vc_y, vc->vc_x);
751 softcursor_original = -1;
752 }
753}
754
755static void hide_cursor(struct vc_data *vc)
756{
757 if (vc == sel_cons)
758 clear_selection();
759 vc->vc_sw->con_cursor(vc, CM_ERASE);
760 hide_softcursor(vc);
761}
762
763static void set_cursor(struct vc_data *vc)
764{
6ca8dfd7 765 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
1da177e4
LT
766 return;
767 if (vc->vc_deccm) {
768 if (vc == sel_cons)
769 clear_selection();
770 add_softcursor(vc);
771 if ((vc->vc_cursor_type & 0x0f) != 1)
772 vc->vc_sw->con_cursor(vc, CM_DRAW);
773 } else
774 hide_cursor(vc);
775}
776
777static void set_origin(struct vc_data *vc)
778{
779 WARN_CONSOLE_UNLOCKED();
780
6ca8dfd7 781 if (!con_is_visible(vc) ||
1da177e4
LT
782 !vc->vc_sw->con_set_origin ||
783 !vc->vc_sw->con_set_origin(vc))
784 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
785 vc->vc_visible_origin = vc->vc_origin;
786 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
787 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
788}
789
c0f160a7 790static void save_screen(struct vc_data *vc)
1da177e4
LT
791{
792 WARN_CONSOLE_UNLOCKED();
793
794 if (vc->vc_sw->con_save_screen)
795 vc->vc_sw->con_save_screen(vc);
796}
797
bcd375f7
MS
798static void flush_scrollback(struct vc_data *vc)
799{
800 WARN_CONSOLE_UNLOCKED();
801
802 if (vc->vc_sw->con_flush_scrollback)
803 vc->vc_sw->con_flush_scrollback(vc);
804}
805
1da177e4
LT
806/*
807 * Redrawing of screen
808 */
809
2a248307 810void clear_buffer_attributes(struct vc_data *vc)
1da177e4
LT
811{
812 unsigned short *p = (unsigned short *)vc->vc_origin;
813 int count = vc->vc_screenbuf_size / 2;
814 int mask = vc->vc_hi_font_mask | 0xff;
815
816 for (; count > 0; count--, p++) {
817 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
818 }
819}
820
821void redraw_screen(struct vc_data *vc, int is_switch)
822{
823 int redraw = 0;
824
825 WARN_CONSOLE_UNLOCKED();
826
827 if (!vc) {
828 /* strange ... */
829 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
830 return;
831 }
832
833 if (is_switch) {
834 struct vc_data *old_vc = vc_cons[fg_console].d;
835 if (old_vc == vc)
836 return;
6ca8dfd7 837 if (!con_is_visible(vc))
1da177e4
LT
838 redraw = 1;
839 *vc->vc_display_fg = vc;
840 fg_console = vc->vc_num;
841 hide_cursor(old_vc);
6ca8dfd7 842 if (!con_is_visible(old_vc)) {
1da177e4
LT
843 save_screen(old_vc);
844 set_origin(old_vc);
845 }
fbc92a34
KS
846 if (tty0dev)
847 sysfs_notify(&tty0dev->kobj, NULL, "active");
1da177e4
LT
848 } else {
849 hide_cursor(vc);
850 redraw = 1;
851 }
852
853 if (redraw) {
854 int update;
855 int old_was_color = vc->vc_can_do_color;
856
857 set_origin(vc);
858 update = vc->vc_sw->con_switch(vc);
859 set_palette(vc);
860 /*
861 * If console changed from mono<->color, the best we can do
862 * is to clear the buffer attributes. As it currently stands,
863 * rebuilding new attributes from the old buffer is not doable
864 * without overly complex code.
865 */
866 if (old_was_color != vc->vc_can_do_color) {
867 update_attr(vc);
868 clear_buffer_attributes(vc);
869 }
8fd4bd22
JB
870
871 /* Forcibly update if we're panicing */
872 if ((update && vc->vc_mode != KD_GRAPHICS) ||
873 vt_force_oops_output(vc))
1da177e4
LT
874 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
875 }
876 set_cursor(vc);
877 if (is_switch) {
878 set_leds();
879 compute_shiftstate();
8182ec49 880 notify_update(vc);
1da177e4
LT
881 }
882}
883
884/*
885 * Allocation, freeing and resizing of VTs.
886 */
887
888int vc_cons_allocated(unsigned int i)
889{
890 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
891}
892
893static void visual_init(struct vc_data *vc, int num, int init)
894{
895 /* ++Geert: vc->vc_sw->con_init determines console size */
896 if (vc->vc_sw)
897 module_put(vc->vc_sw->owner);
898 vc->vc_sw = conswitchp;
899#ifndef VT_SINGLE_DRIVER
900 if (con_driver_map[num])
901 vc->vc_sw = con_driver_map[num];
902#endif
903 __module_get(vc->vc_sw->owner);
904 vc->vc_num = num;
905 vc->vc_display_fg = &master_display_fg;
08b33249
DZ
906 if (vc->vc_uni_pagedir_loc)
907 con_free_unimap(vc);
1da177e4 908 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
e4bdab70 909 vc->vc_uni_pagedir = NULL;
1da177e4
LT
910 vc->vc_hi_font_mask = 0;
911 vc->vc_complement_mask = 0;
912 vc->vc_can_do_color = 0;
8fd4bd22 913 vc->vc_panic_force_write = false;
1b45996d 914 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
915 vc->vc_sw->con_init(vc, init);
916 if (!vc->vc_complement_mask)
917 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
918 vc->vc_s_complement_mask = vc->vc_complement_mask;
919 vc->vc_size_row = vc->vc_cols << 1;
920 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
921}
922
923int vc_allocate(unsigned int currcons) /* return 0 on success */
924{
34902b7f
JS
925 struct vt_notifier_param param;
926 struct vc_data *vc;
927
1da177e4
LT
928 WARN_CONSOLE_UNLOCKED();
929
930 if (currcons >= MAX_NR_CONSOLES)
931 return -ENXIO;
34902b7f
JS
932
933 if (vc_cons[currcons].d)
934 return 0;
935
936 /* due to the granularity of kmalloc, we waste some memory here */
937 /* the alloc is done in two steps, to optimize the common situation
938 of a 25x80 console (structsize=216, screenbuf_size=4000) */
939 /* although the numbers above are not valid since long ago, the
940 point is still up-to-date and the comment still has its value
941 even if only as a historical artifact. --mj, July 1998 */
942 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
943 if (!vc)
1da177e4 944 return -ENOMEM;
34902b7f
JS
945
946 vc_cons[currcons].d = vc;
947 tty_port_init(&vc->port);
948 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
949
950 visual_init(vc, currcons, 1);
951
952 if (!*vc->vc_uni_pagedir_loc)
1da177e4 953 con_set_default_unimap(vc);
f6c06b68 954
34902b7f
JS
955 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
956 if (!vc->vc_screenbuf)
957 goto err_free;
958
959 /* If no drivers have overridden us and the user didn't pass a
960 boot option, default to displaying the cursor */
961 if (global_cursor_default == -1)
962 global_cursor_default = 1;
963
964 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
965 vcs_make_sysfs(currcons);
966 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
f6c06b68 967
1da177e4 968 return 0;
34902b7f
JS
969err_free:
970 kfree(vc);
971 vc_cons[currcons].d = NULL;
972 return -ENOMEM;
1da177e4
LT
973}
974
e400b6ec
AD
975static inline int resize_screen(struct vc_data *vc, int width, int height,
976 int user)
1da177e4
LT
977{
978 /* Resizes the resolution of the display adapater */
979 int err = 0;
980
981 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
e400b6ec
AD
982 err = vc->vc_sw->con_resize(vc, width, height, user);
983
1da177e4
LT
984 return err;
985}
986
987/*
988 * Change # of rows and columns (0 means unchanged/the size of fg_console)
989 * [this is to be used together with some user program
990 * like resize that changes the hardware videomode]
991 */
992#define VC_RESIZE_MAXCOL (32767)
993#define VC_RESIZE_MAXROW (32767)
8c9a9dd0
AC
994
995/**
996 * vc_do_resize - resizing method for the tty
997 * @tty: tty being resized
998 * @real_tty: real tty (different to tty if a pty/tty pair)
999 * @vc: virtual console private data
1000 * @cols: columns
1001 * @lines: lines
1002 *
1003 * Resize a virtual console, clipping according to the actual constraints.
1004 * If the caller passes a tty structure then update the termios winsize
3ad2f3fb 1005 * information and perform any necessary signal handling.
8c9a9dd0 1006 *
6a1c0680 1007 * Caller must hold the console semaphore. Takes the termios rwsem and
8c9a9dd0
AC
1008 * ctrl_lock of the tty IFF a tty is passed.
1009 */
1010
fc6f6238
AC
1011static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
1012 unsigned int cols, unsigned int lines)
1da177e4
LT
1013{
1014 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
9e0ba741 1015 unsigned long end;
d8ae7242 1016 unsigned int old_rows, old_row_size, first_copied_row;
1da177e4 1017 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
9e0ba741 1018 unsigned int user;
1da177e4 1019 unsigned short *newscreen;
d8ae7242 1020 struct uni_screen *new_uniscr = NULL;
1da177e4
LT
1021
1022 WARN_CONSOLE_UNLOCKED();
1023
1024 if (!vc)
1025 return -ENXIO;
1026
e400b6ec
AD
1027 user = vc->vc_resize_user;
1028 vc->vc_resize_user = 0;
1029
1da177e4
LT
1030 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
1031 return -EINVAL;
1032
1033 new_cols = (cols ? cols : vc->vc_cols);
1034 new_rows = (lines ? lines : vc->vc_rows);
1035 new_row_size = new_cols << 1;
1036 new_screen_size = new_row_size * new_rows;
1037
1038 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
1039 return 0;
1040
32b2921e
DV
1041 if (new_screen_size > (4 << 20))
1042 return -EINVAL;
5cbded58 1043 newscreen = kmalloc(new_screen_size, GFP_USER);
1da177e4
LT
1044 if (!newscreen)
1045 return -ENOMEM;
1046
d8ae7242
NP
1047 if (get_vc_uniscr(vc)) {
1048 new_uniscr = vc_uniscr_alloc(new_cols, new_rows);
1049 if (!new_uniscr) {
1050 kfree(newscreen);
1051 return -ENOMEM;
1052 }
1053 }
1054
009e39ae
SD
1055 if (vc == sel_cons)
1056 clear_selection();
1057
1da177e4 1058 old_rows = vc->vc_rows;
1da177e4 1059 old_row_size = vc->vc_size_row;
1da177e4 1060
e400b6ec 1061 err = resize_screen(vc, new_cols, new_rows, user);
1da177e4
LT
1062 if (err) {
1063 kfree(newscreen);
d8ae7242 1064 kfree(new_uniscr);
1da177e4
LT
1065 return err;
1066 }
1067
1068 vc->vc_rows = new_rows;
1069 vc->vc_cols = new_cols;
1070 vc->vc_size_row = new_row_size;
1071 vc->vc_screenbuf_size = new_screen_size;
1072
1073 rlth = min(old_row_size, new_row_size);
1074 rrem = new_row_size - rlth;
1075 old_origin = vc->vc_origin;
1076 new_origin = (long) newscreen;
1077 new_scr_end = new_origin + new_screen_size;
3b41dc1a
AD
1078
1079 if (vc->vc_y > new_rows) {
1080 if (old_rows - vc->vc_y < new_rows) {
1081 /*
1082 * Cursor near the bottom, copy contents from the
1083 * bottom of buffer
1084 */
d8ae7242 1085 first_copied_row = (old_rows - new_rows);
3b41dc1a
AD
1086 } else {
1087 /*
1088 * Cursor is in no man's land, copy 1/2 screenful
1089 * from the top and bottom of cursor position
1090 */
d8ae7242 1091 first_copied_row = (vc->vc_y - new_rows/2);
3b41dc1a 1092 }
d8ae7242
NP
1093 old_origin += first_copied_row * old_row_size;
1094 } else
1095 first_copied_row = 0;
9fc2b2d0 1096 end = old_origin + old_row_size * min(old_rows, new_rows);
1da177e4 1097
d8ae7242
NP
1098 vc_uniscr_copy_area(new_uniscr, new_cols, new_rows,
1099 get_vc_uniscr(vc), rlth/2, first_copied_row,
1100 min(old_rows, new_rows));
1101 vc_uniscr_set(vc, new_uniscr);
1102
1da177e4
LT
1103 update_attr(vc);
1104
3b41dc1a
AD
1105 while (old_origin < end) {
1106 scr_memcpyw((unsigned short *) new_origin,
1107 (unsigned short *) old_origin, rlth);
1da177e4 1108 if (rrem)
3b41dc1a
AD
1109 scr_memsetw((void *)(new_origin + rlth),
1110 vc->vc_video_erase_char, rrem);
1da177e4
LT
1111 old_origin += old_row_size;
1112 new_origin += new_row_size;
1113 }
1114 if (new_scr_end > new_origin)
3b41dc1a
AD
1115 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
1116 new_scr_end - new_origin);
5c9228f0 1117 kfree(vc->vc_screenbuf);
1da177e4 1118 vc->vc_screenbuf = newscreen;
1da177e4
LT
1119 vc->vc_screenbuf_size = new_screen_size;
1120 set_origin(vc);
1121
1122 /* do part of a reset_terminal() */
1123 vc->vc_top = 0;
1124 vc->vc_bottom = vc->vc_rows;
1125 gotoxy(vc, vc->vc_x, vc->vc_y);
1126 save_cur(vc);
1127
8c9a9dd0
AC
1128 if (tty) {
1129 /* Rewrite the requested winsize data with the actual
1130 resulting sizes */
1131 struct winsize ws;
1da177e4
LT
1132 memset(&ws, 0, sizeof(ws));
1133 ws.ws_row = vc->vc_rows;
1134 ws.ws_col = vc->vc_cols;
1135 ws.ws_ypixel = vc->vc_scan_lines;
fc6f6238 1136 tty_do_resize(tty, &ws);
1da177e4
LT
1137 }
1138
6ca8dfd7 1139 if (con_is_visible(vc))
1da177e4 1140 update_screen(vc);
8b92e87d 1141 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
1da177e4
LT
1142 return err;
1143}
1144
8c9a9dd0
AC
1145/**
1146 * vc_resize - resize a VT
1147 * @vc: virtual console
1148 * @cols: columns
1149 * @rows: rows
1150 *
1151 * Resize a virtual console as seen from the console end of things. We
1152 * use the common vc_do_resize methods to update the structures. The
1153 * caller must hold the console sem to protect console internals and
8ce73264 1154 * vc->port.tty
8c9a9dd0
AC
1155 */
1156
1157int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
1158{
8ce73264 1159 return vc_do_resize(vc->port.tty, vc, cols, rows);
8c9a9dd0
AC
1160}
1161
1162/**
1163 * vt_resize - resize a VT
1164 * @tty: tty to resize
8c9a9dd0
AC
1165 * @ws: winsize attributes
1166 *
1167 * Resize a virtual terminal. This is called by the tty layer as we
1168 * register our own handler for resizing. The mutual helper does all
1169 * the actual work.
1170 *
1171 * Takes the console sem and the called methods then take the tty
6a1c0680 1172 * termios_rwsem and the tty ctrl_lock in that order.
8c9a9dd0 1173 */
da2bdf9a 1174static int vt_resize(struct tty_struct *tty, struct winsize *ws)
ca9bda00 1175{
8c9a9dd0
AC
1176 struct vc_data *vc = tty->driver_data;
1177 int ret;
ca9bda00 1178
ac751efa 1179 console_lock();
fc6f6238 1180 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
ac751efa 1181 console_unlock();
8c9a9dd0 1182 return ret;
ca9bda00 1183}
1da177e4 1184
421b40a6 1185struct vc_data *vc_deallocate(unsigned int currcons)
1da177e4 1186{
421b40a6
PH
1187 struct vc_data *vc = NULL;
1188
1da177e4
LT
1189 WARN_CONSOLE_UNLOCKED();
1190
1191 if (vc_cons_allocated(currcons)) {
421b40a6 1192 struct vt_notifier_param param;
4995f8ef 1193
421b40a6 1194 param.vc = vc = vc_cons[currcons].d;
b293d758 1195 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
4995f8ef 1196 vcs_remove_sysfs(currcons);
1da177e4 1197 vc->vc_sw->con_deinit(vc);
bde0d2c9 1198 put_pid(vc->vt_pid);
d459ec0b 1199 module_put(vc->vc_sw->owner);
d8ae7242 1200 vc_uniscr_set(vc, NULL);
5c9228f0 1201 kfree(vc->vc_screenbuf);
1da177e4
LT
1202 vc_cons[currcons].d = NULL;
1203 }
421b40a6 1204 return vc;
1da177e4
LT
1205}
1206
1207/*
1208 * VT102 emulator
1209 */
1210
079c9534
AC
1211#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1212#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1213#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
1da177e4
LT
1214
1215#define decarm VC_REPEAT
1216#define decckm VC_CKMODE
1217#define kbdapplic VC_APPLIC
1218#define lnm VC_CRLF
1219
1220/*
1221 * this is what the terminal answers to a ESC-Z or csi0c query.
1222 */
1223#define VT100ID "\033[?1;2c"
1224#define VT102ID "\033[?6c"
1225
8ede5cce 1226const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1da177e4
LT
1227 8,12,10,14, 9,13,11,15 };
1228
1229/* the default colour table, for VGA+ colour systems */
91e74ca5
JS
1230unsigned char default_red[] = {
1231 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1232 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1233};
1234module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
1235
1236unsigned char default_grn[] = {
1237 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1238 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1239};
1240module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1241
1242unsigned char default_blu[] = {
1243 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1244 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1245};
1246module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
1c2bbe6a 1247
1da177e4
LT
1248/*
1249 * gotoxy() must verify all boundaries, because the arguments
1250 * might also be negative. If the given position is out of
1251 * bounds, the cursor is placed at the nearest margin.
1252 */
1253static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1254{
1255 int min_y, max_y;
1256
1257 if (new_x < 0)
1258 vc->vc_x = 0;
1259 else {
1260 if (new_x >= vc->vc_cols)
1261 vc->vc_x = vc->vc_cols - 1;
1262 else
1263 vc->vc_x = new_x;
1264 }
1265
1266 if (vc->vc_decom) {
1267 min_y = vc->vc_top;
1268 max_y = vc->vc_bottom;
1269 } else {
1270 min_y = 0;
1271 max_y = vc->vc_rows;
1272 }
1273 if (new_y < min_y)
1274 vc->vc_y = min_y;
1275 else if (new_y >= max_y)
1276 vc->vc_y = max_y - 1;
1277 else
1278 vc->vc_y = new_y;
1279 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1280 vc->vc_need_wrap = 0;
1281}
1282
1283/* for absolute user moves, when decom is set */
1284static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1285{
1286 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1287}
1288
1b0ec88a 1289void scrollback(struct vc_data *vc)
1da177e4 1290{
1b0ec88a 1291 scrolldelta(-(vc->vc_rows / 2));
1da177e4
LT
1292}
1293
1294void scrollfront(struct vc_data *vc, int lines)
1295{
1296 if (!lines)
1297 lines = vc->vc_rows / 2;
1298 scrolldelta(lines);
1299}
1300
1301static void lf(struct vc_data *vc)
1302{
1303 /* don't scroll if above bottom of scrolling region, or
1304 * if below scrolling region
1305 */
1306 if (vc->vc_y + 1 == vc->vc_bottom)
89765b94 1307 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
1da177e4
LT
1308 else if (vc->vc_y < vc->vc_rows - 1) {
1309 vc->vc_y++;
1310 vc->vc_pos += vc->vc_size_row;
1311 }
1312 vc->vc_need_wrap = 0;
b293d758 1313 notify_write(vc, '\n');
1da177e4
LT
1314}
1315
1316static void ri(struct vc_data *vc)
1317{
1318 /* don't scroll if below top of scrolling region, or
1319 * if above scrolling region
1320 */
1321 if (vc->vc_y == vc->vc_top)
89765b94 1322 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
1da177e4
LT
1323 else if (vc->vc_y > 0) {
1324 vc->vc_y--;
1325 vc->vc_pos -= vc->vc_size_row;
1326 }
1327 vc->vc_need_wrap = 0;
1328}
1329
1330static inline void cr(struct vc_data *vc)
1331{
1332 vc->vc_pos -= vc->vc_x << 1;
1333 vc->vc_need_wrap = vc->vc_x = 0;
b293d758 1334 notify_write(vc, '\r');
1da177e4
LT
1335}
1336
1337static inline void bs(struct vc_data *vc)
1338{
1339 if (vc->vc_x) {
1340 vc->vc_pos -= 2;
1341 vc->vc_x--;
1342 vc->vc_need_wrap = 0;
b293d758 1343 notify_write(vc, '\b');
1da177e4
LT
1344 }
1345}
1346
1347static inline void del(struct vc_data *vc)
1348{
1349 /* ignored */
1350}
1351
1352static void csi_J(struct vc_data *vc, int vpar)
1353{
1354 unsigned int count;
1355 unsigned short * start;
1356
1357 switch (vpar) {
1358 case 0: /* erase from cursor to end of display */
d8ae7242
NP
1359 vc_uniscr_clear_line(vc, vc->vc_x,
1360 vc->vc_cols - vc->vc_x);
1361 vc_uniscr_clear_lines(vc, vc->vc_y + 1,
1362 vc->vc_rows - vc->vc_y - 1);
1da177e4
LT
1363 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1364 start = (unsigned short *)vc->vc_pos;
1da177e4
LT
1365 break;
1366 case 1: /* erase from start to cursor */
d8ae7242
NP
1367 vc_uniscr_clear_line(vc, 0, vc->vc_x + 1);
1368 vc_uniscr_clear_lines(vc, 0, vc->vc_y);
1da177e4
LT
1369 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1370 start = (unsigned short *)vc->vc_origin;
1da177e4
LT
1371 break;
1372 case 2: /* erase whole display */
4b4ecd9c 1373 case 3: /* (and scrollback buffer later) */
d8ae7242 1374 vc_uniscr_clear_lines(vc, 0, vc->vc_rows);
1da177e4
LT
1375 count = vc->vc_cols * vc->vc_rows;
1376 start = (unsigned short *)vc->vc_origin;
1da177e4
LT
1377 break;
1378 default:
1379 return;
1380 }
1381 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
4b4ecd9c
NP
1382 if (vpar == 3) {
1383 set_origin(vc);
1384 flush_scrollback(vc);
1385 if (con_is_visible(vc))
1386 update_screen(vc);
1387 } else if (con_should_update(vc))
81732c3b 1388 do_update_region(vc, (unsigned long) start, count);
1da177e4
LT
1389 vc->vc_need_wrap = 0;
1390}
1391
1392static void csi_K(struct vc_data *vc, int vpar)
1393{
1394 unsigned int count;
d8ae7242
NP
1395 unsigned short *start = (unsigned short *)vc->vc_pos;
1396 int offset;
1da177e4
LT
1397
1398 switch (vpar) {
1399 case 0: /* erase from cursor to end of line */
d8ae7242 1400 offset = 0;
1da177e4 1401 count = vc->vc_cols - vc->vc_x;
1da177e4
LT
1402 break;
1403 case 1: /* erase from start of line to cursor */
d8ae7242 1404 offset = -vc->vc_x;
1da177e4 1405 count = vc->vc_x + 1;
1da177e4
LT
1406 break;
1407 case 2: /* erase whole line */
d8ae7242 1408 offset = -vc->vc_x;
1da177e4 1409 count = vc->vc_cols;
1da177e4
LT
1410 break;
1411 default:
1412 return;
1413 }
d8ae7242
NP
1414 vc_uniscr_clear_line(vc, vc->vc_x + offset, count);
1415 scr_memsetw(start + offset, vc->vc_video_erase_char, 2 * count);
1da177e4 1416 vc->vc_need_wrap = 0;
6ca8dfd7 1417 if (con_should_update(vc))
81732c3b 1418 do_update_region(vc, (unsigned long) start, count);
1da177e4
LT
1419}
1420
1421static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1422{ /* not vt100? */
1423 int count;
1424
1425 if (!vpar)
1426 vpar++;
1427 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1428
d8ae7242 1429 vc_uniscr_clear_line(vc, vc->vc_x, count);
1da177e4 1430 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
6ca8dfd7 1431 if (con_should_update(vc))
1da177e4
LT
1432 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1433 vc->vc_need_wrap = 0;
1434}
1435
1436static void default_attr(struct vc_data *vc)
1437{
1438 vc->vc_intensity = 1;
fa6ce9ab 1439 vc->vc_italic = 0;
1da177e4
LT
1440 vc->vc_underline = 0;
1441 vc->vc_reverse = 0;
1442 vc->vc_blink = 0;
1443 vc->vc_color = vc->vc_def_color;
1444}
1445
cec5b2a9
AB
1446struct rgb { u8 r; u8 g; u8 b; };
1447
0f91e142 1448static void rgb_from_256(int i, struct rgb *c)
cec5b2a9 1449{
cec5b2a9 1450 if (i < 8) { /* Standard colours. */
0f91e142
JS
1451 c->r = i&1 ? 0xaa : 0x00;
1452 c->g = i&2 ? 0xaa : 0x00;
1453 c->b = i&4 ? 0xaa : 0x00;
cec5b2a9 1454 } else if (i < 16) {
0f91e142
JS
1455 c->r = i&1 ? 0xff : 0x55;
1456 c->g = i&2 ? 0xff : 0x55;
1457 c->b = i&4 ? 0xff : 0x55;
cec5b2a9 1458 } else if (i < 232) { /* 6x6x6 colour cube. */
0f91e142
JS
1459 c->r = (i - 16) / 36 * 85 / 2;
1460 c->g = (i - 16) / 6 % 6 * 85 / 2;
1461 c->b = (i - 16) % 6 * 85 / 2;
cec5b2a9 1462 } else /* Grayscale ramp. */
0f91e142 1463 c->r = c->g = c->b = i * 10 - 2312;
cec5b2a9
AB
1464}
1465
0f91e142 1466static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
cec5b2a9 1467{
193df022
JS
1468 u8 hue = 0, max = max3(c->r, c->g, c->b);
1469
1470 if (c->r > max / 2)
1471 hue |= 4;
1472 if (c->g > max / 2)
1473 hue |= 2;
1474 if (c->b > max / 2)
1475 hue |= 1;
1476
1477 if (hue == 7 && max <= 0x55) {
1478 hue = 0;
1479 vc->vc_intensity = 2;
1480 } else if (max > 0xaa)
1481 vc->vc_intensity = 2;
cec5b2a9 1482 else
193df022
JS
1483 vc->vc_intensity = 1;
1484
cec5b2a9
AB
1485 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1486}
1487
0f91e142 1488static void rgb_background(struct vc_data *vc, const struct rgb *c)
cec5b2a9
AB
1489{
1490 /* For backgrounds, err on the dark side. */
1491 vc->vc_color = (vc->vc_color & 0x0f)
0f91e142 1492 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
cec5b2a9
AB
1493}
1494
e05ab238
JS
1495/*
1496 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1497 * and thus need to be detected and ignored by hand. Strictly speaking, that
1498 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1499 * no one produces such codes and almost no one accepts them.
1500 *
1501 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1502 * supporting them.
1503 */
1504static int vc_t416_color(struct vc_data *vc, int i,
0f91e142 1505 void(*set_color)(struct vc_data *vc, const struct rgb *c))
e05ab238 1506{
0f91e142
JS
1507 struct rgb c;
1508
e05ab238
JS
1509 i++;
1510 if (i > vc->vc_npar)
1511 return i;
1512
0bf1f4a8 1513 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
3e7ec4a0 1514 /* 256 colours */
e05ab238 1515 i++;
0f91e142 1516 rgb_from_256(vc->vc_par[i], &c);
669e0a51 1517 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
3e7ec4a0 1518 /* 24 bit */
0f91e142
JS
1519 c.r = vc->vc_par[i + 1];
1520 c.g = vc->vc_par[i + 2];
1521 c.b = vc->vc_par[i + 3];
e05ab238 1522 i += 3;
0f91e142
JS
1523 } else
1524 return i;
1525
1526 set_color(vc, &c);
e05ab238
JS
1527
1528 return i;
1529}
1530
ac751efa 1531/* console_lock is held */
1da177e4
LT
1532static void csi_m(struct vc_data *vc)
1533{
1534 int i;
1535
1536 for (i = 0; i <= vc->vc_npar; i++)
1537 switch (vc->vc_par[i]) {
aada0a34
JS
1538 case 0: /* all attributes off */
1539 default_attr(vc);
1540 break;
1541 case 1:
1542 vc->vc_intensity = 2;
1543 break;
1544 case 2:
1545 vc->vc_intensity = 0;
1546 break;
1547 case 3:
1548 vc->vc_italic = 1;
1549 break;
65d9982d
MF
1550 case 21:
1551 /*
1552 * No console drivers support double underline, so
1553 * convert it to a single underline.
1554 */
aada0a34
JS
1555 case 4:
1556 vc->vc_underline = 1;
1557 break;
1558 case 5:
1559 vc->vc_blink = 1;
1560 break;
1561 case 7:
1562 vc->vc_reverse = 1;
1563 break;
1564 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1565 * Select primary font, don't display control chars if
1566 * defined, don't set bit 8 on output.
1567 */
1568 vc->vc_translate = set_translate(vc->vc_charset == 0
1569 ? vc->vc_G0_charset
1570 : vc->vc_G1_charset, vc);
1571 vc->vc_disp_ctrl = 0;
1572 vc->vc_toggle_meta = 0;
1573 break;
1574 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1575 * Select first alternate font, lets chars < 32 be
1576 * displayed as ROM chars.
1577 */
1578 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1579 vc->vc_disp_ctrl = 1;
1580 vc->vc_toggle_meta = 0;
1581 break;
1582 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1583 * Select second alternate font, toggle high bit
1584 * before displaying as ROM char.
1585 */
1586 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1587 vc->vc_disp_ctrl = 1;
1588 vc->vc_toggle_meta = 1;
1589 break;
aada0a34
JS
1590 case 22:
1591 vc->vc_intensity = 1;
1592 break;
1593 case 23:
1594 vc->vc_italic = 0;
1595 break;
1596 case 24:
1597 vc->vc_underline = 0;
1598 break;
1599 case 25:
1600 vc->vc_blink = 0;
1601 break;
1602 case 27:
1603 vc->vc_reverse = 0;
1604 break;
1605 case 38:
1606 i = vc_t416_color(vc, i, rgb_foreground);
1607 break;
1608 case 48:
1609 i = vc_t416_color(vc, i, rgb_background);
1610 break;
1611 case 39:
1612 vc->vc_color = (vc->vc_def_color & 0x0f) |
1613 (vc->vc_color & 0xf0);
1614 break;
1615 case 49:
1616 vc->vc_color = (vc->vc_def_color & 0xf0) |
1617 (vc->vc_color & 0x0f);
1618 break;
1619 default:
fadb4244
AB
1620 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1621 if (vc->vc_par[i] < 100)
1622 vc->vc_intensity = 2;
cc67dc28
AB
1623 vc->vc_par[i] -= 60;
1624 }
aada0a34
JS
1625 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1626 vc->vc_color = color_table[vc->vc_par[i] - 30]
1627 | (vc->vc_color & 0xf0);
1628 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1629 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1630 | (vc->vc_color & 0x0f);
1631 break;
1da177e4
LT
1632 }
1633 update_attr(vc);
1634}
1635
6732c8bb 1636static void respond_string(const char *p, struct tty_port *port)
1da177e4
LT
1637{
1638 while (*p) {
6732c8bb 1639 tty_insert_flip_char(port, *p, 0);
1da177e4
LT
1640 p++;
1641 }
6732c8bb 1642 tty_schedule_flip(port);
1da177e4
LT
1643}
1644
1645static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1646{
1647 char buf[40];
1648
1649 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
6732c8bb 1650 respond_string(buf, tty->port);
1da177e4
LT
1651}
1652
1653static inline void status_report(struct tty_struct *tty)
1654{
6732c8bb 1655 respond_string("\033[0n", tty->port); /* Terminal ok */
1da177e4
LT
1656}
1657
6732c8bb 1658static inline void respond_ID(struct tty_struct *tty)
1da177e4 1659{
6732c8bb 1660 respond_string(VT102ID, tty->port);
1da177e4
LT
1661}
1662
1663void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1664{
1665 char buf[8];
1666
1667 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1668 (char)('!' + mry));
6732c8bb 1669 respond_string(buf, tty->port);
1da177e4
LT
1670}
1671
1672/* invoked via ioctl(TIOCLINUX) and through set_selection */
1673int mouse_reporting(void)
1674{
1675 return vc_cons[fg_console].d->vc_report_mouse;
1676}
1677
ac751efa 1678/* console_lock is held */
1da177e4
LT
1679static void set_mode(struct vc_data *vc, int on_off)
1680{
1681 int i;
1682
1683 for (i = 0; i <= vc->vc_npar; i++)
1684 if (vc->vc_ques) {
1685 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1686 case 1: /* Cursor keys send ^[Ox/^[[x */
1687 if (on_off)
1688 set_kbd(vc, decckm);
1689 else
1690 clr_kbd(vc, decckm);
1691 break;
1692 case 3: /* 80/132 mode switch unimplemented */
1da177e4
LT
1693#if 0
1694 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1695 /* this alone does not suffice; some user mode
1696 utility has to change the hardware regs */
1697#endif
1698 break;
1699 case 5: /* Inverted screen on/off */
1700 if (vc->vc_decscnm != on_off) {
1701 vc->vc_decscnm = on_off;
1702 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1703 update_attr(vc);
1704 }
1705 break;
1706 case 6: /* Origin relative/absolute */
1707 vc->vc_decom = on_off;
1708 gotoxay(vc, 0, 0);
1709 break;
1710 case 7: /* Autowrap on/off */
1711 vc->vc_decawm = on_off;
1712 break;
1713 case 8: /* Autorepeat on/off */
1714 if (on_off)
1715 set_kbd(vc, decarm);
1716 else
1717 clr_kbd(vc, decarm);
1718 break;
1719 case 9:
1720 vc->vc_report_mouse = on_off ? 1 : 0;
1721 break;
1722 case 25: /* Cursor on/off */
1723 vc->vc_deccm = on_off;
1724 break;
1725 case 1000:
1726 vc->vc_report_mouse = on_off ? 2 : 0;
1727 break;
1728 }
1729 } else {
1730 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1731 case 3: /* Monitor (display ctrls) */
1732 vc->vc_disp_ctrl = on_off;
1733 break;
1734 case 4: /* Insert Mode on/off */
1735 vc->vc_decim = on_off;
1736 break;
1737 case 20: /* Lf, Enter == CrLf/Lf */
1738 if (on_off)
1739 set_kbd(vc, lnm);
1740 else
1741 clr_kbd(vc, lnm);
1742 break;
1743 }
1744 }
1745}
1746
ac751efa 1747/* console_lock is held */
1da177e4
LT
1748static void setterm_command(struct vc_data *vc)
1749{
1750 switch(vc->vc_par[0]) {
1751 case 1: /* set color for underline mode */
1752 if (vc->vc_can_do_color &&
1753 vc->vc_par[1] < 16) {
1754 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1755 if (vc->vc_underline)
1756 update_attr(vc);
1757 }
1758 break;
1759 case 2: /* set color for half intensity mode */
1760 if (vc->vc_can_do_color &&
1761 vc->vc_par[1] < 16) {
1762 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1763 if (vc->vc_intensity == 0)
1764 update_attr(vc);
1765 }
1766 break;
1767 case 8: /* store colors as defaults */
1768 vc->vc_def_color = vc->vc_attr;
1769 if (vc->vc_hi_font_mask == 0x100)
1770 vc->vc_def_color >>= 1;
1771 default_attr(vc);
1772 update_attr(vc);
1773 break;
1774 case 9: /* set blanking interval */
f324edc8 1775 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1da177e4
LT
1776 poke_blanked_console();
1777 break;
1778 case 10: /* set bell frequency in Hz */
1779 if (vc->vc_npar >= 1)
1780 vc->vc_bell_pitch = vc->vc_par[1];
1781 else
1782 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1783 break;
1784 case 11: /* set bell duration in msec */
1785 if (vc->vc_npar >= 1)
1786 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
3372ec28 1787 msecs_to_jiffies(vc->vc_par[1]) : 0;
1da177e4
LT
1788 else
1789 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1790 break;
1791 case 12: /* bring specified console to the front */
1792 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1793 set_console(vc->vc_par[1] - 1);
1794 break;
1795 case 13: /* unblank the screen */
1796 poke_blanked_console();
1797 break;
1798 case 14: /* set vesa powerdown interval */
1799 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1800 break;
1801 case 15: /* activate the previous console */
1802 set_console(last_console);
1803 break;
bd63364c
SD
1804 case 16: /* set cursor blink duration in msec */
1805 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1806 vc->vc_par[1] <= USHRT_MAX)
1807 vc->vc_cur_blink_ms = vc->vc_par[1];
1808 else
1809 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1810 break;
1da177e4
LT
1811 }
1812}
1813
ac751efa 1814/* console_lock is held */
1da177e4
LT
1815static void csi_at(struct vc_data *vc, unsigned int nr)
1816{
1817 if (nr > vc->vc_cols - vc->vc_x)
1818 nr = vc->vc_cols - vc->vc_x;
1819 else if (!nr)
1820 nr = 1;
1821 insert_char(vc, nr);
1822}
1823
ac751efa 1824/* console_lock is held */
1da177e4
LT
1825static void csi_L(struct vc_data *vc, unsigned int nr)
1826{
1827 if (nr > vc->vc_rows - vc->vc_y)
1828 nr = vc->vc_rows - vc->vc_y;
1829 else if (!nr)
1830 nr = 1;
89765b94 1831 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_DOWN, nr);
1da177e4
LT
1832 vc->vc_need_wrap = 0;
1833}
1834
ac751efa 1835/* console_lock is held */
1da177e4
LT
1836static void csi_P(struct vc_data *vc, unsigned int nr)
1837{
1838 if (nr > vc->vc_cols - vc->vc_x)
1839 nr = vc->vc_cols - vc->vc_x;
1840 else if (!nr)
1841 nr = 1;
1842 delete_char(vc, nr);
1843}
1844
ac751efa 1845/* console_lock is held */
1da177e4
LT
1846static void csi_M(struct vc_data *vc, unsigned int nr)
1847{
1848 if (nr > vc->vc_rows - vc->vc_y)
1849 nr = vc->vc_rows - vc->vc_y;
1850 else if (!nr)
1851 nr=1;
89765b94 1852 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_UP, nr);
1da177e4
LT
1853 vc->vc_need_wrap = 0;
1854}
1855
ac751efa 1856/* console_lock is held (except via vc_init->reset_terminal */
1da177e4
LT
1857static void save_cur(struct vc_data *vc)
1858{
1859 vc->vc_saved_x = vc->vc_x;
1860 vc->vc_saved_y = vc->vc_y;
1861 vc->vc_s_intensity = vc->vc_intensity;
fa6ce9ab 1862 vc->vc_s_italic = vc->vc_italic;
1da177e4
LT
1863 vc->vc_s_underline = vc->vc_underline;
1864 vc->vc_s_blink = vc->vc_blink;
1865 vc->vc_s_reverse = vc->vc_reverse;
1866 vc->vc_s_charset = vc->vc_charset;
1867 vc->vc_s_color = vc->vc_color;
1868 vc->vc_saved_G0 = vc->vc_G0_charset;
1869 vc->vc_saved_G1 = vc->vc_G1_charset;
1870}
1871
ac751efa 1872/* console_lock is held */
1da177e4
LT
1873static void restore_cur(struct vc_data *vc)
1874{
1875 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1876 vc->vc_intensity = vc->vc_s_intensity;
fa6ce9ab 1877 vc->vc_italic = vc->vc_s_italic;
1da177e4
LT
1878 vc->vc_underline = vc->vc_s_underline;
1879 vc->vc_blink = vc->vc_s_blink;
1880 vc->vc_reverse = vc->vc_s_reverse;
1881 vc->vc_charset = vc->vc_s_charset;
1882 vc->vc_color = vc->vc_s_color;
1883 vc->vc_G0_charset = vc->vc_saved_G0;
1884 vc->vc_G1_charset = vc->vc_saved_G1;
1885 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1886 update_attr(vc);
1887 vc->vc_need_wrap = 0;
1888}
1889
b290af68 1890enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
1da177e4 1891 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
63f3a16d 1892 ESpalette, ESosc };
1da177e4 1893
ac751efa 1894/* console_lock is held (except via vc_init()) */
1da177e4
LT
1895static void reset_terminal(struct vc_data *vc, int do_clear)
1896{
1897 vc->vc_top = 0;
1898 vc->vc_bottom = vc->vc_rows;
1899 vc->vc_state = ESnormal;
1900 vc->vc_ques = 0;
1901 vc->vc_translate = set_translate(LAT1_MAP, vc);
1902 vc->vc_G0_charset = LAT1_MAP;
1903 vc->vc_G1_charset = GRAF_MAP;
1904 vc->vc_charset = 0;
1905 vc->vc_need_wrap = 0;
1906 vc->vc_report_mouse = 0;
042f10ec 1907 vc->vc_utf = default_utf8;
1da177e4
LT
1908 vc->vc_utf_count = 0;
1909
1910 vc->vc_disp_ctrl = 0;
1911 vc->vc_toggle_meta = 0;
1912
1913 vc->vc_decscnm = 0;
1914 vc->vc_decom = 0;
1915 vc->vc_decawm = 1;
f6c06b68 1916 vc->vc_deccm = global_cursor_default;
1da177e4
LT
1917 vc->vc_decim = 0;
1918
079c9534 1919 vt_reset_keyboard(vc->vc_num);
1da177e4 1920
9ea9a886 1921 vc->vc_cursor_type = cur_default;
1da177e4
LT
1922 vc->vc_complement_mask = vc->vc_s_complement_mask;
1923
1924 default_attr(vc);
1925 update_attr(vc);
1926
f1869a89 1927 vc->vc_tab_stop[0] =
1da177e4
LT
1928 vc->vc_tab_stop[1] =
1929 vc->vc_tab_stop[2] =
1930 vc->vc_tab_stop[3] =
a564738c
WK
1931 vc->vc_tab_stop[4] =
1932 vc->vc_tab_stop[5] =
1933 vc->vc_tab_stop[6] =
1934 vc->vc_tab_stop[7] = 0x01010101;
1da177e4
LT
1935
1936 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1937 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
bd63364c 1938 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
1939
1940 gotoxy(vc, 0, 0);
1941 save_cur(vc);
1942 if (do_clear)
1943 csi_J(vc, 2);
1944}
1945
ac751efa 1946/* console_lock is held */
1da177e4
LT
1947static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1948{
1949 /*
1950 * Control characters can be used in the _middle_
1951 * of an escape sequence.
1952 */
63f3a16d
AB
1953 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1954 return;
1da177e4
LT
1955 switch (c) {
1956 case 0:
1957 return;
1958 case 7:
63f3a16d
AB
1959 if (vc->vc_state == ESosc)
1960 vc->vc_state = ESnormal;
1961 else if (vc->vc_bell_duration)
1da177e4
LT
1962 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1963 return;
1964 case 8:
1965 bs(vc);
1966 return;
1967 case 9:
1968 vc->vc_pos -= (vc->vc_x << 1);
1969 while (vc->vc_x < vc->vc_cols - 1) {
1970 vc->vc_x++;
f1869a89 1971 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
1da177e4
LT
1972 break;
1973 }
1974 vc->vc_pos += (vc->vc_x << 1);
b293d758 1975 notify_write(vc, '\t');
1da177e4
LT
1976 return;
1977 case 10: case 11: case 12:
1978 lf(vc);
1979 if (!is_kbd(vc, lnm))
1980 return;
1981 case 13:
1982 cr(vc);
1983 return;
1984 case 14:
1985 vc->vc_charset = 1;
1986 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1987 vc->vc_disp_ctrl = 1;
1988 return;
1989 case 15:
1990 vc->vc_charset = 0;
1991 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1992 vc->vc_disp_ctrl = 0;
1993 return;
1994 case 24: case 26:
1995 vc->vc_state = ESnormal;
1996 return;
1997 case 27:
1998 vc->vc_state = ESesc;
1999 return;
2000 case 127:
2001 del(vc);
2002 return;
2003 case 128+27:
2004 vc->vc_state = ESsquare;
2005 return;
2006 }
2007 switch(vc->vc_state) {
2008 case ESesc:
2009 vc->vc_state = ESnormal;
2010 switch (c) {
2011 case '[':
2012 vc->vc_state = ESsquare;
2013 return;
2014 case ']':
2015 vc->vc_state = ESnonstd;
2016 return;
2017 case '%':
2018 vc->vc_state = ESpercent;
2019 return;
2020 case 'E':
2021 cr(vc);
2022 lf(vc);
2023 return;
2024 case 'M':
2025 ri(vc);
2026 return;
2027 case 'D':
2028 lf(vc);
2029 return;
2030 case 'H':
f1869a89 2031 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
1da177e4
LT
2032 return;
2033 case 'Z':
2034 respond_ID(tty);
2035 return;
2036 case '7':
2037 save_cur(vc);
2038 return;
2039 case '8':
2040 restore_cur(vc);
2041 return;
2042 case '(':
2043 vc->vc_state = ESsetG0;
2044 return;
2045 case ')':
2046 vc->vc_state = ESsetG1;
2047 return;
2048 case '#':
2049 vc->vc_state = EShash;
2050 return;
2051 case 'c':
2052 reset_terminal(vc, 1);
2053 return;
2054 case '>': /* Numeric keypad */
2055 clr_kbd(vc, kbdapplic);
2056 return;
2057 case '=': /* Appl. keypad */
2058 set_kbd(vc, kbdapplic);
2059 return;
2060 }
2061 return;
2062 case ESnonstd:
2063 if (c=='P') { /* palette escape sequence */
2064 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2065 vc->vc_par[vc->vc_npar] = 0;
2066 vc->vc_npar = 0;
2067 vc->vc_state = ESpalette;
2068 return;
2069 } else if (c=='R') { /* reset palette */
2070 reset_palette(vc);
2071 vc->vc_state = ESnormal;
63f3a16d
AB
2072 } else if (c>='0' && c<='9')
2073 vc->vc_state = ESosc;
2074 else
1da177e4
LT
2075 vc->vc_state = ESnormal;
2076 return;
2077 case ESpalette:
74c807ce
AS
2078 if (isxdigit(c)) {
2079 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1da177e4
LT
2080 if (vc->vc_npar == 7) {
2081 int i = vc->vc_par[0] * 3, j = 1;
2082 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2083 vc->vc_palette[i++] += vc->vc_par[j++];
2084 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2085 vc->vc_palette[i++] += vc->vc_par[j++];
2086 vc->vc_palette[i] = 16 * vc->vc_par[j++];
2087 vc->vc_palette[i] += vc->vc_par[j];
2088 set_palette(vc);
2089 vc->vc_state = ESnormal;
2090 }
2091 } else
2092 vc->vc_state = ESnormal;
2093 return;
2094 case ESsquare:
2095 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
2096 vc->vc_par[vc->vc_npar] = 0;
2097 vc->vc_npar = 0;
2098 vc->vc_state = ESgetpars;
2099 if (c == '[') { /* Function key */
2100 vc->vc_state=ESfunckey;
2101 return;
2102 }
2103 vc->vc_ques = (c == '?');
2104 if (vc->vc_ques)
2105 return;
2106 case ESgetpars:
2107 if (c == ';' && vc->vc_npar < NPAR - 1) {
2108 vc->vc_npar++;
2109 return;
2110 } else if (c>='0' && c<='9') {
2111 vc->vc_par[vc->vc_npar] *= 10;
2112 vc->vc_par[vc->vc_npar] += c - '0';
2113 return;
b290af68 2114 }
1da177e4
LT
2115 vc->vc_state = ESnormal;
2116 switch(c) {
2117 case 'h':
2118 set_mode(vc, 1);
2119 return;
2120 case 'l':
2121 set_mode(vc, 0);
2122 return;
2123 case 'c':
2124 if (vc->vc_ques) {
2125 if (vc->vc_par[0])
2126 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
2127 else
9ea9a886 2128 vc->vc_cursor_type = cur_default;
1da177e4
LT
2129 return;
2130 }
2131 break;
2132 case 'm':
2133 if (vc->vc_ques) {
2134 clear_selection();
2135 if (vc->vc_par[0])
2136 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
2137 else
2138 vc->vc_complement_mask = vc->vc_s_complement_mask;
2139 return;
2140 }
2141 break;
2142 case 'n':
2143 if (!vc->vc_ques) {
2144 if (vc->vc_par[0] == 5)
2145 status_report(tty);
2146 else if (vc->vc_par[0] == 6)
2147 cursor_report(vc, tty);
2148 }
2149 return;
2150 }
2151 if (vc->vc_ques) {
2152 vc->vc_ques = 0;
2153 return;
2154 }
2155 switch(c) {
2156 case 'G': case '`':
2157 if (vc->vc_par[0])
2158 vc->vc_par[0]--;
2159 gotoxy(vc, vc->vc_par[0], vc->vc_y);
2160 return;
2161 case 'A':
2162 if (!vc->vc_par[0])
2163 vc->vc_par[0]++;
2164 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
2165 return;
2166 case 'B': case 'e':
2167 if (!vc->vc_par[0])
2168 vc->vc_par[0]++;
2169 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
2170 return;
2171 case 'C': case 'a':
2172 if (!vc->vc_par[0])
2173 vc->vc_par[0]++;
2174 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
2175 return;
2176 case 'D':
2177 if (!vc->vc_par[0])
2178 vc->vc_par[0]++;
2179 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
2180 return;
2181 case 'E':
2182 if (!vc->vc_par[0])
2183 vc->vc_par[0]++;
2184 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
2185 return;
2186 case 'F':
2187 if (!vc->vc_par[0])
2188 vc->vc_par[0]++;
2189 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
2190 return;
2191 case 'd':
2192 if (vc->vc_par[0])
2193 vc->vc_par[0]--;
2194 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2195 return;
2196 case 'H': case 'f':
2197 if (vc->vc_par[0])
2198 vc->vc_par[0]--;
2199 if (vc->vc_par[1])
2200 vc->vc_par[1]--;
2201 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2202 return;
2203 case 'J':
2204 csi_J(vc, vc->vc_par[0]);
2205 return;
2206 case 'K':
2207 csi_K(vc, vc->vc_par[0]);
2208 return;
2209 case 'L':
2210 csi_L(vc, vc->vc_par[0]);
2211 return;
2212 case 'M':
2213 csi_M(vc, vc->vc_par[0]);
2214 return;
2215 case 'P':
2216 csi_P(vc, vc->vc_par[0]);
2217 return;
2218 case 'c':
2219 if (!vc->vc_par[0])
2220 respond_ID(tty);
2221 return;
2222 case 'g':
2223 if (!vc->vc_par[0])
f1869a89 2224 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
1da177e4
LT
2225 else if (vc->vc_par[0] == 3) {
2226 vc->vc_tab_stop[0] =
2227 vc->vc_tab_stop[1] =
2228 vc->vc_tab_stop[2] =
2229 vc->vc_tab_stop[3] =
a564738c
WK
2230 vc->vc_tab_stop[4] =
2231 vc->vc_tab_stop[5] =
2232 vc->vc_tab_stop[6] =
2233 vc->vc_tab_stop[7] = 0;
1da177e4
LT
2234 }
2235 return;
2236 case 'm':
2237 csi_m(vc);
2238 return;
2239 case 'q': /* DECLL - but only 3 leds */
2240 /* map 0,1,2,3 to 0,1,2,4 */
2241 if (vc->vc_par[0] < 4)
079c9534 2242 vt_set_led_state(vc->vc_num,
1da177e4
LT
2243 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2244 return;
2245 case 'r':
2246 if (!vc->vc_par[0])
2247 vc->vc_par[0]++;
2248 if (!vc->vc_par[1])
2249 vc->vc_par[1] = vc->vc_rows;
2250 /* Minimum allowed region is 2 lines */
2251 if (vc->vc_par[0] < vc->vc_par[1] &&
2252 vc->vc_par[1] <= vc->vc_rows) {
2253 vc->vc_top = vc->vc_par[0] - 1;
2254 vc->vc_bottom = vc->vc_par[1];
2255 gotoxay(vc, 0, 0);
2256 }
2257 return;
2258 case 's':
2259 save_cur(vc);
2260 return;
2261 case 'u':
2262 restore_cur(vc);
2263 return;
2264 case 'X':
2265 csi_X(vc, vc->vc_par[0]);
2266 return;
2267 case '@':
2268 csi_at(vc, vc->vc_par[0]);
2269 return;
2270 case ']': /* setterm functions */
2271 setterm_command(vc);
2272 return;
2273 }
2274 return;
2275 case ESpercent:
2276 vc->vc_state = ESnormal;
2277 switch (c) {
2278 case '@': /* defined in ISO 2022 */
2279 vc->vc_utf = 0;
2280 return;
2281 case 'G': /* prelim official escape code */
2282 case '8': /* retained for compatibility */
2283 vc->vc_utf = 1;
2284 return;
2285 }
2286 return;
2287 case ESfunckey:
2288 vc->vc_state = ESnormal;
2289 return;
2290 case EShash:
2291 vc->vc_state = ESnormal;
2292 if (c == '8') {
2293 /* DEC screen alignment test. kludge :-) */
2294 vc->vc_video_erase_char =
2295 (vc->vc_video_erase_char & 0xff00) | 'E';
2296 csi_J(vc, 2);
2297 vc->vc_video_erase_char =
2298 (vc->vc_video_erase_char & 0xff00) | ' ';
2299 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2300 }
2301 return;
2302 case ESsetG0:
2303 if (c == '0')
2304 vc->vc_G0_charset = GRAF_MAP;
2305 else if (c == 'B')
2306 vc->vc_G0_charset = LAT1_MAP;
2307 else if (c == 'U')
2308 vc->vc_G0_charset = IBMPC_MAP;
2309 else if (c == 'K')
2310 vc->vc_G0_charset = USER_MAP;
2311 if (vc->vc_charset == 0)
2312 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2313 vc->vc_state = ESnormal;
2314 return;
2315 case ESsetG1:
2316 if (c == '0')
2317 vc->vc_G1_charset = GRAF_MAP;
2318 else if (c == 'B')
2319 vc->vc_G1_charset = LAT1_MAP;
2320 else if (c == 'U')
2321 vc->vc_G1_charset = IBMPC_MAP;
2322 else if (c == 'K')
2323 vc->vc_G1_charset = USER_MAP;
2324 if (vc->vc_charset == 1)
2325 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2326 vc->vc_state = ESnormal;
2327 return;
63f3a16d
AB
2328 case ESosc:
2329 return;
1da177e4
LT
2330 default:
2331 vc->vc_state = ESnormal;
2332 }
2333}
2334
2f1a2ccb 2335/* is_double_width() is based on the wcwidth() implementation by
1ed8a2b3 2336 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2f1a2ccb
EK
2337 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2338 */
2339struct interval {
2340 uint32_t first;
2341 uint32_t last;
2342};
2343
f0a8d84f 2344static int ucs_cmp(const void *key, const void *elt)
2f1a2ccb 2345{
f0a8d84f
TM
2346 uint32_t ucs = *(uint32_t *)key;
2347 struct interval e = *(struct interval *) elt;
2f1a2ccb 2348
f0a8d84f
TM
2349 if (ucs > e.last)
2350 return 1;
2351 else if (ucs < e.first)
2352 return -1;
2f1a2ccb
EK
2353 return 0;
2354}
2355
2356static int is_double_width(uint32_t ucs)
2357{
2358 static const struct interval double_width[] = {
2359 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2360 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
1ed8a2b3
EK
2361 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2362 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2f1a2ccb 2363 };
f0a8d84f
TM
2364 if (ucs < double_width[0].first ||
2365 ucs > double_width[ARRAY_SIZE(double_width) - 1].last)
2366 return 0;
2367
2368 return bsearch(&ucs, double_width, ARRAY_SIZE(double_width),
2369 sizeof(struct interval), ucs_cmp) != NULL;
2f1a2ccb
EK
2370}
2371
d711ea8f
JS
2372static void con_flush(struct vc_data *vc, unsigned long draw_from,
2373 unsigned long draw_to, int *draw_x)
2374{
2375 if (*draw_x < 0)
2376 return;
2377
2378 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2379 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2380 *draw_x = -1;
2381}
2382
ac751efa 2383/* acquires console_lock */
1da177e4
LT
2384static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2385{
d8ae7242 2386 int c, next_c, tc, ok, n = 0, draw_x = -1;
1da177e4
LT
2387 unsigned int currcons;
2388 unsigned long draw_from = 0, draw_to = 0;
2389 struct vc_data *vc;
2f1a2ccb 2390 unsigned char vc_attr;
0341a4d0 2391 struct vt_notifier_param param;
2f1a2ccb
EK
2392 uint8_t rescan;
2393 uint8_t inverse;
2394 uint8_t width;
1da177e4 2395 u16 himask, charmask;
1da177e4
LT
2396
2397 if (in_interrupt())
2398 return count;
2399
2400 might_sleep();
2401
ac751efa 2402 console_lock();
1da177e4
LT
2403 vc = tty->driver_data;
2404 if (vc == NULL) {
bccf1da3 2405 pr_err("vt: argh, driver_data is NULL !\n");
ac751efa 2406 console_unlock();
1da177e4
LT
2407 return 0;
2408 }
2409
2410 currcons = vc->vc_num;
2411 if (!vc_cons_allocated(currcons)) {
1ffdda95
MSB
2412 /* could this happen? */
2413 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2414 console_unlock();
2415 return 0;
1da177e4 2416 }
1da177e4 2417
1da177e4
LT
2418 himask = vc->vc_hi_font_mask;
2419 charmask = himask ? 0x1ff : 0xff;
2420
2421 /* undraw cursor first */
6ca8dfd7 2422 if (con_is_fg(vc))
1da177e4
LT
2423 hide_cursor(vc);
2424
0341a4d0
KD
2425 param.vc = vc;
2426
1da177e4
LT
2427 while (!tty->stopped && count) {
2428 int orig = *buf;
2429 c = orig;
2430 buf++;
2431 n++;
2432 count--;
2f1a2ccb
EK
2433 rescan = 0;
2434 inverse = 0;
2435 width = 1;
1da177e4
LT
2436
2437 /* Do no translation at all in control states */
2438 if (vc->vc_state != ESnormal) {
2439 tc = c;
d4328b40 2440 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2f1a2ccb
EK
2441 /* Combine UTF-8 into Unicode in vc_utf_char.
2442 * vc_utf_count is the number of continuation bytes still
2443 * expected to arrive.
2444 * vc_npar is the number of continuation bytes arrived so
2445 * far
2446 */
d4328b40 2447rescan_last_byte:
2f1a2ccb
EK
2448 if ((c & 0xc0) == 0x80) {
2449 /* Continuation byte received */
2450 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
d4328b40 2451 if (vc->vc_utf_count) {
2f1a2ccb
EK
2452 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2453 vc->vc_npar++;
2454 if (--vc->vc_utf_count) {
2455 /* Still need some bytes */
1da177e4 2456 continue;
2f1a2ccb
EK
2457 }
2458 /* Got a whole character */
2459 c = vc->vc_utf_char;
2460 /* Reject overlong sequences */
2461 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2462 c > utf8_length_changes[vc->vc_npar])
2463 c = 0xfffd;
2464 } else {
2465 /* Unexpected continuation byte */
2466 vc->vc_utf_count = 0;
2467 c = 0xfffd;
2468 }
1da177e4 2469 } else {
2f1a2ccb
EK
2470 /* Single ASCII byte or first byte of a sequence received */
2471 if (vc->vc_utf_count) {
2472 /* Continuation byte expected */
2473 rescan = 1;
2474 vc->vc_utf_count = 0;
2475 c = 0xfffd;
2476 } else if (c > 0x7f) {
2477 /* First byte of a multibyte sequence received */
2478 vc->vc_npar = 0;
2479 if ((c & 0xe0) == 0xc0) {
2480 vc->vc_utf_count = 1;
2481 vc->vc_utf_char = (c & 0x1f);
2482 } else if ((c & 0xf0) == 0xe0) {
2483 vc->vc_utf_count = 2;
2484 vc->vc_utf_char = (c & 0x0f);
2485 } else if ((c & 0xf8) == 0xf0) {
2486 vc->vc_utf_count = 3;
2487 vc->vc_utf_char = (c & 0x07);
2488 } else if ((c & 0xfc) == 0xf8) {
2489 vc->vc_utf_count = 4;
2490 vc->vc_utf_char = (c & 0x03);
2491 } else if ((c & 0xfe) == 0xfc) {
2492 vc->vc_utf_count = 5;
2493 vc->vc_utf_char = (c & 0x01);
2494 } else {
2495 /* 254 and 255 are invalid */
2496 c = 0xfffd;
2497 }
2498 if (vc->vc_utf_count) {
2499 /* Still need some bytes */
2500 continue;
2501 }
2502 }
2503 /* Nothing to do if an ASCII byte was received */
1da177e4 2504 }
2f1a2ccb
EK
2505 /* End of UTF-8 decoding. */
2506 /* c is the received character, or U+FFFD for invalid sequences. */
2507 /* Replace invalid Unicode code points with U+FFFD too */
2508 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2509 c = 0xfffd;
2510 tc = c;
d4328b40 2511 } else { /* no utf or alternate charset mode */
a29ccf6f 2512 tc = vc_translate(vc, c);
1da177e4
LT
2513 }
2514
0341a4d0
KD
2515 param.c = tc;
2516 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2517 &param) == NOTIFY_STOP)
2518 continue;
2519
1da177e4
LT
2520 /* If the original code was a control character we
2521 * only allow a glyph to be displayed if the code is
2522 * not normally used (such as for cursor movement) or
2523 * if the disp_ctrl mode has been explicitly enabled.
2524 * Certain characters (as given by the CTRL_ALWAYS
2525 * bitmap) are always displayed as control characters,
2526 * as the console would be pretty useless without
2527 * them; to display an arbitrary font position use the
2528 * direct-to-font zone in UTF-8 mode.
2529 */
2530 ok = tc && (c >= 32 ||
d4328b40
AT
2531 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2532 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
1da177e4
LT
2533 && (c != 127 || vc->vc_disp_ctrl)
2534 && (c != 128+27);
2535
2536 if (vc->vc_state == ESnormal && ok) {
2f1a2ccb
EK
2537 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2538 if (is_double_width(c))
2539 width = 2;
2540 }
1da177e4
LT
2541 /* Now try to find out how to display it */
2542 tc = conv_uni_to_pc(vc, tc);
d4328b40 2543 if (tc & ~charmask) {
2f1a2ccb
EK
2544 if (tc == -1 || tc == -2) {
2545 continue; /* nothing to display */
2546 }
2547 /* Glyph not found */
c0b79882 2548 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2f1a2ccb 2549 /* In legacy mode use the glyph we get by a 1:1 mapping.
1ed8a2b3
EK
2550 This would make absolutely no sense with Unicode in mind,
2551 but do this for ASCII characters since a font may lack
2552 Unicode mapping info and we don't want to end up with
2553 having question marks only. */
2f1a2ccb
EK
2554 tc = c;
2555 } else {
2556 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2557 tc = conv_uni_to_pc(vc, 0xfffd);
2558 if (tc < 0) {
2559 inverse = 1;
2560 tc = conv_uni_to_pc(vc, '?');
2561 if (tc < 0) tc = '?';
2562 }
2563 }
d4328b40 2564 }
1da177e4 2565
2f1a2ccb
EK
2566 if (!inverse) {
2567 vc_attr = vc->vc_attr;
1da177e4 2568 } else {
2f1a2ccb
EK
2569 /* invert vc_attr */
2570 if (!vc->vc_can_do_color) {
2571 vc_attr = (vc->vc_attr) ^ 0x08;
2572 } else if (vc->vc_hi_font_mask == 0x100) {
2573 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2574 } else {
2575 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2576 }
d711ea8f 2577 con_flush(vc, draw_from, draw_to, &draw_x);
1da177e4 2578 }
2f1a2ccb 2579
d8ae7242 2580 next_c = c;
2f1a2ccb
EK
2581 while (1) {
2582 if (vc->vc_need_wrap || vc->vc_decim)
d711ea8f
JS
2583 con_flush(vc, draw_from, draw_to,
2584 &draw_x);
2f1a2ccb
EK
2585 if (vc->vc_need_wrap) {
2586 cr(vc);
2587 lf(vc);
2588 }
2589 if (vc->vc_decim)
2590 insert_char(vc, 1);
d8ae7242 2591 vc_uniscr_putc(vc, next_c);
2f1a2ccb
EK
2592 scr_writew(himask ?
2593 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2594 (vc_attr << 8) + tc,
2595 (u16 *) vc->vc_pos);
6ca8dfd7 2596 if (con_should_update(vc) && draw_x < 0) {
2f1a2ccb
EK
2597 draw_x = vc->vc_x;
2598 draw_from = vc->vc_pos;
2599 }
2600 if (vc->vc_x == vc->vc_cols - 1) {
2601 vc->vc_need_wrap = vc->vc_decawm;
2602 draw_to = vc->vc_pos + 2;
2603 } else {
2604 vc->vc_x++;
2605 draw_to = (vc->vc_pos += 2);
d4328b40 2606 }
2f1a2ccb
EK
2607
2608 if (!--width) break;
2609
2610 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2611 if (tc < 0) tc = ' ';
d8ae7242 2612 next_c = ' ';
2f1a2ccb 2613 }
b293d758 2614 notify_write(vc, c);
2f1a2ccb 2615
d711ea8f
JS
2616 if (inverse)
2617 con_flush(vc, draw_from, draw_to, &draw_x);
1ed8a2b3 2618
2f1a2ccb
EK
2619 if (rescan) {
2620 rescan = 0;
2621 inverse = 0;
2622 width = 1;
d4328b40
AT
2623 c = orig;
2624 goto rescan_last_byte;
2625 }
1da177e4
LT
2626 continue;
2627 }
d711ea8f 2628 con_flush(vc, draw_from, draw_to, &draw_x);
1da177e4
LT
2629 do_con_trol(tty, vc, orig);
2630 }
d711ea8f 2631 con_flush(vc, draw_from, draw_to, &draw_x);
1da177e4 2632 console_conditional_schedule();
ac751efa 2633 console_unlock();
b293d758 2634 notify_update(vc);
1da177e4 2635 return n;
1da177e4
LT
2636}
2637
2638/*
2639 * This is the console switching callback.
2640 *
2641 * Doing console switching in a process context allows
2642 * us to do the switches asynchronously (needed when we want
2643 * to switch due to a keyboard interrupt). Synchronization
2644 * with other console code and prevention of re-entrancy is
ac751efa 2645 * ensured with console_lock.
1da177e4 2646 */
65f27f38 2647static void console_callback(struct work_struct *ignored)
1da177e4 2648{
ac751efa 2649 console_lock();
1da177e4
LT
2650
2651 if (want_console >= 0) {
2652 if (want_console != fg_console &&
2653 vc_cons_allocated(want_console)) {
2654 hide_cursor(vc_cons[fg_console].d);
2655 change_console(vc_cons[want_console].d);
2656 /* we only changed when the console had already
2657 been allocated - a new console is not created
2658 in an interrupt routine */
2659 }
2660 want_console = -1;
2661 }
2662 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2663 do_poke_blanked_console = 0;
2664 poke_blanked_console();
2665 }
2666 if (scrollback_delta) {
2667 struct vc_data *vc = vc_cons[fg_console].d;
2668 clear_selection();
97293de9 2669 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
1da177e4
LT
2670 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2671 scrollback_delta = 0;
2672 }
2673 if (blank_timer_expired) {
2674 do_blank_screen(0);
2675 blank_timer_expired = 0;
2676 }
b293d758 2677 notify_update(vc_cons[fg_console].d);
1da177e4 2678
ac751efa 2679 console_unlock();
1da177e4
LT
2680}
2681
b257bc05 2682int set_console(int nr)
1da177e4 2683{
b257bc05
AJ
2684 struct vc_data *vc = vc_cons[fg_console].d;
2685
2686 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2687 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2688
2689 /*
2690 * Console switch will fail in console_callback() or
2691 * change_console() so there is no point scheduling
2692 * the callback
2693 *
2694 * Existing set_console() users don't check the return
2695 * value so this shouldn't break anything
2696 */
2697 return -EINVAL;
2698 }
2699
1da177e4
LT
2700 want_console = nr;
2701 schedule_console_callback();
b257bc05
AJ
2702
2703 return 0;
1da177e4
LT
2704}
2705
2706struct tty_driver *console_driver;
2707
2708#ifdef CONFIG_VT_CONSOLE
2709
5ada918b
BW
2710/**
2711 * vt_kmsg_redirect() - Sets/gets the kernel message console
2712 * @new: The new virtual terminal number or -1 if the console should stay
2713 * unchanged
2714 *
2715 * By default, the kernel messages are always printed on the current virtual
2716 * console. However, the user may modify that default with the
2717 * TIOCL_SETKMSGREDIRECT ioctl call.
2718 *
2719 * This function sets the kernel message console to be @new. It returns the old
2720 * virtual console number. The virtual terminal number 0 (both as parameter and
2721 * return value) means no redirection (i.e. always printed on the currently
2722 * active console).
2723 *
2724 * The parameter -1 means that only the current console is returned, but the
2725 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2726 * case to make the code more understandable.
2727 *
2728 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2729 * the parameter and always returns 0.
2730 */
2731int vt_kmsg_redirect(int new)
2732{
2733 static int kmsg_con;
2734
2735 if (new != -1)
2736 return xchg(&kmsg_con, new);
2737 else
2738 return kmsg_con;
2739}
2740
1da177e4
LT
2741/*
2742 * Console on virtual terminal
2743 *
2744 * The console must be locked when we get here.
2745 */
2746
2747static void vt_console_print(struct console *co, const char *b, unsigned count)
2748{
2749 struct vc_data *vc = vc_cons[fg_console].d;
2750 unsigned char c;
b0940003 2751 static DEFINE_SPINLOCK(printing_lock);
1da177e4
LT
2752 const ushort *start;
2753 ushort cnt = 0;
2754 ushort myx;
5ada918b 2755 int kmsg_console;
1da177e4
LT
2756
2757 /* console busy or not yet initialized */
b0940003
NP
2758 if (!printable)
2759 return;
2760 if (!spin_trylock(&printing_lock))
1da177e4
LT
2761 return;
2762
5ada918b
BW
2763 kmsg_console = vt_get_kmsg_redirect();
2764 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2765 vc = vc_cons[kmsg_console - 1].d;
1da177e4
LT
2766
2767 /* read `x' only after setting currcons properly (otherwise
2768 the `x' macro will read the x of the foreground console). */
2769 myx = vc->vc_x;
2770
2771 if (!vc_cons_allocated(fg_console)) {
2772 /* impossible */
2773 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2774 goto quit;
2775 }
2776
8fd4bd22 2777 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
1da177e4
LT
2778 goto quit;
2779
2780 /* undraw cursor first */
6ca8dfd7 2781 if (con_is_fg(vc))
1da177e4
LT
2782 hide_cursor(vc);
2783
2784 start = (ushort *)vc->vc_pos;
2785
2786 /* Contrived structure to try to emulate original need_wrap behaviour
2787 * Problems caused when we have need_wrap set on '\n' character */
2788 while (count--) {
2789 c = *b++;
2790 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2791 if (cnt > 0) {
6ca8dfd7 2792 if (con_is_visible(vc))
1da177e4
LT
2793 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2794 vc->vc_x += cnt;
2795 if (vc->vc_need_wrap)
2796 vc->vc_x--;
2797 cnt = 0;
2798 }
2799 if (c == 8) { /* backspace */
2800 bs(vc);
2801 start = (ushort *)vc->vc_pos;
2802 myx = vc->vc_x;
2803 continue;
2804 }
2805 if (c != 13)
2806 lf(vc);
2807 cr(vc);
2808 start = (ushort *)vc->vc_pos;
2809 myx = vc->vc_x;
2810 if (c == 10 || c == 13)
2811 continue;
2812 }
2813 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
b293d758 2814 notify_write(vc, c);
1da177e4
LT
2815 cnt++;
2816 if (myx == vc->vc_cols - 1) {
2817 vc->vc_need_wrap = 1;
2818 continue;
2819 }
2820 vc->vc_pos += 2;
2821 myx++;
2822 }
2823 if (cnt > 0) {
6ca8dfd7 2824 if (con_is_visible(vc))
1da177e4
LT
2825 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2826 vc->vc_x += cnt;
2827 if (vc->vc_x == vc->vc_cols) {
2828 vc->vc_x--;
2829 vc->vc_need_wrap = 1;
2830 }
2831 }
2832 set_cursor(vc);
b293d758 2833 notify_update(vc);
1da177e4
LT
2834
2835quit:
b0940003 2836 spin_unlock(&printing_lock);
1da177e4
LT
2837}
2838
2839static struct tty_driver *vt_console_device(struct console *c, int *index)
2840{
2841 *index = c->index ? c->index-1 : fg_console;
2842 return console_driver;
2843}
2844
2845static struct console vt_console_driver = {
2846 .name = "tty",
2847 .write = vt_console_print,
2848 .device = vt_console_device,
2849 .unblank = unblank_screen,
2850 .flags = CON_PRINTBUFFER,
2851 .index = -1,
2852};
2853#endif
2854
2855/*
2856 * Handling of Linux-specific VC ioctls
2857 */
2858
2859/*
ac751efa 2860 * Generally a bit racy with respect to console_lock();.
1da177e4
LT
2861 *
2862 * There are some functions which don't need it.
2863 *
2864 * There are some functions which can sleep for arbitrary periods
2865 * (paste_selection) but we don't need the lock there anyway.
2866 *
2867 * set_selection has locking, and definitely needs it
2868 */
2869
2870int tioclinux(struct tty_struct *tty, unsigned long arg)
2871{
2872 char type, data;
2873 char __user *p = (char __user *)arg;
2874 int lines;
2875 int ret;
2876
1da177e4
LT
2877 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2878 return -EPERM;
2879 if (get_user(type, p))
2880 return -EFAULT;
2881 ret = 0;
04f378b1 2882
1da177e4
LT
2883 switch (type)
2884 {
2885 case TIOCL_SETSEL:
ac751efa 2886 console_lock();
1da177e4 2887 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
ac751efa 2888 console_unlock();
1da177e4
LT
2889 break;
2890 case TIOCL_PASTESEL:
2891 ret = paste_selection(tty);
2892 break;
2893 case TIOCL_UNBLANKSCREEN:
ac751efa 2894 console_lock();
1da177e4 2895 unblank_screen();
ac751efa 2896 console_unlock();
1da177e4
LT
2897 break;
2898 case TIOCL_SELLOADLUT:
5289475d 2899 console_lock();
1da177e4 2900 ret = sel_loadlut(p);
5289475d 2901 console_unlock();
1da177e4
LT
2902 break;
2903 case TIOCL_GETSHIFTSTATE:
04f378b1 2904
1da177e4
LT
2905 /*
2906 * Make it possible to react to Shift+Mousebutton.
2907 * Note that 'shift_state' is an undocumented
2908 * kernel-internal variable; programs not closely
2909 * related to the kernel should not use this.
2910 */
079c9534 2911 data = vt_get_shift_state();
6987dc8a 2912 ret = put_user(data, p);
1da177e4
LT
2913 break;
2914 case TIOCL_GETMOUSEREPORTING:
20f62579 2915 console_lock(); /* May be overkill */
1da177e4 2916 data = mouse_reporting();
20f62579 2917 console_unlock();
6987dc8a 2918 ret = put_user(data, p);
1da177e4
LT
2919 break;
2920 case TIOCL_SETVESABLANK:
20f62579 2921 console_lock();
403aac96 2922 ret = set_vesa_blanking(p);
20f62579 2923 console_unlock();
1da177e4 2924 break;
0ca07731 2925 case TIOCL_GETKMSGREDIRECT:
5ada918b 2926 data = vt_get_kmsg_redirect();
6987dc8a 2927 ret = put_user(data, p);
0ca07731 2928 break;
1da177e4
LT
2929 case TIOCL_SETKMSGREDIRECT:
2930 if (!capable(CAP_SYS_ADMIN)) {
2931 ret = -EPERM;
2932 } else {
2933 if (get_user(data, p+1))
2934 ret = -EFAULT;
2935 else
5ada918b 2936 vt_kmsg_redirect(data);
1da177e4
LT
2937 }
2938 break;
2939 case TIOCL_GETFGCONSOLE:
20f62579
AC
2940 /* No locking needed as this is a transiently
2941 correct return anyway if the caller hasn't
2942 disabled switching */
1da177e4
LT
2943 ret = fg_console;
2944 break;
2945 case TIOCL_SCROLLCONSOLE:
2946 if (get_user(lines, (s32 __user *)(p+4))) {
2947 ret = -EFAULT;
2948 } else {
20f62579
AC
2949 /* Need the console lock here. Note that lots
2950 of other calls need fixing before the lock
2951 is actually useful ! */
2952 console_lock();
1da177e4 2953 scrollfront(vc_cons[fg_console].d, lines);
20f62579 2954 console_unlock();
1da177e4
LT
2955 ret = 0;
2956 }
2957 break;
2958 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
ac751efa 2959 console_lock();
1da177e4
LT
2960 ignore_poke = 1;
2961 do_blank_screen(0);
ac751efa 2962 console_unlock();
1da177e4
LT
2963 break;
2964 case TIOCL_BLANKEDSCREEN:
2965 ret = console_blanked;
2966 break;
2967 default:
2968 ret = -EINVAL;
2969 break;
2970 }
2971 return ret;
2972}
2973
2974/*
2975 * /dev/ttyN handling
2976 */
2977
2978static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2979{
2980 int retval;
2981
2982 retval = do_con_write(tty, buf, count);
2983 con_flush_chars(tty);
2984
2985 return retval;
2986}
2987
5d19f546 2988static int con_put_char(struct tty_struct *tty, unsigned char ch)
1da177e4
LT
2989{
2990 if (in_interrupt())
5d19f546
AC
2991 return 0; /* n_r3964 calls put_char() from interrupt context */
2992 return do_con_write(tty, &ch, 1);
1da177e4
LT
2993}
2994
2995static int con_write_room(struct tty_struct *tty)
2996{
2997 if (tty->stopped)
2998 return 0;
a88a69c9 2999 return 32768; /* No limit, really; we're not buffering */
1da177e4
LT
3000}
3001
3002static int con_chars_in_buffer(struct tty_struct *tty)
3003{
3004 return 0; /* we're not buffering */
3005}
3006
3007/*
3008 * con_throttle and con_unthrottle are only used for
3009 * paste_selection(), which has to stuff in a large number of
3010 * characters...
3011 */
3012static void con_throttle(struct tty_struct *tty)
3013{
3014}
3015
3016static void con_unthrottle(struct tty_struct *tty)
3017{
3018 struct vc_data *vc = tty->driver_data;
3019
3020 wake_up_interruptible(&vc->paste_wait);
3021}
3022
3023/*
3024 * Turn the Scroll-Lock LED on when the tty is stopped
3025 */
3026static void con_stop(struct tty_struct *tty)
3027{
3028 int console_num;
3029 if (!tty)
3030 return;
3031 console_num = tty->index;
3032 if (!vc_cons_allocated(console_num))
3033 return;
079c9534 3034 vt_kbd_con_stop(console_num);
1da177e4
LT
3035}
3036
3037/*
3038 * Turn the Scroll-Lock LED off when the console is started
3039 */
3040static void con_start(struct tty_struct *tty)
3041{
3042 int console_num;
3043 if (!tty)
3044 return;
3045 console_num = tty->index;
3046 if (!vc_cons_allocated(console_num))
3047 return;
079c9534 3048 vt_kbd_con_start(console_num);
1da177e4
LT
3049}
3050
3051static void con_flush_chars(struct tty_struct *tty)
3052{
3053 struct vc_data *vc;
3054
3055 if (in_interrupt()) /* from flush_to_ldisc */
3056 return;
3057
3058 /* if we race with con_close(), vt may be null */
ac751efa 3059 console_lock();
1da177e4
LT
3060 vc = tty->driver_data;
3061 if (vc)
3062 set_cursor(vc);
ac751efa 3063 console_unlock();
1da177e4
LT
3064}
3065
3066/*
3067 * Allocate the console screen memory.
3068 */
bc1e99d9 3069static int con_install(struct tty_driver *driver, struct tty_struct *tty)
1da177e4
LT
3070{
3071 unsigned int currcons = tty->index;
bc1e99d9
JS
3072 struct vc_data *vc;
3073 int ret;
1da177e4 3074
ac751efa 3075 console_lock();
bc1e99d9
JS
3076 ret = vc_allocate(currcons);
3077 if (ret)
3078 goto unlock;
feebed65 3079
bc1e99d9 3080 vc = vc_cons[currcons].d;
1da177e4 3081
bc1e99d9
JS
3082 /* Still being freed */
3083 if (vc->port.tty) {
3084 ret = -ERESTARTSYS;
3085 goto unlock;
3086 }
3087
3088 ret = tty_port_install(&vc->port, driver, tty);
3089 if (ret)
3090 goto unlock;
3091
3092 tty->driver_data = vc;
3093 vc->port.tty = tty;
3094
3095 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
3096 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
3097 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
1da177e4 3098 }
bc1e99d9 3099 if (vc->vc_utf)
adc8d746 3100 tty->termios.c_iflag |= IUTF8;
bc1e99d9 3101 else
adc8d746 3102 tty->termios.c_iflag &= ~IUTF8;
bc1e99d9 3103unlock:
ac751efa 3104 console_unlock();
1da177e4
LT
3105 return ret;
3106}
3107
bc1e99d9
JS
3108static int con_open(struct tty_struct *tty, struct file *filp)
3109{
3110 /* everything done in install */
3111 return 0;
3112}
3113
3114
1da177e4
LT
3115static void con_close(struct tty_struct *tty, struct file *filp)
3116{
feebed65
AC
3117 /* Nothing to do - we defer to shutdown */
3118}
1da177e4 3119
feebed65
AC
3120static void con_shutdown(struct tty_struct *tty)
3121{
3122 struct vc_data *vc = tty->driver_data;
3123 BUG_ON(vc == NULL);
ac751efa 3124 console_lock();
8ce73264 3125 vc->port.tty = NULL;
ac751efa 3126 console_unlock();
1da177e4
LT
3127}
3128
3855ae1c 3129static int default_color = 7; /* white */
fa6ce9ab
JE
3130static int default_italic_color = 2; // green (ASCII)
3131static int default_underline_color = 3; // cyan (ASCII)
3855ae1c 3132module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
fa6ce9ab
JE
3133module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
3134module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
3135
1da177e4
LT
3136static void vc_init(struct vc_data *vc, unsigned int rows,
3137 unsigned int cols, int do_clear)
3138{
3139 int j, k ;
3140
3141 vc->vc_cols = cols;
3142 vc->vc_rows = rows;
3143 vc->vc_size_row = cols << 1;
3144 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
3145
3146 set_origin(vc);
3147 vc->vc_pos = vc->vc_origin;
3148 reset_vc(vc);
3149 for (j=k=0; j<16; j++) {
3150 vc->vc_palette[k++] = default_red[j] ;
3151 vc->vc_palette[k++] = default_grn[j] ;
3152 vc->vc_palette[k++] = default_blu[j] ;
3153 }
3855ae1c 3154 vc->vc_def_color = default_color;
fa6ce9ab
JE
3155 vc->vc_ulcolor = default_underline_color;
3156 vc->vc_itcolor = default_italic_color;
1da177e4
LT
3157 vc->vc_halfcolor = 0x08; /* grey */
3158 init_waitqueue_head(&vc->paste_wait);
3159 reset_terminal(vc, do_clear);
3160}
3161
3162/*
3163 * This routine initializes console interrupts, and does nothing
3164 * else. If you want the screen to clear, call tty_write with
3165 * the appropriate escape-sequence.
3166 */
3167
3168static int __init con_init(void)
3169{
3170 const char *display_desc = NULL;
3171 struct vc_data *vc;
3e795de7 3172 unsigned int currcons = 0, i;
1da177e4 3173
ac751efa 3174 console_lock();
1da177e4
LT
3175
3176 if (conswitchp)
3177 display_desc = conswitchp->con_startup();
3178 if (!display_desc) {
3179 fg_console = 0;
ac751efa 3180 console_unlock();
1da177e4
LT
3181 return 0;
3182 }
3183
3e795de7
AD
3184 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3185 struct con_driver *con_driver = &registered_con_driver[i];
3186
3187 if (con_driver->con == NULL) {
3188 con_driver->con = conswitchp;
3189 con_driver->desc = display_desc;
3190 con_driver->flag = CON_DRIVER_FLAG_INIT;
3191 con_driver->first = 0;
3192 con_driver->last = MAX_NR_CONSOLES - 1;
3193 break;
3194 }
3195 }
3196
3197 for (i = 0; i < MAX_NR_CONSOLES; i++)
3198 con_driver_map[i] = conswitchp;
3199
1da177e4
LT
3200 if (blankinterval) {
3201 blank_state = blank_normal_wait;
f324edc8 3202 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
3203 }
3204
1da177e4 3205 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
a5f4f52e 3206 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
7f1f86a0 3207 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
ff917ba4 3208 tty_port_init(&vc->port);
1da177e4 3209 visual_init(vc, currcons, 1);
a5f4f52e 3210 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
1da177e4
LT
3211 vc_init(vc, vc->vc_rows, vc->vc_cols,
3212 currcons || !vc->vc_sw->con_save_screen);
3213 }
3214 currcons = fg_console = 0;
3215 master_display_fg = vc = vc_cons[currcons].d;
3216 set_origin(vc);
3217 save_screen(vc);
3218 gotoxy(vc, vc->vc_x, vc->vc_y);
3219 csi_J(vc, 0);
3220 update_screen(vc);
5da527aa 3221 pr_info("Console: %s %s %dx%d\n",
1da177e4
LT
3222 vc->vc_can_do_color ? "colour" : "mono",
3223 display_desc, vc->vc_cols, vc->vc_rows);
3224 printable = 1;
1da177e4 3225
ac751efa 3226 console_unlock();
1da177e4
LT
3227
3228#ifdef CONFIG_VT_CONSOLE
3229 register_console(&vt_console_driver);
3230#endif
3231 return 0;
3232}
3233console_initcall(con_init);
3234
b68e31d0 3235static const struct tty_operations con_ops = {
bc1e99d9 3236 .install = con_install,
1da177e4
LT
3237 .open = con_open,
3238 .close = con_close,
3239 .write = con_write,
3240 .write_room = con_write_room,
3241 .put_char = con_put_char,
3242 .flush_chars = con_flush_chars,
3243 .chars_in_buffer = con_chars_in_buffer,
3244 .ioctl = vt_ioctl,
e9216651
AB
3245#ifdef CONFIG_COMPAT
3246 .compat_ioctl = vt_compat_ioctl,
3247#endif
1da177e4
LT
3248 .stop = con_stop,
3249 .start = con_start,
3250 .throttle = con_throttle,
3251 .unthrottle = con_unthrottle,
8c9a9dd0 3252 .resize = vt_resize,
feebed65 3253 .shutdown = con_shutdown
1da177e4
LT
3254};
3255
d81ed103
AC
3256static struct cdev vc0_cdev;
3257
fbc92a34
KS
3258static ssize_t show_tty_active(struct device *dev,
3259 struct device_attribute *attr, char *buf)
3260{
3261 return sprintf(buf, "tty%d\n", fg_console + 1);
3262}
3263static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3264
1083a7be
TI
3265static struct attribute *vt_dev_attrs[] = {
3266 &dev_attr_active.attr,
3267 NULL
3268};
3269
3270ATTRIBUTE_GROUPS(vt_dev);
3271
d81ed103 3272int __init vty_init(const struct file_operations *console_fops)
1da177e4 3273{
d81ed103
AC
3274 cdev_init(&vc0_cdev, console_fops);
3275 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3276 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3277 panic("Couldn't register /dev/tty0 driver\n");
1083a7be
TI
3278 tty0dev = device_create_with_groups(tty_class, NULL,
3279 MKDEV(TTY_MAJOR, 0), NULL,
3280 vt_dev_groups, "tty0");
fbc92a34
KS
3281 if (IS_ERR(tty0dev))
3282 tty0dev = NULL;
d81ed103 3283
1da177e4
LT
3284 vcs_init();
3285
3286 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3287 if (!console_driver)
3288 panic("Couldn't allocate console driver\n");
2f16669d 3289
1da177e4
LT
3290 console_driver->name = "tty";
3291 console_driver->name_base = 1;
3292 console_driver->major = TTY_MAJOR;
3293 console_driver->minor_start = 1;
3294 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3295 console_driver->init_termios = tty_std_termios;
c1236d31
ST
3296 if (default_utf8)
3297 console_driver->init_termios.c_iflag |= IUTF8;
1da177e4
LT
3298 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3299 tty_set_operations(console_driver, &con_ops);
3300 if (tty_register_driver(console_driver))
3301 panic("Couldn't register console driver\n");
1da177e4
LT
3302 kbd_init();
3303 console_map_init();
1da177e4
LT
3304#ifdef CONFIG_MDA_CONSOLE
3305 mda_console_init();
3306#endif
3307 return 0;
3308}
3309
3310#ifndef VT_SINGLE_DRIVER
6db4063c
AD
3311
3312static struct class *vtconsole_class;
3313
50e244cc 3314static int do_bind_con_driver(const struct consw *csw, int first, int last,
b7269dd2 3315 int deflt)
1da177e4 3316{
3e795de7
AD
3317 struct module *owner = csw->owner;
3318 const char *desc = NULL;
3319 struct con_driver *con_driver;
3320 int i, j = -1, k = -1, retval = -ENODEV;
1da177e4 3321
1da177e4
LT
3322 if (!try_module_get(owner))
3323 return -ENODEV;
3324
50e244cc 3325 WARN_CONSOLE_UNLOCKED();
1c8ce271 3326
3e795de7
AD
3327 /* check if driver is registered */
3328 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3329 con_driver = &registered_con_driver[i];
3330
3331 if (con_driver->con == csw) {
3332 desc = con_driver->desc;
3333 retval = 0;
3334 break;
3335 }
3336 }
3337
3338 if (retval)
3339 goto err;
3340
3341 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3342 csw->con_startup();
3343 con_driver->flag |= CON_DRIVER_FLAG_INIT;
1da177e4 3344 }
1c8ce271 3345
1da177e4
LT
3346 if (deflt) {
3347 if (conswitchp)
3348 module_put(conswitchp->owner);
1c8ce271 3349
1da177e4
LT
3350 __module_get(owner);
3351 conswitchp = csw;
3352 }
3353
3e795de7
AD
3354 first = max(first, con_driver->first);
3355 last = min(last, con_driver->last);
3356
1da177e4
LT
3357 for (i = first; i <= last; i++) {
3358 int old_was_color;
3359 struct vc_data *vc = vc_cons[i].d;
3360
3361 if (con_driver_map[i])
3362 module_put(con_driver_map[i]->owner);
3363 __module_get(owner);
3364 con_driver_map[i] = csw;
3365
3366 if (!vc || !vc->vc_sw)
3367 continue;
3368
3369 j = i;
1c8ce271 3370
6ca8dfd7 3371 if (con_is_visible(vc)) {
4ee1acce 3372 k = i;
1da177e4 3373 save_screen(vc);
4ee1acce
DH
3374 }
3375
1da177e4
LT
3376 old_was_color = vc->vc_can_do_color;
3377 vc->vc_sw->con_deinit(vc);
9fc2b2d0 3378 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
1da177e4 3379 visual_init(vc, i, 0);
1c8ce271 3380 set_origin(vc);
1da177e4
LT
3381 update_attr(vc);
3382
3383 /* If the console changed between mono <-> color, then
3384 * the attributes in the screenbuf will be wrong. The
3385 * following resets all attributes to something sane.
3386 */
3387 if (old_was_color != vc->vc_can_do_color)
3388 clear_buffer_attributes(vc);
1da177e4 3389 }
4ee1acce 3390
1ffdda95 3391 pr_info("Console: switching ");
1da177e4 3392 if (!deflt)
bccf1da3 3393 pr_cont("consoles %d-%d ", first + 1, last + 1);
4ee1acce
DH
3394 if (j >= 0) {
3395 struct vc_data *vc = vc_cons[j].d;
3396
bccf1da3
JP
3397 pr_cont("to %s %s %dx%d\n",
3398 vc->vc_can_do_color ? "colour" : "mono",
3399 desc, vc->vc_cols, vc->vc_rows);
4ee1acce
DH
3400
3401 if (k >= 0) {
3402 vc = vc_cons[k].d;
3403 update_screen(vc);
3404 }
bccf1da3
JP
3405 } else {
3406 pr_cont("to %s\n", desc);
3407 }
1da177e4 3408
3e795de7
AD
3409 retval = 0;
3410err:
1da177e4 3411 module_put(owner);
3e795de7
AD
3412 return retval;
3413};
1da177e4 3414
50e244cc 3415
13ae6645 3416#ifdef CONFIG_VT_HW_CONSOLE_BINDING
e93a9a86
TI
3417/* unlocked version of unbind_con_driver() */
3418int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
1da177e4 3419{
3e795de7
AD
3420 struct module *owner = csw->owner;
3421 const struct consw *defcsw = NULL;
3422 struct con_driver *con_driver = NULL, *con_back = NULL;
3423 int i, retval = -ENODEV;
1da177e4 3424
3e795de7
AD
3425 if (!try_module_get(owner))
3426 return -ENODEV;
3427
e93a9a86 3428 WARN_CONSOLE_UNLOCKED();
3e795de7
AD
3429
3430 /* check if driver is registered and if it is unbindable */
3431 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3432 con_driver = &registered_con_driver[i];
3433
3434 if (con_driver->con == csw &&
6db4063c 3435 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3e795de7
AD
3436 retval = 0;
3437 break;
3438 }
3439 }
3440
e93a9a86 3441 if (retval)
3e795de7 3442 goto err;
3e795de7 3443
6db4063c
AD
3444 retval = -ENODEV;
3445
3e795de7
AD
3446 /* check if backup driver exists */
3447 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3448 con_back = &registered_con_driver[i];
3449
249f7b3e 3450 if (con_back->con && con_back->con != csw) {
3e795de7
AD
3451 defcsw = con_back->con;
3452 retval = 0;
3453 break;
3454 }
3455 }
3456
e93a9a86 3457 if (retval)
3e795de7 3458 goto err;
3e795de7 3459
e93a9a86 3460 if (!con_is_bound(csw))
3e795de7 3461 goto err;
3e795de7
AD
3462
3463 first = max(first, con_driver->first);
3464 last = min(last, con_driver->last);
3465
3466 for (i = first; i <= last; i++) {
1da177e4
LT
3467 if (con_driver_map[i] == csw) {
3468 module_put(csw->owner);
3469 con_driver_map[i] = NULL;
3470 }
3e795de7
AD
3471 }
3472
3473 if (!con_is_bound(defcsw)) {
6db4063c
AD
3474 const struct consw *defconsw = conswitchp;
3475
3e795de7
AD
3476 defcsw->con_startup();
3477 con_back->flag |= CON_DRIVER_FLAG_INIT;
6db4063c
AD
3478 /*
3479 * vgacon may change the default driver to point
3480 * to dummycon, we restore it here...
3481 */
3482 conswitchp = defconsw;
3e795de7
AD
3483 }
3484
3485 if (!con_is_bound(csw))
3486 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3487
6db4063c 3488 /* ignore return value, binding should not fail */
50e244cc 3489 do_bind_con_driver(defcsw, first, last, deflt);
3e795de7
AD
3490err:
3491 module_put(owner);
3492 return retval;
3493
3494}
e93a9a86 3495EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3e795de7 3496
6db4063c
AD
3497static int vt_bind(struct con_driver *con)
3498{
3499 const struct consw *defcsw = NULL, *csw = NULL;
3500 int i, more = 1, first = -1, last = -1, deflt = 0;
3501
77232f79 3502 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3503 goto err;
3504
3505 csw = con->con;
3506
3507 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3508 struct con_driver *con = &registered_con_driver[i];
3509
3510 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3511 defcsw = con->con;
3512 break;
3513 }
3514 }
3515
3516 if (!defcsw)
3517 goto err;
3518
3519 while (more) {
3520 more = 0;
3521
3522 for (i = con->first; i <= con->last; i++) {
3523 if (con_driver_map[i] == defcsw) {
3524 if (first == -1)
3525 first = i;
3526 last = i;
3527 more = 1;
3528 } else if (first != -1)
3529 break;
3530 }
3531
3532 if (first == 0 && last == MAX_NR_CONSOLES -1)
3533 deflt = 1;
3534
4c215fe8 3535 if (first != -1)
c62a1e57 3536 do_bind_con_driver(csw, first, last, deflt);
6db4063c
AD
3537
3538 first = -1;
3539 last = -1;
3540 deflt = 0;
3541 }
3542
3543err:
3544 return 0;
3545}
3546
3547static int vt_unbind(struct con_driver *con)
3548{
3549 const struct consw *csw = NULL;
3550 int i, more = 1, first = -1, last = -1, deflt = 0;
f418f2ec 3551 int ret;
6db4063c 3552
77232f79 3553 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3554 goto err;
3555
3556 csw = con->con;
3557
3558 while (more) {
3559 more = 0;
3560
3561 for (i = con->first; i <= con->last; i++) {
3562 if (con_driver_map[i] == csw) {
3563 if (first == -1)
3564 first = i;
3565 last = i;
3566 more = 1;
3567 } else if (first != -1)
3568 break;
3569 }
3570
3571 if (first == 0 && last == MAX_NR_CONSOLES -1)
3572 deflt = 1;
3573
618f2b90 3574 if (first != -1) {
f418f2ec 3575 ret = do_unbind_con_driver(csw, first, last, deflt);
f418f2ec
DV
3576 if (ret != 0)
3577 return ret;
618f2b90 3578 }
6db4063c
AD
3579
3580 first = -1;
3581 last = -1;
3582 deflt = 0;
3583 }
3584
3585err:
3586 return 0;
3587}
13ae6645
AD
3588#else
3589static inline int vt_bind(struct con_driver *con)
3590{
3591 return 0;
3592}
3593static inline int vt_unbind(struct con_driver *con)
3594{
3595 return 0;
3596}
3597#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
6db4063c 3598
805952a8 3599static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
6db4063c
AD
3600 const char *buf, size_t count)
3601{
805952a8 3602 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3603 int bind = simple_strtoul(buf, NULL, 0);
3604
4c215fe8
ID
3605 console_lock();
3606
6db4063c
AD
3607 if (bind)
3608 vt_bind(con);
3609 else
3610 vt_unbind(con);
3611
4c215fe8
ID
3612 console_unlock();
3613
6db4063c
AD
3614 return count;
3615}
3616
805952a8
GKH
3617static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3618 char *buf)
6db4063c 3619{
805952a8 3620 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3621 int bind = con_is_bound(con->con);
3622
3623 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3624}
3625
805952a8
GKH
3626static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3627 char *buf)
6db4063c 3628{
805952a8 3629 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3630
3631 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3632 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3633 con->desc);
3634
3635}
3636
1083a7be
TI
3637static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3638static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3639
3640static struct attribute *con_dev_attrs[] = {
3641 &dev_attr_bind.attr,
3642 &dev_attr_name.attr,
3643 NULL
6db4063c
AD
3644};
3645
1083a7be
TI
3646ATTRIBUTE_GROUPS(con_dev);
3647
805952a8 3648static int vtconsole_init_device(struct con_driver *con)
6db4063c 3649{
928e964f 3650 con->flag |= CON_DRIVER_FLAG_ATTR;
1083a7be 3651 return 0;
6db4063c
AD
3652}
3653
805952a8 3654static void vtconsole_deinit_device(struct con_driver *con)
6db4063c 3655{
1083a7be 3656 con->flag &= ~CON_DRIVER_FLAG_ATTR;
6db4063c
AD
3657}
3658
3e795de7
AD
3659/**
3660 * con_is_bound - checks if driver is bound to the console
3661 * @csw: console driver
3662 *
3663 * RETURNS: zero if unbound, nonzero if bound
3664 *
3665 * Drivers can call this and if zero, they should release
3666 * all resources allocated on con_startup()
3667 */
3668int con_is_bound(const struct consw *csw)
3669{
3670 int i, bound = 0;
3671
3672 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3673 if (con_driver_map[i] == csw) {
3674 bound = 1;
3675 break;
3676 }
3677 }
3678
3679 return bound;
3680}
3681EXPORT_SYMBOL(con_is_bound);
3682
b45cfba4
JB
3683/**
3684 * con_debug_enter - prepare the console for the kernel debugger
3685 * @sw: console driver
3686 *
3687 * Called when the console is taken over by the kernel debugger, this
3688 * function needs to save the current console state, then put the console
3689 * into a state suitable for the kernel debugger.
3690 *
3691 * RETURNS:
3692 * Zero on success, nonzero if a failure occurred when trying to prepare
3693 * the console for the debugger.
3694 */
3695int con_debug_enter(struct vc_data *vc)
3696{
3697 int ret = 0;
3698
3699 saved_fg_console = fg_console;
3700 saved_last_console = last_console;
3701 saved_want_console = want_console;
3702 saved_vc_mode = vc->vc_mode;
beed5336 3703 saved_console_blanked = console_blanked;
b45cfba4
JB
3704 vc->vc_mode = KD_TEXT;
3705 console_blanked = 0;
3706 if (vc->vc_sw->con_debug_enter)
3707 ret = vc->vc_sw->con_debug_enter(vc);
81d44507
JW
3708#ifdef CONFIG_KGDB_KDB
3709 /* Set the initial LINES variable if it is not already set */
3710 if (vc->vc_rows < 999) {
3711 int linecount;
3712 char lns[4];
3713 const char *setargs[3] = {
3714 "set",
3715 "LINES",
3716 lns,
3717 };
3718 if (kdbgetintenv(setargs[0], &linecount)) {
3719 snprintf(lns, 4, "%i", vc->vc_rows);
3720 kdb_set(2, setargs);
3721 }
3722 }
17b572e8
JW
3723 if (vc->vc_cols < 999) {
3724 int colcount;
3725 char cols[4];
3726 const char *setargs[3] = {
3727 "set",
3728 "COLUMNS",
3729 cols,
3730 };
3731 if (kdbgetintenv(setargs[0], &colcount)) {
3732 snprintf(cols, 4, "%i", vc->vc_cols);
3733 kdb_set(2, setargs);
3734 }
3735 }
81d44507 3736#endif /* CONFIG_KGDB_KDB */
b45cfba4
JB
3737 return ret;
3738}
3739EXPORT_SYMBOL_GPL(con_debug_enter);
3740
3741/**
3742 * con_debug_leave - restore console state
3743 * @sw: console driver
3744 *
3745 * Restore the console state to what it was before the kernel debugger
3746 * was invoked.
3747 *
3748 * RETURNS:
3749 * Zero on success, nonzero if a failure occurred when trying to restore
3750 * the console.
3751 */
3752int con_debug_leave(void)
3753{
3754 struct vc_data *vc;
3755 int ret = 0;
3756
3757 fg_console = saved_fg_console;
3758 last_console = saved_last_console;
3759 want_console = saved_want_console;
beed5336 3760 console_blanked = saved_console_blanked;
b45cfba4
JB
3761 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3762
3763 vc = vc_cons[fg_console].d;
3764 if (vc->vc_sw->con_debug_leave)
3765 ret = vc->vc_sw->con_debug_leave(vc);
3766 return ret;
3767}
3768EXPORT_SYMBOL_GPL(con_debug_leave);
3769
50e244cc 3770static int do_register_con_driver(const struct consw *csw, int first, int last)
3e795de7
AD
3771{
3772 struct module *owner = csw->owner;
3773 struct con_driver *con_driver;
3774 const char *desc;
96317e9e 3775 int i, retval;
3e795de7 3776
50e244cc
AC
3777 WARN_CONSOLE_UNLOCKED();
3778
3e795de7
AD
3779 if (!try_module_get(owner))
3780 return -ENODEV;
3781
3e795de7
AD
3782 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3783 con_driver = &registered_con_driver[i];
3784
3785 /* already registered */
96317e9e 3786 if (con_driver->con == csw) {
c55c63c6 3787 retval = -EBUSY;
96317e9e
JS
3788 goto err;
3789 }
3e795de7
AD
3790 }
3791
3e795de7 3792 desc = csw->con_startup();
6798df4c
JS
3793 if (!desc) {
3794 retval = -ENODEV;
3e795de7 3795 goto err;
6798df4c 3796 }
3e795de7
AD
3797
3798 retval = -EINVAL;
3799
3800 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3801 con_driver = &registered_con_driver[i];
3802
d364b5c3
ID
3803 if (con_driver->con == NULL &&
3804 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
3e795de7
AD
3805 con_driver->con = csw;
3806 con_driver->desc = desc;
6db4063c
AD
3807 con_driver->node = i;
3808 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3e795de7
AD
3809 CON_DRIVER_FLAG_INIT;
3810 con_driver->first = first;
3811 con_driver->last = last;
3812 retval = 0;
3813 break;
3814 }
3815 }
3816
6db4063c
AD
3817 if (retval)
3818 goto err;
3819
1083a7be
TI
3820 con_driver->dev =
3821 device_create_with_groups(vtconsole_class, NULL,
3822 MKDEV(0, con_driver->node),
3823 con_driver, con_dev_groups,
3824 "vtcon%i", con_driver->node);
805952a8 3825 if (IS_ERR(con_driver->dev)) {
bccf1da3
JP
3826 pr_warn("Unable to create device for %s; errno = %ld\n",
3827 con_driver->desc, PTR_ERR(con_driver->dev));
805952a8 3828 con_driver->dev = NULL;
6db4063c 3829 } else {
805952a8 3830 vtconsole_init_device(con_driver);
6db4063c 3831 }
928e964f 3832
3e795de7 3833err:
3e795de7
AD
3834 module_put(owner);
3835 return retval;
3836}
50e244cc 3837
3e795de7
AD
3838
3839/**
50539dd4 3840 * do_unregister_con_driver - unregister console driver from console layer
3e795de7
AD
3841 * @csw: console driver
3842 *
3843 * DESCRIPTION: All drivers that registers to the console layer must
3844 * call this function upon exit, or if the console driver is in a state
3845 * where it won't be able to handle console services, such as the
3846 * framebuffer console without loaded framebuffer drivers.
3847 *
3848 * The driver must unbind first prior to unregistration.
3849 */
e93a9a86
TI
3850int do_unregister_con_driver(const struct consw *csw)
3851{
d9c660e7 3852 int i;
3e795de7
AD
3853
3854 /* cannot unregister a bound driver */
3855 if (con_is_bound(csw))
d9c660e7
DV
3856 return -EBUSY;
3857
3858 if (csw == conswitchp)
3859 return -EINVAL;
3e795de7
AD
3860
3861 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3862 struct con_driver *con_driver = &registered_con_driver[i];
3863
2cf30f75 3864 if (con_driver->con == csw) {
d364b5c3
ID
3865 /*
3866 * Defer the removal of the sysfs entries since that
3867 * will acquire the kernfs s_active lock and we can't
3868 * acquire this lock while holding the console lock:
3869 * the unbind sysfs entry imposes already the opposite
3870 * order. Reset con already here to prevent any later
3871 * lookup to succeed and mark this slot as zombie, so
3872 * it won't get reused until we complete the removal
3873 * in the deferred work.
3874 */
3e795de7 3875 con_driver->con = NULL;
d364b5c3
ID
3876 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
3877 schedule_work(&con_driver_unregister_work);
3878
d9c660e7 3879 return 0;
3e795de7
AD
3880 }
3881 }
d9c660e7
DV
3882
3883 return -ENODEV;
3e795de7 3884}
e93a9a86 3885EXPORT_SYMBOL_GPL(do_unregister_con_driver);
3e795de7 3886
d364b5c3
ID
3887static void con_driver_unregister_callback(struct work_struct *ignored)
3888{
3889 int i;
3890
3891 console_lock();
3892
3893 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3894 struct con_driver *con_driver = &registered_con_driver[i];
3895
3896 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
3897 continue;
3898
3899 console_unlock();
3900
3901 vtconsole_deinit_device(con_driver);
3902 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
3903
3904 console_lock();
3905
3906 if (WARN_ON_ONCE(con_driver->con))
3907 con_driver->con = NULL;
3908 con_driver->desc = NULL;
3909 con_driver->dev = NULL;
3910 con_driver->node = 0;
3911 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
3912 con_driver->flag = 0;
3913 con_driver->first = 0;
3914 con_driver->last = 0;
3915 }
3916
3917 console_unlock();
3918}
3919
3e795de7
AD
3920/*
3921 * If we support more console drivers, this function is used
3922 * when a driver wants to take over some existing consoles
3923 * and become default driver for newly opened ones.
3924 *
155957f5 3925 * do_take_over_console is basically a register followed by unbind
50e244cc
AC
3926 */
3927int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3928{
3929 int err;
3930
3931 err = do_register_con_driver(csw, first, last);
3932 /*
3933 * If we get an busy error we still want to bind the console driver
3934 * and return success, as we may have unbound the console driver
3935 * but not unregistered it.
3936 */
3937 if (err == -EBUSY)
3938 err = 0;
3939 if (!err)
3940 do_bind_con_driver(csw, first, last, deflt);
3941
3942 return err;
3943}
3944EXPORT_SYMBOL_GPL(do_take_over_console);
3945
3e795de7
AD
3946
3947/*
3948 * give_up_console is a wrapper to unregister_con_driver. It will only
3949 * work if driver is fully unbound.
3950 */
3951void give_up_console(const struct consw *csw)
3952{
70125e76
WY
3953 console_lock();
3954 do_unregister_con_driver(csw);
3955 console_unlock();
3e795de7
AD
3956}
3957
6db4063c 3958static int __init vtconsole_class_init(void)
3e795de7 3959{
6db4063c 3960 int i;
3e795de7 3961
6db4063c
AD
3962 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3963 if (IS_ERR(vtconsole_class)) {
bccf1da3
JP
3964 pr_warn("Unable to create vt console class; errno = %ld\n",
3965 PTR_ERR(vtconsole_class));
6db4063c
AD
3966 vtconsole_class = NULL;
3967 }
3e795de7 3968
6db4063c 3969 /* Add system drivers to sysfs */
3e795de7
AD
3970 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3971 struct con_driver *con = &registered_con_driver[i];
3972
805952a8 3973 if (con->con && !con->dev) {
1083a7be
TI
3974 con->dev =
3975 device_create_with_groups(vtconsole_class, NULL,
3976 MKDEV(0, con->node),
3977 con, con_dev_groups,
3978 "vtcon%i", con->node);
6db4063c 3979
805952a8 3980 if (IS_ERR(con->dev)) {
bccf1da3
JP
3981 pr_warn("Unable to create device for %s; errno = %ld\n",
3982 con->desc, PTR_ERR(con->dev));
805952a8 3983 con->dev = NULL;
6db4063c 3984 } else {
805952a8 3985 vtconsole_init_device(con);
6db4063c 3986 }
3e795de7 3987 }
3e795de7
AD
3988 }
3989
3e795de7
AD
3990 return 0;
3991}
6db4063c 3992postcore_initcall(vtconsole_class_init);
3e795de7 3993
1da177e4
LT
3994#endif
3995
3996/*
3997 * Screen blanking
3998 */
3999
403aac96 4000static int set_vesa_blanking(char __user *p)
1da177e4 4001{
403aac96
YY
4002 unsigned int mode;
4003
4004 if (get_user(mode, p + 1))
4005 return -EFAULT;
4006
4007 vesa_blank_mode = (mode < 4) ? mode : 0;
4008 return 0;
1da177e4
LT
4009}
4010
1da177e4
LT
4011void do_blank_screen(int entering_gfx)
4012{
4013 struct vc_data *vc = vc_cons[fg_console].d;
4014 int i;
4015
4016 WARN_CONSOLE_UNLOCKED();
4017
4018 if (console_blanked) {
4019 if (blank_state == blank_vesa_wait) {
4020 blank_state = blank_off;
d060a321 4021 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
1da177e4
LT
4022 }
4023 return;
4024 }
1da177e4
LT
4025
4026 /* entering graphics mode? */
4027 if (entering_gfx) {
4028 hide_cursor(vc);
4029 save_screen(vc);
4030 vc->vc_sw->con_blank(vc, -1, 1);
4031 console_blanked = fg_console + 1;
b6e8f00f 4032 blank_state = blank_off;
1da177e4
LT
4033 set_origin(vc);
4034 return;
4035 }
4036
b6e8f00f 4037 if (blank_state != blank_normal_wait)
4038 return;
4039 blank_state = blank_off;
4040
1da177e4
LT
4041 /* don't blank graphics */
4042 if (vc->vc_mode != KD_TEXT) {
4043 console_blanked = fg_console + 1;
4044 return;
4045 }
4046
4047 hide_cursor(vc);
4048 del_timer_sync(&console_timer);
4049 blank_timer_expired = 0;
4050
4051 save_screen(vc);
4052 /* In case we need to reset origin, blanking hook returns 1 */
d060a321 4053 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
1da177e4
LT
4054 console_blanked = fg_console + 1;
4055 if (i)
4056 set_origin(vc);
4057
4058 if (console_blank_hook && console_blank_hook(1))
4059 return;
4060
d060a321 4061 if (vesa_off_interval && vesa_blank_mode) {
030babac 4062 blank_state = blank_vesa_wait;
1da177e4
LT
4063 mod_timer(&console_timer, jiffies + vesa_off_interval);
4064 }
8b92e87d 4065 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
4066}
4067EXPORT_SYMBOL(do_blank_screen);
4068
4069/*
4070 * Called by timer as well as from vt_console_driver
4071 */
4072void do_unblank_screen(int leaving_gfx)
4073{
4074 struct vc_data *vc;
4075
4076 /* This should now always be called from a "sane" (read: can schedule)
4077 * context for the sake of the low level drivers, except in the special
4078 * case of oops_in_progress
4079 */
4080 if (!oops_in_progress)
4081 might_sleep();
4082
4083 WARN_CONSOLE_UNLOCKED();
4084
4085 ignore_poke = 0;
4086 if (!console_blanked)
4087 return;
4088 if (!vc_cons_allocated(fg_console)) {
4089 /* impossible */
e620e548
JP
4090 pr_warn("unblank_screen: tty %d not allocated ??\n",
4091 fg_console + 1);
1da177e4
LT
4092 return;
4093 }
4094 vc = vc_cons[fg_console].d;
8fd4bd22
JB
4095 /* Try to unblank in oops case too */
4096 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
1da177e4
LT
4097 return; /* but leave console_blanked != 0 */
4098
4099 if (blankinterval) {
f324edc8 4100 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
4101 blank_state = blank_normal_wait;
4102 }
4103
4104 console_blanked = 0;
8fd4bd22 4105 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
1da177e4
LT
4106 /* Low-level driver cannot restore -> do it ourselves */
4107 update_screen(vc);
4108 if (console_blank_hook)
4109 console_blank_hook(0);
4110 set_palette(vc);
4111 set_cursor(vc);
8b92e87d 4112 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
4113}
4114EXPORT_SYMBOL(do_unblank_screen);
4115
4116/*
4117 * This is called by the outside world to cause a forced unblank, mostly for
4118 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
4119 * call it with 1 as an argument and so force a mode restore... that may kill
4120 * X or at least garbage the screen but would also make the Oops visible...
4121 */
4122void unblank_screen(void)
4123{
4124 do_unblank_screen(0);
4125}
4126
4127/*
70522e12 4128 * We defer the timer blanking to work queue so it can take the console mutex
1da177e4 4129 * (console operations can still happen at irq time, but only from printk which
70522e12 4130 * has the console mutex. Not perfect yet, but better than no locking
1da177e4 4131 */
24ed960a 4132static void blank_screen_t(struct timer_list *unused)
1da177e4
LT
4133{
4134 blank_timer_expired = 1;
4135 schedule_work(&console_work);
4136}
4137
4138void poke_blanked_console(void)
4139{
4140 WARN_CONSOLE_UNLOCKED();
4141
4142 /* Add this so we quickly catch whoever might call us in a non
4143 * safe context. Nowadays, unblank_screen() isn't to be called in
4144 * atomic contexts and is allowed to schedule (with the special case
4145 * of oops_in_progress, but that isn't of any concern for this
4146 * function. --BenH.
4147 */
4148 might_sleep();
4149
4150 /* This isn't perfectly race free, but a race here would be mostly harmless,
4151 * at worse, we'll do a spurrious blank and it's unlikely
4152 */
4153 del_timer(&console_timer);
4154 blank_timer_expired = 0;
4155
4156 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
4157 return;
4158 if (console_blanked)
4159 unblank_screen();
4160 else if (blankinterval) {
f324edc8 4161 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
4162 blank_state = blank_normal_wait;
4163 }
4164}
4165
4166/*
4167 * Palettes
4168 */
4169
4170static void set_palette(struct vc_data *vc)
4171{
4172 WARN_CONSOLE_UNLOCKED();
4173
709280da 4174 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
1da177e4
LT
4175 vc->vc_sw->con_set_palette(vc, color_table);
4176}
4177
1da177e4
LT
4178/*
4179 * Load palette into the DAC registers. arg points to a colour
4180 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4181 */
4182
4183int con_set_cmap(unsigned char __user *arg)
4184{
871bdea6
MG
4185 int i, j, k;
4186 unsigned char colormap[3*16];
4187
4188 if (copy_from_user(colormap, arg, sizeof(colormap)))
4189 return -EFAULT;
1da177e4 4190
ac751efa 4191 console_lock();
871bdea6
MG
4192 for (i = k = 0; i < 16; i++) {
4193 default_red[i] = colormap[k++];
4194 default_grn[i] = colormap[k++];
4195 default_blu[i] = colormap[k++];
4196 }
4197 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4198 if (!vc_cons_allocated(i))
4199 continue;
4200 for (j = k = 0; j < 16; j++) {
4201 vc_cons[i].d->vc_palette[k++] = default_red[j];
4202 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4203 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4204 }
4205 set_palette(vc_cons[i].d);
4206 }
ac751efa 4207 console_unlock();
1da177e4 4208
871bdea6 4209 return 0;
1da177e4
LT
4210}
4211
4212int con_get_cmap(unsigned char __user *arg)
4213{
871bdea6
MG
4214 int i, k;
4215 unsigned char colormap[3*16];
1da177e4 4216
ac751efa 4217 console_lock();
871bdea6
MG
4218 for (i = k = 0; i < 16; i++) {
4219 colormap[k++] = default_red[i];
4220 colormap[k++] = default_grn[i];
4221 colormap[k++] = default_blu[i];
4222 }
ac751efa 4223 console_unlock();
1da177e4 4224
871bdea6
MG
4225 if (copy_to_user(arg, colormap, sizeof(colormap)))
4226 return -EFAULT;
4227
4228 return 0;
1da177e4
LT
4229}
4230
4231void reset_palette(struct vc_data *vc)
4232{
4233 int j, k;
4234 for (j=k=0; j<16; j++) {
4235 vc->vc_palette[k++] = default_red[j];
4236 vc->vc_palette[k++] = default_grn[j];
4237 vc->vc_palette[k++] = default_blu[j];
4238 }
4239 set_palette(vc);
4240}
4241
4242/*
4243 * Font switching
4244 *
4245 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4246 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4247 * depending on width) reserved for each character which is kinda wasty, but
4248 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
25985edc 4249 * is up to the actual low-level console-driver convert data into its favorite
1da177e4
LT
4250 * format (maybe we should add a `fontoffset' field to the `display'
4251 * structure so we won't have to convert the fontdata all the time.
4252 * /Jes
4253 */
4254
4255#define max_font_size 65536
4256
4257static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4258{
4259 struct console_font font;
4260 int rc = -EINVAL;
4261 int c;
4262
1da177e4
LT
4263 if (op->data) {
4264 font.data = kmalloc(max_font_size, GFP_KERNEL);
4265 if (!font.data)
4266 return -ENOMEM;
4267 } else
4268 font.data = NULL;
4269
ac751efa 4270 console_lock();
edab558f
AC
4271 if (vc->vc_mode != KD_TEXT)
4272 rc = -EINVAL;
4273 else if (vc->vc_sw->con_font_get)
1da177e4
LT
4274 rc = vc->vc_sw->con_font_get(vc, &font);
4275 else
4276 rc = -ENOSYS;
ac751efa 4277 console_unlock();
1da177e4
LT
4278
4279 if (rc)
4280 goto out;
4281
4282 c = (font.width+7)/8 * 32 * font.charcount;
04f378b1 4283
1da177e4
LT
4284 if (op->data && font.charcount > op->charcount)
4285 rc = -ENOSPC;
4286 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4287 if (font.width > op->width || font.height > op->height)
4288 rc = -ENOSPC;
4289 } else {
4290 if (font.width != 8)
4291 rc = -EIO;
4292 else if ((op->height && font.height > op->height) ||
4293 font.height > 32)
4294 rc = -ENOSPC;
4295 }
4296 if (rc)
4297 goto out;
4298
4299 op->height = font.height;
4300 op->width = font.width;
4301 op->charcount = font.charcount;
4302
4303 if (op->data && copy_to_user(op->data, font.data, c))
4304 rc = -EFAULT;
4305
4306out:
4307 kfree(font.data);
4308 return rc;
4309}
4310
4311static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4312{
4313 struct console_font font;
4314 int rc = -EINVAL;
4315 int size;
4316
4317 if (vc->vc_mode != KD_TEXT)
4318 return -EINVAL;
4319 if (!op->data)
4320 return -EINVAL;
4321 if (op->charcount > 512)
4322 return -EINVAL;
8ffb8209
MX
4323 if (op->width <= 0 || op->width > 32 || op->height > 32)
4324 return -EINVAL;
4325 size = (op->width+7)/8 * 32 * op->charcount;
4326 if (size > max_font_size)
4327 return -ENOSPC;
4328
4329 font.data = memdup_user(op->data, size);
4330 if (IS_ERR(font.data))
4331 return PTR_ERR(font.data);
4332
1da177e4
LT
4333 if (!op->height) { /* Need to guess font height [compat] */
4334 int h, i;
8ffb8209
MX
4335 u8 *charmap = font.data;
4336
4337 /*
4338 * If from KDFONTOP ioctl, don't allow things which can be done
4339 * in userland,so that we can get rid of this soon
4340 */
4341 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4342 kfree(font.data);
1da177e4 4343 return -EINVAL;
8ffb8209
MX
4344 }
4345
1da177e4 4346 for (h = 32; h > 0; h--)
8ffb8209
MX
4347 for (i = 0; i < op->charcount; i++)
4348 if (charmap[32*i+h-1])
1da177e4 4349 goto nonzero;
8ffb8209
MX
4350
4351 kfree(font.data);
1da177e4 4352 return -EINVAL;
8ffb8209 4353
1da177e4
LT
4354 nonzero:
4355 op->height = h;
4356 }
8ffb8209 4357
1da177e4 4358 font.charcount = op->charcount;
1da177e4 4359 font.width = op->width;
8ffb8209
MX
4360 font.height = op->height;
4361
ac751efa 4362 console_lock();
edab558f
AC
4363 if (vc->vc_mode != KD_TEXT)
4364 rc = -EINVAL;
4365 else if (vc->vc_sw->con_font_set)
1da177e4
LT
4366 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4367 else
4368 rc = -ENOSYS;
ac751efa 4369 console_unlock();
1da177e4
LT
4370 kfree(font.data);
4371 return rc;
4372}
4373
4374static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4375{
4376 struct console_font font = {.width = op->width, .height = op->height};
4377 char name[MAX_FONT_NAME];
4378 char *s = name;
4379 int rc;
4380
1da177e4
LT
4381
4382 if (!op->data)
4383 s = NULL;
4384 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4385 return -EFAULT;
4386 else
4387 name[MAX_FONT_NAME - 1] = 0;
4388
ac751efa 4389 console_lock();
edab558f
AC
4390 if (vc->vc_mode != KD_TEXT) {
4391 console_unlock();
4392 return -EINVAL;
4393 }
1da177e4
LT
4394 if (vc->vc_sw->con_font_default)
4395 rc = vc->vc_sw->con_font_default(vc, &font, s);
4396 else
4397 rc = -ENOSYS;
ac751efa 4398 console_unlock();
1da177e4
LT
4399 if (!rc) {
4400 op->width = font.width;
4401 op->height = font.height;
4402 }
4403 return rc;
4404}
4405
4406static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4407{
4408 int con = op->height;
4409 int rc;
4410
1da177e4 4411
ac751efa 4412 console_lock();
edab558f
AC
4413 if (vc->vc_mode != KD_TEXT)
4414 rc = -EINVAL;
4415 else if (!vc->vc_sw->con_font_copy)
1da177e4
LT
4416 rc = -ENOSYS;
4417 else if (con < 0 || !vc_cons_allocated(con))
4418 rc = -ENOTTY;
4419 else if (con == vc->vc_num) /* nothing to do */
4420 rc = 0;
4421 else
4422 rc = vc->vc_sw->con_font_copy(vc, con);
ac751efa 4423 console_unlock();
1da177e4
LT
4424 return rc;
4425}
4426
4427int con_font_op(struct vc_data *vc, struct console_font_op *op)
4428{
4429 switch (op->op) {
4430 case KD_FONT_OP_SET:
4431 return con_font_set(vc, op);
4432 case KD_FONT_OP_GET:
4433 return con_font_get(vc, op);
4434 case KD_FONT_OP_SET_DEFAULT:
4435 return con_font_default(vc, op);
4436 case KD_FONT_OP_COPY:
4437 return con_font_copy(vc, op);
4438 }
4439 return -ENOSYS;
4440}
4441
4442/*
4443 * Interface exported to selection and vcs.
4444 */
4445
4446/* used by selection */
4447u16 screen_glyph(struct vc_data *vc, int offset)
4448{
4449 u16 w = scr_readw(screenpos(vc, offset, 1));
4450 u16 c = w & 0xff;
4451
4452 if (w & vc->vc_hi_font_mask)
4453 c |= 0x100;
4454 return c;
4455}
f7511d5f 4456EXPORT_SYMBOL_GPL(screen_glyph);
1da177e4
LT
4457
4458/* used by vcs - note the word offset */
4459unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4460{
4461 return screenpos(vc, 2 * w_offset, viewed);
4462}
88867e3d 4463EXPORT_SYMBOL_GPL(screen_pos);
1da177e4
LT
4464
4465void getconsxy(struct vc_data *vc, unsigned char *p)
4466{
4467 p[0] = vc->vc_x;
4468 p[1] = vc->vc_y;
4469}
4470
4471void putconsxy(struct vc_data *vc, unsigned char *p)
4472{
9477e260 4473 hide_cursor(vc);
1da177e4
LT
4474 gotoxy(vc, p[0], p[1]);
4475 set_cursor(vc);
4476}
4477
4478u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4479{
4480 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4481 return softcursor_original;
4482 return scr_readw(org);
4483}
4484
4485void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4486{
4487 scr_writew(val, org);
4488 if ((unsigned long)org == vc->vc_pos) {
4489 softcursor_original = -1;
4490 add_softcursor(vc);
4491 }
4492}
4493
432c9ed2
NP
4494void vcs_scr_updated(struct vc_data *vc)
4495{
4496 notify_update(vc);
4497}
4498
35cc56f9
JS
4499void vc_scrolldelta_helper(struct vc_data *c, int lines,
4500 unsigned int rolled_over, void *base, unsigned int size)
4501{
4502 unsigned long ubase = (unsigned long)base;
210fd746
JS
4503 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4504 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4505 ptrdiff_t origin = (void *)c->vc_origin - base;
35cc56f9 4506 int margin = c->vc_size_row * 4;
7c918cdc 4507 int from, wrap, from_off, avail;
35cc56f9
JS
4508
4509 /* Turn scrollback off */
4510 if (!lines) {
4511 c->vc_visible_origin = c->vc_origin;
4512 return;
4513 }
4514
4515 /* Do we have already enough to allow jumping from 0 to the end? */
210fd746 4516 if (rolled_over > scr_end + margin) {
7c918cdc
JS
4517 from = scr_end;
4518 wrap = rolled_over + c->vc_size_row;
35cc56f9 4519 } else {
7c918cdc
JS
4520 from = 0;
4521 wrap = size;
35cc56f9
JS
4522 }
4523
7c918cdc
JS
4524 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4525 avail = (origin - from + wrap) % wrap;
35cc56f9
JS
4526
4527 /* Only a little piece would be left? Show all incl. the piece! */
7c918cdc 4528 if (avail < 2 * margin)
35cc56f9 4529 margin = 0;
7c918cdc
JS
4530 if (from_off < margin)
4531 from_off = 0;
4532 if (from_off > avail - margin)
4533 from_off = avail;
35cc56f9 4534
7c918cdc 4535 c->vc_visible_origin = ubase + (from + from_off) % wrap;
35cc56f9
JS
4536}
4537EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4538
1da177e4
LT
4539/*
4540 * Visible symbols for modules
4541 */
4542
4543EXPORT_SYMBOL(color_table);
4544EXPORT_SYMBOL(default_red);
4545EXPORT_SYMBOL(default_grn);
4546EXPORT_SYMBOL(default_blu);
4547EXPORT_SYMBOL(update_region);
4548EXPORT_SYMBOL(redraw_screen);
4549EXPORT_SYMBOL(vc_resize);
4550EXPORT_SYMBOL(fg_console);
4551EXPORT_SYMBOL(console_blank_hook);
4552EXPORT_SYMBOL(console_blanked);
4553EXPORT_SYMBOL(vc_cons);
f6c06b68 4554EXPORT_SYMBOL(global_cursor_default);
1da177e4 4555#ifndef VT_SINGLE_DRIVER
1da177e4
LT
4556EXPORT_SYMBOL(give_up_console);
4557#endif