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