Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / char / console.c
blob0285eab79d24f1d55c8b95099bc34fa874b4b89b
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@linux-m68k.org>, 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
70 * Removed old-style timers, introduced console_timer, made timer
71 * deletion SMP-safe. 17Jun00, Andrew Morton <andrewm@uow.edu.au>
74 #include <linux/module.h>
75 #include <linux/sched.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/kernel.h>
79 #include <linux/string.h>
80 #include <linux/errno.h>
81 #include <linux/kd.h>
82 #include <linux/malloc.h>
83 #include <linux/major.h>
84 #include <linux/mm.h>
85 #include <linux/console.h>
86 #include <linux/init.h>
87 #include <linux/devfs_fs_kernel.h>
88 #include <linux/vt_kern.h>
89 #include <linux/selection.h>
90 #include <linux/console_struct.h>
91 #include <linux/kbd_kern.h>
92 #include <linux/consolemap.h>
93 #include <linux/timer.h>
94 #include <linux/interrupt.h>
95 #include <linux/config.h>
96 #include <linux/version.h>
97 #include <linux/tqueue.h>
98 #include <linux/bootmem.h>
99 #include <linux/pm.h>
101 #include <asm/io.h>
102 #include <asm/system.h>
103 #include <asm/uaccess.h>
104 #include <asm/bitops.h>
106 #include <asm/linux_logo.h>
108 #include "console_macros.h"
111 const struct consw *conswitchp;
113 /* A bitmap for codes <32. A bit of 1 indicates that the code
114 * corresponding to that bit number invokes some special action
115 * (such as cursor movement) and should not be displayed as a
116 * glyph unless the disp_ctrl mode is explicitly enabled.
118 #define CTRL_ACTION 0x0d00ff81
119 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
122 * Here is the default bell parameters: 750HZ, 1/8th of a second
124 #define DEFAULT_BELL_PITCH 750
125 #define DEFAULT_BELL_DURATION (HZ/8)
127 extern void vcs_make_devfs (unsigned int index, int unregister);
129 #ifndef MIN
130 #define MIN(a,b) ((a) < (b) ? (a) : (b))
131 #endif
133 static struct tty_struct *console_table[MAX_NR_CONSOLES];
134 static struct termios *console_termios[MAX_NR_CONSOLES];
135 static struct termios *console_termios_locked[MAX_NR_CONSOLES];
136 struct vc vc_cons [MAX_NR_CONSOLES];
138 #ifndef VT_SINGLE_DRIVER
139 static const struct consw *con_driver_map[MAX_NR_CONSOLES];
140 #endif
142 static int con_open(struct tty_struct *, struct file *);
143 static void vc_init(unsigned int console, unsigned int rows,
144 unsigned int cols, int do_clear);
145 static void blank_screen(unsigned long dummy);
146 static void gotoxy(int currcons, int new_x, int new_y);
147 static void save_cur(int currcons);
148 static void reset_terminal(int currcons, int do_clear);
149 static void con_flush_chars(struct tty_struct *tty);
150 static void set_vesa_blanking(unsigned long arg);
151 static void set_cursor(int currcons);
152 static void hide_cursor(int currcons);
153 static void unblank_screen_t(unsigned long dummy);
155 static int printable; /* Is console ready for printing? */
157 int do_poke_blanked_console;
158 int console_blanked;
160 static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
161 static int blankinterval = 10*60*HZ;
162 static int vesa_off_interval;
165 * fg_console is the current virtual console,
166 * last_console is the last used one,
167 * want_console is the console we want to switch to,
168 * kmsg_redirect is the console for kernel messages,
170 int fg_console;
171 int last_console;
172 int want_console = -1;
173 int kmsg_redirect;
176 * For each existing display, we have a pointer to console currently visible
177 * on that display, allowing consoles other than fg_console to be refreshed
178 * appropriately. Unless the low-level driver supplies its own display_fg
179 * variable, we use this one for the "master display".
181 static struct vc_data *master_display_fg;
184 * Unfortunately, we need to delay tty echo when we're currently writing to the
185 * console since the code is (and always was) not re-entrant, so we insert
186 * all filp requests to con_task_queue instead of tq_timer and run it from
187 * the console_tasklet. The console_tasklet is protected by the IRQ
188 * protected console_lock.
190 DECLARE_TASK_QUEUE(con_task_queue);
193 * For the same reason, we defer scrollback to the console tasklet.
195 static int scrollback_delta;
198 * Hook so that the power management routines can (un)blank
199 * the console on our behalf.
201 int (*console_blank_hook)(int);
203 static struct timer_list console_timer;
206 * Low-Level Functions
209 #define IS_FG (currcons == fg_console)
210 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
212 #ifdef VT_BUF_VRAM_ONLY
213 #define DO_UPDATE 0
214 #else
215 #define DO_UPDATE IS_VISIBLE
216 #endif
218 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data);
219 static struct pm_dev *pm_con;
221 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
223 unsigned short *p;
225 if (!viewed)
226 p = (unsigned short *)(origin + offset);
227 else if (!sw->con_screen_pos)
228 p = (unsigned short *)(visible_origin + offset);
229 else
230 p = sw->con_screen_pos(vc_cons[currcons].d, offset);
231 return p;
234 static inline void scrolldelta(int lines)
236 scrollback_delta += lines;
237 tasklet_schedule(&console_tasklet);
240 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
242 unsigned short *d, *s;
244 if (t+nr >= b)
245 nr = b - t - 1;
246 if (b > video_num_lines || t >= b || nr < 1)
247 return;
248 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
249 return;
250 d = (unsigned short *) (origin+video_size_row*t);
251 s = (unsigned short *) (origin+video_size_row*(t+nr));
252 scr_memcpyw(d, s, (b-t-nr) * video_size_row);
253 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
256 static void
257 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
259 unsigned short *s;
260 unsigned int step;
262 if (t+nr >= b)
263 nr = b - t - 1;
264 if (b > video_num_lines || t >= b || nr < 1)
265 return;
266 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
267 return;
268 s = (unsigned short *) (origin+video_size_row*t);
269 step = video_num_columns * nr;
270 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
271 scr_memsetw(s, video_erase_char, 2*step);
274 static void do_update_region(int currcons, unsigned long start, int count)
276 #ifndef VT_BUF_VRAM_ONLY
277 unsigned int xx, yy, offset;
278 u16 *p;
280 p = (u16 *) start;
281 if (!sw->con_getxy) {
282 offset = (start - origin) / 2;
283 xx = offset % video_num_columns;
284 yy = offset / video_num_columns;
285 } else {
286 int nxx, nyy;
287 start = sw->con_getxy(vc_cons[currcons].d, start, &nxx, &nyy);
288 xx = nxx; yy = nyy;
290 for(;;) {
291 u16 attrib = scr_readw(p) & 0xff00;
292 int startx = xx;
293 u16 *q = p;
294 while (xx < video_num_columns && count) {
295 if (attrib != (scr_readw(p) & 0xff00)) {
296 if (p > q)
297 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
298 startx = xx;
299 q = p;
300 attrib = scr_readw(p) & 0xff00;
302 p++;
303 xx++;
304 count--;
306 if (p > q)
307 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
308 if (!count)
309 break;
310 xx = 0;
311 yy++;
312 if (sw->con_getxy) {
313 p = (u16 *)start;
314 start = sw->con_getxy(vc_cons[currcons].d, start, NULL, NULL);
317 #endif
320 void update_region(int currcons, unsigned long start, int count)
322 if (DO_UPDATE) {
323 hide_cursor(currcons);
324 do_update_region(currcons, start, count);
325 set_cursor(currcons);
329 /* Structure of attributes is hardware-dependent */
331 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
333 if (sw->con_build_attr)
334 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
336 #ifndef VT_BUF_VRAM_ONLY
338 * ++roman: I completely changed the attribute format for monochrome
339 * mode (!can_do_color). The formerly used MDA (monochrome display
340 * adapter) format didn't allow the combination of certain effects.
341 * Now the attribute is just a bit vector:
342 * Bit 0..1: intensity (0..2)
343 * Bit 2 : underline
344 * Bit 3 : reverse
345 * Bit 7 : blink
348 u8 a = color;
349 if (!can_do_color)
350 return _intensity |
351 (_underline ? 4 : 0) |
352 (_reverse ? 8 : 0) |
353 (_blink ? 0x80 : 0);
354 if (_underline)
355 a = (a & 0xf0) | ulcolor;
356 else if (_intensity == 0)
357 a = (a & 0xf0) | halfcolor;
358 if (_reverse)
359 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
360 if (_blink)
361 a ^= 0x80;
362 if (_intensity == 2)
363 a ^= 0x08;
364 if (hi_font_mask == 0x100)
365 a <<= 1;
366 return a;
368 #else
369 return 0;
370 #endif
373 static void update_attr(int currcons)
375 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
376 video_erase_char = (build_attr(currcons, color, 1, blink, 0, decscnm) << 8) | ' ';
379 /* Note: inverting the screen twice should revert to the original state */
381 void invert_screen(int currcons, int offset, int count, int viewed)
383 unsigned short *p;
385 count /= 2;
386 p = screenpos(currcons, offset, viewed);
387 if (sw->con_invert_region)
388 sw->con_invert_region(vc_cons[currcons].d, p, count);
389 #ifndef VT_BUF_VRAM_ONLY
390 else {
391 u16 *q = p;
392 int cnt = count;
394 if (!can_do_color) {
395 while (cnt--) *q++ ^= 0x0800;
396 } else if (hi_font_mask == 0x100) {
397 while (cnt--) {
398 u16 a = *q;
399 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
400 *q++ = a;
402 } else {
403 while (cnt--) {
404 u16 a = *q;
405 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
406 *q++ = a;
410 #endif
411 if (DO_UPDATE)
412 do_update_region(currcons, (unsigned long) p, count);
415 /* used by selection: complement pointer position */
416 void complement_pos(int currcons, int offset)
418 static unsigned short *p;
419 static unsigned short old;
420 static unsigned short oldx, oldy;
422 if (p) {
423 scr_writew(old, p);
424 if (DO_UPDATE)
425 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
427 if (offset == -1)
428 p = NULL;
429 else {
430 unsigned short new;
431 p = screenpos(currcons, offset, 1);
432 old = scr_readw(p);
433 new = old ^ complement_mask;
434 scr_writew(new, p);
435 if (DO_UPDATE) {
436 oldx = (offset >> 1) % video_num_columns;
437 oldy = (offset >> 1) / video_num_columns;
438 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
443 static void insert_char(int currcons, unsigned int nr)
445 unsigned short *p, *q = (unsigned short *) pos;
447 p = q + video_num_columns - nr - x;
448 while (--p >= q)
449 scr_writew(scr_readw(p), p + nr);
450 scr_memsetw(q, video_erase_char, nr*2);
451 need_wrap = 0;
452 if (DO_UPDATE) {
453 unsigned short oldattr = attr;
454 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
455 video_num_columns-x-nr);
456 attr = video_erase_char >> 8;
457 while (nr--)
458 sw->con_putc(vc_cons[currcons].d,
459 video_erase_char,y,x+nr);
460 attr = oldattr;
464 static void delete_char(int currcons, unsigned int nr)
466 unsigned int i = x;
467 unsigned short *p = (unsigned short *) pos;
469 while (++i <= video_num_columns - nr) {
470 scr_writew(scr_readw(p+nr), p);
471 p++;
473 scr_memsetw(p, video_erase_char, nr*2);
474 need_wrap = 0;
475 if (DO_UPDATE) {
476 unsigned short oldattr = attr;
477 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
478 video_num_columns-x-nr);
479 attr = video_erase_char >> 8;
480 while (nr--)
481 sw->con_putc(vc_cons[currcons].d,
482 video_erase_char, y,
483 video_num_columns-1-nr);
484 attr = oldattr;
488 static int softcursor_original;
490 static void add_softcursor(int currcons)
492 int i = scr_readw((u16 *) pos);
493 u32 type = cursor_type;
495 if (! (type & 0x10)) return;
496 if (softcursor_original != -1) return;
497 softcursor_original = i;
498 i |= ((type >> 8) & 0xff00 );
499 i ^= ((type) & 0xff00 );
500 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
501 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
502 scr_writew(i, (u16 *) pos);
503 if (DO_UPDATE)
504 sw->con_putc(vc_cons[currcons].d, i, y, x);
507 static void hide_cursor(int currcons)
509 if (currcons == sel_cons)
510 clear_selection();
511 if (softcursor_original != -1) {
512 scr_writew(softcursor_original,(u16 *) pos);
513 if (DO_UPDATE)
514 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
515 softcursor_original = -1;
517 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
520 static void set_cursor(int currcons)
522 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
523 return;
524 if (deccm) {
525 if (currcons == sel_cons)
526 clear_selection();
527 add_softcursor(currcons);
528 if ((cursor_type & 0x0f) != 1)
529 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
530 } else
531 hide_cursor(currcons);
534 static void set_origin(int currcons)
536 if (!IS_VISIBLE ||
537 !sw->con_set_origin ||
538 !sw->con_set_origin(vc_cons[currcons].d))
539 origin = (unsigned long) screenbuf;
540 visible_origin = origin;
541 scr_end = origin + screenbuf_size;
542 pos = origin + video_size_row*y + 2*x;
545 static inline void save_screen(int currcons)
547 if (sw->con_save_screen)
548 sw->con_save_screen(vc_cons[currcons].d);
552 * Redrawing of screen
555 void redraw_screen(int new_console, int is_switch)
557 int redraw = 1;
558 int currcons, old_console;
560 if (!vc_cons_allocated(new_console)) {
561 /* strange ... */
562 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
563 return;
566 if (is_switch) {
567 currcons = fg_console;
568 hide_cursor(currcons);
569 if (fg_console != new_console) {
570 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
571 old_console = (*display) ? (*display)->vc_num : fg_console;
572 *display = vc_cons[new_console].d;
573 fg_console = new_console;
574 currcons = old_console;
575 if (!IS_VISIBLE) {
576 save_screen(currcons);
577 set_origin(currcons);
579 currcons = new_console;
580 if (old_console == new_console)
581 redraw = 0;
583 } else {
584 currcons = new_console;
585 hide_cursor(currcons);
588 if (redraw) {
589 int update;
590 set_origin(currcons);
591 update = sw->con_switch(vc_cons[currcons].d);
592 set_palette(currcons);
593 if (update && vcmode != KD_GRAPHICS)
594 do_update_region(currcons, origin, screenbuf_size/2);
596 set_cursor(currcons);
597 if (is_switch) {
598 set_leds();
599 compute_shiftstate();
604 * Allocation, freeing and resizing of VTs.
607 int vc_cons_allocated(unsigned int i)
609 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
612 static void visual_init(int currcons, int init)
614 /* ++Geert: sw->con_init determines console size */
615 sw = conswitchp;
616 #ifndef VT_SINGLE_DRIVER
617 if (con_driver_map[currcons])
618 sw = con_driver_map[currcons];
619 #endif
620 cons_num = currcons;
621 display_fg = &master_display_fg;
622 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
623 vc_cons[currcons].d->vc_uni_pagedir = 0;
624 hi_font_mask = 0;
625 complement_mask = 0;
626 can_do_color = 0;
627 sw->con_init(vc_cons[currcons].d, init);
628 if (!complement_mask)
629 complement_mask = can_do_color ? 0x7700 : 0x0800;
630 s_complement_mask = complement_mask;
631 video_size_row = video_num_columns<<1;
632 screenbuf_size = video_num_lines*video_size_row;
635 int vc_allocate(unsigned int currcons) /* return 0 on success */
637 if (currcons >= MAX_NR_CONSOLES)
638 return -ENXIO;
639 if (!vc_cons[currcons].d) {
640 long p, q;
642 /* prevent users from taking too much memory */
643 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
644 return -EPERM;
646 /* due to the granularity of kmalloc, we waste some memory here */
647 /* the alloc is done in two steps, to optimize the common situation
648 of a 25x80 console (structsize=216, screenbuf_size=4000) */
649 /* although the numbers above are not valid since long ago, the
650 point is still up-to-date and the comment still has its value
651 even if only as a historical artifact. --mj, July 1998 */
652 p = (long) kmalloc(structsize, GFP_KERNEL);
653 if (!p)
654 return -ENOMEM;
655 vc_cons[currcons].d = (struct vc_data *)p;
656 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
657 visual_init(currcons, 1);
658 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
659 con_set_default_unimap(currcons);
660 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
661 if (!q) {
662 kfree((char *) p);
663 vc_cons[currcons].d = NULL;
664 vt_cons[currcons] = NULL;
665 return -ENOMEM;
667 screenbuf = (unsigned short *) q;
668 kmalloced = 1;
669 vc_init(currcons, video_num_lines, video_num_columns, 1);
671 if (!pm_con) {
672 pm_con = pm_register(PM_SYS_DEV,
673 PM_SYS_VGA,
674 pm_con_request);
677 return 0;
681 * Change # of rows and columns (0 means unchanged/the size of fg_console)
682 * [this is to be used together with some user program
683 * like resize that changes the hardware videomode]
685 int vc_resize(unsigned int lines, unsigned int cols,
686 unsigned int first, unsigned int last)
688 unsigned int cc, ll, ss, sr, todo = 0;
689 unsigned int currcons = fg_console, i;
690 unsigned short *newscreens[MAX_NR_CONSOLES];
692 cc = (cols ? cols : video_num_columns);
693 ll = (lines ? lines : video_num_lines);
694 sr = cc << 1;
695 ss = sr * ll;
697 for (currcons = first; currcons <= last; currcons++) {
698 if (!vc_cons_allocated(currcons) ||
699 (cc == video_num_columns && ll == video_num_lines))
700 newscreens[currcons] = NULL;
701 else {
702 unsigned short *p = (unsigned short *) kmalloc(ss, GFP_USER);
703 if (!p) {
704 for (i = first; i < currcons; i++)
705 if (newscreens[i])
706 kfree(newscreens[i]);
707 return -ENOMEM;
709 newscreens[currcons] = p;
710 todo++;
713 if (!todo)
714 return 0;
716 for (currcons = first; currcons <= last; currcons++) {
717 unsigned int occ, oll, oss, osr;
718 unsigned long ol, nl, nlend, rlth, rrem;
719 if (!newscreens[currcons] || !vc_cons_allocated(currcons))
720 continue;
722 oll = video_num_lines;
723 occ = video_num_columns;
724 osr = video_size_row;
725 oss = screenbuf_size;
727 video_num_lines = ll;
728 video_num_columns = cc;
729 video_size_row = sr;
730 screenbuf_size = ss;
732 rlth = MIN(osr, sr);
733 rrem = sr - rlth;
734 ol = origin;
735 nl = (long) newscreens[currcons];
736 nlend = nl + ss;
737 if (ll < oll)
738 ol += (oll - ll) * osr;
740 update_attr(currcons);
742 while (ol < scr_end) {
743 scr_memcpyw((unsigned short *) nl, (unsigned short *) ol, rlth);
744 if (rrem)
745 scr_memsetw((void *)(nl + rlth), video_erase_char, rrem);
746 ol += osr;
747 nl += sr;
749 if (nlend > nl)
750 scr_memsetw((void *) nl, video_erase_char, nlend - nl);
751 if (kmalloced)
752 kfree(screenbuf);
753 screenbuf = newscreens[currcons];
754 kmalloced = 1;
755 screenbuf_size = ss;
756 set_origin(currcons);
758 /* do part of a reset_terminal() */
759 top = 0;
760 bottom = video_num_lines;
761 gotoxy(currcons, x, y);
762 save_cur(currcons);
764 if (console_table[currcons]) {
765 struct winsize ws, *cws = &console_table[currcons]->winsize;
766 memset(&ws, 0, sizeof(ws));
767 ws.ws_row = video_num_lines;
768 ws.ws_col = video_num_columns;
769 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
770 console_table[currcons]->pgrp > 0)
771 kill_pg(console_table[currcons]->pgrp, SIGWINCH, 1);
772 *cws = ws;
775 if (IS_VISIBLE)
776 update_screen(currcons);
779 return 0;
783 void vc_disallocate(unsigned int currcons)
785 if (vc_cons_allocated(currcons)) {
786 sw->con_deinit(vc_cons[currcons].d);
787 if (kmalloced)
788 kfree(screenbuf);
789 if (currcons >= MIN_NR_CONSOLES)
790 kfree(vc_cons[currcons].d);
791 vc_cons[currcons].d = NULL;
796 * VT102 emulator
799 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
800 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
801 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
803 #define decarm VC_REPEAT
804 #define decckm VC_CKMODE
805 #define kbdapplic VC_APPLIC
806 #define lnm VC_CRLF
809 * this is what the terminal answers to a ESC-Z or csi0c query.
811 #define VT100ID "\033[?1;2c"
812 #define VT102ID "\033[?6c"
814 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
815 8,12,10,14, 9,13,11,15 };
817 /* the default colour table, for VGA+ colour systems */
818 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
819 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
820 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
821 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
822 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
823 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
826 * gotoxy() must verify all boundaries, because the arguments
827 * might also be negative. If the given position is out of
828 * bounds, the cursor is placed at the nearest margin.
830 static void gotoxy(int currcons, int new_x, int new_y)
832 int min_y, max_y;
834 if (new_x < 0)
835 x = 0;
836 else
837 if (new_x >= video_num_columns)
838 x = video_num_columns - 1;
839 else
840 x = new_x;
841 if (decom) {
842 min_y = top;
843 max_y = bottom;
844 } else {
845 min_y = 0;
846 max_y = video_num_lines;
848 if (new_y < min_y)
849 y = min_y;
850 else if (new_y >= max_y)
851 y = max_y - 1;
852 else
853 y = new_y;
854 pos = origin + y*video_size_row + (x<<1);
855 need_wrap = 0;
858 /* for absolute user moves, when decom is set */
859 static void gotoxay(int currcons, int new_x, int new_y)
861 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
864 void scrollback(int lines)
866 int currcons = fg_console;
868 if (!lines)
869 lines = video_num_lines/2;
870 scrolldelta(-lines);
873 void scrollfront(int lines)
875 int currcons = fg_console;
877 if (!lines)
878 lines = video_num_lines/2;
879 scrolldelta(lines);
882 static void lf(int currcons)
884 /* don't scroll if above bottom of scrolling region, or
885 * if below scrolling region
887 if (y+1 == bottom)
888 scrup(currcons,top,bottom,1);
889 else if (y < video_num_lines-1) {
890 y++;
891 pos += video_size_row;
893 need_wrap = 0;
896 static void ri(int currcons)
898 /* don't scroll if below top of scrolling region, or
899 * if above scrolling region
901 if (y == top)
902 scrdown(currcons,top,bottom,1);
903 else if (y > 0) {
904 y--;
905 pos -= video_size_row;
907 need_wrap = 0;
910 static inline void cr(int currcons)
912 pos -= x<<1;
913 need_wrap = x = 0;
916 static inline void bs(int currcons)
918 if (x) {
919 pos -= 2;
920 x--;
921 need_wrap = 0;
925 static inline void del(int currcons)
927 /* ignored */
930 static void csi_J(int currcons, int vpar)
932 unsigned int count;
933 unsigned short * start;
935 switch (vpar) {
936 case 0: /* erase from cursor to end of display */
937 count = (scr_end-pos)>>1;
938 start = (unsigned short *) pos;
939 if (DO_UPDATE) {
940 /* do in two stages */
941 sw->con_clear(vc_cons[currcons].d, y, x, 1,
942 video_num_columns-x);
943 sw->con_clear(vc_cons[currcons].d, y+1, 0,
944 video_num_lines-y-1,
945 video_num_columns);
947 break;
948 case 1: /* erase from start to cursor */
949 count = ((pos-origin)>>1)+1;
950 start = (unsigned short *) origin;
951 if (DO_UPDATE) {
952 /* do in two stages */
953 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
954 video_num_columns);
955 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
956 x + 1);
958 break;
959 case 2: /* erase whole display */
960 count = video_num_columns * video_num_lines;
961 start = (unsigned short *) origin;
962 if (DO_UPDATE)
963 sw->con_clear(vc_cons[currcons].d, 0, 0,
964 video_num_lines,
965 video_num_columns);
966 break;
967 default:
968 return;
970 scr_memsetw(start, video_erase_char, 2*count);
971 need_wrap = 0;
974 static void csi_K(int currcons, int vpar)
976 unsigned int count;
977 unsigned short * start;
979 switch (vpar) {
980 case 0: /* erase from cursor to end of line */
981 count = video_num_columns-x;
982 start = (unsigned short *) pos;
983 if (DO_UPDATE)
984 sw->con_clear(vc_cons[currcons].d, y, x, 1,
985 video_num_columns-x);
986 break;
987 case 1: /* erase from start of line to cursor */
988 start = (unsigned short *) (pos - (x<<1));
989 count = x+1;
990 if (DO_UPDATE)
991 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
992 x + 1);
993 break;
994 case 2: /* erase whole line */
995 start = (unsigned short *) (pos - (x<<1));
996 count = video_num_columns;
997 if (DO_UPDATE)
998 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
999 video_num_columns);
1000 break;
1001 default:
1002 return;
1004 scr_memsetw(start, video_erase_char, 2 * count);
1005 need_wrap = 0;
1008 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
1009 { /* not vt100? */
1010 int count;
1012 if (!vpar)
1013 vpar++;
1014 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
1016 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
1017 if (DO_UPDATE)
1018 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
1019 need_wrap = 0;
1022 static void default_attr(int currcons)
1024 intensity = 1;
1025 underline = 0;
1026 reverse = 0;
1027 blink = 0;
1028 color = def_color;
1031 static void csi_m(int currcons)
1033 int i;
1035 for (i=0;i<=npar;i++)
1036 switch (par[i]) {
1037 case 0: /* all attributes off */
1038 default_attr(currcons);
1039 break;
1040 case 1:
1041 intensity = 2;
1042 break;
1043 case 2:
1044 intensity = 0;
1045 break;
1046 case 4:
1047 underline = 1;
1048 break;
1049 case 5:
1050 blink = 1;
1051 break;
1052 case 7:
1053 reverse = 1;
1054 break;
1055 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1056 * Select primary font, don't display
1057 * control chars if defined, don't set
1058 * bit 8 on output.
1060 translate = set_translate(charset == 0
1061 ? G0_charset
1062 : G1_charset,currcons);
1063 disp_ctrl = 0;
1064 toggle_meta = 0;
1065 break;
1066 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1067 * Select first alternate font, lets
1068 * chars < 32 be displayed as ROM chars.
1070 translate = set_translate(IBMPC_MAP,currcons);
1071 disp_ctrl = 1;
1072 toggle_meta = 0;
1073 break;
1074 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1075 * Select second alternate font, toggle
1076 * high bit before displaying as ROM char.
1078 translate = set_translate(IBMPC_MAP,currcons);
1079 disp_ctrl = 1;
1080 toggle_meta = 1;
1081 break;
1082 case 21:
1083 case 22:
1084 intensity = 1;
1085 break;
1086 case 24:
1087 underline = 0;
1088 break;
1089 case 25:
1090 blink = 0;
1091 break;
1092 case 27:
1093 reverse = 0;
1094 break;
1095 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1096 * Enables underscore, white foreground
1097 * with white underscore (Linux - use
1098 * default foreground).
1100 color = (def_color & 0x0f) | background;
1101 underline = 1;
1102 break;
1103 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1104 * Disable underline option.
1105 * Reset colour to default? It did this
1106 * before...
1108 color = (def_color & 0x0f) | background;
1109 underline = 0;
1110 break;
1111 case 49:
1112 color = (def_color & 0xf0) | foreground;
1113 break;
1114 default:
1115 if (par[i] >= 30 && par[i] <= 37)
1116 color = color_table[par[i]-30]
1117 | background;
1118 else if (par[i] >= 40 && par[i] <= 47)
1119 color = (color_table[par[i]-40]<<4)
1120 | foreground;
1121 break;
1123 update_attr(currcons);
1126 static void respond_string(const char * p, struct tty_struct * tty)
1128 while (*p) {
1129 tty_insert_flip_char(tty, *p, 0);
1130 p++;
1132 con_schedule_flip(tty);
1135 static void cursor_report(int currcons, struct tty_struct * tty)
1137 char buf[40];
1139 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1140 respond_string(buf, tty);
1143 static inline void status_report(struct tty_struct * tty)
1145 respond_string("\033[0n", tty); /* Terminal ok */
1148 static inline void respond_ID(struct tty_struct * tty)
1150 respond_string(VT102ID, tty);
1153 void mouse_report(struct tty_struct * tty, int butt, int mrx, int mry)
1155 char buf[8];
1157 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1158 (char)('!' + mry));
1159 respond_string(buf, tty);
1162 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1163 int mouse_reporting(void)
1165 int currcons = fg_console;
1167 return report_mouse;
1170 static void set_mode(int currcons, int on_off)
1172 int i;
1174 for (i=0; i<=npar; i++)
1175 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1176 case 1: /* Cursor keys send ^[Ox/^[[x */
1177 if (on_off)
1178 set_kbd(decckm);
1179 else
1180 clr_kbd(decckm);
1181 break;
1182 case 3: /* 80/132 mode switch unimplemented */
1183 deccolm = on_off;
1184 #if 0
1185 (void) vc_resize(video_num_lines, deccolm ? 132 : 80);
1186 /* this alone does not suffice; some user mode
1187 utility has to change the hardware regs */
1188 #endif
1189 break;
1190 case 5: /* Inverted screen on/off */
1191 if (decscnm != on_off) {
1192 decscnm = on_off;
1193 invert_screen(currcons, 0, screenbuf_size, 0);
1194 update_attr(currcons);
1196 break;
1197 case 6: /* Origin relative/absolute */
1198 decom = on_off;
1199 gotoxay(currcons,0,0);
1200 break;
1201 case 7: /* Autowrap on/off */
1202 decawm = on_off;
1203 break;
1204 case 8: /* Autorepeat on/off */
1205 if (on_off)
1206 set_kbd(decarm);
1207 else
1208 clr_kbd(decarm);
1209 break;
1210 case 9:
1211 report_mouse = on_off ? 1 : 0;
1212 break;
1213 case 25: /* Cursor on/off */
1214 deccm = on_off;
1215 break;
1216 case 1000:
1217 report_mouse = on_off ? 2 : 0;
1218 break;
1219 } else switch(par[i]) { /* ANSI modes set/reset */
1220 case 3: /* Monitor (display ctrls) */
1221 disp_ctrl = on_off;
1222 break;
1223 case 4: /* Insert Mode on/off */
1224 decim = on_off;
1225 break;
1226 case 20: /* Lf, Enter == CrLf/Lf */
1227 if (on_off)
1228 set_kbd(lnm);
1229 else
1230 clr_kbd(lnm);
1231 break;
1235 static void setterm_command(int currcons)
1237 switch(par[0]) {
1238 case 1: /* set color for underline mode */
1239 if (can_do_color && par[1] < 16) {
1240 ulcolor = color_table[par[1]];
1241 if (underline)
1242 update_attr(currcons);
1244 break;
1245 case 2: /* set color for half intensity mode */
1246 if (can_do_color && par[1] < 16) {
1247 halfcolor = color_table[par[1]];
1248 if (intensity == 0)
1249 update_attr(currcons);
1251 break;
1252 case 8: /* store colors as defaults */
1253 def_color = attr;
1254 if (hi_font_mask == 0x100)
1255 def_color >>= 1;
1256 default_attr(currcons);
1257 update_attr(currcons);
1258 break;
1259 case 9: /* set blanking interval */
1260 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1261 poke_blanked_console();
1262 break;
1263 case 10: /* set bell frequency in Hz */
1264 if (npar >= 1)
1265 bell_pitch = par[1];
1266 else
1267 bell_pitch = DEFAULT_BELL_PITCH;
1268 break;
1269 case 11: /* set bell duration in msec */
1270 if (npar >= 1)
1271 bell_duration = (par[1] < 2000) ?
1272 par[1]*HZ/1000 : 0;
1273 else
1274 bell_duration = DEFAULT_BELL_DURATION;
1275 break;
1276 case 12: /* bring specified console to the front */
1277 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1278 set_console(par[1] - 1);
1279 break;
1280 case 13: /* unblank the screen */
1281 poke_blanked_console();
1282 break;
1283 case 14: /* set vesa powerdown interval */
1284 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1285 break;
1289 static void insert_line(int currcons, unsigned int nr)
1291 scrdown(currcons,y,bottom,nr);
1292 need_wrap = 0;
1296 static void delete_line(int currcons, unsigned int nr)
1298 scrup(currcons,y,bottom,nr);
1299 need_wrap = 0;
1302 static void csi_at(int currcons, unsigned int nr)
1304 if (nr > video_num_columns - x)
1305 nr = video_num_columns - x;
1306 else if (!nr)
1307 nr = 1;
1308 insert_char(currcons, nr);
1311 static void csi_L(int currcons, unsigned int nr)
1313 if (nr > video_num_lines - y)
1314 nr = video_num_lines - y;
1315 else if (!nr)
1316 nr = 1;
1317 insert_line(currcons, nr);
1320 static void csi_P(int currcons, unsigned int nr)
1322 if (nr > video_num_columns - x)
1323 nr = video_num_columns - x;
1324 else if (!nr)
1325 nr = 1;
1326 delete_char(currcons, nr);
1329 static void csi_M(int currcons, unsigned int nr)
1331 if (nr > video_num_lines - y)
1332 nr = video_num_lines - y;
1333 else if (!nr)
1334 nr=1;
1335 delete_line(currcons, nr);
1338 static void save_cur(int currcons)
1340 saved_x = x;
1341 saved_y = y;
1342 s_intensity = intensity;
1343 s_underline = underline;
1344 s_blink = blink;
1345 s_reverse = reverse;
1346 s_charset = charset;
1347 s_color = color;
1348 saved_G0 = G0_charset;
1349 saved_G1 = G1_charset;
1352 static void restore_cur(int currcons)
1354 gotoxy(currcons,saved_x,saved_y);
1355 intensity = s_intensity;
1356 underline = s_underline;
1357 blink = s_blink;
1358 reverse = s_reverse;
1359 charset = s_charset;
1360 color = s_color;
1361 G0_charset = saved_G0;
1362 G1_charset = saved_G1;
1363 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1364 update_attr(currcons);
1365 need_wrap = 0;
1368 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1369 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1370 ESpalette };
1372 static void reset_terminal(int currcons, int do_clear)
1374 top = 0;
1375 bottom = video_num_lines;
1376 vc_state = ESnormal;
1377 ques = 0;
1378 translate = set_translate(LAT1_MAP,currcons);
1379 G0_charset = LAT1_MAP;
1380 G1_charset = GRAF_MAP;
1381 charset = 0;
1382 need_wrap = 0;
1383 report_mouse = 0;
1384 utf = 0;
1385 utf_count = 0;
1387 disp_ctrl = 0;
1388 toggle_meta = 0;
1390 decscnm = 0;
1391 decom = 0;
1392 decawm = 1;
1393 deccm = 1;
1394 decim = 0;
1396 set_kbd(decarm);
1397 clr_kbd(decckm);
1398 clr_kbd(kbdapplic);
1399 clr_kbd(lnm);
1400 kbd_table[currcons].lockstate = 0;
1401 kbd_table[currcons].slockstate = 0;
1402 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1403 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1404 set_leds();
1406 cursor_type = CUR_DEFAULT;
1407 complement_mask = s_complement_mask;
1409 default_attr(currcons);
1410 update_attr(currcons);
1412 tab_stop[0] = 0x01010100;
1413 tab_stop[1] =
1414 tab_stop[2] =
1415 tab_stop[3] =
1416 tab_stop[4] = 0x01010101;
1418 bell_pitch = DEFAULT_BELL_PITCH;
1419 bell_duration = DEFAULT_BELL_DURATION;
1421 gotoxy(currcons,0,0);
1422 save_cur(currcons);
1423 if (do_clear)
1424 csi_J(currcons,2);
1427 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1430 * Control characters can be used in the _middle_
1431 * of an escape sequence.
1433 switch (c) {
1434 case 0:
1435 return;
1436 case 7:
1437 if (bell_duration)
1438 kd_mksound(bell_pitch, bell_duration);
1439 return;
1440 case 8:
1441 bs(currcons);
1442 return;
1443 case 9:
1444 pos -= (x << 1);
1445 while (x < video_num_columns - 1) {
1446 x++;
1447 if (tab_stop[x >> 5] & (1 << (x & 31)))
1448 break;
1450 pos += (x << 1);
1451 return;
1452 case 10: case 11: case 12:
1453 lf(currcons);
1454 if (!is_kbd(lnm))
1455 return;
1456 case 13:
1457 cr(currcons);
1458 return;
1459 case 14:
1460 charset = 1;
1461 translate = set_translate(G1_charset,currcons);
1462 disp_ctrl = 1;
1463 return;
1464 case 15:
1465 charset = 0;
1466 translate = set_translate(G0_charset,currcons);
1467 disp_ctrl = 0;
1468 return;
1469 case 24: case 26:
1470 vc_state = ESnormal;
1471 return;
1472 case 27:
1473 vc_state = ESesc;
1474 return;
1475 case 127:
1476 del(currcons);
1477 return;
1478 case 128+27:
1479 vc_state = ESsquare;
1480 return;
1482 switch(vc_state) {
1483 case ESesc:
1484 vc_state = ESnormal;
1485 switch (c) {
1486 case '[':
1487 vc_state = ESsquare;
1488 return;
1489 case ']':
1490 vc_state = ESnonstd;
1491 return;
1492 case '%':
1493 vc_state = ESpercent;
1494 return;
1495 case 'E':
1496 cr(currcons);
1497 lf(currcons);
1498 return;
1499 case 'M':
1500 ri(currcons);
1501 return;
1502 case 'D':
1503 lf(currcons);
1504 return;
1505 case 'H':
1506 tab_stop[x >> 5] |= (1 << (x & 31));
1507 return;
1508 case 'Z':
1509 respond_ID(tty);
1510 return;
1511 case '7':
1512 save_cur(currcons);
1513 return;
1514 case '8':
1515 restore_cur(currcons);
1516 return;
1517 case '(':
1518 vc_state = ESsetG0;
1519 return;
1520 case ')':
1521 vc_state = ESsetG1;
1522 return;
1523 case '#':
1524 vc_state = EShash;
1525 return;
1526 case 'c':
1527 reset_terminal(currcons,1);
1528 return;
1529 case '>': /* Numeric keypad */
1530 clr_kbd(kbdapplic);
1531 return;
1532 case '=': /* Appl. keypad */
1533 set_kbd(kbdapplic);
1534 return;
1536 return;
1537 case ESnonstd:
1538 if (c=='P') { /* palette escape sequence */
1539 for (npar=0; npar<NPAR; npar++)
1540 par[npar] = 0 ;
1541 npar = 0 ;
1542 vc_state = ESpalette;
1543 return;
1544 } else if (c=='R') { /* reset palette */
1545 reset_palette(currcons);
1546 vc_state = ESnormal;
1547 } else
1548 vc_state = ESnormal;
1549 return;
1550 case ESpalette:
1551 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1552 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1553 if (npar==7) {
1554 int i = par[0]*3, j = 1;
1555 palette[i] = 16*par[j++];
1556 palette[i++] += par[j++];
1557 palette[i] = 16*par[j++];
1558 palette[i++] += par[j++];
1559 palette[i] = 16*par[j++];
1560 palette[i] += par[j];
1561 set_palette(currcons);
1562 vc_state = ESnormal;
1564 } else
1565 vc_state = ESnormal;
1566 return;
1567 case ESsquare:
1568 for(npar = 0 ; npar < NPAR ; npar++)
1569 par[npar] = 0;
1570 npar = 0;
1571 vc_state = ESgetpars;
1572 if (c == '[') { /* Function key */
1573 vc_state=ESfunckey;
1574 return;
1576 ques = (c=='?');
1577 if (ques)
1578 return;
1579 case ESgetpars:
1580 if (c==';' && npar<NPAR-1) {
1581 npar++;
1582 return;
1583 } else if (c>='0' && c<='9') {
1584 par[npar] *= 10;
1585 par[npar] += c-'0';
1586 return;
1587 } else vc_state=ESgotpars;
1588 case ESgotpars:
1589 vc_state = ESnormal;
1590 switch(c) {
1591 case 'h':
1592 set_mode(currcons,1);
1593 return;
1594 case 'l':
1595 set_mode(currcons,0);
1596 return;
1597 case 'c':
1598 if (ques) {
1599 if (par[0])
1600 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1601 else
1602 cursor_type = CUR_DEFAULT;
1603 return;
1605 break;
1606 case 'm':
1607 if (ques) {
1608 clear_selection();
1609 if (par[0])
1610 complement_mask = par[0]<<8 | par[1];
1611 else
1612 complement_mask = s_complement_mask;
1613 return;
1615 break;
1616 case 'n':
1617 if (!ques) {
1618 if (par[0] == 5)
1619 status_report(tty);
1620 else if (par[0] == 6)
1621 cursor_report(currcons,tty);
1623 return;
1625 if (ques) {
1626 ques = 0;
1627 return;
1629 switch(c) {
1630 case 'G': case '`':
1631 if (par[0]) par[0]--;
1632 gotoxy(currcons,par[0],y);
1633 return;
1634 case 'A':
1635 if (!par[0]) par[0]++;
1636 gotoxy(currcons,x,y-par[0]);
1637 return;
1638 case 'B': case 'e':
1639 if (!par[0]) par[0]++;
1640 gotoxy(currcons,x,y+par[0]);
1641 return;
1642 case 'C': case 'a':
1643 if (!par[0]) par[0]++;
1644 gotoxy(currcons,x+par[0],y);
1645 return;
1646 case 'D':
1647 if (!par[0]) par[0]++;
1648 gotoxy(currcons,x-par[0],y);
1649 return;
1650 case 'E':
1651 if (!par[0]) par[0]++;
1652 gotoxy(currcons,0,y+par[0]);
1653 return;
1654 case 'F':
1655 if (!par[0]) par[0]++;
1656 gotoxy(currcons,0,y-par[0]);
1657 return;
1658 case 'd':
1659 if (par[0]) par[0]--;
1660 gotoxay(currcons,x,par[0]);
1661 return;
1662 case 'H': case 'f':
1663 if (par[0]) par[0]--;
1664 if (par[1]) par[1]--;
1665 gotoxay(currcons,par[1],par[0]);
1666 return;
1667 case 'J':
1668 csi_J(currcons,par[0]);
1669 return;
1670 case 'K':
1671 csi_K(currcons,par[0]);
1672 return;
1673 case 'L':
1674 csi_L(currcons,par[0]);
1675 return;
1676 case 'M':
1677 csi_M(currcons,par[0]);
1678 return;
1679 case 'P':
1680 csi_P(currcons,par[0]);
1681 return;
1682 case 'c':
1683 if (!par[0])
1684 respond_ID(tty);
1685 return;
1686 case 'g':
1687 if (!par[0])
1688 tab_stop[x >> 5] &= ~(1 << (x & 31));
1689 else if (par[0] == 3) {
1690 tab_stop[0] =
1691 tab_stop[1] =
1692 tab_stop[2] =
1693 tab_stop[3] =
1694 tab_stop[4] = 0;
1696 return;
1697 case 'm':
1698 csi_m(currcons);
1699 return;
1700 case 'q': /* DECLL - but only 3 leds */
1701 /* map 0,1,2,3 to 0,1,2,4 */
1702 if (par[0] < 4)
1703 setledstate(kbd_table + currcons,
1704 (par[0] < 3) ? par[0] : 4);
1705 return;
1706 case 'r':
1707 if (!par[0])
1708 par[0]++;
1709 if (!par[1])
1710 par[1] = video_num_lines;
1711 /* Minimum allowed region is 2 lines */
1712 if (par[0] < par[1] &&
1713 par[1] <= video_num_lines) {
1714 top=par[0]-1;
1715 bottom=par[1];
1716 gotoxay(currcons,0,0);
1718 return;
1719 case 's':
1720 save_cur(currcons);
1721 return;
1722 case 'u':
1723 restore_cur(currcons);
1724 return;
1725 case 'X':
1726 csi_X(currcons, par[0]);
1727 return;
1728 case '@':
1729 csi_at(currcons,par[0]);
1730 return;
1731 case ']': /* setterm functions */
1732 setterm_command(currcons);
1733 return;
1735 return;
1736 case ESpercent:
1737 vc_state = ESnormal;
1738 switch (c) {
1739 case '@': /* defined in ISO 2022 */
1740 utf = 0;
1741 return;
1742 case 'G': /* prelim official escape code */
1743 case '8': /* retained for compatibility */
1744 utf = 1;
1745 return;
1747 return;
1748 case ESfunckey:
1749 vc_state = ESnormal;
1750 return;
1751 case EShash:
1752 vc_state = ESnormal;
1753 if (c == '8') {
1754 /* DEC screen alignment test. kludge :-) */
1755 video_erase_char =
1756 (video_erase_char & 0xff00) | 'E';
1757 csi_J(currcons, 2);
1758 video_erase_char =
1759 (video_erase_char & 0xff00) | ' ';
1760 do_update_region(currcons, origin, screenbuf_size/2);
1762 return;
1763 case ESsetG0:
1764 if (c == '0')
1765 G0_charset = GRAF_MAP;
1766 else if (c == 'B')
1767 G0_charset = LAT1_MAP;
1768 else if (c == 'U')
1769 G0_charset = IBMPC_MAP;
1770 else if (c == 'K')
1771 G0_charset = USER_MAP;
1772 if (charset == 0)
1773 translate = set_translate(G0_charset,currcons);
1774 vc_state = ESnormal;
1775 return;
1776 case ESsetG1:
1777 if (c == '0')
1778 G1_charset = GRAF_MAP;
1779 else if (c == 'B')
1780 G1_charset = LAT1_MAP;
1781 else if (c == 'U')
1782 G1_charset = IBMPC_MAP;
1783 else if (c == 'K')
1784 G1_charset = USER_MAP;
1785 if (charset == 1)
1786 translate = set_translate(G1_charset,currcons);
1787 vc_state = ESnormal;
1788 return;
1789 default:
1790 vc_state = ESnormal;
1794 /* This is a temporary buffer used to prepare a tty console write
1795 * so that we can easily avoid touching user space while holding the
1796 * console spinlock. It is allocated in con_init and is shared by
1797 * this code and the vc_screen read/write tty calls.
1799 * We have to allocate this statically in the kernel data section
1800 * since console_init (and thus con_init) are called before any
1801 * kernel memory allocation is available.
1803 char con_buf[PAGE_SIZE];
1804 #define CON_BUF_SIZE PAGE_SIZE
1805 DECLARE_MUTEX(con_buf_sem);
1807 static int do_con_write(struct tty_struct * tty, int from_user,
1808 const unsigned char *buf, int count)
1810 #ifdef VT_BUF_VRAM_ONLY
1811 #define FLUSH do { } while(0);
1812 #else
1813 #define FLUSH if (draw_x >= 0) { \
1814 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1815 draw_x = -1; \
1817 #endif
1819 int c, tc, ok, n = 0, draw_x = -1;
1820 unsigned int currcons;
1821 unsigned long draw_from = 0, draw_to = 0;
1822 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
1823 u16 himask, charmask;
1824 const unsigned char *orig_buf = NULL;
1825 int orig_count;
1827 currcons = vt->vc_num;
1828 if (!vc_cons_allocated(currcons)) {
1829 /* could this happen? */
1830 static int error = 0;
1831 if (!error) {
1832 error = 1;
1833 printk("con_write: tty %d not allocated\n", currcons+1);
1835 return 0;
1838 orig_buf = buf;
1839 orig_count = count;
1841 if (from_user) {
1842 down(&con_buf_sem);
1844 again:
1845 if (count > CON_BUF_SIZE)
1846 count = CON_BUF_SIZE;
1847 if (copy_from_user(con_buf, buf, count)) {
1848 n = 0; /* ?? are error codes legal here ?? */
1849 goto out;
1852 buf = con_buf;
1855 /* At this point 'buf' is guarenteed to be a kernel buffer
1856 * and therefore no access to userspace (and therefore sleeping)
1857 * will be needed. The con_buf_sem serializes all tty based
1858 * console rendering and vcs write/read operations. We hold
1859 * the console spinlock during the entire write.
1862 spin_lock_irq(&console_lock);
1864 himask = hi_font_mask;
1865 charmask = himask ? 0x1ff : 0xff;
1867 /* undraw cursor first */
1868 if (IS_FG)
1869 hide_cursor(currcons);
1871 while (!tty->stopped && count) {
1872 c = *buf;
1873 buf++;
1874 n++;
1875 count--;
1877 if (utf) {
1878 /* Combine UTF-8 into Unicode */
1879 /* Incomplete characters silently ignored */
1880 if(c > 0x7f) {
1881 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1882 utf_char = (utf_char << 6) | (c & 0x3f);
1883 utf_count--;
1884 if (utf_count == 0)
1885 tc = c = utf_char;
1886 else continue;
1887 } else {
1888 if ((c & 0xe0) == 0xc0) {
1889 utf_count = 1;
1890 utf_char = (c & 0x1f);
1891 } else if ((c & 0xf0) == 0xe0) {
1892 utf_count = 2;
1893 utf_char = (c & 0x0f);
1894 } else if ((c & 0xf8) == 0xf0) {
1895 utf_count = 3;
1896 utf_char = (c & 0x07);
1897 } else if ((c & 0xfc) == 0xf8) {
1898 utf_count = 4;
1899 utf_char = (c & 0x03);
1900 } else if ((c & 0xfe) == 0xfc) {
1901 utf_count = 5;
1902 utf_char = (c & 0x01);
1903 } else
1904 utf_count = 0;
1905 continue;
1907 } else {
1908 tc = c;
1909 utf_count = 0;
1911 } else { /* no utf */
1912 tc = translate[toggle_meta ? (c|0x80) : c];
1915 /* If the original code was a control character we
1916 * only allow a glyph to be displayed if the code is
1917 * not normally used (such as for cursor movement) or
1918 * if the disp_ctrl mode has been explicitly enabled.
1919 * Certain characters (as given by the CTRL_ALWAYS
1920 * bitmap) are always displayed as control characters,
1921 * as the console would be pretty useless without
1922 * them; to display an arbitrary font position use the
1923 * direct-to-font zone in UTF-8 mode.
1925 ok = tc && (c >= 32 ||
1926 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
1927 : CTRL_ACTION) >> c) & 1)))
1928 && (c != 127 || disp_ctrl)
1929 && (c != 128+27);
1931 if (vc_state == ESnormal && ok) {
1932 /* Now try to find out how to display it */
1933 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
1934 if ( tc == -4 ) {
1935 /* If we got -4 (not found) then see if we have
1936 defined a replacement character (U+FFFD) */
1937 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
1939 /* One reason for the -4 can be that we just
1940 did a clear_unimap();
1941 try at least to show something. */
1942 if (tc == -4)
1943 tc = c;
1944 } else if ( tc == -3 ) {
1945 /* Bad hash table -- hope for the best */
1946 tc = c;
1948 if (tc & ~charmask)
1949 continue; /* Conversion failed */
1951 if (need_wrap || decim)
1952 FLUSH
1953 if (need_wrap) {
1954 cr(currcons);
1955 lf(currcons);
1957 if (decim)
1958 insert_char(currcons, 1);
1959 scr_writew(himask ?
1960 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
1961 (attr << 8) + tc,
1962 (u16 *) pos);
1963 if (DO_UPDATE && draw_x < 0) {
1964 draw_x = x;
1965 draw_from = pos;
1967 if (x == video_num_columns - 1) {
1968 need_wrap = decawm;
1969 draw_to = pos+2;
1970 } else {
1971 x++;
1972 draw_to = (pos+=2);
1974 continue;
1976 FLUSH
1977 do_con_trol(tty, currcons, c);
1979 FLUSH
1980 spin_unlock_irq(&console_lock);
1982 out:
1983 if (from_user) {
1984 /* If the user requested something larger than
1985 * the CON_BUF_SIZE, and the tty is not stopped,
1986 * keep going.
1988 if ((orig_count > CON_BUF_SIZE) && !tty->stopped) {
1989 orig_count -= CON_BUF_SIZE;
1990 orig_buf += CON_BUF_SIZE;
1991 count = orig_count;
1992 buf = orig_buf;
1993 goto again;
1996 up(&con_buf_sem);
1999 return n;
2000 #undef FLUSH
2004 * This is the console switching tasklet.
2006 * Doing console switching in a tasklet allows
2007 * us to do the switches asynchronously (needed when we want
2008 * to switch due to a keyboard interrupt). Synchronization
2009 * with other console code and prevention of re-entrancy is
2010 * ensured with console_lock.
2012 static void console_softint(unsigned long ignored)
2014 /* Runs the task queue outside of the console lock. These
2015 * callbacks can come back into the console code and thus
2016 * will perform their own locking.
2018 run_task_queue(&con_task_queue);
2020 spin_lock_irq(&console_lock);
2022 if (want_console >= 0) {
2023 if (want_console != fg_console && vc_cons_allocated(want_console)) {
2024 hide_cursor(fg_console);
2025 change_console(want_console);
2026 /* we only changed when the console had already
2027 been allocated - a new console is not created
2028 in an interrupt routine */
2030 want_console = -1;
2032 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2033 do_poke_blanked_console = 0;
2034 poke_blanked_console();
2036 if (scrollback_delta) {
2037 int currcons = fg_console;
2038 clear_selection();
2039 if (vcmode == KD_TEXT)
2040 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
2041 scrollback_delta = 0;
2044 spin_unlock_irq(&console_lock);
2047 #ifdef CONFIG_VT_CONSOLE
2050 * Console on virtual terminal
2052 * The console_lock must be held when we get here.
2055 void vt_console_print(struct console *co, const char * b, unsigned count)
2057 int currcons = fg_console;
2058 unsigned char c;
2059 static unsigned long printing;
2060 const ushort *start;
2061 ushort cnt = 0;
2062 ushort myx;
2064 /* console busy or not yet initialized */
2065 if (!printable || test_and_set_bit(0, &printing))
2066 return;
2068 pm_access(pm_con);
2070 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2071 currcons = kmsg_redirect - 1;
2073 /* read `x' only after setting currecons properly (otherwise
2074 the `x' macro will read the x of the foreground console). */
2075 myx = x;
2077 if (!vc_cons_allocated(currcons)) {
2078 /* impossible */
2079 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2080 goto quit;
2083 if (vcmode != KD_TEXT)
2084 goto quit;
2086 /* undraw cursor first */
2087 if (IS_FG)
2088 hide_cursor(currcons);
2090 start = (ushort *)pos;
2092 /* Contrived structure to try to emulate original need_wrap behaviour
2093 * Problems caused when we have need_wrap set on '\n' character */
2094 while (count--) {
2095 c = *b++;
2096 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2097 if (cnt > 0) {
2098 if (IS_VISIBLE)
2099 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2100 x += cnt;
2101 if (need_wrap)
2102 x--;
2103 cnt = 0;
2105 if (c == 8) { /* backspace */
2106 bs(currcons);
2107 start = (ushort *)pos;
2108 myx = x;
2109 continue;
2111 if (c != 13)
2112 lf(currcons);
2113 cr(currcons);
2114 start = (ushort *)pos;
2115 myx = x;
2116 if (c == 10 || c == 13)
2117 continue;
2119 scr_writew((attr << 8) + c, (unsigned short *) pos);
2120 cnt++;
2121 if (myx == video_num_columns - 1) {
2122 need_wrap = 1;
2123 continue;
2125 pos+=2;
2126 myx++;
2128 if (cnt > 0) {
2129 if (IS_VISIBLE)
2130 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2131 x += cnt;
2132 if (x == video_num_columns) {
2133 x--;
2134 need_wrap = 1;
2137 set_cursor(currcons);
2139 quit:
2140 clear_bit(0, &printing);
2143 static kdev_t vt_console_device(struct console *c)
2145 return MKDEV(TTY_MAJOR, c->index ? c->index : fg_console + 1);
2148 struct console vt_console_driver = {
2149 name: "tty",
2150 write: vt_console_print,
2151 device: vt_console_device,
2152 wait_key: keyboard_wait_for_keypress,
2153 unblank: unblank_screen,
2154 flags: CON_PRINTBUFFER,
2155 index: -1,
2157 #endif
2160 * Handling of Linux-specific VC ioctls
2163 int tioclinux(struct tty_struct *tty, unsigned long arg)
2165 char type, data;
2167 if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
2168 return -EINVAL;
2169 if (current->tty != tty && !suser())
2170 return -EPERM;
2171 if (get_user(type, (char *)arg))
2172 return -EFAULT;
2173 switch (type)
2175 case 2:
2176 return set_selection(arg, tty, 1);
2177 case 3:
2178 return paste_selection(tty);
2179 case 4:
2180 unblank_screen();
2181 return 0;
2182 case 5:
2183 return sel_loadlut(arg);
2184 case 6:
2187 * Make it possible to react to Shift+Mousebutton.
2188 * Note that 'shift_state' is an undocumented
2189 * kernel-internal variable; programs not closely
2190 * related to the kernel should not use this.
2192 data = shift_state;
2193 return __put_user(data, (char *) arg);
2194 case 7:
2195 data = mouse_reporting();
2196 return __put_user(data, (char *) arg);
2197 case 10:
2198 set_vesa_blanking(arg);
2199 return 0;
2200 case 11: /* set kmsg redirect */
2201 if (!suser())
2202 return -EPERM;
2203 if (get_user(data, (char *)arg+1))
2204 return -EFAULT;
2205 kmsg_redirect = data;
2206 return 0;
2207 case 12: /* get fg_console */
2208 return fg_console;
2210 return -EINVAL;
2214 * /dev/ttyN handling
2217 static int con_write(struct tty_struct * tty, int from_user,
2218 const unsigned char *buf, int count)
2220 int retval;
2222 pm_access(pm_con);
2223 retval = do_con_write(tty, from_user, buf, count);
2224 con_flush_chars(tty);
2226 return retval;
2229 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2231 pm_access(pm_con);
2232 do_con_write(tty, 0, &ch, 1);
2235 static int con_write_room(struct tty_struct *tty)
2237 if (tty->stopped)
2238 return 0;
2239 return 4096; /* No limit, really; we're not buffering */
2242 static int con_chars_in_buffer(struct tty_struct *tty)
2244 return 0; /* we're not buffering */
2248 * con_throttle and con_unthrottle are only used for
2249 * paste_selection(), which has to stuff in a large number of
2250 * characters...
2252 static void con_throttle(struct tty_struct *tty)
2256 static void con_unthrottle(struct tty_struct *tty)
2258 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
2260 wake_up_interruptible(&vt->paste_wait);
2264 * Turn the Scroll-Lock LED on when the tty is stopped
2266 static void con_stop(struct tty_struct *tty)
2268 int console_num;
2269 if (!tty)
2270 return;
2271 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2272 if (!vc_cons_allocated(console_num))
2273 return;
2274 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2275 set_leds();
2279 * Turn the Scroll-Lock LED off when the console is started
2281 static void con_start(struct tty_struct *tty)
2283 int console_num;
2284 if (!tty)
2285 return;
2286 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2287 if (!vc_cons_allocated(console_num))
2288 return;
2289 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2290 set_leds();
2293 static void con_flush_chars(struct tty_struct *tty)
2295 unsigned long flags;
2296 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
2298 pm_access(pm_con);
2299 spin_lock_irqsave(&console_lock, flags);
2300 set_cursor(vt->vc_num);
2301 spin_unlock_irqrestore(&console_lock, flags);
2305 * Allocate the console screen memory.
2307 static int con_open(struct tty_struct *tty, struct file * filp)
2309 unsigned int currcons;
2310 int i;
2312 currcons = MINOR(tty->device) - tty->driver.minor_start;
2314 i = vc_allocate(currcons);
2315 if (i)
2316 return i;
2318 vt_cons[currcons]->vc_num = currcons;
2319 tty->driver_data = vt_cons[currcons];
2321 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2322 tty->winsize.ws_row = video_num_lines;
2323 tty->winsize.ws_col = video_num_columns;
2325 if (tty->count == 1)
2326 vcs_make_devfs (currcons, 0);
2327 return 0;
2330 static void con_close(struct tty_struct *tty, struct file * filp)
2332 if (!tty)
2333 return;
2334 if (tty->count != 1) return;
2335 vcs_make_devfs (MINOR (tty->device) - tty->driver.minor_start, 1);
2336 tty->driver_data = 0;
2339 static void vc_init(unsigned int currcons, unsigned int rows, unsigned int cols, int do_clear)
2341 int j, k ;
2343 video_num_columns = cols;
2344 video_num_lines = rows;
2345 video_size_row = cols<<1;
2346 screenbuf_size = video_num_lines * video_size_row;
2348 set_origin(currcons);
2349 pos = origin;
2350 reset_vc(currcons);
2351 for (j=k=0; j<16; j++) {
2352 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2353 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2354 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2356 def_color = 0x07; /* white */
2357 ulcolor = 0x0f; /* bold white */
2358 halfcolor = 0x08; /* grey */
2359 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2360 reset_terminal(currcons, do_clear);
2364 * This routine initializes console interrupts, and does nothing
2365 * else. If you want the screen to clear, call tty_write with
2366 * the appropriate escape-sequence.
2369 struct tty_driver console_driver;
2370 static int console_refcount;
2372 DECLARE_TASKLET_DISABLED(console_tasklet, console_softint, 0);
2374 void __init con_init(void)
2376 const char *display_desc = NULL;
2377 unsigned int currcons = 0;
2379 if (conswitchp)
2380 display_desc = conswitchp->con_startup();
2381 if (!display_desc) {
2382 fg_console = 0;
2383 return;
2386 memset(&console_driver, 0, sizeof(struct tty_driver));
2387 console_driver.magic = TTY_DRIVER_MAGIC;
2388 console_driver.name = "vc/%d";
2389 console_driver.name_base = 1;
2390 console_driver.major = TTY_MAJOR;
2391 console_driver.minor_start = 1;
2392 console_driver.num = MAX_NR_CONSOLES;
2393 console_driver.type = TTY_DRIVER_TYPE_CONSOLE;
2394 console_driver.init_termios = tty_std_termios;
2395 console_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2396 /* Tell tty_register_driver() to skip consoles because they are
2397 * registered before kmalloc() is ready. We'll patch them in later.
2398 * See comments at console_init(); see also con_init_devfs().
2400 console_driver.flags |= TTY_DRIVER_NO_DEVFS;
2401 console_driver.refcount = &console_refcount;
2402 console_driver.table = console_table;
2403 console_driver.termios = console_termios;
2404 console_driver.termios_locked = console_termios_locked;
2406 console_driver.open = con_open;
2407 console_driver.close = con_close;
2408 console_driver.write = con_write;
2409 console_driver.write_room = con_write_room;
2410 console_driver.put_char = con_put_char;
2411 console_driver.flush_chars = con_flush_chars;
2412 console_driver.chars_in_buffer = con_chars_in_buffer;
2413 console_driver.ioctl = vt_ioctl;
2414 console_driver.stop = con_stop;
2415 console_driver.start = con_start;
2416 console_driver.throttle = con_throttle;
2417 console_driver.unthrottle = con_unthrottle;
2419 if (tty_register_driver(&console_driver))
2420 panic("Couldn't register console driver\n");
2422 init_timer(&console_timer);
2423 console_timer.function = blank_screen;
2424 if (blankinterval) {
2425 mod_timer(&console_timer, jiffies + blankinterval);
2429 * kmalloc is not running yet - we use the bootmem allocator.
2431 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2432 vc_cons[currcons].d = (struct vc_data *)
2433 alloc_bootmem(sizeof(struct vc_data));
2434 vt_cons[currcons] = (struct vt_struct *)
2435 alloc_bootmem(sizeof(struct vt_struct));
2436 visual_init(currcons, 1);
2437 screenbuf = (unsigned short *) alloc_bootmem(screenbuf_size);
2438 kmalloced = 0;
2439 vc_init(currcons, video_num_lines, video_num_columns,
2440 currcons || !sw->con_save_screen);
2442 currcons = fg_console = 0;
2443 master_display_fg = vc_cons[currcons].d;
2444 set_origin(currcons);
2445 save_screen(currcons);
2446 gotoxy(currcons,x,y);
2447 csi_J(currcons, 0);
2448 update_screen(fg_console);
2449 printk("Console: %s %s %dx%d",
2450 can_do_color ? "colour" : "mono",
2451 display_desc, video_num_columns, video_num_lines);
2452 printable = 1;
2453 printk("\n");
2455 #ifdef CONFIG_VT_CONSOLE
2456 register_console(&vt_console_driver);
2457 #endif
2459 tasklet_enable(&console_tasklet);
2460 tasklet_schedule(&console_tasklet);
2463 #ifndef VT_SINGLE_DRIVER
2465 static void clear_buffer_attributes(int currcons)
2467 unsigned short *p = (unsigned short *) origin;
2468 int count = screenbuf_size/2;
2469 int mask = hi_font_mask | 0xff;
2471 for (; count > 0; count--, p++) {
2472 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2477 * If we support more console drivers, this function is used
2478 * when a driver wants to take over some existing consoles
2479 * and become default driver for newly opened ones.
2482 void take_over_console(const struct consw *csw, int first, int last, int deflt)
2484 int i, j = -1;
2485 const char *desc;
2487 desc = csw->con_startup();
2488 if (!desc) return;
2489 if (deflt)
2490 conswitchp = csw;
2492 for (i = first; i <= last; i++) {
2493 int old_was_color;
2494 int currcons = i;
2496 con_driver_map[i] = csw;
2498 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2499 continue;
2501 j = i;
2502 if (IS_VISIBLE)
2503 save_screen(i);
2504 old_was_color = vc_cons[i].d->vc_can_do_color;
2505 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2506 visual_init(i, 0);
2507 update_attr(i);
2509 /* If the console changed between mono <-> color, then
2510 * the attributes in the screenbuf will be wrong. The
2511 * following resets all attributes to something sane.
2513 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2514 clear_buffer_attributes(i);
2516 if (IS_VISIBLE)
2517 update_screen(i);
2519 printk("Console: switching ");
2520 if (!deflt)
2521 printk("consoles %d-%d ", first+1, last+1);
2522 if (j >= 0)
2523 printk("to %s %s %dx%d\n",
2524 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2525 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2526 else
2527 printk("to %s\n", desc);
2530 void give_up_console(const struct consw *csw)
2532 int i;
2534 for(i = 0; i < MAX_NR_CONSOLES; i++)
2535 if (con_driver_map[i] == csw)
2536 con_driver_map[i] = NULL;
2539 #endif
2542 * Screen blanking
2545 static void set_vesa_blanking(unsigned long arg)
2547 char *argp = (char *)arg + 1;
2548 unsigned int mode;
2549 get_user(mode, argp);
2550 vesa_blank_mode = (mode < 4) ? mode : 0;
2553 /* We can't register the console with devfs during con_init(), because it
2554 * is called before kmalloc() works. This function is called later to
2555 * do the registration.
2557 void __init con_init_devfs (void)
2559 int i;
2561 for (i = 0; i < console_driver.num; i++)
2562 tty_register_devfs (&console_driver, DEVFS_FL_AOPEN_NOTIFY,
2563 console_driver.minor_start + i);
2566 static void vesa_powerdown(void)
2568 struct vc_data *c = vc_cons[fg_console].d;
2570 * Power down if currently suspended (1 or 2),
2571 * suspend if currently blanked (0),
2572 * else do nothing (i.e. already powered down (3)).
2573 * Called only if powerdown features are allowed.
2575 switch (vesa_blank_mode) {
2576 case VESA_NO_BLANKING:
2577 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1);
2578 break;
2579 case VESA_VSYNC_SUSPEND:
2580 case VESA_HSYNC_SUSPEND:
2581 c->vc_sw->con_blank(c, VESA_POWERDOWN+1);
2582 break;
2586 static void vesa_powerdown_screen(unsigned long dummy)
2588 console_timer.function = unblank_screen_t; /* I don't have a clue why this is necessary */
2590 vesa_powerdown();
2593 static void timer_do_blank_screen(int entering_gfx, int from_timer_handler)
2595 int currcons = fg_console;
2596 int i;
2598 if (console_blanked)
2599 return;
2601 /* entering graphics mode? */
2602 if (entering_gfx) {
2603 hide_cursor(currcons);
2604 save_screen(currcons);
2605 sw->con_blank(vc_cons[currcons].d, -1);
2606 console_blanked = fg_console + 1;
2607 set_origin(currcons);
2608 return;
2611 /* don't blank graphics */
2612 if (vcmode != KD_TEXT) {
2613 console_blanked = fg_console + 1;
2614 return;
2617 hide_cursor(currcons);
2618 if (!from_timer_handler)
2619 del_timer_sync(&console_timer);
2620 if (vesa_off_interval) {
2621 console_timer.function = vesa_powerdown_screen;
2622 mod_timer(&console_timer, jiffies + vesa_off_interval);
2623 } else {
2624 if (!from_timer_handler)
2625 del_timer_sync(&console_timer);
2626 console_timer.function = unblank_screen_t;
2629 save_screen(currcons);
2630 /* In case we need to reset origin, blanking hook returns 1 */
2631 i = sw->con_blank(vc_cons[currcons].d, 1);
2632 console_blanked = fg_console + 1;
2633 if (i)
2634 set_origin(currcons);
2636 if (console_blank_hook && console_blank_hook(1))
2637 return;
2638 if (vesa_blank_mode)
2639 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1);
2642 void do_blank_screen(int entering_gfx)
2644 timer_do_blank_screen(entering_gfx, 0);
2647 static void unblank_screen_t(unsigned long dummy)
2649 unblank_screen();
2652 void unblank_screen(void)
2654 int currcons;
2656 if (!console_blanked)
2657 return;
2658 if (!vc_cons_allocated(fg_console)) {
2659 /* impossible */
2660 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2661 return;
2663 console_timer.function = blank_screen;
2664 if (blankinterval) {
2665 mod_timer(&console_timer, jiffies + blankinterval);
2668 currcons = fg_console;
2669 console_blanked = 0;
2670 if (console_blank_hook)
2671 console_blank_hook(0);
2672 set_palette(currcons);
2673 if (sw->con_blank(vc_cons[currcons].d, 0))
2674 /* Low-level driver cannot restore -> do it ourselves */
2675 update_screen(fg_console);
2676 set_cursor(fg_console);
2679 static void blank_screen(unsigned long dummy)
2681 timer_do_blank_screen(0, 1);
2684 void poke_blanked_console(void)
2686 del_timer(&console_timer); /* Can't use _sync here: called from tasklet */
2687 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2688 return;
2689 if (console_blanked) {
2690 console_timer.function = unblank_screen_t;
2691 mod_timer(&console_timer, jiffies); /* Now */
2692 } else if (blankinterval) {
2693 mod_timer(&console_timer, jiffies + blankinterval);
2698 * Palettes
2701 void set_palette(int currcons)
2703 if (vcmode != KD_GRAPHICS)
2704 sw->con_set_palette(vc_cons[currcons].d, color_table);
2707 static int set_get_cmap(unsigned char *arg, int set)
2709 int i, j, k;
2711 for (i = 0; i < 16; i++)
2712 if (set) {
2713 get_user(default_red[i], arg++);
2714 get_user(default_grn[i], arg++);
2715 get_user(default_blu[i], arg++);
2716 } else {
2717 put_user(default_red[i], arg++);
2718 put_user(default_grn[i], arg++);
2719 put_user(default_blu[i], arg++);
2721 if (set) {
2722 for (i = 0; i < MAX_NR_CONSOLES; i++)
2723 if (vc_cons_allocated(i)) {
2724 for (j = k = 0; j < 16; j++) {
2725 vc_cons[i].d->vc_palette[k++] = default_red[j];
2726 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2727 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2729 set_palette(i);
2732 return 0;
2736 * Load palette into the DAC registers. arg points to a colour
2737 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2740 int con_set_cmap(unsigned char *arg)
2742 return set_get_cmap (arg,1);
2745 int con_get_cmap(unsigned char *arg)
2747 return set_get_cmap (arg,0);
2750 void reset_palette(int currcons)
2752 int j, k;
2753 for (j=k=0; j<16; j++) {
2754 palette[k++] = default_red[j];
2755 palette[k++] = default_grn[j];
2756 palette[k++] = default_blu[j];
2758 set_palette(currcons);
2762 * Font switching
2764 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2765 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2766 * depending on width) reserved for each character which is kinda wasty, but
2767 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2768 * is upto the actual low-level console-driver convert data into its favorite
2769 * format (maybe we should add a `fontoffset' field to the `display'
2770 * structure so we wont have to convert the fontdata all the time.
2771 * /Jes
2774 #define max_font_size 65536
2776 int con_font_op(int currcons, struct console_font_op *op)
2778 int rc = -EINVAL;
2779 int size = max_font_size, set;
2780 u8 *temp = NULL;
2781 struct console_font_op old_op;
2783 if (vt_cons[currcons]->vc_mode != KD_TEXT)
2784 goto quit;
2785 memcpy(&old_op, op, sizeof(old_op));
2786 if (op->op == KD_FONT_OP_SET) {
2787 if (!op->data)
2788 return -EINVAL;
2789 if (op->charcount > 512)
2790 goto quit;
2791 if (!op->height) { /* Need to guess font height [compat] */
2792 int h, i;
2793 u8 *charmap = op->data, tmp;
2795 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2796 so that we can get rid of this soon */
2797 if (!(op->flags & KD_FONT_FLAG_OLD))
2798 goto quit;
2799 rc = -EFAULT;
2800 for (h = 32; h > 0; h--)
2801 for (i = 0; i < op->charcount; i++) {
2802 if (get_user(tmp, &charmap[32*i+h-1]))
2803 goto quit;
2804 if (tmp)
2805 goto nonzero;
2807 rc = -EINVAL;
2808 goto quit;
2809 nonzero:
2810 rc = -EINVAL;
2811 op->height = h;
2813 if (op->width > 32 || op->height > 32)
2814 goto quit;
2815 size = (op->width+7)/8 * 32 * op->charcount;
2816 if (size > max_font_size)
2817 return -ENOSPC;
2818 set = 1;
2819 } else if (op->op == KD_FONT_OP_GET)
2820 set = 0;
2821 else
2822 return sw->con_font_op(vc_cons[currcons].d, op);
2823 if (op->data) {
2824 temp = kmalloc(size, GFP_KERNEL);
2825 if (!temp)
2826 return -ENOMEM;
2827 if (set && copy_from_user(temp, op->data, size)) {
2828 rc = -EFAULT;
2829 goto quit;
2831 op->data = temp;
2834 spin_lock_irq(&console_lock);
2835 rc = sw->con_font_op(vc_cons[currcons].d, op);
2836 spin_unlock_irq(&console_lock);
2838 op->data = old_op.data;
2839 if (!rc && !set) {
2840 int c = (op->width+7)/8 * 32 * op->charcount;
2842 if (op->data && op->charcount > old_op.charcount)
2843 rc = -ENOSPC;
2844 if (!(op->flags & KD_FONT_FLAG_OLD)) {
2845 if (op->width > old_op.width ||
2846 op->height > old_op.height)
2847 rc = -ENOSPC;
2848 } else {
2849 if (op->width != 8)
2850 rc = -EIO;
2851 else if ((old_op.height && op->height > old_op.height) ||
2852 op->height > 32)
2853 rc = -ENOSPC;
2855 if (!rc && op->data && copy_to_user(op->data, temp, c))
2856 rc = -EFAULT;
2858 quit: if (temp)
2859 kfree(temp);
2860 return rc;
2864 * Interface exported to selection and vcs.
2867 /* used by selection */
2868 u16 screen_glyph(int currcons, int offset)
2870 u16 w = scr_readw(screenpos(currcons, offset, 1));
2871 u16 c = w & 0xff;
2873 if (w & hi_font_mask)
2874 c |= 0x100;
2875 return c;
2878 /* used by vcs - note the word offset */
2879 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
2881 return screenpos(currcons, 2 * w_offset, viewed);
2884 void getconsxy(int currcons, char *p)
2886 p[0] = x;
2887 p[1] = y;
2890 void putconsxy(int currcons, char *p)
2892 gotoxy(currcons, p[0], p[1]);
2893 set_cursor(currcons);
2896 u16 vcs_scr_readw(int currcons, const u16 *org)
2898 if ((unsigned long)org == pos && softcursor_original != -1)
2899 return softcursor_original;
2900 return scr_readw(org);
2903 void vcs_scr_writew(int currcons, u16 val, u16 *org)
2905 scr_writew(val, org);
2906 if ((unsigned long)org == pos) {
2907 softcursor_original = -1;
2908 add_softcursor(currcons);
2912 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data)
2914 switch (rqst)
2916 case PM_RESUME:
2917 unblank_screen();
2918 break;
2919 case PM_SUSPEND:
2920 do_blank_screen(0);
2921 break;
2923 return 0;
2927 * Visible symbols for modules
2930 EXPORT_SYMBOL(color_table);
2931 EXPORT_SYMBOL(default_red);
2932 EXPORT_SYMBOL(default_grn);
2933 EXPORT_SYMBOL(default_blu);
2934 EXPORT_SYMBOL(video_font_height);
2935 EXPORT_SYMBOL(video_scan_lines);
2936 EXPORT_SYMBOL(vc_resize);
2937 EXPORT_SYMBOL(fg_console);
2938 EXPORT_SYMBOL(console_blank_hook);
2939 #ifdef CONFIG_VT
2940 EXPORT_SYMBOL(vt_cons);
2941 #endif
2942 #ifndef VT_SINGLE_DRIVER
2943 EXPORT_SYMBOL(take_over_console);
2944 EXPORT_SYMBOL(give_up_console);
2945 #endif