tty: vt, cleanup and document con_scroll
[linux-2.6-block.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>
75#include <linux/sched.h>
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>
1da177e4 105
3e795de7 106#define MAX_NR_CON_DRIVER 16
1da177e4 107
6db4063c 108#define CON_DRIVER_FLAG_MODULE 1
928e964f
AD
109#define CON_DRIVER_FLAG_INIT 2
110#define CON_DRIVER_FLAG_ATTR 4
d364b5c3 111#define CON_DRIVER_FLAG_ZOMBIE 8
3e795de7
AD
112
113struct con_driver {
114 const struct consw *con;
115 const char *desc;
805952a8 116 struct device *dev;
6db4063c 117 int node;
3e795de7
AD
118 int first;
119 int last;
120 int flag;
121};
122
123static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
1da177e4
LT
124const struct consw *conswitchp;
125
126/* A bitmap for codes <32. A bit of 1 indicates that the code
127 * corresponding to that bit number invokes some special action
128 * (such as cursor movement) and should not be displayed as a
129 * glyph unless the disp_ctrl mode is explicitly enabled.
130 */
131#define CTRL_ACTION 0x0d00ff81
132#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
133
134/*
135 * Here is the default bell parameters: 750HZ, 1/8th of a second
136 */
137#define DEFAULT_BELL_PITCH 750
138#define DEFAULT_BELL_DURATION (HZ/8)
bd63364c 139#define DEFAULT_CURSOR_BLINK_MS 200
1da177e4 140
1da177e4
LT
141struct vc vc_cons [MAX_NR_CONSOLES];
142
143#ifndef VT_SINGLE_DRIVER
144static const struct consw *con_driver_map[MAX_NR_CONSOLES];
145#endif
146
147static int con_open(struct tty_struct *, struct file *);
148static void vc_init(struct vc_data *vc, unsigned int rows,
149 unsigned int cols, int do_clear);
150static void gotoxy(struct vc_data *vc, int new_x, int new_y);
151static void save_cur(struct vc_data *vc);
152static void reset_terminal(struct vc_data *vc, int do_clear);
153static void con_flush_chars(struct tty_struct *tty);
403aac96 154static int set_vesa_blanking(char __user *p);
1da177e4
LT
155static void set_cursor(struct vc_data *vc);
156static void hide_cursor(struct vc_data *vc);
65f27f38 157static void console_callback(struct work_struct *ignored);
d364b5c3 158static void con_driver_unregister_callback(struct work_struct *ignored);
1da177e4
LT
159static void blank_screen_t(unsigned long dummy);
160static void set_palette(struct vc_data *vc);
161
52c40fca
PH
162#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
163
1da177e4 164static int printable; /* Is console ready for printing? */
77bf2bab 165int default_utf8 = true;
042f10ec 166module_param(default_utf8, int, S_IRUGO | S_IWUSR);
f6c06b68
MG
167int global_cursor_default = -1;
168module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
1da177e4 169
9ea9a886
CL
170static int cur_default = CUR_DEFAULT;
171module_param(cur_default, int, S_IRUGO | S_IWUSR);
172
1da177e4
LT
173/*
174 * ignore_poke: don't unblank the screen when things are typed. This is
175 * mainly for the privacy of braille terminal users.
176 */
177static int ignore_poke;
178
179int do_poke_blanked_console;
180int console_blanked;
181
182static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
1da177e4 183static int vesa_off_interval;
f324edc8
DM
184static int blankinterval = 10*60;
185core_param(consoleblank, blankinterval, int, 0444);
1da177e4 186
65f27f38 187static DECLARE_WORK(console_work, console_callback);
d364b5c3 188static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
1da177e4
LT
189
190/*
191 * fg_console is the current virtual console,
192 * last_console is the last used one,
193 * want_console is the console we want to switch to,
b45cfba4 194 * saved_* variants are for save/restore around kernel debugger enter/leave
1da177e4
LT
195 */
196int fg_console;
197int last_console;
198int want_console = -1;
fed891c8
JW
199static int saved_fg_console;
200static int saved_last_console;
201static int saved_want_console;
202static int saved_vc_mode;
beed5336 203static int saved_console_blanked;
1da177e4
LT
204
205/*
206 * For each existing display, we have a pointer to console currently visible
207 * on that display, allowing consoles other than fg_console to be refreshed
208 * appropriately. Unless the low-level driver supplies its own display_fg
209 * variable, we use this one for the "master display".
210 */
211static struct vc_data *master_display_fg;
212
213/*
214 * Unfortunately, we need to delay tty echo when we're currently writing to the
215 * console since the code is (and always was) not re-entrant, so we schedule
216 * all flip requests to process context with schedule-task() and run it from
217 * console_callback().
218 */
219
220/*
221 * For the same reason, we defer scrollback to the console callback.
222 */
223static int scrollback_delta;
224
225/*
226 * Hook so that the power management routines can (un)blank
227 * the console on our behalf.
228 */
229int (*console_blank_hook)(int);
230
40565f19 231static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
1da177e4
LT
232static int blank_state;
233static int blank_timer_expired;
234enum {
235 blank_off = 0,
236 blank_normal_wait,
237 blank_vesa_wait,
238};
239
fbc92a34
KS
240/*
241 * /sys/class/tty/tty0/
242 *
243 * the attribute 'active' contains the name of the current vc
244 * console and it supports poll() to detect vc switches
245 */
246static struct device *tty0dev;
247
b293d758
ST
248/*
249 * Notifier list for console events.
250 */
251static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
252
253int register_vt_notifier(struct notifier_block *nb)
254{
255 return atomic_notifier_chain_register(&vt_notifier_list, nb);
256}
257EXPORT_SYMBOL_GPL(register_vt_notifier);
258
259int unregister_vt_notifier(struct notifier_block *nb)
260{
261 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
262}
263EXPORT_SYMBOL_GPL(unregister_vt_notifier);
264
265static void notify_write(struct vc_data *vc, unsigned int unicode)
266{
502fcb79 267 struct vt_notifier_param param = { .vc = vc, .c = unicode };
b293d758
ST
268 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
269}
270
271static void notify_update(struct vc_data *vc)
272{
273 struct vt_notifier_param param = { .vc = vc };
274 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
275}
1da177e4
LT
276/*
277 * Low-Level Functions
278 */
279
6ca8dfd7
JS
280static inline bool con_is_fg(const struct vc_data *vc)
281{
282 return vc->vc_num == fg_console;
283}
284
285static inline bool con_should_update(const struct vc_data *vc)
286{
287 return con_is_visible(vc) && !console_blanked;
288}
1da177e4
LT
289
290static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
291{
292 unsigned short *p;
293
294 if (!viewed)
295 p = (unsigned short *)(vc->vc_origin + offset);
296 else if (!vc->vc_sw->con_screen_pos)
297 p = (unsigned short *)(vc->vc_visible_origin + offset);
298 else
299 p = vc->vc_sw->con_screen_pos(vc, offset);
300 return p;
301}
302
e33ac1c1 303/* Called from the keyboard irq path.. */
1da177e4
LT
304static inline void scrolldelta(int lines)
305{
e33ac1c1
AC
306 /* FIXME */
307 /* scrolldelta needs some kind of consistency lock, but the BKL was
308 and still is not protecting versus the scheduled back end */
1da177e4
LT
309 scrollback_delta += lines;
310 schedule_console_callback();
311}
312
313void schedule_console_callback(void)
314{
315 schedule_work(&console_work);
316}
317
d705ff38
JS
318static void scrup(struct vc_data *vc, unsigned int t, unsigned int b,
319 unsigned int nr)
1da177e4
LT
320{
321 unsigned short *d, *s;
322
323 if (t+nr >= b)
324 nr = b - t - 1;
325 if (b > vc->vc_rows || t >= b || nr < 1)
326 return;
6ca8dfd7 327 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
1da177e4
LT
328 return;
329 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
330 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
331 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
386f40c8 332 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
1da177e4
LT
333 vc->vc_size_row * nr);
334}
335
d705ff38
JS
336static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b,
337 unsigned int nr)
1da177e4
LT
338{
339 unsigned short *s;
340 unsigned int step;
341
342 if (t+nr >= b)
343 nr = b - t - 1;
344 if (b > vc->vc_rows || t >= b || nr < 1)
345 return;
6ca8dfd7 346 if (con_is_visible(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
1da177e4
LT
347 return;
348 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
349 step = vc->vc_cols * nr;
350 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
93f78da4 351 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
1da177e4
LT
352}
353
354static void do_update_region(struct vc_data *vc, unsigned long start, int count)
355{
1da177e4
LT
356 unsigned int xx, yy, offset;
357 u16 *p;
358
359 p = (u16 *) start;
360 if (!vc->vc_sw->con_getxy) {
361 offset = (start - vc->vc_origin) / 2;
362 xx = offset % vc->vc_cols;
363 yy = offset / vc->vc_cols;
364 } else {
365 int nxx, nyy;
366 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
367 xx = nxx; yy = nyy;
368 }
369 for(;;) {
370 u16 attrib = scr_readw(p) & 0xff00;
371 int startx = xx;
372 u16 *q = p;
373 while (xx < vc->vc_cols && count) {
374 if (attrib != (scr_readw(p) & 0xff00)) {
375 if (p > q)
376 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
377 startx = xx;
378 q = p;
379 attrib = scr_readw(p) & 0xff00;
380 }
381 p++;
382 xx++;
383 count--;
384 }
385 if (p > q)
386 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
387 if (!count)
388 break;
389 xx = 0;
390 yy++;
391 if (vc->vc_sw->con_getxy) {
392 p = (u16 *)start;
393 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
394 }
395 }
1da177e4
LT
396}
397
398void update_region(struct vc_data *vc, unsigned long start, int count)
399{
400 WARN_CONSOLE_UNLOCKED();
401
6ca8dfd7 402 if (con_should_update(vc)) {
1da177e4
LT
403 hide_cursor(vc);
404 do_update_region(vc, start, count);
405 set_cursor(vc);
406 }
407}
408
409/* Structure of attributes is hardware-dependent */
410
fa6ce9ab
JE
411static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
412 u8 _underline, u8 _reverse, u8 _italic)
1da177e4
LT
413{
414 if (vc->vc_sw->con_build_attr)
fa6ce9ab
JE
415 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
416 _blink, _underline, _reverse, _italic);
1da177e4 417
1da177e4
LT
418/*
419 * ++roman: I completely changed the attribute format for monochrome
420 * mode (!can_do_color). The formerly used MDA (monochrome display
421 * adapter) format didn't allow the combination of certain effects.
422 * Now the attribute is just a bit vector:
423 * Bit 0..1: intensity (0..2)
424 * Bit 2 : underline
425 * Bit 3 : reverse
426 * Bit 7 : blink
427 */
428 {
c9e587ab 429 u8 a = _color;
1da177e4
LT
430 if (!vc->vc_can_do_color)
431 return _intensity |
fa6ce9ab 432 (_italic ? 2 : 0) |
1da177e4
LT
433 (_underline ? 4 : 0) |
434 (_reverse ? 8 : 0) |
435 (_blink ? 0x80 : 0);
fa6ce9ab
JE
436 if (_italic)
437 a = (a & 0xF0) | vc->vc_itcolor;
438 else if (_underline)
1da177e4
LT
439 a = (a & 0xf0) | vc->vc_ulcolor;
440 else if (_intensity == 0)
441 a = (a & 0xf0) | vc->vc_ulcolor;
442 if (_reverse)
443 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
444 if (_blink)
445 a ^= 0x80;
446 if (_intensity == 2)
447 a ^= 0x08;
448 if (vc->vc_hi_font_mask == 0x100)
449 a <<= 1;
450 return a;
451 }
1da177e4
LT
452}
453
454static void update_attr(struct vc_data *vc)
455{
fa6ce9ab
JE
456 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
457 vc->vc_blink, vc->vc_underline,
458 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
459 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
1da177e4
LT
460}
461
462/* Note: inverting the screen twice should revert to the original state */
463void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
464{
465 unsigned short *p;
466
467 WARN_CONSOLE_UNLOCKED();
468
469 count /= 2;
470 p = screenpos(vc, offset, viewed);
6af39ed3 471 if (vc->vc_sw->con_invert_region) {
1da177e4 472 vc->vc_sw->con_invert_region(vc, p, count);
6af39ed3 473 } else {
1da177e4
LT
474 u16 *q = p;
475 int cnt = count;
476 u16 a;
477
478 if (!vc->vc_can_do_color) {
479 while (cnt--) {
480 a = scr_readw(q);
481 a ^= 0x0800;
482 scr_writew(a, q);
483 q++;
484 }
485 } else if (vc->vc_hi_font_mask == 0x100) {
486 while (cnt--) {
487 a = scr_readw(q);
488 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
489 scr_writew(a, q);
490 q++;
491 }
492 } else {
493 while (cnt--) {
494 a = scr_readw(q);
495 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
496 scr_writew(a, q);
497 q++;
498 }
499 }
500 }
6af39ed3 501
6ca8dfd7 502 if (con_should_update(vc))
1da177e4 503 do_update_region(vc, (unsigned long) p, count);
19e3ae6b 504 notify_update(vc);
1da177e4
LT
505}
506
507/* used by selection: complement pointer position */
508void complement_pos(struct vc_data *vc, int offset)
509{
414edcd3 510 static int old_offset = -1;
1da177e4
LT
511 static unsigned short old;
512 static unsigned short oldx, oldy;
513
514 WARN_CONSOLE_UNLOCKED();
515
414edcd3
AD
516 if (old_offset != -1 && old_offset >= 0 &&
517 old_offset < vc->vc_screenbuf_size) {
518 scr_writew(old, screenpos(vc, old_offset, 1));
6ca8dfd7 519 if (con_should_update(vc))
1da177e4 520 vc->vc_sw->con_putc(vc, old, oldy, oldx);
19e3ae6b 521 notify_update(vc);
1da177e4 522 }
414edcd3
AD
523
524 old_offset = offset;
525
526 if (offset != -1 && offset >= 0 &&
527 offset < vc->vc_screenbuf_size) {
1da177e4 528 unsigned short new;
414edcd3 529 unsigned short *p;
1da177e4
LT
530 p = screenpos(vc, offset, 1);
531 old = scr_readw(p);
532 new = old ^ vc->vc_complement_mask;
533 scr_writew(new, p);
6ca8dfd7 534 if (con_should_update(vc)) {
1da177e4
LT
535 oldx = (offset >> 1) % vc->vc_cols;
536 oldy = (offset >> 1) / vc->vc_cols;
537 vc->vc_sw->con_putc(vc, new, oldy, oldx);
538 }
19e3ae6b 539 notify_update(vc);
1da177e4
LT
540 }
541}
542
543static void insert_char(struct vc_data *vc, unsigned int nr)
544{
81732c3b 545 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 546
a883b70d 547 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
81732c3b 548 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
1da177e4 549 vc->vc_need_wrap = 0;
6ca8dfd7 550 if (con_should_update(vc))
81732c3b 551 do_update_region(vc, (unsigned long) p,
b1a925f4 552 vc->vc_cols - vc->vc_x);
1da177e4
LT
553}
554
555static void delete_char(struct vc_data *vc, unsigned int nr)
556{
81732c3b 557 unsigned short *p = (unsigned short *) vc->vc_pos;
1da177e4 558
b1a925f4 559 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
81732c3b
JFM
560 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
561 nr * 2);
1da177e4 562 vc->vc_need_wrap = 0;
6ca8dfd7 563 if (con_should_update(vc))
81732c3b 564 do_update_region(vc, (unsigned long) p,
b1a925f4 565 vc->vc_cols - vc->vc_x);
1da177e4
LT
566}
567
e882f715 568static int softcursor_original = -1;
1da177e4
LT
569
570static void add_softcursor(struct vc_data *vc)
571{
572 int i = scr_readw((u16 *) vc->vc_pos);
573 u32 type = vc->vc_cursor_type;
574
575 if (! (type & 0x10)) return;
576 if (softcursor_original != -1) return;
577 softcursor_original = i;
578 i |= ((type >> 8) & 0xff00 );
579 i ^= ((type) & 0xff00 );
580 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
581 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
582 scr_writew(i, (u16 *) vc->vc_pos);
6ca8dfd7 583 if (con_should_update(vc))
1da177e4
LT
584 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
585}
586
587static void hide_softcursor(struct vc_data *vc)
588{
589 if (softcursor_original != -1) {
590 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
6ca8dfd7 591 if (con_should_update(vc))
1da177e4
LT
592 vc->vc_sw->con_putc(vc, softcursor_original,
593 vc->vc_y, vc->vc_x);
594 softcursor_original = -1;
595 }
596}
597
598static void hide_cursor(struct vc_data *vc)
599{
600 if (vc == sel_cons)
601 clear_selection();
602 vc->vc_sw->con_cursor(vc, CM_ERASE);
603 hide_softcursor(vc);
604}
605
606static void set_cursor(struct vc_data *vc)
607{
6ca8dfd7 608 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
1da177e4
LT
609 return;
610 if (vc->vc_deccm) {
611 if (vc == sel_cons)
612 clear_selection();
613 add_softcursor(vc);
614 if ((vc->vc_cursor_type & 0x0f) != 1)
615 vc->vc_sw->con_cursor(vc, CM_DRAW);
616 } else
617 hide_cursor(vc);
618}
619
620static void set_origin(struct vc_data *vc)
621{
622 WARN_CONSOLE_UNLOCKED();
623
6ca8dfd7 624 if (!con_is_visible(vc) ||
1da177e4
LT
625 !vc->vc_sw->con_set_origin ||
626 !vc->vc_sw->con_set_origin(vc))
627 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
628 vc->vc_visible_origin = vc->vc_origin;
629 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
630 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
631}
632
c0f160a7 633static void save_screen(struct vc_data *vc)
1da177e4
LT
634{
635 WARN_CONSOLE_UNLOCKED();
636
637 if (vc->vc_sw->con_save_screen)
638 vc->vc_sw->con_save_screen(vc);
639}
640
641/*
642 * Redrawing of screen
643 */
644
2a248307 645void clear_buffer_attributes(struct vc_data *vc)
1da177e4
LT
646{
647 unsigned short *p = (unsigned short *)vc->vc_origin;
648 int count = vc->vc_screenbuf_size / 2;
649 int mask = vc->vc_hi_font_mask | 0xff;
650
651 for (; count > 0; count--, p++) {
652 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
653 }
654}
655
656void redraw_screen(struct vc_data *vc, int is_switch)
657{
658 int redraw = 0;
659
660 WARN_CONSOLE_UNLOCKED();
661
662 if (!vc) {
663 /* strange ... */
664 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
665 return;
666 }
667
668 if (is_switch) {
669 struct vc_data *old_vc = vc_cons[fg_console].d;
670 if (old_vc == vc)
671 return;
6ca8dfd7 672 if (!con_is_visible(vc))
1da177e4
LT
673 redraw = 1;
674 *vc->vc_display_fg = vc;
675 fg_console = vc->vc_num;
676 hide_cursor(old_vc);
6ca8dfd7 677 if (!con_is_visible(old_vc)) {
1da177e4
LT
678 save_screen(old_vc);
679 set_origin(old_vc);
680 }
fbc92a34
KS
681 if (tty0dev)
682 sysfs_notify(&tty0dev->kobj, NULL, "active");
1da177e4
LT
683 } else {
684 hide_cursor(vc);
685 redraw = 1;
686 }
687
688 if (redraw) {
689 int update;
690 int old_was_color = vc->vc_can_do_color;
691
692 set_origin(vc);
693 update = vc->vc_sw->con_switch(vc);
694 set_palette(vc);
695 /*
696 * If console changed from mono<->color, the best we can do
697 * is to clear the buffer attributes. As it currently stands,
698 * rebuilding new attributes from the old buffer is not doable
699 * without overly complex code.
700 */
701 if (old_was_color != vc->vc_can_do_color) {
702 update_attr(vc);
703 clear_buffer_attributes(vc);
704 }
8fd4bd22
JB
705
706 /* Forcibly update if we're panicing */
707 if ((update && vc->vc_mode != KD_GRAPHICS) ||
708 vt_force_oops_output(vc))
1da177e4
LT
709 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
710 }
711 set_cursor(vc);
712 if (is_switch) {
713 set_leds();
714 compute_shiftstate();
8182ec49 715 notify_update(vc);
1da177e4
LT
716 }
717}
718
719/*
720 * Allocation, freeing and resizing of VTs.
721 */
722
723int vc_cons_allocated(unsigned int i)
724{
725 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
726}
727
728static void visual_init(struct vc_data *vc, int num, int init)
729{
730 /* ++Geert: vc->vc_sw->con_init determines console size */
731 if (vc->vc_sw)
732 module_put(vc->vc_sw->owner);
733 vc->vc_sw = conswitchp;
734#ifndef VT_SINGLE_DRIVER
735 if (con_driver_map[num])
736 vc->vc_sw = con_driver_map[num];
737#endif
738 __module_get(vc->vc_sw->owner);
739 vc->vc_num = num;
740 vc->vc_display_fg = &master_display_fg;
08b33249
DZ
741 if (vc->vc_uni_pagedir_loc)
742 con_free_unimap(vc);
1da177e4 743 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
e4bdab70 744 vc->vc_uni_pagedir = NULL;
1da177e4
LT
745 vc->vc_hi_font_mask = 0;
746 vc->vc_complement_mask = 0;
747 vc->vc_can_do_color = 0;
8fd4bd22 748 vc->vc_panic_force_write = false;
1b45996d 749 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
750 vc->vc_sw->con_init(vc, init);
751 if (!vc->vc_complement_mask)
752 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
753 vc->vc_s_complement_mask = vc->vc_complement_mask;
754 vc->vc_size_row = vc->vc_cols << 1;
755 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
756}
757
758int vc_allocate(unsigned int currcons) /* return 0 on success */
759{
34902b7f
JS
760 struct vt_notifier_param param;
761 struct vc_data *vc;
762
1da177e4
LT
763 WARN_CONSOLE_UNLOCKED();
764
765 if (currcons >= MAX_NR_CONSOLES)
766 return -ENXIO;
34902b7f
JS
767
768 if (vc_cons[currcons].d)
769 return 0;
770
771 /* due to the granularity of kmalloc, we waste some memory here */
772 /* the alloc is done in two steps, to optimize the common situation
773 of a 25x80 console (structsize=216, screenbuf_size=4000) */
774 /* although the numbers above are not valid since long ago, the
775 point is still up-to-date and the comment still has its value
776 even if only as a historical artifact. --mj, July 1998 */
777 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
778 if (!vc)
1da177e4 779 return -ENOMEM;
34902b7f
JS
780
781 vc_cons[currcons].d = vc;
782 tty_port_init(&vc->port);
783 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
784
785 visual_init(vc, currcons, 1);
786
787 if (!*vc->vc_uni_pagedir_loc)
1da177e4 788 con_set_default_unimap(vc);
f6c06b68 789
34902b7f
JS
790 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
791 if (!vc->vc_screenbuf)
792 goto err_free;
793
794 /* If no drivers have overridden us and the user didn't pass a
795 boot option, default to displaying the cursor */
796 if (global_cursor_default == -1)
797 global_cursor_default = 1;
798
799 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
800 vcs_make_sysfs(currcons);
801 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
f6c06b68 802
1da177e4 803 return 0;
34902b7f
JS
804err_free:
805 kfree(vc);
806 vc_cons[currcons].d = NULL;
807 return -ENOMEM;
1da177e4
LT
808}
809
e400b6ec
AD
810static inline int resize_screen(struct vc_data *vc, int width, int height,
811 int user)
1da177e4
LT
812{
813 /* Resizes the resolution of the display adapater */
814 int err = 0;
815
816 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
e400b6ec
AD
817 err = vc->vc_sw->con_resize(vc, width, height, user);
818
1da177e4
LT
819 return err;
820}
821
822/*
823 * Change # of rows and columns (0 means unchanged/the size of fg_console)
824 * [this is to be used together with some user program
825 * like resize that changes the hardware videomode]
826 */
827#define VC_RESIZE_MAXCOL (32767)
828#define VC_RESIZE_MAXROW (32767)
8c9a9dd0
AC
829
830/**
831 * vc_do_resize - resizing method for the tty
832 * @tty: tty being resized
833 * @real_tty: real tty (different to tty if a pty/tty pair)
834 * @vc: virtual console private data
835 * @cols: columns
836 * @lines: lines
837 *
838 * Resize a virtual console, clipping according to the actual constraints.
839 * If the caller passes a tty structure then update the termios winsize
3ad2f3fb 840 * information and perform any necessary signal handling.
8c9a9dd0 841 *
6a1c0680 842 * Caller must hold the console semaphore. Takes the termios rwsem and
8c9a9dd0
AC
843 * ctrl_lock of the tty IFF a tty is passed.
844 */
845
fc6f6238
AC
846static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
847 unsigned int cols, unsigned int lines)
1da177e4
LT
848{
849 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
9e0ba741 850 unsigned long end;
f2ee4ae8 851 unsigned int old_rows, old_row_size;
1da177e4 852 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
9e0ba741 853 unsigned int user;
1da177e4
LT
854 unsigned short *newscreen;
855
856 WARN_CONSOLE_UNLOCKED();
857
858 if (!vc)
859 return -ENXIO;
860
e400b6ec
AD
861 user = vc->vc_resize_user;
862 vc->vc_resize_user = 0;
863
1da177e4
LT
864 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
865 return -EINVAL;
866
867 new_cols = (cols ? cols : vc->vc_cols);
868 new_rows = (lines ? lines : vc->vc_rows);
869 new_row_size = new_cols << 1;
870 new_screen_size = new_row_size * new_rows;
871
872 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
873 return 0;
874
5cbded58 875 newscreen = kmalloc(new_screen_size, GFP_USER);
1da177e4
LT
876 if (!newscreen)
877 return -ENOMEM;
878
879 old_rows = vc->vc_rows;
1da177e4 880 old_row_size = vc->vc_size_row;
1da177e4 881
e400b6ec 882 err = resize_screen(vc, new_cols, new_rows, user);
1da177e4
LT
883 if (err) {
884 kfree(newscreen);
885 return err;
886 }
887
888 vc->vc_rows = new_rows;
889 vc->vc_cols = new_cols;
890 vc->vc_size_row = new_row_size;
891 vc->vc_screenbuf_size = new_screen_size;
892
893 rlth = min(old_row_size, new_row_size);
894 rrem = new_row_size - rlth;
895 old_origin = vc->vc_origin;
896 new_origin = (long) newscreen;
897 new_scr_end = new_origin + new_screen_size;
3b41dc1a
AD
898
899 if (vc->vc_y > new_rows) {
900 if (old_rows - vc->vc_y < new_rows) {
901 /*
902 * Cursor near the bottom, copy contents from the
903 * bottom of buffer
904 */
905 old_origin += (old_rows - new_rows) * old_row_size;
3b41dc1a
AD
906 } else {
907 /*
908 * Cursor is in no man's land, copy 1/2 screenful
909 * from the top and bottom of cursor position
910 */
911 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
3b41dc1a 912 }
9fc2b2d0
FJ
913 }
914
915 end = old_origin + old_row_size * min(old_rows, new_rows);
1da177e4
LT
916
917 update_attr(vc);
918
3b41dc1a
AD
919 while (old_origin < end) {
920 scr_memcpyw((unsigned short *) new_origin,
921 (unsigned short *) old_origin, rlth);
1da177e4 922 if (rrem)
3b41dc1a
AD
923 scr_memsetw((void *)(new_origin + rlth),
924 vc->vc_video_erase_char, rrem);
1da177e4
LT
925 old_origin += old_row_size;
926 new_origin += new_row_size;
927 }
928 if (new_scr_end > new_origin)
3b41dc1a
AD
929 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
930 new_scr_end - new_origin);
5c9228f0 931 kfree(vc->vc_screenbuf);
1da177e4 932 vc->vc_screenbuf = newscreen;
1da177e4
LT
933 vc->vc_screenbuf_size = new_screen_size;
934 set_origin(vc);
935
936 /* do part of a reset_terminal() */
937 vc->vc_top = 0;
938 vc->vc_bottom = vc->vc_rows;
939 gotoxy(vc, vc->vc_x, vc->vc_y);
940 save_cur(vc);
941
8c9a9dd0
AC
942 if (tty) {
943 /* Rewrite the requested winsize data with the actual
944 resulting sizes */
945 struct winsize ws;
1da177e4
LT
946 memset(&ws, 0, sizeof(ws));
947 ws.ws_row = vc->vc_rows;
948 ws.ws_col = vc->vc_cols;
949 ws.ws_ypixel = vc->vc_scan_lines;
fc6f6238 950 tty_do_resize(tty, &ws);
1da177e4
LT
951 }
952
6ca8dfd7 953 if (con_is_visible(vc))
1da177e4 954 update_screen(vc);
8b92e87d 955 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
1da177e4
LT
956 return err;
957}
958
8c9a9dd0
AC
959/**
960 * vc_resize - resize a VT
961 * @vc: virtual console
962 * @cols: columns
963 * @rows: rows
964 *
965 * Resize a virtual console as seen from the console end of things. We
966 * use the common vc_do_resize methods to update the structures. The
967 * caller must hold the console sem to protect console internals and
8ce73264 968 * vc->port.tty
8c9a9dd0
AC
969 */
970
971int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
972{
8ce73264 973 return vc_do_resize(vc->port.tty, vc, cols, rows);
8c9a9dd0
AC
974}
975
976/**
977 * vt_resize - resize a VT
978 * @tty: tty to resize
8c9a9dd0
AC
979 * @ws: winsize attributes
980 *
981 * Resize a virtual terminal. This is called by the tty layer as we
982 * register our own handler for resizing. The mutual helper does all
983 * the actual work.
984 *
985 * Takes the console sem and the called methods then take the tty
6a1c0680 986 * termios_rwsem and the tty ctrl_lock in that order.
8c9a9dd0 987 */
da2bdf9a 988static int vt_resize(struct tty_struct *tty, struct winsize *ws)
ca9bda00 989{
8c9a9dd0
AC
990 struct vc_data *vc = tty->driver_data;
991 int ret;
ca9bda00 992
ac751efa 993 console_lock();
fc6f6238 994 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
ac751efa 995 console_unlock();
8c9a9dd0 996 return ret;
ca9bda00 997}
1da177e4 998
421b40a6 999struct vc_data *vc_deallocate(unsigned int currcons)
1da177e4 1000{
421b40a6
PH
1001 struct vc_data *vc = NULL;
1002
1da177e4
LT
1003 WARN_CONSOLE_UNLOCKED();
1004
1005 if (vc_cons_allocated(currcons)) {
421b40a6 1006 struct vt_notifier_param param;
4995f8ef 1007
421b40a6 1008 param.vc = vc = vc_cons[currcons].d;
b293d758 1009 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
4995f8ef 1010 vcs_remove_sysfs(currcons);
1da177e4 1011 vc->vc_sw->con_deinit(vc);
bde0d2c9 1012 put_pid(vc->vt_pid);
d459ec0b 1013 module_put(vc->vc_sw->owner);
5c9228f0 1014 kfree(vc->vc_screenbuf);
1da177e4
LT
1015 vc_cons[currcons].d = NULL;
1016 }
421b40a6 1017 return vc;
1da177e4
LT
1018}
1019
1020/*
1021 * VT102 emulator
1022 */
1023
079c9534
AC
1024#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1025#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1026#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
1da177e4
LT
1027
1028#define decarm VC_REPEAT
1029#define decckm VC_CKMODE
1030#define kbdapplic VC_APPLIC
1031#define lnm VC_CRLF
1032
1033/*
1034 * this is what the terminal answers to a ESC-Z or csi0c query.
1035 */
1036#define VT100ID "\033[?1;2c"
1037#define VT102ID "\033[?6c"
1038
8ede5cce 1039const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1da177e4
LT
1040 8,12,10,14, 9,13,11,15 };
1041
1042/* the default colour table, for VGA+ colour systems */
91e74ca5
JS
1043unsigned char default_red[] = {
1044 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1045 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1046};
1047module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
1048
1049unsigned char default_grn[] = {
1050 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1051 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1052};
1053module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1054
1055unsigned char default_blu[] = {
1056 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1057 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1058};
1059module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
1c2bbe6a 1060
1da177e4
LT
1061/*
1062 * gotoxy() must verify all boundaries, because the arguments
1063 * might also be negative. If the given position is out of
1064 * bounds, the cursor is placed at the nearest margin.
1065 */
1066static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1067{
1068 int min_y, max_y;
1069
1070 if (new_x < 0)
1071 vc->vc_x = 0;
1072 else {
1073 if (new_x >= vc->vc_cols)
1074 vc->vc_x = vc->vc_cols - 1;
1075 else
1076 vc->vc_x = new_x;
1077 }
1078
1079 if (vc->vc_decom) {
1080 min_y = vc->vc_top;
1081 max_y = vc->vc_bottom;
1082 } else {
1083 min_y = 0;
1084 max_y = vc->vc_rows;
1085 }
1086 if (new_y < min_y)
1087 vc->vc_y = min_y;
1088 else if (new_y >= max_y)
1089 vc->vc_y = max_y - 1;
1090 else
1091 vc->vc_y = new_y;
1092 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1093 vc->vc_need_wrap = 0;
1094}
1095
1096/* for absolute user moves, when decom is set */
1097static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1098{
1099 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1100}
1101
1b0ec88a 1102void scrollback(struct vc_data *vc)
1da177e4 1103{
1b0ec88a 1104 scrolldelta(-(vc->vc_rows / 2));
1da177e4
LT
1105}
1106
1107void scrollfront(struct vc_data *vc, int lines)
1108{
1109 if (!lines)
1110 lines = vc->vc_rows / 2;
1111 scrolldelta(lines);
1112}
1113
1114static void lf(struct vc_data *vc)
1115{
1116 /* don't scroll if above bottom of scrolling region, or
1117 * if below scrolling region
1118 */
1119 if (vc->vc_y + 1 == vc->vc_bottom)
1120 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1121 else if (vc->vc_y < vc->vc_rows - 1) {
1122 vc->vc_y++;
1123 vc->vc_pos += vc->vc_size_row;
1124 }
1125 vc->vc_need_wrap = 0;
b293d758 1126 notify_write(vc, '\n');
1da177e4
LT
1127}
1128
1129static void ri(struct vc_data *vc)
1130{
1131 /* don't scroll if below top of scrolling region, or
1132 * if above scrolling region
1133 */
1134 if (vc->vc_y == vc->vc_top)
1135 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1136 else if (vc->vc_y > 0) {
1137 vc->vc_y--;
1138 vc->vc_pos -= vc->vc_size_row;
1139 }
1140 vc->vc_need_wrap = 0;
1141}
1142
1143static inline void cr(struct vc_data *vc)
1144{
1145 vc->vc_pos -= vc->vc_x << 1;
1146 vc->vc_need_wrap = vc->vc_x = 0;
b293d758 1147 notify_write(vc, '\r');
1da177e4
LT
1148}
1149
1150static inline void bs(struct vc_data *vc)
1151{
1152 if (vc->vc_x) {
1153 vc->vc_pos -= 2;
1154 vc->vc_x--;
1155 vc->vc_need_wrap = 0;
b293d758 1156 notify_write(vc, '\b');
1da177e4
LT
1157 }
1158}
1159
1160static inline void del(struct vc_data *vc)
1161{
1162 /* ignored */
1163}
1164
1165static void csi_J(struct vc_data *vc, int vpar)
1166{
1167 unsigned int count;
1168 unsigned short * start;
1169
1170 switch (vpar) {
1171 case 0: /* erase from cursor to end of display */
1172 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1173 start = (unsigned short *)vc->vc_pos;
1da177e4
LT
1174 break;
1175 case 1: /* erase from start to cursor */
1176 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1177 start = (unsigned short *)vc->vc_origin;
1da177e4 1178 break;
f8df13e0
PP
1179 case 3: /* erase scroll-back buffer (and whole display) */
1180 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1181 vc->vc_screenbuf_size >> 1);
1182 set_origin(vc);
6ca8dfd7 1183 if (con_is_visible(vc))
0930b095 1184 update_screen(vc);
f8df13e0 1185 /* fall through */
1da177e4
LT
1186 case 2: /* erase whole display */
1187 count = vc->vc_cols * vc->vc_rows;
1188 start = (unsigned short *)vc->vc_origin;
1da177e4
LT
1189 break;
1190 default:
1191 return;
1192 }
1193 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
6ca8dfd7 1194 if (con_should_update(vc))
81732c3b 1195 do_update_region(vc, (unsigned long) start, count);
1da177e4
LT
1196 vc->vc_need_wrap = 0;
1197}
1198
1199static void csi_K(struct vc_data *vc, int vpar)
1200{
1201 unsigned int count;
1202 unsigned short * start;
1203
1204 switch (vpar) {
1205 case 0: /* erase from cursor to end of line */
1206 count = vc->vc_cols - vc->vc_x;
1207 start = (unsigned short *)vc->vc_pos;
1da177e4
LT
1208 break;
1209 case 1: /* erase from start of line to cursor */
1210 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1211 count = vc->vc_x + 1;
1da177e4
LT
1212 break;
1213 case 2: /* erase whole line */
1214 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1215 count = vc->vc_cols;
1da177e4
LT
1216 break;
1217 default:
1218 return;
1219 }
1220 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1221 vc->vc_need_wrap = 0;
6ca8dfd7 1222 if (con_should_update(vc))
81732c3b 1223 do_update_region(vc, (unsigned long) start, count);
1da177e4
LT
1224}
1225
1226static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1227{ /* not vt100? */
1228 int count;
1229
1230 if (!vpar)
1231 vpar++;
1232 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1233
1234 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
6ca8dfd7 1235 if (con_should_update(vc))
1da177e4
LT
1236 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1237 vc->vc_need_wrap = 0;
1238}
1239
1240static void default_attr(struct vc_data *vc)
1241{
1242 vc->vc_intensity = 1;
fa6ce9ab 1243 vc->vc_italic = 0;
1da177e4
LT
1244 vc->vc_underline = 0;
1245 vc->vc_reverse = 0;
1246 vc->vc_blink = 0;
1247 vc->vc_color = vc->vc_def_color;
1248}
1249
cec5b2a9
AB
1250struct rgb { u8 r; u8 g; u8 b; };
1251
0f91e142 1252static void rgb_from_256(int i, struct rgb *c)
cec5b2a9 1253{
cec5b2a9 1254 if (i < 8) { /* Standard colours. */
0f91e142
JS
1255 c->r = i&1 ? 0xaa : 0x00;
1256 c->g = i&2 ? 0xaa : 0x00;
1257 c->b = i&4 ? 0xaa : 0x00;
cec5b2a9 1258 } else if (i < 16) {
0f91e142
JS
1259 c->r = i&1 ? 0xff : 0x55;
1260 c->g = i&2 ? 0xff : 0x55;
1261 c->b = i&4 ? 0xff : 0x55;
cec5b2a9 1262 } else if (i < 232) { /* 6x6x6 colour cube. */
0f91e142
JS
1263 c->r = (i - 16) / 36 * 85 / 2;
1264 c->g = (i - 16) / 6 % 6 * 85 / 2;
1265 c->b = (i - 16) % 6 * 85 / 2;
cec5b2a9 1266 } else /* Grayscale ramp. */
0f91e142 1267 c->r = c->g = c->b = i * 10 - 2312;
cec5b2a9
AB
1268}
1269
0f91e142 1270static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
cec5b2a9 1271{
193df022
JS
1272 u8 hue = 0, max = max3(c->r, c->g, c->b);
1273
1274 if (c->r > max / 2)
1275 hue |= 4;
1276 if (c->g > max / 2)
1277 hue |= 2;
1278 if (c->b > max / 2)
1279 hue |= 1;
1280
1281 if (hue == 7 && max <= 0x55) {
1282 hue = 0;
1283 vc->vc_intensity = 2;
1284 } else if (max > 0xaa)
1285 vc->vc_intensity = 2;
cec5b2a9 1286 else
193df022
JS
1287 vc->vc_intensity = 1;
1288
cec5b2a9
AB
1289 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1290}
1291
0f91e142 1292static void rgb_background(struct vc_data *vc, const struct rgb *c)
cec5b2a9
AB
1293{
1294 /* For backgrounds, err on the dark side. */
1295 vc->vc_color = (vc->vc_color & 0x0f)
0f91e142 1296 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
cec5b2a9
AB
1297}
1298
e05ab238
JS
1299/*
1300 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1301 * and thus need to be detected and ignored by hand. Strictly speaking, that
1302 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1303 * no one produces such codes and almost no one accepts them.
1304 *
1305 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1306 * supporting them.
1307 */
1308static int vc_t416_color(struct vc_data *vc, int i,
0f91e142 1309 void(*set_color)(struct vc_data *vc, const struct rgb *c))
e05ab238 1310{
0f91e142
JS
1311 struct rgb c;
1312
e05ab238
JS
1313 i++;
1314 if (i > vc->vc_npar)
1315 return i;
1316
0bf1f4a8 1317 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
3e7ec4a0 1318 /* 256 colours */
e05ab238 1319 i++;
0f91e142 1320 rgb_from_256(vc->vc_par[i], &c);
669e0a51 1321 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
3e7ec4a0 1322 /* 24 bit */
0f91e142
JS
1323 c.r = vc->vc_par[i + 1];
1324 c.g = vc->vc_par[i + 2];
1325 c.b = vc->vc_par[i + 3];
e05ab238 1326 i += 3;
0f91e142
JS
1327 } else
1328 return i;
1329
1330 set_color(vc, &c);
e05ab238
JS
1331
1332 return i;
1333}
1334
ac751efa 1335/* console_lock is held */
1da177e4
LT
1336static void csi_m(struct vc_data *vc)
1337{
1338 int i;
1339
1340 for (i = 0; i <= vc->vc_npar; i++)
1341 switch (vc->vc_par[i]) {
aada0a34
JS
1342 case 0: /* all attributes off */
1343 default_attr(vc);
1344 break;
1345 case 1:
1346 vc->vc_intensity = 2;
1347 break;
1348 case 2:
1349 vc->vc_intensity = 0;
1350 break;
1351 case 3:
1352 vc->vc_italic = 1;
1353 break;
1354 case 4:
1355 vc->vc_underline = 1;
1356 break;
1357 case 5:
1358 vc->vc_blink = 1;
1359 break;
1360 case 7:
1361 vc->vc_reverse = 1;
1362 break;
1363 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1364 * Select primary font, don't display control chars if
1365 * defined, don't set bit 8 on output.
1366 */
1367 vc->vc_translate = set_translate(vc->vc_charset == 0
1368 ? vc->vc_G0_charset
1369 : vc->vc_G1_charset, vc);
1370 vc->vc_disp_ctrl = 0;
1371 vc->vc_toggle_meta = 0;
1372 break;
1373 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1374 * Select first alternate font, lets chars < 32 be
1375 * displayed as ROM chars.
1376 */
1377 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1378 vc->vc_disp_ctrl = 1;
1379 vc->vc_toggle_meta = 0;
1380 break;
1381 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1382 * Select second alternate font, toggle high bit
1383 * before displaying as ROM char.
1384 */
1385 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1386 vc->vc_disp_ctrl = 1;
1387 vc->vc_toggle_meta = 1;
1388 break;
1389 case 21:
1390 case 22:
1391 vc->vc_intensity = 1;
1392 break;
1393 case 23:
1394 vc->vc_italic = 0;
1395 break;
1396 case 24:
1397 vc->vc_underline = 0;
1398 break;
1399 case 25:
1400 vc->vc_blink = 0;
1401 break;
1402 case 27:
1403 vc->vc_reverse = 0;
1404 break;
1405 case 38:
1406 i = vc_t416_color(vc, i, rgb_foreground);
1407 break;
1408 case 48:
1409 i = vc_t416_color(vc, i, rgb_background);
1410 break;
1411 case 39:
1412 vc->vc_color = (vc->vc_def_color & 0x0f) |
1413 (vc->vc_color & 0xf0);
1414 break;
1415 case 49:
1416 vc->vc_color = (vc->vc_def_color & 0xf0) |
1417 (vc->vc_color & 0x0f);
1418 break;
1419 default:
fadb4244
AB
1420 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1421 if (vc->vc_par[i] < 100)
1422 vc->vc_intensity = 2;
cc67dc28
AB
1423 vc->vc_par[i] -= 60;
1424 }
aada0a34
JS
1425 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1426 vc->vc_color = color_table[vc->vc_par[i] - 30]
1427 | (vc->vc_color & 0xf0);
1428 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1429 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1430 | (vc->vc_color & 0x0f);
1431 break;
1da177e4
LT
1432 }
1433 update_attr(vc);
1434}
1435
6732c8bb 1436static void respond_string(const char *p, struct tty_port *port)
1da177e4
LT
1437{
1438 while (*p) {
6732c8bb 1439 tty_insert_flip_char(port, *p, 0);
1da177e4
LT
1440 p++;
1441 }
6732c8bb 1442 tty_schedule_flip(port);
1da177e4
LT
1443}
1444
1445static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1446{
1447 char buf[40];
1448
1449 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
6732c8bb 1450 respond_string(buf, tty->port);
1da177e4
LT
1451}
1452
1453static inline void status_report(struct tty_struct *tty)
1454{
6732c8bb 1455 respond_string("\033[0n", tty->port); /* Terminal ok */
1da177e4
LT
1456}
1457
6732c8bb 1458static inline void respond_ID(struct tty_struct *tty)
1da177e4 1459{
6732c8bb 1460 respond_string(VT102ID, tty->port);
1da177e4
LT
1461}
1462
1463void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1464{
1465 char buf[8];
1466
1467 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1468 (char)('!' + mry));
6732c8bb 1469 respond_string(buf, tty->port);
1da177e4
LT
1470}
1471
1472/* invoked via ioctl(TIOCLINUX) and through set_selection */
1473int mouse_reporting(void)
1474{
1475 return vc_cons[fg_console].d->vc_report_mouse;
1476}
1477
ac751efa 1478/* console_lock is held */
1da177e4
LT
1479static void set_mode(struct vc_data *vc, int on_off)
1480{
1481 int i;
1482
1483 for (i = 0; i <= vc->vc_npar; i++)
1484 if (vc->vc_ques) {
1485 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1486 case 1: /* Cursor keys send ^[Ox/^[[x */
1487 if (on_off)
1488 set_kbd(vc, decckm);
1489 else
1490 clr_kbd(vc, decckm);
1491 break;
1492 case 3: /* 80/132 mode switch unimplemented */
1da177e4
LT
1493#if 0
1494 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1495 /* this alone does not suffice; some user mode
1496 utility has to change the hardware regs */
1497#endif
1498 break;
1499 case 5: /* Inverted screen on/off */
1500 if (vc->vc_decscnm != on_off) {
1501 vc->vc_decscnm = on_off;
1502 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1503 update_attr(vc);
1504 }
1505 break;
1506 case 6: /* Origin relative/absolute */
1507 vc->vc_decom = on_off;
1508 gotoxay(vc, 0, 0);
1509 break;
1510 case 7: /* Autowrap on/off */
1511 vc->vc_decawm = on_off;
1512 break;
1513 case 8: /* Autorepeat on/off */
1514 if (on_off)
1515 set_kbd(vc, decarm);
1516 else
1517 clr_kbd(vc, decarm);
1518 break;
1519 case 9:
1520 vc->vc_report_mouse = on_off ? 1 : 0;
1521 break;
1522 case 25: /* Cursor on/off */
1523 vc->vc_deccm = on_off;
1524 break;
1525 case 1000:
1526 vc->vc_report_mouse = on_off ? 2 : 0;
1527 break;
1528 }
1529 } else {
1530 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1531 case 3: /* Monitor (display ctrls) */
1532 vc->vc_disp_ctrl = on_off;
1533 break;
1534 case 4: /* Insert Mode on/off */
1535 vc->vc_decim = on_off;
1536 break;
1537 case 20: /* Lf, Enter == CrLf/Lf */
1538 if (on_off)
1539 set_kbd(vc, lnm);
1540 else
1541 clr_kbd(vc, lnm);
1542 break;
1543 }
1544 }
1545}
1546
ac751efa 1547/* console_lock is held */
1da177e4
LT
1548static void setterm_command(struct vc_data *vc)
1549{
1550 switch(vc->vc_par[0]) {
1551 case 1: /* set color for underline mode */
1552 if (vc->vc_can_do_color &&
1553 vc->vc_par[1] < 16) {
1554 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1555 if (vc->vc_underline)
1556 update_attr(vc);
1557 }
1558 break;
1559 case 2: /* set color for half intensity mode */
1560 if (vc->vc_can_do_color &&
1561 vc->vc_par[1] < 16) {
1562 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1563 if (vc->vc_intensity == 0)
1564 update_attr(vc);
1565 }
1566 break;
1567 case 8: /* store colors as defaults */
1568 vc->vc_def_color = vc->vc_attr;
1569 if (vc->vc_hi_font_mask == 0x100)
1570 vc->vc_def_color >>= 1;
1571 default_attr(vc);
1572 update_attr(vc);
1573 break;
1574 case 9: /* set blanking interval */
f324edc8 1575 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1da177e4
LT
1576 poke_blanked_console();
1577 break;
1578 case 10: /* set bell frequency in Hz */
1579 if (vc->vc_npar >= 1)
1580 vc->vc_bell_pitch = vc->vc_par[1];
1581 else
1582 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1583 break;
1584 case 11: /* set bell duration in msec */
1585 if (vc->vc_npar >= 1)
1586 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
3372ec28 1587 msecs_to_jiffies(vc->vc_par[1]) : 0;
1da177e4
LT
1588 else
1589 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1590 break;
1591 case 12: /* bring specified console to the front */
1592 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1593 set_console(vc->vc_par[1] - 1);
1594 break;
1595 case 13: /* unblank the screen */
1596 poke_blanked_console();
1597 break;
1598 case 14: /* set vesa powerdown interval */
1599 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1600 break;
1601 case 15: /* activate the previous console */
1602 set_console(last_console);
1603 break;
bd63364c
SD
1604 case 16: /* set cursor blink duration in msec */
1605 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1606 vc->vc_par[1] <= USHRT_MAX)
1607 vc->vc_cur_blink_ms = vc->vc_par[1];
1608 else
1609 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1610 break;
1da177e4
LT
1611 }
1612}
1613
ac751efa 1614/* console_lock is held */
1da177e4
LT
1615static void csi_at(struct vc_data *vc, unsigned int nr)
1616{
1617 if (nr > vc->vc_cols - vc->vc_x)
1618 nr = vc->vc_cols - vc->vc_x;
1619 else if (!nr)
1620 nr = 1;
1621 insert_char(vc, nr);
1622}
1623
ac751efa 1624/* console_lock is held */
1da177e4
LT
1625static void csi_L(struct vc_data *vc, unsigned int nr)
1626{
1627 if (nr > vc->vc_rows - vc->vc_y)
1628 nr = vc->vc_rows - vc->vc_y;
1629 else if (!nr)
1630 nr = 1;
1631 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1632 vc->vc_need_wrap = 0;
1633}
1634
ac751efa 1635/* console_lock is held */
1da177e4
LT
1636static void csi_P(struct vc_data *vc, unsigned int nr)
1637{
1638 if (nr > vc->vc_cols - vc->vc_x)
1639 nr = vc->vc_cols - vc->vc_x;
1640 else if (!nr)
1641 nr = 1;
1642 delete_char(vc, nr);
1643}
1644
ac751efa 1645/* console_lock is held */
1da177e4
LT
1646static void csi_M(struct vc_data *vc, unsigned int nr)
1647{
1648 if (nr > vc->vc_rows - vc->vc_y)
1649 nr = vc->vc_rows - vc->vc_y;
1650 else if (!nr)
1651 nr=1;
1652 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1653 vc->vc_need_wrap = 0;
1654}
1655
ac751efa 1656/* console_lock is held (except via vc_init->reset_terminal */
1da177e4
LT
1657static void save_cur(struct vc_data *vc)
1658{
1659 vc->vc_saved_x = vc->vc_x;
1660 vc->vc_saved_y = vc->vc_y;
1661 vc->vc_s_intensity = vc->vc_intensity;
fa6ce9ab 1662 vc->vc_s_italic = vc->vc_italic;
1da177e4
LT
1663 vc->vc_s_underline = vc->vc_underline;
1664 vc->vc_s_blink = vc->vc_blink;
1665 vc->vc_s_reverse = vc->vc_reverse;
1666 vc->vc_s_charset = vc->vc_charset;
1667 vc->vc_s_color = vc->vc_color;
1668 vc->vc_saved_G0 = vc->vc_G0_charset;
1669 vc->vc_saved_G1 = vc->vc_G1_charset;
1670}
1671
ac751efa 1672/* console_lock is held */
1da177e4
LT
1673static void restore_cur(struct vc_data *vc)
1674{
1675 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1676 vc->vc_intensity = vc->vc_s_intensity;
fa6ce9ab 1677 vc->vc_italic = vc->vc_s_italic;
1da177e4
LT
1678 vc->vc_underline = vc->vc_s_underline;
1679 vc->vc_blink = vc->vc_s_blink;
1680 vc->vc_reverse = vc->vc_s_reverse;
1681 vc->vc_charset = vc->vc_s_charset;
1682 vc->vc_color = vc->vc_s_color;
1683 vc->vc_G0_charset = vc->vc_saved_G0;
1684 vc->vc_G1_charset = vc->vc_saved_G1;
1685 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1686 update_attr(vc);
1687 vc->vc_need_wrap = 0;
1688}
1689
b290af68 1690enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
1da177e4 1691 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
63f3a16d 1692 ESpalette, ESosc };
1da177e4 1693
ac751efa 1694/* console_lock is held (except via vc_init()) */
1da177e4
LT
1695static void reset_terminal(struct vc_data *vc, int do_clear)
1696{
1697 vc->vc_top = 0;
1698 vc->vc_bottom = vc->vc_rows;
1699 vc->vc_state = ESnormal;
1700 vc->vc_ques = 0;
1701 vc->vc_translate = set_translate(LAT1_MAP, vc);
1702 vc->vc_G0_charset = LAT1_MAP;
1703 vc->vc_G1_charset = GRAF_MAP;
1704 vc->vc_charset = 0;
1705 vc->vc_need_wrap = 0;
1706 vc->vc_report_mouse = 0;
042f10ec 1707 vc->vc_utf = default_utf8;
1da177e4
LT
1708 vc->vc_utf_count = 0;
1709
1710 vc->vc_disp_ctrl = 0;
1711 vc->vc_toggle_meta = 0;
1712
1713 vc->vc_decscnm = 0;
1714 vc->vc_decom = 0;
1715 vc->vc_decawm = 1;
f6c06b68 1716 vc->vc_deccm = global_cursor_default;
1da177e4
LT
1717 vc->vc_decim = 0;
1718
079c9534 1719 vt_reset_keyboard(vc->vc_num);
1da177e4 1720
9ea9a886 1721 vc->vc_cursor_type = cur_default;
1da177e4
LT
1722 vc->vc_complement_mask = vc->vc_s_complement_mask;
1723
1724 default_attr(vc);
1725 update_attr(vc);
1726
1727 vc->vc_tab_stop[0] = 0x01010100;
1728 vc->vc_tab_stop[1] =
1729 vc->vc_tab_stop[2] =
1730 vc->vc_tab_stop[3] =
a564738c
WK
1731 vc->vc_tab_stop[4] =
1732 vc->vc_tab_stop[5] =
1733 vc->vc_tab_stop[6] =
1734 vc->vc_tab_stop[7] = 0x01010101;
1da177e4
LT
1735
1736 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1737 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
bd63364c 1738 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1da177e4
LT
1739
1740 gotoxy(vc, 0, 0);
1741 save_cur(vc);
1742 if (do_clear)
1743 csi_J(vc, 2);
1744}
1745
ac751efa 1746/* console_lock is held */
1da177e4
LT
1747static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1748{
1749 /*
1750 * Control characters can be used in the _middle_
1751 * of an escape sequence.
1752 */
63f3a16d
AB
1753 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1754 return;
1da177e4
LT
1755 switch (c) {
1756 case 0:
1757 return;
1758 case 7:
63f3a16d
AB
1759 if (vc->vc_state == ESosc)
1760 vc->vc_state = ESnormal;
1761 else if (vc->vc_bell_duration)
1da177e4
LT
1762 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1763 return;
1764 case 8:
1765 bs(vc);
1766 return;
1767 case 9:
1768 vc->vc_pos -= (vc->vc_x << 1);
1769 while (vc->vc_x < vc->vc_cols - 1) {
1770 vc->vc_x++;
1771 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1772 break;
1773 }
1774 vc->vc_pos += (vc->vc_x << 1);
b293d758 1775 notify_write(vc, '\t');
1da177e4
LT
1776 return;
1777 case 10: case 11: case 12:
1778 lf(vc);
1779 if (!is_kbd(vc, lnm))
1780 return;
1781 case 13:
1782 cr(vc);
1783 return;
1784 case 14:
1785 vc->vc_charset = 1;
1786 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1787 vc->vc_disp_ctrl = 1;
1788 return;
1789 case 15:
1790 vc->vc_charset = 0;
1791 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1792 vc->vc_disp_ctrl = 0;
1793 return;
1794 case 24: case 26:
1795 vc->vc_state = ESnormal;
1796 return;
1797 case 27:
1798 vc->vc_state = ESesc;
1799 return;
1800 case 127:
1801 del(vc);
1802 return;
1803 case 128+27:
1804 vc->vc_state = ESsquare;
1805 return;
1806 }
1807 switch(vc->vc_state) {
1808 case ESesc:
1809 vc->vc_state = ESnormal;
1810 switch (c) {
1811 case '[':
1812 vc->vc_state = ESsquare;
1813 return;
1814 case ']':
1815 vc->vc_state = ESnonstd;
1816 return;
1817 case '%':
1818 vc->vc_state = ESpercent;
1819 return;
1820 case 'E':
1821 cr(vc);
1822 lf(vc);
1823 return;
1824 case 'M':
1825 ri(vc);
1826 return;
1827 case 'D':
1828 lf(vc);
1829 return;
1830 case 'H':
1831 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1832 return;
1833 case 'Z':
1834 respond_ID(tty);
1835 return;
1836 case '7':
1837 save_cur(vc);
1838 return;
1839 case '8':
1840 restore_cur(vc);
1841 return;
1842 case '(':
1843 vc->vc_state = ESsetG0;
1844 return;
1845 case ')':
1846 vc->vc_state = ESsetG1;
1847 return;
1848 case '#':
1849 vc->vc_state = EShash;
1850 return;
1851 case 'c':
1852 reset_terminal(vc, 1);
1853 return;
1854 case '>': /* Numeric keypad */
1855 clr_kbd(vc, kbdapplic);
1856 return;
1857 case '=': /* Appl. keypad */
1858 set_kbd(vc, kbdapplic);
1859 return;
1860 }
1861 return;
1862 case ESnonstd:
1863 if (c=='P') { /* palette escape sequence */
1864 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1865 vc->vc_par[vc->vc_npar] = 0;
1866 vc->vc_npar = 0;
1867 vc->vc_state = ESpalette;
1868 return;
1869 } else if (c=='R') { /* reset palette */
1870 reset_palette(vc);
1871 vc->vc_state = ESnormal;
63f3a16d
AB
1872 } else if (c>='0' && c<='9')
1873 vc->vc_state = ESosc;
1874 else
1da177e4
LT
1875 vc->vc_state = ESnormal;
1876 return;
1877 case ESpalette:
74c807ce
AS
1878 if (isxdigit(c)) {
1879 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1da177e4
LT
1880 if (vc->vc_npar == 7) {
1881 int i = vc->vc_par[0] * 3, j = 1;
1882 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1883 vc->vc_palette[i++] += vc->vc_par[j++];
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 set_palette(vc);
1889 vc->vc_state = ESnormal;
1890 }
1891 } else
1892 vc->vc_state = ESnormal;
1893 return;
1894 case ESsquare:
1895 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1896 vc->vc_par[vc->vc_npar] = 0;
1897 vc->vc_npar = 0;
1898 vc->vc_state = ESgetpars;
1899 if (c == '[') { /* Function key */
1900 vc->vc_state=ESfunckey;
1901 return;
1902 }
1903 vc->vc_ques = (c == '?');
1904 if (vc->vc_ques)
1905 return;
1906 case ESgetpars:
1907 if (c == ';' && vc->vc_npar < NPAR - 1) {
1908 vc->vc_npar++;
1909 return;
1910 } else if (c>='0' && c<='9') {
1911 vc->vc_par[vc->vc_npar] *= 10;
1912 vc->vc_par[vc->vc_npar] += c - '0';
1913 return;
b290af68 1914 }
1da177e4
LT
1915 vc->vc_state = ESnormal;
1916 switch(c) {
1917 case 'h':
1918 set_mode(vc, 1);
1919 return;
1920 case 'l':
1921 set_mode(vc, 0);
1922 return;
1923 case 'c':
1924 if (vc->vc_ques) {
1925 if (vc->vc_par[0])
1926 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1927 else
9ea9a886 1928 vc->vc_cursor_type = cur_default;
1da177e4
LT
1929 return;
1930 }
1931 break;
1932 case 'm':
1933 if (vc->vc_ques) {
1934 clear_selection();
1935 if (vc->vc_par[0])
1936 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1937 else
1938 vc->vc_complement_mask = vc->vc_s_complement_mask;
1939 return;
1940 }
1941 break;
1942 case 'n':
1943 if (!vc->vc_ques) {
1944 if (vc->vc_par[0] == 5)
1945 status_report(tty);
1946 else if (vc->vc_par[0] == 6)
1947 cursor_report(vc, tty);
1948 }
1949 return;
1950 }
1951 if (vc->vc_ques) {
1952 vc->vc_ques = 0;
1953 return;
1954 }
1955 switch(c) {
1956 case 'G': case '`':
1957 if (vc->vc_par[0])
1958 vc->vc_par[0]--;
1959 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1960 return;
1961 case 'A':
1962 if (!vc->vc_par[0])
1963 vc->vc_par[0]++;
1964 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1965 return;
1966 case 'B': case 'e':
1967 if (!vc->vc_par[0])
1968 vc->vc_par[0]++;
1969 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1970 return;
1971 case 'C': case 'a':
1972 if (!vc->vc_par[0])
1973 vc->vc_par[0]++;
1974 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1975 return;
1976 case 'D':
1977 if (!vc->vc_par[0])
1978 vc->vc_par[0]++;
1979 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1980 return;
1981 case 'E':
1982 if (!vc->vc_par[0])
1983 vc->vc_par[0]++;
1984 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1985 return;
1986 case 'F':
1987 if (!vc->vc_par[0])
1988 vc->vc_par[0]++;
1989 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1990 return;
1991 case 'd':
1992 if (vc->vc_par[0])
1993 vc->vc_par[0]--;
1994 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1995 return;
1996 case 'H': case 'f':
1997 if (vc->vc_par[0])
1998 vc->vc_par[0]--;
1999 if (vc->vc_par[1])
2000 vc->vc_par[1]--;
2001 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2002 return;
2003 case 'J':
2004 csi_J(vc, vc->vc_par[0]);
2005 return;
2006 case 'K':
2007 csi_K(vc, vc->vc_par[0]);
2008 return;
2009 case 'L':
2010 csi_L(vc, vc->vc_par[0]);
2011 return;
2012 case 'M':
2013 csi_M(vc, vc->vc_par[0]);
2014 return;
2015 case 'P':
2016 csi_P(vc, vc->vc_par[0]);
2017 return;
2018 case 'c':
2019 if (!vc->vc_par[0])
2020 respond_ID(tty);
2021 return;
2022 case 'g':
2023 if (!vc->vc_par[0])
2024 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
2025 else if (vc->vc_par[0] == 3) {
2026 vc->vc_tab_stop[0] =
2027 vc->vc_tab_stop[1] =
2028 vc->vc_tab_stop[2] =
2029 vc->vc_tab_stop[3] =
a564738c
WK
2030 vc->vc_tab_stop[4] =
2031 vc->vc_tab_stop[5] =
2032 vc->vc_tab_stop[6] =
2033 vc->vc_tab_stop[7] = 0;
1da177e4
LT
2034 }
2035 return;
2036 case 'm':
2037 csi_m(vc);
2038 return;
2039 case 'q': /* DECLL - but only 3 leds */
2040 /* map 0,1,2,3 to 0,1,2,4 */
2041 if (vc->vc_par[0] < 4)
079c9534 2042 vt_set_led_state(vc->vc_num,
1da177e4
LT
2043 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2044 return;
2045 case 'r':
2046 if (!vc->vc_par[0])
2047 vc->vc_par[0]++;
2048 if (!vc->vc_par[1])
2049 vc->vc_par[1] = vc->vc_rows;
2050 /* Minimum allowed region is 2 lines */
2051 if (vc->vc_par[0] < vc->vc_par[1] &&
2052 vc->vc_par[1] <= vc->vc_rows) {
2053 vc->vc_top = vc->vc_par[0] - 1;
2054 vc->vc_bottom = vc->vc_par[1];
2055 gotoxay(vc, 0, 0);
2056 }
2057 return;
2058 case 's':
2059 save_cur(vc);
2060 return;
2061 case 'u':
2062 restore_cur(vc);
2063 return;
2064 case 'X':
2065 csi_X(vc, vc->vc_par[0]);
2066 return;
2067 case '@':
2068 csi_at(vc, vc->vc_par[0]);
2069 return;
2070 case ']': /* setterm functions */
2071 setterm_command(vc);
2072 return;
2073 }
2074 return;
2075 case ESpercent:
2076 vc->vc_state = ESnormal;
2077 switch (c) {
2078 case '@': /* defined in ISO 2022 */
2079 vc->vc_utf = 0;
2080 return;
2081 case 'G': /* prelim official escape code */
2082 case '8': /* retained for compatibility */
2083 vc->vc_utf = 1;
2084 return;
2085 }
2086 return;
2087 case ESfunckey:
2088 vc->vc_state = ESnormal;
2089 return;
2090 case EShash:
2091 vc->vc_state = ESnormal;
2092 if (c == '8') {
2093 /* DEC screen alignment test. kludge :-) */
2094 vc->vc_video_erase_char =
2095 (vc->vc_video_erase_char & 0xff00) | 'E';
2096 csi_J(vc, 2);
2097 vc->vc_video_erase_char =
2098 (vc->vc_video_erase_char & 0xff00) | ' ';
2099 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2100 }
2101 return;
2102 case ESsetG0:
2103 if (c == '0')
2104 vc->vc_G0_charset = GRAF_MAP;
2105 else if (c == 'B')
2106 vc->vc_G0_charset = LAT1_MAP;
2107 else if (c == 'U')
2108 vc->vc_G0_charset = IBMPC_MAP;
2109 else if (c == 'K')
2110 vc->vc_G0_charset = USER_MAP;
2111 if (vc->vc_charset == 0)
2112 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2113 vc->vc_state = ESnormal;
2114 return;
2115 case ESsetG1:
2116 if (c == '0')
2117 vc->vc_G1_charset = GRAF_MAP;
2118 else if (c == 'B')
2119 vc->vc_G1_charset = LAT1_MAP;
2120 else if (c == 'U')
2121 vc->vc_G1_charset = IBMPC_MAP;
2122 else if (c == 'K')
2123 vc->vc_G1_charset = USER_MAP;
2124 if (vc->vc_charset == 1)
2125 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2126 vc->vc_state = ESnormal;
2127 return;
63f3a16d
AB
2128 case ESosc:
2129 return;
1da177e4
LT
2130 default:
2131 vc->vc_state = ESnormal;
2132 }
2133}
2134
2f1a2ccb 2135/* is_double_width() is based on the wcwidth() implementation by
1ed8a2b3 2136 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2f1a2ccb
EK
2137 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2138 */
2139struct interval {
2140 uint32_t first;
2141 uint32_t last;
2142};
2143
2144static int bisearch(uint32_t ucs, const struct interval *table, int max)
2145{
2146 int min = 0;
2147 int mid;
2148
2149 if (ucs < table[0].first || ucs > table[max].last)
2150 return 0;
2151 while (max >= min) {
2152 mid = (min + max) / 2;
2153 if (ucs > table[mid].last)
2154 min = mid + 1;
2155 else if (ucs < table[mid].first)
2156 max = mid - 1;
2157 else
2158 return 1;
2159 }
2160 return 0;
2161}
2162
2163static int is_double_width(uint32_t ucs)
2164{
2165 static const struct interval double_width[] = {
2166 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2167 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
1ed8a2b3
EK
2168 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2169 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2f1a2ccb 2170 };
0f11541b 2171 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
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) {
2207 printk(KERN_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();
1da177e4
LT
2711 ret = __put_user(data, p);
2712 break;
2713 case TIOCL_GETMOUSEREPORTING:
20f62579 2714 console_lock(); /* May be overkill */
1da177e4 2715 data = mouse_reporting();
20f62579 2716 console_unlock();
1da177e4
LT
2717 ret = __put_user(data, p);
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();
0ca07731
RW
2726 ret = __put_user(data, p);
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)
4bcc595c 3192 printk(KERN_CONT "consoles %d-%d ", first+1, last+1);
4ee1acce
DH
3193 if (j >= 0) {
3194 struct vc_data *vc = vc_cons[j].d;
3195
4bcc595c 3196 printk(KERN_CONT "to %s %s %dx%d\n",
4ee1acce
DH
3197 vc->vc_can_do_color ? "colour" : "mono",
3198 desc, vc->vc_cols, vc->vc_rows);
3199
3200 if (k >= 0) {
3201 vc = vc_cons[k].d;
3202 update_screen(vc);
3203 }
3204 } else
4bcc595c 3205 printk(KERN_CONT "to %s\n", desc);
1da177e4 3206
3e795de7
AD
3207 retval = 0;
3208err:
1da177e4 3209 module_put(owner);
3e795de7
AD
3210 return retval;
3211};
1da177e4 3212
50e244cc 3213
13ae6645 3214#ifdef CONFIG_VT_HW_CONSOLE_BINDING
e93a9a86
TI
3215/* unlocked version of unbind_con_driver() */
3216int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
1da177e4 3217{
3e795de7
AD
3218 struct module *owner = csw->owner;
3219 const struct consw *defcsw = NULL;
3220 struct con_driver *con_driver = NULL, *con_back = NULL;
3221 int i, retval = -ENODEV;
1da177e4 3222
3e795de7
AD
3223 if (!try_module_get(owner))
3224 return -ENODEV;
3225
e93a9a86 3226 WARN_CONSOLE_UNLOCKED();
3e795de7
AD
3227
3228 /* check if driver is registered and if it is unbindable */
3229 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3230 con_driver = &registered_con_driver[i];
3231
3232 if (con_driver->con == csw &&
6db4063c 3233 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3e795de7
AD
3234 retval = 0;
3235 break;
3236 }
3237 }
3238
e93a9a86 3239 if (retval)
3e795de7 3240 goto err;
3e795de7 3241
6db4063c
AD
3242 retval = -ENODEV;
3243
3e795de7
AD
3244 /* check if backup driver exists */
3245 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3246 con_back = &registered_con_driver[i];
3247
249f7b3e 3248 if (con_back->con && con_back->con != csw) {
3e795de7
AD
3249 defcsw = con_back->con;
3250 retval = 0;
3251 break;
3252 }
3253 }
3254
e93a9a86 3255 if (retval)
3e795de7 3256 goto err;
3e795de7 3257
e93a9a86 3258 if (!con_is_bound(csw))
3e795de7 3259 goto err;
3e795de7
AD
3260
3261 first = max(first, con_driver->first);
3262 last = min(last, con_driver->last);
3263
3264 for (i = first; i <= last; i++) {
1da177e4
LT
3265 if (con_driver_map[i] == csw) {
3266 module_put(csw->owner);
3267 con_driver_map[i] = NULL;
3268 }
3e795de7
AD
3269 }
3270
3271 if (!con_is_bound(defcsw)) {
6db4063c
AD
3272 const struct consw *defconsw = conswitchp;
3273
3e795de7
AD
3274 defcsw->con_startup();
3275 con_back->flag |= CON_DRIVER_FLAG_INIT;
6db4063c
AD
3276 /*
3277 * vgacon may change the default driver to point
3278 * to dummycon, we restore it here...
3279 */
3280 conswitchp = defconsw;
3e795de7
AD
3281 }
3282
3283 if (!con_is_bound(csw))
3284 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3285
6db4063c 3286 /* ignore return value, binding should not fail */
50e244cc 3287 do_bind_con_driver(defcsw, first, last, deflt);
3e795de7
AD
3288err:
3289 module_put(owner);
3290 return retval;
3291
3292}
e93a9a86 3293EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3e795de7 3294
6db4063c
AD
3295static int vt_bind(struct con_driver *con)
3296{
3297 const struct consw *defcsw = NULL, *csw = NULL;
3298 int i, more = 1, first = -1, last = -1, deflt = 0;
3299
77232f79 3300 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3301 goto err;
3302
3303 csw = con->con;
3304
3305 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3306 struct con_driver *con = &registered_con_driver[i];
3307
3308 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3309 defcsw = con->con;
3310 break;
3311 }
3312 }
3313
3314 if (!defcsw)
3315 goto err;
3316
3317 while (more) {
3318 more = 0;
3319
3320 for (i = con->first; i <= con->last; i++) {
3321 if (con_driver_map[i] == defcsw) {
3322 if (first == -1)
3323 first = i;
3324 last = i;
3325 more = 1;
3326 } else if (first != -1)
3327 break;
3328 }
3329
3330 if (first == 0 && last == MAX_NR_CONSOLES -1)
3331 deflt = 1;
3332
4c215fe8 3333 if (first != -1)
c62a1e57 3334 do_bind_con_driver(csw, first, last, deflt);
6db4063c
AD
3335
3336 first = -1;
3337 last = -1;
3338 deflt = 0;
3339 }
3340
3341err:
3342 return 0;
3343}
3344
3345static int vt_unbind(struct con_driver *con)
3346{
3347 const struct consw *csw = NULL;
3348 int i, more = 1, first = -1, last = -1, deflt = 0;
f418f2ec 3349 int ret;
6db4063c 3350
77232f79 3351 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
6db4063c
AD
3352 goto err;
3353
3354 csw = con->con;
3355
3356 while (more) {
3357 more = 0;
3358
3359 for (i = con->first; i <= con->last; i++) {
3360 if (con_driver_map[i] == csw) {
3361 if (first == -1)
3362 first = i;
3363 last = i;
3364 more = 1;
3365 } else if (first != -1)
3366 break;
3367 }
3368
3369 if (first == 0 && last == MAX_NR_CONSOLES -1)
3370 deflt = 1;
3371
618f2b90 3372 if (first != -1) {
f418f2ec 3373 ret = do_unbind_con_driver(csw, first, last, deflt);
f418f2ec
DV
3374 if (ret != 0)
3375 return ret;
618f2b90 3376 }
6db4063c
AD
3377
3378 first = -1;
3379 last = -1;
3380 deflt = 0;
3381 }
3382
3383err:
3384 return 0;
3385}
13ae6645
AD
3386#else
3387static inline int vt_bind(struct con_driver *con)
3388{
3389 return 0;
3390}
3391static inline int vt_unbind(struct con_driver *con)
3392{
3393 return 0;
3394}
3395#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
6db4063c 3396
805952a8 3397static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
6db4063c
AD
3398 const char *buf, size_t count)
3399{
805952a8 3400 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3401 int bind = simple_strtoul(buf, NULL, 0);
3402
4c215fe8
ID
3403 console_lock();
3404
6db4063c
AD
3405 if (bind)
3406 vt_bind(con);
3407 else
3408 vt_unbind(con);
3409
4c215fe8
ID
3410 console_unlock();
3411
6db4063c
AD
3412 return count;
3413}
3414
805952a8
GKH
3415static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3416 char *buf)
6db4063c 3417{
805952a8 3418 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3419 int bind = con_is_bound(con->con);
3420
3421 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3422}
3423
805952a8
GKH
3424static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3425 char *buf)
6db4063c 3426{
805952a8 3427 struct con_driver *con = dev_get_drvdata(dev);
6db4063c
AD
3428
3429 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3430 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3431 con->desc);
3432
3433}
3434
1083a7be
TI
3435static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3436static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3437
3438static struct attribute *con_dev_attrs[] = {
3439 &dev_attr_bind.attr,
3440 &dev_attr_name.attr,
3441 NULL
6db4063c
AD
3442};
3443
1083a7be
TI
3444ATTRIBUTE_GROUPS(con_dev);
3445
805952a8 3446static int vtconsole_init_device(struct con_driver *con)
6db4063c 3447{
928e964f 3448 con->flag |= CON_DRIVER_FLAG_ATTR;
1083a7be 3449 return 0;
6db4063c
AD
3450}
3451
805952a8 3452static void vtconsole_deinit_device(struct con_driver *con)
6db4063c 3453{
1083a7be 3454 con->flag &= ~CON_DRIVER_FLAG_ATTR;
6db4063c
AD
3455}
3456
3e795de7
AD
3457/**
3458 * con_is_bound - checks if driver is bound to the console
3459 * @csw: console driver
3460 *
3461 * RETURNS: zero if unbound, nonzero if bound
3462 *
3463 * Drivers can call this and if zero, they should release
3464 * all resources allocated on con_startup()
3465 */
3466int con_is_bound(const struct consw *csw)
3467{
3468 int i, bound = 0;
3469
3470 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3471 if (con_driver_map[i] == csw) {
3472 bound = 1;
3473 break;
3474 }
3475 }
3476
3477 return bound;
3478}
3479EXPORT_SYMBOL(con_is_bound);
3480
b45cfba4
JB
3481/**
3482 * con_debug_enter - prepare the console for the kernel debugger
3483 * @sw: console driver
3484 *
3485 * Called when the console is taken over by the kernel debugger, this
3486 * function needs to save the current console state, then put the console
3487 * into a state suitable for the kernel debugger.
3488 *
3489 * RETURNS:
3490 * Zero on success, nonzero if a failure occurred when trying to prepare
3491 * the console for the debugger.
3492 */
3493int con_debug_enter(struct vc_data *vc)
3494{
3495 int ret = 0;
3496
3497 saved_fg_console = fg_console;
3498 saved_last_console = last_console;
3499 saved_want_console = want_console;
3500 saved_vc_mode = vc->vc_mode;
beed5336 3501 saved_console_blanked = console_blanked;
b45cfba4
JB
3502 vc->vc_mode = KD_TEXT;
3503 console_blanked = 0;
3504 if (vc->vc_sw->con_debug_enter)
3505 ret = vc->vc_sw->con_debug_enter(vc);
81d44507
JW
3506#ifdef CONFIG_KGDB_KDB
3507 /* Set the initial LINES variable if it is not already set */
3508 if (vc->vc_rows < 999) {
3509 int linecount;
3510 char lns[4];
3511 const char *setargs[3] = {
3512 "set",
3513 "LINES",
3514 lns,
3515 };
3516 if (kdbgetintenv(setargs[0], &linecount)) {
3517 snprintf(lns, 4, "%i", vc->vc_rows);
3518 kdb_set(2, setargs);
3519 }
3520 }
17b572e8
JW
3521 if (vc->vc_cols < 999) {
3522 int colcount;
3523 char cols[4];
3524 const char *setargs[3] = {
3525 "set",
3526 "COLUMNS",
3527 cols,
3528 };
3529 if (kdbgetintenv(setargs[0], &colcount)) {
3530 snprintf(cols, 4, "%i", vc->vc_cols);
3531 kdb_set(2, setargs);
3532 }
3533 }
81d44507 3534#endif /* CONFIG_KGDB_KDB */
b45cfba4
JB
3535 return ret;
3536}
3537EXPORT_SYMBOL_GPL(con_debug_enter);
3538
3539/**
3540 * con_debug_leave - restore console state
3541 * @sw: console driver
3542 *
3543 * Restore the console state to what it was before the kernel debugger
3544 * was invoked.
3545 *
3546 * RETURNS:
3547 * Zero on success, nonzero if a failure occurred when trying to restore
3548 * the console.
3549 */
3550int con_debug_leave(void)
3551{
3552 struct vc_data *vc;
3553 int ret = 0;
3554
3555 fg_console = saved_fg_console;
3556 last_console = saved_last_console;
3557 want_console = saved_want_console;
beed5336 3558 console_blanked = saved_console_blanked;
b45cfba4
JB
3559 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3560
3561 vc = vc_cons[fg_console].d;
3562 if (vc->vc_sw->con_debug_leave)
3563 ret = vc->vc_sw->con_debug_leave(vc);
3564 return ret;
3565}
3566EXPORT_SYMBOL_GPL(con_debug_leave);
3567
50e244cc 3568static int do_register_con_driver(const struct consw *csw, int first, int last)
3e795de7
AD
3569{
3570 struct module *owner = csw->owner;
3571 struct con_driver *con_driver;
3572 const char *desc;
96317e9e 3573 int i, retval;
3e795de7 3574
50e244cc
AC
3575 WARN_CONSOLE_UNLOCKED();
3576
3e795de7
AD
3577 if (!try_module_get(owner))
3578 return -ENODEV;
3579
3e795de7
AD
3580 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3581 con_driver = &registered_con_driver[i];
3582
3583 /* already registered */
96317e9e 3584 if (con_driver->con == csw) {
c55c63c6 3585 retval = -EBUSY;
96317e9e
JS
3586 goto err;
3587 }
3e795de7
AD
3588 }
3589
3e795de7 3590 desc = csw->con_startup();
6798df4c
JS
3591 if (!desc) {
3592 retval = -ENODEV;
3e795de7 3593 goto err;
6798df4c 3594 }
3e795de7
AD
3595
3596 retval = -EINVAL;
3597
3598 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3599 con_driver = &registered_con_driver[i];
3600
d364b5c3
ID
3601 if (con_driver->con == NULL &&
3602 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
3e795de7
AD
3603 con_driver->con = csw;
3604 con_driver->desc = desc;
6db4063c
AD
3605 con_driver->node = i;
3606 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3e795de7
AD
3607 CON_DRIVER_FLAG_INIT;
3608 con_driver->first = first;
3609 con_driver->last = last;
3610 retval = 0;
3611 break;
3612 }
3613 }
3614
6db4063c
AD
3615 if (retval)
3616 goto err;
3617
1083a7be
TI
3618 con_driver->dev =
3619 device_create_with_groups(vtconsole_class, NULL,
3620 MKDEV(0, con_driver->node),
3621 con_driver, con_dev_groups,
3622 "vtcon%i", con_driver->node);
805952a8
GKH
3623 if (IS_ERR(con_driver->dev)) {
3624 printk(KERN_WARNING "Unable to create device for %s; "
6db4063c 3625 "errno = %ld\n", con_driver->desc,
805952a8
GKH
3626 PTR_ERR(con_driver->dev));
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)) {
3763 printk(KERN_WARNING "Unable to create vt console class; "
3764 "errno = %ld\n", PTR_ERR(vtconsole_class));
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)) {
6db4063c 3780 printk(KERN_WARNING "Unable to create "
805952a8
GKH
3781 "device for %s; errno = %ld\n",
3782 con->desc, PTR_ERR(con->dev));
3783 con->dev = NULL;
6db4063c 3784 } else {
805952a8 3785 vtconsole_init_device(con);
6db4063c 3786 }
3e795de7 3787 }
3e795de7
AD
3788 }
3789
3e795de7
AD
3790 return 0;
3791}
6db4063c 3792postcore_initcall(vtconsole_class_init);
3e795de7 3793
1da177e4
LT
3794#endif
3795
3796/*
3797 * Screen blanking
3798 */
3799
403aac96 3800static int set_vesa_blanking(char __user *p)
1da177e4 3801{
403aac96
YY
3802 unsigned int mode;
3803
3804 if (get_user(mode, p + 1))
3805 return -EFAULT;
3806
3807 vesa_blank_mode = (mode < 4) ? mode : 0;
3808 return 0;
1da177e4
LT
3809}
3810
1da177e4
LT
3811void do_blank_screen(int entering_gfx)
3812{
3813 struct vc_data *vc = vc_cons[fg_console].d;
3814 int i;
3815
3816 WARN_CONSOLE_UNLOCKED();
3817
3818 if (console_blanked) {
3819 if (blank_state == blank_vesa_wait) {
3820 blank_state = blank_off;
d060a321 3821 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
1da177e4
LT
3822 }
3823 return;
3824 }
1da177e4
LT
3825
3826 /* entering graphics mode? */
3827 if (entering_gfx) {
3828 hide_cursor(vc);
3829 save_screen(vc);
3830 vc->vc_sw->con_blank(vc, -1, 1);
3831 console_blanked = fg_console + 1;
b6e8f00f 3832 blank_state = blank_off;
1da177e4
LT
3833 set_origin(vc);
3834 return;
3835 }
3836
b6e8f00f 3837 if (blank_state != blank_normal_wait)
3838 return;
3839 blank_state = blank_off;
3840
1da177e4
LT
3841 /* don't blank graphics */
3842 if (vc->vc_mode != KD_TEXT) {
3843 console_blanked = fg_console + 1;
3844 return;
3845 }
3846
3847 hide_cursor(vc);
3848 del_timer_sync(&console_timer);
3849 blank_timer_expired = 0;
3850
3851 save_screen(vc);
3852 /* In case we need to reset origin, blanking hook returns 1 */
d060a321 3853 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
1da177e4
LT
3854 console_blanked = fg_console + 1;
3855 if (i)
3856 set_origin(vc);
3857
3858 if (console_blank_hook && console_blank_hook(1))
3859 return;
3860
d060a321 3861 if (vesa_off_interval && vesa_blank_mode) {
030babac 3862 blank_state = blank_vesa_wait;
1da177e4
LT
3863 mod_timer(&console_timer, jiffies + vesa_off_interval);
3864 }
8b92e87d 3865 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
3866}
3867EXPORT_SYMBOL(do_blank_screen);
3868
3869/*
3870 * Called by timer as well as from vt_console_driver
3871 */
3872void do_unblank_screen(int leaving_gfx)
3873{
3874 struct vc_data *vc;
3875
3876 /* This should now always be called from a "sane" (read: can schedule)
3877 * context for the sake of the low level drivers, except in the special
3878 * case of oops_in_progress
3879 */
3880 if (!oops_in_progress)
3881 might_sleep();
3882
3883 WARN_CONSOLE_UNLOCKED();
3884
3885 ignore_poke = 0;
3886 if (!console_blanked)
3887 return;
3888 if (!vc_cons_allocated(fg_console)) {
3889 /* impossible */
e620e548
JP
3890 pr_warn("unblank_screen: tty %d not allocated ??\n",
3891 fg_console + 1);
1da177e4
LT
3892 return;
3893 }
3894 vc = vc_cons[fg_console].d;
8fd4bd22
JB
3895 /* Try to unblank in oops case too */
3896 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
1da177e4
LT
3897 return; /* but leave console_blanked != 0 */
3898
3899 if (blankinterval) {
f324edc8 3900 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
3901 blank_state = blank_normal_wait;
3902 }
3903
3904 console_blanked = 0;
8fd4bd22 3905 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
1da177e4
LT
3906 /* Low-level driver cannot restore -> do it ourselves */
3907 update_screen(vc);
3908 if (console_blank_hook)
3909 console_blank_hook(0);
3910 set_palette(vc);
3911 set_cursor(vc);
8b92e87d 3912 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
1da177e4
LT
3913}
3914EXPORT_SYMBOL(do_unblank_screen);
3915
3916/*
3917 * This is called by the outside world to cause a forced unblank, mostly for
3918 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3919 * call it with 1 as an argument and so force a mode restore... that may kill
3920 * X or at least garbage the screen but would also make the Oops visible...
3921 */
3922void unblank_screen(void)
3923{
3924 do_unblank_screen(0);
3925}
3926
3927/*
70522e12 3928 * We defer the timer blanking to work queue so it can take the console mutex
1da177e4 3929 * (console operations can still happen at irq time, but only from printk which
70522e12 3930 * has the console mutex. Not perfect yet, but better than no locking
1da177e4
LT
3931 */
3932static void blank_screen_t(unsigned long dummy)
3933{
cc63b1e1 3934 if (unlikely(!keventd_up())) {
f324edc8 3935 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
cc63b1e1
JB
3936 return;
3937 }
1da177e4
LT
3938 blank_timer_expired = 1;
3939 schedule_work(&console_work);
3940}
3941
3942void poke_blanked_console(void)
3943{
3944 WARN_CONSOLE_UNLOCKED();
3945
3946 /* Add this so we quickly catch whoever might call us in a non
3947 * safe context. Nowadays, unblank_screen() isn't to be called in
3948 * atomic contexts and is allowed to schedule (with the special case
3949 * of oops_in_progress, but that isn't of any concern for this
3950 * function. --BenH.
3951 */
3952 might_sleep();
3953
3954 /* This isn't perfectly race free, but a race here would be mostly harmless,
3955 * at worse, we'll do a spurrious blank and it's unlikely
3956 */
3957 del_timer(&console_timer);
3958 blank_timer_expired = 0;
3959
3960 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3961 return;
3962 if (console_blanked)
3963 unblank_screen();
3964 else if (blankinterval) {
f324edc8 3965 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
1da177e4
LT
3966 blank_state = blank_normal_wait;
3967 }
3968}
3969
3970/*
3971 * Palettes
3972 */
3973
3974static void set_palette(struct vc_data *vc)
3975{
3976 WARN_CONSOLE_UNLOCKED();
3977
709280da 3978 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
1da177e4
LT
3979 vc->vc_sw->con_set_palette(vc, color_table);
3980}
3981
1da177e4
LT
3982/*
3983 * Load palette into the DAC registers. arg points to a colour
3984 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3985 */
3986
3987int con_set_cmap(unsigned char __user *arg)
3988{
871bdea6
MG
3989 int i, j, k;
3990 unsigned char colormap[3*16];
3991
3992 if (copy_from_user(colormap, arg, sizeof(colormap)))
3993 return -EFAULT;
1da177e4 3994
ac751efa 3995 console_lock();
871bdea6
MG
3996 for (i = k = 0; i < 16; i++) {
3997 default_red[i] = colormap[k++];
3998 default_grn[i] = colormap[k++];
3999 default_blu[i] = colormap[k++];
4000 }
4001 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4002 if (!vc_cons_allocated(i))
4003 continue;
4004 for (j = k = 0; j < 16; j++) {
4005 vc_cons[i].d->vc_palette[k++] = default_red[j];
4006 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4007 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4008 }
4009 set_palette(vc_cons[i].d);
4010 }
ac751efa 4011 console_unlock();
1da177e4 4012
871bdea6 4013 return 0;
1da177e4
LT
4014}
4015
4016int con_get_cmap(unsigned char __user *arg)
4017{
871bdea6
MG
4018 int i, k;
4019 unsigned char colormap[3*16];
1da177e4 4020
ac751efa 4021 console_lock();
871bdea6
MG
4022 for (i = k = 0; i < 16; i++) {
4023 colormap[k++] = default_red[i];
4024 colormap[k++] = default_grn[i];
4025 colormap[k++] = default_blu[i];
4026 }
ac751efa 4027 console_unlock();
1da177e4 4028
871bdea6
MG
4029 if (copy_to_user(arg, colormap, sizeof(colormap)))
4030 return -EFAULT;
4031
4032 return 0;
1da177e4
LT
4033}
4034
4035void reset_palette(struct vc_data *vc)
4036{
4037 int j, k;
4038 for (j=k=0; j<16; j++) {
4039 vc->vc_palette[k++] = default_red[j];
4040 vc->vc_palette[k++] = default_grn[j];
4041 vc->vc_palette[k++] = default_blu[j];
4042 }
4043 set_palette(vc);
4044}
4045
4046/*
4047 * Font switching
4048 *
4049 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4050 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4051 * depending on width) reserved for each character which is kinda wasty, but
4052 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
25985edc 4053 * is up to the actual low-level console-driver convert data into its favorite
1da177e4
LT
4054 * format (maybe we should add a `fontoffset' field to the `display'
4055 * structure so we won't have to convert the fontdata all the time.
4056 * /Jes
4057 */
4058
4059#define max_font_size 65536
4060
4061static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4062{
4063 struct console_font font;
4064 int rc = -EINVAL;
4065 int c;
4066
1da177e4
LT
4067 if (op->data) {
4068 font.data = kmalloc(max_font_size, GFP_KERNEL);
4069 if (!font.data)
4070 return -ENOMEM;
4071 } else
4072 font.data = NULL;
4073
ac751efa 4074 console_lock();
edab558f
AC
4075 if (vc->vc_mode != KD_TEXT)
4076 rc = -EINVAL;
4077 else if (vc->vc_sw->con_font_get)
1da177e4
LT
4078 rc = vc->vc_sw->con_font_get(vc, &font);
4079 else
4080 rc = -ENOSYS;
ac751efa 4081 console_unlock();
1da177e4
LT
4082
4083 if (rc)
4084 goto out;
4085
4086 c = (font.width+7)/8 * 32 * font.charcount;
04f378b1 4087
1da177e4
LT
4088 if (op->data && font.charcount > op->charcount)
4089 rc = -ENOSPC;
4090 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4091 if (font.width > op->width || font.height > op->height)
4092 rc = -ENOSPC;
4093 } else {
4094 if (font.width != 8)
4095 rc = -EIO;
4096 else if ((op->height && font.height > op->height) ||
4097 font.height > 32)
4098 rc = -ENOSPC;
4099 }
4100 if (rc)
4101 goto out;
4102
4103 op->height = font.height;
4104 op->width = font.width;
4105 op->charcount = font.charcount;
4106
4107 if (op->data && copy_to_user(op->data, font.data, c))
4108 rc = -EFAULT;
4109
4110out:
4111 kfree(font.data);
4112 return rc;
4113}
4114
4115static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4116{
4117 struct console_font font;
4118 int rc = -EINVAL;
4119 int size;
4120
4121 if (vc->vc_mode != KD_TEXT)
4122 return -EINVAL;
4123 if (!op->data)
4124 return -EINVAL;
4125 if (op->charcount > 512)
4126 return -EINVAL;
4127 if (!op->height) { /* Need to guess font height [compat] */
4128 int h, i;
4129 u8 __user *charmap = op->data;
4130 u8 tmp;
4131
4132 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4133 so that we can get rid of this soon */
4134 if (!(op->flags & KD_FONT_FLAG_OLD))
4135 return -EINVAL;
4136 for (h = 32; h > 0; h--)
4137 for (i = 0; i < op->charcount; i++) {
4138 if (get_user(tmp, &charmap[32*i+h-1]))
4139 return -EFAULT;
4140 if (tmp)
4141 goto nonzero;
4142 }
4143 return -EINVAL;
4144 nonzero:
4145 op->height = h;
4146 }
4147 if (op->width <= 0 || op->width > 32 || op->height > 32)
4148 return -EINVAL;
4149 size = (op->width+7)/8 * 32 * op->charcount;
4150 if (size > max_font_size)
4151 return -ENOSPC;
4152 font.charcount = op->charcount;
4153 font.height = op->height;
4154 font.width = op->width;
9b71ca20
JL
4155 font.data = memdup_user(op->data, size);
4156 if (IS_ERR(font.data))
4157 return PTR_ERR(font.data);
ac751efa 4158 console_lock();
edab558f
AC
4159 if (vc->vc_mode != KD_TEXT)
4160 rc = -EINVAL;
4161 else if (vc->vc_sw->con_font_set)
1da177e4
LT
4162 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4163 else
4164 rc = -ENOSYS;
ac751efa 4165 console_unlock();
1da177e4
LT
4166 kfree(font.data);
4167 return rc;
4168}
4169
4170static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4171{
4172 struct console_font font = {.width = op->width, .height = op->height};
4173 char name[MAX_FONT_NAME];
4174 char *s = name;
4175 int rc;
4176
1da177e4
LT
4177
4178 if (!op->data)
4179 s = NULL;
4180 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4181 return -EFAULT;
4182 else
4183 name[MAX_FONT_NAME - 1] = 0;
4184
ac751efa 4185 console_lock();
edab558f
AC
4186 if (vc->vc_mode != KD_TEXT) {
4187 console_unlock();
4188 return -EINVAL;
4189 }
1da177e4
LT
4190 if (vc->vc_sw->con_font_default)
4191 rc = vc->vc_sw->con_font_default(vc, &font, s);
4192 else
4193 rc = -ENOSYS;
ac751efa 4194 console_unlock();
1da177e4
LT
4195 if (!rc) {
4196 op->width = font.width;
4197 op->height = font.height;
4198 }
4199 return rc;
4200}
4201
4202static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4203{
4204 int con = op->height;
4205 int rc;
4206
1da177e4 4207
ac751efa 4208 console_lock();
edab558f
AC
4209 if (vc->vc_mode != KD_TEXT)
4210 rc = -EINVAL;
4211 else if (!vc->vc_sw->con_font_copy)
1da177e4
LT
4212 rc = -ENOSYS;
4213 else if (con < 0 || !vc_cons_allocated(con))
4214 rc = -ENOTTY;
4215 else if (con == vc->vc_num) /* nothing to do */
4216 rc = 0;
4217 else
4218 rc = vc->vc_sw->con_font_copy(vc, con);
ac751efa 4219 console_unlock();
1da177e4
LT
4220 return rc;
4221}
4222
4223int con_font_op(struct vc_data *vc, struct console_font_op *op)
4224{
4225 switch (op->op) {
4226 case KD_FONT_OP_SET:
4227 return con_font_set(vc, op);
4228 case KD_FONT_OP_GET:
4229 return con_font_get(vc, op);
4230 case KD_FONT_OP_SET_DEFAULT:
4231 return con_font_default(vc, op);
4232 case KD_FONT_OP_COPY:
4233 return con_font_copy(vc, op);
4234 }
4235 return -ENOSYS;
4236}
4237
4238/*
4239 * Interface exported to selection and vcs.
4240 */
4241
4242/* used by selection */
4243u16 screen_glyph(struct vc_data *vc, int offset)
4244{
4245 u16 w = scr_readw(screenpos(vc, offset, 1));
4246 u16 c = w & 0xff;
4247
4248 if (w & vc->vc_hi_font_mask)
4249 c |= 0x100;
4250 return c;
4251}
f7511d5f 4252EXPORT_SYMBOL_GPL(screen_glyph);
1da177e4
LT
4253
4254/* used by vcs - note the word offset */
4255unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4256{
4257 return screenpos(vc, 2 * w_offset, viewed);
4258}
88867e3d 4259EXPORT_SYMBOL_GPL(screen_pos);
1da177e4
LT
4260
4261void getconsxy(struct vc_data *vc, unsigned char *p)
4262{
4263 p[0] = vc->vc_x;
4264 p[1] = vc->vc_y;
4265}
4266
4267void putconsxy(struct vc_data *vc, unsigned char *p)
4268{
9477e260 4269 hide_cursor(vc);
1da177e4
LT
4270 gotoxy(vc, p[0], p[1]);
4271 set_cursor(vc);
4272}
4273
4274u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4275{
4276 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4277 return softcursor_original;
4278 return scr_readw(org);
4279}
4280
4281void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4282{
4283 scr_writew(val, org);
4284 if ((unsigned long)org == vc->vc_pos) {
4285 softcursor_original = -1;
4286 add_softcursor(vc);
4287 }
4288}
4289
432c9ed2
NP
4290void vcs_scr_updated(struct vc_data *vc)
4291{
4292 notify_update(vc);
4293}
4294
1da177e4
LT
4295/*
4296 * Visible symbols for modules
4297 */
4298
4299EXPORT_SYMBOL(color_table);
4300EXPORT_SYMBOL(default_red);
4301EXPORT_SYMBOL(default_grn);
4302EXPORT_SYMBOL(default_blu);
4303EXPORT_SYMBOL(update_region);
4304EXPORT_SYMBOL(redraw_screen);
4305EXPORT_SYMBOL(vc_resize);
4306EXPORT_SYMBOL(fg_console);
4307EXPORT_SYMBOL(console_blank_hook);
4308EXPORT_SYMBOL(console_blanked);
4309EXPORT_SYMBOL(vc_cons);
f6c06b68 4310EXPORT_SYMBOL(global_cursor_default);
1da177e4 4311#ifndef VT_SINGLE_DRIVER
1da177e4
LT
4312EXPORT_SYMBOL(give_up_console);
4313#endif