2 * linux/drivers/char/vt.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
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
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):
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>
73 * Removed console_lock, enabled interrupts across all console operations
74 * 13 March 2001, Andrew Morton
77 #include <linux/module.h>
78 #include <linux/types.h>
79 #include <linux/sched.h>
80 #include <linux/tty.h>
81 #include <linux/tty_flip.h>
82 #include <linux/kernel.h>
83 #include <linux/string.h>
84 #include <linux/errno.h>
86 #include <linux/slab.h>
87 #include <linux/major.h>
89 #include <linux/console.h>
90 #include <linux/init.h>
91 #include <linux/devfs_fs_kernel.h>
92 #include <linux/vt_kern.h>
93 #include <linux/selection.h>
94 #include <linux/tiocl.h>
95 #include <linux/kbd_kern.h>
96 #include <linux/consolemap.h>
97 #include <linux/timer.h>
98 #include <linux/interrupt.h>
99 #include <linux/config.h>
100 #include <linux/workqueue.h>
101 #include <linux/bootmem.h>
102 #include <linux/pm.h>
105 #include <asm/system.h>
106 #include <asm/uaccess.h>
107 #include <asm/bitops.h>
109 #include "console_macros.h"
112 const struct consw *conswitchp;
114 /* A bitmap for codes <32. A bit of 1 indicates that the code
115 * corresponding to that bit number invokes some special action
116 * (such as cursor movement) and should not be displayed as a
117 * glyph unless the disp_ctrl mode is explicitly enabled.
119 #define CTRL_ACTION 0x0d00ff81
120 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
123 * Here is the default bell parameters: 750HZ, 1/8th of a second
125 #define DEFAULT_BELL_PITCH 750
126 #define DEFAULT_BELL_DURATION (HZ/8)
128 extern void vcs_make_devfs(struct tty_struct *tty);
129 extern void vcs_remove_devfs(struct tty_struct *tty);
131 extern void console_map_init(void);
132 #ifdef CONFIG_PROM_CONSOLE
133 extern void prom_con_init(void);
135 #ifdef CONFIG_MDA_CONSOLE
136 extern int mda_console_init(void);
138 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
139 extern int fb_console_init(void);
142 struct vc vc_cons [MAX_NR_CONSOLES];
144 #ifndef VT_SINGLE_DRIVER
145 static const struct consw *con_driver_map[MAX_NR_CONSOLES];
148 static int con_open(struct tty_struct *, struct file *);
149 static void vc_init(unsigned int console, unsigned int rows,
150 unsigned int cols, int do_clear);
151 static void gotoxy(int currcons, int new_x, int new_y);
152 static void save_cur(int currcons);
153 static void reset_terminal(int currcons, int do_clear);
154 static void con_flush_chars(struct tty_struct *tty);
155 static void set_vesa_blanking(unsigned long arg);
156 static void set_cursor(int currcons);
157 static void hide_cursor(int currcons);
158 static void console_callback(void *ignored);
159 static void blank_screen_t(unsigned long dummy);
161 static int printable; /* Is console ready for printing? */
164 * ignore_poke: don't unblank the screen when things are typed. This is
165 * mainly for the privacy of braille terminal users.
167 static int ignore_poke;
169 int do_poke_blanked_console;
172 static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
173 static int blankinterval = 10*60*HZ;
174 static int vesa_off_interval;
176 static DECLARE_WORK(console_work, console_callback, NULL);
179 * fg_console is the current virtual console,
180 * last_console is the last used one,
181 * want_console is the console we want to switch to,
182 * kmsg_redirect is the console for kernel messages,
186 int want_console = -1;
190 * For each existing display, we have a pointer to console currently visible
191 * on that display, allowing consoles other than fg_console to be refreshed
192 * appropriately. Unless the low-level driver supplies its own display_fg
193 * variable, we use this one for the "master display".
195 static struct vc_data *master_display_fg;
198 * Unfortunately, we need to delay tty echo when we're currently writing to the
199 * console since the code is (and always was) not re-entrant, so we schedule
200 * all flip requests to process context with schedule-task() and run it from
201 * console_callback().
205 * For the same reason, we defer scrollback to the console callback.
207 static int scrollback_delta;
210 * Hook so that the power management routines can (un)blank
211 * the console on our behalf.
213 int (*console_blank_hook)(int);
215 static struct timer_list console_timer;
216 static int blank_state;
217 static int blank_timer_expired;
225 * Low-Level Functions
228 #define IS_FG (currcons == fg_console)
229 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
231 #ifdef VT_BUF_VRAM_ONLY
234 #define DO_UPDATE IS_VISIBLE
237 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data);
238 static struct pm_dev *pm_con;
240 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
245 p = (unsigned short *)(origin + offset);
246 else if (!sw->con_screen_pos)
247 p = (unsigned short *)(visible_origin + offset);
249 p = sw->con_screen_pos(vc_cons[currcons].d, offset);
253 static inline void scrolldelta(int lines)
255 scrollback_delta += lines;
256 schedule_console_callback();
259 void schedule_console_callback(void)
261 schedule_work(&console_work);
264 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
266 unsigned short *d, *s;
270 if (b > video_num_lines || t >= b || nr < 1)
272 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
274 d = (unsigned short *) (origin+video_size_row*t);
275 s = (unsigned short *) (origin+video_size_row*(t+nr));
276 scr_memmovew(d, s, (b-t-nr) * video_size_row);
277 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
281 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
288 if (b > video_num_lines || t >= b || nr < 1)
290 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
292 s = (unsigned short *) (origin+video_size_row*t);
293 step = video_num_columns * nr;
294 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
295 scr_memsetw(s, video_erase_char, 2*step);
298 static void do_update_region(int currcons, unsigned long start, int count)
300 #ifndef VT_BUF_VRAM_ONLY
301 unsigned int xx, yy, offset;
305 if (!sw->con_getxy) {
306 offset = (start - origin) / 2;
307 xx = offset % video_num_columns;
308 yy = offset / video_num_columns;
311 start = sw->con_getxy(vc_cons[currcons].d, start, &nxx, &nyy);
315 u16 attrib = scr_readw(p) & 0xff00;
318 while (xx < video_num_columns && count) {
319 if (attrib != (scr_readw(p) & 0xff00)) {
321 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
324 attrib = scr_readw(p) & 0xff00;
331 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
338 start = sw->con_getxy(vc_cons[currcons].d, start, NULL, NULL);
344 void update_region(int currcons, unsigned long start, int count)
346 WARN_CONSOLE_UNLOCKED();
349 hide_cursor(currcons);
350 do_update_region(currcons, start, count);
351 set_cursor(currcons);
355 /* Structure of attributes is hardware-dependent */
357 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
359 if (sw->con_build_attr)
360 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
362 #ifndef VT_BUF_VRAM_ONLY
364 * ++roman: I completely changed the attribute format for monochrome
365 * mode (!can_do_color). The formerly used MDA (monochrome display
366 * adapter) format didn't allow the combination of certain effects.
367 * Now the attribute is just a bit vector:
368 * Bit 0..1: intensity (0..2)
377 (_underline ? 4 : 0) |
381 a = (a & 0xf0) | ulcolor;
382 else if (_intensity == 0)
383 a = (a & 0xf0) | halfcolor;
385 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
390 if (hi_font_mask == 0x100)
399 static void update_attr(int currcons)
401 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
402 video_erase_char = (build_attr(currcons, color, 1, blink, 0, decscnm) << 8) | ' ';
405 /* Note: inverting the screen twice should revert to the original state */
407 void invert_screen(int currcons, int offset, int count, int viewed)
411 WARN_CONSOLE_UNLOCKED();
414 p = screenpos(currcons, offset, viewed);
415 if (sw->con_invert_region)
416 sw->con_invert_region(vc_cons[currcons].d, p, count);
417 #ifndef VT_BUF_VRAM_ONLY
430 } else if (hi_font_mask == 0x100) {
433 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
440 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
448 do_update_region(currcons, (unsigned long) p, count);
451 /* used by selection: complement pointer position */
452 void complement_pos(int currcons, int offset)
454 static unsigned short *p;
455 static unsigned short old;
456 static unsigned short oldx, oldy;
458 WARN_CONSOLE_UNLOCKED();
463 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
469 p = screenpos(currcons, offset, 1);
471 new = old ^ complement_mask;
474 oldx = (offset >> 1) % video_num_columns;
475 oldy = (offset >> 1) / video_num_columns;
476 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
481 static void insert_char(int currcons, unsigned int nr)
483 unsigned short *p, *q = (unsigned short *) pos;
485 p = q + video_num_columns - nr - x;
487 scr_writew(scr_readw(p), p + nr);
488 scr_memsetw(q, video_erase_char, nr*2);
491 unsigned short oldattr = attr;
492 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
493 video_num_columns-x-nr);
494 attr = video_erase_char >> 8;
496 sw->con_putc(vc_cons[currcons].d,
497 video_erase_char,y,x+nr);
502 static void delete_char(int currcons, unsigned int nr)
505 unsigned short *p = (unsigned short *) pos;
507 while (++i <= video_num_columns - nr) {
508 scr_writew(scr_readw(p+nr), p);
511 scr_memsetw(p, video_erase_char, nr*2);
514 unsigned short oldattr = attr;
515 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
516 video_num_columns-x-nr);
517 attr = video_erase_char >> 8;
519 sw->con_putc(vc_cons[currcons].d,
521 video_num_columns-1-nr);
526 static int softcursor_original;
528 static void add_softcursor(int currcons)
530 int i = scr_readw((u16 *) pos);
531 u32 type = cursor_type;
533 if (! (type & 0x10)) return;
534 if (softcursor_original != -1) return;
535 softcursor_original = i;
536 i |= ((type >> 8) & 0xff00 );
537 i ^= ((type) & 0xff00 );
538 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
539 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
540 scr_writew(i, (u16 *) pos);
542 sw->con_putc(vc_cons[currcons].d, i, y, x);
545 static void hide_softcursor(int currcons)
547 if (softcursor_original != -1) {
548 scr_writew(softcursor_original,(u16 *) pos);
550 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
551 softcursor_original = -1;
555 static void hide_cursor(int currcons)
557 if (currcons == sel_cons)
559 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
560 hide_softcursor(currcons);
563 static void set_cursor(int currcons)
565 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
568 if (currcons == sel_cons)
570 add_softcursor(currcons);
571 if ((cursor_type & 0x0f) != 1)
572 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
574 hide_cursor(currcons);
577 static void set_origin(int currcons)
579 WARN_CONSOLE_UNLOCKED();
582 !sw->con_set_origin ||
583 !sw->con_set_origin(vc_cons[currcons].d))
584 origin = (unsigned long) screenbuf;
585 visible_origin = origin;
586 scr_end = origin + screenbuf_size;
587 pos = origin + video_size_row*y + 2*x;
590 static inline void save_screen(int currcons)
592 WARN_CONSOLE_UNLOCKED();
594 if (sw->con_save_screen)
595 sw->con_save_screen(vc_cons[currcons].d);
599 * Redrawing of screen
602 void redraw_screen(int new_console, int is_switch)
605 int currcons, old_console;
607 WARN_CONSOLE_UNLOCKED();
609 if (!vc_cons_allocated(new_console)) {
611 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
616 currcons = fg_console;
617 hide_cursor(currcons);
618 if (fg_console != new_console) {
619 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
620 old_console = (*display) ? (*display)->vc_num : fg_console;
621 *display = vc_cons[new_console].d;
622 fg_console = new_console;
623 currcons = old_console;
625 save_screen(currcons);
626 set_origin(currcons);
628 currcons = new_console;
629 if (old_console == new_console)
633 currcons = new_console;
634 hide_cursor(currcons);
639 set_origin(currcons);
640 update = sw->con_switch(vc_cons[currcons].d);
641 set_palette(currcons);
642 if (update && vcmode != KD_GRAPHICS)
643 do_update_region(currcons, origin, screenbuf_size/2);
645 set_cursor(currcons);
648 compute_shiftstate();
653 * Allocation, freeing and resizing of VTs.
656 int vc_cons_allocated(unsigned int i)
658 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
661 static void visual_init(int currcons, int init)
663 /* ++Geert: sw->con_init determines console size */
665 #ifndef VT_SINGLE_DRIVER
666 if (con_driver_map[currcons])
667 sw = con_driver_map[currcons];
670 display_fg = &master_display_fg;
671 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
672 vc_cons[currcons].d->vc_uni_pagedir = 0;
676 sw->con_init(vc_cons[currcons].d, init);
677 if (!complement_mask)
678 complement_mask = can_do_color ? 0x7700 : 0x0800;
679 s_complement_mask = complement_mask;
680 video_size_row = video_num_columns<<1;
681 screenbuf_size = video_num_lines*video_size_row;
684 int vc_allocate(unsigned int currcons) /* return 0 on success */
686 WARN_CONSOLE_UNLOCKED();
688 if (currcons >= MAX_NR_CONSOLES)
690 if (!vc_cons[currcons].d) {
693 /* prevent users from taking too much memory */
694 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
697 /* due to the granularity of kmalloc, we waste some memory here */
698 /* the alloc is done in two steps, to optimize the common situation
699 of a 25x80 console (structsize=216, screenbuf_size=4000) */
700 /* although the numbers above are not valid since long ago, the
701 point is still up-to-date and the comment still has its value
702 even if only as a historical artifact. --mj, July 1998 */
703 p = (long) kmalloc(structsize, GFP_KERNEL);
706 memset((void *)p, 0, structsize);
707 vc_cons[currcons].d = (struct vc_data *)p;
708 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
709 visual_init(currcons, 1);
710 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
711 con_set_default_unimap(currcons);
712 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
715 vc_cons[currcons].d = NULL;
716 vt_cons[currcons] = NULL;
719 screenbuf = (unsigned short *) q;
721 vc_init(currcons, video_num_lines, video_num_columns, 1);
724 pm_con = pm_register(PM_SYS_DEV,
732 inline int resize_screen(int currcons, int width, int height)
734 /* Resizes the resolution of the display adapater */
737 if (vcmode != KD_GRAPHICS && sw->con_resize)
738 err = sw->con_resize(vc_cons[currcons].d, width, height);
743 * Change # of rows and columns (0 means unchanged/the size of fg_console)
744 * [this is to be used together with some user program
745 * like resize that changes the hardware videomode]
747 int vc_resize(int currcons, unsigned int cols, unsigned int lines)
749 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
750 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
751 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
752 unsigned short *newscreen;
754 WARN_CONSOLE_UNLOCKED();
756 if (!vc_cons_allocated(currcons))
759 new_cols = (cols ? cols : video_num_columns);
760 new_rows = (lines ? lines : video_num_lines);
761 new_row_size = new_cols << 1;
762 new_screen_size = new_row_size * new_rows;
764 if (new_cols == video_num_columns && new_rows == video_num_lines)
767 newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
771 old_rows = video_num_lines;
772 old_cols = video_num_columns;
773 old_row_size = video_size_row;
774 old_screen_size = screenbuf_size;
776 video_num_lines = new_rows;
777 video_num_columns = new_cols;
778 video_size_row = new_row_size;
779 screenbuf_size = new_screen_size;
781 err = resize_screen(currcons, new_cols, new_rows);
787 rlth = min(old_row_size, new_row_size);
788 rrem = new_row_size - rlth;
790 new_origin = (long) newscreen;
791 new_scr_end = new_origin + new_screen_size;
792 if (new_rows < old_rows)
793 old_origin += (old_rows - new_rows) * old_row_size;
795 update_attr(currcons);
797 while (old_origin < scr_end) {
798 scr_memcpyw((unsigned short *) new_origin, (unsigned short *) old_origin, rlth);
800 scr_memsetw((void *)(new_origin + rlth), video_erase_char, rrem);
801 old_origin += old_row_size;
802 new_origin += new_row_size;
804 if (new_scr_end > new_origin)
805 scr_memsetw((void *) new_origin, video_erase_char, new_scr_end - new_origin);
808 screenbuf = newscreen;
810 screenbuf_size = new_screen_size;
812 err = resize_screen(currcons, new_cols, new_rows);
813 set_origin(currcons);
815 /* do part of a reset_terminal() */
817 bottom = video_num_lines;
818 gotoxy(currcons, x, y);
821 if (vc_cons[currcons].d->vc_tty) {
822 struct winsize ws, *cws = &vc_cons[currcons].d->vc_tty->winsize;
824 memset(&ws, 0, sizeof(ws));
825 ws.ws_row = video_num_lines;
826 ws.ws_col = video_num_columns;
827 ws.ws_ypixel = video_scan_lines;
828 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
829 vc_cons[currcons].d->vc_tty->pgrp > 0)
830 kill_pg(vc_cons[currcons].d->vc_tty->pgrp, SIGWINCH, 1);
835 update_screen(currcons);
840 void vc_disallocate(unsigned int currcons)
842 WARN_CONSOLE_UNLOCKED();
844 if (vc_cons_allocated(currcons)) {
845 sw->con_deinit(vc_cons[currcons].d);
848 if (currcons >= MIN_NR_CONSOLES)
849 kfree(vc_cons[currcons].d);
850 vc_cons[currcons].d = NULL;
858 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
859 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
860 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
862 #define decarm VC_REPEAT
863 #define decckm VC_CKMODE
864 #define kbdapplic VC_APPLIC
868 * this is what the terminal answers to a ESC-Z or csi0c query.
870 #define VT100ID "\033[?1;2c"
871 #define VT102ID "\033[?6c"
873 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
874 8,12,10,14, 9,13,11,15 };
876 /* the default colour table, for VGA+ colour systems */
877 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
878 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
879 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
880 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
881 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
882 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
885 * gotoxy() must verify all boundaries, because the arguments
886 * might also be negative. If the given position is out of
887 * bounds, the cursor is placed at the nearest margin.
889 static void gotoxy(int currcons, int new_x, int new_y)
896 if (new_x >= video_num_columns)
897 x = video_num_columns - 1;
905 max_y = video_num_lines;
909 else if (new_y >= max_y)
913 pos = origin + y*video_size_row + (x<<1);
917 /* for absolute user moves, when decom is set */
918 static void gotoxay(int currcons, int new_x, int new_y)
920 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
923 void scrollback(int lines)
925 int currcons = fg_console;
928 lines = video_num_lines/2;
932 void scrollfront(int lines)
934 int currcons = fg_console;
937 lines = video_num_lines/2;
941 static void lf(int currcons)
943 /* don't scroll if above bottom of scrolling region, or
944 * if below scrolling region
947 scrup(currcons,top,bottom,1);
948 else if (y < video_num_lines-1) {
950 pos += video_size_row;
955 static void ri(int currcons)
957 /* don't scroll if below top of scrolling region, or
958 * if above scrolling region
961 scrdown(currcons,top,bottom,1);
964 pos -= video_size_row;
969 static inline void cr(int currcons)
975 static inline void bs(int currcons)
984 static inline void del(int currcons)
989 static void csi_J(int currcons, int vpar)
992 unsigned short * start;
995 case 0: /* erase from cursor to end of display */
996 count = (scr_end-pos)>>1;
997 start = (unsigned short *) pos;
999 /* do in two stages */
1000 sw->con_clear(vc_cons[currcons].d, y, x, 1,
1001 video_num_columns-x);
1002 sw->con_clear(vc_cons[currcons].d, y+1, 0,
1003 video_num_lines-y-1,
1007 case 1: /* erase from start to cursor */
1008 count = ((pos-origin)>>1)+1;
1009 start = (unsigned short *) origin;
1011 /* do in two stages */
1012 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
1014 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1018 case 2: /* erase whole display */
1019 count = video_num_columns * video_num_lines;
1020 start = (unsigned short *) origin;
1022 sw->con_clear(vc_cons[currcons].d, 0, 0,
1029 scr_memsetw(start, video_erase_char, 2*count);
1033 static void csi_K(int currcons, int vpar)
1036 unsigned short * start;
1039 case 0: /* erase from cursor to end of line */
1040 count = video_num_columns-x;
1041 start = (unsigned short *) pos;
1043 sw->con_clear(vc_cons[currcons].d, y, x, 1,
1044 video_num_columns-x);
1046 case 1: /* erase from start of line to cursor */
1047 start = (unsigned short *) (pos - (x<<1));
1050 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1053 case 2: /* erase whole line */
1054 start = (unsigned short *) (pos - (x<<1));
1055 count = video_num_columns;
1057 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1063 scr_memsetw(start, video_erase_char, 2 * count);
1067 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
1073 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
1075 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
1077 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
1081 static void default_attr(int currcons)
1090 /* console_sem is held */
1091 static void csi_m(int currcons)
1095 for (i=0;i<=npar;i++)
1097 case 0: /* all attributes off */
1098 default_attr(currcons);
1115 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1116 * Select primary font, don't display
1117 * control chars if defined, don't set
1120 translate = set_translate(charset == 0
1122 : G1_charset,currcons);
1126 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1127 * Select first alternate font, lets
1128 * chars < 32 be displayed as ROM chars.
1130 translate = set_translate(IBMPC_MAP,currcons);
1134 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1135 * Select second alternate font, toggle
1136 * high bit before displaying as ROM char.
1138 translate = set_translate(IBMPC_MAP,currcons);
1155 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1156 * Enables underscore, white foreground
1157 * with white underscore (Linux - use
1158 * default foreground).
1160 color = (def_color & 0x0f) | background;
1163 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1164 * Disable underline option.
1165 * Reset colour to default? It did this
1168 color = (def_color & 0x0f) | background;
1172 color = (def_color & 0xf0) | foreground;
1175 if (par[i] >= 30 && par[i] <= 37)
1176 color = color_table[par[i]-30]
1178 else if (par[i] >= 40 && par[i] <= 47)
1179 color = (color_table[par[i]-40]<<4)
1183 update_attr(currcons);
1186 static void respond_string(const char *p, struct tty_struct *tty)
1189 tty_insert_flip_char(tty, *p, 0);
1192 con_schedule_flip(tty);
1195 static void cursor_report(int currcons, struct tty_struct *tty)
1199 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1200 respond_string(buf, tty);
1203 static inline void status_report(struct tty_struct *tty)
1205 respond_string("\033[0n", tty); /* Terminal ok */
1208 static inline void respond_ID(struct tty_struct * tty)
1210 respond_string(VT102ID, tty);
1213 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1217 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1219 respond_string(buf, tty);
1222 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1223 int mouse_reporting(void)
1225 int currcons = fg_console;
1227 return report_mouse;
1230 /* console_sem is held */
1231 static void set_mode(int currcons, int on_off)
1235 for (i=0; i<=npar; i++)
1236 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1237 case 1: /* Cursor keys send ^[Ox/^[[x */
1243 case 3: /* 80/132 mode switch unimplemented */
1246 (void) vc_resize(deccolm ? 132 : 80, video_num_lines);
1247 /* this alone does not suffice; some user mode
1248 utility has to change the hardware regs */
1251 case 5: /* Inverted screen on/off */
1252 if (decscnm != on_off) {
1254 invert_screen(currcons, 0, screenbuf_size, 0);
1255 update_attr(currcons);
1258 case 6: /* Origin relative/absolute */
1260 gotoxay(currcons,0,0);
1262 case 7: /* Autowrap on/off */
1265 case 8: /* Autorepeat on/off */
1272 report_mouse = on_off ? 1 : 0;
1274 case 25: /* Cursor on/off */
1278 report_mouse = on_off ? 2 : 0;
1280 } else switch(par[i]) { /* ANSI modes set/reset */
1281 case 3: /* Monitor (display ctrls) */
1284 case 4: /* Insert Mode on/off */
1287 case 20: /* Lf, Enter == CrLf/Lf */
1296 /* console_sem is held */
1297 static void setterm_command(int currcons)
1300 case 1: /* set color for underline mode */
1301 if (can_do_color && par[1] < 16) {
1302 ulcolor = color_table[par[1]];
1304 update_attr(currcons);
1307 case 2: /* set color for half intensity mode */
1308 if (can_do_color && par[1] < 16) {
1309 halfcolor = color_table[par[1]];
1311 update_attr(currcons);
1314 case 8: /* store colors as defaults */
1316 if (hi_font_mask == 0x100)
1318 default_attr(currcons);
1319 update_attr(currcons);
1321 case 9: /* set blanking interval */
1322 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1323 poke_blanked_console();
1325 case 10: /* set bell frequency in Hz */
1327 bell_pitch = par[1];
1329 bell_pitch = DEFAULT_BELL_PITCH;
1331 case 11: /* set bell duration in msec */
1333 bell_duration = (par[1] < 2000) ?
1336 bell_duration = DEFAULT_BELL_DURATION;
1338 case 12: /* bring specified console to the front */
1339 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1340 set_console(par[1] - 1);
1342 case 13: /* unblank the screen */
1343 poke_blanked_console();
1345 case 14: /* set vesa powerdown interval */
1346 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1348 case 15: /* activate the previous console */
1349 set_console(last_console);
1354 /* console_sem is held */
1355 static void csi_at(int currcons, unsigned int nr)
1357 if (nr > video_num_columns - x)
1358 nr = video_num_columns - x;
1361 insert_char(currcons, nr);
1364 /* console_sem is held */
1365 static void csi_L(int currcons, unsigned int nr)
1367 if (nr > video_num_lines - y)
1368 nr = video_num_lines - y;
1371 scrdown(currcons,y,bottom,nr);
1375 /* console_sem is held */
1376 static void csi_P(int currcons, unsigned int nr)
1378 if (nr > video_num_columns - x)
1379 nr = video_num_columns - x;
1382 delete_char(currcons, nr);
1385 /* console_sem is held */
1386 static void csi_M(int currcons, unsigned int nr)
1388 if (nr > video_num_lines - y)
1389 nr = video_num_lines - y;
1392 scrup(currcons,y,bottom,nr);
1396 /* console_sem is held (except via vc_init->reset_terminal */
1397 static void save_cur(int currcons)
1401 s_intensity = intensity;
1402 s_underline = underline;
1404 s_reverse = reverse;
1405 s_charset = charset;
1407 saved_G0 = G0_charset;
1408 saved_G1 = G1_charset;
1411 /* console_sem is held */
1412 static void restore_cur(int currcons)
1414 gotoxy(currcons,saved_x,saved_y);
1415 intensity = s_intensity;
1416 underline = s_underline;
1418 reverse = s_reverse;
1419 charset = s_charset;
1421 G0_charset = saved_G0;
1422 G1_charset = saved_G1;
1423 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1424 update_attr(currcons);
1428 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1429 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1432 /* console_sem is held (except via vc_init()) */
1433 static void reset_terminal(int currcons, int do_clear)
1436 bottom = video_num_lines;
1437 vc_state = ESnormal;
1439 translate = set_translate(LAT1_MAP,currcons);
1440 G0_charset = LAT1_MAP;
1441 G1_charset = GRAF_MAP;
1461 kbd_table[currcons].lockstate = 0;
1462 kbd_table[currcons].slockstate = 0;
1463 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1464 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1465 /* do not do set_leds here because this causes an endless tasklet loop
1466 when the keyboard hasn't been initialized yet */
1468 cursor_type = CUR_DEFAULT;
1469 complement_mask = s_complement_mask;
1471 default_attr(currcons);
1472 update_attr(currcons);
1474 tab_stop[0] = 0x01010100;
1478 tab_stop[4] = 0x01010101;
1480 bell_pitch = DEFAULT_BELL_PITCH;
1481 bell_duration = DEFAULT_BELL_DURATION;
1483 gotoxy(currcons,0,0);
1489 /* console_sem is held */
1490 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1493 * Control characters can be used in the _middle_
1494 * of an escape sequence.
1501 kd_mksound(bell_pitch, bell_duration);
1508 while (x < video_num_columns - 1) {
1510 if (tab_stop[x >> 5] & (1 << (x & 31)))
1515 case 10: case 11: case 12:
1524 translate = set_translate(G1_charset,currcons);
1529 translate = set_translate(G0_charset,currcons);
1533 vc_state = ESnormal;
1542 vc_state = ESsquare;
1547 vc_state = ESnormal;
1550 vc_state = ESsquare;
1553 vc_state = ESnonstd;
1556 vc_state = ESpercent;
1569 tab_stop[x >> 5] |= (1 << (x & 31));
1578 restore_cur(currcons);
1590 reset_terminal(currcons,1);
1592 case '>': /* Numeric keypad */
1595 case '=': /* Appl. keypad */
1601 if (c=='P') { /* palette escape sequence */
1602 for (npar=0; npar<NPAR; npar++)
1605 vc_state = ESpalette;
1607 } else if (c=='R') { /* reset palette */
1608 reset_palette(currcons);
1609 vc_state = ESnormal;
1611 vc_state = ESnormal;
1614 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1615 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1617 int i = par[0]*3, j = 1;
1618 palette[i] = 16*par[j++];
1619 palette[i++] += par[j++];
1620 palette[i] = 16*par[j++];
1621 palette[i++] += par[j++];
1622 palette[i] = 16*par[j++];
1623 palette[i] += par[j];
1624 set_palette(currcons);
1625 vc_state = ESnormal;
1628 vc_state = ESnormal;
1631 for(npar = 0 ; npar < NPAR ; npar++)
1634 vc_state = ESgetpars;
1635 if (c == '[') { /* Function key */
1643 if (c==';' && npar<NPAR-1) {
1646 } else if (c>='0' && c<='9') {
1650 } else vc_state=ESgotpars;
1652 vc_state = ESnormal;
1655 set_mode(currcons,1);
1658 set_mode(currcons,0);
1663 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1665 cursor_type = CUR_DEFAULT;
1673 complement_mask = par[0]<<8 | par[1];
1675 complement_mask = s_complement_mask;
1683 else if (par[0] == 6)
1684 cursor_report(currcons,tty);
1694 if (par[0]) par[0]--;
1695 gotoxy(currcons,par[0],y);
1698 if (!par[0]) par[0]++;
1699 gotoxy(currcons,x,y-par[0]);
1702 if (!par[0]) par[0]++;
1703 gotoxy(currcons,x,y+par[0]);
1706 if (!par[0]) par[0]++;
1707 gotoxy(currcons,x+par[0],y);
1710 if (!par[0]) par[0]++;
1711 gotoxy(currcons,x-par[0],y);
1714 if (!par[0]) par[0]++;
1715 gotoxy(currcons,0,y+par[0]);
1718 if (!par[0]) par[0]++;
1719 gotoxy(currcons,0,y-par[0]);
1722 if (par[0]) par[0]--;
1723 gotoxay(currcons,x,par[0]);
1726 if (par[0]) par[0]--;
1727 if (par[1]) par[1]--;
1728 gotoxay(currcons,par[1],par[0]);
1731 csi_J(currcons,par[0]);
1734 csi_K(currcons,par[0]);
1737 csi_L(currcons,par[0]);
1740 csi_M(currcons,par[0]);
1743 csi_P(currcons,par[0]);
1751 tab_stop[x >> 5] &= ~(1 << (x & 31));
1752 else if (par[0] == 3) {
1763 case 'q': /* DECLL - but only 3 leds */
1764 /* map 0,1,2,3 to 0,1,2,4 */
1766 setledstate(kbd_table + currcons,
1767 (par[0] < 3) ? par[0] : 4);
1773 par[1] = video_num_lines;
1774 /* Minimum allowed region is 2 lines */
1775 if (par[0] < par[1] &&
1776 par[1] <= video_num_lines) {
1779 gotoxay(currcons,0,0);
1786 restore_cur(currcons);
1789 csi_X(currcons, par[0]);
1792 csi_at(currcons,par[0]);
1794 case ']': /* setterm functions */
1795 setterm_command(currcons);
1800 vc_state = ESnormal;
1802 case '@': /* defined in ISO 2022 */
1805 case 'G': /* prelim official escape code */
1806 case '8': /* retained for compatibility */
1812 vc_state = ESnormal;
1815 vc_state = ESnormal;
1817 /* DEC screen alignment test. kludge :-) */
1819 (video_erase_char & 0xff00) | 'E';
1822 (video_erase_char & 0xff00) | ' ';
1823 do_update_region(currcons, origin, screenbuf_size/2);
1828 G0_charset = GRAF_MAP;
1830 G0_charset = LAT1_MAP;
1832 G0_charset = IBMPC_MAP;
1834 G0_charset = USER_MAP;
1836 translate = set_translate(G0_charset,currcons);
1837 vc_state = ESnormal;
1841 G1_charset = GRAF_MAP;
1843 G1_charset = LAT1_MAP;
1845 G1_charset = IBMPC_MAP;
1847 G1_charset = USER_MAP;
1849 translate = set_translate(G1_charset,currcons);
1850 vc_state = ESnormal;
1853 vc_state = ESnormal;
1857 /* This is a temporary buffer used to prepare a tty console write
1858 * so that we can easily avoid touching user space while holding the
1859 * console spinlock. It is allocated in con_init and is shared by
1860 * this code and the vc_screen read/write tty calls.
1862 * We have to allocate this statically in the kernel data section
1863 * since console_init (and thus con_init) are called before any
1864 * kernel memory allocation is available.
1866 char con_buf[PAGE_SIZE];
1867 #define CON_BUF_SIZE PAGE_SIZE
1868 DECLARE_MUTEX(con_buf_sem);
1870 /* acquires console_sem */
1871 static int do_con_write(struct tty_struct *tty, int from_user,
1872 const unsigned char *buf, int count)
1874 #ifdef VT_BUF_VRAM_ONLY
1875 #define FLUSH do { } while(0);
1877 #define FLUSH if (draw_x >= 0) { \
1878 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1883 int c, tc, ok, n = 0, draw_x = -1;
1884 unsigned int currcons;
1885 unsigned long draw_from = 0, draw_to = 0;
1886 struct vt_struct *vt;
1887 u16 himask, charmask;
1888 const unsigned char *orig_buf = NULL;
1896 acquire_console_sem();
1897 vt = tty->driver_data;
1899 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
1900 release_console_sem();
1904 currcons = vt->vc_num;
1905 if (!vc_cons_allocated(currcons)) {
1906 /* could this happen? */
1907 static int error = 0;
1910 printk("con_write: tty %d not allocated\n", currcons+1);
1912 release_console_sem();
1915 release_console_sem();
1925 if (count > CON_BUF_SIZE)
1926 count = CON_BUF_SIZE;
1927 console_conditional_schedule();
1928 if (copy_from_user(con_buf, buf, count)) {
1929 n = 0; /* ?? are error codes legal here ?? */
1936 /* At this point 'buf' is guaranteed to be a kernel buffer
1937 * and therefore no access to userspace (and therefore sleeping)
1938 * will be needed. The con_buf_sem serializes all tty based
1939 * console rendering and vcs write/read operations. We hold
1940 * the console spinlock during the entire write.
1943 acquire_console_sem();
1945 vt = tty->driver_data;
1947 printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
1948 release_console_sem();
1952 himask = hi_font_mask;
1953 charmask = himask ? 0x1ff : 0xff;
1955 /* undraw cursor first */
1957 hide_cursor(currcons);
1959 while (!tty->stopped && count) {
1966 /* Combine UTF-8 into Unicode */
1967 /* Incomplete characters silently ignored */
1969 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1970 utf_char = (utf_char << 6) | (c & 0x3f);
1976 if ((c & 0xe0) == 0xc0) {
1978 utf_char = (c & 0x1f);
1979 } else if ((c & 0xf0) == 0xe0) {
1981 utf_char = (c & 0x0f);
1982 } else if ((c & 0xf8) == 0xf0) {
1984 utf_char = (c & 0x07);
1985 } else if ((c & 0xfc) == 0xf8) {
1987 utf_char = (c & 0x03);
1988 } else if ((c & 0xfe) == 0xfc) {
1990 utf_char = (c & 0x01);
1999 } else { /* no utf */
2000 tc = translate[toggle_meta ? (c|0x80) : c];
2003 /* If the original code was a control character we
2004 * only allow a glyph to be displayed if the code is
2005 * not normally used (such as for cursor movement) or
2006 * if the disp_ctrl mode has been explicitly enabled.
2007 * Certain characters (as given by the CTRL_ALWAYS
2008 * bitmap) are always displayed as control characters,
2009 * as the console would be pretty useless without
2010 * them; to display an arbitrary font position use the
2011 * direct-to-font zone in UTF-8 mode.
2013 ok = tc && (c >= 32 ||
2014 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
2015 : CTRL_ACTION) >> c) & 1)))
2016 && (c != 127 || disp_ctrl)
2019 if (vc_state == ESnormal && ok) {
2020 /* Now try to find out how to display it */
2021 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
2023 /* If we got -4 (not found) then see if we have
2024 defined a replacement character (U+FFFD) */
2025 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
2027 /* One reason for the -4 can be that we just
2028 did a clear_unimap();
2029 try at least to show something. */
2032 } else if ( tc == -3 ) {
2033 /* Bad hash table -- hope for the best */
2037 continue; /* Conversion failed */
2039 if (need_wrap || decim)
2046 insert_char(currcons, 1);
2048 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2051 if (DO_UPDATE && draw_x < 0) {
2055 if (x == video_num_columns - 1) {
2065 do_con_trol(tty, currcons, c);
2068 console_conditional_schedule();
2069 release_console_sem();
2073 /* If the user requested something larger than
2074 * the CON_BUF_SIZE, and the tty is not stopped,
2077 if ((orig_count > CON_BUF_SIZE) && !tty->stopped) {
2078 orig_count -= CON_BUF_SIZE;
2079 orig_buf += CON_BUF_SIZE;
2093 * This is the console switching callback.
2095 * Doing console switching in a process context allows
2096 * us to do the switches asynchronously (needed when we want
2097 * to switch due to a keyboard interrupt). Synchronization
2098 * with other console code and prevention of re-entrancy is
2099 * ensured with console_sem.
2101 static void console_callback(void *ignored)
2103 acquire_console_sem();
2105 if (want_console >= 0) {
2106 if (want_console != fg_console &&
2107 vc_cons_allocated(want_console)) {
2108 hide_cursor(fg_console);
2109 change_console(want_console);
2110 /* we only changed when the console had already
2111 been allocated - a new console is not created
2112 in an interrupt routine */
2116 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2117 do_poke_blanked_console = 0;
2118 poke_blanked_console();
2120 if (scrollback_delta) {
2121 int currcons = fg_console;
2123 if (vcmode == KD_TEXT)
2124 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
2125 scrollback_delta = 0;
2127 if (blank_timer_expired) {
2129 blank_timer_expired = 0;
2132 release_console_sem();
2135 void set_console(int nr)
2138 schedule_console_callback();
2141 struct tty_driver *console_driver;
2143 #ifdef CONFIG_VT_CONSOLE
2146 * Console on virtual terminal
2148 * The console must be locked when we get here.
2151 void vt_console_print(struct console *co, const char *b, unsigned count)
2153 int currcons = fg_console;
2155 static unsigned long printing;
2156 const ushort *start;
2160 /* console busy or not yet initialized */
2161 if (!printable || test_and_set_bit(0, &printing))
2166 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2167 currcons = kmsg_redirect - 1;
2169 /* read `x' only after setting currcons properly (otherwise
2170 the `x' macro will read the x of the foreground console). */
2173 if (!vc_cons_allocated(currcons)) {
2175 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2179 if (vcmode != KD_TEXT)
2182 /* undraw cursor first */
2184 hide_cursor(currcons);
2186 start = (ushort *)pos;
2188 /* Contrived structure to try to emulate original need_wrap behaviour
2189 * Problems caused when we have need_wrap set on '\n' character */
2192 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2195 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2201 if (c == 8) { /* backspace */
2203 start = (ushort *)pos;
2210 start = (ushort *)pos;
2212 if (c == 10 || c == 13)
2215 scr_writew((attr << 8) + c, (unsigned short *) pos);
2217 if (myx == video_num_columns - 1) {
2226 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2228 if (x == video_num_columns) {
2233 set_cursor(currcons);
2235 if (!oops_in_progress)
2236 poke_blanked_console();
2239 clear_bit(0, &printing);
2242 static struct tty_driver *vt_console_device(struct console *c, int *index)
2244 *index = c->index ? c->index-1 : fg_console;
2245 return console_driver;
2248 struct console vt_console_driver = {
2250 .write = vt_console_print,
2251 .device = vt_console_device,
2252 .unblank = unblank_screen,
2253 .flags = CON_PRINTBUFFER,
2259 * Handling of Linux-specific VC ioctls
2263 * Generally a bit racy with respect to console_sem().
2265 * There are some functions which don't need it.
2267 * There are some functions which can sleep for arbitrary periods
2268 * (paste_selection) but we don't need the lock there anyway.
2270 * set_selection has locking, and definitely needs it
2273 int tioclinux(struct tty_struct *tty, unsigned long arg)
2279 if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
2281 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2283 if (get_user(type, (char *)arg))
2289 acquire_console_sem();
2290 ret = set_selection((struct tiocl_selection *)((char *)arg+1), tty, 1);
2291 release_console_sem();
2293 case TIOCL_PASTESEL:
2294 ret = paste_selection(tty);
2296 case TIOCL_UNBLANKSCREEN:
2299 case TIOCL_SELLOADLUT:
2300 ret = sel_loadlut(arg);
2302 case TIOCL_GETSHIFTSTATE:
2305 * Make it possible to react to Shift+Mousebutton.
2306 * Note that 'shift_state' is an undocumented
2307 * kernel-internal variable; programs not closely
2308 * related to the kernel should not use this.
2311 ret = __put_user(data, (char *) arg);
2313 case TIOCL_GETMOUSEREPORTING:
2314 data = mouse_reporting();
2315 ret = __put_user(data, (char *) arg);
2317 case TIOCL_SETVESABLANK:
2318 set_vesa_blanking(arg);
2320 case TIOCL_SETKMSGREDIRECT:
2321 if (!capable(CAP_SYS_ADMIN)) {
2324 if (get_user(data, (char *)arg+1))
2327 kmsg_redirect = data;
2330 case TIOCL_GETFGCONSOLE:
2333 case TIOCL_SCROLLCONSOLE:
2334 if (get_user(lines, (s32 *)((char *)arg+4))) {
2341 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2345 case TIOCL_BLANKEDSCREEN:
2346 ret = console_blanked;
2356 * /dev/ttyN handling
2359 static int con_write(struct tty_struct *tty, int from_user,
2360 const unsigned char *buf, int count)
2365 retval = do_con_write(tty, from_user, buf, count);
2366 con_flush_chars(tty);
2371 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2374 return; /* n_r3964 calls put_char() from interrupt context */
2376 do_con_write(tty, 0, &ch, 1);
2379 static int con_write_room(struct tty_struct *tty)
2383 return 4096; /* No limit, really; we're not buffering */
2386 static int con_chars_in_buffer(struct tty_struct *tty)
2388 return 0; /* we're not buffering */
2392 * con_throttle and con_unthrottle are only used for
2393 * paste_selection(), which has to stuff in a large number of
2396 static void con_throttle(struct tty_struct *tty)
2400 static void con_unthrottle(struct tty_struct *tty)
2402 struct vt_struct *vt = tty->driver_data;
2404 wake_up_interruptible(&vt->paste_wait);
2408 * Turn the Scroll-Lock LED on when the tty is stopped
2410 static void con_stop(struct tty_struct *tty)
2415 console_num = tty->index;
2416 if (!vc_cons_allocated(console_num))
2418 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2423 * Turn the Scroll-Lock LED off when the console is started
2425 static void con_start(struct tty_struct *tty)
2430 console_num = tty->index;
2431 if (!vc_cons_allocated(console_num))
2433 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2437 static void con_flush_chars(struct tty_struct *tty)
2439 struct vt_struct *vt;
2441 if (in_interrupt()) /* from flush_to_ldisc */
2446 /* if we race with con_close(), vt may be null */
2447 acquire_console_sem();
2448 vt = tty->driver_data;
2450 set_cursor(vt->vc_num);
2451 release_console_sem();
2455 * Allocate the console screen memory.
2457 static int con_open(struct tty_struct *tty, struct file *filp)
2459 unsigned int currcons = tty->index;
2462 acquire_console_sem();
2463 if (tty->count == 1) {
2464 ret = vc_allocate(currcons);
2466 vt_cons[currcons]->vc_num = currcons;
2467 tty->driver_data = vt_cons[currcons];
2468 vc_cons[currcons].d->vc_tty = tty;
2470 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2471 tty->winsize.ws_row = video_num_lines;
2472 tty->winsize.ws_col = video_num_columns;
2474 release_console_sem();
2475 vcs_make_devfs(tty);
2479 release_console_sem();
2484 * We take tty_sem in here to prevent another thread from coming in via init_dev
2485 * and taking a ref against the tty while we're in the process of forgetting
2486 * about it and cleaning things up.
2488 * This is because vcs_remove_devfs() can sleep and will drop the BKL.
2490 static void con_close(struct tty_struct *tty, struct file *filp)
2493 acquire_console_sem();
2494 if (tty && tty->count == 1) {
2495 struct vt_struct *vt;
2497 vt = tty->driver_data;
2499 vc_cons[vt->vc_num].d->vc_tty = NULL;
2500 tty->driver_data = 0;
2501 release_console_sem();
2502 vcs_remove_devfs(tty);
2505 * tty_sem is released, but we still hold BKL, so there is
2506 * still exclusion against init_dev()
2510 release_console_sem();
2514 static void vc_init(unsigned int currcons, unsigned int rows,
2515 unsigned int cols, int do_clear)
2519 video_num_columns = cols;
2520 video_num_lines = rows;
2521 video_size_row = cols<<1;
2522 screenbuf_size = video_num_lines * video_size_row;
2524 set_origin(currcons);
2527 for (j=k=0; j<16; j++) {
2528 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2529 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2530 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2532 def_color = 0x07; /* white */
2533 ulcolor = 0x0f; /* bold white */
2534 halfcolor = 0x08; /* grey */
2535 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2536 reset_terminal(currcons, do_clear);
2540 * This routine initializes console interrupts, and does nothing
2541 * else. If you want the screen to clear, call tty_write with
2542 * the appropriate escape-sequence.
2545 static int __init con_init(void)
2547 const char *display_desc = NULL;
2548 unsigned int currcons = 0;
2550 acquire_console_sem();
2553 display_desc = conswitchp->con_startup();
2554 if (!display_desc) {
2556 release_console_sem();
2560 init_timer(&console_timer);
2561 console_timer.function = blank_screen_t;
2562 if (blankinterval) {
2563 blank_state = blank_normal_wait;
2564 mod_timer(&console_timer, jiffies + blankinterval);
2568 * kmalloc is not running yet - we use the bootmem allocator.
2570 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2571 vc_cons[currcons].d = (struct vc_data *)
2572 alloc_bootmem(sizeof(struct vc_data));
2573 vt_cons[currcons] = (struct vt_struct *)
2574 alloc_bootmem(sizeof(struct vt_struct));
2575 visual_init(currcons, 1);
2576 screenbuf = (unsigned short *) alloc_bootmem(screenbuf_size);
2578 vc_init(currcons, video_num_lines, video_num_columns,
2579 currcons || !sw->con_save_screen);
2581 currcons = fg_console = 0;
2582 master_display_fg = vc_cons[currcons].d;
2583 set_origin(currcons);
2584 save_screen(currcons);
2585 gotoxy(currcons,x,y);
2587 update_screen(fg_console);
2588 printk("Console: %s %s %dx%d",
2589 can_do_color ? "colour" : "mono",
2590 display_desc, video_num_columns, video_num_lines);
2594 release_console_sem();
2596 #ifdef CONFIG_VT_CONSOLE
2597 register_console(&vt_console_driver);
2601 console_initcall(con_init);
2603 static struct tty_operations con_ops = {
2607 .write_room = con_write_room,
2608 .put_char = con_put_char,
2609 .flush_chars = con_flush_chars,
2610 .chars_in_buffer = con_chars_in_buffer,
2614 .throttle = con_throttle,
2615 .unthrottle = con_unthrottle,
2618 int __init vty_init(void)
2622 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2623 if (!console_driver)
2624 panic("Couldn't allocate console driver\n");
2625 console_driver->owner = THIS_MODULE;
2626 console_driver->devfs_name = "vc/";
2627 console_driver->name = "tty";
2628 console_driver->name_base = 1;
2629 console_driver->major = TTY_MAJOR;
2630 console_driver->minor_start = 1;
2631 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2632 console_driver->init_termios = tty_std_termios;
2633 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2634 tty_set_operations(console_driver, &con_ops);
2635 if (tty_register_driver(console_driver))
2636 panic("Couldn't register console driver\n");
2640 #ifdef CONFIG_PROM_CONSOLE
2643 #ifdef CONFIG_MDA_CONSOLE
2646 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
2652 #ifndef VT_SINGLE_DRIVER
2654 static void clear_buffer_attributes(int currcons)
2656 unsigned short *p = (unsigned short *) origin;
2657 int count = screenbuf_size/2;
2658 int mask = hi_font_mask | 0xff;
2660 for (; count > 0; count--, p++) {
2661 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2666 * If we support more console drivers, this function is used
2667 * when a driver wants to take over some existing consoles
2668 * and become default driver for newly opened ones.
2671 void take_over_console(const struct consw *csw, int first, int last, int deflt)
2676 acquire_console_sem();
2678 desc = csw->con_startup();
2680 release_console_sem();
2686 for (i = first; i <= last; i++) {
2690 con_driver_map[i] = csw;
2692 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2698 old_was_color = vc_cons[i].d->vc_can_do_color;
2699 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2703 /* If the console changed between mono <-> color, then
2704 * the attributes in the screenbuf will be wrong. The
2705 * following resets all attributes to something sane.
2707 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2708 clear_buffer_attributes(i);
2713 printk("Console: switching ");
2715 printk("consoles %d-%d ", first+1, last+1);
2717 printk("to %s %s %dx%d\n",
2718 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2719 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2721 printk("to %s\n", desc);
2723 release_console_sem();
2726 void give_up_console(const struct consw *csw)
2730 for(i = 0; i < MAX_NR_CONSOLES; i++)
2731 if (con_driver_map[i] == csw)
2732 con_driver_map[i] = NULL;
2741 static void set_vesa_blanking(unsigned long arg)
2743 char *argp = (char *)arg + 1;
2745 get_user(mode, argp);
2746 vesa_blank_mode = (mode < 4) ? mode : 0;
2750 * This is called by a timer handler
2752 static void vesa_powerdown(void)
2754 struct vc_data *c = vc_cons[fg_console].d;
2756 * Power down if currently suspended (1 or 2),
2757 * suspend if currently blanked (0),
2758 * else do nothing (i.e. already powered down (3)).
2759 * Called only if powerdown features are allowed.
2761 switch (vesa_blank_mode) {
2762 case VESA_NO_BLANKING:
2763 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1, 0);
2765 case VESA_VSYNC_SUSPEND:
2766 case VESA_HSYNC_SUSPEND:
2767 c->vc_sw->con_blank(c, VESA_POWERDOWN+1, 0);
2772 void do_blank_screen(int entering_gfx)
2774 int currcons = fg_console;
2777 WARN_CONSOLE_UNLOCKED();
2779 if (console_blanked) {
2780 if (blank_state == blank_vesa_wait) {
2781 blank_state = blank_off;
2787 if (blank_state != blank_normal_wait)
2789 blank_state = blank_off;
2791 /* entering graphics mode? */
2793 hide_cursor(currcons);
2794 save_screen(currcons);
2795 sw->con_blank(vc_cons[currcons].d, -1, 1);
2796 console_blanked = fg_console + 1;
2797 set_origin(currcons);
2801 /* don't blank graphics */
2802 if (vcmode != KD_TEXT) {
2803 console_blanked = fg_console + 1;
2807 hide_cursor(currcons);
2808 del_timer_sync(&console_timer);
2809 blank_timer_expired = 0;
2811 save_screen(currcons);
2812 /* In case we need to reset origin, blanking hook returns 1 */
2813 i = sw->con_blank(vc_cons[currcons].d, 1, 0);
2814 console_blanked = fg_console + 1;
2816 set_origin(currcons);
2818 if (console_blank_hook && console_blank_hook(1))
2821 if (vesa_off_interval) {
2822 blank_state = blank_vesa_wait,
2823 mod_timer(&console_timer, jiffies + vesa_off_interval);
2826 if (vesa_blank_mode)
2827 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1, 0);
2832 * Called by timer as well as from vt_console_driver
2834 void do_unblank_screen(int leaving_gfx)
2838 WARN_CONSOLE_UNLOCKED();
2841 if (!console_blanked)
2843 if (!vc_cons_allocated(fg_console)) {
2845 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2848 currcons = fg_console;
2849 if (vcmode != KD_TEXT)
2850 return; /* but leave console_blanked != 0 */
2852 if (blankinterval) {
2853 mod_timer(&console_timer, jiffies + blankinterval);
2854 blank_state = blank_normal_wait;
2857 console_blanked = 0;
2858 if (sw->con_blank(vc_cons[currcons].d, 0, leaving_gfx))
2859 /* Low-level driver cannot restore -> do it ourselves */
2860 update_screen(fg_console);
2861 if (console_blank_hook)
2862 console_blank_hook(0);
2863 set_palette(currcons);
2864 set_cursor(fg_console);
2868 * This is called by the outside world to cause a forced unblank, mostly for
2869 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
2870 * call it with 1 as an argument and so force a mode restore... that may kill
2871 * X or at least garbage the screen but would also make the Oops visible...
2873 void unblank_screen(void)
2875 do_unblank_screen(0);
2879 * We defer the timer blanking to work queue so it can take the console semaphore
2880 * (console operations can still happen at irq time, but only from printk which
2881 * has the console semaphore. Not perfect yet, but better than no locking
2883 static void blank_screen_t(unsigned long dummy)
2885 blank_timer_expired = 1;
2886 schedule_work(&console_work);
2889 void poke_blanked_console(void)
2891 WARN_CONSOLE_UNLOCKED();
2893 /* This isn't perfectly race free, but a race here would be mostly harmless,
2894 * at worse, we'll do a spurrious blank and it's unlikely
2896 del_timer(&console_timer);
2897 blank_timer_expired = 0;
2899 if (ignore_poke || !vt_cons[fg_console] || vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2901 if (console_blanked)
2903 else if (blankinterval) {
2904 mod_timer(&console_timer, jiffies + blankinterval);
2905 blank_state = blank_normal_wait;
2913 void set_palette(int currcons)
2915 WARN_CONSOLE_UNLOCKED();
2917 if (vcmode != KD_GRAPHICS)
2918 sw->con_set_palette(vc_cons[currcons].d, color_table);
2921 static int set_get_cmap(unsigned char *arg, int set)
2925 WARN_CONSOLE_UNLOCKED();
2927 for (i = 0; i < 16; i++)
2929 get_user(default_red[i], arg++);
2930 get_user(default_grn[i], arg++);
2931 get_user(default_blu[i], arg++);
2933 put_user(default_red[i], arg++);
2934 put_user(default_grn[i], arg++);
2935 put_user(default_blu[i], arg++);
2938 for (i = 0; i < MAX_NR_CONSOLES; i++)
2939 if (vc_cons_allocated(i)) {
2940 for (j = k = 0; j < 16; j++) {
2941 vc_cons[i].d->vc_palette[k++] = default_red[j];
2942 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2943 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2952 * Load palette into the DAC registers. arg points to a colour
2953 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2956 int con_set_cmap(unsigned char *arg)
2960 acquire_console_sem();
2961 rc = set_get_cmap (arg,1);
2962 release_console_sem();
2967 int con_get_cmap(unsigned char *arg)
2971 acquire_console_sem();
2972 rc = set_get_cmap (arg,0);
2973 release_console_sem();
2978 void reset_palette(int currcons)
2981 for (j=k=0; j<16; j++) {
2982 palette[k++] = default_red[j];
2983 palette[k++] = default_grn[j];
2984 palette[k++] = default_blu[j];
2986 set_palette(currcons);
2992 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2993 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2994 * depending on width) reserved for each character which is kinda wasty, but
2995 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2996 * is upto the actual low-level console-driver convert data into its favorite
2997 * format (maybe we should add a `fontoffset' field to the `display'
2998 * structure so we won't have to convert the fontdata all the time.
3002 #define max_font_size 65536
3004 int con_font_op(int currcons, struct console_font_op *op)
3007 int size = max_font_size, set;
3009 struct console_font_op old_op;
3011 if (vt_cons[currcons]->vc_mode != KD_TEXT)
3013 memcpy(&old_op, op, sizeof(old_op));
3014 if (op->op == KD_FONT_OP_SET) {
3017 if (op->charcount > 512)
3019 if (!op->height) { /* Need to guess font height [compat] */
3021 u8 *charmap = op->data, tmp;
3023 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
3024 so that we can get rid of this soon */
3025 if (!(op->flags & KD_FONT_FLAG_OLD))
3028 for (h = 32; h > 0; h--)
3029 for (i = 0; i < op->charcount; i++) {
3030 if (get_user(tmp, &charmap[32*i+h-1]))
3041 if (op->width > 32 || op->height > 32)
3043 size = (op->width+7)/8 * 32 * op->charcount;
3044 if (size > max_font_size)
3047 } else if (op->op == KD_FONT_OP_GET)
3050 acquire_console_sem();
3051 rc = sw->con_font_op(vc_cons[currcons].d, op);
3052 release_console_sem();
3056 temp = kmalloc(size, GFP_KERNEL);
3059 if (set && copy_from_user(temp, op->data, size)) {
3066 acquire_console_sem();
3067 rc = sw->con_font_op(vc_cons[currcons].d, op);
3068 release_console_sem();
3070 op->data = old_op.data;
3072 int c = (op->width+7)/8 * 32 * op->charcount;
3074 if (op->data && op->charcount > old_op.charcount)
3076 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3077 if (op->width > old_op.width ||
3078 op->height > old_op.height)
3083 else if ((old_op.height && op->height > old_op.height) ||
3087 if (!rc && op->data && copy_to_user(op->data, temp, c))
3096 * Interface exported to selection and vcs.
3099 /* used by selection */
3100 u16 screen_glyph(int currcons, int offset)
3102 u16 w = scr_readw(screenpos(currcons, offset, 1));
3105 if (w & hi_font_mask)
3110 /* used by vcs - note the word offset */
3111 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
3113 return screenpos(currcons, 2 * w_offset, viewed);
3116 void getconsxy(int currcons, unsigned char *p)
3122 void putconsxy(int currcons, unsigned char *p)
3124 gotoxy(currcons, p[0], p[1]);
3125 set_cursor(currcons);
3128 u16 vcs_scr_readw(int currcons, const u16 *org)
3130 if ((unsigned long)org == pos && softcursor_original != -1)
3131 return softcursor_original;
3132 return scr_readw(org);
3135 void vcs_scr_writew(int currcons, u16 val, u16 *org)
3137 scr_writew(val, org);
3138 if ((unsigned long)org == pos) {
3139 softcursor_original = -1;
3140 add_softcursor(currcons);
3144 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data)
3149 acquire_console_sem();
3151 release_console_sem();
3154 acquire_console_sem();
3156 release_console_sem();
3163 * Visible symbols for modules
3166 EXPORT_SYMBOL(color_table);
3167 EXPORT_SYMBOL(default_red);
3168 EXPORT_SYMBOL(default_grn);
3169 EXPORT_SYMBOL(default_blu);
3170 EXPORT_SYMBOL(vc_cons_allocated);
3171 EXPORT_SYMBOL(update_region);
3172 EXPORT_SYMBOL(redraw_screen);
3173 EXPORT_SYMBOL(vc_resize);
3174 EXPORT_SYMBOL(fg_console);
3175 EXPORT_SYMBOL(console_blank_hook);
3176 EXPORT_SYMBOL(console_blanked);
3177 EXPORT_SYMBOL(vt_cons);
3178 EXPORT_SYMBOL(vc_cons);
3179 #ifndef VT_SINGLE_DRIVER
3180 EXPORT_SYMBOL(take_over_console);
3181 EXPORT_SYMBOL(give_up_console);