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