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