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