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