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