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