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