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