tty: vt, fix bogus division in csi_J
[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         newscreen = kmalloc(new_screen_size, GFP_USER);
886         if (!newscreen)
887                 return -ENOMEM;
888
889         old_rows = vc->vc_rows;
890         old_row_size = vc->vc_size_row;
891
892         err = resize_screen(vc, new_cols, new_rows, user);
893         if (err) {
894                 kfree(newscreen);
895                 return err;
896         }
897
898         vc->vc_rows = new_rows;
899         vc->vc_cols = new_cols;
900         vc->vc_size_row = new_row_size;
901         vc->vc_screenbuf_size = new_screen_size;
902
903         rlth = min(old_row_size, new_row_size);
904         rrem = new_row_size - rlth;
905         old_origin = vc->vc_origin;
906         new_origin = (long) newscreen;
907         new_scr_end = new_origin + new_screen_size;
908
909         if (vc->vc_y > new_rows) {
910                 if (old_rows - vc->vc_y < new_rows) {
911                         /*
912                          * Cursor near the bottom, copy contents from the
913                          * bottom of buffer
914                          */
915                         old_origin += (old_rows - new_rows) * old_row_size;
916                 } else {
917                         /*
918                          * Cursor is in no man's land, copy 1/2 screenful
919                          * from the top and bottom of cursor position
920                          */
921                         old_origin += (vc->vc_y - new_rows/2) * old_row_size;
922                 }
923         }
924
925         end = old_origin + old_row_size * min(old_rows, new_rows);
926
927         update_attr(vc);
928
929         while (old_origin < end) {
930                 scr_memcpyw((unsigned short *) new_origin,
931                             (unsigned short *) old_origin, rlth);
932                 if (rrem)
933                         scr_memsetw((void *)(new_origin + rlth),
934                                     vc->vc_video_erase_char, rrem);
935                 old_origin += old_row_size;
936                 new_origin += new_row_size;
937         }
938         if (new_scr_end > new_origin)
939                 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
940                             new_scr_end - new_origin);
941         kfree(vc->vc_screenbuf);
942         vc->vc_screenbuf = newscreen;
943         vc->vc_screenbuf_size = new_screen_size;
944         set_origin(vc);
945
946         /* do part of a reset_terminal() */
947         vc->vc_top = 0;
948         vc->vc_bottom = vc->vc_rows;
949         gotoxy(vc, vc->vc_x, vc->vc_y);
950         save_cur(vc);
951
952         if (tty) {
953                 /* Rewrite the requested winsize data with the actual
954                    resulting sizes */
955                 struct winsize ws;
956                 memset(&ws, 0, sizeof(ws));
957                 ws.ws_row = vc->vc_rows;
958                 ws.ws_col = vc->vc_cols;
959                 ws.ws_ypixel = vc->vc_scan_lines;
960                 tty_do_resize(tty, &ws);
961         }
962
963         if (CON_IS_VISIBLE(vc))
964                 update_screen(vc);
965         vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
966         return err;
967 }
968
969 /**
970  *      vc_resize               -       resize a VT
971  *      @vc: virtual console
972  *      @cols: columns
973  *      @rows: rows
974  *
975  *      Resize a virtual console as seen from the console end of things. We
976  *      use the common vc_do_resize methods to update the structures. The
977  *      caller must hold the console sem to protect console internals and
978  *      vc->port.tty
979  */
980
981 int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
982 {
983         return vc_do_resize(vc->port.tty, vc, cols, rows);
984 }
985
986 /**
987  *      vt_resize               -       resize a VT
988  *      @tty: tty to resize
989  *      @ws: winsize attributes
990  *
991  *      Resize a virtual terminal. This is called by the tty layer as we
992  *      register our own handler for resizing. The mutual helper does all
993  *      the actual work.
994  *
995  *      Takes the console sem and the called methods then take the tty
996  *      termios_mutex and the tty ctrl_lock in that order.
997  */
998 static int vt_resize(struct tty_struct *tty, struct winsize *ws)
999 {
1000         struct vc_data *vc = tty->driver_data;
1001         int ret;
1002
1003         console_lock();
1004         ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1005         console_unlock();
1006         return ret;
1007 }
1008
1009 void vc_deallocate(unsigned int currcons)
1010 {
1011         WARN_CONSOLE_UNLOCKED();
1012
1013         if (vc_cons_allocated(currcons)) {
1014                 struct vc_data *vc = vc_cons[currcons].d;
1015                 struct vt_notifier_param param = { .vc = vc };
1016
1017                 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1018                 vcs_remove_sysfs(currcons);
1019                 vc->vc_sw->con_deinit(vc);
1020                 put_pid(vc->vt_pid);
1021                 module_put(vc->vc_sw->owner);
1022                 kfree(vc->vc_screenbuf);
1023                 if (currcons >= MIN_NR_CONSOLES)
1024                         kfree(vc);
1025                 vc_cons[currcons].d = NULL;
1026         }
1027 }
1028
1029 /*
1030  *      VT102 emulator
1031  */
1032
1033 #define set_kbd(vc, x)  set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1034 #define clr_kbd(vc, x)  clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1035 #define is_kbd(vc, x)   vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1036
1037 #define decarm          VC_REPEAT
1038 #define decckm          VC_CKMODE
1039 #define kbdapplic       VC_APPLIC
1040 #define lnm             VC_CRLF
1041
1042 /*
1043  * this is what the terminal answers to a ESC-Z or csi0c query.
1044  */
1045 #define VT100ID "\033[?1;2c"
1046 #define VT102ID "\033[?6c"
1047
1048 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1049                                        8,12,10,14, 9,13,11,15 };
1050
1051 /* the default colour table, for VGA+ colour systems */
1052 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1053     0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1054 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1055     0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1056 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1057     0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1058
1059 module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1060 module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1061 module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1062
1063 /*
1064  * gotoxy() must verify all boundaries, because the arguments
1065  * might also be negative. If the given position is out of
1066  * bounds, the cursor is placed at the nearest margin.
1067  */
1068 static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1069 {
1070         int min_y, max_y;
1071
1072         if (new_x < 0)
1073                 vc->vc_x = 0;
1074         else {
1075                 if (new_x >= vc->vc_cols)
1076                         vc->vc_x = vc->vc_cols - 1;
1077                 else
1078                         vc->vc_x = new_x;
1079         }
1080
1081         if (vc->vc_decom) {
1082                 min_y = vc->vc_top;
1083                 max_y = vc->vc_bottom;
1084         } else {
1085                 min_y = 0;
1086                 max_y = vc->vc_rows;
1087         }
1088         if (new_y < min_y)
1089                 vc->vc_y = min_y;
1090         else if (new_y >= max_y)
1091                 vc->vc_y = max_y - 1;
1092         else
1093                 vc->vc_y = new_y;
1094         vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1095         vc->vc_need_wrap = 0;
1096 }
1097
1098 /* for absolute user moves, when decom is set */
1099 static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1100 {
1101         gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1102 }
1103
1104 void scrollback(struct vc_data *vc, int lines)
1105 {
1106         if (!lines)
1107                 lines = vc->vc_rows / 2;
1108         scrolldelta(-lines);
1109 }
1110
1111 void scrollfront(struct vc_data *vc, int lines)
1112 {
1113         if (!lines)
1114                 lines = vc->vc_rows / 2;
1115         scrolldelta(lines);
1116 }
1117
1118 static void lf(struct vc_data *vc)
1119 {
1120         /* don't scroll if above bottom of scrolling region, or
1121          * if below scrolling region
1122          */
1123         if (vc->vc_y + 1 == vc->vc_bottom)
1124                 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1125         else if (vc->vc_y < vc->vc_rows - 1) {
1126                 vc->vc_y++;
1127                 vc->vc_pos += vc->vc_size_row;
1128         }
1129         vc->vc_need_wrap = 0;
1130         notify_write(vc, '\n');
1131 }
1132
1133 static void ri(struct vc_data *vc)
1134 {
1135         /* don't scroll if below top of scrolling region, or
1136          * if above scrolling region
1137          */
1138         if (vc->vc_y == vc->vc_top)
1139                 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1140         else if (vc->vc_y > 0) {
1141                 vc->vc_y--;
1142                 vc->vc_pos -= vc->vc_size_row;
1143         }
1144         vc->vc_need_wrap = 0;
1145 }
1146
1147 static inline void cr(struct vc_data *vc)
1148 {
1149         vc->vc_pos -= vc->vc_x << 1;
1150         vc->vc_need_wrap = vc->vc_x = 0;
1151         notify_write(vc, '\r');
1152 }
1153
1154 static inline void bs(struct vc_data *vc)
1155 {
1156         if (vc->vc_x) {
1157                 vc->vc_pos -= 2;
1158                 vc->vc_x--;
1159                 vc->vc_need_wrap = 0;
1160                 notify_write(vc, '\b');
1161         }
1162 }
1163
1164 static inline void del(struct vc_data *vc)
1165 {
1166         /* ignored */
1167 }
1168
1169 static void csi_J(struct vc_data *vc, int vpar)
1170 {
1171         unsigned int count;
1172         unsigned short * start;
1173
1174         switch (vpar) {
1175                 case 0: /* erase from cursor to end of display */
1176                         count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1177                         start = (unsigned short *)vc->vc_pos;
1178                         if (DO_UPDATE(vc)) {
1179                                 /* do in two stages */
1180                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1181                                               vc->vc_cols - vc->vc_x);
1182                                 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1183                                               vc->vc_rows - vc->vc_y - 1,
1184                                               vc->vc_cols);
1185                         }
1186                         break;
1187                 case 1: /* erase from start to cursor */
1188                         count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1189                         start = (unsigned short *)vc->vc_origin;
1190                         if (DO_UPDATE(vc)) {
1191                                 /* do in two stages */
1192                                 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1193                                               vc->vc_cols);
1194                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1195                                               vc->vc_x + 1);
1196                         }
1197                         break;
1198                 case 3: /* erase scroll-back buffer (and whole display) */
1199                         scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1200                                     vc->vc_screenbuf_size);
1201                         set_origin(vc);
1202                         if (CON_IS_VISIBLE(vc))
1203                                 update_screen(vc);
1204                         /* fall through */
1205                 case 2: /* erase whole display */
1206                         count = vc->vc_cols * vc->vc_rows;
1207                         start = (unsigned short *)vc->vc_origin;
1208                         if (DO_UPDATE(vc))
1209                                 vc->vc_sw->con_clear(vc, 0, 0,
1210                                               vc->vc_rows,
1211                                               vc->vc_cols);
1212                         break;
1213                 default:
1214                         return;
1215         }
1216         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1217         vc->vc_need_wrap = 0;
1218 }
1219
1220 static void csi_K(struct vc_data *vc, int vpar)
1221 {
1222         unsigned int count;
1223         unsigned short * start;
1224
1225         switch (vpar) {
1226                 case 0: /* erase from cursor to end of line */
1227                         count = vc->vc_cols - vc->vc_x;
1228                         start = (unsigned short *)vc->vc_pos;
1229                         if (DO_UPDATE(vc))
1230                                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1231                                                      vc->vc_cols - vc->vc_x);
1232                         break;
1233                 case 1: /* erase from start of line to cursor */
1234                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1235                         count = vc->vc_x + 1;
1236                         if (DO_UPDATE(vc))
1237                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1238                                                      vc->vc_x + 1);
1239                         break;
1240                 case 2: /* erase whole line */
1241                         start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1242                         count = vc->vc_cols;
1243                         if (DO_UPDATE(vc))
1244                                 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1245                                               vc->vc_cols);
1246                         break;
1247                 default:
1248                         return;
1249         }
1250         scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1251         vc->vc_need_wrap = 0;
1252 }
1253
1254 static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1255 {                                         /* not vt100? */
1256         int count;
1257
1258         if (!vpar)
1259                 vpar++;
1260         count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1261
1262         scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1263         if (DO_UPDATE(vc))
1264                 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1265         vc->vc_need_wrap = 0;
1266 }
1267
1268 static void default_attr(struct vc_data *vc)
1269 {
1270         vc->vc_intensity = 1;
1271         vc->vc_italic = 0;
1272         vc->vc_underline = 0;
1273         vc->vc_reverse = 0;
1274         vc->vc_blink = 0;
1275         vc->vc_color = vc->vc_def_color;
1276 }
1277
1278 /* console_lock is held */
1279 static void csi_m(struct vc_data *vc)
1280 {
1281         int i;
1282
1283         for (i = 0; i <= vc->vc_npar; i++)
1284                 switch (vc->vc_par[i]) {
1285                         case 0: /* all attributes off */
1286                                 default_attr(vc);
1287                                 break;
1288                         case 1:
1289                                 vc->vc_intensity = 2;
1290                                 break;
1291                         case 2:
1292                                 vc->vc_intensity = 0;
1293                                 break;
1294                         case 3:
1295                                 vc->vc_italic = 1;
1296                                 break;
1297                         case 4:
1298                                 vc->vc_underline = 1;
1299                                 break;
1300                         case 5:
1301                                 vc->vc_blink = 1;
1302                                 break;
1303                         case 7:
1304                                 vc->vc_reverse = 1;
1305                                 break;
1306                         case 10: /* ANSI X3.64-1979 (SCO-ish?)
1307                                   * Select primary font, don't display
1308                                   * control chars if defined, don't set
1309                                   * bit 8 on output.
1310                                   */
1311                                 vc->vc_translate = set_translate(vc->vc_charset == 0
1312                                                 ? vc->vc_G0_charset
1313                                                 : vc->vc_G1_charset, vc);
1314                                 vc->vc_disp_ctrl = 0;
1315                                 vc->vc_toggle_meta = 0;
1316                                 break;
1317                         case 11: /* ANSI X3.64-1979 (SCO-ish?)
1318                                   * Select first alternate font, lets
1319                                   * chars < 32 be displayed as ROM chars.
1320                                   */
1321                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1322                                 vc->vc_disp_ctrl = 1;
1323                                 vc->vc_toggle_meta = 0;
1324                                 break;
1325                         case 12: /* ANSI X3.64-1979 (SCO-ish?)
1326                                   * Select second alternate font, toggle
1327                                   * high bit before displaying as ROM char.
1328                                   */
1329                                 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1330                                 vc->vc_disp_ctrl = 1;
1331                                 vc->vc_toggle_meta = 1;
1332                                 break;
1333                         case 21:
1334                         case 22:
1335                                 vc->vc_intensity = 1;
1336                                 break;
1337                         case 23:
1338                                 vc->vc_italic = 0;
1339                                 break;
1340                         case 24:
1341                                 vc->vc_underline = 0;
1342                                 break;
1343                         case 25:
1344                                 vc->vc_blink = 0;
1345                                 break;
1346                         case 27:
1347                                 vc->vc_reverse = 0;
1348                                 break;
1349                         case 38: /* ANSI X3.64-1979 (SCO-ish?)
1350                                   * Enables underscore, white foreground
1351                                   * with white underscore (Linux - use
1352                                   * default foreground).
1353                                   */
1354                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1355                                 vc->vc_underline = 1;
1356                                 break;
1357                         case 39: /* ANSI X3.64-1979 (SCO-ish?)
1358                                   * Disable underline option.
1359                                   * Reset colour to default? It did this
1360                                   * before...
1361                                   */
1362                                 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1363                                 vc->vc_underline = 0;
1364                                 break;
1365                         case 49:
1366                                 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1367                                 break;
1368                         default:
1369                                 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1370                                         vc->vc_color = color_table[vc->vc_par[i] - 30]
1371                                                 | (vc->vc_color & 0xf0);
1372                                 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1373                                         vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1374                                                 | (vc->vc_color & 0x0f);
1375                                 break;
1376                 }
1377         update_attr(vc);
1378 }
1379
1380 static void respond_string(const char *p, struct tty_struct *tty)
1381 {
1382         while (*p) {
1383                 tty_insert_flip_char(tty, *p, 0);
1384                 p++;
1385         }
1386         con_schedule_flip(tty);
1387 }
1388
1389 static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1390 {
1391         char buf[40];
1392
1393         sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1394         respond_string(buf, tty);
1395 }
1396
1397 static inline void status_report(struct tty_struct *tty)
1398 {
1399         respond_string("\033[0n", tty); /* Terminal ok */
1400 }
1401
1402 static inline void respond_ID(struct tty_struct * tty)
1403 {
1404         respond_string(VT102ID, tty);
1405 }
1406
1407 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1408 {
1409         char buf[8];
1410
1411         sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1412                 (char)('!' + mry));
1413         respond_string(buf, tty);
1414 }
1415
1416 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1417 int mouse_reporting(void)
1418 {
1419         return vc_cons[fg_console].d->vc_report_mouse;
1420 }
1421
1422 /* console_lock is held */
1423 static void set_mode(struct vc_data *vc, int on_off)
1424 {
1425         int i;
1426
1427         for (i = 0; i <= vc->vc_npar; i++)
1428                 if (vc->vc_ques) {
1429                         switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1430                         case 1:                 /* Cursor keys send ^[Ox/^[[x */
1431                                 if (on_off)
1432                                         set_kbd(vc, decckm);
1433                                 else
1434                                         clr_kbd(vc, decckm);
1435                                 break;
1436                         case 3: /* 80/132 mode switch unimplemented */
1437                                 vc->vc_deccolm = on_off;
1438 #if 0
1439                                 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1440                                 /* this alone does not suffice; some user mode
1441                                    utility has to change the hardware regs */
1442 #endif
1443                                 break;
1444                         case 5:                 /* Inverted screen on/off */
1445                                 if (vc->vc_decscnm != on_off) {
1446                                         vc->vc_decscnm = on_off;
1447                                         invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1448                                         update_attr(vc);
1449                                 }
1450                                 break;
1451                         case 6:                 /* Origin relative/absolute */
1452                                 vc->vc_decom = on_off;
1453                                 gotoxay(vc, 0, 0);
1454                                 break;
1455                         case 7:                 /* Autowrap on/off */
1456                                 vc->vc_decawm = on_off;
1457                                 break;
1458                         case 8:                 /* Autorepeat on/off */
1459                                 if (on_off)
1460                                         set_kbd(vc, decarm);
1461                                 else
1462                                         clr_kbd(vc, decarm);
1463                                 break;
1464                         case 9:
1465                                 vc->vc_report_mouse = on_off ? 1 : 0;
1466                                 break;
1467                         case 25:                /* Cursor on/off */
1468                                 vc->vc_deccm = on_off;
1469                                 break;
1470                         case 1000:
1471                                 vc->vc_report_mouse = on_off ? 2 : 0;
1472                                 break;
1473                         }
1474                 } else {
1475                         switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1476                         case 3:                 /* Monitor (display ctrls) */
1477                                 vc->vc_disp_ctrl = on_off;
1478                                 break;
1479                         case 4:                 /* Insert Mode on/off */
1480                                 vc->vc_decim = on_off;
1481                                 break;
1482                         case 20:                /* Lf, Enter == CrLf/Lf */
1483                                 if (on_off)
1484                                         set_kbd(vc, lnm);
1485                                 else
1486                                         clr_kbd(vc, lnm);
1487                                 break;
1488                         }
1489                 }
1490 }
1491
1492 /* console_lock is held */
1493 static void setterm_command(struct vc_data *vc)
1494 {
1495         switch(vc->vc_par[0]) {
1496                 case 1: /* set color for underline mode */
1497                         if (vc->vc_can_do_color &&
1498                                         vc->vc_par[1] < 16) {
1499                                 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1500                                 if (vc->vc_underline)
1501                                         update_attr(vc);
1502                         }
1503                         break;
1504                 case 2: /* set color for half intensity mode */
1505                         if (vc->vc_can_do_color &&
1506                                         vc->vc_par[1] < 16) {
1507                                 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1508                                 if (vc->vc_intensity == 0)
1509                                         update_attr(vc);
1510                         }
1511                         break;
1512                 case 8: /* store colors as defaults */
1513                         vc->vc_def_color = vc->vc_attr;
1514                         if (vc->vc_hi_font_mask == 0x100)
1515                                 vc->vc_def_color >>= 1;
1516                         default_attr(vc);
1517                         update_attr(vc);
1518                         break;
1519                 case 9: /* set blanking interval */
1520                         blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1521                         poke_blanked_console();
1522                         break;
1523                 case 10: /* set bell frequency in Hz */
1524                         if (vc->vc_npar >= 1)
1525                                 vc->vc_bell_pitch = vc->vc_par[1];
1526                         else
1527                                 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1528                         break;
1529                 case 11: /* set bell duration in msec */
1530                         if (vc->vc_npar >= 1)
1531                                 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1532                                         vc->vc_par[1] * HZ / 1000 : 0;
1533                         else
1534                                 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1535                         break;
1536                 case 12: /* bring specified console to the front */
1537                         if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1538                                 set_console(vc->vc_par[1] - 1);
1539                         break;
1540                 case 13: /* unblank the screen */
1541                         poke_blanked_console();
1542                         break;
1543                 case 14: /* set vesa powerdown interval */
1544                         vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1545                         break;
1546                 case 15: /* activate the previous console */
1547                         set_console(last_console);
1548                         break;
1549         }
1550 }
1551
1552 /* console_lock is held */
1553 static void csi_at(struct vc_data *vc, unsigned int nr)
1554 {
1555         if (nr > vc->vc_cols - vc->vc_x)
1556                 nr = vc->vc_cols - vc->vc_x;
1557         else if (!nr)
1558                 nr = 1;
1559         insert_char(vc, nr);
1560 }
1561
1562 /* console_lock is held */
1563 static void csi_L(struct vc_data *vc, unsigned int nr)
1564 {
1565         if (nr > vc->vc_rows - vc->vc_y)
1566                 nr = vc->vc_rows - vc->vc_y;
1567         else if (!nr)
1568                 nr = 1;
1569         scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1570         vc->vc_need_wrap = 0;
1571 }
1572
1573 /* console_lock is held */
1574 static void csi_P(struct vc_data *vc, unsigned int nr)
1575 {
1576         if (nr > vc->vc_cols - vc->vc_x)
1577                 nr = vc->vc_cols - vc->vc_x;
1578         else if (!nr)
1579                 nr = 1;
1580         delete_char(vc, nr);
1581 }
1582
1583 /* console_lock is held */
1584 static void csi_M(struct vc_data *vc, unsigned int nr)
1585 {
1586         if (nr > vc->vc_rows - vc->vc_y)
1587                 nr = vc->vc_rows - vc->vc_y;
1588         else if (!nr)
1589                 nr=1;
1590         scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1591         vc->vc_need_wrap = 0;
1592 }
1593
1594 /* console_lock is held (except via vc_init->reset_terminal */
1595 static void save_cur(struct vc_data *vc)
1596 {
1597         vc->vc_saved_x          = vc->vc_x;
1598         vc->vc_saved_y          = vc->vc_y;
1599         vc->vc_s_intensity      = vc->vc_intensity;
1600         vc->vc_s_italic         = vc->vc_italic;
1601         vc->vc_s_underline      = vc->vc_underline;
1602         vc->vc_s_blink          = vc->vc_blink;
1603         vc->vc_s_reverse        = vc->vc_reverse;
1604         vc->vc_s_charset        = vc->vc_charset;
1605         vc->vc_s_color          = vc->vc_color;
1606         vc->vc_saved_G0         = vc->vc_G0_charset;
1607         vc->vc_saved_G1         = vc->vc_G1_charset;
1608 }
1609
1610 /* console_lock is held */
1611 static void restore_cur(struct vc_data *vc)
1612 {
1613         gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1614         vc->vc_intensity        = vc->vc_s_intensity;
1615         vc->vc_italic           = vc->vc_s_italic;
1616         vc->vc_underline        = vc->vc_s_underline;
1617         vc->vc_blink            = vc->vc_s_blink;
1618         vc->vc_reverse          = vc->vc_s_reverse;
1619         vc->vc_charset          = vc->vc_s_charset;
1620         vc->vc_color            = vc->vc_s_color;
1621         vc->vc_G0_charset       = vc->vc_saved_G0;
1622         vc->vc_G1_charset       = vc->vc_saved_G1;
1623         vc->vc_translate        = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1624         update_attr(vc);
1625         vc->vc_need_wrap = 0;
1626 }
1627
1628 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1629         EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1630         ESpalette };
1631
1632 /* console_lock is held (except via vc_init()) */
1633 static void reset_terminal(struct vc_data *vc, int do_clear)
1634 {
1635         vc->vc_top              = 0;
1636         vc->vc_bottom           = vc->vc_rows;
1637         vc->vc_state            = ESnormal;
1638         vc->vc_ques             = 0;
1639         vc->vc_translate        = set_translate(LAT1_MAP, vc);
1640         vc->vc_G0_charset       = LAT1_MAP;
1641         vc->vc_G1_charset       = GRAF_MAP;
1642         vc->vc_charset          = 0;
1643         vc->vc_need_wrap        = 0;
1644         vc->vc_report_mouse     = 0;
1645         vc->vc_utf              = default_utf8;
1646         vc->vc_utf_count        = 0;
1647
1648         vc->vc_disp_ctrl        = 0;
1649         vc->vc_toggle_meta      = 0;
1650
1651         vc->vc_decscnm          = 0;
1652         vc->vc_decom            = 0;
1653         vc->vc_decawm           = 1;
1654         vc->vc_deccm            = global_cursor_default;
1655         vc->vc_decim            = 0;
1656
1657         set_kbd(vc, decarm);
1658         clr_kbd(vc, decckm);
1659         clr_kbd(vc, kbdapplic);
1660         clr_kbd(vc, lnm);
1661         kbd_table[vc->vc_num].lockstate = 0;
1662         kbd_table[vc->vc_num].slockstate = 0;
1663         kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1664         kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1665         /* do not do set_leds here because this causes an endless tasklet loop
1666            when the keyboard hasn't been initialized yet */
1667
1668         vc->vc_cursor_type = cur_default;
1669         vc->vc_complement_mask = vc->vc_s_complement_mask;
1670
1671         default_attr(vc);
1672         update_attr(vc);
1673
1674         vc->vc_tab_stop[0]      = 0x01010100;
1675         vc->vc_tab_stop[1]      =
1676         vc->vc_tab_stop[2]      =
1677         vc->vc_tab_stop[3]      =
1678         vc->vc_tab_stop[4]      =
1679         vc->vc_tab_stop[5]      =
1680         vc->vc_tab_stop[6]      =
1681         vc->vc_tab_stop[7]      = 0x01010101;
1682
1683         vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1684         vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1685
1686         gotoxy(vc, 0, 0);
1687         save_cur(vc);
1688         if (do_clear)
1689             csi_J(vc, 2);
1690 }
1691
1692 /* console_lock is held */
1693 static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1694 {
1695         /*
1696          *  Control characters can be used in the _middle_
1697          *  of an escape sequence.
1698          */
1699         switch (c) {
1700         case 0:
1701                 return;
1702         case 7:
1703                 if (vc->vc_bell_duration)
1704                         kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1705                 return;
1706         case 8:
1707                 bs(vc);
1708                 return;
1709         case 9:
1710                 vc->vc_pos -= (vc->vc_x << 1);
1711                 while (vc->vc_x < vc->vc_cols - 1) {
1712                         vc->vc_x++;
1713                         if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1714                                 break;
1715                 }
1716                 vc->vc_pos += (vc->vc_x << 1);
1717                 notify_write(vc, '\t');
1718                 return;
1719         case 10: case 11: case 12:
1720                 lf(vc);
1721                 if (!is_kbd(vc, lnm))
1722                         return;
1723         case 13:
1724                 cr(vc);
1725                 return;
1726         case 14:
1727                 vc->vc_charset = 1;
1728                 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1729                 vc->vc_disp_ctrl = 1;
1730                 return;
1731         case 15:
1732                 vc->vc_charset = 0;
1733                 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1734                 vc->vc_disp_ctrl = 0;
1735                 return;
1736         case 24: case 26:
1737                 vc->vc_state = ESnormal;
1738                 return;
1739         case 27:
1740                 vc->vc_state = ESesc;
1741                 return;
1742         case 127:
1743                 del(vc);
1744                 return;
1745         case 128+27:
1746                 vc->vc_state = ESsquare;
1747                 return;
1748         }
1749         switch(vc->vc_state) {
1750         case ESesc:
1751                 vc->vc_state = ESnormal;
1752                 switch (c) {
1753                 case '[':
1754                         vc->vc_state = ESsquare;
1755                         return;
1756                 case ']':
1757                         vc->vc_state = ESnonstd;
1758                         return;
1759                 case '%':
1760                         vc->vc_state = ESpercent;
1761                         return;
1762                 case 'E':
1763                         cr(vc);
1764                         lf(vc);
1765                         return;
1766                 case 'M':
1767                         ri(vc);
1768                         return;
1769                 case 'D':
1770                         lf(vc);
1771                         return;
1772                 case 'H':
1773                         vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1774                         return;
1775                 case 'Z':
1776                         respond_ID(tty);
1777                         return;
1778                 case '7':
1779                         save_cur(vc);
1780                         return;
1781                 case '8':
1782                         restore_cur(vc);
1783                         return;
1784                 case '(':
1785                         vc->vc_state = ESsetG0;
1786                         return;
1787                 case ')':
1788                         vc->vc_state = ESsetG1;
1789                         return;
1790                 case '#':
1791                         vc->vc_state = EShash;
1792                         return;
1793                 case 'c':
1794                         reset_terminal(vc, 1);
1795                         return;
1796                 case '>':  /* Numeric keypad */
1797                         clr_kbd(vc, kbdapplic);
1798                         return;
1799                 case '=':  /* Appl. keypad */
1800                         set_kbd(vc, kbdapplic);
1801                         return;
1802                 }
1803                 return;
1804         case ESnonstd:
1805                 if (c=='P') {   /* palette escape sequence */
1806                         for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1807                                 vc->vc_par[vc->vc_npar] = 0;
1808                         vc->vc_npar = 0;
1809                         vc->vc_state = ESpalette;
1810                         return;
1811                 } else if (c=='R') {   /* reset palette */
1812                         reset_palette(vc);
1813                         vc->vc_state = ESnormal;
1814                 } else
1815                         vc->vc_state = ESnormal;
1816                 return;
1817         case ESpalette:
1818                 if (isxdigit(c)) {
1819                         vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1820                         if (vc->vc_npar == 7) {
1821                                 int i = vc->vc_par[0] * 3, j = 1;
1822                                 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1823                                 vc->vc_palette[i++] += vc->vc_par[j++];
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                                 set_palette(vc);
1829                                 vc->vc_state = ESnormal;
1830                         }
1831                 } else
1832                         vc->vc_state = ESnormal;
1833                 return;
1834         case ESsquare:
1835                 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1836                         vc->vc_par[vc->vc_npar] = 0;
1837                 vc->vc_npar = 0;
1838                 vc->vc_state = ESgetpars;
1839                 if (c == '[') { /* Function key */
1840                         vc->vc_state=ESfunckey;
1841                         return;
1842                 }
1843                 vc->vc_ques = (c == '?');
1844                 if (vc->vc_ques)
1845                         return;
1846         case ESgetpars:
1847                 if (c == ';' && vc->vc_npar < NPAR - 1) {
1848                         vc->vc_npar++;
1849                         return;
1850                 } else if (c>='0' && c<='9') {
1851                         vc->vc_par[vc->vc_npar] *= 10;
1852                         vc->vc_par[vc->vc_npar] += c - '0';
1853                         return;
1854                 } else
1855                         vc->vc_state = ESgotpars;
1856         case ESgotpars:
1857                 vc->vc_state = ESnormal;
1858                 switch(c) {
1859                 case 'h':
1860                         set_mode(vc, 1);
1861                         return;
1862                 case 'l':
1863                         set_mode(vc, 0);
1864                         return;
1865                 case 'c':
1866                         if (vc->vc_ques) {
1867                                 if (vc->vc_par[0])
1868                                         vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1869                                 else
1870                                         vc->vc_cursor_type = cur_default;
1871                                 return;
1872                         }
1873                         break;
1874                 case 'm':
1875                         if (vc->vc_ques) {
1876                                 clear_selection();
1877                                 if (vc->vc_par[0])
1878                                         vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1879                                 else
1880                                         vc->vc_complement_mask = vc->vc_s_complement_mask;
1881                                 return;
1882                         }
1883                         break;
1884                 case 'n':
1885                         if (!vc->vc_ques) {
1886                                 if (vc->vc_par[0] == 5)
1887                                         status_report(tty);
1888                                 else if (vc->vc_par[0] == 6)
1889                                         cursor_report(vc, tty);
1890                         }
1891                         return;
1892                 }
1893                 if (vc->vc_ques) {
1894                         vc->vc_ques = 0;
1895                         return;
1896                 }
1897                 switch(c) {
1898                 case 'G': case '`':
1899                         if (vc->vc_par[0])
1900                                 vc->vc_par[0]--;
1901                         gotoxy(vc, vc->vc_par[0], vc->vc_y);
1902                         return;
1903                 case 'A':
1904                         if (!vc->vc_par[0])
1905                                 vc->vc_par[0]++;
1906                         gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1907                         return;
1908                 case 'B': case 'e':
1909                         if (!vc->vc_par[0])
1910                                 vc->vc_par[0]++;
1911                         gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1912                         return;
1913                 case 'C': case 'a':
1914                         if (!vc->vc_par[0])
1915                                 vc->vc_par[0]++;
1916                         gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1917                         return;
1918                 case 'D':
1919                         if (!vc->vc_par[0])
1920                                 vc->vc_par[0]++;
1921                         gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1922                         return;
1923                 case 'E':
1924                         if (!vc->vc_par[0])
1925                                 vc->vc_par[0]++;
1926                         gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1927                         return;
1928                 case 'F':
1929                         if (!vc->vc_par[0])
1930                                 vc->vc_par[0]++;
1931                         gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1932                         return;
1933                 case 'd':
1934                         if (vc->vc_par[0])
1935                                 vc->vc_par[0]--;
1936                         gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1937                         return;
1938                 case 'H': case 'f':
1939                         if (vc->vc_par[0])
1940                                 vc->vc_par[0]--;
1941                         if (vc->vc_par[1])
1942                                 vc->vc_par[1]--;
1943                         gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1944                         return;
1945                 case 'J':
1946                         csi_J(vc, vc->vc_par[0]);
1947                         return;
1948                 case 'K':
1949                         csi_K(vc, vc->vc_par[0]);
1950                         return;
1951                 case 'L':
1952                         csi_L(vc, vc->vc_par[0]);
1953                         return;
1954                 case 'M':
1955                         csi_M(vc, vc->vc_par[0]);
1956                         return;
1957                 case 'P':
1958                         csi_P(vc, vc->vc_par[0]);
1959                         return;
1960                 case 'c':
1961                         if (!vc->vc_par[0])
1962                                 respond_ID(tty);
1963                         return;
1964                 case 'g':
1965                         if (!vc->vc_par[0])
1966                                 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1967                         else if (vc->vc_par[0] == 3) {
1968                                 vc->vc_tab_stop[0] =
1969                                         vc->vc_tab_stop[1] =
1970                                         vc->vc_tab_stop[2] =
1971                                         vc->vc_tab_stop[3] =
1972                                         vc->vc_tab_stop[4] =
1973                                         vc->vc_tab_stop[5] =
1974                                         vc->vc_tab_stop[6] =
1975                                         vc->vc_tab_stop[7] = 0;
1976                         }
1977                         return;
1978                 case 'm':
1979                         csi_m(vc);
1980                         return;
1981                 case 'q': /* DECLL - but only 3 leds */
1982                         /* map 0,1,2,3 to 0,1,2,4 */
1983                         if (vc->vc_par[0] < 4)
1984                                 setledstate(kbd_table + vc->vc_num,
1985                                             (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1986                         return;
1987                 case 'r':
1988                         if (!vc->vc_par[0])
1989                                 vc->vc_par[0]++;
1990                         if (!vc->vc_par[1])
1991                                 vc->vc_par[1] = vc->vc_rows;
1992                         /* Minimum allowed region is 2 lines */
1993                         if (vc->vc_par[0] < vc->vc_par[1] &&
1994                             vc->vc_par[1] <= vc->vc_rows) {
1995                                 vc->vc_top = vc->vc_par[0] - 1;
1996                                 vc->vc_bottom = vc->vc_par[1];
1997                                 gotoxay(vc, 0, 0);
1998                         }
1999                         return;
2000                 case 's':
2001                         save_cur(vc);
2002                         return;
2003                 case 'u':
2004                         restore_cur(vc);
2005                         return;
2006                 case 'X':
2007                         csi_X(vc, vc->vc_par[0]);
2008                         return;
2009                 case '@':
2010                         csi_at(vc, vc->vc_par[0]);
2011                         return;
2012                 case ']': /* setterm functions */
2013                         setterm_command(vc);
2014                         return;
2015                 }
2016                 return;
2017         case ESpercent:
2018                 vc->vc_state = ESnormal;
2019                 switch (c) {
2020                 case '@':  /* defined in ISO 2022 */
2021                         vc->vc_utf = 0;
2022                         return;
2023                 case 'G':  /* prelim official escape code */
2024                 case '8':  /* retained for compatibility */
2025                         vc->vc_utf = 1;
2026                         return;
2027                 }
2028                 return;
2029         case ESfunckey:
2030                 vc->vc_state = ESnormal;
2031                 return;
2032         case EShash:
2033                 vc->vc_state = ESnormal;
2034                 if (c == '8') {
2035                         /* DEC screen alignment test. kludge :-) */
2036                         vc->vc_video_erase_char =
2037                                 (vc->vc_video_erase_char & 0xff00) | 'E';
2038                         csi_J(vc, 2);
2039                         vc->vc_video_erase_char =
2040                                 (vc->vc_video_erase_char & 0xff00) | ' ';
2041                         do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2042                 }
2043                 return;
2044         case ESsetG0:
2045                 if (c == '0')
2046                         vc->vc_G0_charset = GRAF_MAP;
2047                 else if (c == 'B')
2048                         vc->vc_G0_charset = LAT1_MAP;
2049                 else if (c == 'U')
2050                         vc->vc_G0_charset = IBMPC_MAP;
2051                 else if (c == 'K')
2052                         vc->vc_G0_charset = USER_MAP;
2053                 if (vc->vc_charset == 0)
2054                         vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2055                 vc->vc_state = ESnormal;
2056                 return;
2057         case ESsetG1:
2058                 if (c == '0')
2059                         vc->vc_G1_charset = GRAF_MAP;
2060                 else if (c == 'B')
2061                         vc->vc_G1_charset = LAT1_MAP;
2062                 else if (c == 'U')
2063                         vc->vc_G1_charset = IBMPC_MAP;
2064                 else if (c == 'K')
2065                         vc->vc_G1_charset = USER_MAP;
2066                 if (vc->vc_charset == 1)
2067                         vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2068                 vc->vc_state = ESnormal;
2069                 return;
2070         default:
2071                 vc->vc_state = ESnormal;
2072         }
2073 }
2074
2075 /* is_double_width() is based on the wcwidth() implementation by
2076  * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2077  * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2078  */
2079 struct interval {
2080         uint32_t first;
2081         uint32_t last;
2082 };
2083
2084 static int bisearch(uint32_t ucs, const struct interval *table, int max)
2085 {
2086         int min = 0;
2087         int mid;
2088
2089         if (ucs < table[0].first || ucs > table[max].last)
2090                 return 0;
2091         while (max >= min) {
2092                 mid = (min + max) / 2;
2093                 if (ucs > table[mid].last)
2094                         min = mid + 1;
2095                 else if (ucs < table[mid].first)
2096                         max = mid - 1;
2097                 else
2098                         return 1;
2099         }
2100         return 0;
2101 }
2102
2103 static int is_double_width(uint32_t ucs)
2104 {
2105         static const struct interval double_width[] = {
2106                 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2107                 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2108                 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2109                 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2110         };
2111         return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2112 }
2113
2114 /* acquires console_lock */
2115 static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2116 {
2117 #ifdef VT_BUF_VRAM_ONLY
2118 #define FLUSH do { } while(0);
2119 #else
2120 #define FLUSH if (draw_x >= 0) { \
2121         vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2122         draw_x = -1; \
2123         }
2124 #endif
2125
2126         int c, tc, ok, n = 0, draw_x = -1;
2127         unsigned int currcons;
2128         unsigned long draw_from = 0, draw_to = 0;
2129         struct vc_data *vc;
2130         unsigned char vc_attr;
2131         struct vt_notifier_param param;
2132         uint8_t rescan;
2133         uint8_t inverse;
2134         uint8_t width;
2135         u16 himask, charmask;
2136
2137         if (in_interrupt())
2138                 return count;
2139
2140         might_sleep();
2141
2142         console_lock();
2143         vc = tty->driver_data;
2144         if (vc == NULL) {
2145                 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2146                 console_unlock();
2147                 return 0;
2148         }
2149
2150         currcons = vc->vc_num;
2151         if (!vc_cons_allocated(currcons)) {
2152                 /* could this happen? */
2153                 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2154                 console_unlock();
2155                 return 0;
2156         }
2157
2158         himask = vc->vc_hi_font_mask;
2159         charmask = himask ? 0x1ff : 0xff;
2160
2161         /* undraw cursor first */
2162         if (IS_FG(vc))
2163                 hide_cursor(vc);
2164
2165         param.vc = vc;
2166
2167         while (!tty->stopped && count) {
2168                 int orig = *buf;
2169                 c = orig;
2170                 buf++;
2171                 n++;
2172                 count--;
2173                 rescan = 0;
2174                 inverse = 0;
2175                 width = 1;
2176
2177                 /* Do no translation at all in control states */
2178                 if (vc->vc_state != ESnormal) {
2179                         tc = c;
2180                 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2181                     /* Combine UTF-8 into Unicode in vc_utf_char.
2182                      * vc_utf_count is the number of continuation bytes still
2183                      * expected to arrive.
2184                      * vc_npar is the number of continuation bytes arrived so
2185                      * far
2186                      */
2187 rescan_last_byte:
2188                     if ((c & 0xc0) == 0x80) {
2189                         /* Continuation byte received */
2190                         static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2191                         if (vc->vc_utf_count) {
2192                             vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2193                             vc->vc_npar++;
2194                             if (--vc->vc_utf_count) {
2195                                 /* Still need some bytes */
2196                                 continue;
2197                             }
2198                             /* Got a whole character */
2199                             c = vc->vc_utf_char;
2200                             /* Reject overlong sequences */
2201                             if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2202                                         c > utf8_length_changes[vc->vc_npar])
2203                                 c = 0xfffd;
2204                         } else {
2205                             /* Unexpected continuation byte */
2206                             vc->vc_utf_count = 0;
2207                             c = 0xfffd;
2208                         }
2209                     } else {
2210                         /* Single ASCII byte or first byte of a sequence received */
2211                         if (vc->vc_utf_count) {
2212                             /* Continuation byte expected */
2213                             rescan = 1;
2214                             vc->vc_utf_count = 0;
2215                             c = 0xfffd;
2216                         } else if (c > 0x7f) {
2217                             /* First byte of a multibyte sequence received */
2218                             vc->vc_npar = 0;
2219                             if ((c & 0xe0) == 0xc0) {
2220                                 vc->vc_utf_count = 1;
2221                                 vc->vc_utf_char = (c & 0x1f);
2222                             } else if ((c & 0xf0) == 0xe0) {
2223                                 vc->vc_utf_count = 2;
2224                                 vc->vc_utf_char = (c & 0x0f);
2225                             } else if ((c & 0xf8) == 0xf0) {
2226                                 vc->vc_utf_count = 3;
2227                                 vc->vc_utf_char = (c & 0x07);
2228                             } else if ((c & 0xfc) == 0xf8) {
2229                                 vc->vc_utf_count = 4;
2230                                 vc->vc_utf_char = (c & 0x03);
2231                             } else if ((c & 0xfe) == 0xfc) {
2232                                 vc->vc_utf_count = 5;
2233                                 vc->vc_utf_char = (c & 0x01);
2234                             } else {
2235                                 /* 254 and 255 are invalid */
2236                                 c = 0xfffd;
2237                             }
2238                             if (vc->vc_utf_count) {
2239                                 /* Still need some bytes */
2240                                 continue;
2241                             }
2242                         }
2243                         /* Nothing to do if an ASCII byte was received */
2244                     }
2245                     /* End of UTF-8 decoding. */
2246                     /* c is the received character, or U+FFFD for invalid sequences. */
2247                     /* Replace invalid Unicode code points with U+FFFD too */
2248                     if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2249                         c = 0xfffd;
2250                     tc = c;
2251                 } else {        /* no utf or alternate charset mode */
2252                     tc = vc_translate(vc, c);
2253                 }
2254
2255                 param.c = tc;
2256                 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2257                                         &param) == NOTIFY_STOP)
2258                         continue;
2259
2260                 /* If the original code was a control character we
2261                  * only allow a glyph to be displayed if the code is
2262                  * not normally used (such as for cursor movement) or
2263                  * if the disp_ctrl mode has been explicitly enabled.
2264                  * Certain characters (as given by the CTRL_ALWAYS
2265                  * bitmap) are always displayed as control characters,
2266                  * as the console would be pretty useless without
2267                  * them; to display an arbitrary font position use the
2268                  * direct-to-font zone in UTF-8 mode.
2269                  */
2270                 ok = tc && (c >= 32 ||
2271                             !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2272                                   vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2273                         && (c != 127 || vc->vc_disp_ctrl)
2274                         && (c != 128+27);
2275
2276                 if (vc->vc_state == ESnormal && ok) {
2277                         if (vc->vc_utf && !vc->vc_disp_ctrl) {
2278                                 if (is_double_width(c))
2279                                         width = 2;
2280                         }
2281                         /* Now try to find out how to display it */
2282                         tc = conv_uni_to_pc(vc, tc);
2283                         if (tc & ~charmask) {
2284                                 if (tc == -1 || tc == -2) {
2285                                     continue; /* nothing to display */
2286                                 }
2287                                 /* Glyph not found */
2288                                 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2289                                     /* In legacy mode use the glyph we get by a 1:1 mapping.
2290                                        This would make absolutely no sense with Unicode in mind,
2291                                        but do this for ASCII characters since a font may lack
2292                                        Unicode mapping info and we don't want to end up with
2293                                        having question marks only. */
2294                                     tc = c;
2295                                 } else {
2296                                     /* Display U+FFFD. If it's not found, display an inverse question mark. */
2297                                     tc = conv_uni_to_pc(vc, 0xfffd);
2298                                     if (tc < 0) {
2299                                         inverse = 1;
2300                                         tc = conv_uni_to_pc(vc, '?');
2301                                         if (tc < 0) tc = '?';
2302                                     }
2303                                 }
2304                         }
2305
2306                         if (!inverse) {
2307                                 vc_attr = vc->vc_attr;
2308                         } else {
2309                                 /* invert vc_attr */
2310                                 if (!vc->vc_can_do_color) {
2311                                         vc_attr = (vc->vc_attr) ^ 0x08;
2312                                 } else if (vc->vc_hi_font_mask == 0x100) {
2313                                         vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2314                                 } else {
2315                                         vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2316                                 }
2317                                 FLUSH
2318                         }
2319
2320                         while (1) {
2321                                 if (vc->vc_need_wrap || vc->vc_decim)
2322                                         FLUSH
2323                                 if (vc->vc_need_wrap) {
2324                                         cr(vc);
2325                                         lf(vc);
2326                                 }
2327                                 if (vc->vc_decim)
2328                                         insert_char(vc, 1);
2329                                 scr_writew(himask ?
2330                                              ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2331                                              (vc_attr << 8) + tc,
2332                                            (u16 *) vc->vc_pos);
2333                                 if (DO_UPDATE(vc) && draw_x < 0) {
2334                                         draw_x = vc->vc_x;
2335                                         draw_from = vc->vc_pos;
2336                                 }
2337                                 if (vc->vc_x == vc->vc_cols - 1) {
2338                                         vc->vc_need_wrap = vc->vc_decawm;
2339                                         draw_to = vc->vc_pos + 2;
2340                                 } else {
2341                                         vc->vc_x++;
2342                                         draw_to = (vc->vc_pos += 2);
2343                                 }
2344
2345                                 if (!--width) break;
2346
2347                                 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2348                                 if (tc < 0) tc = ' ';
2349                         }
2350                         notify_write(vc, c);
2351
2352                         if (inverse) {
2353                                 FLUSH
2354                         }
2355
2356                         if (rescan) {
2357                                 rescan = 0;
2358                                 inverse = 0;
2359                                 width = 1;
2360                                 c = orig;
2361                                 goto rescan_last_byte;
2362                         }
2363                         continue;
2364                 }
2365                 FLUSH
2366                 do_con_trol(tty, vc, orig);
2367         }
2368         FLUSH
2369         console_conditional_schedule();
2370         console_unlock();
2371         notify_update(vc);
2372         return n;
2373 #undef FLUSH
2374 }
2375
2376 /*
2377  * This is the console switching callback.
2378  *
2379  * Doing console switching in a process context allows
2380  * us to do the switches asynchronously (needed when we want
2381  * to switch due to a keyboard interrupt).  Synchronization
2382  * with other console code and prevention of re-entrancy is
2383  * ensured with console_lock.
2384  */
2385 static void console_callback(struct work_struct *ignored)
2386 {
2387         console_lock();
2388
2389         if (want_console >= 0) {
2390                 if (want_console != fg_console &&
2391                     vc_cons_allocated(want_console)) {
2392                         hide_cursor(vc_cons[fg_console].d);
2393                         change_console(vc_cons[want_console].d);
2394                         /* we only changed when the console had already
2395                            been allocated - a new console is not created
2396                            in an interrupt routine */
2397                 }
2398                 want_console = -1;
2399         }
2400         if (do_poke_blanked_console) { /* do not unblank for a LED change */
2401                 do_poke_blanked_console = 0;
2402                 poke_blanked_console();
2403         }
2404         if (scrollback_delta) {
2405                 struct vc_data *vc = vc_cons[fg_console].d;
2406                 clear_selection();
2407                 if (vc->vc_mode == KD_TEXT)
2408                         vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2409                 scrollback_delta = 0;
2410         }
2411         if (blank_timer_expired) {
2412                 do_blank_screen(0);
2413                 blank_timer_expired = 0;
2414         }
2415         notify_update(vc_cons[fg_console].d);
2416
2417         console_unlock();
2418 }
2419
2420 int set_console(int nr)
2421 {
2422         struct vc_data *vc = vc_cons[fg_console].d;
2423
2424         if (!vc_cons_allocated(nr) || vt_dont_switch ||
2425                 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2426
2427                 /*
2428                  * Console switch will fail in console_callback() or
2429                  * change_console() so there is no point scheduling
2430                  * the callback
2431                  *
2432                  * Existing set_console() users don't check the return
2433                  * value so this shouldn't break anything
2434                  */
2435                 return -EINVAL;
2436         }
2437
2438         want_console = nr;
2439         schedule_console_callback();
2440
2441         return 0;
2442 }
2443
2444 struct tty_driver *console_driver;
2445
2446 #ifdef CONFIG_VT_CONSOLE
2447
2448 /**
2449  * vt_kmsg_redirect() - Sets/gets the kernel message console
2450  * @new:        The new virtual terminal number or -1 if the console should stay
2451  *              unchanged
2452  *
2453  * By default, the kernel messages are always printed on the current virtual
2454  * console. However, the user may modify that default with the
2455  * TIOCL_SETKMSGREDIRECT ioctl call.
2456  *
2457  * This function sets the kernel message console to be @new. It returns the old
2458  * virtual console number. The virtual terminal number 0 (both as parameter and
2459  * return value) means no redirection (i.e. always printed on the currently
2460  * active console).
2461  *
2462  * The parameter -1 means that only the current console is returned, but the
2463  * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2464  * case to make the code more understandable.
2465  *
2466  * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2467  * the parameter and always returns 0.
2468  */
2469 int vt_kmsg_redirect(int new)
2470 {
2471         static int kmsg_con;
2472
2473         if (new != -1)
2474                 return xchg(&kmsg_con, new);
2475         else
2476                 return kmsg_con;
2477 }
2478
2479 /*
2480  *      Console on virtual terminal
2481  *
2482  * The console must be locked when we get here.
2483  */
2484
2485 static void vt_console_print(struct console *co, const char *b, unsigned count)
2486 {
2487         struct vc_data *vc = vc_cons[fg_console].d;
2488         unsigned char c;
2489         static DEFINE_SPINLOCK(printing_lock);
2490         const ushort *start;
2491         ushort cnt = 0;
2492         ushort myx;
2493         int kmsg_console;
2494
2495         /* console busy or not yet initialized */
2496         if (!printable)
2497                 return;
2498         if (!spin_trylock(&printing_lock))
2499                 return;
2500
2501         kmsg_console = vt_get_kmsg_redirect();
2502         if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2503                 vc = vc_cons[kmsg_console - 1].d;
2504
2505         /* read `x' only after setting currcons properly (otherwise
2506            the `x' macro will read the x of the foreground console). */
2507         myx = vc->vc_x;
2508
2509         if (!vc_cons_allocated(fg_console)) {
2510                 /* impossible */
2511                 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2512                 goto quit;
2513         }
2514
2515         if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2516                 goto quit;
2517
2518         /* undraw cursor first */
2519         if (IS_FG(vc))
2520                 hide_cursor(vc);
2521
2522         start = (ushort *)vc->vc_pos;
2523
2524         /* Contrived structure to try to emulate original need_wrap behaviour
2525          * Problems caused when we have need_wrap set on '\n' character */
2526         while (count--) {
2527                 c = *b++;
2528                 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2529                         if (cnt > 0) {
2530                                 if (CON_IS_VISIBLE(vc))
2531                                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2532                                 vc->vc_x += cnt;
2533                                 if (vc->vc_need_wrap)
2534                                         vc->vc_x--;
2535                                 cnt = 0;
2536                         }
2537                         if (c == 8) {           /* backspace */
2538                                 bs(vc);
2539                                 start = (ushort *)vc->vc_pos;
2540                                 myx = vc->vc_x;
2541                                 continue;
2542                         }
2543                         if (c != 13)
2544                                 lf(vc);
2545                         cr(vc);
2546                         start = (ushort *)vc->vc_pos;
2547                         myx = vc->vc_x;
2548                         if (c == 10 || c == 13)
2549                                 continue;
2550                 }
2551                 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2552                 notify_write(vc, c);
2553                 cnt++;
2554                 if (myx == vc->vc_cols - 1) {
2555                         vc->vc_need_wrap = 1;
2556                         continue;
2557                 }
2558                 vc->vc_pos += 2;
2559                 myx++;
2560         }
2561         if (cnt > 0) {
2562                 if (CON_IS_VISIBLE(vc))
2563                         vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2564                 vc->vc_x += cnt;
2565                 if (vc->vc_x == vc->vc_cols) {
2566                         vc->vc_x--;
2567                         vc->vc_need_wrap = 1;
2568                 }
2569         }
2570         set_cursor(vc);
2571         notify_update(vc);
2572
2573 quit:
2574         spin_unlock(&printing_lock);
2575 }
2576
2577 static struct tty_driver *vt_console_device(struct console *c, int *index)
2578 {
2579         *index = c->index ? c->index-1 : fg_console;
2580         return console_driver;
2581 }
2582
2583 static struct console vt_console_driver = {
2584         .name           = "tty",
2585         .write          = vt_console_print,
2586         .device         = vt_console_device,
2587         .unblank        = unblank_screen,
2588         .flags          = CON_PRINTBUFFER,
2589         .index          = -1,
2590 };
2591 #endif
2592
2593 /*
2594  *      Handling of Linux-specific VC ioctls
2595  */
2596
2597 /*
2598  * Generally a bit racy with respect to console_lock();.
2599  *
2600  * There are some functions which don't need it.
2601  *
2602  * There are some functions which can sleep for arbitrary periods
2603  * (paste_selection) but we don't need the lock there anyway.
2604  *
2605  * set_selection has locking, and definitely needs it
2606  */
2607
2608 int tioclinux(struct tty_struct *tty, unsigned long arg)
2609 {
2610         char type, data;
2611         char __user *p = (char __user *)arg;
2612         int lines;
2613         int ret;
2614
2615         if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2616                 return -EPERM;
2617         if (get_user(type, p))
2618                 return -EFAULT;
2619         ret = 0;
2620
2621         switch (type)
2622         {
2623                 case TIOCL_SETSEL:
2624                         console_lock();
2625                         ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2626                         console_unlock();
2627                         break;
2628                 case TIOCL_PASTESEL:
2629                         ret = paste_selection(tty);
2630                         break;
2631                 case TIOCL_UNBLANKSCREEN:
2632                         console_lock();
2633                         unblank_screen();
2634                         console_unlock();
2635                         break;
2636                 case TIOCL_SELLOADLUT:
2637                         ret = sel_loadlut(p);
2638                         break;
2639                 case TIOCL_GETSHIFTSTATE:
2640
2641         /*
2642          * Make it possible to react to Shift+Mousebutton.
2643          * Note that 'shift_state' is an undocumented
2644          * kernel-internal variable; programs not closely
2645          * related to the kernel should not use this.
2646          */
2647                         data = shift_state;
2648                         ret = __put_user(data, p);
2649                         break;
2650                 case TIOCL_GETMOUSEREPORTING:
2651                         data = mouse_reporting();
2652                         ret = __put_user(data, p);
2653                         break;
2654                 case TIOCL_SETVESABLANK:
2655                         ret = set_vesa_blanking(p);
2656                         break;
2657                 case TIOCL_GETKMSGREDIRECT:
2658                         data = vt_get_kmsg_redirect();
2659                         ret = __put_user(data, p);
2660                         break;
2661                 case TIOCL_SETKMSGREDIRECT:
2662                         if (!capable(CAP_SYS_ADMIN)) {
2663                                 ret = -EPERM;
2664                         } else {
2665                                 if (get_user(data, p+1))
2666                                         ret = -EFAULT;
2667                                 else
2668                                         vt_kmsg_redirect(data);
2669                         }
2670                         break;
2671                 case TIOCL_GETFGCONSOLE:
2672                         ret = fg_console;
2673                         break;
2674                 case TIOCL_SCROLLCONSOLE:
2675                         if (get_user(lines, (s32 __user *)(p+4))) {
2676                                 ret = -EFAULT;
2677                         } else {
2678                                 scrollfront(vc_cons[fg_console].d, lines);
2679                                 ret = 0;
2680                         }
2681                         break;
2682                 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2683                         console_lock();
2684                         ignore_poke = 1;
2685                         do_blank_screen(0);
2686                         console_unlock();
2687                         break;
2688                 case TIOCL_BLANKEDSCREEN:
2689                         ret = console_blanked;
2690                         break;
2691                 default:
2692                         ret = -EINVAL;
2693                         break;
2694         }
2695         return ret;
2696 }
2697
2698 /*
2699  * /dev/ttyN handling
2700  */
2701
2702 static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2703 {
2704         int     retval;
2705
2706         retval = do_con_write(tty, buf, count);
2707         con_flush_chars(tty);
2708
2709         return retval;
2710 }
2711
2712 static int con_put_char(struct tty_struct *tty, unsigned char ch)
2713 {
2714         if (in_interrupt())
2715                 return 0;       /* n_r3964 calls put_char() from interrupt context */
2716         return do_con_write(tty, &ch, 1);
2717 }
2718
2719 static int con_write_room(struct tty_struct *tty)
2720 {
2721         if (tty->stopped)
2722                 return 0;
2723         return 32768;           /* No limit, really; we're not buffering */
2724 }
2725
2726 static int con_chars_in_buffer(struct tty_struct *tty)
2727 {
2728         return 0;               /* we're not buffering */
2729 }
2730
2731 /*
2732  * con_throttle and con_unthrottle are only used for
2733  * paste_selection(), which has to stuff in a large number of
2734  * characters...
2735  */
2736 static void con_throttle(struct tty_struct *tty)
2737 {
2738 }
2739
2740 static void con_unthrottle(struct tty_struct *tty)
2741 {
2742         struct vc_data *vc = tty->driver_data;
2743
2744         wake_up_interruptible(&vc->paste_wait);
2745 }
2746
2747 /*
2748  * Turn the Scroll-Lock LED on when the tty is stopped
2749  */
2750 static void con_stop(struct tty_struct *tty)
2751 {
2752         int console_num;
2753         if (!tty)
2754                 return;
2755         console_num = tty->index;
2756         if (!vc_cons_allocated(console_num))
2757                 return;
2758         set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2759         set_leds();
2760 }
2761
2762 /*
2763  * Turn the Scroll-Lock LED off when the console is started
2764  */
2765 static void con_start(struct tty_struct *tty)
2766 {
2767         int console_num;
2768         if (!tty)
2769                 return;
2770         console_num = tty->index;
2771         if (!vc_cons_allocated(console_num))
2772                 return;
2773         clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2774         set_leds();
2775 }
2776
2777 static void con_flush_chars(struct tty_struct *tty)
2778 {
2779         struct vc_data *vc;
2780
2781         if (in_interrupt())     /* from flush_to_ldisc */
2782                 return;
2783
2784         /* if we race with con_close(), vt may be null */
2785         console_lock();
2786         vc = tty->driver_data;
2787         if (vc)
2788                 set_cursor(vc);
2789         console_unlock();
2790 }
2791
2792 /*
2793  * Allocate the console screen memory.
2794  */
2795 static int con_open(struct tty_struct *tty, struct file *filp)
2796 {
2797         unsigned int currcons = tty->index;
2798         int ret = 0;
2799
2800         console_lock();
2801         if (tty->driver_data == NULL) {
2802                 ret = vc_allocate(currcons);
2803                 if (ret == 0) {
2804                         struct vc_data *vc = vc_cons[currcons].d;
2805
2806                         /* Still being freed */
2807                         if (vc->port.tty) {
2808                                 console_unlock();
2809                                 return -ERESTARTSYS;
2810                         }
2811                         tty->driver_data = vc;
2812                         vc->port.tty = tty;
2813
2814                         if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2815                                 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2816                                 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2817                         }
2818                         if (vc->vc_utf)
2819                                 tty->termios->c_iflag |= IUTF8;
2820                         else
2821                                 tty->termios->c_iflag &= ~IUTF8;
2822                         console_unlock();
2823                         return ret;
2824                 }
2825         }
2826         console_unlock();
2827         return ret;
2828 }
2829
2830 static void con_close(struct tty_struct *tty, struct file *filp)
2831 {
2832         /* Nothing to do - we defer to shutdown */
2833 }
2834
2835 static void con_shutdown(struct tty_struct *tty)
2836 {
2837         struct vc_data *vc = tty->driver_data;
2838         BUG_ON(vc == NULL);
2839         console_lock();
2840         vc->port.tty = NULL;
2841         console_unlock();
2842         tty_shutdown(tty);
2843 }
2844
2845 static int default_italic_color    = 2; // green (ASCII)
2846 static int default_underline_color = 3; // cyan (ASCII)
2847 module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2848 module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2849
2850 static void vc_init(struct vc_data *vc, unsigned int rows,
2851                     unsigned int cols, int do_clear)
2852 {
2853         int j, k ;
2854
2855         vc->vc_cols = cols;
2856         vc->vc_rows = rows;
2857         vc->vc_size_row = cols << 1;
2858         vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2859
2860         set_origin(vc);
2861         vc->vc_pos = vc->vc_origin;
2862         reset_vc(vc);
2863         for (j=k=0; j<16; j++) {
2864                 vc->vc_palette[k++] = default_red[j] ;
2865                 vc->vc_palette[k++] = default_grn[j] ;
2866                 vc->vc_palette[k++] = default_blu[j] ;
2867         }
2868         vc->vc_def_color       = 0x07;   /* white */
2869         vc->vc_ulcolor         = default_underline_color;
2870         vc->vc_itcolor         = default_italic_color;
2871         vc->vc_halfcolor       = 0x08;   /* grey */
2872         init_waitqueue_head(&vc->paste_wait);
2873         reset_terminal(vc, do_clear);
2874 }
2875
2876 /*
2877  * This routine initializes console interrupts, and does nothing
2878  * else. If you want the screen to clear, call tty_write with
2879  * the appropriate escape-sequence.
2880  */
2881
2882 static int __init con_init(void)
2883 {
2884         const char *display_desc = NULL;
2885         struct vc_data *vc;
2886         unsigned int currcons = 0, i;
2887
2888         console_lock();
2889
2890         if (conswitchp)
2891                 display_desc = conswitchp->con_startup();
2892         if (!display_desc) {
2893                 fg_console = 0;
2894                 console_unlock();
2895                 return 0;
2896         }
2897
2898         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2899                 struct con_driver *con_driver = &registered_con_driver[i];
2900
2901                 if (con_driver->con == NULL) {
2902                         con_driver->con = conswitchp;
2903                         con_driver->desc = display_desc;
2904                         con_driver->flag = CON_DRIVER_FLAG_INIT;
2905                         con_driver->first = 0;
2906                         con_driver->last = MAX_NR_CONSOLES - 1;
2907                         break;
2908                 }
2909         }
2910
2911         for (i = 0; i < MAX_NR_CONSOLES; i++)
2912                 con_driver_map[i] = conswitchp;
2913
2914         if (blankinterval) {
2915                 blank_state = blank_normal_wait;
2916                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
2917         }
2918
2919         for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2920                 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
2921                 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
2922                 tty_port_init(&vc->port);
2923                 visual_init(vc, currcons, 1);
2924                 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
2925                 vc_init(vc, vc->vc_rows, vc->vc_cols,
2926                         currcons || !vc->vc_sw->con_save_screen);
2927         }
2928         currcons = fg_console = 0;
2929         master_display_fg = vc = vc_cons[currcons].d;
2930         set_origin(vc);
2931         save_screen(vc);
2932         gotoxy(vc, vc->vc_x, vc->vc_y);
2933         csi_J(vc, 0);
2934         update_screen(vc);
2935         pr_info("Console: %s %s %dx%d",
2936                 vc->vc_can_do_color ? "colour" : "mono",
2937                 display_desc, vc->vc_cols, vc->vc_rows);
2938         printable = 1;
2939         printk("\n");
2940
2941         console_unlock();
2942
2943 #ifdef CONFIG_VT_CONSOLE
2944         register_console(&vt_console_driver);
2945 #endif
2946         return 0;
2947 }
2948 console_initcall(con_init);
2949
2950 static const struct tty_operations con_ops = {
2951         .open = con_open,
2952         .close = con_close,
2953         .write = con_write,
2954         .write_room = con_write_room,
2955         .put_char = con_put_char,
2956         .flush_chars = con_flush_chars,
2957         .chars_in_buffer = con_chars_in_buffer,
2958         .ioctl = vt_ioctl,
2959 #ifdef CONFIG_COMPAT
2960         .compat_ioctl = vt_compat_ioctl,
2961 #endif
2962         .stop = con_stop,
2963         .start = con_start,
2964         .throttle = con_throttle,
2965         .unthrottle = con_unthrottle,
2966         .resize = vt_resize,
2967         .shutdown = con_shutdown
2968 };
2969
2970 static struct cdev vc0_cdev;
2971
2972 static ssize_t show_tty_active(struct device *dev,
2973                                 struct device_attribute *attr, char *buf)
2974 {
2975         return sprintf(buf, "tty%d\n", fg_console + 1);
2976 }
2977 static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2978
2979 int __init vty_init(const struct file_operations *console_fops)
2980 {
2981         cdev_init(&vc0_cdev, console_fops);
2982         if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2983             register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2984                 panic("Couldn't register /dev/tty0 driver\n");
2985         tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2986         if (IS_ERR(tty0dev))
2987                 tty0dev = NULL;
2988         else
2989                 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
2990
2991         vcs_init();
2992
2993         console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2994         if (!console_driver)
2995                 panic("Couldn't allocate console driver\n");
2996         console_driver->owner = THIS_MODULE;
2997         console_driver->name = "tty";
2998         console_driver->name_base = 1;
2999         console_driver->major = TTY_MAJOR;
3000         console_driver->minor_start = 1;
3001         console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3002         console_driver->init_termios = tty_std_termios;
3003         if (default_utf8)
3004                 console_driver->init_termios.c_iflag |= IUTF8;
3005         console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3006         tty_set_operations(console_driver, &con_ops);
3007         if (tty_register_driver(console_driver))
3008                 panic("Couldn't register console driver\n");
3009         kbd_init();
3010         console_map_init();
3011 #ifdef CONFIG_MDA_CONSOLE
3012         mda_console_init();
3013 #endif
3014         return 0;
3015 }
3016
3017 #ifndef VT_SINGLE_DRIVER
3018
3019 static struct class *vtconsole_class;
3020
3021 static int do_bind_con_driver(const struct consw *csw, int first, int last,
3022                            int deflt)
3023 {
3024         struct module *owner = csw->owner;
3025         const char *desc = NULL;
3026         struct con_driver *con_driver;
3027         int i, j = -1, k = -1, retval = -ENODEV;
3028
3029         if (!try_module_get(owner))
3030                 return -ENODEV;
3031
3032         WARN_CONSOLE_UNLOCKED();
3033
3034         /* check if driver is registered */
3035         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3036                 con_driver = &registered_con_driver[i];
3037
3038                 if (con_driver->con == csw) {
3039                         desc = con_driver->desc;
3040                         retval = 0;
3041                         break;
3042                 }
3043         }
3044
3045         if (retval)
3046                 goto err;
3047
3048         if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3049                 csw->con_startup();
3050                 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3051         }
3052
3053         if (deflt) {
3054                 if (conswitchp)
3055                         module_put(conswitchp->owner);
3056
3057                 __module_get(owner);
3058                 conswitchp = csw;
3059         }
3060
3061         first = max(first, con_driver->first);
3062         last = min(last, con_driver->last);
3063
3064         for (i = first; i <= last; i++) {
3065                 int old_was_color;
3066                 struct vc_data *vc = vc_cons[i].d;
3067
3068                 if (con_driver_map[i])
3069                         module_put(con_driver_map[i]->owner);
3070                 __module_get(owner);
3071                 con_driver_map[i] = csw;
3072
3073                 if (!vc || !vc->vc_sw)
3074                         continue;
3075
3076                 j = i;
3077
3078                 if (CON_IS_VISIBLE(vc)) {
3079                         k = i;
3080                         save_screen(vc);
3081                 }
3082
3083                 old_was_color = vc->vc_can_do_color;
3084                 vc->vc_sw->con_deinit(vc);
3085                 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3086                 visual_init(vc, i, 0);
3087                 set_origin(vc);
3088                 update_attr(vc);
3089
3090                 /* If the console changed between mono <-> color, then
3091                  * the attributes in the screenbuf will be wrong.  The
3092                  * following resets all attributes to something sane.
3093                  */
3094                 if (old_was_color != vc->vc_can_do_color)
3095                         clear_buffer_attributes(vc);
3096         }
3097
3098         pr_info("Console: switching ");
3099         if (!deflt)
3100                 printk("consoles %d-%d ", first+1, last+1);
3101         if (j >= 0) {
3102                 struct vc_data *vc = vc_cons[j].d;
3103
3104                 printk("to %s %s %dx%d\n",
3105                        vc->vc_can_do_color ? "colour" : "mono",
3106                        desc, vc->vc_cols, vc->vc_rows);
3107
3108                 if (k >= 0) {
3109                         vc = vc_cons[k].d;
3110                         update_screen(vc);
3111                 }
3112         } else
3113                 printk("to %s\n", desc);
3114
3115         retval = 0;
3116 err:
3117         module_put(owner);
3118         return retval;
3119 };
3120
3121
3122 static int bind_con_driver(const struct consw *csw, int first, int last,
3123                            int deflt)
3124 {
3125         int ret;
3126
3127         console_lock();
3128         ret = do_bind_con_driver(csw, first, last, deflt);
3129         console_unlock();
3130         return ret;
3131 }
3132
3133 #ifdef CONFIG_VT_HW_CONSOLE_BINDING
3134 static int con_is_graphics(const struct consw *csw, int first, int last)
3135 {
3136         int i, retval = 0;
3137
3138         for (i = first; i <= last; i++) {
3139                 struct vc_data *vc = vc_cons[i].d;
3140
3141                 if (vc && vc->vc_mode == KD_GRAPHICS) {
3142                         retval = 1;
3143                         break;
3144                 }
3145         }
3146
3147         return retval;
3148 }
3149
3150 /**
3151  * unbind_con_driver - unbind a console driver
3152  * @csw: pointer to console driver to unregister
3153  * @first: first in range of consoles that @csw should be unbound from
3154  * @last: last in range of consoles that @csw should be unbound from
3155  * @deflt: should next bound console driver be default after @csw is unbound?
3156  *
3157  * To unbind a driver from all possible consoles, pass 0 as @first and
3158  * %MAX_NR_CONSOLES as @last.
3159  *
3160  * @deflt controls whether the console that ends up replacing @csw should be
3161  * the default console.
3162  *
3163  * RETURNS:
3164  * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3165  * or 0 on success.
3166  */
3167 int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3168 {
3169         int retval;
3170
3171         console_lock();
3172         retval = do_unbind_con_driver(csw, first, last, deflt);
3173         console_unlock();
3174         return retval;
3175 }
3176 EXPORT_SYMBOL(unbind_con_driver);
3177
3178 /* unlocked version of unbind_con_driver() */
3179 int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3180 {
3181         struct module *owner = csw->owner;
3182         const struct consw *defcsw = NULL;
3183         struct con_driver *con_driver = NULL, *con_back = NULL;
3184         int i, retval = -ENODEV;
3185
3186         if (!try_module_get(owner))
3187                 return -ENODEV;
3188
3189         WARN_CONSOLE_UNLOCKED();
3190
3191         /* check if driver is registered and if it is unbindable */
3192         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3193                 con_driver = &registered_con_driver[i];
3194
3195                 if (con_driver->con == csw &&
3196                     con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3197                         retval = 0;
3198                         break;
3199                 }
3200         }
3201
3202         if (retval)
3203                 goto err;
3204
3205         retval = -ENODEV;
3206
3207         /* check if backup driver exists */
3208         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3209                 con_back = &registered_con_driver[i];
3210
3211                 if (con_back->con &&
3212                     !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
3213                         defcsw = con_back->con;
3214                         retval = 0;
3215                         break;
3216                 }
3217         }
3218
3219         if (retval)
3220                 goto err;
3221
3222         if (!con_is_bound(csw))
3223                 goto err;
3224
3225         first = max(first, con_driver->first);
3226         last = min(last, con_driver->last);
3227
3228         for (i = first; i <= last; i++) {
3229                 if (con_driver_map[i] == csw) {
3230                         module_put(csw->owner);
3231                         con_driver_map[i] = NULL;
3232                 }
3233         }
3234
3235         if (!con_is_bound(defcsw)) {
3236                 const struct consw *defconsw = conswitchp;
3237
3238                 defcsw->con_startup();
3239                 con_back->flag |= CON_DRIVER_FLAG_INIT;
3240                 /*
3241                  * vgacon may change the default driver to point
3242                  * to dummycon, we restore it here...
3243                  */
3244                 conswitchp = defconsw;
3245         }
3246
3247         if (!con_is_bound(csw))
3248                 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3249
3250         /* ignore return value, binding should not fail */
3251         do_bind_con_driver(defcsw, first, last, deflt);
3252 err:
3253         module_put(owner);
3254         return retval;
3255
3256 }
3257 EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3258
3259 static int vt_bind(struct con_driver *con)
3260 {
3261         const struct consw *defcsw = NULL, *csw = NULL;
3262         int i, more = 1, first = -1, last = -1, deflt = 0;
3263
3264         if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3265             con_is_graphics(con->con, con->first, con->last))
3266                 goto err;
3267
3268         csw = con->con;
3269
3270         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3271                 struct con_driver *con = &registered_con_driver[i];
3272
3273                 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3274                         defcsw = con->con;
3275                         break;
3276                 }
3277         }
3278
3279         if (!defcsw)
3280                 goto err;
3281
3282         while (more) {
3283                 more = 0;
3284
3285                 for (i = con->first; i <= con->last; i++) {
3286                         if (con_driver_map[i] == defcsw) {
3287                                 if (first == -1)
3288                                         first = i;
3289                                 last = i;
3290                                 more = 1;
3291                         } else if (first != -1)
3292                                 break;
3293                 }
3294
3295                 if (first == 0 && last == MAX_NR_CONSOLES -1)
3296                         deflt = 1;
3297
3298                 if (first != -1)
3299                         bind_con_driver(csw, first, last, deflt);
3300
3301                 first = -1;
3302                 last = -1;
3303                 deflt = 0;
3304         }
3305
3306 err:
3307         return 0;
3308 }
3309
3310 static int vt_unbind(struct con_driver *con)
3311 {
3312         const struct consw *csw = NULL;
3313         int i, more = 1, first = -1, last = -1, deflt = 0;
3314
3315         if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3316             con_is_graphics(con->con, con->first, con->last))
3317                 goto err;
3318
3319         csw = con->con;
3320
3321         while (more) {
3322                 more = 0;
3323
3324                 for (i = con->first; i <= con->last; i++) {
3325                         if (con_driver_map[i] == csw) {
3326                                 if (first == -1)
3327                                         first = i;
3328                                 last = i;
3329                                 more = 1;
3330                         } else if (first != -1)
3331                                 break;
3332                 }
3333
3334                 if (first == 0 && last == MAX_NR_CONSOLES -1)
3335                         deflt = 1;
3336
3337                 if (first != -1)
3338                         unbind_con_driver(csw, first, last, deflt);
3339
3340                 first = -1;
3341                 last = -1;
3342                 deflt = 0;
3343         }
3344
3345 err:
3346         return 0;
3347 }
3348 #else
3349 static inline int vt_bind(struct con_driver *con)
3350 {
3351         return 0;
3352 }
3353 static inline int vt_unbind(struct con_driver *con)
3354 {
3355         return 0;
3356 }
3357 #endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3358
3359 static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3360                           const char *buf, size_t count)
3361 {
3362         struct con_driver *con = dev_get_drvdata(dev);
3363         int bind = simple_strtoul(buf, NULL, 0);
3364
3365         if (bind)
3366                 vt_bind(con);
3367         else
3368                 vt_unbind(con);
3369
3370         return count;
3371 }
3372
3373 static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3374                          char *buf)
3375 {
3376         struct con_driver *con = dev_get_drvdata(dev);
3377         int bind = con_is_bound(con->con);
3378
3379         return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3380 }
3381
3382 static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3383                          char *buf)
3384 {
3385         struct con_driver *con = dev_get_drvdata(dev);
3386
3387         return snprintf(buf, PAGE_SIZE, "%s %s\n",
3388                         (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3389                          con->desc);
3390
3391 }
3392
3393 static struct device_attribute device_attrs[] = {
3394         __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3395         __ATTR(name, S_IRUGO, show_name, NULL),
3396 };
3397
3398 static int vtconsole_init_device(struct con_driver *con)
3399 {
3400         int i;
3401         int error = 0;
3402
3403         con->flag |= CON_DRIVER_FLAG_ATTR;
3404         dev_set_drvdata(con->dev, con);
3405         for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3406                 error = device_create_file(con->dev, &device_attrs[i]);
3407                 if (error)
3408                         break;
3409         }
3410
3411         if (error) {
3412                 while (--i >= 0)
3413                         device_remove_file(con->dev, &device_attrs[i]);
3414                 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3415         }
3416
3417         return error;
3418 }
3419
3420 static void vtconsole_deinit_device(struct con_driver *con)
3421 {
3422         int i;
3423
3424         if (con->flag & CON_DRIVER_FLAG_ATTR) {
3425                 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3426                         device_remove_file(con->dev, &device_attrs[i]);
3427                 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3428         }
3429 }
3430
3431 /**
3432  * con_is_bound - checks if driver is bound to the console
3433  * @csw: console driver
3434  *
3435  * RETURNS: zero if unbound, nonzero if bound
3436  *
3437  * Drivers can call this and if zero, they should release
3438  * all resources allocated on con_startup()
3439  */
3440 int con_is_bound(const struct consw *csw)
3441 {
3442         int i, bound = 0;
3443
3444         for (i = 0; i < MAX_NR_CONSOLES; i++) {
3445                 if (con_driver_map[i] == csw) {
3446                         bound = 1;
3447                         break;
3448                 }
3449         }
3450
3451         return bound;
3452 }
3453 EXPORT_SYMBOL(con_is_bound);
3454
3455 /**
3456  * con_debug_enter - prepare the console for the kernel debugger
3457  * @sw: console driver
3458  *
3459  * Called when the console is taken over by the kernel debugger, this
3460  * function needs to save the current console state, then put the console
3461  * into a state suitable for the kernel debugger.
3462  *
3463  * RETURNS:
3464  * Zero on success, nonzero if a failure occurred when trying to prepare
3465  * the console for the debugger.
3466  */
3467 int con_debug_enter(struct vc_data *vc)
3468 {
3469         int ret = 0;
3470
3471         saved_fg_console = fg_console;
3472         saved_last_console = last_console;
3473         saved_want_console = want_console;
3474         saved_vc_mode = vc->vc_mode;
3475         saved_console_blanked = console_blanked;
3476         vc->vc_mode = KD_TEXT;
3477         console_blanked = 0;
3478         if (vc->vc_sw->con_debug_enter)
3479                 ret = vc->vc_sw->con_debug_enter(vc);
3480 #ifdef CONFIG_KGDB_KDB
3481         /* Set the initial LINES variable if it is not already set */
3482         if (vc->vc_rows < 999) {
3483                 int linecount;
3484                 char lns[4];
3485                 const char *setargs[3] = {
3486                         "set",
3487                         "LINES",
3488                         lns,
3489                 };
3490                 if (kdbgetintenv(setargs[0], &linecount)) {
3491                         snprintf(lns, 4, "%i", vc->vc_rows);
3492                         kdb_set(2, setargs);
3493                 }
3494         }
3495 #endif /* CONFIG_KGDB_KDB */
3496         return ret;
3497 }
3498 EXPORT_SYMBOL_GPL(con_debug_enter);
3499
3500 /**
3501  * con_debug_leave - restore console state
3502  * @sw: console driver
3503  *
3504  * Restore the console state to what it was before the kernel debugger
3505  * was invoked.
3506  *
3507  * RETURNS:
3508  * Zero on success, nonzero if a failure occurred when trying to restore
3509  * the console.
3510  */
3511 int con_debug_leave(void)
3512 {
3513         struct vc_data *vc;
3514         int ret = 0;
3515
3516         fg_console = saved_fg_console;
3517         last_console = saved_last_console;
3518         want_console = saved_want_console;
3519         console_blanked = saved_console_blanked;
3520         vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3521
3522         vc = vc_cons[fg_console].d;
3523         if (vc->vc_sw->con_debug_leave)
3524                 ret = vc->vc_sw->con_debug_leave(vc);
3525         return ret;
3526 }
3527 EXPORT_SYMBOL_GPL(con_debug_leave);
3528
3529 static int do_register_con_driver(const struct consw *csw, int first, int last)
3530 {
3531         struct module *owner = csw->owner;
3532         struct con_driver *con_driver;
3533         const char *desc;
3534         int i, retval = 0;
3535
3536         WARN_CONSOLE_UNLOCKED();
3537
3538         if (!try_module_get(owner))
3539                 return -ENODEV;
3540
3541         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3542                 con_driver = &registered_con_driver[i];
3543
3544                 /* already registered */
3545                 if (con_driver->con == csw)
3546                         retval = -EBUSY;
3547         }
3548
3549         if (retval)
3550                 goto err;
3551
3552         desc = csw->con_startup();
3553         if (!desc) {
3554                 retval = -ENODEV;
3555                 goto err;
3556         }
3557
3558         retval = -EINVAL;
3559
3560         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3561                 con_driver = &registered_con_driver[i];
3562
3563                 if (con_driver->con == NULL) {
3564                         con_driver->con = csw;
3565                         con_driver->desc = desc;
3566                         con_driver->node = i;
3567                         con_driver->flag = CON_DRIVER_FLAG_MODULE |
3568                                            CON_DRIVER_FLAG_INIT;
3569                         con_driver->first = first;
3570                         con_driver->last = last;
3571                         retval = 0;
3572                         break;
3573                 }
3574         }
3575
3576         if (retval)
3577                 goto err;
3578
3579         con_driver->dev = device_create(vtconsole_class, NULL,
3580                                                 MKDEV(0, con_driver->node),
3581                                                 NULL, "vtcon%i",
3582                                                 con_driver->node);
3583
3584         if (IS_ERR(con_driver->dev)) {
3585                 printk(KERN_WARNING "Unable to create device for %s; "
3586                        "errno = %ld\n", con_driver->desc,
3587                        PTR_ERR(con_driver->dev));
3588                 con_driver->dev = NULL;
3589         } else {
3590                 vtconsole_init_device(con_driver);
3591         }
3592
3593 err:
3594         module_put(owner);
3595         return retval;
3596 }
3597
3598 /**
3599  * register_con_driver - register console driver to console layer
3600  * @csw: console driver
3601  * @first: the first console to take over, minimum value is 0
3602  * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3603  *
3604  * DESCRIPTION: This function registers a console driver which can later
3605  * bind to a range of consoles specified by @first and @last. It will
3606  * also initialize the console driver by calling con_startup().
3607  */
3608 int register_con_driver(const struct consw *csw, int first, int last)
3609 {
3610         int retval;
3611
3612         console_lock();
3613         retval = do_register_con_driver(csw, first, last);
3614         console_unlock();
3615         return retval;
3616 }
3617 EXPORT_SYMBOL(register_con_driver);
3618
3619 /**
3620  * unregister_con_driver - unregister console driver from console layer
3621  * @csw: console driver
3622  *
3623  * DESCRIPTION: All drivers that registers to the console layer must
3624  * call this function upon exit, or if the console driver is in a state
3625  * where it won't be able to handle console services, such as the
3626  * framebuffer console without loaded framebuffer drivers.
3627  *
3628  * The driver must unbind first prior to unregistration.
3629  */
3630 int unregister_con_driver(const struct consw *csw)
3631 {
3632         int retval;
3633
3634         console_lock();
3635         retval = do_unregister_con_driver(csw);
3636         console_unlock();
3637         return retval;
3638 }
3639 EXPORT_SYMBOL(unregister_con_driver);
3640
3641 int do_unregister_con_driver(const struct consw *csw)
3642 {
3643         int i, retval = -ENODEV;
3644
3645         /* cannot unregister a bound driver */
3646         if (con_is_bound(csw))
3647                 goto err;
3648
3649         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3650                 struct con_driver *con_driver = &registered_con_driver[i];
3651
3652                 if (con_driver->con == csw &&
3653                     con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3654                         vtconsole_deinit_device(con_driver);
3655                         device_destroy(vtconsole_class,
3656                                        MKDEV(0, con_driver->node));
3657                         con_driver->con = NULL;
3658                         con_driver->desc = NULL;
3659                         con_driver->dev = NULL;
3660                         con_driver->node = 0;
3661                         con_driver->flag = 0;
3662                         con_driver->first = 0;
3663                         con_driver->last = 0;
3664                         retval = 0;
3665                         break;
3666                 }
3667         }
3668 err:
3669         return retval;
3670 }
3671 EXPORT_SYMBOL_GPL(do_unregister_con_driver);
3672
3673 /*
3674  *      If we support more console drivers, this function is used
3675  *      when a driver wants to take over some existing consoles
3676  *      and become default driver for newly opened ones.
3677  *
3678  *      take_over_console is basically a register followed by unbind
3679  */
3680 int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3681 {
3682         int err;
3683
3684         err = do_register_con_driver(csw, first, last);
3685         /*
3686          * If we get an busy error we still want to bind the console driver
3687          * and return success, as we may have unbound the console driver
3688          * but not unregistered it.
3689          */
3690         if (err == -EBUSY)
3691                 err = 0;
3692         if (!err)
3693                 do_bind_con_driver(csw, first, last, deflt);
3694
3695         return err;
3696 }
3697 EXPORT_SYMBOL_GPL(do_take_over_console);
3698
3699 /*
3700  *      If we support more console drivers, this function is used
3701  *      when a driver wants to take over some existing consoles
3702  *      and become default driver for newly opened ones.
3703  *
3704  *      take_over_console is basically a register followed by unbind
3705  */
3706 int take_over_console(const struct consw *csw, int first, int last, int deflt)
3707 {
3708         int err;
3709
3710         err = register_con_driver(csw, first, last);
3711         /*
3712          * If we get an busy error we still want to bind the console driver
3713          * and return success, as we may have unbound the console driver
3714          * but not unregistered it.
3715          */
3716         if (err == -EBUSY)
3717                 err = 0;
3718         if (!err)
3719                 bind_con_driver(csw, first, last, deflt);
3720
3721         return err;
3722 }
3723
3724 /*
3725  * give_up_console is a wrapper to unregister_con_driver. It will only
3726  * work if driver is fully unbound.
3727  */
3728 void give_up_console(const struct consw *csw)
3729 {
3730         unregister_con_driver(csw);
3731 }
3732
3733 static int __init vtconsole_class_init(void)
3734 {
3735         int i;
3736
3737         vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3738         if (IS_ERR(vtconsole_class)) {
3739                 printk(KERN_WARNING "Unable to create vt console class; "
3740                        "errno = %ld\n", PTR_ERR(vtconsole_class));
3741                 vtconsole_class = NULL;
3742         }
3743
3744         /* Add system drivers to sysfs */
3745         for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3746                 struct con_driver *con = &registered_con_driver[i];
3747
3748                 if (con->con && !con->dev) {
3749                         con->dev = device_create(vtconsole_class, NULL,
3750                                                          MKDEV(0, con->node),
3751                                                          NULL, "vtcon%i",
3752                                                          con->node);
3753
3754                         if (IS_ERR(con->dev)) {
3755                                 printk(KERN_WARNING "Unable to create "
3756                                        "device for %s; errno = %ld\n",
3757                                        con->desc, PTR_ERR(con->dev));
3758                                 con->dev = NULL;
3759                         } else {
3760                                 vtconsole_init_device(con);
3761                         }
3762                 }
3763         }
3764
3765         return 0;
3766 }
3767 postcore_initcall(vtconsole_class_init);
3768
3769 #endif
3770
3771 /*
3772  *      Screen blanking
3773  */
3774
3775 static int set_vesa_blanking(char __user *p)
3776 {
3777         unsigned int mode;
3778
3779         if (get_user(mode, p + 1))
3780                 return -EFAULT;
3781
3782         vesa_blank_mode = (mode < 4) ? mode : 0;
3783         return 0;
3784 }
3785
3786 void do_blank_screen(int entering_gfx)
3787 {
3788         struct vc_data *vc = vc_cons[fg_console].d;
3789         int i;
3790
3791         WARN_CONSOLE_UNLOCKED();
3792
3793         if (console_blanked) {
3794                 if (blank_state == blank_vesa_wait) {
3795                         blank_state = blank_off;
3796                         vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
3797                 }
3798                 return;
3799         }
3800
3801         /* entering graphics mode? */
3802         if (entering_gfx) {
3803                 hide_cursor(vc);
3804                 save_screen(vc);
3805                 vc->vc_sw->con_blank(vc, -1, 1);
3806                 console_blanked = fg_console + 1;
3807                 blank_state = blank_off;
3808                 set_origin(vc);
3809                 return;
3810         }
3811
3812         if (blank_state != blank_normal_wait)
3813                 return;
3814         blank_state = blank_off;
3815
3816         /* don't blank graphics */
3817         if (vc->vc_mode != KD_TEXT) {
3818                 console_blanked = fg_console + 1;
3819                 return;
3820         }
3821
3822         hide_cursor(vc);
3823         del_timer_sync(&console_timer);
3824         blank_timer_expired = 0;
3825
3826         save_screen(vc);
3827         /* In case we need to reset origin, blanking hook returns 1 */
3828         i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
3829         console_blanked = fg_console + 1;
3830         if (i)
3831                 set_origin(vc);
3832
3833         if (console_blank_hook && console_blank_hook(1))
3834                 return;
3835
3836         if (vesa_off_interval && vesa_blank_mode) {
3837                 blank_state = blank_vesa_wait;
3838                 mod_timer(&console_timer, jiffies + vesa_off_interval);
3839         }
3840         vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
3841 }
3842 EXPORT_SYMBOL(do_blank_screen);
3843
3844 /*
3845  * Called by timer as well as from vt_console_driver
3846  */
3847 void do_unblank_screen(int leaving_gfx)
3848 {
3849         struct vc_data *vc;
3850
3851         /* This should now always be called from a "sane" (read: can schedule)
3852          * context for the sake of the low level drivers, except in the special
3853          * case of oops_in_progress
3854          */
3855         if (!oops_in_progress)
3856                 might_sleep();
3857
3858         WARN_CONSOLE_UNLOCKED();
3859
3860         ignore_poke = 0;
3861         if (!console_blanked)
3862                 return;
3863         if (!vc_cons_allocated(fg_console)) {
3864                 /* impossible */
3865                 pr_warning("unblank_screen: tty %d not allocated ??\n",
3866                            fg_console+1);
3867                 return;
3868         }
3869         vc = vc_cons[fg_console].d;
3870         /* Try to unblank in oops case too */
3871         if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
3872                 return; /* but leave console_blanked != 0 */
3873
3874         if (blankinterval) {
3875                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3876                 blank_state = blank_normal_wait;
3877         }
3878
3879         console_blanked = 0;
3880         if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
3881                 /* Low-level driver cannot restore -> do it ourselves */
3882                 update_screen(vc);
3883         if (console_blank_hook)
3884                 console_blank_hook(0);
3885         set_palette(vc);
3886         set_cursor(vc);
3887         vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
3888 }
3889 EXPORT_SYMBOL(do_unblank_screen);
3890
3891 /*
3892  * This is called by the outside world to cause a forced unblank, mostly for
3893  * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3894  * call it with 1 as an argument and so force a mode restore... that may kill
3895  * X or at least garbage the screen but would also make the Oops visible...
3896  */
3897 void unblank_screen(void)
3898 {
3899         do_unblank_screen(0);
3900 }
3901
3902 /*
3903  * We defer the timer blanking to work queue so it can take the console mutex
3904  * (console operations can still happen at irq time, but only from printk which
3905  * has the console mutex. Not perfect yet, but better than no locking
3906  */
3907 static void blank_screen_t(unsigned long dummy)
3908 {
3909         if (unlikely(!keventd_up())) {
3910                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3911                 return;
3912         }
3913         blank_timer_expired = 1;
3914         schedule_work(&console_work);
3915 }
3916
3917 void poke_blanked_console(void)
3918 {
3919         WARN_CONSOLE_UNLOCKED();
3920
3921         /* Add this so we quickly catch whoever might call us in a non
3922          * safe context. Nowadays, unblank_screen() isn't to be called in
3923          * atomic contexts and is allowed to schedule (with the special case
3924          * of oops_in_progress, but that isn't of any concern for this
3925          * function. --BenH.
3926          */
3927         might_sleep();
3928
3929         /* This isn't perfectly race free, but a race here would be mostly harmless,
3930          * at worse, we'll do a spurrious blank and it's unlikely
3931          */
3932         del_timer(&console_timer);
3933         blank_timer_expired = 0;
3934
3935         if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3936                 return;
3937         if (console_blanked)
3938                 unblank_screen();
3939         else if (blankinterval) {
3940                 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3941                 blank_state = blank_normal_wait;
3942         }
3943 }
3944
3945 /*
3946  *      Palettes
3947  */
3948
3949 static void set_palette(struct vc_data *vc)
3950 {
3951         WARN_CONSOLE_UNLOCKED();
3952
3953         if (vc->vc_mode != KD_GRAPHICS)
3954                 vc->vc_sw->con_set_palette(vc, color_table);
3955 }
3956
3957 static int set_get_cmap(unsigned char __user *arg, int set)
3958 {
3959     int i, j, k;
3960
3961     WARN_CONSOLE_UNLOCKED();
3962
3963     for (i = 0; i < 16; i++)
3964         if (set) {
3965             get_user(default_red[i], arg++);
3966             get_user(default_grn[i], arg++);
3967             get_user(default_blu[i], arg++);
3968         } else {
3969             put_user(default_red[i], arg++);
3970             put_user(default_grn[i], arg++);
3971             put_user(default_blu[i], arg++);
3972         }
3973     if (set) {
3974         for (i = 0; i < MAX_NR_CONSOLES; i++)
3975             if (vc_cons_allocated(i)) {
3976                 for (j = k = 0; j < 16; j++) {
3977                     vc_cons[i].d->vc_palette[k++] = default_red[j];
3978                     vc_cons[i].d->vc_palette[k++] = default_grn[j];
3979                     vc_cons[i].d->vc_palette[k++] = default_blu[j];
3980                 }
3981                 set_palette(vc_cons[i].d);
3982             }
3983     }
3984     return 0;
3985 }
3986
3987 /*
3988  * Load palette into the DAC registers. arg points to a colour
3989  * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3990  */
3991
3992 int con_set_cmap(unsigned char __user *arg)
3993 {
3994         int rc;
3995
3996         console_lock();
3997         rc = set_get_cmap (arg,1);
3998         console_unlock();
3999
4000         return rc;
4001 }
4002
4003 int con_get_cmap(unsigned char __user *arg)
4004 {
4005         int rc;
4006
4007         console_lock();
4008         rc = set_get_cmap (arg,0);
4009         console_unlock();
4010
4011         return rc;
4012 }
4013
4014 void reset_palette(struct vc_data *vc)
4015 {
4016         int j, k;
4017         for (j=k=0; j<16; j++) {
4018                 vc->vc_palette[k++] = default_red[j];
4019                 vc->vc_palette[k++] = default_grn[j];
4020                 vc->vc_palette[k++] = default_blu[j];
4021         }
4022         set_palette(vc);
4023 }
4024
4025 /*
4026  *  Font switching
4027  *
4028  *  Currently we only support fonts up to 32 pixels wide, at a maximum height
4029  *  of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints, 
4030  *  depending on width) reserved for each character which is kinda wasty, but 
4031  *  this is done in order to maintain compatibility with the EGA/VGA fonts. It 
4032  *  is up to the actual low-level console-driver convert data into its favorite
4033  *  format (maybe we should add a `fontoffset' field to the `display'
4034  *  structure so we won't have to convert the fontdata all the time.
4035  *  /Jes
4036  */
4037
4038 #define max_font_size 65536
4039
4040 static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4041 {
4042         struct console_font font;
4043         int rc = -EINVAL;
4044         int c;
4045
4046         if (vc->vc_mode != KD_TEXT)
4047                 return -EINVAL;
4048
4049         if (op->data) {
4050                 font.data = kmalloc(max_font_size, GFP_KERNEL);
4051                 if (!font.data)
4052                         return -ENOMEM;
4053         } else
4054                 font.data = NULL;
4055
4056         console_lock();
4057         if (vc->vc_sw->con_font_get)
4058                 rc = vc->vc_sw->con_font_get(vc, &font);
4059         else
4060                 rc = -ENOSYS;
4061         console_unlock();
4062
4063         if (rc)
4064                 goto out;
4065
4066         c = (font.width+7)/8 * 32 * font.charcount;
4067
4068         if (op->data && font.charcount > op->charcount)
4069                 rc = -ENOSPC;
4070         if (!(op->flags & KD_FONT_FLAG_OLD)) {
4071                 if (font.width > op->width || font.height > op->height) 
4072                         rc = -ENOSPC;
4073         } else {
4074                 if (font.width != 8)
4075                         rc = -EIO;
4076                 else if ((op->height && font.height > op->height) ||
4077                          font.height > 32)
4078                         rc = -ENOSPC;
4079         }
4080         if (rc)
4081                 goto out;
4082
4083         op->height = font.height;
4084         op->width = font.width;
4085         op->charcount = font.charcount;
4086
4087         if (op->data && copy_to_user(op->data, font.data, c))
4088                 rc = -EFAULT;
4089
4090 out:
4091         kfree(font.data);
4092         return rc;
4093 }
4094
4095 static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4096 {
4097         struct console_font font;
4098         int rc = -EINVAL;
4099         int size;
4100
4101         if (vc->vc_mode != KD_TEXT)
4102                 return -EINVAL;
4103         if (!op->data)
4104                 return -EINVAL;
4105         if (op->charcount > 512)
4106                 return -EINVAL;
4107         if (!op->height) {              /* Need to guess font height [compat] */
4108                 int h, i;
4109                 u8 __user *charmap = op->data;
4110                 u8 tmp;
4111                 
4112                 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4113                    so that we can get rid of this soon */
4114                 if (!(op->flags & KD_FONT_FLAG_OLD))
4115                         return -EINVAL;
4116                 for (h = 32; h > 0; h--)
4117                         for (i = 0; i < op->charcount; i++) {
4118                                 if (get_user(tmp, &charmap[32*i+h-1]))
4119                                         return -EFAULT;
4120                                 if (tmp)
4121                                         goto nonzero;
4122                         }
4123                 return -EINVAL;
4124         nonzero:
4125                 op->height = h;
4126         }
4127         if (op->width <= 0 || op->width > 32 || op->height > 32)
4128                 return -EINVAL;
4129         size = (op->width+7)/8 * 32 * op->charcount;
4130         if (size > max_font_size)
4131                 return -ENOSPC;
4132         font.charcount = op->charcount;
4133         font.height = op->height;
4134         font.width = op->width;
4135         font.data = memdup_user(op->data, size);
4136         if (IS_ERR(font.data))
4137                 return PTR_ERR(font.data);
4138         console_lock();
4139         if (vc->vc_sw->con_font_set)
4140                 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4141         else
4142                 rc = -ENOSYS;
4143         console_unlock();
4144         kfree(font.data);
4145         return rc;
4146 }
4147
4148 static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4149 {
4150         struct console_font font = {.width = op->width, .height = op->height};
4151         char name[MAX_FONT_NAME];
4152         char *s = name;
4153         int rc;
4154
4155         if (vc->vc_mode != KD_TEXT)
4156                 return -EINVAL;
4157
4158         if (!op->data)
4159                 s = NULL;
4160         else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4161                 return -EFAULT;
4162         else
4163                 name[MAX_FONT_NAME - 1] = 0;
4164
4165         console_lock();
4166         if (vc->vc_sw->con_font_default)
4167                 rc = vc->vc_sw->con_font_default(vc, &font, s);
4168         else
4169                 rc = -ENOSYS;
4170         console_unlock();
4171         if (!rc) {
4172                 op->width = font.width;
4173                 op->height = font.height;
4174         }
4175         return rc;
4176 }
4177
4178 static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4179 {
4180         int con = op->height;
4181         int rc;
4182
4183         if (vc->vc_mode != KD_TEXT)
4184                 return -EINVAL;
4185
4186         console_lock();
4187         if (!vc->vc_sw->con_font_copy)
4188                 rc = -ENOSYS;
4189         else if (con < 0 || !vc_cons_allocated(con))
4190                 rc = -ENOTTY;
4191         else if (con == vc->vc_num)     /* nothing to do */
4192                 rc = 0;
4193         else
4194                 rc = vc->vc_sw->con_font_copy(vc, con);
4195         console_unlock();
4196         return rc;
4197 }
4198
4199 int con_font_op(struct vc_data *vc, struct console_font_op *op)
4200 {
4201         switch (op->op) {
4202         case KD_FONT_OP_SET:
4203                 return con_font_set(vc, op);
4204         case KD_FONT_OP_GET:
4205                 return con_font_get(vc, op);
4206         case KD_FONT_OP_SET_DEFAULT:
4207                 return con_font_default(vc, op);
4208         case KD_FONT_OP_COPY:
4209                 return con_font_copy(vc, op);
4210         }
4211         return -ENOSYS;
4212 }
4213
4214 /*
4215  *      Interface exported to selection and vcs.
4216  */
4217
4218 /* used by selection */
4219 u16 screen_glyph(struct vc_data *vc, int offset)
4220 {
4221         u16 w = scr_readw(screenpos(vc, offset, 1));
4222         u16 c = w & 0xff;
4223
4224         if (w & vc->vc_hi_font_mask)
4225                 c |= 0x100;
4226         return c;
4227 }
4228 EXPORT_SYMBOL_GPL(screen_glyph);
4229
4230 /* used by vcs - note the word offset */
4231 unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4232 {
4233         return screenpos(vc, 2 * w_offset, viewed);
4234 }
4235
4236 void getconsxy(struct vc_data *vc, unsigned char *p)
4237 {
4238         p[0] = vc->vc_x;
4239         p[1] = vc->vc_y;
4240 }
4241
4242 void putconsxy(struct vc_data *vc, unsigned char *p)
4243 {
4244         hide_cursor(vc);
4245         gotoxy(vc, p[0], p[1]);
4246         set_cursor(vc);
4247 }
4248
4249 u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4250 {
4251         if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4252                 return softcursor_original;
4253         return scr_readw(org);
4254 }
4255
4256 void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4257 {
4258         scr_writew(val, org);
4259         if ((unsigned long)org == vc->vc_pos) {
4260                 softcursor_original = -1;
4261                 add_softcursor(vc);
4262         }
4263 }
4264
4265 void vcs_scr_updated(struct vc_data *vc)
4266 {
4267         notify_update(vc);
4268 }
4269
4270 /*
4271  *      Visible symbols for modules
4272  */
4273
4274 EXPORT_SYMBOL(color_table);
4275 EXPORT_SYMBOL(default_red);
4276 EXPORT_SYMBOL(default_grn);
4277 EXPORT_SYMBOL(default_blu);
4278 EXPORT_SYMBOL(update_region);
4279 EXPORT_SYMBOL(redraw_screen);
4280 EXPORT_SYMBOL(vc_resize);
4281 EXPORT_SYMBOL(fg_console);
4282 EXPORT_SYMBOL(console_blank_hook);
4283 EXPORT_SYMBOL(console_blanked);
4284 EXPORT_SYMBOL(vc_cons);
4285 EXPORT_SYMBOL(global_cursor_default);
4286 #ifndef VT_SINGLE_DRIVER
4287 EXPORT_SYMBOL(take_over_console);
4288 EXPORT_SYMBOL(give_up_console);
4289 #endif