9dc1e2be9d13cb9ec99f0daac6b7295401c3f707
[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         old_rows = vc->vc_rows;
892         old_row_size = vc->vc_size_row;
893
894         err = resize_screen(vc, new_cols, new_rows, user);
895         if (err) {
896                 kfree(newscreen);
897                 return err;
898         }
899
900         vc->vc_rows = new_rows;
901         vc->vc_cols = new_cols;
902         vc->vc_size_row = new_row_size;
903         vc->vc_screenbuf_size = new_screen_size;
904
905         rlth = min(old_row_size, new_row_size);
906         rrem = new_row_size - rlth;
907         old_origin = vc->vc_origin;
908         new_origin = (long) newscreen;
909         new_scr_end = new_origin + new_screen_size;
910
911         if (vc->vc_y > new_rows) {
912                 if (old_rows - vc->vc_y < new_rows) {
913                         /*
914                          * Cursor near the bottom, copy contents from the
915                          * bottom of buffer
916                          */
917                         old_origin += (old_rows - new_rows) * old_row_size;
918                 } else {
919                         /*
920                          * Cursor is in no man's land, copy 1/2 screenful
921                          * from the top and bottom of cursor position
922                          */
923                         old_origin += (vc->vc_y - new_rows/2) * old_row_size;
924                 }
925         }
926
927         end = old_origin + old_row_size * min(old_rows, new_rows);
928
929         update_attr(vc);
930
931         while (old_origin < end) {
932                 scr_memcpyw((unsigned short *) new_origin,
933                             (unsigned short *) old_origin, rlth);
934                 if (rrem)
935                         scr_memsetw((void *)(new_origin + rlth),
936                                     vc->vc_video_erase_char, rrem);
937                 old_origin += old_row_size;
938                 new_origin += new_row_size;
939         }
940         if (new_scr_end > new_origin)
941                 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
942                             new_scr_end - new_origin);
943         kfree(vc->vc_screenbuf);
944         vc->vc_screenbuf = newscreen;
945         vc->vc_screenbuf_size = new_screen_size;
946         set_origin(vc);
947
948         /* do part of a reset_terminal() */
949         vc->vc_top = 0;
950         vc->vc_bottom = vc->vc_rows;
951         gotoxy(vc, vc->vc_x, vc->vc_y);
952         save_cur(vc);
953
954         if (tty) {
955                 /* Rewrite the requested winsize data with the actual
956                    resulting sizes */
957                 struct winsize ws;
958                 memset(&ws, 0, sizeof(ws));
959                 ws.ws_row = vc->vc_rows;
960                 ws.ws_col = vc->vc_cols;
961                 ws.ws_ypixel = vc->vc_scan_lines;
962                 tty_do_resize(tty, &ws);
963         }
964
965         if (CON_IS_VISIBLE(vc))
966                 update_screen(vc);
967         vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
968         return err;
969 }
970
971 /**
972  *      vc_resize               -       resize a VT
973  *      @vc: virtual console
974  *      @cols: columns
975  *      @rows: rows
976  *
977  *      Resize a virtual console as seen from the console end of things. We
978  *      use the common vc_do_resize methods to update the structures. The
979  *      caller must hold the console sem to protect console internals and
980  *      vc->port.tty
981  */
982
983 int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
984 {
985         return vc_do_resize(vc->port.tty, vc, cols, rows);
986 }
987
988 /**
989  *      vt_resize               -       resize a VT
990  *      @tty: tty to resize
991  *      @ws: winsize attributes
992  *
993  *      Resize a virtual terminal. This is called by the tty layer as we
994  *      register our own handler for resizing. The mutual helper does all
995  *      the actual work.
996  *
997  *      Takes the console sem and the called methods then take the tty
998  *      termios_mutex and the tty ctrl_lock in that order.
999  */
1000 static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1001 {
1002         struct vc_data *vc = tty->driver_data;
1003         int ret;
1004
1005         console_lock();
1006         ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1007         console_unlock();
1008         return ret;
1009 }
1010
1011 void vc_deallocate(unsigned int currcons)
1012 {
1013         WARN_CONSOLE_UNLOCKED();
1014
1015         if (vc_cons_allocated(currcons)) {
1016                 struct vc_data *vc = vc_cons[currcons].d;
1017                 struct vt_notifier_param param = { .vc = vc };
1018
1019                 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1020                 vcs_remove_sysfs(currcons);
1021                 vc->vc_sw->con_deinit(vc);
1022                 put_pid(vc->vt_pid);
1023                 module_put(vc->vc_sw->owner);
1024                 kfree(vc->vc_screenbuf);
1025                 if (currcons >= MIN_NR_CONSOLES)
1026                         kfree(vc);
1027                 vc_cons[currcons].d = NULL;
1028         }
1029 }
1030
1031 /*
1032  *      VT102 emulator
1033  */
1034
1035 #define set_kbd(vc, x)  set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1036 #define clr_kbd(vc, x)  clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1037 #define is_kbd(vc, x)   vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1038
1039 #define decarm          VC_REPEAT
1040 #define decckm          VC_CKMODE
1041 #define kbdapplic       VC_APPLIC
1042 #define lnm             VC_CRLF
1043
1044 /*
1045  * this is what the terminal answers to a ESC-Z or csi0c query.
1046  */
1047 #define VT100ID "\033[?1;2c"
1048 #define VT102ID "\033[?6c"
1049
1050 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1051                                        8,12,10,14, 9,13,11,15 };
1052
1053 /* the default colour table, for VGA+ colour systems */
1054 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1055     0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1056 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1057     0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1058 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1059     0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1060
1061 module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1062 module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1063 module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1064
1065 /*
1066  * gotoxy() must verify all boundaries, because the arguments
1067  * might also be negative. If the given position is out of
1068  * bounds, the cursor is placed at the nearest margin.
1069  */
1070 static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1071 {
1072         int min_y, max_y;
1073
1074         if (new_x < 0)
1075                 vc->vc_x = 0;
1076         else {
1077                 if (new_x >= vc->vc_cols)
1078                         vc->vc_x = vc->vc_cols - 1;
1079                 else
1080                         vc->vc_x = new_x;
1081         }
1082
1083         if (vc->vc_decom) {
1084                 min_y = vc->vc_top;
1085                 max_y = vc->vc_bottom;
1086         } else {
1087                 min_y = 0;
1088                 max_y = vc->vc_rows;
1089         }
1090         if (new_y < min_y)
1091                 vc->vc_y = min_y;
1092         else if (new_y >= max_y)
1093                 vc->vc_y = max_y - 1;
1094         else
1095                 vc->vc_y = new_y;
1096         vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1097         vc->vc_need_wrap = 0;
1098 }
1099
1100 /* for absolute user moves, when decom is set */
1101 static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1102 {
1103         gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1104 }
1105
1106 void scrollback(struct vc_data *vc, int lines)
1107 {
1108         if (!lines)
1109                 lines = vc->vc_rows / 2;
1110         scrolldelta(-lines);
1111 }
1112
1113 void scrollfront(struct vc_data *vc, int lines)
1114 {
1115         if (!lines)
1116                 lines = vc->vc_rows / 2;
1117         scrolldelta(lines);
1118 }
1119
1120 static void lf(struct vc_data *vc)
1121 {
1122         /* don't scroll if above bottom of scrolling region, or
1123          * if below scrolling region
1124          */
1125         if (vc->vc_y + 1 == vc->vc_bottom)
1126                 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1127         else if (vc->vc_y < vc->vc_rows - 1) {
1128                 vc->vc_y++;
1129                 vc->vc_pos += vc->vc_size_row;
1130         }
1131         vc->vc_need_wrap = 0;
1132         notify_write(vc, '\n');
1133 }
1134
1135 static void ri(struct vc_data *vc)
1136 {
1137         /* don't scroll if below top of scrolling region, or
1138          * if above scrolling region
1139          */
1140         if (vc->vc_y == vc->vc_top)
1141                 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1142         else if (vc->vc_y > 0) {
1143                 vc->vc_y--;
1144                 vc->vc_pos -= vc->vc_size_row;
1145         }
1146         vc->vc_need_wrap = 0;
1147 }
1148
1149 static inline void cr(struct vc_data *vc)
1150 {
1151         vc->vc_pos -= vc->vc_x << 1;
1152         vc->vc_need_wrap = vc->vc_x = 0;
1153         notify_write(vc, '\r');
1154 }
1155
1156 static inline void bs(struct vc_data *vc)
1157 {
1158         if (vc->vc_x) {
1159                 vc->vc_pos -= 2;
1160                 vc->vc_x--;
1161                 vc->vc_need_wrap = 0;
1162                 notify_write(vc, '\b');
1163         }
1164 }
1165
1166 static inline void del(struct vc_data *vc)
1167 {
1168         /* ignored */
1169 }
1170
1171 static void csi_J(struct vc_data *vc, int vpar)
1172 {
1173         unsigned int count;
1174         unsigned short * start;
1175
1176         switch (vpar) {
1177                 case 0: /* erase from cursor to end of display */
1178                         count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1179                         start = (unsigned short *)vc->vc_pos;
1180                         if (DO_UPDATE(vc)) {
1181                                 /* do in two stages */
1182                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1183                                               vc->vc_cols - vc->vc_x);
1184                                 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1185                                               vc->vc_rows - vc->vc_y - 1,
1186                                               vc->vc_cols);
1187                         }
1188                         break;
1189                 case 1: /* erase from start to cursor */
1190                         count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1191                         start = (unsigned short *)vc->vc_origin;
1192                         if (DO_UPDATE(vc)) {
1193                                 /* do in two stages */
1194                                 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1195                                               vc->vc_cols);
1196                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1197                                               vc->vc_x + 1);
1198                         }
1199                         break;
1200                 case 3: /* erase scroll-back buffer (and whole display) */
1201                         scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1202                                     vc->vc_screenbuf_size);
1203                         set_origin(vc);
1204                         if (CON_IS_VISIBLE(vc))
1205                                 update_screen(vc);
1206                         /* fall through */
1207                 case 2: /* erase whole display */
1208                         count = vc->vc_cols * vc->vc_rows;
1209                         start = (unsigned short *)vc->vc_origin;
1210                         if (DO_UPDATE(vc))
1211                                 vc->vc_sw->con_clear(vc, 0, 0,
1212                                               vc->vc_rows,
1213                                               vc->vc_cols);
1214                         break;
1215                 default:
1216                         return;
1217         }
1218         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1219         vc->vc_need_wrap = 0;
1220 }
1221
1222 static void csi_K(struct vc_data *vc, int vpar)
1223 {
1224         unsigned int count;
1225         unsigned short * start;
1226
1227         switch (vpar) {
1228                 case 0: /* erase from cursor to end of line */
1229                         count = vc->vc_cols - vc->vc_x;
1230                         start = (unsigned short *)vc->vc_pos;
1231                         if (DO_UPDATE(vc))
1232                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1233                                                      vc->vc_cols - vc->vc_x);
1234                         break;
1235                 case 1: /* erase from start of line to cursor */
1236                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1237                         count = vc->vc_x + 1;
1238                         if (DO_UPDATE(vc))
1239                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1240                                                      vc->vc_x + 1);
1241                         break;
1242                 case 2: /* erase whole line */
1243                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1244                         count = vc->vc_cols;
1245                         if (DO_UPDATE(vc))
1246                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1247                                               vc->vc_cols);
1248                         break;
1249                 default:
1250                         return;
1251         }
1252         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1253         vc->vc_need_wrap = 0;
1254 }
1255
1256 static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1257 {                                         /* not vt100? */
1258         int count;
1259
1260         if (!vpar)
1261                 vpar++;
1262         count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1263
1264         scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1265         if (DO_UPDATE(vc))
1266                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1267         vc->vc_need_wrap = 0;
1268 }
1269
1270 static void default_attr(struct vc_data *vc)
1271 {
1272         vc->vc_intensity = 1;
1273         vc->vc_italic = 0;
1274         vc->vc_underline = 0;
1275         vc->vc_reverse = 0;
1276         vc->vc_blink = 0;
1277         vc->vc_color = vc->vc_def_color;
1278 }
1279
1280 /* console_lock is held */
1281 static void csi_m(struct vc_data *vc)
1282 {
1283         int i;
1284
1285         for (i = 0; i <= vc->vc_npar; i++)
1286                 switch (vc->vc_par[i]) {
1287                         case 0: /* all attributes off */
1288                                 default_attr(vc);
1289                                 break;
1290                         case 1:
1291                                 vc->vc_intensity = 2;
1292                                 break;
1293                         case 2:
1294                                 vc->vc_intensity = 0;
1295                                 break;
1296                         case 3:
1297                                 vc->vc_italic = 1;
1298                                 break;
1299                         case 4:
1300                                 vc->vc_underline = 1;
1301                                 break;
1302                         case 5:
1303                                 vc->vc_blink = 1;
1304                                 break;
1305                         case 7:
1306                                 vc->vc_reverse = 1;
1307                                 break;
1308                         case 10: /* ANSI X3.64-1979 (SCO-ish?)
1309                                   * Select primary font, don't display
1310                                   * control chars if defined, don't set
1311                                   * bit 8 on output.
1312                                   */
1313                                 vc->vc_translate = set_translate(vc->vc_charset == 0
1314                                                 ? vc->vc_G0_charset
1315                                                 : vc->vc_G1_charset, vc);
1316                                 vc->vc_disp_ctrl = 0;
1317                                 vc->vc_toggle_meta = 0;
1318                                 break;
1319                         case 11: /* ANSI X3.64-1979 (SCO-ish?)
1320                                   * Select first alternate font, lets
1321                                   * chars < 32 be displayed as ROM chars.
1322                                   */
1323                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1324                                 vc->vc_disp_ctrl = 1;
1325                                 vc->vc_toggle_meta = 0;
1326                                 break;
1327                         case 12: /* ANSI X3.64-1979 (SCO-ish?)
1328                                   * Select second alternate font, toggle
1329                                   * high bit before displaying as ROM char.
1330                                   */
1331                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1332                                 vc->vc_disp_ctrl = 1;
1333                                 vc->vc_toggle_meta = 1;
1334                                 break;
1335                         case 21:
1336                         case 22:
1337                                 vc->vc_intensity = 1;
1338                                 break;
1339                         case 23:
1340                                 vc->vc_italic = 0;
1341                                 break;
1342                         case 24:
1343                                 vc->vc_underline = 0;
1344                                 break;
1345                         case 25:
1346                                 vc->vc_blink = 0;
1347                                 break;
1348                         case 27:
1349                                 vc->vc_reverse = 0;
1350                                 break;
1351                         case 38: /* ANSI X3.64-1979 (SCO-ish?)
1352                                   * Enables underscore, white foreground
1353                                   * with white underscore (Linux - use
1354                                   * default foreground).
1355                                   */
1356                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1357                                 vc->vc_underline = 1;
1358                                 break;
1359                         case 39: /* ANSI X3.64-1979 (SCO-ish?)
1360                                   * Disable underline option.
1361                                   * Reset colour to default? It did this
1362                                   * before...
1363                                   */
1364                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1365                                 vc->vc_underline = 0;
1366                                 break;
1367                         case 49:
1368                                 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1369                                 break;
1370                         default:
1371                                 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1372                                         vc->vc_color = color_table[vc->vc_par[i] - 30]
1373                                                 | (vc->vc_color & 0xf0);
1374                                 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1375                                         vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1376                                                 | (vc->vc_color & 0x0f);
1377                                 break;
1378                 }
1379         update_attr(vc);
1380 }
1381
1382 static void respond_string(const char *p, struct tty_struct *tty)
1383 {
1384         while (*p) {
1385                 tty_insert_flip_char(tty, *p, 0);
1386                 p++;
1387         }
1388         con_schedule_flip(tty);
1389 }
1390
1391 static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1392 {
1393         char buf[40];
1394
1395         sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1396         respond_string(buf, tty);
1397 }
1398
1399 static inline void status_report(struct tty_struct *tty)
1400 {
1401         respond_string("\033[0n", tty); /* Terminal ok */
1402 }
1403
1404 static inline void respond_ID(struct tty_struct * tty)
1405 {
1406         respond_string(VT102ID, tty);
1407 }
1408
1409 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1410 {
1411         char buf[8];
1412
1413         sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1414                 (char)('!' + mry));
1415         respond_string(buf, tty);
1416 }
1417
1418 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1419 int mouse_reporting(void)
1420 {
1421         return vc_cons[fg_console].d->vc_report_mouse;
1422 }
1423
1424 /* console_lock is held */
1425 static void set_mode(struct vc_data *vc, int on_off)
1426 {
1427         int i;
1428
1429         for (i = 0; i <= vc->vc_npar; i++)
1430                 if (vc->vc_ques) {
1431                         switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1432                         case 1:                 /* Cursor keys send ^[Ox/^[[x */
1433                                 if (on_off)
1434                                         set_kbd(vc, decckm);
1435                                 else
1436                                         clr_kbd(vc, decckm);
1437                                 break;
1438                         case 3: /* 80/132 mode switch unimplemented */
1439                                 vc->vc_deccolm = on_off;
1440 #if 0
1441                                 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1442                                 /* this alone does not suffice; some user mode
1443                                    utility has to change the hardware regs */
1444 #endif
1445                                 break;
1446                         case 5:                 /* Inverted screen on/off */
1447                                 if (vc->vc_decscnm != on_off) {
1448                                         vc->vc_decscnm = on_off;
1449                                         invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1450                                         update_attr(vc);
1451                                 }
1452                                 break;
1453                         case 6:                 /* Origin relative/absolute */
1454                                 vc->vc_decom = on_off;
1455                                 gotoxay(vc, 0, 0);
1456                                 break;
1457                         case 7:                 /* Autowrap on/off */
1458                                 vc->vc_decawm = on_off;
1459                                 break;
1460                         case 8:                 /* Autorepeat on/off */
1461                                 if (on_off)
1462                                         set_kbd(vc, decarm);
1463                                 else
1464                                         clr_kbd(vc, decarm);
1465                                 break;
1466                         case 9:
1467                                 vc->vc_report_mouse = on_off ? 1 : 0;
1468                                 break;
1469                         case 25:                /* Cursor on/off */
1470                                 vc->vc_deccm = on_off;
1471                                 break;
1472                         case 1000:
1473                                 vc->vc_report_mouse = on_off ? 2 : 0;
1474                                 break;
1475                         }
1476                 } else {
1477                         switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1478                         case 3:                 /* Monitor (display ctrls) */
1479                                 vc->vc_disp_ctrl = on_off;
1480                                 break;
1481                         case 4:                 /* Insert Mode on/off */
1482                                 vc->vc_decim = on_off;
1483                                 break;
1484                         case 20:                /* Lf, Enter == CrLf/Lf */
1485                                 if (on_off)
1486                                         set_kbd(vc, lnm);
1487                                 else
1488                                         clr_kbd(vc, lnm);
1489                                 break;
1490                         }
1491                 }
1492 }
1493
1494 /* console_lock is held */
1495 static void setterm_command(struct vc_data *vc)
1496 {
1497         switch(vc->vc_par[0]) {
1498                 case 1: /* set color for underline mode */
1499                         if (vc->vc_can_do_color &&
1500                                         vc->vc_par[1] < 16) {
1501                                 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1502                                 if (vc->vc_underline)
1503                                         update_attr(vc);
1504                         }
1505                         break;
1506                 case 2: /* set color for half intensity mode */
1507                         if (vc->vc_can_do_color &&
1508                                         vc->vc_par[1] < 16) {
1509                                 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1510                                 if (vc->vc_intensity == 0)
1511                                         update_attr(vc);
1512                         }
1513                         break;
1514                 case 8: /* store colors as defaults */
1515                         vc->vc_def_color = vc->vc_attr;
1516                         if (vc->vc_hi_font_mask == 0x100)
1517                                 vc->vc_def_color >>= 1;
1518                         default_attr(vc);
1519                         update_attr(vc);
1520                         break;
1521                 case 9: /* set blanking interval */
1522                         blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1523                         poke_blanked_console();
1524                         break;
1525                 case 10: /* set bell frequency in Hz */
1526                         if (vc->vc_npar >= 1)
1527                                 vc->vc_bell_pitch = vc->vc_par[1];
1528                         else
1529                                 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1530                         break;
1531                 case 11: /* set bell duration in msec */
1532                         if (vc->vc_npar >= 1)
1533                                 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1534                                         vc->vc_par[1] * HZ / 1000 : 0;
1535                         else
1536                                 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1537                         break;
1538                 case 12: /* bring specified console to the front */
1539                         if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1540                                 set_console(vc->vc_par[1] - 1);
1541                         break;
1542                 case 13: /* unblank the screen */
1543                         poke_blanked_console();
1544                         break;
1545                 case 14: /* set vesa powerdown interval */
1546                         vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1547                         break;
1548                 case 15: /* activate the previous console */
1549                         set_console(last_console);
1550                         break;
1551         }
1552 }
1553
1554 /* console_lock is held */
1555 static void csi_at(struct vc_data *vc, unsigned int nr)
1556 {
1557         if (nr > vc->vc_cols - vc->vc_x)
1558                 nr = vc->vc_cols - vc->vc_x;
1559         else if (!nr)
1560                 nr = 1;
1561         insert_char(vc, nr);
1562 }
1563
1564 /* console_lock is held */
1565 static void csi_L(struct vc_data *vc, unsigned int nr)
1566 {
1567         if (nr > vc->vc_rows - vc->vc_y)
1568                 nr = vc->vc_rows - vc->vc_y;
1569         else if (!nr)
1570                 nr = 1;
1571         scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1572         vc->vc_need_wrap = 0;
1573 }
1574
1575 /* console_lock is held */
1576 static void csi_P(struct vc_data *vc, unsigned int nr)
1577 {
1578         if (nr > vc->vc_cols - vc->vc_x)
1579                 nr = vc->vc_cols - vc->vc_x;
1580         else if (!nr)
1581                 nr = 1;
1582         delete_char(vc, nr);
1583 }
1584
1585 /* console_lock is held */
1586 static void csi_M(struct vc_data *vc, unsigned int nr)
1587 {
1588         if (nr > vc->vc_rows - vc->vc_y)
1589                 nr = vc->vc_rows - vc->vc_y;
1590         else if (!nr)
1591                 nr=1;
1592         scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1593         vc->vc_need_wrap = 0;
1594 }
1595
1596 /* console_lock is held (except via vc_init->reset_terminal */
1597 static void save_cur(struct vc_data *vc)
1598 {
1599         vc->vc_saved_x          = vc->vc_x;
1600         vc->vc_saved_y          = vc->vc_y;
1601         vc->vc_s_intensity      = vc->vc_intensity;
1602         vc->vc_s_italic         = vc->vc_italic;
1603         vc->vc_s_underline      = vc->vc_underline;
1604         vc->vc_s_blink          = vc->vc_blink;
1605         vc->vc_s_reverse        = vc->vc_reverse;
1606         vc->vc_s_charset        = vc->vc_charset;
1607         vc->vc_s_color          = vc->vc_color;
1608         vc->vc_saved_G0         = vc->vc_G0_charset;
1609         vc->vc_saved_G1         = vc->vc_G1_charset;
1610 }
1611
1612 /* console_lock is held */
1613 static void restore_cur(struct vc_data *vc)
1614 {
1615         gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1616         vc->vc_intensity        = vc->vc_s_intensity;
1617         vc->vc_italic           = vc->vc_s_italic;
1618         vc->vc_underline        = vc->vc_s_underline;
1619         vc->vc_blink            = vc->vc_s_blink;
1620         vc->vc_reverse          = vc->vc_s_reverse;
1621         vc->vc_charset          = vc->vc_s_charset;
1622         vc->vc_color            = vc->vc_s_color;
1623         vc->vc_G0_charset       = vc->vc_saved_G0;
1624         vc->vc_G1_charset       = vc->vc_saved_G1;
1625         vc->vc_translate        = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1626         update_attr(vc);
1627         vc->vc_need_wrap = 0;
1628 }
1629
1630 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1631         EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1632         ESpalette };
1633
1634 /* console_lock is held (except via vc_init()) */
1635 static void reset_terminal(struct vc_data *vc, int do_clear)
1636 {
1637         vc->vc_top              = 0;
1638         vc->vc_bottom           = vc->vc_rows;
1639         vc->vc_state            = ESnormal;
1640         vc->vc_ques             = 0;
1641         vc->vc_translate        = set_translate(LAT1_MAP, vc);
1642         vc->vc_G0_charset       = LAT1_MAP;
1643         vc->vc_G1_charset       = GRAF_MAP;
1644         vc->vc_charset          = 0;
1645         vc->vc_need_wrap        = 0;
1646         vc->vc_report_mouse     = 0;
1647         vc->vc_utf              = default_utf8;
1648         vc->vc_utf_count        = 0;
1649
1650         vc->vc_disp_ctrl        = 0;
1651         vc->vc_toggle_meta      = 0;
1652
1653         vc->vc_decscnm          = 0;
1654         vc->vc_decom            = 0;
1655         vc->vc_decawm           = 1;
1656         vc->vc_deccm            = global_cursor_default;
1657         vc->vc_decim            = 0;
1658
1659         set_kbd(vc, decarm);
1660         clr_kbd(vc, decckm);
1661         clr_kbd(vc, kbdapplic);
1662         clr_kbd(vc, lnm);
1663         kbd_table[vc->vc_num].lockstate = 0;
1664         kbd_table[vc->vc_num].slockstate = 0;
1665         kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1666         kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1667         /* do not do set_leds here because this causes an endless tasklet loop
1668            when the keyboard hasn't been initialized yet */
1669
1670         vc->vc_cursor_type = cur_default;
1671         vc->vc_complement_mask = vc->vc_s_complement_mask;
1672
1673         default_attr(vc);
1674         update_attr(vc);
1675
1676         vc->vc_tab_stop[0]      = 0x01010100;
1677         vc->vc_tab_stop[1]      =
1678         vc->vc_tab_stop[2]      =
1679         vc->vc_tab_stop[3]      =
1680         vc->vc_tab_stop[4]      =
1681         vc->vc_tab_stop[5]      =
1682         vc->vc_tab_stop[6]      =
1683         vc->vc_tab_stop[7]      = 0x01010101;
1684
1685         vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1686         vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1687
1688         gotoxy(vc, 0, 0);
1689         save_cur(vc);
1690         if (do_clear)
1691             csi_J(vc, 2);
1692 }
1693
1694 /* console_lock is held */
1695 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1696 {
1697         /*
1698          *  Control characters can be used in the _middle_
1699          *  of an escape sequence.
1700          */
1701         switch (c) {
1702         case 0:
1703                 return;
1704         case 7:
1705                 if (vc->vc_bell_duration)
1706                         kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1707                 return;
1708         case 8:
1709                 bs(vc);
1710                 return;
1711         case 9:
1712                 vc->vc_pos -= (vc->vc_x << 1);
1713                 while (vc->vc_x < vc->vc_cols - 1) {
1714                         vc->vc_x++;
1715                         if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1716                                 break;
1717                 }
1718                 vc->vc_pos += (vc->vc_x << 1);
1719                 notify_write(vc, '\t');
1720                 return;
1721         case 10: case 11: case 12:
1722                 lf(vc);
1723                 if (!is_kbd(vc, lnm))
1724                         return;
1725         case 13:
1726                 cr(vc);
1727                 return;
1728         case 14:
1729                 vc->vc_charset = 1;
1730                 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1731                 vc->vc_disp_ctrl = 1;
1732                 return;
1733         case 15:
1734                 vc->vc_charset = 0;
1735                 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1736                 vc->vc_disp_ctrl = 0;
1737                 return;
1738         case 24: case 26:
1739                 vc->vc_state = ESnormal;
1740                 return;
1741         case 27:
1742                 vc->vc_state = ESesc;
1743                 return;
1744         case 127:
1745                 del(vc);
1746                 return;
1747         case 128+27:
1748                 vc->vc_state = ESsquare;
1749                 return;
1750         }
1751         switch(vc->vc_state) {
1752         case ESesc:
1753                 vc->vc_state = ESnormal;
1754                 switch (c) {
1755                 case '[':
1756                         vc->vc_state = ESsquare;
1757                         return;
1758                 case ']':
1759                         vc->vc_state = ESnonstd;
1760                         return;
1761                 case '%':
1762                         vc->vc_state = ESpercent;
1763                         return;
1764                 case 'E':
1765                         cr(vc);
1766                         lf(vc);
1767                         return;
1768                 case 'M':
1769                         ri(vc);
1770                         return;
1771                 case 'D':
1772                         lf(vc);
1773                         return;
1774                 case 'H':
1775                         vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1776                         return;
1777                 case 'Z':
1778                         respond_ID(tty);
1779                         return;
1780                 case '7':
1781                         save_cur(vc);
1782                         return;
1783                 case '8':
1784                         restore_cur(vc);
1785                         return;
1786                 case '(':
1787                         vc->vc_state = ESsetG0;
1788                         return;
1789                 case ')':
1790                         vc->vc_state = ESsetG1;
1791                         return;
1792                 case '#':
1793                         vc->vc_state = EShash;
1794                         return;
1795                 case 'c':
1796                         reset_terminal(vc, 1);
1797                         return;
1798                 case '>':  /* Numeric keypad */
1799                         clr_kbd(vc, kbdapplic);
1800                         return;
1801                 case '=':  /* Appl. keypad */
1802                         set_kbd(vc, kbdapplic);
1803                         return;
1804                 }
1805                 return;
1806         case ESnonstd:
1807                 if (c=='P') {   /* palette escape sequence */
1808                         for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1809                                 vc->vc_par[vc->vc_npar] = 0;
1810                         vc->vc_npar = 0;
1811                         vc->vc_state = ESpalette;
1812                         return;
1813                 } else if (c=='R') {   /* reset palette */
1814                         reset_palette(vc);
1815                         vc->vc_state = ESnormal;
1816                 } else
1817                         vc->vc_state = ESnormal;
1818                 return;
1819         case ESpalette:
1820                 if (isxdigit(c)) {
1821                         vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1822                         if (vc->vc_npar == 7) {
1823                                 int i = vc->vc_par[0] * 3, j = 1;
1824                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1825                                 vc->vc_palette[i++] += vc->vc_par[j++];
1826                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1827                                 vc->vc_palette[i++] += vc->vc_par[j++];
1828                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1829                                 vc->vc_palette[i] += vc->vc_par[j];
1830                                 set_palette(vc);
1831                                 vc->vc_state = ESnormal;
1832                         }
1833                 } else
1834                         vc->vc_state = ESnormal;
1835                 return;
1836         case ESsquare:
1837                 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1838                         vc->vc_par[vc->vc_npar] = 0;
1839                 vc->vc_npar = 0;
1840                 vc->vc_state = ESgetpars;
1841                 if (c == '[') { /* Function key */
1842                         vc->vc_state=ESfunckey;
1843                         return;
1844                 }
1845                 vc->vc_ques = (c == '?');
1846                 if (vc->vc_ques)
1847                         return;
1848         case ESgetpars:
1849                 if (c == ';' && vc->vc_npar < NPAR - 1) {
1850                         vc->vc_npar++;
1851                         return;
1852                 } else if (c>='0' && c<='9') {
1853                         vc->vc_par[vc->vc_npar] *= 10;
1854                         vc->vc_par[vc->vc_npar] += c - '0';
1855                         return;
1856                 } else
1857                         vc->vc_state = ESgotpars;
1858         case ESgotpars:
1859                 vc->vc_state = ESnormal;
1860                 switch(c) {
1861                 case 'h':
1862                         set_mode(vc, 1);
1863                         return;
1864                 case 'l':
1865                         set_mode(vc, 0);
1866                         return;
1867                 case 'c':
1868                         if (vc->vc_ques) {
1869                                 if (vc->vc_par[0])
1870                                         vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1871                                 else
1872                                         vc->vc_cursor_type = cur_default;
1873                                 return;
1874                         }
1875                         break;
1876                 case 'm':
1877                         if (vc->vc_ques) {
1878                                 clear_selection();
1879                                 if (vc->vc_par[0])
1880                                         vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1881                                 else
1882                                         vc->vc_complement_mask = vc->vc_s_complement_mask;
1883                                 return;
1884                         }
1885                         break;
1886                 case 'n':
1887                         if (!vc->vc_ques) {
1888                                 if (vc->vc_par[0] == 5)
1889                                         status_report(tty);
1890                                 else if (vc->vc_par[0] == 6)
1891                                         cursor_report(vc, tty);
1892                         }
1893                         return;
1894                 }
1895                 if (vc->vc_ques) {
1896                         vc->vc_ques = 0;
1897                         return;
1898                 }
1899                 switch(c) {
1900                 case 'G': case '`':
1901                         if (vc->vc_par[0])
1902                                 vc->vc_par[0]--;
1903                         gotoxy(vc, vc->vc_par[0], vc->vc_y);
1904                         return;
1905                 case 'A':
1906                         if (!vc->vc_par[0])
1907                                 vc->vc_par[0]++;
1908                         gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1909                         return;
1910                 case 'B': case 'e':
1911                         if (!vc->vc_par[0])
1912                                 vc->vc_par[0]++;
1913                         gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1914                         return;
1915                 case 'C': case 'a':
1916                         if (!vc->vc_par[0])
1917                                 vc->vc_par[0]++;
1918                         gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1919                         return;
1920                 case 'D':
1921                         if (!vc->vc_par[0])
1922                                 vc->vc_par[0]++;
1923                         gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1924                         return;
1925                 case 'E':
1926                         if (!vc->vc_par[0])
1927                                 vc->vc_par[0]++;
1928                         gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1929                         return;
1930                 case 'F':
1931                         if (!vc->vc_par[0])
1932                                 vc->vc_par[0]++;
1933                         gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1934                         return;
1935                 case 'd':
1936                         if (vc->vc_par[0])
1937                                 vc->vc_par[0]--;
1938                         gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1939                         return;
1940                 case 'H': case 'f':
1941                         if (vc->vc_par[0])
1942                                 vc->vc_par[0]--;
1943                         if (vc->vc_par[1])
1944                                 vc->vc_par[1]--;
1945                         gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1946                         return;
1947                 case 'J':
1948                         csi_J(vc, vc->vc_par[0]);
1949                         return;
1950                 case 'K':
1951                         csi_K(vc, vc->vc_par[0]);
1952                         return;
1953                 case 'L':
1954                         csi_L(vc, vc->vc_par[0]);
1955                         return;
1956                 case 'M':
1957                         csi_M(vc, vc->vc_par[0]);
1958                         return;
1959                 case 'P':
1960                         csi_P(vc, vc->vc_par[0]);
1961                         return;
1962                 case 'c':
1963                         if (!vc->vc_par[0])
1964                                 respond_ID(tty);
1965                         return;
1966                 case 'g':
1967                         if (!vc->vc_par[0])
1968                                 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1969                         else if (vc->vc_par[0] == 3) {
1970                                 vc->vc_tab_stop[0] =
1971                                         vc->vc_tab_stop[1] =
1972                                         vc->vc_tab_stop[2] =
1973                                         vc->vc_tab_stop[3] =
1974                                         vc->vc_tab_stop[4] =
1975                                         vc->vc_tab_stop[5] =
1976                                         vc->vc_tab_stop[6] =
1977                                         vc->vc_tab_stop[7] = 0;
1978                         }
1979                         return;
1980                 case 'm':
1981                         csi_m(vc);
1982                         return;
1983                 case 'q': /* DECLL - but only 3 leds */
1984                         /* map 0,1,2,3 to 0,1,2,4 */
1985                         if (vc->vc_par[0] < 4)
1986                                 setledstate(kbd_table + vc->vc_num,
1987                                             (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1988                         return;
1989                 case 'r':
1990                         if (!vc->vc_par[0])
1991                                 vc->vc_par[0]++;
1992                         if (!vc->vc_par[1])
1993                                 vc->vc_par[1] = vc->vc_rows;
1994                         /* Minimum allowed region is 2 lines */
1995                         if (vc->vc_par[0] < vc->vc_par[1] &&
1996                             vc->vc_par[1] <= vc->vc_rows) {
1997                                 vc->vc_top = vc->vc_par[0] - 1;
1998                                 vc->vc_bottom = vc->vc_par[1];
1999                                 gotoxay(vc, 0, 0);
2000                         }
2001                         return;
2002                 case 's':
2003                         save_cur(vc);
2004                         return;
2005                 case 'u':
2006                         restore_cur(vc);
2007                         return;
2008                 case 'X':
2009                         csi_X(vc, vc->vc_par[0]);
2010                         return;
2011                 case '@':
2012                         csi_at(vc, vc->vc_par[0]);
2013                         return;
2014                 case ']': /* setterm functions */
2015                         setterm_command(vc);
2016                         return;
2017                 }
2018                 return;
2019         case ESpercent:
2020                 vc->vc_state = ESnormal;
2021                 switch (c) {
2022                 case '@':  /* defined in ISO 2022 */
2023                         vc->vc_utf = 0;
2024                         return;
2025                 case 'G':  /* prelim official escape code */
2026                 case '8':  /* retained for compatibility */
2027                         vc->vc_utf = 1;
2028                         return;
2029                 }
2030                 return;
2031         case ESfunckey:
2032                 vc->vc_state = ESnormal;
2033                 return;
2034         case EShash:
2035                 vc->vc_state = ESnormal;
2036                 if (c == '8') {
2037                         /* DEC screen alignment test. kludge :-) */
2038                         vc->vc_video_erase_char =
2039                                 (vc->vc_video_erase_char & 0xff00) | 'E';
2040                         csi_J(vc, 2);
2041                         vc->vc_video_erase_char =
2042                                 (vc->vc_video_erase_char & 0xff00) | ' ';
2043                         do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2044                 }
2045                 return;
2046         case ESsetG0:
2047                 if (c == '0')
2048                         vc->vc_G0_charset = GRAF_MAP;
2049                 else if (c == 'B')
2050                         vc->vc_G0_charset = LAT1_MAP;
2051                 else if (c == 'U')
2052                         vc->vc_G0_charset = IBMPC_MAP;
2053                 else if (c == 'K')
2054                         vc->vc_G0_charset = USER_MAP;
2055                 if (vc->vc_charset == 0)
2056                         vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2057                 vc->vc_state = ESnormal;
2058                 return;
2059         case ESsetG1:
2060                 if (c == '0')
2061                         vc->vc_G1_charset = GRAF_MAP;
2062                 else if (c == 'B')
2063                         vc->vc_G1_charset = LAT1_MAP;
2064                 else if (c == 'U')
2065                         vc->vc_G1_charset = IBMPC_MAP;
2066                 else if (c == 'K')
2067                         vc->vc_G1_charset = USER_MAP;
2068                 if (vc->vc_charset == 1)
2069                         vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2070                 vc->vc_state = ESnormal;
2071                 return;
2072         default:
2073                 vc->vc_state = ESnormal;
2074         }
2075 }
2076
2077 /* is_double_width() is based on the wcwidth() implementation by
2078  * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2079  * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2080  */
2081 struct interval {
2082         uint32_t first;
2083         uint32_t last;
2084 };
2085
2086 static int bisearch(uint32_t ucs, const struct interval *table, int max)
2087 {
2088         int min = 0;
2089         int mid;
2090
2091         if (ucs < table[0].first || ucs > table[max].last)
2092                 return 0;
2093         while (max >= min) {
2094                 mid = (min + max) / 2;
2095                 if (ucs > table[mid].last)
2096                         min = mid + 1;
2097                 else if (ucs < table[mid].first)
2098                         max = mid - 1;
2099                 else
2100                         return 1;
2101         }
2102         return 0;
2103 }
2104
2105 static int is_double_width(uint32_t ucs)
2106 {
2107         static const struct interval double_width[] = {
2108                 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2109                 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2110                 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2111                 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2112         };
2113         return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2114 }
2115
2116 /* acquires console_lock */
2117 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2118 {
2119 #ifdef VT_BUF_VRAM_ONLY
2120 #define FLUSH do { } while(0);
2121 #else
2122 #define FLUSH if (draw_x >= 0) { \
2123         vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2124         draw_x = -1; \
2125         }
2126 #endif
2127
2128         int c, tc, ok, n = 0, draw_x = -1;
2129         unsigned int currcons;
2130         unsigned long draw_from = 0, draw_to = 0;
2131         struct vc_data *vc;
2132         unsigned char vc_attr;
2133         struct vt_notifier_param param;
2134         uint8_t rescan;
2135         uint8_t inverse;
2136         uint8_t width;
2137         u16 himask, charmask;
2138
2139         if (in_interrupt())
2140                 return count;
2141
2142         might_sleep();
2143
2144         console_lock();
2145         vc = tty->driver_data;
2146         if (vc == NULL) {
2147                 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2148                 console_unlock();
2149                 return 0;
2150         }
2151
2152         currcons = vc->vc_num;
2153         if (!vc_cons_allocated(currcons)) {
2154                 /* could this happen? */
2155                 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2156                 console_unlock();
2157                 return 0;
2158         }
2159
2160         himask = vc->vc_hi_font_mask;
2161         charmask = himask ? 0x1ff : 0xff;
2162
2163         /* undraw cursor first */
2164         if (IS_FG(vc))
2165                 hide_cursor(vc);
2166
2167         param.vc = vc;
2168
2169         while (!tty->stopped && count) {
2170                 int orig = *buf;
2171                 c = orig;
2172                 buf++;
2173                 n++;
2174                 count--;
2175                 rescan = 0;
2176                 inverse = 0;
2177                 width = 1;
2178
2179                 /* Do no translation at all in control states */
2180                 if (vc->vc_state != ESnormal) {
2181                         tc = c;
2182                 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2183                     /* Combine UTF-8 into Unicode in vc_utf_char.
2184                      * vc_utf_count is the number of continuation bytes still
2185                      * expected to arrive.
2186                      * vc_npar is the number of continuation bytes arrived so
2187                      * far
2188                      */
2189 rescan_last_byte:
2190                     if ((c & 0xc0) == 0x80) {
2191                         /* Continuation byte received */
2192                         static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2193                         if (vc->vc_utf_count) {
2194                             vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2195                             vc->vc_npar++;
2196                             if (--vc->vc_utf_count) {
2197                                 /* Still need some bytes */
2198                                 continue;
2199                             }
2200                             /* Got a whole character */
2201                             c = vc->vc_utf_char;
2202                             /* Reject overlong sequences */
2203                             if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2204                                         c > utf8_length_changes[vc->vc_npar])
2205                                 c = 0xfffd;
2206                         } else {
2207                             /* Unexpected continuation byte */
2208                             vc->vc_utf_count = 0;
2209                             c = 0xfffd;
2210                         }
2211                     } else {
2212                         /* Single ASCII byte or first byte of a sequence received */
2213                         if (vc->vc_utf_count) {
2214                             /* Continuation byte expected */
2215                             rescan = 1;
2216                             vc->vc_utf_count = 0;
2217                             c = 0xfffd;
2218                         } else if (c > 0x7f) {
2219                             /* First byte of a multibyte sequence received */
2220                             vc->vc_npar = 0;
2221                             if ((c & 0xe0) == 0xc0) {
2222                                 vc->vc_utf_count = 1;
2223                                 vc->vc_utf_char = (c & 0x1f);
2224                             } else if ((c & 0xf0) == 0xe0) {
2225                                 vc->vc_utf_count = 2;
2226                                 vc->vc_utf_char = (c & 0x0f);
2227                             } else if ((c & 0xf8) == 0xf0) {
2228                                 vc->vc_utf_count = 3;
2229                                 vc->vc_utf_char = (c & 0x07);
2230                             } else if ((c & 0xfc) == 0xf8) {
2231                                 vc->vc_utf_count = 4;
2232                                 vc->vc_utf_char = (c & 0x03);
2233                             } else if ((c & 0xfe) == 0xfc) {
2234                                 vc->vc_utf_count = 5;
2235                                 vc->vc_utf_char = (c & 0x01);
2236                             } else {
2237                                 /* 254 and 255 are invalid */
2238                                 c = 0xfffd;
2239                             }
2240                             if (vc->vc_utf_count) {
2241                                 /* Still need some bytes */
2242                                 continue;
2243                             }
2244                         }
2245                         /* Nothing to do if an ASCII byte was received */
2246                     }
2247                     /* End of UTF-8 decoding. */
2248                     /* c is the received character, or U+FFFD for invalid sequences. */
2249                     /* Replace invalid Unicode code points with U+FFFD too */
2250                     if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2251                         c = 0xfffd;
2252                     tc = c;
2253                 } else {        /* no utf or alternate charset mode */
2254                     tc = vc_translate(vc, c);
2255                 }
2256
2257                 param.c = tc;
2258                 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2259                                         &param) == NOTIFY_STOP)
2260                         continue;
2261
2262                 /* If the original code was a control character we
2263                  * only allow a glyph to be displayed if the code is
2264                  * not normally used (such as for cursor movement) or
2265                  * if the disp_ctrl mode has been explicitly enabled.
2266                  * Certain characters (as given by the CTRL_ALWAYS
2267                  * bitmap) are always displayed as control characters,
2268                  * as the console would be pretty useless without
2269                  * them; to display an arbitrary font position use the
2270                  * direct-to-font zone in UTF-8 mode.
2271                  */
2272                 ok = tc && (c >= 32 ||
2273                             !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2274                                   vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2275                         && (c != 127 || vc->vc_disp_ctrl)
2276                         && (c != 128+27);
2277
2278                 if (vc->vc_state == ESnormal && ok) {
2279                         if (vc->vc_utf && !vc->vc_disp_ctrl) {
2280                                 if (is_double_width(c))
2281                                         width = 2;
2282                         }
2283                         /* Now try to find out how to display it */
2284                         tc = conv_uni_to_pc(vc, tc);
2285                         if (tc & ~charmask) {
2286                                 if (tc == -1 || tc == -2) {
2287                                     continue; /* nothing to display */
2288                                 }
2289                                 /* Glyph not found */
2290                                 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2291                                     /* In legacy mode use the glyph we get by a 1:1 mapping.
2292                                        This would make absolutely no sense with Unicode in mind,
2293                                        but do this for ASCII characters since a font may lack
2294                                        Unicode mapping info and we don't want to end up with
2295                                        having question marks only. */
2296                                     tc = c;
2297                                 } else {
2298                                     /* Display U+FFFD. If it's not found, display an inverse question mark. */
2299                                     tc = conv_uni_to_pc(vc, 0xfffd);
2300                                     if (tc < 0) {
2301                                         inverse = 1;
2302                                         tc = conv_uni_to_pc(vc, '?');
2303                                         if (tc < 0) tc = '?';
2304                                     }
2305                                 }
2306                         }
2307
2308                         if (!inverse) {
2309                                 vc_attr = vc->vc_attr;
2310                         } else {
2311                                 /* invert vc_attr */
2312                                 if (!vc->vc_can_do_color) {
2313                                         vc_attr = (vc->vc_attr) ^ 0x08;
2314                                 } else if (vc->vc_hi_font_mask == 0x100) {
2315                                         vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2316                                 } else {
2317                                         vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2318                                 }
2319                                 FLUSH
2320                         }
2321
2322                         while (1) {
2323                                 if (vc->vc_need_wrap || vc->vc_decim)
2324                                         FLUSH
2325                                 if (vc->vc_need_wrap) {
2326                                         cr(vc);
2327                                         lf(vc);
2328                                 }
2329                                 if (vc->vc_decim)
2330                                         insert_char(vc, 1);
2331                                 scr_writew(himask ?
2332                                              ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2333                                              (vc_attr << 8) + tc,
2334                                            (u16 *) vc->vc_pos);
2335                                 if (DO_UPDATE(vc) && draw_x < 0) {
2336                                         draw_x = vc->vc_x;
2337                                         draw_from = vc->vc_pos;
2338                                 }
2339                                 if (vc->vc_x == vc->vc_cols - 1) {
2340                                         vc->vc_need_wrap = vc->vc_decawm;
2341                                         draw_to = vc->vc_pos + 2;
2342                                 } else {
2343                                         vc->vc_x++;
2344                                         draw_to = (vc->vc_pos += 2);
2345                                 }
2346
2347                                 if (!--width) break;
2348
2349                                 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2350                                 if (tc < 0) tc = ' ';
2351                         }
2352                         notify_write(vc, c);
2353
2354                         if (inverse) {
2355                                 FLUSH
2356                         }
2357
2358                         if (rescan) {
2359                                 rescan = 0;
2360                                 inverse = 0;
2361                                 width = 1;
2362                                 c = orig;
2363                                 goto rescan_last_byte;
2364                         }
2365                         continue;
2366                 }
2367                 FLUSH
2368                 do_con_trol(tty, vc, orig);
2369         }
2370         FLUSH
2371         console_conditional_schedule();
2372         console_unlock();
2373         notify_update(vc);
2374         return n;
2375 #undef FLUSH
2376 }
2377
2378 /*
2379  * This is the console switching callback.
2380  *
2381  * Doing console switching in a process context allows
2382  * us to do the switches asynchronously (needed when we want
2383  * to switch due to a keyboard interrupt).  Synchronization
2384  * with other console code and prevention of re-entrancy is
2385  * ensured with console_lock.
2386  */
2387 static void console_callback(struct work_struct *ignored)
2388 {
2389         console_lock();
2390
2391         if (want_console >= 0) {
2392                 if (want_console != fg_console &&
2393                     vc_cons_allocated(want_console)) {
2394                         hide_cursor(vc_cons[fg_console].d);
2395                         change_console(vc_cons[want_console].d);
2396                         /* we only changed when the console had already
2397                            been allocated - a new console is not created
2398                            in an interrupt routine */
2399                 }
2400                 want_console = -1;
2401         }
2402         if (do_poke_blanked_console) { /* do not unblank for a LED change */
2403                 do_poke_blanked_console = 0;
2404                 poke_blanked_console();
2405         }
2406         if (scrollback_delta) {
2407                 struct vc_data *vc = vc_cons[fg_console].d;
2408                 clear_selection();
2409                 if (vc->vc_mode == KD_TEXT)
2410                         vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2411                 scrollback_delta = 0;
2412         }
2413         if (blank_timer_expired) {
2414                 do_blank_screen(0);
2415                 blank_timer_expired = 0;
2416         }
2417         notify_update(vc_cons[fg_console].d);
2418
2419         console_unlock();
2420 }
2421
2422 int set_console(int nr)
2423 {
2424         struct vc_data *vc = vc_cons[fg_console].d;
2425
2426         if (!vc_cons_allocated(nr) || vt_dont_switch ||
2427                 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2428
2429                 /*
2430                  * Console switch will fail in console_callback() or
2431                  * change_console() so there is no point scheduling
2432                  * the callback
2433                  *
2434                  * Existing set_console() users don't check the return
2435                  * value so this shouldn't break anything
2436                  */
2437                 return -EINVAL;
2438         }
2439
2440         want_console = nr;
2441         schedule_console_callback();
2442
2443         return 0;
2444 }
2445
2446 struct tty_driver *console_driver;
2447
2448 #ifdef CONFIG_VT_CONSOLE
2449
2450 /**
2451  * vt_kmsg_redirect() - Sets/gets the kernel message console
2452  * @new:        The new virtual terminal number or -1 if the console should stay
2453  *              unchanged
2454  *
2455  * By default, the kernel messages are always printed on the current virtual
2456  * console. However, the user may modify that default with the
2457  * TIOCL_SETKMSGREDIRECT ioctl call.
2458  *
2459  * This function sets the kernel message console to be @new. It returns the old
2460  * virtual console number. The virtual terminal number 0 (both as parameter and
2461  * return value) means no redirection (i.e. always printed on the currently
2462  * active console).
2463  *
2464  * The parameter -1 means that only the current console is returned, but the
2465  * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2466  * case to make the code more understandable.
2467  *
2468  * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2469  * the parameter and always returns 0.
2470  */
2471 int vt_kmsg_redirect(int new)
2472 {
2473         static int kmsg_con;
2474
2475         if (new != -1)
2476                 return xchg(&kmsg_con, new);
2477         else
2478                 return kmsg_con;
2479 }
2480
2481 /*
2482  *      Console on virtual terminal
2483  *
2484  * The console must be locked when we get here.
2485  */
2486
2487 static void vt_console_print(struct console *co, const char *b, unsigned count)
2488 {
2489         struct vc_data *vc = vc_cons[fg_console].d;
2490         unsigned char c;
2491         static DEFINE_SPINLOCK(printing_lock);
2492         const ushort *start;
2493         ushort cnt = 0;
2494         ushort myx;
2495         int kmsg_console;
2496
2497         /* console busy or not yet initialized */
2498         if (!printable)
2499                 return;
2500         if (!spin_trylock(&printing_lock))
2501                 return;
2502
2503         kmsg_console = vt_get_kmsg_redirect();
2504         if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2505                 vc = vc_cons[kmsg_console - 1].d;
2506
2507         /* read `x' only after setting currcons properly (otherwise
2508            the `x' macro will read the x of the foreground console). */
2509         myx = vc->vc_x;
2510
2511         if (!vc_cons_allocated(fg_console)) {
2512                 /* impossible */
2513                 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2514                 goto quit;
2515         }
2516
2517         if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2518                 goto quit;
2519
2520         /* undraw cursor first */
2521         if (IS_FG(vc))
2522                 hide_cursor(vc);
2523
2524         start = (ushort *)vc->vc_pos;
2525
2526         /* Contrived structure to try to emulate original need_wrap behaviour
2527          * Problems caused when we have need_wrap set on '\n' character */
2528         while (count--) {
2529                 c = *b++;
2530                 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2531                         if (cnt > 0) {
2532                                 if (CON_IS_VISIBLE(vc))
2533                                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2534                                 vc->vc_x += cnt;
2535                                 if (vc->vc_need_wrap)
2536                                         vc->vc_x--;
2537                                 cnt = 0;
2538                         }
2539                         if (c == 8) {           /* backspace */
2540                                 bs(vc);
2541                                 start = (ushort *)vc->vc_pos;
2542                                 myx = vc->vc_x;
2543                                 continue;
2544                         }
2545                         if (c != 13)
2546                                 lf(vc);
2547                         cr(vc);
2548                         start = (ushort *)vc->vc_pos;
2549                         myx = vc->vc_x;
2550                         if (c == 10 || c == 13)
2551                                 continue;
2552                 }
2553                 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2554                 notify_write(vc, c);
2555                 cnt++;
2556                 if (myx == vc->vc_cols - 1) {
2557                         vc->vc_need_wrap = 1;
2558                         continue;
2559                 }
2560                 vc->vc_pos += 2;
2561                 myx++;
2562         }
2563         if (cnt > 0) {
2564                 if (CON_IS_VISIBLE(vc))
2565                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2566                 vc->vc_x += cnt;
2567                 if (vc->vc_x == vc->vc_cols) {
2568                         vc->vc_x--;
2569                         vc->vc_need_wrap = 1;
2570                 }
2571         }
2572         set_cursor(vc);
2573         notify_update(vc);
2574
2575 quit:
2576         spin_unlock(&printing_lock);
2577 }
2578
2579 static struct tty_driver *vt_console_device(struct console *c, int *index)
2580 {
2581         *index = c->index ? c->index-1 : fg_console;
2582         return console_driver;
2583 }
2584
2585 static struct console vt_console_driver = {
2586         .name           = "tty",
2587         .write          = vt_console_print,
2588         .device         = vt_console_device,
2589         .unblank        = unblank_screen,
2590         .flags          = CON_PRINTBUFFER,
2591         .index          = -1,
2592 };
2593 #endif
2594
2595 /*
2596  *      Handling of Linux-specific VC ioctls
2597  */
2598
2599 /*
2600  * Generally a bit racy with respect to console_lock();.
2601  *
2602  * There are some functions which don't need it.
2603  *
2604  * There are some functions which can sleep for arbitrary periods
2605  * (paste_selection) but we don't need the lock there anyway.
2606  *
2607  * set_selection has locking, and definitely needs it
2608  */
2609
2610 int tioclinux(struct tty_struct *tty, unsigned long arg)
2611 {
2612         char type, data;
2613         char __user *p = (char __user *)arg;
2614         int lines;
2615         int ret;
2616
2617         if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2618                 return -EPERM;
2619         if (get_user(type, p))
2620                 return -EFAULT;
2621         ret = 0;
2622
2623         switch (type)
2624         {
2625                 case TIOCL_SETSEL:
2626                         console_lock();
2627                         ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2628                         console_unlock();
2629                         break;
2630                 case TIOCL_PASTESEL:
2631                         ret = paste_selection(tty);
2632                         break;
2633                 case TIOCL_UNBLANKSCREEN:
2634                         console_lock();
2635                         unblank_screen();
2636                         console_unlock();
2637                         break;
2638                 case TIOCL_SELLOADLUT:
2639                         ret = sel_loadlut(p);
2640                         break;
2641                 case TIOCL_GETSHIFTSTATE:
2642
2643         /*
2644          * Make it possible to react to Shift+Mousebutton.
2645          * Note that 'shift_state' is an undocumented
2646          * kernel-internal variable; programs not closely
2647          * related to the kernel should not use this.
2648          */
2649                         data = shift_state;
2650                         ret = __put_user(data, p);
2651                         break;
2652                 case TIOCL_GETMOUSEREPORTING:
2653                         data = mouse_reporting();
2654                         ret = __put_user(data, p);
2655                         break;
2656                 case TIOCL_SETVESABLANK:
2657                         ret = set_vesa_blanking(p);
2658                         break;
2659                 case TIOCL_GETKMSGREDIRECT:
2660                         data = vt_get_kmsg_redirect();
2661                         ret = __put_user(data, p);
2662                         break;
2663                 case TIOCL_SETKMSGREDIRECT:
2664                         if (!capable(CAP_SYS_ADMIN)) {
2665                                 ret = -EPERM;
2666                         } else {
2667                                 if (get_user(data, p+1))
2668                                         ret = -EFAULT;
2669                                 else
2670                                         vt_kmsg_redirect(data);
2671                         }
2672                         break;
2673                 case TIOCL_GETFGCONSOLE:
2674                         ret = fg_console;
2675                         break;
2676                 case TIOCL_SCROLLCONSOLE:
2677                         if (get_user(lines, (s32 __user *)(p+4))) {
2678                                 ret = -EFAULT;
2679                         } else {
2680                                 scrollfront(vc_cons[fg_console].d, lines);
2681                                 ret = 0;
2682                         }
2683                         break;
2684                 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2685                         console_lock();
2686                         ignore_poke = 1;
2687                         do_blank_screen(0);
2688                         console_unlock();
2689                         break;
2690                 case TIOCL_BLANKEDSCREEN:
2691                         ret = console_blanked;
2692                         break;
2693                 default:
2694                         ret = -EINVAL;
2695                         break;
2696         }
2697         return ret;
2698 }
2699
2700 /*
2701  * /dev/ttyN handling
2702  */
2703
2704 static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2705 {
2706         int     retval;
2707
2708         retval = do_con_write(tty, buf, count);
2709         con_flush_chars(tty);
2710
2711         return retval;
2712 }
2713
2714 static int con_put_char(struct tty_struct *tty, unsigned char ch)
2715 {
2716         if (in_interrupt())
2717                 return 0;       /* n_r3964 calls put_char() from interrupt context */
2718         return do_con_write(tty, &ch, 1);
2719 }
2720
2721 static int con_write_room(struct tty_struct *tty)
2722 {
2723         if (tty->stopped)
2724                 return 0;
2725         return 32768;           /* No limit, really; we're not buffering */
2726 }
2727
2728 static int con_chars_in_buffer(struct tty_struct *tty)
2729 {
2730         return 0;               /* we're not buffering */
2731 }
2732
2733 /*
2734  * con_throttle and con_unthrottle are only used for
2735  * paste_selection(), which has to stuff in a large number of
2736  * characters...
2737  */
2738 static void con_throttle(struct tty_struct *tty)
2739 {
2740 }
2741
2742 static void con_unthrottle(struct tty_struct *tty)
2743 {
2744         struct vc_data *vc = tty->driver_data;
2745
2746         wake_up_interruptible(&vc->paste_wait);
2747 }
2748
2749 /*
2750  * Turn the Scroll-Lock LED on when the tty is stopped
2751  */
2752 static void con_stop(struct tty_struct *tty)
2753 {
2754         int console_num;
2755         if (!tty)
2756                 return;
2757         console_num = tty->index;
2758         if (!vc_cons_allocated(console_num))
2759                 return;
2760         set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2761         set_leds();
2762 }
2763
2764 /*
2765  * Turn the Scroll-Lock LED off when the console is started
2766  */
2767 static void con_start(struct tty_struct *tty)
2768 {
2769         int console_num;
2770         if (!tty)
2771                 return;
2772         console_num = tty->index;
2773         if (!vc_cons_allocated(console_num))
2774                 return;
2775         clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2776         set_leds();
2777 }
2778
2779 static void con_flush_chars(struct tty_struct *tty)
2780 {
2781         struct vc_data *vc;
2782
2783         if (in_interrupt())     /* from flush_to_ldisc */
2784                 return;
2785
2786         /* if we race with con_close(), vt may be null */
2787         console_lock();
2788         vc = tty->driver_data;
2789         if (vc)
2790                 set_cursor(vc);
2791         console_unlock();
2792 }
2793
2794 /*
2795  * Allocate the console screen memory.
2796  */
2797 static int con_open(struct tty_struct *tty, struct file *filp)
2798 {
2799         unsigned int currcons = tty->index;
2800         int ret = 0;
2801
2802         console_lock();
2803         if (tty->driver_data == NULL) {
2804                 ret = vc_allocate(currcons);
2805                 if (ret == 0) {
2806                         struct vc_data *vc = vc_cons[currcons].d;
2807
2808                         /* Still being freed */
2809                         if (vc->port.tty) {
2810                                 console_unlock();
2811                                 return -ERESTARTSYS;
2812                         }
2813                         tty->driver_data = vc;
2814                         vc->port.tty = tty;
2815
2816                         if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2817                                 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2818                                 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2819                         }
2820                         if (vc->vc_utf)
2821                                 tty->termios->c_iflag |= IUTF8;
2822                         else
2823                                 tty->termios->c_iflag &= ~IUTF8;
2824                         console_unlock();
2825                         return ret;
2826                 }
2827         }
2828         console_unlock();
2829         return ret;
2830 }
2831
2832 static void con_close(struct tty_struct *tty, struct file *filp)
2833 {
2834         /* Nothing to do - we defer to shutdown */
2835 }
2836
2837 static void con_shutdown(struct tty_struct *tty)
2838 {
2839         struct vc_data *vc = tty->driver_data;
2840         BUG_ON(vc == NULL);
2841         console_lock();
2842         vc->port.tty = NULL;
2843         console_unlock();
2844         tty_shutdown(tty);
2845 }
2846
2847 static int default_italic_color    = 2; // green (ASCII)
2848 static int default_underline_color = 3; // cyan (ASCII)
2849 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2850 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2851
2852 static void vc_init(struct vc_data *vc, unsigned int rows,
2853                     unsigned int cols, int do_clear)
2854 {
2855         int j, k ;
2856
2857         vc->vc_cols = cols;
2858         vc->vc_rows = rows;
2859         vc->vc_size_row = cols << 1;
2860         vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2861
2862         set_origin(vc);
2863         vc->vc_pos = vc->vc_origin;
2864         reset_vc(vc);
2865         for (j=k=0; j<16; j++) {
2866                 vc->vc_palette[k++] = default_red[j] ;
2867                 vc->vc_palette[k++] = default_grn[j] ;
2868                 vc->vc_palette[k++] = default_blu[j] ;
2869         }
2870         vc->vc_def_color       = 0x07;   /* white */
2871         vc->vc_ulcolor         = default_underline_color;
2872         vc->vc_itcolor         = default_italic_color;
2873         vc->vc_halfcolor       = 0x08;   /* grey */
2874         init_waitqueue_head(&vc->paste_wait);
2875         reset_terminal(vc, do_clear);
2876 }
2877
2878 /*
2879  * This routine initializes console interrupts, and does nothing
2880  * else. If you want the screen to clear, call tty_write with
2881  * the appropriate escape-sequence.
2882  */
2883
2884 static int __init con_init(void)
2885 {
2886         const char *display_desc = NULL;
2887         struct vc_data *vc;
2888         unsigned int currcons = 0, i;
2889
2890         console_lock();
2891
2892         if (conswitchp)
2893                 display_desc = conswitchp->con_startup();
2894         if (!display_desc) {
2895                 fg_console = 0;
2896                 console_unlock();
2897                 return 0;
2898         }
2899
2900         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2901                 struct con_driver *con_driver = &registered_con_driver[i];
2902
2903                 if (con_driver->con == NULL) {
2904                         con_driver->con = conswitchp;
2905                         con_driver->desc = display_desc;
2906                         con_driver->flag = CON_DRIVER_FLAG_INIT;
2907                         con_driver->first = 0;
2908                         con_driver->last = MAX_NR_CONSOLES - 1;
2909                         break;
2910                 }
2911         }
2912
2913         for (i = 0; i < MAX_NR_CONSOLES; i++)
2914                 con_driver_map[i] = conswitchp;
2915
2916         if (blankinterval) {
2917                 blank_state = blank_normal_wait;
2918                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
2919         }
2920
2921         for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2922                 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2923                 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2924                 tty_port_init(&vc->port);
2925                 visual_init(vc, currcons, 1);
2926                 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2927                 vc_init(vc, vc->vc_rows, vc->vc_cols,
2928                         currcons || !vc->vc_sw->con_save_screen);
2929         }
2930         currcons = fg_console = 0;
2931         master_display_fg = vc = vc_cons[currcons].d;
2932         set_origin(vc);
2933         save_screen(vc);
2934         gotoxy(vc, vc->vc_x, vc->vc_y);
2935         csi_J(vc, 0);
2936         update_screen(vc);
2937         pr_info("Console: %s %s %dx%d",
2938                 vc->vc_can_do_color ? "colour" : "mono",
2939                 display_desc, vc->vc_cols, vc->vc_rows);
2940         printable = 1;
2941         printk("\n");
2942
2943         console_unlock();
2944
2945 #ifdef CONFIG_VT_CONSOLE
2946         register_console(&vt_console_driver);
2947 #endif
2948         return 0;
2949 }
2950 console_initcall(con_init);
2951
2952 static const struct tty_operations con_ops = {
2953         .open = con_open,
2954         .close = con_close,
2955         .write = con_write,
2956         .write_room = con_write_room,
2957         .put_char = con_put_char,
2958         .flush_chars = con_flush_chars,
2959         .chars_in_buffer = con_chars_in_buffer,
2960         .ioctl = vt_ioctl,
2961 #ifdef CONFIG_COMPAT
2962         .compat_ioctl = vt_compat_ioctl,
2963 #endif
2964         .stop = con_stop,
2965         .start = con_start,
2966         .throttle = con_throttle,
2967         .unthrottle = con_unthrottle,
2968         .resize = vt_resize,
2969         .shutdown = con_shutdown
2970 };
2971
2972 static struct cdev vc0_cdev;
2973
2974 static ssize_t show_tty_active(struct device *dev,
2975                                 struct device_attribute *attr, char *buf)
2976 {
2977         return sprintf(buf, "tty%d\n", fg_console + 1);
2978 }
2979 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2980
2981 int __init vty_init(const struct file_operations *console_fops)
2982 {
2983         cdev_init(&vc0_cdev, console_fops);
2984         if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2985             register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2986                 panic("Couldn't register /dev/tty0 driver\n");
2987         tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2988         if (IS_ERR(tty0dev))
2989                 tty0dev = NULL;
2990         else
2991                 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
2992
2993         vcs_init();
2994
2995         console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2996         if (!console_driver)
2997                 panic("Couldn't allocate console driver\n");
2998         console_driver->owner = THIS_MODULE;
2999         console_driver->name = "tty";
3000         console_driver->name_base = 1;
3001         console_driver->major = TTY_MAJOR;
3002         console_driver->minor_start = 1;
3003         console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3004         console_driver->init_termios = tty_std_termios;
3005         if (default_utf8)
3006                 console_driver->init_termios.c_iflag |= IUTF8;
3007         console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3008         tty_set_operations(console_driver, &con_ops);
3009         if (tty_register_driver(console_driver))
3010                 panic("Couldn't register console driver\n");
3011         kbd_init();
3012         console_map_init();
3013 #ifdef CONFIG_MDA_CONSOLE
3014         mda_console_init();
3015 #endif
3016         return 0;
3017 }
3018
3019 #ifndef VT_SINGLE_DRIVER
3020
3021 static struct class *vtconsole_class;
3022
3023 static int do_bind_con_driver(const struct consw *csw, int first, int last,
3024                            int deflt)
3025 {
3026         struct module *owner = csw->owner;
3027         const char *desc = NULL;
3028         struct con_driver *con_driver;
3029         int i, j = -1, k = -1, retval = -ENODEV;
3030
3031         if (!try_module_get(owner))
3032                 return -ENODEV;
3033
3034         WARN_CONSOLE_UNLOCKED();
3035
3036         /* check if driver is registered */
3037         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3038                 con_driver = &registered_con_driver[i];
3039
3040                 if (con_driver->con == csw) {
3041                         desc = con_driver->desc;
3042                         retval = 0;
3043                         break;
3044                 }
3045         }
3046
3047         if (retval)
3048                 goto err;
3049
3050         if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3051                 csw->con_startup();
3052                 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3053         }
3054
3055         if (deflt) {
3056                 if (conswitchp)
3057                         module_put(conswitchp->owner);
3058
3059                 __module_get(owner);
3060                 conswitchp = csw;
3061         }
3062
3063         first = max(first, con_driver->first);
3064         last = min(last, con_driver->last);
3065
3066         for (i = first; i <= last; i++) {
3067                 int old_was_color;
3068                 struct vc_data *vc = vc_cons[i].d;
3069
3070                 if (con_driver_map[i])
3071                         module_put(con_driver_map[i]->owner);
3072                 __module_get(owner);
3073                 con_driver_map[i] = csw;
3074
3075                 if (!vc || !vc->vc_sw)
3076                         continue;
3077
3078                 j = i;
3079
3080                 if (CON_IS_VISIBLE(vc)) {
3081                         k = i;
3082                         save_screen(vc);
3083                 }
3084
3085                 old_was_color = vc->vc_can_do_color;
3086                 vc->vc_sw->con_deinit(vc);
3087                 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3088                 visual_init(vc, i, 0);
3089                 set_origin(vc);
3090                 update_attr(vc);
3091
3092                 /* If the console changed between mono <-> color, then
3093                  * the attributes in the screenbuf will be wrong.  The
3094                  * following resets all attributes to something sane.
3095                  */
3096                 if (old_was_color != vc->vc_can_do_color)
3097                         clear_buffer_attributes(vc);
3098         }
3099
3100         pr_info("Console: switching ");
3101         if (!deflt)
3102                 printk("consoles %d-%d ", first+1, last+1);
3103         if (j >= 0) {
3104                 struct vc_data *vc = vc_cons[j].d;
3105
3106                 printk("to %s %s %dx%d\n",
3107                        vc->vc_can_do_color ? "colour" : "mono",
3108                        desc, vc->vc_cols, vc->vc_rows);
3109
3110                 if (k >= 0) {
3111                         vc = vc_cons[k].d;
3112                         update_screen(vc);
3113                 }
3114         } else
3115                 printk("to %s\n", desc);
3116
3117         retval = 0;
3118 err:
3119         module_put(owner);
3120         return retval;
3121 };
3122
3123
3124 static int bind_con_driver(const struct consw *csw, int first, int last,
3125                            int deflt)
3126 {
3127         int ret;
3128
3129         console_lock();
3130         ret = do_bind_con_driver(csw, first, last, deflt);
3131         console_unlock();
3132         return ret;
3133 }
3134
3135 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3136 static int con_is_graphics(const struct consw *csw, int first, int last)
3137 {
3138         int i, retval = 0;
3139
3140         for (i = first; i <= last; i++) {
3141                 struct vc_data *vc = vc_cons[i].d;
3142
3143                 if (vc && vc->vc_mode == KD_GRAPHICS) {
3144                         retval = 1;
3145                         break;
3146                 }
3147         }
3148
3149         return retval;
3150 }
3151
3152 /**
3153  * unbind_con_driver - unbind a console driver
3154  * @csw: pointer to console driver to unregister
3155  * @first: first in range of consoles that @csw should be unbound from
3156  * @last: last in range of consoles that @csw should be unbound from
3157  * @deflt: should next bound console driver be default after @csw is unbound?
3158  *
3159  * To unbind a driver from all possible consoles, pass 0 as @first and
3160  * %MAX_NR_CONSOLES as @last.
3161  *
3162  * @deflt controls whether the console that ends up replacing @csw should be
3163  * the default console.
3164  *
3165  * RETURNS:
3166  * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3167  * or 0 on success.
3168  */
3169 int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3170 {
3171         int retval;
3172
3173         console_lock();
3174         retval = do_unbind_con_driver(csw, first, last, deflt);
3175         console_unlock();
3176         return retval;
3177 }
3178 EXPORT_SYMBOL(unbind_con_driver);
3179
3180 /* unlocked version of unbind_con_driver() */
3181 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3182 {
3183         struct module *owner = csw->owner;
3184         const struct consw *defcsw = NULL;
3185         struct con_driver *con_driver = NULL, *con_back = NULL;
3186         int i, retval = -ENODEV;
3187
3188         if (!try_module_get(owner))
3189                 return -ENODEV;
3190
3191         WARN_CONSOLE_UNLOCKED();
3192
3193         /* check if driver is registered and if it is unbindable */
3194         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3195                 con_driver = &registered_con_driver[i];
3196
3197                 if (con_driver->con == csw &&
3198                     con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3199                         retval = 0;
3200                         break;
3201                 }
3202         }
3203
3204         if (retval)
3205                 goto err;
3206
3207         retval = -ENODEV;
3208
3209         /* check if backup driver exists */
3210         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3211                 con_back = &registered_con_driver[i];
3212
3213                 if (con_back->con &&
3214                     !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
3215                         defcsw = con_back->con;
3216                         retval = 0;
3217                         break;
3218                 }
3219         }
3220
3221         if (retval)
3222                 goto err;
3223
3224         if (!con_is_bound(csw))
3225                 goto err;
3226
3227         first = max(first, con_driver->first);
3228         last = min(last, con_driver->last);
3229
3230         for (i = first; i <= last; i++) {
3231                 if (con_driver_map[i] == csw) {
3232                         module_put(csw->owner);
3233                         con_driver_map[i] = NULL;
3234                 }
3235         }
3236
3237         if (!con_is_bound(defcsw)) {
3238                 const struct consw *defconsw = conswitchp;
3239
3240                 defcsw->con_startup();
3241                 con_back->flag |= CON_DRIVER_FLAG_INIT;
3242                 /*
3243                  * vgacon may change the default driver to point
3244                  * to dummycon, we restore it here...
3245                  */
3246                 conswitchp = defconsw;
3247         }
3248
3249         if (!con_is_bound(csw))
3250                 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3251
3252         /* ignore return value, binding should not fail */
3253         do_bind_con_driver(defcsw, first, last, deflt);
3254 err:
3255         module_put(owner);
3256         return retval;
3257
3258 }
3259 EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3260
3261 static int vt_bind(struct con_driver *con)
3262 {
3263         const struct consw *defcsw = NULL, *csw = NULL;
3264         int i, more = 1, first = -1, last = -1, deflt = 0;
3265
3266         if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3267             con_is_graphics(con->con, con->first, con->last))
3268                 goto err;
3269
3270         csw = con->con;
3271
3272         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3273                 struct con_driver *con = &registered_con_driver[i];
3274
3275                 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3276                         defcsw = con->con;
3277                         break;
3278                 }
3279         }
3280
3281         if (!defcsw)
3282                 goto err;
3283
3284         while (more) {
3285                 more = 0;
3286
3287                 for (i = con->first; i <= con->last; i++) {
3288                         if (con_driver_map[i] == defcsw) {
3289                                 if (first == -1)
3290                                         first = i;
3291                                 last = i;
3292                                 more = 1;
3293                         } else if (first != -1)
3294                                 break;
3295                 }
3296
3297                 if (first == 0 && last == MAX_NR_CONSOLES -1)
3298                         deflt = 1;
3299
3300                 if (first != -1)
3301                         bind_con_driver(csw, first, last, deflt);
3302
3303                 first = -1;
3304                 last = -1;
3305                 deflt = 0;
3306         }
3307
3308 err:
3309         return 0;
3310 }
3311
3312 static int vt_unbind(struct con_driver *con)
3313 {
3314         const struct consw *csw = NULL;
3315         int i, more = 1, first = -1, last = -1, deflt = 0;
3316
3317         if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3318             con_is_graphics(con->con, con->first, con->last))
3319                 goto err;
3320
3321         csw = con->con;
3322
3323         while (more) {
3324                 more = 0;
3325
3326                 for (i = con->first; i <= con->last; i++) {
3327                         if (con_driver_map[i] == csw) {
3328                                 if (first == -1)
3329                                         first = i;
3330                                 last = i;
3331                                 more = 1;
3332                         } else if (first != -1)
3333                                 break;
3334                 }
3335
3336                 if (first == 0 && last == MAX_NR_CONSOLES -1)
3337                         deflt = 1;
3338
3339                 if (first != -1)
3340                         unbind_con_driver(csw, first, last, deflt);
3341
3342                 first = -1;
3343                 last = -1;
3344                 deflt = 0;
3345         }
3346
3347 err:
3348         return 0;
3349 }
3350 #else
3351 static inline int vt_bind(struct con_driver *con)
3352 {
3353         return 0;
3354 }
3355 static inline int vt_unbind(struct con_driver *con)
3356 {
3357         return 0;
3358 }
3359 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3360
3361 static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3362                           const char *buf, size_t count)
3363 {
3364         struct con_driver *con = dev_get_drvdata(dev);
3365         int bind = simple_strtoul(buf, NULL, 0);
3366
3367         if (bind)
3368                 vt_bind(con);
3369         else
3370                 vt_unbind(con);
3371
3372         return count;
3373 }
3374
3375 static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3376                          char *buf)
3377 {
3378         struct con_driver *con = dev_get_drvdata(dev);
3379         int bind = con_is_bound(con->con);
3380
3381         return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3382 }
3383
3384 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3385                          char *buf)
3386 {
3387         struct con_driver *con = dev_get_drvdata(dev);
3388
3389         return snprintf(buf, PAGE_SIZE, "%s %s\n",
3390                         (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3391                          con->desc);
3392
3393 }
3394
3395 static struct device_attribute device_attrs[] = {
3396         __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3397         __ATTR(name, S_IRUGO, show_name, NULL),
3398 };
3399
3400 static int vtconsole_init_device(struct con_driver *con)
3401 {
3402         int i;
3403         int error = 0;
3404
3405         con->flag |= CON_DRIVER_FLAG_ATTR;
3406         dev_set_drvdata(con->dev, con);
3407         for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3408                 error = device_create_file(con->dev, &device_attrs[i]);
3409                 if (error)
3410                         break;
3411         }
3412
3413         if (error) {
3414                 while (--i >= 0)
3415                         device_remove_file(con->dev, &device_attrs[i]);
3416                 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3417         }
3418
3419         return error;
3420 }
3421
3422 static void vtconsole_deinit_device(struct con_driver *con)
3423 {
3424         int i;
3425
3426         if (con->flag & CON_DRIVER_FLAG_ATTR) {
3427                 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3428                         device_remove_file(con->dev, &device_attrs[i]);
3429                 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3430         }
3431 }
3432
3433 /**
3434  * con_is_bound - checks if driver is bound to the console
3435  * @csw: console driver
3436  *
3437  * RETURNS: zero if unbound, nonzero if bound
3438  *
3439  * Drivers can call this and if zero, they should release
3440  * all resources allocated on con_startup()
3441  */
3442 int con_is_bound(const struct consw *csw)
3443 {
3444         int i, bound = 0;
3445
3446         for (i = 0; i < MAX_NR_CONSOLES; i++) {
3447                 if (con_driver_map[i] == csw) {
3448                         bound = 1;
3449                         break;
3450                 }
3451         }
3452
3453         return bound;
3454 }
3455 EXPORT_SYMBOL(con_is_bound);
3456
3457 /**
3458  * con_debug_enter - prepare the console for the kernel debugger
3459  * @sw: console driver
3460  *
3461  * Called when the console is taken over by the kernel debugger, this
3462  * function needs to save the current console state, then put the console
3463  * into a state suitable for the kernel debugger.
3464  *
3465  * RETURNS:
3466  * Zero on success, nonzero if a failure occurred when trying to prepare
3467  * the console for the debugger.
3468  */
3469 int con_debug_enter(struct vc_data *vc)
3470 {
3471         int ret = 0;
3472
3473         saved_fg_console = fg_console;
3474         saved_last_console = last_console;
3475         saved_want_console = want_console;
3476         saved_vc_mode = vc->vc_mode;
3477         saved_console_blanked = console_blanked;
3478         vc->vc_mode = KD_TEXT;
3479         console_blanked = 0;
3480         if (vc->vc_sw->con_debug_enter)
3481                 ret = vc->vc_sw->con_debug_enter(vc);
3482 #ifdef CONFIG_KGDB_KDB
3483         /* Set the initial LINES variable if it is not already set */
3484         if (vc->vc_rows < 999) {
3485                 int linecount;
3486                 char lns[4];
3487                 const char *setargs[3] = {
3488                         "set",
3489                         "LINES",
3490                         lns,
3491                 };
3492                 if (kdbgetintenv(setargs[0], &linecount)) {
3493                         snprintf(lns, 4, "%i", vc->vc_rows);
3494                         kdb_set(2, setargs);
3495                 }
3496         }
3497 #endif /* CONFIG_KGDB_KDB */
3498         return ret;
3499 }
3500 EXPORT_SYMBOL_GPL(con_debug_enter);
3501
3502 /**
3503  * con_debug_leave - restore console state
3504  * @sw: console driver
3505  *
3506  * Restore the console state to what it was before the kernel debugger
3507  * was invoked.
3508  *
3509  * RETURNS:
3510  * Zero on success, nonzero if a failure occurred when trying to restore
3511  * the console.
3512  */
3513 int con_debug_leave(void)
3514 {
3515         struct vc_data *vc;
3516         int ret = 0;
3517
3518         fg_console = saved_fg_console;
3519         last_console = saved_last_console;
3520         want_console = saved_want_console;
3521         console_blanked = saved_console_blanked;
3522         vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3523
3524         vc = vc_cons[fg_console].d;
3525         if (vc->vc_sw->con_debug_leave)
3526                 ret = vc->vc_sw->con_debug_leave(vc);
3527         return ret;
3528 }
3529 EXPORT_SYMBOL_GPL(con_debug_leave);
3530
3531 static int do_register_con_driver(const struct consw *csw, int first, int last)
3532 {
3533         struct module *owner = csw->owner;
3534         struct con_driver *con_driver;
3535         const char *desc;
3536         int i, retval = 0;
3537
3538         WARN_CONSOLE_UNLOCKED();
3539
3540