vt: clear selection before resizing
[pandora-kernel.git] / drivers / tty / vt / vt.c
1 /*
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
60  * deletion SMP-safe.  17Jun00, Andrew Morton
61  *
62  * Removed console_lock, enabled interrupts across all console operations
63  * 13 March 2001, Andrew Morton
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
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>
87 #include <linux/mutex.h>
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>
95 #include <linux/workqueue.h>
96 #include <linux/pm.h>
97 #include <linux/font.h>
98 #include <linux/bitops.h>
99 #include <linux/notifier.h>
100 #include <linux/device.h>
101 #include <linux/io.h>
102 #include <asm/system.h>
103 #include <linux/uaccess.h>
104 #include <linux/kdb.h>
105 #include <linux/ctype.h>
106
107 #define MAX_NR_CON_DRIVER 16
108
109 #define CON_DRIVER_FLAG_MODULE 1
110 #define CON_DRIVER_FLAG_INIT   2
111 #define CON_DRIVER_FLAG_ATTR   4
112
113 struct con_driver {
114         const struct consw *con;
115         const char *desc;
116         struct device *dev;
117         int node;
118         int first;
119         int last;
120         int flag;
121 };
122
123 static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
124 const 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)
139
140 struct vc vc_cons [MAX_NR_CONSOLES];
141
142 #ifndef VT_SINGLE_DRIVER
143 static const struct consw *con_driver_map[MAX_NR_CONSOLES];
144 #endif
145
146 static int con_open(struct tty_struct *, struct file *);
147 static void vc_init(struct vc_data *vc, unsigned int rows,
148                     unsigned int cols, int do_clear);
149 static void gotoxy(struct vc_data *vc, int new_x, int new_y);
150 static void save_cur(struct vc_data *vc);
151 static void reset_terminal(struct vc_data *vc, int do_clear);
152 static void con_flush_chars(struct tty_struct *tty);
153 static int set_vesa_blanking(char __user *p);
154 static void set_cursor(struct vc_data *vc);
155 static void hide_cursor(struct vc_data *vc);
156 static void console_callback(struct work_struct *ignored);
157 static void blank_screen_t(unsigned long dummy);
158 static void set_palette(struct vc_data *vc);
159
160 static int printable;           /* Is console ready for printing? */
161 int default_utf8 = true;
162 module_param(default_utf8, int, S_IRUGO | S_IWUSR);
163 int global_cursor_default = -1;
164 module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
165
166 static int cur_default = CUR_DEFAULT;
167 module_param(cur_default, int, S_IRUGO | S_IWUSR);
168
169 /*
170  * ignore_poke: don't unblank the screen when things are typed.  This is
171  * mainly for the privacy of braille terminal users.
172  */
173 static int ignore_poke;
174
175 int do_poke_blanked_console;
176 int console_blanked;
177
178 static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
179 static int vesa_off_interval;
180 static int blankinterval = 10*60;
181 core_param(consoleblank, blankinterval, int, 0444);
182
183 static DECLARE_WORK(console_work, console_callback);
184
185 /*
186  * fg_console is the current virtual console,
187  * last_console is the last used one,
188  * want_console is the console we want to switch to,
189  * saved_* variants are for save/restore around kernel debugger enter/leave
190  */
191 int fg_console;
192 int last_console;
193 int want_console = -1;
194 static int saved_fg_console;
195 static int saved_last_console;
196 static int saved_want_console;
197 static int saved_vc_mode;
198 static int saved_console_blanked;
199
200 /*
201  * For each existing display, we have a pointer to console currently visible
202  * on that display, allowing consoles other than fg_console to be refreshed
203  * appropriately. Unless the low-level driver supplies its own display_fg
204  * variable, we use this one for the "master display".
205  */
206 static struct vc_data *master_display_fg;
207
208 /*
209  * Unfortunately, we need to delay tty echo when we're currently writing to the
210  * console since the code is (and always was) not re-entrant, so we schedule
211  * all flip requests to process context with schedule-task() and run it from
212  * console_callback().
213  */
214
215 /*
216  * For the same reason, we defer scrollback to the console callback.
217  */
218 static int scrollback_delta;
219
220 /*
221  * Hook so that the power management routines can (un)blank
222  * the console on our behalf.
223  */
224 int (*console_blank_hook)(int);
225
226 static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
227 static int blank_state;
228 static int blank_timer_expired;
229 enum {
230         blank_off = 0,
231         blank_normal_wait,
232         blank_vesa_wait,
233 };
234
235 /*
236  * /sys/class/tty/tty0/
237  *
238  * the attribute 'active' contains the name of the current vc
239  * console and it supports poll() to detect vc switches
240  */
241 static struct device *tty0dev;
242
243 /*
244  * Notifier list for console events.
245  */
246 static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
247
248 int register_vt_notifier(struct notifier_block *nb)
249 {
250         return atomic_notifier_chain_register(&vt_notifier_list, nb);
251 }
252 EXPORT_SYMBOL_GPL(register_vt_notifier);
253
254 int unregister_vt_notifier(struct notifier_block *nb)
255 {
256         return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
257 }
258 EXPORT_SYMBOL_GPL(unregister_vt_notifier);
259
260 static void notify_write(struct vc_data *vc, unsigned int unicode)
261 {
262         struct vt_notifier_param param = { .vc = vc, .c = unicode };
263         atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
264 }
265
266 static void notify_update(struct vc_data *vc)
267 {
268         struct vt_notifier_param param = { .vc = vc };
269         atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
270 }
271 /*
272  *      Low-Level Functions
273  */
274
275 #define IS_FG(vc)       ((vc)->vc_num == fg_console)
276
277 #ifdef VT_BUF_VRAM_ONLY
278 #define DO_UPDATE(vc)   0
279 #else
280 #define DO_UPDATE(vc)   (CON_IS_VISIBLE(vc) && !console_blanked)
281 #endif
282
283 static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
284 {
285         unsigned short *p;
286         
287         if (!viewed)
288                 p = (unsigned short *)(vc->vc_origin + offset);
289         else if (!vc->vc_sw->con_screen_pos)
290                 p = (unsigned short *)(vc->vc_visible_origin + offset);
291         else
292                 p = vc->vc_sw->con_screen_pos(vc, offset);
293         return p;
294 }
295
296 /* Called  from the keyboard irq path.. */
297 static inline void scrolldelta(int lines)
298 {
299         /* FIXME */
300         /* scrolldelta needs some kind of consistency lock, but the BKL was
301            and still is not protecting versus the scheduled back end */
302         scrollback_delta += lines;
303         schedule_console_callback();
304 }
305
306 void schedule_console_callback(void)
307 {
308         schedule_work(&console_work);
309 }
310
311 static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
312 {
313         unsigned short *d, *s;
314
315         if (t+nr >= b)
316                 nr = b - t - 1;
317         if (b > vc->vc_rows || t >= b || nr < 1)
318                 return;
319         if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
320                 return;
321         d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
322         s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
323         scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
324         scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
325                     vc->vc_size_row * nr);
326 }
327
328 static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
329 {
330         unsigned short *s;
331         unsigned int step;
332
333         if (t+nr >= b)
334                 nr = b - t - 1;
335         if (b > vc->vc_rows || t >= b || nr < 1)
336                 return;
337         if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
338                 return;
339         s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
340         step = vc->vc_cols * nr;
341         scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
342         scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
343 }
344
345 static void do_update_region(struct vc_data *vc, unsigned long start, int count)
346 {
347 #ifndef VT_BUF_VRAM_ONLY
348         unsigned int xx, yy, offset;
349         u16 *p;
350
351         p = (u16 *) start;
352         if (!vc->vc_sw->con_getxy) {
353                 offset = (start - vc->vc_origin) / 2;
354                 xx = offset % vc->vc_cols;
355                 yy = offset / vc->vc_cols;
356         } else {
357                 int nxx, nyy;
358                 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
359                 xx = nxx; yy = nyy;
360         }
361         for(;;) {
362                 u16 attrib = scr_readw(p) & 0xff00;
363                 int startx = xx;
364                 u16 *q = p;
365                 while (xx < vc->vc_cols && count) {
366                         if (attrib != (scr_readw(p) & 0xff00)) {
367                                 if (p > q)
368                                         vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
369                                 startx = xx;
370                                 q = p;
371                                 attrib = scr_readw(p) & 0xff00;
372                         }
373                         p++;
374                         xx++;
375                         count--;
376                 }
377                 if (p > q)
378                         vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
379                 if (!count)
380                         break;
381                 xx = 0;
382                 yy++;
383                 if (vc->vc_sw->con_getxy) {
384                         p = (u16 *)start;
385                         start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
386                 }
387         }
388 #endif
389 }
390
391 void update_region(struct vc_data *vc, unsigned long start, int count)
392 {
393         WARN_CONSOLE_UNLOCKED();
394
395         if (DO_UPDATE(vc)) {
396                 hide_cursor(vc);
397                 do_update_region(vc, start, count);
398                 set_cursor(vc);
399         }
400 }
401
402 /* Structure of attributes is hardware-dependent */
403
404 static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
405     u8 _underline, u8 _reverse, u8 _italic)
406 {
407         if (vc->vc_sw->con_build_attr)
408                 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
409                        _blink, _underline, _reverse, _italic);
410
411 #ifndef VT_BUF_VRAM_ONLY
412 /*
413  * ++roman: I completely changed the attribute format for monochrome
414  * mode (!can_do_color). The formerly used MDA (monochrome display
415  * adapter) format didn't allow the combination of certain effects.
416  * Now the attribute is just a bit vector:
417  *  Bit 0..1: intensity (0..2)
418  *  Bit 2   : underline
419  *  Bit 3   : reverse
420  *  Bit 7   : blink
421  */
422         {
423         u8 a = _color;
424         if (!vc->vc_can_do_color)
425                 return _intensity |
426                        (_italic ? 2 : 0) |
427                        (_underline ? 4 : 0) |
428                        (_reverse ? 8 : 0) |
429                        (_blink ? 0x80 : 0);
430         if (_italic)
431                 a = (a & 0xF0) | vc->vc_itcolor;
432         else if (_underline)
433                 a = (a & 0xf0) | vc->vc_ulcolor;
434         else if (_intensity == 0)
435                 a = (a & 0xf0) | vc->vc_ulcolor;
436         if (_reverse)
437                 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
438         if (_blink)
439                 a ^= 0x80;
440         if (_intensity == 2)
441                 a ^= 0x08;
442         if (vc->vc_hi_font_mask == 0x100)
443                 a <<= 1;
444         return a;
445         }
446 #else
447         return 0;
448 #endif
449 }
450
451 static void update_attr(struct vc_data *vc)
452 {
453         vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
454                       vc->vc_blink, vc->vc_underline,
455                       vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
456         vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
457 }
458
459 /* Note: inverting the screen twice should revert to the original state */
460 void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
461 {
462         unsigned short *p;
463
464         WARN_CONSOLE_UNLOCKED();
465
466         count /= 2;
467         p = screenpos(vc, offset, viewed);
468         if (vc->vc_sw->con_invert_region)
469                 vc->vc_sw->con_invert_region(vc, p, count);
470 #ifndef VT_BUF_VRAM_ONLY
471         else {
472                 u16 *q = p;
473                 int cnt = count;
474                 u16 a;
475
476                 if (!vc->vc_can_do_color) {
477                         while (cnt--) {
478                             a = scr_readw(q);
479                             a ^= 0x0800;
480                             scr_writew(a, q);
481                             q++;
482                         }
483                 } else if (vc->vc_hi_font_mask == 0x100) {
484                         while (cnt--) {
485                                 a = scr_readw(q);
486                                 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
487                                 scr_writew(a, q);
488                                 q++;
489                         }
490                 } else {
491                         while (cnt--) {
492                                 a = scr_readw(q);
493                                 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
494                                 scr_writew(a, q);
495                                 q++;
496                         }
497                 }
498         }
499 #endif
500         if (DO_UPDATE(vc))
501                 do_update_region(vc, (unsigned long) p, count);
502         notify_update(vc);
503 }
504
505 /* used by selection: complement pointer position */
506 void complement_pos(struct vc_data *vc, int offset)
507 {
508         static int old_offset = -1;
509         static unsigned short old;
510         static unsigned short oldx, oldy;
511
512         WARN_CONSOLE_UNLOCKED();
513
514         if (old_offset != -1 && old_offset >= 0 &&
515             old_offset < vc->vc_screenbuf_size) {
516                 scr_writew(old, screenpos(vc, old_offset, 1));
517                 if (DO_UPDATE(vc))
518                         vc->vc_sw->con_putc(vc, old, oldy, oldx);
519                 notify_update(vc);
520         }
521
522         old_offset = offset;
523
524         if (offset != -1 && offset >= 0 &&
525             offset < vc->vc_screenbuf_size) {
526                 unsigned short new;
527                 unsigned short *p;
528                 p = screenpos(vc, offset, 1);
529                 old = scr_readw(p);
530                 new = old ^ vc->vc_complement_mask;
531                 scr_writew(new, p);
532                 if (DO_UPDATE(vc)) {
533                         oldx = (offset >> 1) % vc->vc_cols;
534                         oldy = (offset >> 1) / vc->vc_cols;
535                         vc->vc_sw->con_putc(vc, new, oldy, oldx);
536                 }
537                 notify_update(vc);
538         }
539 }
540
541 static void insert_char(struct vc_data *vc, unsigned int nr)
542 {
543         unsigned short *p, *q = (unsigned short *)vc->vc_pos;
544
545         p = q + vc->vc_cols - nr - vc->vc_x;
546         while (--p >= q)
547                 scr_writew(scr_readw(p), p + nr);
548         scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
549         vc->vc_need_wrap = 0;
550         if (DO_UPDATE(vc)) {
551                 unsigned short oldattr = vc->vc_attr;
552                 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
553                                      vc->vc_cols - vc->vc_x - nr);
554                 vc->vc_attr = vc->vc_video_erase_char >> 8;
555                 while (nr--)
556                         vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
557                 vc->vc_attr = oldattr;
558         }
559 }
560
561 static void delete_char(struct vc_data *vc, unsigned int nr)
562 {
563         unsigned int i = vc->vc_x;
564         unsigned short *p = (unsigned short *)vc->vc_pos;
565
566         while (++i <= vc->vc_cols - nr) {
567                 scr_writew(scr_readw(p+nr), p);
568                 p++;
569         }
570         scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
571         vc->vc_need_wrap = 0;
572         if (DO_UPDATE(vc)) {
573                 unsigned short oldattr = vc->vc_attr;
574                 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
575                                      vc->vc_cols - vc->vc_x - nr);
576                 vc->vc_attr = vc->vc_video_erase_char >> 8;
577                 while (nr--)
578                         vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
579                                      vc->vc_cols - 1 - nr);
580                 vc->vc_attr = oldattr;
581         }
582 }
583
584 static int softcursor_original;
585
586 static void add_softcursor(struct vc_data *vc)
587 {
588         int i = scr_readw((u16 *) vc->vc_pos);
589         u32 type = vc->vc_cursor_type;
590
591         if (! (type & 0x10)) return;
592         if (softcursor_original != -1) return;
593         softcursor_original = i;
594         i |= ((type >> 8) & 0xff00 );
595         i ^= ((type) & 0xff00 );
596         if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
597         if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
598         scr_writew(i, (u16 *) vc->vc_pos);
599         if (DO_UPDATE(vc))
600                 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
601 }
602
603 static void hide_softcursor(struct vc_data *vc)
604 {
605         if (softcursor_original != -1) {
606                 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
607                 if (DO_UPDATE(vc))
608                         vc->vc_sw->con_putc(vc, softcursor_original,
609                                         vc->vc_y, vc->vc_x);
610                 softcursor_original = -1;
611         }
612 }
613
614 static void hide_cursor(struct vc_data *vc)
615 {
616         if (vc == sel_cons)
617                 clear_selection();
618         vc->vc_sw->con_cursor(vc, CM_ERASE);
619         hide_softcursor(vc);
620 }
621
622 static void set_cursor(struct vc_data *vc)
623 {
624         if (!IS_FG(vc) || console_blanked ||
625             vc->vc_mode == KD_GRAPHICS)
626                 return;
627         if (vc->vc_deccm) {
628                 if (vc == sel_cons)
629                         clear_selection();
630                 add_softcursor(vc);
631                 if ((vc->vc_cursor_type & 0x0f) != 1)
632                         vc->vc_sw->con_cursor(vc, CM_DRAW);
633         } else
634                 hide_cursor(vc);
635 }
636
637 static void set_origin(struct vc_data *vc)
638 {
639         WARN_CONSOLE_UNLOCKED();
640
641         if (!CON_IS_VISIBLE(vc) ||
642             !vc->vc_sw->con_set_origin ||
643             !vc->vc_sw->con_set_origin(vc))
644                 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
645         vc->vc_visible_origin = vc->vc_origin;
646         vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
647         vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
648 }
649
650 static inline void save_screen(struct vc_data *vc)
651 {
652         WARN_CONSOLE_UNLOCKED();
653
654         if (vc->vc_sw->con_save_screen)
655                 vc->vc_sw->con_save_screen(vc);
656 }
657
658 /*
659  *      Redrawing of screen
660  */
661
662 void clear_buffer_attributes(struct vc_data *vc)
663 {
664         unsigned short *p = (unsigned short *)vc->vc_origin;
665         int count = vc->vc_screenbuf_size / 2;
666         int mask = vc->vc_hi_font_mask | 0xff;
667
668         for (; count > 0; count--, p++) {
669                 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
670         }
671 }
672
673 void redraw_screen(struct vc_data *vc, int is_switch)
674 {
675         int redraw = 0;
676
677         WARN_CONSOLE_UNLOCKED();
678
679         if (!vc) {
680                 /* strange ... */
681                 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
682                 return;
683         }
684
685         if (is_switch) {
686                 struct vc_data *old_vc = vc_cons[fg_console].d;
687                 if (old_vc == vc)
688                         return;
689                 if (!CON_IS_VISIBLE(vc))
690                         redraw = 1;
691                 *vc->vc_display_fg = vc;
692                 fg_console = vc->vc_num;
693                 hide_cursor(old_vc);
694                 if (!CON_IS_VISIBLE(old_vc)) {
695                         save_screen(old_vc);
696                         set_origin(old_vc);
697                 }
698                 if (tty0dev)
699                         sysfs_notify(&tty0dev->kobj, NULL, "active");
700         } else {
701                 hide_cursor(vc);
702                 redraw = 1;
703         }
704
705         if (redraw) {
706                 int update;
707                 int old_was_color = vc->vc_can_do_color;
708
709                 set_origin(vc);
710                 update = vc->vc_sw->con_switch(vc);
711                 set_palette(vc);
712                 /*
713                  * If console changed from mono<->color, the best we can do
714                  * is to clear the buffer attributes. As it currently stands,
715                  * rebuilding new attributes from the old buffer is not doable
716                  * without overly complex code.
717                  */
718                 if (old_was_color != vc->vc_can_do_color) {
719                         update_attr(vc);
720                         clear_buffer_attributes(vc);
721                 }
722
723                 /* Forcibly update if we're panicing */
724                 if ((update && vc->vc_mode != KD_GRAPHICS) ||
725                     vt_force_oops_output(vc))
726                         do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
727         }
728         set_cursor(vc);
729         if (is_switch) {
730                 set_leds();
731                 compute_shiftstate();
732                 notify_update(vc);
733         }
734 }
735
736 /*
737  *      Allocation, freeing and resizing of VTs.
738  */
739
740 int vc_cons_allocated(unsigned int i)
741 {
742         return (i < MAX_NR_CONSOLES && vc_cons[i].d);
743 }
744
745 static void visual_init(struct vc_data *vc, int num, int init)
746 {
747         /* ++Geert: vc->vc_sw->con_init determines console size */
748         if (vc->vc_sw)
749                 module_put(vc->vc_sw->owner);
750         vc->vc_sw = conswitchp;
751 #ifndef VT_SINGLE_DRIVER
752         if (con_driver_map[num])
753                 vc->vc_sw = con_driver_map[num];
754 #endif
755         __module_get(vc->vc_sw->owner);
756         vc->vc_num = num;
757         vc->vc_display_fg = &master_display_fg;
758         vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
759         vc->vc_uni_pagedir = 0;
760         vc->vc_hi_font_mask = 0;
761         vc->vc_complement_mask = 0;
762         vc->vc_can_do_color = 0;
763         vc->vc_panic_force_write = false;
764         vc->vc_sw->con_init(vc, init);
765         if (!vc->vc_complement_mask)
766                 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
767         vc->vc_s_complement_mask = vc->vc_complement_mask;
768         vc->vc_size_row = vc->vc_cols << 1;
769         vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
770 }
771
772 int vc_allocate(unsigned int currcons)  /* return 0 on success */
773 {
774         WARN_CONSOLE_UNLOCKED();
775
776         if (currcons >= MAX_NR_CONSOLES)
777                 return -ENXIO;
778         if (!vc_cons[currcons].d) {
779             struct vc_data *vc;
780             struct vt_notifier_param param;
781
782             /* prevent users from taking too much memory */
783             if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
784               return -EPERM;
785
786             /* due to the granularity of kmalloc, we waste some memory here */
787             /* the alloc is done in two steps, to optimize the common situation
788                of a 25x80 console (structsize=216, screenbuf_size=4000) */
789             /* although the numbers above are not valid since long ago, the
790                point is still up-to-date and the comment still has its value
791                even if only as a historical artifact.  --mj, July 1998 */
792             param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
793             if (!vc)
794                 return -ENOMEM;
795             vc_cons[currcons].d = vc;
796             tty_port_init(&vc->port);
797             INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
798             visual_init(vc, currcons, 1);
799             if (!*vc->vc_uni_pagedir_loc)
800                 con_set_default_unimap(vc);
801             vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
802             if (!vc->vc_screenbuf) {
803                 kfree(vc);
804                 vc_cons[currcons].d = NULL;
805                 return -ENOMEM;
806             }
807
808             /* If no drivers have overridden us and the user didn't pass a
809                boot option, default to displaying the cursor */
810             if (global_cursor_default == -1)
811                     global_cursor_default = 1;
812
813             vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
814             vcs_make_sysfs(currcons);
815             atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
816         }
817         return 0;
818 }
819
820 static inline int resize_screen(struct vc_data *vc, int width, int height,
821                                 int user)
822 {
823         /* Resizes the resolution of the display adapater */
824         int err = 0;
825
826         if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
827                 err = vc->vc_sw->con_resize(vc, width, height, user);
828
829         return err;
830 }
831
832 /*
833  * Change # of rows and columns (0 means unchanged/the size of fg_console)
834  * [this is to be used together with some user program
835  * like resize that changes the hardware videomode]
836  */
837 #define VC_RESIZE_MAXCOL (32767)
838 #define VC_RESIZE_MAXROW (32767)
839
840 /**
841  *      vc_do_resize    -       resizing method for the tty
842  *      @tty: tty being resized
843  *      @real_tty: real tty (different to tty if a pty/tty pair)
844  *      @vc: virtual console private data
845  *      @cols: columns
846  *      @lines: lines
847  *
848  *      Resize a virtual console, clipping according to the actual constraints.
849  *      If the caller passes a tty structure then update the termios winsize
850  *      information and perform any necessary signal handling.
851  *
852  *      Caller must hold the console semaphore. Takes the termios mutex and
853  *      ctrl_lock of the tty IFF a tty is passed.
854  */
855
856 static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
857                                 unsigned int cols, unsigned int lines)
858 {
859         unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
860         unsigned long end;
861         unsigned int old_rows, old_row_size;
862         unsigned int new_cols, new_rows, new_row_size, new_screen_size;
863         unsigned int user;
864         unsigned short *newscreen;
865
866         WARN_CONSOLE_UNLOCKED();
867
868         if (!vc)
869                 return -ENXIO;
870
871         user = vc->vc_resize_user;
872         vc->vc_resize_user = 0;
873
874         if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
875                 return -EINVAL;
876
877         new_cols = (cols ? cols : vc->vc_cols);
878         new_rows = (lines ? lines : vc->vc_rows);
879         new_row_size = new_cols << 1;
880         new_screen_size = new_row_size * new_rows;
881
882         if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
883                 return 0;
884
885         if (new_screen_size > (4 << 20))
886                 return -EINVAL;
887         newscreen = kmalloc(new_screen_size, GFP_USER);
888         if (!newscreen)
889                 return -ENOMEM;
890
891         if (vc == sel_cons)
892                 clear_selection();
893
894         old_rows = vc->vc_rows;
895         old_row_size = vc->vc_size_row;
896
897         err = resize_screen(vc, new_cols, new_rows, user);
898         if (err) {
899                 kfree(newscreen);
900                 return err;
901         }
902
903         vc->vc_rows = new_rows;
904         vc->vc_cols = new_cols;
905         vc->vc_size_row = new_row_size;
906         vc->vc_screenbuf_size = new_screen_size;
907
908         rlth = min(old_row_size, new_row_size);
909         rrem = new_row_size - rlth;
910         old_origin = vc->vc_origin;
911         new_origin = (long) newscreen;
912         new_scr_end = new_origin + new_screen_size;
913
914         if (vc->vc_y > new_rows) {
915                 if (old_rows - vc->vc_y < new_rows) {
916                         /*
917                          * Cursor near the bottom, copy contents from the
918                          * bottom of buffer
919                          */
920                         old_origin += (old_rows - new_rows) * old_row_size;
921                 } else {
922                         /*
923                          * Cursor is in no man's land, copy 1/2 screenful
924                          * from the top and bottom of cursor position
925                          */
926                         old_origin += (vc->vc_y - new_rows/2) * old_row_size;
927                 }
928         }
929
930         end = old_origin + old_row_size * min(old_rows, new_rows);
931
932         update_attr(vc);
933
934         while (old_origin < end) {
935                 scr_memcpyw((unsigned short *) new_origin,
936                             (unsigned short *) old_origin, rlth);
937                 if (rrem)
938                         scr_memsetw((void *)(new_origin + rlth),
939                                     vc->vc_video_erase_char, rrem);
940                 old_origin += old_row_size;
941                 new_origin += new_row_size;
942         }
943         if (new_scr_end > new_origin)
944                 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
945                             new_scr_end - new_origin);
946         kfree(vc->vc_screenbuf);
947         vc->vc_screenbuf = newscreen;
948         vc->vc_screenbuf_size = new_screen_size;
949         set_origin(vc);
950
951         /* do part of a reset_terminal() */
952         vc->vc_top = 0;
953         vc->vc_bottom = vc->vc_rows;
954         gotoxy(vc, vc->vc_x, vc->vc_y);
955         save_cur(vc);
956
957         if (tty) {
958                 /* Rewrite the requested winsize data with the actual
959                    resulting sizes */
960                 struct winsize ws;
961                 memset(&ws, 0, sizeof(ws));
962                 ws.ws_row = vc->vc_rows;
963                 ws.ws_col = vc->vc_cols;
964                 ws.ws_ypixel = vc->vc_scan_lines;
965                 tty_do_resize(tty, &ws);
966         }
967
968         if (CON_IS_VISIBLE(vc))
969                 update_screen(vc);
970         vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
971         return err;
972 }
973
974 /**
975  *      vc_resize               -       resize a VT
976  *      @vc: virtual console
977  *      @cols: columns
978  *      @rows: rows
979  *
980  *      Resize a virtual console as seen from the console end of things. We
981  *      use the common vc_do_resize methods to update the structures. The
982  *      caller must hold the console sem to protect console internals and
983  *      vc->port.tty
984  */
985
986 int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
987 {
988         return vc_do_resize(vc->port.tty, vc, cols, rows);
989 }
990
991 /**
992  *      vt_resize               -       resize a VT
993  *      @tty: tty to resize
994  *      @ws: winsize attributes
995  *
996  *      Resize a virtual terminal. This is called by the tty layer as we
997  *      register our own handler for resizing. The mutual helper does all
998  *      the actual work.
999  *
1000  *      Takes the console sem and the called methods then take the tty
1001  *      termios_mutex and the tty ctrl_lock in that order.
1002  */
1003 static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1004 {
1005         struct vc_data *vc = tty->driver_data;
1006         int ret;
1007
1008         console_lock();
1009         ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1010         console_unlock();
1011         return ret;
1012 }
1013
1014 void vc_deallocate(unsigned int currcons)
1015 {
1016         WARN_CONSOLE_UNLOCKED();
1017
1018         if (vc_cons_allocated(currcons)) {
1019                 struct vc_data *vc = vc_cons[currcons].d;
1020                 struct vt_notifier_param param = { .vc = vc };
1021
1022                 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1023                 vcs_remove_sysfs(currcons);
1024                 vc->vc_sw->con_deinit(vc);
1025                 put_pid(vc->vt_pid);
1026                 module_put(vc->vc_sw->owner);
1027                 kfree(vc->vc_screenbuf);
1028                 if (currcons >= MIN_NR_CONSOLES)
1029                         kfree(vc);
1030                 vc_cons[currcons].d = NULL;
1031         }
1032 }
1033
1034 /*
1035  *      VT102 emulator
1036  */
1037
1038 #define set_kbd(vc, x)  set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1039 #define clr_kbd(vc, x)  clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1040 #define is_kbd(vc, x)   vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1041
1042 #define decarm          VC_REPEAT
1043 #define decckm          VC_CKMODE
1044 #define kbdapplic       VC_APPLIC
1045 #define lnm             VC_CRLF
1046
1047 /*
1048  * this is what the terminal answers to a ESC-Z or csi0c query.
1049  */
1050 #define VT100ID "\033[?1;2c"
1051 #define VT102ID "\033[?6c"
1052
1053 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1054                                        8,12,10,14, 9,13,11,15 };
1055
1056 /* the default colour table, for VGA+ colour systems */
1057 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1058     0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1059 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1060     0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1061 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1062     0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1063
1064 module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1065 module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1066 module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1067
1068 /*
1069  * gotoxy() must verify all boundaries, because the arguments
1070  * might also be negative. If the given position is out of
1071  * bounds, the cursor is placed at the nearest margin.
1072  */
1073 static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1074 {
1075         int min_y, max_y;
1076
1077         if (new_x < 0)
1078                 vc->vc_x = 0;
1079         else {
1080                 if (new_x >= vc->vc_cols)
1081                         vc->vc_x = vc->vc_cols - 1;
1082                 else
1083                         vc->vc_x = new_x;
1084         }
1085
1086         if (vc->vc_decom) {
1087                 min_y = vc->vc_top;
1088                 max_y = vc->vc_bottom;
1089         } else {
1090                 min_y = 0;
1091                 max_y = vc->vc_rows;
1092         }
1093         if (new_y < min_y)
1094                 vc->vc_y = min_y;
1095         else if (new_y >= max_y)
1096                 vc->vc_y = max_y - 1;
1097         else
1098                 vc->vc_y = new_y;
1099         vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1100         vc->vc_need_wrap = 0;
1101 }
1102
1103 /* for absolute user moves, when decom is set */
1104 static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1105 {
1106         gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1107 }
1108
1109 void scrollback(struct vc_data *vc, int lines)
1110 {
1111         if (!lines)
1112                 lines = vc->vc_rows / 2;
1113         scrolldelta(-lines);
1114 }
1115
1116 void scrollfront(struct vc_data *vc, int lines)
1117 {
1118         if (!lines)
1119                 lines = vc->vc_rows / 2;
1120         scrolldelta(lines);
1121 }
1122
1123 static void lf(struct vc_data *vc)
1124 {
1125         /* don't scroll if above bottom of scrolling region, or
1126          * if below scrolling region
1127          */
1128         if (vc->vc_y + 1 == vc->vc_bottom)
1129                 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1130         else if (vc->vc_y < vc->vc_rows - 1) {
1131                 vc->vc_y++;
1132                 vc->vc_pos += vc->vc_size_row;
1133         }
1134         vc->vc_need_wrap = 0;
1135         notify_write(vc, '\n');
1136 }
1137
1138 static void ri(struct vc_data *vc)
1139 {
1140         /* don't scroll if below top of scrolling region, or
1141          * if above scrolling region
1142          */
1143         if (vc->vc_y == vc->vc_top)
1144                 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1145         else if (vc->vc_y > 0) {
1146                 vc->vc_y--;
1147                 vc->vc_pos -= vc->vc_size_row;
1148         }
1149         vc->vc_need_wrap = 0;
1150 }
1151
1152 static inline void cr(struct vc_data *vc)
1153 {
1154         vc->vc_pos -= vc->vc_x << 1;
1155         vc->vc_need_wrap = vc->vc_x = 0;
1156         notify_write(vc, '\r');
1157 }
1158
1159 static inline void bs(struct vc_data *vc)
1160 {
1161         if (vc->vc_x) {
1162                 vc->vc_pos -= 2;
1163                 vc->vc_x--;
1164                 vc->vc_need_wrap = 0;
1165                 notify_write(vc, '\b');
1166         }
1167 }
1168
1169 static inline void del(struct vc_data *vc)
1170 {
1171         /* ignored */
1172 }
1173
1174 static void csi_J(struct vc_data *vc, int vpar)
1175 {
1176         unsigned int count;
1177         unsigned short * start;
1178
1179         switch (vpar) {
1180                 case 0: /* erase from cursor to end of display */
1181                         count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1182                         start = (unsigned short *)vc->vc_pos;
1183                         if (DO_UPDATE(vc)) {
1184                                 /* do in two stages */
1185                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1186                                               vc->vc_cols - vc->vc_x);
1187                                 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1188                                               vc->vc_rows - vc->vc_y - 1,
1189                                               vc->vc_cols);
1190                         }
1191                         break;
1192                 case 1: /* erase from start to cursor */
1193                         count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1194                         start = (unsigned short *)vc->vc_origin;
1195                         if (DO_UPDATE(vc)) {
1196                                 /* do in two stages */
1197                                 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1198                                               vc->vc_cols);
1199                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1200                                               vc->vc_x + 1);
1201                         }
1202                         break;
1203                 case 3: /* erase scroll-back buffer (and whole display) */
1204                         scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1205                                     vc->vc_screenbuf_size);
1206                         set_origin(vc);
1207                         if (CON_IS_VISIBLE(vc))
1208                                 update_screen(vc);
1209                         /* fall through */
1210                 case 2: /* erase whole display */
1211                         count = vc->vc_cols * vc->vc_rows;
1212                         start = (unsigned short *)vc->vc_origin;
1213                         if (DO_UPDATE(vc))
1214                                 vc->vc_sw->con_clear(vc, 0, 0,
1215                                               vc->vc_rows,
1216                                               vc->vc_cols);
1217                         break;
1218                 default:
1219                         return;
1220         }
1221         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1222         vc->vc_need_wrap = 0;
1223 }
1224
1225 static void csi_K(struct vc_data *vc, int vpar)
1226 {
1227         unsigned int count;
1228         unsigned short * start;
1229
1230         switch (vpar) {
1231                 case 0: /* erase from cursor to end of line */
1232                         count = vc->vc_cols - vc->vc_x;
1233                         start = (unsigned short *)vc->vc_pos;
1234                         if (DO_UPDATE(vc))
1235                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1236                                                      vc->vc_cols - vc->vc_x);
1237                         break;
1238                 case 1: /* erase from start of line to cursor */
1239                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1240                         count = vc->vc_x + 1;
1241                         if (DO_UPDATE(vc))
1242                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1243                                                      vc->vc_x + 1);
1244                         break;
1245                 case 2: /* erase whole line */
1246                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1247                         count = vc->vc_cols;
1248                         if (DO_UPDATE(vc))
1249                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1250                                               vc->vc_cols);
1251                         break;
1252                 default:
1253                         return;
1254         }
1255         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1256         vc->vc_need_wrap = 0;
1257 }
1258
1259 static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1260 {                                         /* not vt100? */
1261         int count;
1262
1263         if (!vpar)
1264                 vpar++;
1265         count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1266
1267         scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1268         if (DO_UPDATE(vc))
1269                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1270         vc->vc_need_wrap = 0;
1271 }
1272
1273 static void default_attr(struct vc_data *vc)
1274 {
1275         vc->vc_intensity = 1;
1276         vc->vc_italic = 0;
1277         vc->vc_underline = 0;
1278         vc->vc_reverse = 0;
1279         vc->vc_blink = 0;
1280         vc->vc_color = vc->vc_def_color;
1281 }
1282
1283 /* console_lock is held */
1284 static void csi_m(struct vc_data *vc)
1285 {
1286         int i;
1287
1288         for (i = 0; i <= vc->vc_npar; i++)
1289                 switch (vc->vc_par[i]) {
1290                         case 0: /* all attributes off */
1291                                 default_attr(vc);
1292                                 break;
1293                         case 1:
1294                                 vc->vc_intensity = 2;
1295                                 break;
1296                         case 2:
1297                                 vc->vc_intensity = 0;
1298                                 break;
1299                         case 3:
1300                                 vc->vc_italic = 1;
1301                                 break;
1302                         case 4:
1303                                 vc->vc_underline = 1;
1304                                 break;
1305                         case 5:
1306                                 vc->vc_blink = 1;
1307                                 break;
1308                         case 7:
1309                                 vc->vc_reverse = 1;
1310                                 break;
1311                         case 10: /* ANSI X3.64-1979 (SCO-ish?)
1312                                   * Select primary font, don't display
1313                                   * control chars if defined, don't set
1314                                   * bit 8 on output.
1315                                   */
1316                                 vc->vc_translate = set_translate(vc->vc_charset == 0
1317                                                 ? vc->vc_G0_charset
1318                                                 : vc->vc_G1_charset, vc);
1319                                 vc->vc_disp_ctrl = 0;
1320                                 vc->vc_toggle_meta = 0;
1321                                 break;
1322                         case 11: /* ANSI X3.64-1979 (SCO-ish?)
1323                                   * Select first alternate font, lets
1324                                   * chars < 32 be displayed as ROM chars.
1325                                   */
1326                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1327                                 vc->vc_disp_ctrl = 1;
1328                                 vc->vc_toggle_meta = 0;
1329                                 break;
1330                         case 12: /* ANSI X3.64-1979 (SCO-ish?)
1331                                   * Select second alternate font, toggle
1332                                   * high bit before displaying as ROM char.
1333                                   */
1334                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1335                                 vc->vc_disp_ctrl = 1;
1336                                 vc->vc_toggle_meta = 1;
1337                                 break;
1338                         case 21:
1339                         case 22:
1340                                 vc->vc_intensity = 1;
1341                                 break;
1342                         case 23:
1343                                 vc->vc_italic = 0;
1344                                 break;
1345                         case 24:
1346                                 vc->vc_underline = 0;
1347                                 break;
1348                         case 25:
1349                                 vc->vc_blink = 0;
1350                                 break;
1351                         case 27:
1352                                 vc->vc_reverse = 0;
1353                                 break;
1354                         case 38: /* ANSI X3.64-1979 (SCO-ish?)
1355                                   * Enables underscore, white foreground
1356                                   * with white underscore (Linux - use
1357                                   * default foreground).
1358                                   */
1359                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1360                                 vc->vc_underline = 1;
1361                                 break;
1362                         case 39: /* ANSI X3.64-1979 (SCO-ish?)
1363                                   * Disable underline option.
1364                                   * Reset colour to default? It did this
1365                                   * before...
1366                                   */
1367                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1368                                 vc->vc_underline = 0;
1369                                 break;
1370                         case 49:
1371                                 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1372                                 break;
1373                         default:
1374                                 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1375                                         vc->vc_color = color_table[vc->vc_par[i] - 30]
1376                                                 | (vc->vc_color & 0xf0);
1377                                 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1378                                         vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1379                                                 | (vc->vc_color & 0x0f);
1380                                 break;
1381                 }
1382         update_attr(vc);
1383 }
1384
1385 static void respond_string(const char *p, struct tty_struct *tty)
1386 {
1387         while (*p) {
1388                 tty_insert_flip_char(tty, *p, 0);
1389                 p++;
1390         }
1391         con_schedule_flip(tty);
1392 }
1393
1394 static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1395 {
1396         char buf[40];
1397
1398         sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1399         respond_string(buf, tty);
1400 }
1401
1402 static inline void status_report(struct tty_struct *tty)
1403 {
1404         respond_string("\033[0n", tty); /* Terminal ok */
1405 }
1406
1407 static inline void respond_ID(struct tty_struct * tty)
1408 {
1409         respond_string(VT102ID, tty);
1410 }
1411
1412 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1413 {
1414         char buf[8];
1415
1416         sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1417                 (char)('!' + mry));
1418         respond_string(buf, tty);
1419 }
1420
1421 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1422 int mouse_reporting(void)
1423 {
1424         return vc_cons[fg_console].d->vc_report_mouse;
1425 }
1426
1427 /* console_lock is held */
1428 static void set_mode(struct vc_data *vc, int on_off)
1429 {
1430         int i;
1431
1432         for (i = 0; i <= vc->vc_npar; i++)
1433                 if (vc->vc_ques) {
1434                         switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1435                         case 1:                 /* Cursor keys send ^[Ox/^[[x */
1436                                 if (on_off)
1437                                         set_kbd(vc, decckm);
1438                                 else
1439                                         clr_kbd(vc, decckm);
1440                                 break;
1441                         case 3: /* 80/132 mode switch unimplemented */
1442                                 vc->vc_deccolm = on_off;
1443 #if 0
1444                                 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1445                                 /* this alone does not suffice; some user mode
1446                                    utility has to change the hardware regs */
1447 #endif
1448                                 break;
1449                         case 5:                 /* Inverted screen on/off */
1450                                 if (vc->vc_decscnm != on_off) {
1451                                         vc->vc_decscnm = on_off;
1452                                         invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1453                                         update_attr(vc);
1454                                 }
1455                                 break;
1456                         case 6:                 /* Origin relative/absolute */
1457                                 vc->vc_decom = on_off;
1458                                 gotoxay(vc, 0, 0);
1459                                 break;
1460                         case 7:                 /* Autowrap on/off */
1461                                 vc->vc_decawm = on_off;
1462                                 break;
1463                         case 8:                 /* Autorepeat on/off */
1464                                 if (on_off)
1465                                         set_kbd(vc, decarm);
1466                                 else
1467                                         clr_kbd(vc, decarm);
1468                                 break;
1469                         case 9:
1470                                 vc->vc_report_mouse = on_off ? 1 : 0;
1471                                 break;
1472                         case 25:                /* Cursor on/off */
1473                                 vc->vc_deccm = on_off;
1474                                 break;
1475                         case 1000:
1476                                 vc->vc_report_mouse = on_off ? 2 : 0;
1477                                 break;
1478                         }
1479                 } else {
1480                         switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1481                         case 3:                 /* Monitor (display ctrls) */
1482                                 vc->vc_disp_ctrl = on_off;
1483                                 break;
1484                         case 4:                 /* Insert Mode on/off */
1485                                 vc->vc_decim = on_off;
1486                                 break;
1487                         case 20:                /* Lf, Enter == CrLf/Lf */
1488                                 if (on_off)
1489                                         set_kbd(vc, lnm);
1490                                 else
1491                                         clr_kbd(vc, lnm);
1492                                 break;
1493                         }
1494                 }
1495 }
1496
1497 /* console_lock is held */
1498 static void setterm_command(struct vc_data *vc)
1499 {
1500         switch(vc->vc_par[0]) {
1501                 case 1: /* set color for underline mode */
1502                         if (vc->vc_can_do_color &&
1503                                         vc->vc_par[1] < 16) {
1504                                 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1505                                 if (vc->vc_underline)
1506                                         update_attr(vc);
1507                         }
1508                         break;
1509                 case 2: /* set color for half intensity mode */
1510                         if (vc->vc_can_do_color &&
1511                                         vc->vc_par[1] < 16) {
1512                                 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1513                                 if (vc->vc_intensity == 0)
1514                                         update_attr(vc);
1515                         }
1516                         break;
1517                 case 8: /* store colors as defaults */
1518                         vc->vc_def_color = vc->vc_attr;
1519                         if (vc->vc_hi_font_mask == 0x100)
1520                                 vc->vc_def_color >>= 1;
1521                         default_attr(vc);
1522                         update_attr(vc);
1523                         break;
1524                 case 9: /* set blanking interval */
1525                         blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1526                         poke_blanked_console();
1527                         break;
1528                 case 10: /* set bell frequency in Hz */
1529                         if (vc->vc_npar >= 1)
1530                                 vc->vc_bell_pitch = vc->vc_par[1];
1531                         else
1532                                 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1533                         break;
1534                 case 11: /* set bell duration in msec */
1535                         if (vc->vc_npar >= 1)
1536                                 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1537                                         vc->vc_par[1] * HZ / 1000 : 0;
1538                         else
1539                                 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1540                         break;
1541                 case 12: /* bring specified console to the front */
1542                         if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1543                                 set_console(vc->vc_par[1] - 1);
1544                         break;
1545                 case 13: /* unblank the screen */
1546                         poke_blanked_console();
1547                         break;
1548                 case 14: /* set vesa powerdown interval */
1549                         vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1550                         break;
1551                 case 15: /* activate the previous console */
1552                         set_console(last_console);
1553                         break;
1554         }
1555 }
1556
1557 /* console_lock is held */
1558 static void csi_at(struct vc_data *vc, unsigned int nr)
1559 {
1560         if (nr > vc->vc_cols - vc->vc_x)
1561                 nr = vc->vc_cols - vc->vc_x;
1562         else if (!nr)
1563                 nr = 1;
1564         insert_char(vc, nr);
1565 }
1566
1567 /* console_lock is held */
1568 static void csi_L(struct vc_data *vc, unsigned int nr)
1569 {
1570         if (nr > vc->vc_rows - vc->vc_y)
1571                 nr = vc->vc_rows - vc->vc_y;
1572         else if (!nr)
1573                 nr = 1;
1574         scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1575         vc->vc_need_wrap = 0;
1576 }
1577
1578 /* console_lock is held */
1579 static void csi_P(struct vc_data *vc, unsigned int nr)
1580 {
1581         if (nr > vc->vc_cols - vc->vc_x)
1582                 nr = vc->vc_cols - vc->vc_x;
1583         else if (!nr)
1584                 nr = 1;
1585         delete_char(vc, nr);
1586 }
1587
1588 /* console_lock is held */
1589 static void csi_M(struct vc_data *vc, unsigned int nr)
1590 {
1591         if (nr > vc->vc_rows - vc->vc_y)
1592                 nr = vc->vc_rows - vc->vc_y;
1593         else if (!nr)
1594                 nr=1;
1595         scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1596         vc->vc_need_wrap = 0;
1597 }
1598
1599 /* console_lock is held (except via vc_init->reset_terminal */
1600 static void save_cur(struct vc_data *vc)
1601 {
1602         vc->vc_saved_x          = vc->vc_x;
1603         vc->vc_saved_y          = vc->vc_y;
1604         vc->vc_s_intensity      = vc->vc_intensity;
1605         vc->vc_s_italic         = vc->vc_italic;
1606         vc->vc_s_underline      = vc->vc_underline;
1607         vc->vc_s_blink          = vc->vc_blink;
1608         vc->vc_s_reverse        = vc->vc_reverse;
1609         vc->vc_s_charset        = vc->vc_charset;
1610         vc->vc_s_color          = vc->vc_color;
1611         vc->vc_saved_G0         = vc->vc_G0_charset;
1612         vc->vc_saved_G1         = vc->vc_G1_charset;
1613 }
1614
1615 /* console_lock is held */
1616 static void restore_cur(struct vc_data *vc)
1617 {
1618         gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1619         vc->vc_intensity        = vc->vc_s_intensity;
1620         vc->vc_italic           = vc->vc_s_italic;
1621         vc->vc_underline        = vc->vc_s_underline;
1622         vc->vc_blink            = vc->vc_s_blink;
1623         vc->vc_reverse          = vc->vc_s_reverse;
1624         vc->vc_charset          = vc->vc_s_charset;
1625         vc->vc_color            = vc->vc_s_color;
1626         vc->vc_G0_charset       = vc->vc_saved_G0;
1627         vc->vc_G1_charset       = vc->vc_saved_G1;
1628         vc->vc_translate        = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1629         update_attr(vc);
1630         vc->vc_need_wrap = 0;
1631 }
1632
1633 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1634         EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1635         ESpalette };
1636
1637 /* console_lock is held (except via vc_init()) */
1638 static void reset_terminal(struct vc_data *vc, int do_clear)
1639 {
1640         vc->vc_top              = 0;
1641         vc->vc_bottom           = vc->vc_rows;
1642         vc->vc_state            = ESnormal;
1643         vc->vc_ques             = 0;
1644         vc->vc_translate        = set_translate(LAT1_MAP, vc);
1645         vc->vc_G0_charset       = LAT1_MAP;
1646         vc->vc_G1_charset       = GRAF_MAP;
1647         vc->vc_charset          = 0;
1648         vc->vc_need_wrap        = 0;
1649         vc->vc_report_mouse     = 0;
1650         vc->vc_utf              = default_utf8;
1651         vc->vc_utf_count        = 0;
1652
1653         vc->vc_disp_ctrl        = 0;
1654         vc->vc_toggle_meta      = 0;
1655
1656         vc->vc_decscnm          = 0;
1657         vc->vc_decom            = 0;
1658         vc->vc_decawm           = 1;
1659         vc->vc_deccm            = global_cursor_default;
1660         vc->vc_decim            = 0;
1661
1662         set_kbd(vc, decarm);
1663         clr_kbd(vc, decckm);
1664         clr_kbd(vc, kbdapplic);
1665         clr_kbd(vc, lnm);
1666         kbd_table[vc->vc_num].lockstate = 0;
1667         kbd_table[vc->vc_num].slockstate = 0;
1668         kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1669         kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1670         /* do not do set_leds here because this causes an endless tasklet loop
1671            when the keyboard hasn't been initialized yet */
1672
1673         vc->vc_cursor_type = cur_default;
1674         vc->vc_complement_mask = vc->vc_s_complement_mask;
1675
1676         default_attr(vc);
1677         update_attr(vc);
1678
1679         vc->vc_tab_stop[0]      = 0x01010100;
1680         vc->vc_tab_stop[1]      =
1681         vc->vc_tab_stop[2]      =
1682         vc->vc_tab_stop[3]      =
1683         vc->vc_tab_stop[4]      =
1684         vc->vc_tab_stop[5]      =
1685         vc->vc_tab_stop[6]      =
1686         vc->vc_tab_stop[7]      = 0x01010101;
1687
1688         vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1689         vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1690
1691         gotoxy(vc, 0, 0);
1692         save_cur(vc);
1693         if (do_clear)
1694             csi_J(vc, 2);
1695 }
1696
1697 /* console_lock is held */
1698 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1699 {
1700         /*
1701          *  Control characters can be used in the _middle_
1702          *  of an escape sequence.
1703          */
1704         switch (c) {
1705         case 0:
1706                 return;
1707         case 7:
1708                 if (vc->vc_bell_duration)
1709                         kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1710                 return;
1711         case 8:
1712                 bs(vc);
1713                 return;
1714         case 9:
1715                 vc->vc_pos -= (vc->vc_x << 1);
1716                 while (vc->vc_x < vc->vc_cols - 1) {
1717                         vc->vc_x++;
1718                         if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1719                                 break;
1720                 }
1721                 vc->vc_pos += (vc->vc_x << 1);
1722                 notify_write(vc, '\t');
1723                 return;
1724         case 10: case 11: case 12:
1725                 lf(vc);
1726                 if (!is_kbd(vc, lnm))
1727                         return;
1728         case 13:
1729                 cr(vc);
1730                 return;
1731         case 14:
1732                 vc->vc_charset = 1;
1733                 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1734                 vc->vc_disp_ctrl = 1;
1735                 return;
1736         case 15:
1737                 vc->vc_charset = 0;
1738                 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1739                 vc->vc_disp_ctrl = 0;
1740                 return;
1741         case 24: case 26:
1742                 vc->vc_state = ESnormal;
1743                 return;
1744         case 27:
1745                 vc->vc_state = ESesc;
1746                 return;
1747         case 127:
1748                 del(vc);
1749                 return;
1750         case 128+27:
1751                 vc->vc_state = ESsquare;
1752                 return;
1753         }
1754         switch(vc->vc_state) {
1755         case ESesc:
1756                 vc->vc_state = ESnormal;
1757                 switch (c) {
1758                 case '[':
1759                         vc->vc_state = ESsquare;
1760                         return;
1761                 case ']':
1762                         vc->vc_state = ESnonstd;
1763                         return;
1764                 case '%':
1765                         vc->vc_state = ESpercent;
1766                         return;
1767                 case 'E':
1768                         cr(vc);
1769                         lf(vc);
1770                         return;
1771                 case 'M':
1772                         ri(vc);
1773                         return;
1774                 case 'D':
1775                         lf(vc);
1776                         return;
1777                 case 'H':
1778                         vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1779                         return;
1780                 case 'Z':
1781                         respond_ID(tty);
1782                         return;
1783                 case '7':
1784                         save_cur(vc);
1785                         return;
1786                 case '8':
1787                         restore_cur(vc);
1788                         return;
1789                 case '(':
1790                         vc->vc_state = ESsetG0;
1791                         return;
1792                 case ')':
1793                         vc->vc_state = ESsetG1;
1794                         return;
1795                 case '#':
1796                         vc->vc_state = EShash;
1797                         return;
1798                 case 'c':
1799                         reset_terminal(vc, 1);
1800                         return;
1801                 case '>':  /* Numeric keypad */
1802                         clr_kbd(vc, kbdapplic);
1803                         return;
1804                 case '=':  /* Appl. keypad */
1805                         set_kbd(vc, kbdapplic);
1806                         return;
1807                 }
1808                 return;
1809         case ESnonstd:
1810                 if (c=='P') {   /* palette escape sequence */
1811                         for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1812                                 vc->vc_par[vc->vc_npar] = 0;
1813                         vc->vc_npar = 0;
1814                         vc->vc_state = ESpalette;
1815                         return;
1816                 } else if (c=='R') {   /* reset palette */
1817                         reset_palette(vc);
1818                         vc->vc_state = ESnormal;
1819                 } else
1820                         vc->vc_state = ESnormal;
1821                 return;
1822         case ESpalette:
1823                 if (isxdigit(c)) {
1824                         vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1825                         if (vc->vc_npar == 7) {
1826                                 int i = vc->vc_par[0] * 3, j = 1;
1827                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1828                                 vc->vc_palette[i++] += vc->vc_par[j++];
1829                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1830                                 vc->vc_palette[i++] += vc->vc_par[j++];
1831                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1832                                 vc->vc_palette[i] += vc->vc_par[j];
1833                                 set_palette(vc);
1834                                 vc->vc_state = ESnormal;
1835                         }
1836                 } else
1837                         vc->vc_state = ESnormal;
1838                 return;
1839         case ESsquare:
1840                 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1841                         vc->vc_par[vc->vc_npar] = 0;
1842                 vc->vc_npar = 0;
1843                 vc->vc_state = ESgetpars;
1844                 if (c == '[') { /* Function key */
1845                         vc->vc_state=ESfunckey;
1846                         return;
1847                 }
1848                 vc->vc_ques = (c == '?');
1849                 if (vc->vc_ques)
1850                         return;
1851         case ESgetpars:
1852                 if (c == ';' && vc->vc_npar < NPAR - 1) {
1853                         vc->vc_npar++;
1854                         return;
1855                 } else if (c>='0' && c<='9') {
1856                         vc->vc_par[vc->vc_npar] *= 10;
1857                         vc->vc_par[vc->vc_npar] += c - '0';
1858                         return;
1859                 } else
1860                         vc->vc_state = ESgotpars;
1861         case ESgotpars:
1862                 vc->vc_state = ESnormal;
1863                 switch(c) {
1864                 case 'h':
1865                         set_mode(vc, 1);
1866                         return;
1867                 case 'l':
1868                         set_mode(vc, 0);
1869                         return;
1870                 case 'c':
1871                         if (vc->vc_ques) {
1872                                 if (vc->vc_par[0])
1873                                         vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1874                                 else
1875                                         vc->vc_cursor_type = cur_default;
1876                                 return;
1877                         }
1878                         break;
1879                 case 'm':
1880                         if (vc->vc_ques) {
1881                                 clear_selection();
1882                                 if (vc->vc_par[0])
1883                                         vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1884                                 else
1885                                         vc->vc_complement_mask = vc->vc_s_complement_mask;
1886                                 return;
1887                         }
1888                         break;
1889                 case 'n':
1890                         if (!vc->vc_ques) {
1891                                 if (vc->vc_par[0] == 5)
1892                                         status_report(tty);
1893                                 else if (vc->vc_par[0] == 6)
1894                                         cursor_report(vc, tty);
1895                         }
1896                         return;
1897                 }
1898                 if (vc->vc_ques) {
1899                         vc->vc_ques = 0;
1900                         return;
1901                 }
1902                 switch(c) {
1903                 case 'G': case '`':
1904                         if (vc->vc_par[0])
1905                                 vc->vc_par[0]--;
1906                         gotoxy(vc, vc->vc_par[0], vc->vc_y);
1907                         return;
1908                 case 'A':
1909                         if (!vc->vc_par[0])
1910                                 vc->vc_par[0]++;
1911                         gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1912                         return;
1913                 case 'B': case 'e':
1914                         if (!vc->vc_par[0])
1915                                 vc->vc_par[0]++;
1916                         gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1917                         return;
1918                 case 'C': case 'a':
1919                         if (!vc->vc_par[0])
1920                                 vc->vc_par[0]++;
1921                         gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1922                         return;
1923                 case 'D':
1924                         if (!vc->vc_par[0])
1925                                 vc->vc_par[0]++;
1926                         gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1927                         return;
1928                 case 'E':
1929                         if (!vc->vc_par[0])
1930                                 vc->vc_par[0]++;
1931                         gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1932                         return;
1933                 case 'F':
1934                         if (!vc->vc_par[0])
1935                                 vc->vc_par[0]++;
1936                         gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1937                         return;
1938                 case 'd':
1939                         if (vc->vc_par[0])
1940                                 vc->vc_par[0]--;
1941                         gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1942                         return;
1943                 case 'H': case 'f':
1944                         if (vc->vc_par[0])
1945                                 vc->vc_par[0]--;
1946                         if (vc->vc_par[1])
1947                                 vc->vc_par[1]--;
1948                         gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1949                         return;
1950                 case 'J':
1951                         csi_J(vc, vc->vc_par[0]);
1952                         return;
1953                 case 'K':
1954                         csi_K(vc, vc->vc_par[0]);
1955                         return;
1956                 case 'L':
1957                         csi_L(vc, vc->vc_par[0]);
1958                         return;
1959                 case 'M':
1960                         csi_M(vc, vc->vc_par[0]);
1961                         return;
1962                 case 'P':
1963                         csi_P(vc, vc->vc_par[0]);
1964                         return;
1965                 case 'c':
1966                         if (!vc->vc_par[0])
1967                                 respond_ID(tty);
1968                         return;
1969                 case 'g':
1970                         if (!vc->vc_par[0])
1971                                 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1972                         else if (vc->vc_par[0] == 3) {
1973                                 vc->vc_tab_stop[0] =
1974                                         vc->vc_tab_stop[1] =
1975                                         vc->vc_tab_stop[2] =
1976                                         vc->vc_tab_stop[3] =
1977                                         vc->vc_tab_stop[4] =
1978                                         vc->vc_tab_stop[5] =
1979                                         vc->vc_tab_stop[6] =
1980                                         vc->vc_tab_stop[7] = 0;
1981                         }
1982                         return;
1983                 case 'm':
1984                         csi_m(vc);
1985                         return;
1986                 case 'q': /* DECLL - but only 3 leds */
1987                         /* map 0,1,2,3 to 0,1,2,4 */
1988                         if (vc->vc_par[0] < 4)
1989                                 setledstate(kbd_table + vc->vc_num,
1990                                             (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1991                         return;
1992                 case 'r':
1993                         if (!vc->vc_par[0])
1994                                 vc->vc_par[0]++;
1995                         if (!vc->vc_par[1])
1996                                 vc->vc_par[1] = vc->vc_rows;
1997                         /* Minimum allowed region is 2 lines */
1998                         if (vc->vc_par[0] < vc->vc_par[1] &&
1999                             vc->vc_par[1] <= vc->vc_rows) {
2000                                 vc->vc_top = vc->vc_par[0] - 1;
2001                                 vc->vc_bottom = vc->vc_par[1];
2002                                 gotoxay(vc, 0, 0);
2003                         }
2004                         return;
2005                 case 's':
2006                         save_cur(vc);
2007                         return;
2008                 case 'u':
2009                         restore_cur(vc);
2010                         return;
2011                 case 'X':
2012                         csi_X(vc, vc->vc_par[0]);
2013                         return;
2014                 case '@':
2015                         csi_at(vc, vc->vc_par[0]);
2016                         return;
2017                 case ']': /* setterm functions */
2018                         setterm_command(vc);
2019                         return;
2020                 }
2021                 return;
2022         case ESpercent:
2023                 vc->vc_state = ESnormal;
2024                 switch (c) {
2025                 case '@':  /* defined in ISO 2022 */
2026                         vc->vc_utf = 0;
2027                         return;
2028                 case 'G':  /* prelim official escape code */
2029                 case '8':  /* retained for compatibility */
2030                         vc->vc_utf = 1;
2031                         return;
2032                 }
2033                 return;
2034         case ESfunckey:
2035                 vc->vc_state = ESnormal;
2036                 return;
2037         case EShash:
2038                 vc->vc_state = ESnormal;
2039                 if (c == '8') {
2040                         /* DEC screen alignment test. kludge :-) */
2041                         vc->vc_video_erase_char =
2042                                 (vc->vc_video_erase_char & 0xff00) | 'E';
2043                         csi_J(vc, 2);
2044                         vc->vc_video_erase_char =
2045                                 (vc->vc_video_erase_char & 0xff00) | ' ';
2046                         do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2047                 }
2048                 return;
2049         case ESsetG0:
2050                 if (c == '0')
2051                         vc->vc_G0_charset = GRAF_MAP;
2052                 else if (c == 'B')
2053                         vc->vc_G0_charset = LAT1_MAP;
2054                 else if (c == 'U')
2055                         vc->vc_G0_charset = IBMPC_MAP;
2056                 else if (c == 'K')
2057                         vc->vc_G0_charset = USER_MAP;
2058                 if (vc->vc_charset == 0)
2059                         vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2060                 vc->vc_state = ESnormal;
2061                 return;
2062         case ESsetG1:
2063                 if (c == '0')
2064                         vc->vc_G1_charset = GRAF_MAP;
2065                 else if (c == 'B')
2066                         vc->vc_G1_charset = LAT1_MAP;
2067                 else if (c == 'U')
2068                         vc->vc_G1_charset = IBMPC_MAP;
2069                 else if (c == 'K')
2070                         vc->vc_G1_charset = USER_MAP;
2071                 if (vc->vc_charset == 1)
2072                         vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2073                 vc->vc_state = ESnormal;
2074                 return;
2075         default:
2076                 vc->vc_state = ESnormal;
2077         }
2078 }
2079
2080 /* is_double_width() is based on the wcwidth() implementation by
2081  * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2082  * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2083  */
2084 struct interval {
2085         uint32_t first;
2086         uint32_t last;
2087 };
2088
2089 static int bisearch(uint32_t ucs, const struct interval *table, int max)
2090 {
2091         int min = 0;
2092         int mid;
2093
2094         if (ucs < table[0].first || ucs > table[max].last)
2095                 return 0;
2096         while (max >= min) {
2097                 mid = (min + max) / 2;
2098                 if (ucs > table[mid].last)
2099                         min = mid + 1;
2100                 else if (ucs < table[mid].first)
2101                         max = mid - 1;
2102                 else
2103                         return 1;
2104         }
2105         return 0;
2106 }
2107
2108 static int is_double_width(uint32_t ucs)
2109 {
2110         static const struct interval double_width[] = {
2111                 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2112                 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2113                 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2114                 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2115         };
2116         return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2117 }
2118
2119 /* acquires console_lock */
2120 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2121 {
2122 #ifdef VT_BUF_VRAM_ONLY
2123 #define FLUSH do { } while(0);
2124 #else
2125 #define FLUSH if (draw_x >= 0) { \
2126         vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2127         draw_x = -1; \
2128         }
2129 #endif
2130
2131         int c, tc, ok, n = 0, draw_x = -1;
2132         unsigned int currcons;
2133         unsigned long draw_from = 0, draw_to = 0;
2134         struct vc_data *vc;
2135         unsigned char vc_attr;
2136         struct vt_notifier_param param;
2137         uint8_t rescan;
2138         uint8_t inverse;
2139         uint8_t width;
2140         u16 himask, charmask;
2141
2142         if (in_interrupt())
2143                 return count;
2144
2145         might_sleep();
2146
2147         console_lock();
2148         vc = tty->driver_data;
2149         if (vc == NULL) {
2150                 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2151                 console_unlock();
2152                 return 0;
2153         }
2154
2155         currcons = vc->vc_num;
2156         if (!vc_cons_allocated(currcons)) {
2157                 /* could this happen? */
2158                 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2159                 console_unlock();
2160                 return 0;
2161         }
2162
2163         himask = vc->vc_hi_font_mask;
2164         charmask = himask ? 0x1ff : 0xff;
2165
2166         /* undraw cursor first */
2167         if (IS_FG(vc))
2168                 hide_cursor(vc);
2169
2170         param.vc = vc;
2171
2172         while (!tty->stopped && count) {
2173                 int orig = *buf;
2174                 c = orig;
2175                 buf++;
2176                 n++;
2177                 count--;
2178                 rescan = 0;
2179                 inverse = 0;
2180                 width = 1;
2181
2182                 /* Do no translation at all in control states */
2183                 if (vc->vc_state != ESnormal) {
2184                         tc = c;
2185                 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2186                     /* Combine UTF-8 into Unicode in vc_utf_char.
2187                      * vc_utf_count is the number of continuation bytes still
2188                      * expected to arrive.
2189                      * vc_npar is the number of continuation bytes arrived so
2190                      * far
2191                      */
2192 rescan_last_byte:
2193                     if ((c & 0xc0) == 0x80) {
2194                         /* Continuation byte received */
2195                         static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2196                         if (vc->vc_utf_count) {
2197                             vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2198                             vc->vc_npar++;
2199                             if (--vc->vc_utf_count) {
2200                                 /* Still need some bytes */
2201                                 continue;
2202                             }
2203                             /* Got a whole character */
2204                             c = vc->vc_utf_char;
2205                             /* Reject overlong sequences */
2206                             if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2207                                         c > utf8_length_changes[vc->vc_npar])
2208                                 c = 0xfffd;
2209                         } else {
2210                             /* Unexpected continuation byte */
2211                             vc->vc_utf_count = 0;
2212                             c = 0xfffd;
2213                         }
2214                     } else {
2215                         /* Single ASCII byte or first byte of a sequence received */
2216                         if (vc->vc_utf_count) {
2217                             /* Continuation byte expected */
2218                             rescan = 1;
2219                             vc->vc_utf_count = 0;
2220                             c = 0xfffd;
2221                         } else if (c > 0x7f) {
2222                             /* First byte of a multibyte sequence received */
2223                             vc->vc_npar = 0;
2224                             if ((c & 0xe0) == 0xc0) {
2225                                 vc->vc_utf_count = 1;
2226                                 vc->vc_utf_char = (c & 0x1f);
2227                             } else if ((c & 0xf0) == 0xe0) {
2228                                 vc->vc_utf_count = 2;
2229                                 vc->vc_utf_char = (c & 0x0f);
2230                             } else if ((c & 0xf8) == 0xf0) {
2231                                 vc->vc_utf_count = 3;
2232                                 vc->vc_utf_char = (c & 0x07);
2233                             } else if ((c & 0xfc) == 0xf8) {
2234                                 vc->vc_utf_count = 4;
2235                                 vc->vc_utf_char = (c & 0x03);
2236                             } else if ((c & 0xfe) == 0xfc) {
2237                                 vc->vc_utf_count = 5;
2238                                 vc->vc_utf_char = (c & 0x01);
2239                             } else {
2240                                 /* 254 and 255 are invalid */
2241                                 c = 0xfffd;
2242                             }
2243                             if (vc->vc_utf_count) {
2244                                 /* Still need some bytes */
2245                                 continue;
2246                             }
2247                         }
2248                         /* Nothing to do if an ASCII byte was received */
2249                     }
2250                     /* End of UTF-8 decoding. */
2251                     /* c is the received character, or U+FFFD for invalid sequences. */
2252                     /* Replace invalid Unicode code points with U+FFFD too */
2253                     if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2254                         c = 0xfffd;
2255                     tc = c;
2256                 } else {        /* no utf or alternate charset mode */
2257                     tc = vc_translate(vc, c);
2258                 }
2259
2260                 param.c = tc;
2261                 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2262                                         &param) == NOTIFY_STOP)
2263                         continue;
2264
2265                 /* If the original code was a control character we
2266                  * only allow a glyph to be displayed if the code is
2267                  * not normally used (such as for cursor movement) or
2268                  * if the disp_ctrl mode has been explicitly enabled.
2269                  * Certain characters (as given by the CTRL_ALWAYS
2270                  * bitmap) are always displayed as control characters,
2271                  * as the console would be pretty useless without
2272                  * them; to display an arbitrary font position use the
2273                  * direct-to-font zone in UTF-8 mode.
2274                  */
2275                 ok = tc && (c >= 32 ||
2276                             !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2277                                   vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2278                         && (c != 127 || vc->vc_disp_ctrl)
2279                         && (c != 128+27);
2280
2281                 if (vc->vc_state == ESnormal && ok) {
2282                         if (vc->vc_utf && !vc->vc_disp_ctrl) {
2283                                 if (is_double_width(c))
2284                                         width = 2;
2285                         }
2286                         /* Now try to find out how to display it */
2287                         tc = conv_uni_to_pc(vc, tc);
2288                         if (tc & ~charmask) {
2289                                 if (tc == -1 || tc == -2) {
2290                                     continue; /* nothing to display */
2291                                 }
2292                                 /* Glyph not found */
2293                                 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2294                                     /* In legacy mode use the glyph we get by a 1:1 mapping.
2295                                        This would make absolutely no sense with Unicode in mind,
2296                                        but do this for ASCII characters since a font may lack
2297                                        Unicode mapping info and we don't want to end up with
2298                                        having question marks only. */
2299                                     tc = c;
2300                                 } else {
2301                                     /* Display U+FFFD. If it's not found, display an inverse question mark. */
2302                                     tc = conv_uni_to_pc(vc, 0xfffd);
2303                                     if (tc < 0) {
2304                                         inverse = 1;
2305                                         tc = conv_uni_to_pc(vc, '?');
2306                                         if (tc < 0) tc = '?';
2307                                     }
2308                                 }
2309                         }
2310
2311                         if (!inverse) {
2312                                 vc_attr = vc->vc_attr;
2313                         } else {
2314                                 /* invert vc_attr */
2315                                 if (!vc->vc_can_do_color) {
2316                                         vc_attr = (vc->vc_attr) ^ 0x08;
2317                                 } else if (vc->vc_hi_font_mask == 0x100) {
2318                                         vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2319                                 } else {
2320                                         vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2321                                 }
2322                                 FLUSH
2323                         }
2324
2325                         while (1) {
2326                                 if (vc->vc_need_wrap || vc->vc_decim)
2327                                         FLUSH
2328                                 if (vc->vc_need_wrap) {
2329                                         cr(vc);
2330                                         lf(vc);
2331                                 }
2332                                 if (vc->vc_decim)
2333                                         insert_char(vc, 1);
2334                                 scr_writew(himask ?
2335                                              ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2336                                              (vc_attr << 8) + tc,
2337                                            (u16 *) vc->vc_pos);
2338                                 if (DO_UPDATE(vc) && draw_x < 0) {
2339                                         draw_x = vc->vc_x;
2340                                         draw_from = vc->vc_pos;
2341                                 }
2342                                 if (vc->vc_x == vc->vc_cols - 1) {
2343                                         vc->vc_need_wrap = vc->vc_decawm;
2344                                         draw_to = vc->vc_pos + 2;
2345                                 } else {
2346                                         vc->vc_x++;
2347                                         draw_to = (vc->vc_pos += 2);
2348                                 }
2349
2350                                 if (!--width) break;
2351
2352                                 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2353                                 if (tc < 0) tc = ' ';
2354                         }
2355                         notify_write(vc, c);
2356
2357                         if (inverse) {
2358                                 FLUSH
2359                         }
2360
2361                         if (rescan) {
2362                                 rescan = 0;
2363                                 inverse = 0;
2364                                 width = 1;
2365                                 c = orig;
2366                                 goto rescan_last_byte;
2367                         }
2368                         continue;
2369                 }
2370                 FLUSH
2371                 do_con_trol(tty, vc, orig);
2372         }
2373         FLUSH
2374         console_conditional_schedule();
2375         console_unlock();
2376         notify_update(vc);
2377         return n;
2378 #undef FLUSH
2379 }
2380
2381 /*
2382  * This is the console switching callback.
2383  *
2384  * Doing console switching in a process context allows
2385  * us to do the switches asynchronously (needed when we want
2386  * to switch due to a keyboard interrupt).  Synchronization
2387  * with other console code and prevention of re-entrancy is
2388  * ensured with console_lock.
2389  */
2390 static void console_callback(struct work_struct *ignored)
2391 {
2392         console_lock();
2393
2394         if (want_console >= 0) {
2395                 if (want_console != fg_console &&
2396                     vc_cons_allocated(want_console)) {
2397                         hide_cursor(vc_cons[fg_console].d);
2398                         change_console(vc_cons[want_console].d);
2399                         /* we only changed when the console had already
2400                            been allocated - a new console is not created
2401                            in an interrupt routine */
2402                 }
2403                 want_console = -1;
2404         }
2405         if (do_poke_blanked_console) { /* do not unblank for a LED change */
2406                 do_poke_blanked_console = 0;
2407                 poke_blanked_console();
2408         }
2409         if (scrollback_delta) {
2410                 struct vc_data *vc = vc_cons[fg_console].d;
2411                 clear_selection();
2412                 if (vc->vc_mode == KD_TEXT)
2413                         vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2414                 scrollback_delta = 0;
2415         }
2416         if (blank_timer_expired) {
2417                 do_blank_screen(0);
2418                 blank_timer_expired = 0;
2419         }
2420         notify_update(vc_cons[fg_console].d);
2421
2422         console_unlock();
2423 }
2424
2425 int set_console(int nr)
2426 {
2427         struct vc_data *vc = vc_cons[fg_console].d;
2428
2429         if (!vc_cons_allocated(nr) || vt_dont_switch ||
2430                 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2431
2432                 /*
2433                  * Console switch will fail in console_callback() or
2434                  * change_console() so there is no point scheduling
2435                  * the callback
2436                  *
2437                  * Existing set_console() users don't check the return
2438                  * value so this shouldn't break anything
2439                  */
2440                 return -EINVAL;
2441         }
2442
2443         want_console = nr;
2444         schedule_console_callback();
2445
2446         return 0;
2447 }
2448
2449 struct tty_driver *console_driver;
2450
2451 #ifdef CONFIG_VT_CONSOLE
2452
2453 /**
2454  * vt_kmsg_redirect() - Sets/gets the kernel message console
2455  * @new:        The new virtual terminal number or -1 if the console should stay
2456  *              unchanged
2457  *
2458  * By default, the kernel messages are always printed on the current virtual
2459  * console. However, the user may modify that default with the
2460  * TIOCL_SETKMSGREDIRECT ioctl call.
2461  *
2462  * This function sets the kernel message console to be @new. It returns the old
2463  * virtual console number. The virtual terminal number 0 (both as parameter and
2464  * return value) means no redirection (i.e. always printed on the currently
2465  * active console).
2466  *
2467  * The parameter -1 means that only the current console is returned, but the
2468  * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2469  * case to make the code more understandable.
2470  *
2471  * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2472  * the parameter and always returns 0.
2473  */
2474 int vt_kmsg_redirect(int new)
2475 {
2476         static int kmsg_con;
2477
2478         if (new != -1)
2479                 return xchg(&kmsg_con, new);
2480         else
2481                 return kmsg_con;
2482 }
2483
2484 /*
2485  *      Console on virtual terminal
2486  *
2487  * The console must be locked when we get here.
2488  */
2489
2490 static void vt_console_print(struct console *co, const char *b, unsigned count)
2491 {
2492         struct vc_data *vc = vc_cons[fg_console].d;
2493         unsigned char c;
2494         static DEFINE_SPINLOCK(printing_lock);
2495         const ushort *start;
2496         ushort cnt = 0;
2497         ushort myx;
2498         int kmsg_console;
2499
2500         /* console busy or not yet initialized */
2501         if (!printable)
2502                 return;
2503         if (!spin_trylock(&printing_lock))
2504                 return;
2505
2506         kmsg_console = vt_get_kmsg_redirect();
2507         if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2508                 vc = vc_cons[kmsg_console - 1].d;
2509
2510         /* read `x' only after setting currcons properly (otherwise
2511            the `x' macro will read the x of the foreground console). */
2512         myx = vc->vc_x;
2513
2514         if (!vc_cons_allocated(fg_console)) {
2515                 /* impossible */
2516                 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2517                 goto quit;
2518         }
2519
2520         if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2521                 goto quit;
2522
2523         /* undraw cursor first */
2524         if (IS_FG(vc))
2525                 hide_cursor(vc);
2526
2527         start = (ushort *)vc->vc_pos;
2528
2529         /* Contrived structure to try to emulate original need_wrap behaviour
2530          * Problems caused when we have need_wrap set on '\n' character */
2531         while (count--) {
2532                 c = *b++;
2533                 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2534                         if (cnt > 0) {
2535                                 if (CON_IS_VISIBLE(vc))
2536                                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2537                                 vc->vc_x += cnt;
2538                                 if (vc->vc_need_wrap)
2539                                         vc->vc_x--;
2540                                 cnt = 0;
2541                         }
2542                         if (c == 8) {           /* backspace */
2543                                 bs(vc);
2544                                 start = (ushort *)vc->vc_pos;
2545                                 myx = vc->vc_x;
2546                                 continue;
2547                         }
2548                         if (c != 13)
2549                                 lf(vc);
2550                         cr(vc);
2551                         start = (ushort *)vc->vc_pos;
2552                         myx = vc->vc_x;
2553                         if (c == 10 || c == 13)
2554                                 continue;
2555                 }
2556                 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2557                 notify_write(vc, c);
2558                 cnt++;
2559                 if (myx == vc->vc_cols - 1) {
2560                         vc->vc_need_wrap = 1;
2561                         continue;
2562                 }
2563                 vc->vc_pos += 2;
2564                 myx++;
2565         }
2566         if (cnt > 0) {
2567                 if (CON_IS_VISIBLE(vc))
2568                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2569                 vc->vc_x += cnt;
2570                 if (vc->vc_x == vc->vc_cols) {
2571                         vc->vc_x--;
2572                         vc->vc_need_wrap = 1;
2573                 }
2574         }
2575         set_cursor(vc);
2576         notify_update(vc);
2577
2578 quit:
2579         spin_unlock(&printing_lock);
2580 }
2581
2582 static struct tty_driver *vt_console_device(struct console *c, int *index)
2583 {
2584         *index = c->index ? c->index-1 : fg_console;
2585         return console_driver;
2586 }
2587
2588 static struct console vt_console_driver = {
2589         .name           = "tty",
2590         .write          = vt_console_print,
2591         .device         = vt_console_device,
2592         .unblank        = unblank_screen,
2593         .flags          = CON_PRINTBUFFER,
2594         .index          = -1,
2595 };
2596 #endif
2597
2598 /*
2599  *      Handling of Linux-specific VC ioctls
2600  */
2601
2602 /*
2603  * Generally a bit racy with respect to console_lock();.
2604  *
2605  * There are some functions which don't need it.
2606  *
2607  * There are some functions which can sleep for arbitrary periods
2608  * (paste_selection) but we don't need the lock there anyway.
2609  *
2610  * set_selection has locking, and definitely needs it
2611  */
2612
2613 int tioclinux(struct tty_struct *tty, unsigned long arg)
2614 {
2615         char type, data;
2616         char __user *p = (char __user *)arg;
2617         int lines;
2618         int ret;
2619
2620         if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2621                 return -EPERM;
2622         if (get_user(type, p))
2623                 return -EFAULT;
2624         ret = 0;
2625
2626         switch (type)
2627         {
2628                 case TIOCL_SETSEL:
2629                         console_lock();
2630                         ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2631                         console_unlock();
2632                         break;
2633                 case TIOCL_PASTESEL:
2634                         ret = paste_selection(tty);
2635                         break;
2636                 case TIOCL_UNBLANKSCREEN:
2637                         console_lock();
2638                         unblank_screen();
2639                         console_unlock();
2640                         break;
2641                 case TIOCL_SELLOADLUT:
2642                         ret = sel_loadlut(p);
2643                         break;
2644                 case TIOCL_GETSHIFTSTATE:
2645
2646         /*
2647          * Make it possible to react to Shift+Mousebutton.
2648          * Note that 'shift_state' is an undocumented
2649          * kernel-internal variable; programs not closely
2650          * related to the kernel should not use this.
2651          */
2652                         data = shift_state;
2653                         ret = __put_user(data, p);
2654                         break;
2655                 case TIOCL_GETMOUSEREPORTING:
2656                         data = mouse_reporting();
2657                         ret = __put_user(data, p);
2658                         break;
2659                 case TIOCL_SETVESABLANK:
2660                         ret = set_vesa_blanking(p);
2661                         break;
2662                 case TIOCL_GETKMSGREDIRECT:
2663                         data = vt_get_kmsg_redirect();
2664                         ret = __put_user(data, p);
2665                         break;
2666                 case TIOCL_SETKMSGREDIRECT:
2667                         if (!capable(CAP_SYS_ADMIN)) {
2668                                 ret = -EPERM;
2669                         } else {
2670                                 if (get_user(data, p+1))
2671                                         ret = -EFAULT;
2672                                 else
2673                                         vt_kmsg_redirect(data);
2674                         }
2675                         break;
2676                 case TIOCL_GETFGCONSOLE:
2677                         ret = fg_console;
2678                         break;
2679                 case TIOCL_SCROLLCONSOLE:
2680                         if (get_user(lines, (s32 __user *)(p+4))) {
2681                                 ret = -EFAULT;
2682                         } else {
2683                                 scrollfront(vc_cons[fg_console].d, lines);
2684                                 ret = 0;
2685                         }
2686                         break;
2687                 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2688                         console_lock();
2689                         ignore_poke = 1;
2690                         do_blank_screen(0);
2691                         console_unlock();
2692                         break;
2693                 case TIOCL_BLANKEDSCREEN:
2694                         ret = console_blanked;
2695                         break;
2696                 default:
2697                         ret = -EINVAL;
2698                         break;
2699         }
2700         return ret;
2701 }
2702
2703 /*
2704  * /dev/ttyN handling
2705  */
2706
2707 static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2708 {
2709         int     retval;
2710
2711         retval = do_con_write(tty, buf, count);
2712         con_flush_chars(tty);
2713
2714         return retval;
2715 }
2716
2717 static int con_put_char(struct tty_struct *tty, unsigned char ch)
2718 {
2719         if (in_interrupt())
2720                 return 0;       /* n_r3964 calls put_char() from interrupt context */
2721         return do_con_write(tty, &ch, 1);
2722 }
2723
2724 static int con_write_room(struct tty_struct *tty)
2725 {
2726         if (tty->stopped)
2727                 return 0;
2728         return 32768;           /* No limit, really; we're not buffering */
2729 }
2730
2731 static int con_chars_in_buffer(struct tty_struct *tty)
2732 {
2733         return 0;               /* we're not buffering */
2734 }
2735
2736 /*
2737  * con_throttle and con_unthrottle are only used for
2738  * paste_selection(), which has to stuff in a large number of
2739  * characters...
2740  */
2741 static void con_throttle(struct tty_struct *tty)
2742 {
2743 }
2744
2745 static void con_unthrottle(struct tty_struct *tty)
2746 {
2747         struct vc_data *vc = tty->driver_data;
2748
2749         wake_up_interruptible(&vc->paste_wait);
2750 }
2751
2752 /*
2753  * Turn the Scroll-Lock LED on when the tty is stopped
2754  */
2755 static void con_stop(struct tty_struct *tty)
2756 {
2757         int console_num;
2758         if (!tty)
2759                 return;
2760         console_num = tty->index;
2761         if (!vc_cons_allocated(console_num))
2762                 return;
2763         set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2764         set_leds();
2765 }
2766
2767 /*
2768  * Turn the Scroll-Lock LED off when the console is started
2769  */
2770 static void con_start(struct tty_struct *tty)
2771 {
2772         int console_num;
2773         if (!tty)
2774                 return;
2775         console_num = tty->index;
2776         if (!vc_cons_allocated(console_num))
2777                 return;
2778         clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2779         set_leds();
2780 }
2781
2782 static void con_flush_chars(struct tty_struct *tty)
2783 {
2784         struct vc_data *vc;
2785
2786         if (in_interrupt())     /* from flush_to_ldisc */
2787                 return;
2788
2789         /* if we race with con_close(), vt may be null */
2790         console_lock();
2791         vc = tty->driver_data;
2792         if (vc)
2793                 set_cursor(vc);
2794         console_unlock();
2795 }
2796
2797 /*
2798  * Allocate the console screen memory.
2799  */
2800 static int con_open(struct tty_struct *tty, struct file *filp)
2801 {
2802         unsigned int currcons = tty->index;
2803         int ret = 0;
2804
2805         console_lock();
2806         if (tty->driver_data == NULL) {
2807                 ret = vc_allocate(currcons);
2808                 if (ret == 0) {
2809                         struct vc_data *vc = vc_cons[currcons].d;
2810
2811                         /* Still being freed */
2812                         if (vc->port.tty) {
2813                                 console_unlock();
2814                                 return -ERESTARTSYS;
2815                         }
2816                         tty->driver_data = vc;
2817                         vc->port.tty = tty;
2818
2819                         if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2820                                 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2821                                 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2822                         }
2823                         if (vc->vc_utf)
2824                                 tty->termios->c_iflag |= IUTF8;
2825                         else
2826                                 tty->termios->c_iflag &= ~IUTF8;
2827                         console_unlock();
2828                         return ret;
2829                 }
2830         }
2831         console_unlock();
2832         return ret;
2833 }
2834
2835 static void con_close(struct tty_struct *tty, struct file *filp)
2836 {
2837         /* Nothing to do - we defer to shutdown */
2838 }
2839
2840 static void con_shutdown(struct tty_struct *tty)
2841 {
2842         struct vc_data *vc = tty->driver_data;
2843         BUG_ON(vc == NULL);
2844         console_lock();
2845         vc->port.tty = NULL;
2846         console_unlock();
2847         tty_shutdown(tty);
2848 }
2849
2850 static int default_italic_color    = 2; // green (ASCII)
2851 static int default_underline_color = 3; // cyan (ASCII)
2852 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2853 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2854
2855 static void vc_init(struct vc_data *vc, unsigned int rows,
2856                     unsigned int cols, int do_clear)
2857 {
2858         int j, k ;
2859
2860         vc->vc_cols = cols;
2861         vc->vc_rows = rows;
2862         vc->vc_size_row = cols << 1;
2863         vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2864
2865         set_origin(vc);
2866         vc->vc_pos = vc->vc_origin;
2867         reset_vc(vc);
2868         for (j=k=0; j<16; j++) {
2869                 vc->vc_palette[k++] = default_red[j] ;
2870                 vc->vc_palette[k++] = default_grn[j] ;
2871                 vc->vc_palette[k++] = default_blu[j] ;
2872         }
2873         vc->vc_def_color       = 0x07;   /* white */
2874         vc->vc_ulcolor         = default_underline_color;
2875         vc->vc_itcolor         = default_italic_color;
2876         vc->vc_halfcolor       = 0x08;   /* grey */
2877         init_waitqueue_head(&vc->paste_wait);
2878         reset_terminal(vc, do_clear);
2879 }
2880
2881 /*
2882  * This routine initializes console interrupts, and does nothing
2883  * else. If you want the screen to clear, call tty_write with
2884  * the appropriate escape-sequence.
2885  */
2886
2887 static int __init con_init(void)
2888 {
2889         const char *display_desc = NULL;
2890         struct vc_data *vc;
2891         unsigned int currcons = 0, i;
2892
2893         console_lock();
2894
2895         if (conswitchp)
2896                 display_desc = conswitchp->con_startup();
2897         if (!display_desc) {
2898                 fg_console = 0;
2899                 console_unlock();
2900                 return 0;
2901         }
2902
2903         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2904                 struct con_driver *con_driver = &registered_con_driver[i];
2905
2906                 if (con_driver->con == NULL) {
2907                         con_driver->con = conswitchp;
2908                         con_driver->desc = display_desc;
2909                         con_driver->flag = CON_DRIVER_FLAG_INIT;
2910                         con_driver->first = 0;
2911                         con_driver->last = MAX_NR_CONSOLES - 1;
2912                         break;
2913                 }
2914         }
2915
2916         for (i = 0; i < MAX_NR_CONSOLES; i++)
2917                 con_driver_map[i] = conswitchp;
2918
2919         if (blankinterval) {
2920                 blank_state = blank_normal_wait;
2921                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
2922         }
2923
2924         for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2925                 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2926                 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2927                 tty_port_init(&vc->port);
2928                 visual_init(vc, currcons, 1);
2929                 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2930                 vc_init(vc, vc->vc_rows, vc->vc_cols,
2931                         currcons || !vc->vc_sw->con_save_screen);
2932         }
2933         currcons = fg_console = 0;
2934         master_display_fg = vc = vc_cons[currcons].d;
2935         set_origin(vc);
2936         save_screen(vc);
2937         gotoxy(vc, vc->vc_x, vc->vc_y);
2938         csi_J(vc, 0);
2939         update_screen(vc);
2940         pr_info("Console: %s %s %dx%d",
2941                 vc->vc_can_do_color ? "colour" : "mono",
2942                 display_desc, vc->vc_cols, vc->vc_rows);
2943         printable = 1;
2944         printk("\n");
2945
2946         console_unlock();
2947
2948 #ifdef CONFIG_VT_CONSOLE
2949         register_console(&vt_console_driver);
2950 #endif
2951         return 0;
2952 }
2953 console_initcall(con_init);
2954
2955 static const struct tty_operations con_ops = {
2956         .open = con_open,
2957         .close = con_close,
2958         .write = con_write,
2959         .write_room = con_write_room,
2960         .put_char = con_put_char,
2961         .flush_chars = con_flush_chars,
2962         .chars_in_buffer = con_chars_in_buffer,
2963         .ioctl = vt_ioctl,
2964 #ifdef CONFIG_COMPAT
2965         .compat_ioctl = vt_compat_ioctl,
2966 #endif
2967         .stop = con_stop,
2968         .start = con_start,
2969         .throttle = con_throttle,
2970         .unthrottle = con_unthrottle,
2971         .resize = vt_resize,
2972         .shutdown = con_shutdown
2973 };
2974
2975 static struct cdev vc0_cdev;
2976
2977 static ssize_t show_tty_active(struct device *dev,
2978                                 struct device_attribute *attr, char *buf)
2979 {
2980         return sprintf(buf, "tty%d\n", fg_console + 1);
2981 }
2982 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2983
2984 int __init vty_init(const struct file_operations *console_fops)
2985 {
2986         cdev_init(&vc0_cdev, console_fops);
2987         if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2988             register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2989                 panic("Couldn't register /dev/tty0 driver\n");
2990         tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2991         if (IS_ERR(tty0dev))
2992                 tty0dev = NULL;
2993         else
2994                 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
2995
2996         vcs_init();
2997
2998         console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2999         if (!console_driver)
3000                 panic("Couldn't allocate console driver\n");
3001         console_driver->owner = THIS_MODULE;
3002         console_driver->name = "tty";
3003         console_driver->name_base = 1;
3004         console_driver->major = TTY_MAJOR;
3005         console_driver->minor_start = 1;
3006         console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3007         console_driver->init_termios = tty_std_termios;
3008         if (default_utf8)
3009                 console_driver->init_termios.c_iflag |= IUTF8;
3010         console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3011         tty_set_operations(console_driver, &con_ops);
3012         if (tty_register_driver(console_driver))
3013                 panic("Couldn't register console driver\n");
3014         kbd_init();
3015         console_map_init();
3016 #ifdef CONFIG_MDA_CONSOLE
3017         mda_console_init();
3018 #endif
3019         return 0;
3020 }
3021
3022 #ifndef VT_SINGLE_DRIVER
3023
3024 static struct class *vtconsole_class;
3025
3026 static int do_bind_con_driver(const struct consw *csw, int first, int last,
3027                            int deflt)
3028 {
3029         struct module *owner = csw->owner;
3030         const char *desc = NULL;
3031         struct con_driver *con_driver;
3032         int i, j = -1, k = -1, retval = -ENODEV;
3033
3034         if (!try_module_get(owner))
3035                 return -ENODEV;
3036
3037         WARN_CONSOLE_UNLOCKED();
3038
3039         /* check if