Import 2.2.0pre6
[davej-history.git] / drivers / char / console.c
blob88f959138a5509ff7dcfb1994e4f73858211de58
1 /*
2 * linux/drivers/char/console.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Hopefully this will be a rather complete VT102 implementation.
10 * Beeping thanks to John T Kohl.
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <Geert.Uytterhoeven@cs.kuleuven.ac.be>, Jan 1997.
38 * Original m68k console driver modifications by
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
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):
47 * - video_num_columns
48 * - video_num_lines
49 * - video_size_row
50 * - can_do_color
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).
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).
61 * Support for changeable cursor shape
62 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
64 * Ported to i386 and con_scrolldelta fixed
65 * by Emmanuel Marty <core@ggi-project.org>, April 1998
67 * Resurrected character buffers in videoram plus lots of other trickery
68 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
71 #include <linux/module.h>
72 #include <linux/sched.h>
73 #include <linux/tty.h>
74 #include <linux/tty_flip.h>
75 #include <linux/kernel.h>
76 #include <linux/string.h>
77 #include <linux/errno.h>
78 #include <linux/kd.h>
79 #include <linux/malloc.h>
80 #include <linux/major.h>
81 #include <linux/mm.h>
82 #include <linux/console.h>
83 #include <linux/init.h>
84 #include <linux/vt_kern.h>
85 #include <linux/selection.h>
86 #include <linux/console_struct.h>
87 #include <linux/kbd_kern.h>
88 #include <linux/vt_kern.h>
89 #include <linux/consolemap.h>
90 #include <linux/timer.h>
91 #include <linux/interrupt.h>
92 #include <linux/config.h>
93 #include <linux/version.h>
94 #include <linux/tqueue.h>
95 #ifdef CONFIG_APM
96 #include <linux/apm_bios.h>
97 #endif
99 #include <asm/io.h>
100 #include <asm/system.h>
101 #include <asm/uaccess.h>
102 #include <asm/bitops.h>
104 #include <asm/linux_logo.h>
106 #include "console_macros.h"
109 struct consw *conswitchp = NULL;
111 /* A bitmap for codes <32. A bit of 1 indicates that the code
112 * corresponding to that bit number invokes some special action
113 * (such as cursor movement) and should not be displayed as a
114 * glyph unless the disp_ctrl mode is explicitly enabled.
116 #define CTRL_ACTION 0x0d00ff81
117 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
120 * Here is the default bell parameters: 750HZ, 1/8th of a second
122 #define DEFAULT_BELL_PITCH 750
123 #define DEFAULT_BELL_DURATION (HZ/8)
125 #ifndef MIN
126 #define MIN(a,b) ((a) < (b) ? (a) : (b))
127 #endif
129 static struct tty_struct *console_table[MAX_NR_CONSOLES];
130 static struct termios *console_termios[MAX_NR_CONSOLES];
131 static struct termios *console_termios_locked[MAX_NR_CONSOLES];
132 struct vc vc_cons [MAX_NR_CONSOLES];
134 #ifndef VT_SINGLE_DRIVER
135 static struct consw *con_driver_map[MAX_NR_CONSOLES];
136 #endif
138 static int con_open(struct tty_struct *, struct file *);
139 static void vc_init(unsigned int console, unsigned int rows,
140 unsigned int cols, int do_clear);
141 static void blank_screen(void);
142 static void gotoxy(int currcons, int new_x, int new_y);
143 static void save_cur(int currcons);
144 static void reset_terminal(int currcons, int do_clear);
145 static void con_flush_chars(struct tty_struct *tty);
146 static void set_vesa_blanking(unsigned long arg);
147 static void set_cursor(int currcons);
148 static void hide_cursor(int currcons);
150 static int printable = 0; /* Is console ready for printing? */
152 int do_poke_blanked_console = 0;
153 int console_blanked = 0;
155 static int vesa_blank_mode = 0; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
156 static int blankinterval = 10*60*HZ;
157 static int vesa_off_interval = 0;
160 * fg_console is the current virtual console,
161 * last_console is the last used one,
162 * want_console is the console we want to switch to,
163 * kmsg_redirect is the console for kernel messages,
165 int fg_console = 0;
166 int last_console = 0;
167 int want_console = -1;
168 int kmsg_redirect = 0;
171 * For each existing display, we have a pointer to console currently visible
172 * on that display, allowing consoles other than fg_console to be refreshed
173 * appropriately. Unless the low-level driver supplies its own display_fg
174 * variable, we use this one for the "master display".
176 static struct vc_data *master_display_fg = NULL;
179 * Unfortunately, we need to delay tty echo when we're currently writing to the
180 * console since the code is (and always was) not re-entrant, so we insert
181 * all filp requests to con_task_queue instead of tq_timer and run it from
182 * the console_bh.
184 DECLARE_TASK_QUEUE(con_task_queue);
187 * For the same reason, we defer scrollback to the console_bh.
189 static int scrollback_delta = 0;
192 * Low-Level Functions
195 #define IS_FG (currcons == fg_console)
196 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
198 #ifdef VT_BUF_VRAM_ONLY
199 #define DO_UPDATE 0
200 #else
201 #define DO_UPDATE IS_VISIBLE
202 #endif
204 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
206 unsigned short *p = (unsigned short *)(visible_origin + offset);
207 return p;
210 static inline void scrolldelta(int lines)
212 scrollback_delta += lines;
213 mark_bh(CONSOLE_BH);
216 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
218 unsigned short *d, *s;
220 if (t+nr >= b)
221 nr = b - t - 1;
222 if (b > video_num_lines || t >= b || nr < 1)
223 return;
224 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
225 return;
226 d = (unsigned short *) (origin+video_size_row*t);
227 s = (unsigned short *) (origin+video_size_row*(t+nr));
228 scr_memcpyw(d, s, (b-t-nr) * video_size_row);
229 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
232 static void
233 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
235 unsigned short *s;
236 unsigned int step;
238 if (t+nr >= b)
239 nr = b - t - 1;
240 if (b > video_num_lines || t >= b || nr < 1)
241 return;
242 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
243 return;
244 s = (unsigned short *) (origin+video_size_row*t);
245 step = video_num_columns * nr;
246 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
247 scr_memsetw(s, video_erase_char, 2*step);
250 static void do_update_region(int currcons, unsigned long start, int count)
252 #ifndef VT_BUF_VRAM_ONLY
253 unsigned int xx, yy, offset;
254 u16 *p;
256 if (start < origin) {
257 count -= origin - start;
258 start = origin;
260 if (count <= 0)
261 return;
262 offset = (start - origin) / 2;
263 xx = offset % video_num_columns;
264 yy = offset / video_num_columns;
265 p = (u16 *) start;
266 for(;;) {
267 u16 attrib = scr_readw(p) & 0xff00;
268 int startx = xx;
269 u16 *q = p;
270 while (xx < video_num_columns && count) {
271 if (attrib != (scr_readw(p) & 0xff00)) {
272 if (p > q)
273 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
274 startx = xx;
275 q = p;
276 attrib = scr_readw(p) & 0xff00;
278 p++;
279 xx++;
280 count--;
282 if (p > q)
283 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
284 if (!count)
285 break;
286 xx = 0;
287 yy++;
289 #endif
292 void update_region(int currcons, unsigned long start, int count)
294 if (DO_UPDATE) {
295 hide_cursor(currcons);
296 do_update_region(currcons, start, count);
297 set_cursor(currcons);
301 /* Structure of attributes is hardware-dependent */
303 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
305 if (sw->con_build_attr)
306 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
308 #ifndef VT_BUF_VRAM_ONLY
310 * ++roman: I completely changed the attribute format for monochrome
311 * mode (!can_do_color). The formerly used MDA (monochrome display
312 * adapter) format didn't allow the combination of certain effects.
313 * Now the attribute is just a bit vector:
314 * Bit 0..1: intensity (0..2)
315 * Bit 2 : underline
316 * Bit 3 : reverse
317 * Bit 7 : blink
320 u8 a = color;
321 if (!can_do_color)
322 return _intensity |
323 (_underline ? 4 : 0) |
324 (_reverse ? 8 : 0) |
325 (_blink ? 0x80 : 0);
326 if (_underline)
327 a = (a & 0xf0) | ulcolor;
328 else if (_intensity == 0)
329 a = (a & 0xf0) | halfcolor;
330 if (_reverse)
331 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
332 if (_blink)
333 a ^= 0x80;
334 if (_intensity == 2)
335 a ^= 0x08;
336 if (hi_font_mask == 0x100)
337 a <<= 1;
338 return a;
340 #else
341 return 0;
342 #endif
345 static void update_attr(int currcons)
347 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
348 video_erase_char = (build_attr(currcons, color, 1, 0, 0, decscnm) << 8) | ' ';
351 /* Note: inverting the screen twice should revert to the original state */
353 void invert_screen(int currcons, int offset, int count, int viewed)
355 unsigned short *p;
357 count /= 2;
358 p = screenpos(currcons, offset, viewed);
359 if (sw->con_invert_region)
360 sw->con_invert_region(vc_cons[currcons].d, p, count);
361 #ifndef VT_BUF_VRAM_ONLY
362 else {
363 u16 *q = p;
364 int cnt = count;
366 if (!can_do_color) {
367 while (cnt--) *q++ ^= 0x0800;
368 } else if (hi_font_mask == 0x100) {
369 while (cnt--) {
370 u16 a = *q;
371 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
372 *q++ = a;
374 } else {
375 while (cnt--) {
376 u16 a = *q;
377 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
378 *q++ = a;
382 #endif
383 if (DO_UPDATE)
384 do_update_region(currcons, (unsigned long) p, count);
387 /* used by selection: complement pointer position */
388 void complement_pos(int currcons, int offset)
390 static unsigned short *p = NULL;
391 static unsigned short old = 0;
392 static unsigned short oldx = 0, oldy = 0;
394 if (p) {
395 scr_writew(old, p);
396 if (DO_UPDATE)
397 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
399 if (offset == -1)
400 p = NULL;
401 else {
402 unsigned short new;
403 p = screenpos(currcons, offset, 1);
404 old = scr_readw(p);
405 new = old ^ complement_mask;
406 scr_writew(new, p);
407 if (DO_UPDATE) {
408 oldx = (offset >> 1) % video_num_columns;
409 oldy = (offset >> 1) / video_num_columns;
410 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
415 static void insert_char(int currcons, unsigned int nr)
417 unsigned short *p, *q = (unsigned short *) pos;
419 p = q + video_num_columns - nr - x;
420 while (--p >= q)
421 scr_writew(scr_readw(p), p + nr);
422 scr_memsetw(q, video_erase_char, nr*2);
423 need_wrap = 0;
424 if (DO_UPDATE) {
425 unsigned short oldattr = attr;
426 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
427 video_num_columns-x-nr);
428 attr = video_erase_char >> 8;
429 while (nr--)
430 sw->con_putc(vc_cons[currcons].d,
431 video_erase_char,y,x+nr);
432 attr = oldattr;
436 static void delete_char(int currcons, unsigned int nr)
438 unsigned int i = x;
439 unsigned short *p = (unsigned short *) pos;
441 while (++i <= video_num_columns - nr) {
442 scr_writew(scr_readw(p+nr), p);
443 p++;
445 scr_memsetw(p, video_erase_char, nr*2);
446 need_wrap = 0;
447 if (DO_UPDATE) {
448 unsigned short oldattr = attr;
449 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
450 video_num_columns-x-nr);
451 attr = video_erase_char >> 8;
452 while (nr--)
453 sw->con_putc(vc_cons[currcons].d,
454 video_erase_char, y,
455 video_num_columns-1-nr);
456 attr = oldattr;
460 static int softcursor_original;
462 static void add_softcursor(int currcons)
464 int i = scr_readw((u16 *) pos);
465 u32 type = cursor_type;
467 if (! (type & 0x10)) return;
468 if (softcursor_original != -1) return;
469 softcursor_original = i;
470 i |= ((type >> 8) & 0xff00 );
471 i ^= ((type) & 0xff00 );
472 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
473 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
474 scr_writew(i, (u16 *) pos);
475 if (DO_UPDATE)
476 sw->con_putc(vc_cons[currcons].d, i, y, x);
479 static void hide_cursor(int currcons)
481 if (currcons == sel_cons)
482 clear_selection();
483 if (softcursor_original != -1) {
484 scr_writew(softcursor_original,(u16 *) pos);
485 if (DO_UPDATE)
486 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
487 softcursor_original = -1;
489 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
492 static void set_cursor(int currcons)
494 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
495 return;
496 if (deccm) {
497 if (currcons == sel_cons)
498 clear_selection();
499 add_softcursor(currcons);
500 if ((cursor_type & 0x0f) != 1)
501 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
502 } else
503 hide_cursor(currcons);
506 static void set_origin(int currcons)
508 if (!IS_VISIBLE ||
509 !sw->con_set_origin ||
510 !sw->con_set_origin(vc_cons[currcons].d))
511 origin = (unsigned long) screenbuf;
512 visible_origin = origin;
513 scr_end = origin + screenbuf_size;
514 pos = origin + video_size_row*y + 2*x;
517 static inline void save_screen(int currcons)
519 if (sw->con_save_screen)
520 sw->con_save_screen(vc_cons[currcons].d);
524 * Redrawing of screen
527 void redraw_screen(int new_console, int is_switch)
529 int redraw = 1;
530 int currcons, old_console;
531 static int lock = 0;
533 if (lock)
534 return;
535 if (!vc_cons_allocated(new_console)) {
536 /* strange ... */
537 printk("redraw_screen: tty %d not allocated ??\n", new_console+1);
538 return;
540 lock = 1;
542 if (is_switch) {
543 currcons = fg_console;
544 hide_cursor(currcons);
545 if (fg_console != new_console) {
546 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
547 old_console = (*display) ? (*display)->vc_num : fg_console;
548 *display = vc_cons[new_console].d;
549 fg_console = new_console;
550 currcons = old_console;
551 if (!IS_VISIBLE) {
552 save_screen(currcons);
553 set_origin(currcons);
555 currcons = new_console;
556 if (old_console == new_console)
557 redraw = 0;
559 } else {
560 currcons = new_console;
561 hide_cursor(currcons);
564 if (redraw) {
565 set_origin(currcons);
566 set_palette(currcons);
567 if (sw->con_switch(vc_cons[currcons].d) && vcmode != KD_GRAPHICS)
568 /* Update the screen contents */
569 do_update_region(currcons, origin, screenbuf_size/2);
571 set_cursor(currcons);
572 if (is_switch) {
573 set_leds();
574 compute_shiftstate();
576 lock = 0;
580 * Allocation, freeing and resizing of VTs.
583 int vc_cons_allocated(unsigned int i)
585 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
588 static void visual_init(int currcons, int init)
590 /* ++Geert: sw->con_init determines console size */
591 sw = conswitchp;
592 #ifndef VT_SINGLE_DRIVER
593 if (con_driver_map[currcons])
594 sw = con_driver_map[currcons];
595 #endif
596 cons_num = currcons;
597 display_fg = &master_display_fg;
598 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
599 vc_cons[currcons].d->vc_uni_pagedir = 0;
600 hi_font_mask = 0;
601 complement_mask = 0;
602 can_do_color = 0;
603 sw->con_init(vc_cons[currcons].d, init);
604 if (!complement_mask)
605 complement_mask = can_do_color ? 0x7700 : 0x0800;
606 s_complement_mask = complement_mask;
607 video_size_row = video_num_columns<<1;
608 screenbuf_size = video_num_lines*video_size_row;
611 int vc_allocate(unsigned int currcons) /* return 0 on success */
613 if (currcons >= MAX_NR_CONSOLES)
614 return -ENXIO;
615 if (!vc_cons[currcons].d) {
616 long p, q;
618 /* prevent users from taking too much memory */
619 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
620 return -EPERM;
622 /* due to the granularity of kmalloc, we waste some memory here */
623 /* the alloc is done in two steps, to optimize the common situation
624 of a 25x80 console (structsize=216, screenbuf_size=4000) */
625 /* although the numbers above are not valid since long ago, the
626 point is still up-to-date and the comment still has its value
627 even if only as a historical artifact. --mj, July 1998 */
628 p = (long) kmalloc(structsize, GFP_KERNEL);
629 if (!p)
630 return -ENOMEM;
631 vc_cons[currcons].d = (struct vc_data *)p;
632 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
633 visual_init(currcons, 1);
634 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
635 con_set_default_unimap(currcons);
636 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
637 if (!q) {
638 kfree_s((char *) p, structsize);
639 vc_cons[currcons].d = NULL;
640 vt_cons[currcons] = NULL;
641 return -ENOMEM;
643 screenbuf = (unsigned short *) q;
644 kmalloced = 1;
645 vc_init(currcons, video_num_lines, video_num_columns, 1);
647 return 0;
651 * Change # of rows and columns (0 means unchanged/the size of fg_console)
652 * [this is to be used together with some user program
653 * like resize that changes the hardware videomode]
655 int vc_resize(unsigned int lines, unsigned int cols,
656 unsigned int first, unsigned int last)
658 unsigned int cc, ll, ss, sr, todo = 0;
659 unsigned int currcons = fg_console, i;
660 unsigned short *newscreens[MAX_NR_CONSOLES];
662 cc = (cols ? cols : video_num_columns);
663 ll = (lines ? lines : video_num_lines);
664 sr = cc << 1;
665 ss = sr * ll;
667 for (currcons = first; currcons <= last; currcons++) {
668 if (!vc_cons_allocated(currcons) ||
669 (cc == video_num_columns && ll == video_num_lines))
670 newscreens[currcons] = NULL;
671 else {
672 unsigned short *p = (unsigned short *) kmalloc(ss, GFP_USER);
673 if (!p) {
674 for (i = 0; i< currcons; i++)
675 if (newscreens[i])
676 kfree_s(newscreens[i], ss);
677 return -ENOMEM;
679 newscreens[currcons] = p;
680 todo++;
683 if (!todo)
684 return 0;
686 for (currcons = first; currcons <= last; currcons++) {
687 unsigned int occ, oll, oss, osr;
688 unsigned long ol, nl, nlend, rlth, rrem;
689 if (!newscreens[currcons] || !vc_cons_allocated(currcons))
690 continue;
692 oll = video_num_lines;
693 occ = video_num_columns;
694 osr = video_size_row;
695 oss = screenbuf_size;
697 video_num_lines = ll;
698 video_num_columns = cc;
699 video_size_row = sr;
700 screenbuf_size = ss;
702 rlth = MIN(osr, sr);
703 rrem = sr - rlth;
704 ol = origin;
705 nl = (long) newscreens[currcons];
706 nlend = nl + ss;
707 if (ll < oll)
708 ol += (oll - ll) * osr;
710 update_attr(currcons);
712 while (ol < scr_end) {
713 scr_memcpyw((unsigned short *) nl, (unsigned short *) ol, rlth);
714 if (rrem)
715 scr_memsetw((void *)(nl + rlth), video_erase_char, rrem);
716 ol += osr;
717 nl += sr;
719 if (nlend > nl)
720 scr_memsetw((void *) nl, video_erase_char, nlend - nl);
721 if (kmalloced)
722 kfree_s(screenbuf, oss);
723 screenbuf = newscreens[currcons];
724 kmalloced = 1;
725 screenbuf_size = ss;
726 set_origin(currcons);
728 /* do part of a reset_terminal() */
729 top = 0;
730 bottom = video_num_lines;
731 gotoxy(currcons, x, y);
732 save_cur(currcons);
734 if (console_table[currcons]) {
735 struct winsize ws, *cws = &console_table[currcons]->winsize;
736 memset(&ws, 0, sizeof(ws));
737 ws.ws_row = video_num_lines;
738 ws.ws_col = video_num_columns;
739 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
740 console_table[currcons]->pgrp > 0)
741 kill_pg(console_table[currcons]->pgrp, SIGWINCH, 1);
742 *cws = ws;
745 if (IS_VISIBLE)
746 update_screen(currcons);
749 return 0;
753 void vc_disallocate(unsigned int currcons)
755 if (vc_cons_allocated(currcons)) {
756 sw->con_deinit(vc_cons[currcons].d);
757 if (kmalloced)
758 kfree_s(screenbuf, screenbuf_size);
759 if (currcons >= MIN_NR_CONSOLES)
760 kfree_s(vc_cons[currcons].d, structsize);
761 vc_cons[currcons].d = NULL;
766 * VT102 emulator
769 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
770 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
771 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
773 #define decarm VC_REPEAT
774 #define decckm VC_CKMODE
775 #define kbdapplic VC_APPLIC
776 #define lnm VC_CRLF
779 * this is what the terminal answers to a ESC-Z or csi0c query.
781 #define VT100ID "\033[?1;2c"
782 #define VT102ID "\033[?6c"
784 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
785 8,12,10,14, 9,13,11,15 };
787 /* the default colour table, for VGA+ colour systems */
788 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
789 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
790 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
791 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
792 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
793 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
796 * gotoxy() must verify all boundaries, because the arguments
797 * might also be negative. If the given position is out of
798 * bounds, the cursor is placed at the nearest margin.
800 static void gotoxy(int currcons, int new_x, int new_y)
802 int min_y, max_y;
804 if (new_x < 0)
805 x = 0;
806 else
807 if (new_x >= video_num_columns)
808 x = video_num_columns - 1;
809 else
810 x = new_x;
811 if (decom) {
812 min_y = top;
813 max_y = bottom;
814 } else {
815 min_y = 0;
816 max_y = video_num_lines;
818 if (new_y < min_y)
819 y = min_y;
820 else if (new_y >= max_y)
821 y = max_y - 1;
822 else
823 y = new_y;
824 pos = origin + y*video_size_row + (x<<1);
825 need_wrap = 0;
828 /* for absolute user moves, when decom is set */
829 static void gotoxay(int currcons, int new_x, int new_y)
831 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
834 void scrollback(int lines)
836 int currcons = fg_console;
838 if (!lines)
839 lines = video_num_lines/2;
840 scrolldelta(-lines);
843 void scrollfront(int lines)
845 int currcons = fg_console;
847 if (!lines)
848 lines = video_num_lines/2;
849 scrolldelta(lines);
852 static void lf(int currcons)
854 /* don't scroll if above bottom of scrolling region, or
855 * if below scrolling region
857 if (y+1 == bottom)
858 scrup(currcons,top,bottom,1);
859 else if (y < video_num_lines-1) {
860 y++;
861 pos += video_size_row;
863 need_wrap = 0;
866 static void ri(int currcons)
868 /* don't scroll if below top of scrolling region, or
869 * if above scrolling region
871 if (y == top)
872 scrdown(currcons,top,bottom,1);
873 else if (y > 0) {
874 y--;
875 pos -= video_size_row;
877 need_wrap = 0;
880 static inline void cr(int currcons)
882 pos -= x<<1;
883 need_wrap = x = 0;
886 static inline void bs(int currcons)
888 if (x) {
889 pos -= 2;
890 x--;
891 need_wrap = 0;
895 static inline void del(int currcons)
897 /* ignored */
900 static void csi_J(int currcons, int vpar)
902 unsigned int count;
903 unsigned short * start;
905 switch (vpar) {
906 case 0: /* erase from cursor to end of display */
907 count = (scr_end-pos)>>1;
908 start = (unsigned short *) pos;
909 if (DO_UPDATE) {
910 /* do in two stages */
911 sw->con_clear(vc_cons[currcons].d, y, x, 1,
912 video_num_columns-x);
913 sw->con_clear(vc_cons[currcons].d, y+1, 0,
914 video_num_lines-y-1,
915 video_num_columns);
917 break;
918 case 1: /* erase from start to cursor */
919 count = ((pos-origin)>>1)+1;
920 start = (unsigned short *) origin;
921 if (DO_UPDATE) {
922 /* do in two stages */
923 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
924 video_num_columns);
925 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
926 x + 1);
928 break;
929 case 2: /* erase whole display */
930 count = video_num_columns * video_num_lines;
931 start = (unsigned short *) origin;
932 if (DO_UPDATE)
933 sw->con_clear(vc_cons[currcons].d, 0, 0,
934 video_num_lines,
935 video_num_columns);
936 break;
937 default:
938 return;
940 scr_memsetw(start, video_erase_char, 2*count);
941 need_wrap = 0;
944 static void csi_K(int currcons, int vpar)
946 unsigned int count;
947 unsigned short * start;
949 switch (vpar) {
950 case 0: /* erase from cursor to end of line */
951 count = video_num_columns-x;
952 start = (unsigned short *) pos;
953 if (DO_UPDATE)
954 sw->con_clear(vc_cons[currcons].d, y, x, 1,
955 video_num_columns-x);
956 break;
957 case 1: /* erase from start of line to cursor */
958 start = (unsigned short *) (pos - (x<<1));
959 count = x+1;
960 if (DO_UPDATE)
961 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
962 x + 1);
963 break;
964 case 2: /* erase whole line */
965 start = (unsigned short *) (pos - (x<<1));
966 count = video_num_columns;
967 if (DO_UPDATE)
968 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
969 video_num_columns);
970 break;
971 default:
972 return;
974 scr_memsetw(start, video_erase_char, 2 * count);
975 need_wrap = 0;
978 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
979 { /* not vt100? */
980 int count;
982 if (!vpar)
983 vpar++;
984 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
986 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
987 if (DO_UPDATE)
988 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
989 need_wrap = 0;
992 static void default_attr(int currcons)
994 intensity = 1;
995 underline = 0;
996 reverse = 0;
997 blink = 0;
998 color = def_color;
1001 static void csi_m(int currcons)
1003 int i;
1005 for (i=0;i<=npar;i++)
1006 switch (par[i]) {
1007 case 0: /* all attributes off */
1008 default_attr(currcons);
1009 break;
1010 case 1:
1011 intensity = 2;
1012 break;
1013 case 2:
1014 intensity = 0;
1015 break;
1016 case 4:
1017 underline = 1;
1018 break;
1019 case 5:
1020 blink = 1;
1021 break;
1022 case 7:
1023 reverse = 1;
1024 break;
1025 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1026 * Select primary font, don't display
1027 * control chars if defined, don't set
1028 * bit 8 on output.
1030 translate = set_translate(charset == 0
1031 ? G0_charset
1032 : G1_charset,currcons);
1033 disp_ctrl = 0;
1034 toggle_meta = 0;
1035 break;
1036 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1037 * Select first alternate font, lets
1038 * chars < 32 be displayed as ROM chars.
1040 translate = set_translate(IBMPC_MAP,currcons);
1041 disp_ctrl = 1;
1042 toggle_meta = 0;
1043 break;
1044 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1045 * Select second alternate font, toggle
1046 * high bit before displaying as ROM char.
1048 translate = set_translate(IBMPC_MAP,currcons);
1049 disp_ctrl = 1;
1050 toggle_meta = 1;
1051 break;
1052 case 21:
1053 case 22:
1054 intensity = 1;
1055 break;
1056 case 24:
1057 underline = 0;
1058 break;
1059 case 25:
1060 blink = 0;
1061 break;
1062 case 27:
1063 reverse = 0;
1064 break;
1065 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1066 * Enables underscore, white foreground
1067 * with white underscore (Linux - use
1068 * default foreground).
1070 color = (def_color & 0x0f) | background;
1071 underline = 1;
1072 break;
1073 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1074 * Disable underline option.
1075 * Reset colour to default? It did this
1076 * before...
1078 color = (def_color & 0x0f) | background;
1079 underline = 0;
1080 break;
1081 case 49:
1082 color = (def_color & 0xf0) | foreground;
1083 break;
1084 default:
1085 if (par[i] >= 30 && par[i] <= 37)
1086 color = color_table[par[i]-30]
1087 | background;
1088 else if (par[i] >= 40 && par[i] <= 47)
1089 color = (color_table[par[i]-40]<<4)
1090 | foreground;
1091 break;
1093 update_attr(currcons);
1096 static void respond_string(const char * p, struct tty_struct * tty)
1098 while (*p) {
1099 tty_insert_flip_char(tty, *p, 0);
1100 p++;
1102 con_schedule_flip(tty);
1105 static void cursor_report(int currcons, struct tty_struct * tty)
1107 char buf[40];
1109 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1110 respond_string(buf, tty);
1113 static inline void status_report(struct tty_struct * tty)
1115 respond_string("\033[0n", tty); /* Terminal ok */
1118 static inline void respond_ID(struct tty_struct * tty)
1120 respond_string(VT102ID, tty);
1123 void mouse_report(struct tty_struct * tty, int butt, int mrx, int mry)
1125 char buf[8];
1127 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1128 (char)('!' + mry));
1129 respond_string(buf, tty);
1132 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1133 int mouse_reporting(void)
1135 int currcons = fg_console;
1137 return report_mouse;
1140 static void set_mode(int currcons, int on_off)
1142 int i;
1144 for (i=0; i<=npar; i++)
1145 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1146 case 1: /* Cursor keys send ^[Ox/^[[x */
1147 if (on_off)
1148 set_kbd(decckm);
1149 else
1150 clr_kbd(decckm);
1151 break;
1152 case 3: /* 80/132 mode switch unimplemented */
1153 deccolm = on_off;
1154 #if 0
1155 (void) vc_resize(video_num_lines, deccolm ? 132 : 80);
1156 /* this alone does not suffice; some user mode
1157 utility has to change the hardware regs */
1158 #endif
1159 break;
1160 case 5: /* Inverted screen on/off */
1161 if (decscnm != on_off) {
1162 decscnm = on_off;
1163 invert_screen(currcons, 0, screenbuf_size, 0);
1164 update_attr(currcons);
1166 break;
1167 case 6: /* Origin relative/absolute */
1168 decom = on_off;
1169 gotoxay(currcons,0,0);
1170 break;
1171 case 7: /* Autowrap on/off */
1172 decawm = on_off;
1173 break;
1174 case 8: /* Autorepeat on/off */
1175 if (on_off)
1176 set_kbd(decarm);
1177 else
1178 clr_kbd(decarm);
1179 break;
1180 case 9:
1181 report_mouse = on_off ? 1 : 0;
1182 break;
1183 case 25: /* Cursor on/off */
1184 deccm = on_off;
1185 break;
1186 case 1000:
1187 report_mouse = on_off ? 2 : 0;
1188 break;
1189 } else switch(par[i]) { /* ANSI modes set/reset */
1190 case 3: /* Monitor (display ctrls) */
1191 disp_ctrl = on_off;
1192 break;
1193 case 4: /* Insert Mode on/off */
1194 decim = on_off;
1195 break;
1196 case 20: /* Lf, Enter == CrLf/Lf */
1197 if (on_off)
1198 set_kbd(lnm);
1199 else
1200 clr_kbd(lnm);
1201 break;
1205 static void setterm_command(int currcons)
1207 switch(par[0]) {
1208 case 1: /* set color for underline mode */
1209 if (can_do_color && par[1] < 16) {
1210 ulcolor = color_table[par[1]];
1211 if (underline)
1212 update_attr(currcons);
1214 break;
1215 case 2: /* set color for half intensity mode */
1216 if (can_do_color && par[1] < 16) {
1217 halfcolor = color_table[par[1]];
1218 if (intensity == 0)
1219 update_attr(currcons);
1221 break;
1222 case 8: /* store colors as defaults */
1223 def_color = attr;
1224 default_attr(currcons);
1225 update_attr(currcons);
1226 break;
1227 case 9: /* set blanking interval */
1228 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1229 poke_blanked_console();
1230 break;
1231 case 10: /* set bell frequency in Hz */
1232 if (npar >= 1)
1233 bell_pitch = par[1];
1234 else
1235 bell_pitch = DEFAULT_BELL_PITCH;
1236 break;
1237 case 11: /* set bell duration in msec */
1238 if (npar >= 1)
1239 bell_duration = (par[1] < 2000) ?
1240 par[1]*HZ/1000 : 0;
1241 else
1242 bell_duration = DEFAULT_BELL_DURATION;
1243 break;
1244 case 12: /* bring specified console to the front */
1245 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1246 set_console(par[1] - 1);
1247 break;
1248 case 13: /* unblank the screen */
1249 poke_blanked_console();
1250 break;
1251 case 14: /* set vesa powerdown interval */
1252 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1253 break;
1257 static void insert_line(int currcons, unsigned int nr)
1259 scrdown(currcons,y,bottom,nr);
1260 need_wrap = 0;
1264 static void delete_line(int currcons, unsigned int nr)
1266 scrup(currcons,y,bottom,nr);
1267 need_wrap = 0;
1270 static void csi_at(int currcons, unsigned int nr)
1272 if (nr > video_num_columns - x)
1273 nr = video_num_columns - x;
1274 else if (!nr)
1275 nr = 1;
1276 insert_char(currcons, nr);
1279 static void csi_L(int currcons, unsigned int nr)
1281 if (nr > video_num_lines - y)
1282 nr = video_num_lines - y;
1283 else if (!nr)
1284 nr = 1;
1285 insert_line(currcons, nr);
1288 static void csi_P(int currcons, unsigned int nr)
1290 if (nr > video_num_columns - x)
1291 nr = video_num_columns - x;
1292 else if (!nr)
1293 nr = 1;
1294 delete_char(currcons, nr);
1297 static void csi_M(int currcons, unsigned int nr)
1299 if (nr > video_num_lines - y)
1300 nr = video_num_lines - y;
1301 else if (!nr)
1302 nr=1;
1303 delete_line(currcons, nr);
1306 static void save_cur(int currcons)
1308 saved_x = x;
1309 saved_y = y;
1310 s_intensity = intensity;
1311 s_underline = underline;
1312 s_blink = blink;
1313 s_reverse = reverse;
1314 s_charset = charset;
1315 s_color = color;
1316 saved_G0 = G0_charset;
1317 saved_G1 = G1_charset;
1320 static void restore_cur(int currcons)
1322 gotoxy(currcons,saved_x,saved_y);
1323 intensity = s_intensity;
1324 underline = s_underline;
1325 blink = s_blink;
1326 reverse = s_reverse;
1327 charset = s_charset;
1328 color = s_color;
1329 G0_charset = saved_G0;
1330 G1_charset = saved_G1;
1331 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1332 update_attr(currcons);
1333 need_wrap = 0;
1336 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1337 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1338 ESpalette };
1340 static void reset_terminal(int currcons, int do_clear)
1342 top = 0;
1343 bottom = video_num_lines;
1344 vc_state = ESnormal;
1345 ques = 0;
1346 translate = set_translate(LAT1_MAP,currcons);
1347 G0_charset = LAT1_MAP;
1348 G1_charset = GRAF_MAP;
1349 charset = 0;
1350 need_wrap = 0;
1351 report_mouse = 0;
1352 utf = 0;
1353 utf_count = 0;
1355 disp_ctrl = 0;
1356 toggle_meta = 0;
1358 decscnm = 0;
1359 decom = 0;
1360 decawm = 1;
1361 deccm = 1;
1362 decim = 0;
1364 set_kbd(decarm);
1365 clr_kbd(decckm);
1366 clr_kbd(kbdapplic);
1367 clr_kbd(lnm);
1368 kbd_table[currcons].lockstate = 0;
1369 kbd_table[currcons].slockstate = 0;
1370 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1371 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1372 set_leds();
1374 cursor_type = CUR_DEFAULT;
1375 complement_mask = s_complement_mask;
1377 default_attr(currcons);
1378 update_attr(currcons);
1380 tab_stop[0] = 0x01010100;
1381 tab_stop[1] =
1382 tab_stop[2] =
1383 tab_stop[3] =
1384 tab_stop[4] = 0x01010101;
1386 bell_pitch = DEFAULT_BELL_PITCH;
1387 bell_duration = DEFAULT_BELL_DURATION;
1389 gotoxy(currcons,0,0);
1390 save_cur(currcons);
1391 if (do_clear)
1392 csi_J(currcons,2);
1395 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1398 * Control characters can be used in the _middle_
1399 * of an escape sequence.
1401 switch (c) {
1402 case 0:
1403 return;
1404 case 7:
1405 if (bell_duration)
1406 kd_mksound(bell_pitch, bell_duration);
1407 return;
1408 case 8:
1409 bs(currcons);
1410 return;
1411 case 9:
1412 pos -= (x << 1);
1413 while (x < video_num_columns - 1) {
1414 x++;
1415 if (tab_stop[x >> 5] & (1 << (x & 31)))
1416 break;
1418 pos += (x << 1);
1419 return;
1420 case 10: case 11: case 12:
1421 lf(currcons);
1422 if (!is_kbd(lnm))
1423 return;
1424 case 13:
1425 cr(currcons);
1426 return;
1427 case 14:
1428 charset = 1;
1429 translate = set_translate(G1_charset,currcons);
1430 disp_ctrl = 1;
1431 return;
1432 case 15:
1433 charset = 0;
1434 translate = set_translate(G0_charset,currcons);
1435 disp_ctrl = 0;
1436 return;
1437 case 24: case 26:
1438 vc_state = ESnormal;
1439 return;
1440 case 27:
1441 vc_state = ESesc;
1442 return;
1443 case 127:
1444 del(currcons);
1445 return;
1446 case 128+27:
1447 vc_state = ESsquare;
1448 return;
1450 switch(vc_state) {
1451 case ESesc:
1452 vc_state = ESnormal;
1453 switch (c) {
1454 case '[':
1455 vc_state = ESsquare;
1456 return;
1457 case ']':
1458 vc_state = ESnonstd;
1459 return;
1460 case '%':
1461 vc_state = ESpercent;
1462 return;
1463 case 'E':
1464 cr(currcons);
1465 lf(currcons);
1466 return;
1467 case 'M':
1468 ri(currcons);
1469 return;
1470 case 'D':
1471 lf(currcons);
1472 return;
1473 case 'H':
1474 tab_stop[x >> 5] |= (1 << (x & 31));
1475 return;
1476 case 'Z':
1477 respond_ID(tty);
1478 return;
1479 case '7':
1480 save_cur(currcons);
1481 return;
1482 case '8':
1483 restore_cur(currcons);
1484 return;
1485 case '(':
1486 vc_state = ESsetG0;
1487 return;
1488 case ')':
1489 vc_state = ESsetG1;
1490 return;
1491 case '#':
1492 vc_state = EShash;
1493 return;
1494 case 'c':
1495 reset_terminal(currcons,1);
1496 return;
1497 case '>': /* Numeric keypad */
1498 clr_kbd(kbdapplic);
1499 return;
1500 case '=': /* Appl. keypad */
1501 set_kbd(kbdapplic);
1502 return;
1504 return;
1505 case ESnonstd:
1506 if (c=='P') { /* palette escape sequence */
1507 for (npar=0; npar<NPAR; npar++)
1508 par[npar] = 0 ;
1509 npar = 0 ;
1510 vc_state = ESpalette;
1511 return;
1512 } else if (c=='R') { /* reset palette */
1513 reset_palette(currcons);
1514 vc_state = ESnormal;
1515 } else
1516 vc_state = ESnormal;
1517 return;
1518 case ESpalette:
1519 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1520 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1521 if (npar==7) {
1522 int i = par[0]*3, j = 1;
1523 palette[i] = 16*par[j++];
1524 palette[i++] += par[j++];
1525 palette[i] = 16*par[j++];
1526 palette[i++] += par[j++];
1527 palette[i] = 16*par[j++];
1528 palette[i] += par[j];
1529 set_palette(currcons);
1530 vc_state = ESnormal;
1532 } else
1533 vc_state = ESnormal;
1534 return;
1535 case ESsquare:
1536 for(npar = 0 ; npar < NPAR ; npar++)
1537 par[npar] = 0;
1538 npar = 0;
1539 vc_state = ESgetpars;
1540 if (c == '[') { /* Function key */
1541 vc_state=ESfunckey;
1542 return;
1544 ques = (c=='?');
1545 if (ques)
1546 return;
1547 case ESgetpars:
1548 if (c==';' && npar<NPAR-1) {
1549 npar++;
1550 return;
1551 } else if (c>='0' && c<='9') {
1552 par[npar] *= 10;
1553 par[npar] += c-'0';
1554 return;
1555 } else vc_state=ESgotpars;
1556 case ESgotpars:
1557 vc_state = ESnormal;
1558 switch(c) {
1559 case 'h':
1560 set_mode(currcons,1);
1561 return;
1562 case 'l':
1563 set_mode(currcons,0);
1564 return;
1565 case 'c':
1566 if (ques) {
1567 if (par[0])
1568 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1569 else
1570 cursor_type = CUR_DEFAULT;
1571 return;
1573 break;
1574 case 'm':
1575 if (ques) {
1576 clear_selection();
1577 if (par[0])
1578 complement_mask = par[0]<<8 | par[1];
1579 else
1580 complement_mask = s_complement_mask;
1581 return;
1583 break;
1584 case 'n':
1585 if (!ques) {
1586 if (par[0] == 5)
1587 status_report(tty);
1588 else if (par[0] == 6)
1589 cursor_report(currcons,tty);
1591 return;
1593 if (ques) {
1594 ques = 0;
1595 return;
1597 switch(c) {
1598 case 'G': case '`':
1599 if (par[0]) par[0]--;
1600 gotoxy(currcons,par[0],y);
1601 return;
1602 case 'A':
1603 if (!par[0]) par[0]++;
1604 gotoxy(currcons,x,y-par[0]);
1605 return;
1606 case 'B': case 'e':
1607 if (!par[0]) par[0]++;
1608 gotoxy(currcons,x,y+par[0]);
1609 return;
1610 case 'C': case 'a':
1611 if (!par[0]) par[0]++;
1612 gotoxy(currcons,x+par[0],y);
1613 return;
1614 case 'D':
1615 if (!par[0]) par[0]++;
1616 gotoxy(currcons,x-par[0],y);
1617 return;
1618 case 'E':
1619 if (!par[0]) par[0]++;
1620 gotoxy(currcons,0,y+par[0]);
1621 return;
1622 case 'F':
1623 if (!par[0]) par[0]++;
1624 gotoxy(currcons,0,y-par[0]);
1625 return;
1626 case 'd':
1627 if (par[0]) par[0]--;
1628 gotoxay(currcons,x,par[0]);
1629 return;
1630 case 'H': case 'f':
1631 if (par[0]) par[0]--;
1632 if (par[1]) par[1]--;
1633 gotoxay(currcons,par[1],par[0]);
1634 return;
1635 case 'J':
1636 csi_J(currcons,par[0]);
1637 return;
1638 case 'K':
1639 csi_K(currcons,par[0]);
1640 return;
1641 case 'L':
1642 csi_L(currcons,par[0]);
1643 return;
1644 case 'M':
1645 csi_M(currcons,par[0]);
1646 return;
1647 case 'P':
1648 csi_P(currcons,par[0]);
1649 return;
1650 case 'c':
1651 if (!par[0])
1652 respond_ID(tty);
1653 return;
1654 case 'g':
1655 if (!par[0])
1656 tab_stop[x >> 5] &= ~(1 << (x & 31));
1657 else if (par[0] == 3) {
1658 tab_stop[0] =
1659 tab_stop[1] =
1660 tab_stop[2] =
1661 tab_stop[3] =
1662 tab_stop[4] = 0;
1664 return;
1665 case 'm':
1666 csi_m(currcons);
1667 return;
1668 case 'q': /* DECLL - but only 3 leds */
1669 /* map 0,1,2,3 to 0,1,2,4 */
1670 if (par[0] < 4)
1671 setledstate(kbd_table + currcons,
1672 (par[0] < 3) ? par[0] : 4);
1673 return;
1674 case 'r':
1675 if (!par[0])
1676 par[0]++;
1677 if (!par[1])
1678 par[1] = video_num_lines;
1679 /* Minimum allowed region is 2 lines */
1680 if (par[0] < par[1] &&
1681 par[1] <= video_num_lines) {
1682 top=par[0]-1;
1683 bottom=par[1];
1684 gotoxay(currcons,0,0);
1686 return;
1687 case 's':
1688 save_cur(currcons);
1689 return;
1690 case 'u':
1691 restore_cur(currcons);
1692 return;
1693 case 'X':
1694 csi_X(currcons, par[0]);
1695 return;
1696 case '@':
1697 csi_at(currcons,par[0]);
1698 return;
1699 case ']': /* setterm functions */
1700 setterm_command(currcons);
1701 return;
1703 return;
1704 case ESpercent:
1705 vc_state = ESnormal;
1706 switch (c) {
1707 case '@': /* defined in ISO 2022 */
1708 utf = 0;
1709 return;
1710 case 'G': /* prelim official escape code */
1711 case '8': /* retained for compatibility */
1712 utf = 1;
1713 return;
1715 return;
1716 case ESfunckey:
1717 vc_state = ESnormal;
1718 return;
1719 case EShash:
1720 vc_state = ESnormal;
1721 if (c == '8') {
1722 /* DEC screen alignment test. kludge :-) */
1723 video_erase_char =
1724 (video_erase_char & 0xff00) | 'E';
1725 csi_J(currcons, 2);
1726 video_erase_char =
1727 (video_erase_char & 0xff00) | ' ';
1728 do_update_region(currcons, origin, screenbuf_size/2);
1730 return;
1731 case ESsetG0:
1732 if (c == '0')
1733 G0_charset = GRAF_MAP;
1734 else if (c == 'B')
1735 G0_charset = LAT1_MAP;
1736 else if (c == 'U')
1737 G0_charset = IBMPC_MAP;
1738 else if (c == 'K')
1739 G0_charset = USER_MAP;
1740 if (charset == 0)
1741 translate = set_translate(G0_charset,currcons);
1742 vc_state = ESnormal;
1743 return;
1744 case ESsetG1:
1745 if (c == '0')
1746 G1_charset = GRAF_MAP;
1747 else if (c == 'B')
1748 G1_charset = LAT1_MAP;
1749 else if (c == 'U')
1750 G1_charset = IBMPC_MAP;
1751 else if (c == 'K')
1752 G1_charset = USER_MAP;
1753 if (charset == 1)
1754 translate = set_translate(G1_charset,currcons);
1755 vc_state = ESnormal;
1756 return;
1757 default:
1758 vc_state = ESnormal;
1762 static int do_con_write(struct tty_struct * tty, int from_user,
1763 const unsigned char *buf, int count)
1765 #ifdef VT_BUF_VRAM_ONLY
1766 #define FLUSH do { } while(0);
1767 #else
1768 #define FLUSH if (draw_x >= 0) { \
1769 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1770 draw_x = -1; \
1772 #endif
1774 int c, tc, ok, n = 0, draw_x = -1;
1775 unsigned int currcons;
1776 unsigned long draw_from = 0, draw_to = 0;
1777 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
1778 u16 himask, charmask;
1780 currcons = vt->vc_num;
1781 if (!vc_cons_allocated(currcons)) {
1782 /* could this happen? */
1783 static int error = 0;
1784 if (!error) {
1785 error = 1;
1786 printk("con_write: tty %d not allocated\n", currcons+1);
1788 return 0;
1791 if (from_user) {
1792 /* just to make sure that noone lurks at places he shouldn't see. */
1793 if (verify_area(VERIFY_READ, buf, count))
1794 return 0; /* ?? are error codes legal here ?? */
1797 himask = hi_font_mask;
1798 charmask = himask ? 0x1ff : 0xff;
1800 /* undraw cursor first */
1801 if (IS_FG)
1802 hide_cursor(currcons);
1804 disable_bh(CONSOLE_BH);
1805 while (!tty->stopped && count) {
1806 enable_bh(CONSOLE_BH);
1807 if (from_user)
1808 __get_user(c, buf);
1809 else
1810 c = *buf;
1811 buf++; n++; count--;
1812 disable_bh(CONSOLE_BH);
1814 if (utf) {
1815 /* Combine UTF-8 into Unicode */
1816 /* Incomplete characters silently ignored */
1817 if(c > 0x7f) {
1818 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1819 utf_char = (utf_char << 6) | (c & 0x3f);
1820 utf_count--;
1821 if (utf_count == 0)
1822 tc = c = utf_char;
1823 else continue;
1824 } else {
1825 if ((c & 0xe0) == 0xc0) {
1826 utf_count = 1;
1827 utf_char = (c & 0x1f);
1828 } else if ((c & 0xf0) == 0xe0) {
1829 utf_count = 2;
1830 utf_char = (c & 0x0f);
1831 } else if ((c & 0xf8) == 0xf0) {
1832 utf_count = 3;
1833 utf_char = (c & 0x07);
1834 } else if ((c & 0xfc) == 0xf8) {
1835 utf_count = 4;
1836 utf_char = (c & 0x03);
1837 } else if ((c & 0xfe) == 0xfc) {
1838 utf_count = 5;
1839 utf_char = (c & 0x01);
1840 } else
1841 utf_count = 0;
1842 continue;
1844 } else {
1845 tc = c;
1846 utf_count = 0;
1848 } else { /* no utf */
1849 tc = translate[toggle_meta ? (c|0x80) : c];
1852 /* If the original code was a control character we
1853 * only allow a glyph to be displayed if the code is
1854 * not normally used (such as for cursor movement) or
1855 * if the disp_ctrl mode has been explicitly enabled.
1856 * Certain characters (as given by the CTRL_ALWAYS
1857 * bitmap) are always displayed as control characters,
1858 * as the console would be pretty useless without
1859 * them; to display an arbitrary font position use the
1860 * direct-to-font zone in UTF-8 mode.
1862 ok = tc && (c >= 32 ||
1863 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
1864 : CTRL_ACTION) >> c) & 1)))
1865 && (c != 127 || disp_ctrl)
1866 && (c != 128+27);
1868 if (vc_state == ESnormal && ok) {
1869 /* Now try to find out how to display it */
1870 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
1871 if ( tc == -4 ) {
1872 /* If we got -4 (not found) then see if we have
1873 defined a replacement character (U+FFFD) */
1874 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
1876 /* One reason for the -4 can be that we just
1877 did a clear_unimap();
1878 try at least to show something. */
1879 if (tc == -4)
1880 tc = c;
1881 } else if ( tc == -3 ) {
1882 /* Bad hash table -- hope for the best */
1883 tc = c;
1885 if (tc & ~charmask)
1886 continue; /* Conversion failed */
1888 if (need_wrap || decim)
1889 FLUSH
1890 if (need_wrap) {
1891 cr(currcons);
1892 lf(currcons);
1894 if (decim)
1895 insert_char(currcons, 1);
1896 scr_writew(himask ?
1897 ((attr & ~himask) << 8) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
1898 (attr << 8) + tc,
1899 (u16 *) pos);
1900 if (DO_UPDATE && draw_x < 0) {
1901 draw_x = x;
1902 draw_from = pos;
1904 if (x == video_num_columns - 1) {
1905 need_wrap = decawm;
1906 draw_to = pos+2;
1907 } else {
1908 x++;
1909 draw_to = (pos+=2);
1911 continue;
1913 FLUSH
1914 do_con_trol(tty, currcons, c);
1916 FLUSH
1917 enable_bh(CONSOLE_BH);
1918 return n;
1919 #undef FLUSH
1923 * This is the console switching bottom half handler.
1925 * Doing console switching in a bottom half handler allows
1926 * us to do the switches asynchronously (needed when we want
1927 * to switch due to a keyboard interrupt), while still giving
1928 * us the option to easily disable it to avoid races when we
1929 * need to write to the console.
1931 static void console_bh(void)
1933 run_task_queue(&con_task_queue);
1934 if (want_console >= 0) {
1935 if (want_console != fg_console && vc_cons_allocated(want_console)) {
1936 hide_cursor(fg_console);
1937 change_console(want_console);
1938 /* we only changed when the console had already
1939 been allocated - a new console is not created
1940 in an interrupt routine */
1942 want_console = -1;
1944 if (do_poke_blanked_console) { /* do not unblank for a LED change */
1945 do_poke_blanked_console = 0;
1946 poke_blanked_console();
1948 if (scrollback_delta) {
1949 int currcons = fg_console;
1950 clear_selection();
1951 if (vcmode == KD_TEXT)
1952 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
1953 scrollback_delta = 0;
1957 #ifdef CONFIG_VT_CONSOLE
1960 * Console on virtual terminal
1962 * NOTE NOTE NOTE! This code can do no global locking. In particular,
1963 * we can't disable interrupts or bottom half handlers globally, because
1964 * we can be called from contexts that hold critical spinlocks, and
1965 * trying do get a global lock at this point will lead to deadlocks.
1968 void vt_console_print(struct console *co, const char * b, unsigned count)
1970 int currcons = fg_console;
1971 unsigned char c;
1972 static unsigned long printing = 0;
1973 const ushort *start;
1974 ushort cnt = 0;
1975 ushort myx = x;
1977 /* console busy or not yet initialized */
1978 if (!printable || test_and_set_bit(0, &printing))
1979 return;
1981 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
1982 currcons = kmsg_redirect - 1;
1984 if (!vc_cons_allocated(currcons)) {
1985 /* impossible */
1986 printk("vt_console_print: tty %d not allocated ??\n", currcons+1);
1987 goto quit;
1990 if (vcmode != KD_TEXT)
1991 goto quit;
1993 /* undraw cursor first */
1994 if (IS_FG)
1995 hide_cursor(currcons);
1997 start = (ushort *)pos;
1999 /* Contrived structure to try to emulate original need_wrap behaviour
2000 * Problems caused when we have need_wrap set on '\n' character */
2001 while (count--) {
2002 c = *b++;
2003 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2004 if (cnt > 0) {
2005 if (IS_VISIBLE)
2006 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2007 x += cnt;
2008 if (need_wrap)
2009 x--;
2010 cnt = 0;
2012 if (c == 8) { /* backspace */
2013 bs(currcons);
2014 start = (ushort *)pos;
2015 myx = x;
2016 continue;
2018 if (c != 13)
2019 lf(currcons);
2020 cr(currcons);
2021 start = (ushort *)pos;
2022 myx = x;
2023 if (c == 10 || c == 13)
2024 continue;
2026 scr_writew((attr << 8) + c, (unsigned short *) pos);
2027 cnt++;
2028 if (myx == video_num_columns - 1) {
2029 need_wrap = 1;
2030 continue;
2032 pos+=2;
2033 myx++;
2035 if (cnt > 0) {
2036 if (IS_VISIBLE)
2037 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2038 x += cnt;
2039 if (x == video_num_columns) {
2040 x--;
2041 need_wrap = 1;
2044 set_cursor(currcons);
2045 poke_blanked_console();
2047 quit:
2048 clear_bit(0, &printing);
2051 static kdev_t vt_console_device(struct console *c)
2053 return MKDEV(TTY_MAJOR, c->index ? c->index : fg_console + 1);
2056 struct console vt_console_driver = {
2057 "tty",
2058 vt_console_print,
2059 NULL,
2060 vt_console_device,
2061 keyboard_wait_for_keypress,
2062 unblank_screen,
2063 NULL,
2064 CON_PRINTBUFFER,
2067 NULL
2069 #endif
2072 * Handling of Linux-specific VC ioctls
2075 int tioclinux(struct tty_struct *tty, unsigned long arg)
2077 char type, data;
2079 if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
2080 return -EINVAL;
2081 if (current->tty != tty && !suser())
2082 return -EPERM;
2083 if (get_user(type, (char *)arg))
2084 return -EFAULT;
2085 switch (type)
2087 case 2:
2088 return set_selection(arg, tty, 1);
2089 case 3:
2090 return paste_selection(tty);
2091 case 4:
2092 unblank_screen();
2093 return 0;
2094 case 5:
2095 return sel_loadlut(arg);
2096 case 6:
2099 * Make it possible to react to Shift+Mousebutton.
2100 * Note that 'shift_state' is an undocumented
2101 * kernel-internal variable; programs not closely
2102 * related to the kernel should not use this.
2104 data = shift_state;
2105 return __put_user(data, (char *) arg);
2106 case 7:
2107 data = mouse_reporting();
2108 return __put_user(data, (char *) arg);
2109 case 10:
2110 set_vesa_blanking(arg);
2111 return 0;
2112 case 11: /* set kmsg redirect */
2113 if (!suser())
2114 return -EPERM;
2115 if (get_user(data, (char *)arg+1))
2116 return -EFAULT;
2117 kmsg_redirect = data;
2118 return 0;
2119 case 12: /* get fg_console */
2120 return fg_console;
2122 return -EINVAL;
2126 * /dev/ttyN handling
2129 static int con_write(struct tty_struct * tty, int from_user,
2130 const unsigned char *buf, int count)
2132 int retval;
2134 retval = do_con_write(tty, from_user, buf, count);
2135 con_flush_chars(tty);
2137 return retval;
2140 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2142 do_con_write(tty, 0, &ch, 1);
2145 static int con_write_room(struct tty_struct *tty)
2147 if (tty->stopped)
2148 return 0;
2149 return 4096; /* No limit, really; we're not buffering */
2152 static int con_chars_in_buffer(struct tty_struct *tty)
2154 return 0; /* we're not buffering */
2158 * con_throttle and con_unthrottle are only used for
2159 * paste_selection(), which has to stuff in a large number of
2160 * characters...
2162 static void con_throttle(struct tty_struct *tty)
2166 static void con_unthrottle(struct tty_struct *tty)
2168 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
2170 wake_up_interruptible(&vt->paste_wait);
2174 * Turn the Scroll-Lock LED on when the tty is stopped
2176 static void con_stop(struct tty_struct *tty)
2178 int console_num;
2179 if (!tty)
2180 return;
2181 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2182 if (!vc_cons_allocated(console_num))
2183 return;
2184 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2185 set_leds();
2189 * Turn the Scroll-Lock LED off when the console is started
2191 static void con_start(struct tty_struct *tty)
2193 int console_num;
2194 if (!tty)
2195 return;
2196 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2197 if (!vc_cons_allocated(console_num))
2198 return;
2199 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2200 set_leds();
2203 static void con_flush_chars(struct tty_struct *tty)
2205 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
2207 set_cursor(vt->vc_num);
2211 * Allocate the console screen memory.
2213 static int con_open(struct tty_struct *tty, struct file * filp)
2215 unsigned int currcons;
2216 int i;
2218 currcons = MINOR(tty->device) - tty->driver.minor_start;
2220 i = vc_allocate(currcons);
2221 if (i)
2222 return i;
2224 vt_cons[currcons]->vc_num = currcons;
2225 tty->driver_data = vt_cons[currcons];
2227 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2228 tty->winsize.ws_row = video_num_lines;
2229 tty->winsize.ws_col = video_num_columns;
2231 return 0;
2234 static void con_close(struct tty_struct *tty, struct file * filp)
2236 if (tty->count == 1)
2237 tty->driver_data = 0;
2240 static void vc_init(unsigned int currcons, unsigned int rows, unsigned int cols, int do_clear)
2242 int j, k ;
2244 video_num_columns = cols;
2245 video_num_lines = rows;
2246 video_size_row = cols<<1;
2247 screenbuf_size = video_num_lines * video_size_row;
2249 set_origin(currcons);
2250 pos = origin;
2251 reset_vc(currcons);
2252 for (j=k=0; j<16; j++) {
2253 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2254 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2255 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2257 def_color = 0x07; /* white */
2258 ulcolor = 0x0f; /* bold white */
2259 halfcolor = 0x08; /* grey */
2260 vt_cons[currcons]->paste_wait = 0;
2261 reset_terminal(currcons, do_clear);
2265 * This routine initializes console interrupts, and does nothing
2266 * else. If you want the screen to clear, call tty_write with
2267 * the appropriate escape-sequence.
2270 struct tty_driver console_driver;
2271 static int console_refcount;
2273 __initfunc(unsigned long con_init(unsigned long kmem_start))
2275 const char *display_desc = NULL;
2276 unsigned int currcons = 0;
2278 if (conswitchp)
2279 display_desc = conswitchp->con_startup();
2280 if (!display_desc) {
2281 fg_console = 0;
2282 return kmem_start;
2285 memset(&console_driver, 0, sizeof(struct tty_driver));
2286 console_driver.magic = TTY_DRIVER_MAGIC;
2287 console_driver.name = "tty";
2288 console_driver.name_base = 1;
2289 console_driver.major = TTY_MAJOR;
2290 console_driver.minor_start = 1;
2291 console_driver.num = MAX_NR_CONSOLES;
2292 console_driver.type = TTY_DRIVER_TYPE_CONSOLE;
2293 console_driver.init_termios = tty_std_termios;
2294 console_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2295 console_driver.refcount = &console_refcount;
2296 console_driver.table = console_table;
2297 console_driver.termios = console_termios;
2298 console_driver.termios_locked = console_termios_locked;
2300 console_driver.open = con_open;
2301 console_driver.close = con_close;
2302 console_driver.write = con_write;
2303 console_driver.write_room = con_write_room;
2304 console_driver.put_char = con_put_char;
2305 console_driver.flush_chars = con_flush_chars;
2306 console_driver.chars_in_buffer = con_chars_in_buffer;
2307 console_driver.ioctl = vt_ioctl;
2308 console_driver.stop = con_stop;
2309 console_driver.start = con_start;
2310 console_driver.throttle = con_throttle;
2311 console_driver.unthrottle = con_unthrottle;
2313 if (tty_register_driver(&console_driver))
2314 panic("Couldn't register console driver\n");
2316 timer_table[BLANK_TIMER].fn = blank_screen;
2317 timer_table[BLANK_TIMER].expires = 0;
2318 if (blankinterval) {
2319 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2320 timer_active |= 1<<BLANK_TIMER;
2323 /* Unfortunately, kmalloc is not running yet */
2324 /* Due to kmalloc roundup allocating statically is more efficient -
2325 so provide MIN_NR_CONSOLES for people with very little memory */
2326 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2327 int j, k ;
2329 vc_cons[currcons].d = (struct vc_data *) kmem_start;
2330 kmem_start += sizeof(struct vc_data);
2331 vt_cons[currcons] = (struct vt_struct *) kmem_start;
2332 kmem_start += sizeof(struct vt_struct);
2333 visual_init(currcons, 1);
2334 screenbuf = (unsigned short *) kmem_start;
2335 kmem_start += screenbuf_size;
2336 kmalloced = 0;
2337 vc_init(currcons, video_num_lines, video_num_columns,
2338 currcons || !sw->con_save_screen);
2339 for (j=k=0; j<16; j++) {
2340 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2341 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2342 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2345 currcons = fg_console = 0;
2346 master_display_fg = vc_cons[currcons].d;
2347 set_origin(currcons);
2348 save_screen(currcons);
2349 gotoxy(currcons,x,y);
2350 csi_J(currcons, 0);
2351 update_screen(fg_console);
2352 printk("Console: %s %s %dx%d",
2353 can_do_color ? "colour" : "mono",
2354 display_desc, video_num_columns, video_num_lines);
2355 printable = 1;
2356 printk("\n");
2358 #ifdef CONFIG_VT_CONSOLE
2359 register_console(&vt_console_driver);
2360 #endif
2362 init_bh(CONSOLE_BH, console_bh);
2364 return kmem_start;
2367 #ifndef VT_SINGLE_DRIVER
2369 static void clear_buffer_attributes(int currcons)
2371 unsigned short *p = (unsigned short *) origin;
2372 int count = screenbuf_size/2;
2373 int mask = hi_font_mask | 0xff;
2375 for (; count > 0; count--, p++) {
2376 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2381 * If we support more console drivers, this function is used
2382 * when a driver wants to take over some existing consoles
2383 * and become default driver for newly opened ones.
2386 void take_over_console(struct consw *csw, int first, int last, int deflt)
2388 int i, j = -1;
2389 const char *desc;
2391 desc = csw->con_startup();
2392 if (!desc) return;
2393 if (deflt)
2394 conswitchp = csw;
2396 for (i = first; i <= last; i++) {
2397 int old_was_color;
2398 int currcons = i;
2400 con_driver_map[i] = csw;
2402 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2403 continue;
2405 j = i;
2406 if (IS_VISIBLE)
2407 save_screen(i);
2408 old_was_color = vc_cons[i].d->vc_can_do_color;
2409 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2410 visual_init(i, 0);
2411 update_attr(i);
2413 /* If the console changed between mono <-> color, then
2414 * the attributes in the screenbuf will be wrong. The
2415 * following resets all attributes to something sane.
2417 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2418 clear_buffer_attributes(i);
2420 if (IS_VISIBLE)
2421 update_screen(i);
2423 printk("Console: switching ");
2424 if (!deflt)
2425 printk("consoles %d-%d ", first+1, last+1);
2426 if (j >= 0)
2427 printk("to %s %s %dx%d\n",
2428 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2429 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2430 else
2431 printk("to %s\n", desc);
2434 void give_up_console(struct consw *csw)
2436 int i;
2438 for(i = 0; i < MAX_NR_CONSOLES; i++)
2439 if (con_driver_map[i] == csw)
2440 con_driver_map[i] = NULL;
2443 #endif
2446 * Screen blanking
2449 static void set_vesa_blanking(unsigned long arg)
2451 char *argp = (char *)arg + 1;
2452 unsigned int mode;
2453 get_user(mode, argp);
2454 vesa_blank_mode = (mode < 4) ? mode : 0;
2457 static void vesa_powerdown(void)
2459 struct vc_data *c = vc_cons[fg_console].d;
2461 * Power down if currently suspended (1 or 2),
2462 * suspend if currently blanked (0),
2463 * else do nothing (i.e. already powered down (3)).
2464 * Called only if powerdown features are allowed.
2466 switch (vesa_blank_mode) {
2467 case VESA_NO_BLANKING:
2468 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1);
2469 break;
2470 case VESA_VSYNC_SUSPEND:
2471 case VESA_HSYNC_SUSPEND:
2472 c->vc_sw->con_blank(c, VESA_POWERDOWN+1);
2473 break;
2477 static void vesa_powerdown_screen(void)
2479 timer_active &= ~(1<<BLANK_TIMER);
2480 timer_table[BLANK_TIMER].fn = unblank_screen;
2482 vesa_powerdown();
2485 void do_blank_screen(int entering_gfx)
2487 int currcons = fg_console;
2488 int i;
2490 if (console_blanked)
2491 return;
2493 /* entering graphics mode? */
2494 if (entering_gfx) {
2495 hide_cursor(currcons);
2496 save_screen(currcons);
2497 sw->con_blank(vc_cons[currcons].d, -1);
2498 console_blanked = fg_console + 1;
2499 set_origin(currcons);
2500 return;
2503 /* don't blank graphics */
2504 if (vcmode != KD_TEXT) {
2505 console_blanked = fg_console + 1;
2506 return;
2509 hide_cursor(currcons);
2510 if (vesa_off_interval) {
2511 timer_table[BLANK_TIMER].fn = vesa_powerdown_screen;
2512 timer_table[BLANK_TIMER].expires = jiffies + vesa_off_interval;
2513 timer_active |= (1<<BLANK_TIMER);
2514 } else {
2515 timer_active &= ~(1<<BLANK_TIMER);
2516 timer_table[BLANK_TIMER].fn = unblank_screen;
2519 save_screen(currcons);
2520 /* In case we need to reset origin, blanking hook returns 1 */
2521 i = sw->con_blank(vc_cons[currcons].d, 1);
2522 console_blanked = fg_console + 1;
2523 if (i)
2524 set_origin(currcons);
2526 #ifdef CONFIG_APM
2527 if (apm_display_blank())
2528 return;
2529 #endif
2530 if (vesa_blank_mode)
2531 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1);
2534 void unblank_screen(void)
2536 int currcons;
2538 if (!console_blanked)
2539 return;
2540 if (!vc_cons_allocated(fg_console)) {
2541 /* impossible */
2542 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2543 return;
2545 timer_table[BLANK_TIMER].fn = blank_screen;
2546 if (blankinterval) {
2547 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2548 timer_active |= 1<<BLANK_TIMER;
2551 currcons = fg_console;
2552 console_blanked = 0;
2553 #ifdef CONFIG_APM
2554 apm_display_unblank();
2555 #endif
2556 if (sw->con_blank(vc_cons[currcons].d, 0))
2557 /* Low-level driver cannot restore -> do it ourselves */
2558 update_screen(fg_console);
2559 set_cursor(fg_console);
2562 static void blank_screen(void)
2564 do_blank_screen(0);
2567 void poke_blanked_console(void)
2569 timer_active &= ~(1<<BLANK_TIMER);
2570 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2571 return;
2572 if (console_blanked) {
2573 timer_table[BLANK_TIMER].fn = unblank_screen;
2574 timer_table[BLANK_TIMER].expires = jiffies; /* Now */
2575 timer_active |= 1<<BLANK_TIMER;
2576 } else if (blankinterval) {
2577 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2578 timer_active |= 1<<BLANK_TIMER;
2583 * Palettes
2586 void set_palette(int currcons)
2588 if (vcmode != KD_GRAPHICS)
2589 sw->con_set_palette(vc_cons[currcons].d, color_table);
2592 static int set_get_cmap(unsigned char *arg, int set)
2594 int i, j, k;
2596 for (i = 0; i < 16; i++)
2597 if (set) {
2598 get_user(default_red[i], arg++);
2599 get_user(default_grn[i], arg++);
2600 get_user(default_blu[i], arg++);
2601 } else {
2602 put_user(default_red[i], arg++);
2603 put_user(default_grn[i], arg++);
2604 put_user(default_blu[i], arg++);
2606 if (set) {
2607 for (i = 0; i < MAX_NR_CONSOLES; i++)
2608 if (vc_cons_allocated(i)) {
2609 for (j = k = 0; j < 16; j++) {
2610 vc_cons[i].d->vc_palette[k++] = default_red[j];
2611 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2612 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2614 set_palette(i);
2617 return 0;
2621 * Load palette into the DAC registers. arg points to a colour
2622 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2625 int con_set_cmap(unsigned char *arg)
2627 return set_get_cmap (arg,1);
2630 int con_get_cmap(unsigned char *arg)
2632 return set_get_cmap (arg,0);
2635 void reset_palette(int currcons)
2637 int j, k;
2638 for (j=k=0; j<16; j++) {
2639 palette[k++] = default_red[j];
2640 palette[k++] = default_grn[j];
2641 palette[k++] = default_blu[j];
2643 set_palette(currcons);
2647 * Font switching
2649 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2650 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2651 * depending on width) reserved for each character which is kinda wasty, but
2652 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2653 * is upto the actual low-level console-driver convert data into its favorite
2654 * format (maybe we should add a `fontoffset' field to the `display'
2655 * structure so we wont have to convert the fontdata all the time.
2656 * /Jes
2659 #define max_font_size 65536
2661 int con_font_op(int currcons, struct console_font_op *op)
2663 int rc = -EINVAL;
2664 int size = max_font_size, set;
2665 u8 *temp = NULL;
2666 struct console_font_op old_op;
2668 if (vt_cons[currcons]->vc_mode != KD_TEXT)
2669 goto quit;
2670 memcpy(&old_op, op, sizeof(old_op));
2671 if (op->op == KD_FONT_OP_SET) {
2672 if (!op->data)
2673 return -EINVAL;
2674 if (op->charcount > 512)
2675 goto quit;
2676 if (!op->height) { /* Need to guess font height [compat] */
2677 int h, i;
2678 u8 *charmap = op->data, tmp;
2680 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2681 so that we can get rid of this soon */
2682 if (!(op->flags & KD_FONT_FLAG_OLD))
2683 goto quit;
2684 rc = -EFAULT;
2685 for (h = 32; h > 0; h--)
2686 for (i = 0; i < op->charcount; i++) {
2687 if (get_user(tmp, &charmap[32*i+h-1]))
2688 goto quit;
2689 if (tmp)
2690 goto nonzero;
2692 rc = -EINVAL;
2693 goto quit;
2694 nonzero:
2695 rc = -EINVAL;
2696 op->height = h;
2698 if (op->width > 32 || op->height > 32)
2699 goto quit;
2700 size = (op->width+7)/8 * 32 * op->charcount;
2701 if (size > max_font_size)
2702 return -ENOSPC;
2703 set = 1;
2704 } else if (op->op == KD_FONT_OP_GET)
2705 set = 0;
2706 else
2707 return sw->con_font_op(vc_cons[currcons].d, op);
2708 if (op->data) {
2709 temp = kmalloc(size, GFP_KERNEL);
2710 if (!temp)
2711 return -ENOMEM;
2712 if (set && copy_from_user(temp, op->data, size)) {
2713 rc = -EFAULT;
2714 goto quit;
2716 op->data = temp;
2718 disable_bh(CONSOLE_BH);
2719 rc = sw->con_font_op(vc_cons[currcons].d, op);
2720 enable_bh(CONSOLE_BH);
2721 op->data = old_op.data;
2722 if (!rc && !set) {
2723 int c = (op->width+7)/8 * 32 * op->charcount;
2725 if (op->data && op->charcount > old_op.charcount)
2726 rc = -ENOSPC;
2727 if (!(op->flags & KD_FONT_FLAG_OLD)) {
2728 if (op->width > old_op.width ||
2729 op->height > old_op.height)
2730 rc = -ENOSPC;
2731 } else {
2732 if (op->width != 8)
2733 rc = -EIO;
2734 else if ((old_op.height && op->height > old_op.height) ||
2735 op->height > 32)
2736 rc = -ENOSPC;
2738 if (!rc && op->data && copy_to_user(op->data, temp, c))
2739 rc = -EFAULT;
2741 quit: if (temp)
2742 kfree_s(temp, size);
2743 return rc;
2747 * Interface exported to selection and vcs.
2750 /* used by selection */
2751 u16 screen_glyph(int currcons, int offset)
2753 u16 w = scr_readw(screenpos(currcons, offset, 1));
2754 u16 c = w & 0xff;
2756 if (w & hi_font_mask)
2757 c |= 0x100;
2758 return c;
2761 /* used by vcs - note the word offset */
2762 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
2764 return screenpos(currcons, 2 * w_offset, viewed);
2767 void getconsxy(int currcons, char *p)
2769 p[0] = x;
2770 p[1] = y;
2773 void putconsxy(int currcons, char *p)
2775 gotoxy(currcons, p[0], p[1]);
2776 set_cursor(currcons);
2779 u16 vcs_scr_readw(int currcons, u16 *org)
2781 if ((unsigned long)org == pos && softcursor_original != -1)
2782 return softcursor_original;
2783 return scr_readw(org);
2786 void vcs_scr_writew(int currcons, u16 val, u16 *org)
2788 scr_writew(val, org);
2789 if ((unsigned long)org == pos) {
2790 softcursor_original = -1;
2791 add_softcursor(currcons);
2797 * Visible symbols for modules
2800 EXPORT_SYMBOL(color_table);
2801 EXPORT_SYMBOL(default_red);
2802 EXPORT_SYMBOL(default_grn);
2803 EXPORT_SYMBOL(default_blu);
2804 EXPORT_SYMBOL(video_font_height);
2805 EXPORT_SYMBOL(video_scan_lines);
2806 EXPORT_SYMBOL(vc_resize);
2808 #ifndef VT_SINGLE_DRIVER
2809 EXPORT_SYMBOL(take_over_console);
2810 EXPORT_SYMBOL(give_up_console);
2811 #endif