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(char __user *p);
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 module_put(sw->owner);
667 #ifndef VT_SINGLE_DRIVER
668 if (con_driver_map[currcons])
669 sw = con_driver_map[currcons];
671 __module_get(sw->owner);
673 display_fg = &master_display_fg;
674 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
675 vc_cons[currcons].d->vc_uni_pagedir = 0;
679 sw->con_init(vc_cons[currcons].d, init);
680 if (!complement_mask)
681 complement_mask = can_do_color ? 0x7700 : 0x0800;
682 s_complement_mask = complement_mask;
683 video_size_row = video_num_columns<<1;
684 screenbuf_size = video_num_lines*video_size_row;
687 int vc_allocate(unsigned int currcons) /* return 0 on success */
689 WARN_CONSOLE_UNLOCKED();
691 if (currcons >= MAX_NR_CONSOLES)
693 if (!vc_cons[currcons].d) {
696 /* prevent users from taking too much memory */
697 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
700 /* due to the granularity of kmalloc, we waste some memory here */
701 /* the alloc is done in two steps, to optimize the common situation
702 of a 25x80 console (structsize=216, screenbuf_size=4000) */
703 /* although the numbers above are not valid since long ago, the
704 point is still up-to-date and the comment still has its value
705 even if only as a historical artifact. --mj, July 1998 */
706 p = (long) kmalloc(structsize, GFP_KERNEL);
709 memset((void *)p, 0, structsize);
710 vc_cons[currcons].d = (struct vc_data *)p;
711 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
712 visual_init(currcons, 1);
713 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
714 con_set_default_unimap(currcons);
715 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
718 vc_cons[currcons].d = NULL;
719 vt_cons[currcons] = NULL;
722 screenbuf = (unsigned short *) q;
724 vc_init(currcons, video_num_lines, video_num_columns, 1);
727 pm_con = pm_register(PM_SYS_DEV,
735 inline int resize_screen(int currcons, int width, int height)
737 /* Resizes the resolution of the display adapater */
740 if (vcmode != KD_GRAPHICS && sw->con_resize)
741 err = sw->con_resize(vc_cons[currcons].d, width, height);
746 * Change # of rows and columns (0 means unchanged/the size of fg_console)
747 * [this is to be used together with some user program
748 * like resize that changes the hardware videomode]
750 int vc_resize(int currcons, unsigned int cols, unsigned int lines)
752 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
753 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
754 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
755 unsigned short *newscreen;
757 WARN_CONSOLE_UNLOCKED();
759 if (!vc_cons_allocated(currcons))
762 new_cols = (cols ? cols : video_num_columns);
763 new_rows = (lines ? lines : video_num_lines);
764 new_row_size = new_cols << 1;
765 new_screen_size = new_row_size * new_rows;
767 if (new_cols == video_num_columns && new_rows == video_num_lines)
770 newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
774 old_rows = video_num_lines;
775 old_cols = video_num_columns;
776 old_row_size = video_size_row;
777 old_screen_size = screenbuf_size;
779 err = resize_screen(currcons, new_cols, new_rows);
785 video_num_lines = new_rows;
786 video_num_columns = new_cols;
787 video_size_row = new_row_size;
788 screenbuf_size = new_screen_size;
790 rlth = min(old_row_size, new_row_size);
791 rrem = new_row_size - rlth;
793 new_origin = (long) newscreen;
794 new_scr_end = new_origin + new_screen_size;
795 if (new_rows < old_rows)
796 old_origin += (old_rows - new_rows) * old_row_size;
798 update_attr(currcons);
800 while (old_origin < scr_end) {
801 scr_memcpyw((unsigned short *) new_origin, (unsigned short *) old_origin, rlth);
803 scr_memsetw((void *)(new_origin + rlth), video_erase_char, rrem);
804 old_origin += old_row_size;
805 new_origin += new_row_size;
807 if (new_scr_end > new_origin)
808 scr_memsetw((void *) new_origin, video_erase_char, new_scr_end - new_origin);
811 screenbuf = newscreen;
813 screenbuf_size = new_screen_size;
814 set_origin(currcons);
816 /* do part of a reset_terminal() */
818 bottom = video_num_lines;
819 gotoxy(currcons, x, y);
822 if (vc_cons[currcons].d->vc_tty) {
823 struct winsize ws, *cws = &vc_cons[currcons].d->vc_tty->winsize;
825 memset(&ws, 0, sizeof(ws));
826 ws.ws_row = video_num_lines;
827 ws.ws_col = video_num_columns;
828 ws.ws_ypixel = video_scan_lines;
829 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
830 vc_cons[currcons].d->vc_tty->pgrp > 0)
831 kill_pg(vc_cons[currcons].d->vc_tty->pgrp, SIGWINCH, 1);
836 update_screen(currcons);
841 void vc_disallocate(unsigned int currcons)
843 WARN_CONSOLE_UNLOCKED();
845 if (vc_cons_allocated(currcons)) {
846 sw->con_deinit(vc_cons[currcons].d);
849 if (currcons >= MIN_NR_CONSOLES)
850 kfree(vc_cons[currcons].d);
851 vc_cons[currcons].d = NULL;
859 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
860 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
861 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
863 #define decarm VC_REPEAT
864 #define decckm VC_CKMODE
865 #define kbdapplic VC_APPLIC
869 * this is what the terminal answers to a ESC-Z or csi0c query.
871 #define VT100ID "\033[?1;2c"
872 #define VT102ID "\033[?6c"
874 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
875 8,12,10,14, 9,13,11,15 };
877 /* the default colour table, for VGA+ colour systems */
878 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
879 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
880 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
881 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
882 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
883 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
886 * gotoxy() must verify all boundaries, because the arguments
887 * might also be negative. If the given position is out of
888 * bounds, the cursor is placed at the nearest margin.
890 static void gotoxy(int currcons, int new_x, int new_y)
897 if (new_x >= video_num_columns)
898 x = video_num_columns - 1;
906 max_y = video_num_lines;
910 else if (new_y >= max_y)
914 pos = origin + y*video_size_row + (x<<1);
918 /* for absolute user moves, when decom is set */
919 static void gotoxay(int currcons, int new_x, int new_y)
921 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
924 void scrollback(int lines)
926 int currcons = fg_console;
929 lines = video_num_lines/2;
933 void scrollfront(int lines)
935 int currcons = fg_console;
938 lines = video_num_lines/2;
942 static void lf(int currcons)
944 /* don't scroll if above bottom of scrolling region, or
945 * if below scrolling region
948 scrup(currcons,top,bottom,1);
949 else if (y < video_num_lines-1) {
951 pos += video_size_row;
956 static void ri(int currcons)
958 /* don't scroll if below top of scrolling region, or
959 * if above scrolling region
962 scrdown(currcons,top,bottom,1);
965 pos -= video_size_row;
970 static inline void cr(int currcons)
976 static inline void bs(int currcons)
985 static inline void del(int currcons)
990 static void csi_J(int currcons, int vpar)
993 unsigned short * start;
996 case 0: /* erase from cursor to end of display */
997 count = (scr_end-pos)>>1;
998 start = (unsigned short *) pos;
1000 /* do in two stages */
1001 sw->con_clear(vc_cons[currcons].d, y, x, 1,
1002 video_num_columns-x);
1003 sw->con_clear(vc_cons[currcons].d, y+1, 0,
1004 video_num_lines-y-1,
1008 case 1: /* erase from start to cursor */
1009 count = ((pos-origin)>>1)+1;
1010 start = (unsigned short *) origin;
1012 /* do in two stages */
1013 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
1015 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1019 case 2: /* erase whole display */
1020 count = video_num_columns * video_num_lines;
1021 start = (unsigned short *) origin;
1023 sw->con_clear(vc_cons[currcons].d, 0, 0,
1030 scr_memsetw(start, video_erase_char, 2*count);
1034 static void csi_K(int currcons, int vpar)
1037 unsigned short * start;
1040 case 0: /* erase from cursor to end of line */
1041 count = video_num_columns-x;
1042 start = (unsigned short *) pos;
1044 sw->con_clear(vc_cons[currcons].d, y, x, 1,
1045 video_num_columns-x);
1047 case 1: /* erase from start of line to cursor */
1048 start = (unsigned short *) (pos - (x<<1));
1051 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1054 case 2: /* erase whole line */
1055 start = (unsigned short *) (pos - (x<<1));
1056 count = video_num_columns;
1058 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1064 scr_memsetw(start, video_erase_char, 2 * count);
1068 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
1074 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
1076 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
1078 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
1082 static void default_attr(int currcons)
1091 /* console_sem is held */
1092 static void csi_m(int currcons)
1096 for (i=0;i<=npar;i++)
1098 case 0: /* all attributes off */
1099 default_attr(currcons);
1116 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1117 * Select primary font, don't display
1118 * control chars if defined, don't set
1121 translate = set_translate(charset == 0
1123 : G1_charset,currcons);
1127 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1128 * Select first alternate font, lets
1129 * chars < 32 be displayed as ROM chars.
1131 translate = set_translate(IBMPC_MAP,currcons);
1135 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1136 * Select second alternate font, toggle
1137 * high bit before displaying as ROM char.
1139 translate = set_translate(IBMPC_MAP,currcons);
1156 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1157 * Enables underscore, white foreground
1158 * with white underscore (Linux - use
1159 * default foreground).
1161 color = (def_color & 0x0f) | background;
1164 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1165 * Disable underline option.
1166 * Reset colour to default? It did this
1169 color = (def_color & 0x0f) | background;
1173 color = (def_color & 0xf0) | foreground;
1176 if (par[i] >= 30 && par[i] <= 37)
1177 color = color_table[par[i]-30]
1179 else if (par[i] >= 40 && par[i] <= 47)
1180 color = (color_table[par[i]-40]<<4)
1184 update_attr(currcons);
1187 static void respond_string(const char *p, struct tty_struct *tty)
1190 tty_insert_flip_char(tty, *p, 0);
1193 con_schedule_flip(tty);
1196 static void cursor_report(int currcons, struct tty_struct *tty)
1200 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1201 respond_string(buf, tty);
1204 static inline void status_report(struct tty_struct *tty)
1206 respond_string("\033[0n", tty); /* Terminal ok */
1209 static inline void respond_ID(struct tty_struct * tty)
1211 respond_string(VT102ID, tty);
1214 void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1218 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1220 respond_string(buf, tty);
1223 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1224 int mouse_reporting(void)
1226 int currcons = fg_console;
1228 return report_mouse;
1231 /* console_sem is held */
1232 static void set_mode(int currcons, int on_off)
1236 for (i=0; i<=npar; i++)
1237 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1238 case 1: /* Cursor keys send ^[Ox/^[[x */
1244 case 3: /* 80/132 mode switch unimplemented */
1247 (void) vc_resize(deccolm ? 132 : 80, video_num_lines);
1248 /* this alone does not suffice; some user mode
1249 utility has to change the hardware regs */
1252 case 5: /* Inverted screen on/off */
1253 if (decscnm != on_off) {
1255 invert_screen(currcons, 0, screenbuf_size, 0);
1256 update_attr(currcons);
1259 case 6: /* Origin relative/absolute */
1261 gotoxay(currcons,0,0);
1263 case 7: /* Autowrap on/off */
1266 case 8: /* Autorepeat on/off */
1273 report_mouse = on_off ? 1 : 0;
1275 case 25: /* Cursor on/off */
1279 report_mouse = on_off ? 2 : 0;
1281 } else switch(par[i]) { /* ANSI modes set/reset */
1282 case 3: /* Monitor (display ctrls) */
1285 case 4: /* Insert Mode on/off */
1288 case 20: /* Lf, Enter == CrLf/Lf */
1297 /* console_sem is held */
1298 static void setterm_command(int currcons)
1301 case 1: /* set color for underline mode */
1302 if (can_do_color && par[1] < 16) {
1303 ulcolor = color_table[par[1]];
1305 update_attr(currcons);
1308 case 2: /* set color for half intensity mode */
1309 if (can_do_color && par[1] < 16) {
1310 halfcolor = color_table[par[1]];
1312 update_attr(currcons);
1315 case 8: /* store colors as defaults */
1317 if (hi_font_mask == 0x100)
1319 default_attr(currcons);
1320 update_attr(currcons);
1322 case 9: /* set blanking interval */
1323 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1324 poke_blanked_console();
1326 case 10: /* set bell frequency in Hz */
1328 bell_pitch = par[1];
1330 bell_pitch = DEFAULT_BELL_PITCH;
1332 case 11: /* set bell duration in msec */
1334 bell_duration = (par[1] < 2000) ?
1337 bell_duration = DEFAULT_BELL_DURATION;
1339 case 12: /* bring specified console to the front */
1340 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1341 set_console(par[1] - 1);
1343 case 13: /* unblank the screen */
1344 poke_blanked_console();
1346 case 14: /* set vesa powerdown interval */
1347 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1349 case 15: /* activate the previous console */
1350 set_console(last_console);
1355 /* console_sem is held */
1356 static void csi_at(int currcons, unsigned int nr)
1358 if (nr > video_num_columns - x)
1359 nr = video_num_columns - x;
1362 insert_char(currcons, nr);
1365 /* console_sem is held */
1366 static void csi_L(int currcons, unsigned int nr)
1368 if (nr > video_num_lines - y)
1369 nr = video_num_lines - y;
1372 scrdown(currcons,y,bottom,nr);
1376 /* console_sem is held */
1377 static void csi_P(int currcons, unsigned int nr)
1379 if (nr > video_num_columns - x)
1380 nr = video_num_columns - x;
1383 delete_char(currcons, nr);
1386 /* console_sem is held */
1387 static void csi_M(int currcons, unsigned int nr)
1389 if (nr > video_num_lines - y)
1390 nr = video_num_lines - y;
1393 scrup(currcons,y,bottom,nr);
1397 /* console_sem is held (except via vc_init->reset_terminal */
1398 static void save_cur(int currcons)
1402 s_intensity = intensity;
1403 s_underline = underline;
1405 s_reverse = reverse;
1406 s_charset = charset;
1408 saved_G0 = G0_charset;
1409 saved_G1 = G1_charset;
1412 /* console_sem is held */
1413 static void restore_cur(int currcons)
1415 gotoxy(currcons,saved_x,saved_y);
1416 intensity = s_intensity;
1417 underline = s_underline;
1419 reverse = s_reverse;
1420 charset = s_charset;
1422 G0_charset = saved_G0;
1423 G1_charset = saved_G1;
1424 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1425 update_attr(currcons);
1429 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1430 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1433 /* console_sem is held (except via vc_init()) */
1434 static void reset_terminal(int currcons, int do_clear)
1437 bottom = video_num_lines;
1438 vc_state = ESnormal;
1440 translate = set_translate(LAT1_MAP,currcons);
1441 G0_charset = LAT1_MAP;
1442 G1_charset = GRAF_MAP;
1462 kbd_table[currcons].lockstate = 0;
1463 kbd_table[currcons].slockstate = 0;
1464 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1465 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1466 /* do not do set_leds here because this causes an endless tasklet loop
1467 when the keyboard hasn't been initialized yet */
1469 cursor_type = CUR_DEFAULT;
1470 complement_mask = s_complement_mask;
1472 default_attr(currcons);
1473 update_attr(currcons);
1475 tab_stop[0] = 0x01010100;
1479 tab_stop[4] = 0x01010101;
1481 bell_pitch = DEFAULT_BELL_PITCH;
1482 bell_duration = DEFAULT_BELL_DURATION;
1484 gotoxy(currcons,0,0);
1490 /* console_sem is held */
1491 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1494 * Control characters can be used in the _middle_
1495 * of an escape sequence.
1502 kd_mksound(bell_pitch, bell_duration);
1509 while (x < video_num_columns - 1) {
1511 if (tab_stop[x >> 5] & (1 << (x & 31)))
1516 case 10: case 11: case 12:
1525 translate = set_translate(G1_charset,currcons);
1530 translate = set_translate(G0_charset,currcons);
1534 vc_state = ESnormal;
1543 vc_state = ESsquare;
1548 vc_state = ESnormal;
1551 vc_state = ESsquare;
1554 vc_state = ESnonstd;
1557 vc_state = ESpercent;
1570 tab_stop[x >> 5] |= (1 << (x & 31));
1579 restore_cur(currcons);
1591 reset_terminal(currcons,1);
1593 case '>': /* Numeric keypad */
1596 case '=': /* Appl. keypad */
1602 if (c=='P') { /* palette escape sequence */
1603 for (npar=0; npar<NPAR; npar++)
1606 vc_state = ESpalette;
1608 } else if (c=='R') { /* reset palette */
1609 reset_palette(currcons);
1610 vc_state = ESnormal;
1612 vc_state = ESnormal;
1615 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1616 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1618 int i = par[0]*3, j = 1;
1619 palette[i] = 16*par[j++];
1620 palette[i++] += par[j++];
1621 palette[i] = 16*par[j++];
1622 palette[i++] += par[j++];
1623 palette[i] = 16*par[j++];
1624 palette[i] += par[j];
1625 set_palette(currcons);
1626 vc_state = ESnormal;
1629 vc_state = ESnormal;
1632 for(npar = 0 ; npar < NPAR ; npar++)
1635 vc_state = ESgetpars;
1636 if (c == '[') { /* Function key */
1644 if (c==';' && npar<NPAR-1) {
1647 } else if (c>='0' && c<='9') {
1651 } else vc_state=ESgotpars;
1653 vc_state = ESnormal;
1656 set_mode(currcons,1);
1659 set_mode(currcons,0);
1664 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1666 cursor_type = CUR_DEFAULT;
1674 complement_mask = par[0]<<8 | par[1];
1676 complement_mask = s_complement_mask;
1684 else if (par[0] == 6)
1685 cursor_report(currcons,tty);
1695 if (par[0]) par[0]--;
1696 gotoxy(currcons,par[0],y);
1699 if (!par[0]) par[0]++;
1700 gotoxy(currcons,x,y-par[0]);
1703 if (!par[0]) par[0]++;
1704 gotoxy(currcons,x,y+par[0]);
1707 if (!par[0]) par[0]++;
1708 gotoxy(currcons,x+par[0],y);
1711 if (!par[0]) par[0]++;
1712 gotoxy(currcons,x-par[0],y);
1715 if (!par[0]) par[0]++;
1716 gotoxy(currcons,0,y+par[0]);
1719 if (!par[0]) par[0]++;
1720 gotoxy(currcons,0,y-par[0]);
1723 if (par[0]) par[0]--;
1724 gotoxay(currcons,x,par[0]);
1727 if (par[0]) par[0]--;
1728 if (par[1]) par[1]--;
1729 gotoxay(currcons,par[1],par[0]);
1732 csi_J(currcons,par[0]);
1735 csi_K(currcons,par[0]);
1738 csi_L(currcons,par[0]);
1741 csi_M(currcons,par[0]);
1744 csi_P(currcons,par[0]);
1752 tab_stop[x >> 5] &= ~(1 << (x & 31));
1753 else if (par[0] == 3) {
1764 case 'q': /* DECLL - but only 3 leds */
1765 /* map 0,1,2,3 to 0,1,2,4 */
1767 setledstate(kbd_table + currcons,
1768 (par[0] < 3) ? par[0] : 4);
1774 par[1] = video_num_lines;
1775 /* Minimum allowed region is 2 lines */
1776 if (par[0] < par[1] &&
1777 par[1] <= video_num_lines) {
1780 gotoxay(currcons,0,0);
1787 restore_cur(currcons);
1790 csi_X(currcons, par[0]);
1793 csi_at(currcons,par[0]);
1795 case ']': /* setterm functions */
1796 setterm_command(currcons);
1801 vc_state = ESnormal;
1803 case '@': /* defined in ISO 2022 */
1806 case 'G': /* prelim official escape code */
1807 case '8': /* retained for compatibility */
1813 vc_state = ESnormal;
1816 vc_state = ESnormal;
1818 /* DEC screen alignment test. kludge :-) */
1820 (video_erase_char & 0xff00) | 'E';
1823 (video_erase_char & 0xff00) | ' ';
1824 do_update_region(currcons, origin, screenbuf_size/2);
1829 G0_charset = GRAF_MAP;
1831 G0_charset = LAT1_MAP;
1833 G0_charset = IBMPC_MAP;
1835 G0_charset = USER_MAP;
1837 translate = set_translate(G0_charset,currcons);
1838 vc_state = ESnormal;
1842 G1_charset = GRAF_MAP;
1844 G1_charset = LAT1_MAP;
1846 G1_charset = IBMPC_MAP;
1848 G1_charset = USER_MAP;
1850 translate = set_translate(G1_charset,currcons);
1851 vc_state = ESnormal;
1854 vc_state = ESnormal;
1858 /* This is a temporary buffer used to prepare a tty console write
1859 * so that we can easily avoid touching user space while holding the
1860 * console spinlock. It is allocated in con_init and is shared by
1861 * this code and the vc_screen read/write tty calls.
1863 * We have to allocate this statically in the kernel data section
1864 * since console_init (and thus con_init) are called before any
1865 * kernel memory allocation is available.
1867 char con_buf[PAGE_SIZE];
1868 #define CON_BUF_SIZE PAGE_SIZE
1869 DECLARE_MUTEX(con_buf_sem);
1871 /* acquires console_sem */
1872 static int do_con_write(struct tty_struct *tty, int from_user,
1873 const unsigned char *buf, int count)
1875 #ifdef VT_BUF_VRAM_ONLY
1876 #define FLUSH do { } while(0);
1878 #define FLUSH if (draw_x >= 0) { \
1879 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1884 int c, tc, ok, n = 0, draw_x = -1;
1885 unsigned int currcons;
1886 unsigned long draw_from = 0, draw_to = 0;
1887 struct vt_struct *vt;
1888 u16 himask, charmask;
1889 const unsigned char *orig_buf = NULL;
1897 acquire_console_sem();
1898 vt = tty->driver_data;
1900 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
1901 release_console_sem();
1905 currcons = vt->vc_num;
1906 if (!vc_cons_allocated(currcons)) {
1907 /* could this happen? */
1908 static int error = 0;
1911 printk("con_write: tty %d not allocated\n", currcons+1);
1913 release_console_sem();
1916 release_console_sem();
1926 if (count > CON_BUF_SIZE)
1927 count = CON_BUF_SIZE;
1928 console_conditional_schedule();
1929 if (copy_from_user(con_buf, buf, count)) {
1930 n = 0; /* ?? are error codes legal here ?? */
1937 /* At this point 'buf' is guaranteed to be a kernel buffer
1938 * and therefore no access to userspace (and therefore sleeping)
1939 * will be needed. The con_buf_sem serializes all tty based
1940 * console rendering and vcs write/read operations. We hold
1941 * the console spinlock during the entire write.
1944 acquire_console_sem();
1946 vt = tty->driver_data;
1948 printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
1949 release_console_sem();
1953 himask = hi_font_mask;
1954 charmask = himask ? 0x1ff : 0xff;
1956 /* undraw cursor first */
1958 hide_cursor(currcons);
1960 while (!tty->stopped && count) {
1967 /* Combine UTF-8 into Unicode */
1968 /* Incomplete characters silently ignored */
1970 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1971 utf_char = (utf_char << 6) | (c & 0x3f);
1977 if ((c & 0xe0) == 0xc0) {
1979 utf_char = (c & 0x1f);
1980 } else if ((c & 0xf0) == 0xe0) {
1982 utf_char = (c & 0x0f);
1983 } else if ((c & 0xf8) == 0xf0) {
1985 utf_char = (c & 0x07);
1986 } else if ((c & 0xfc) == 0xf8) {
1988 utf_char = (c & 0x03);
1989 } else if ((c & 0xfe) == 0xfc) {
1991 utf_char = (c & 0x01);
2000 } else { /* no utf */
2001 tc = translate[toggle_meta ? (c|0x80) : c];
2004 /* If the original code was a control character we
2005 * only allow a glyph to be displayed if the code is
2006 * not normally used (such as for cursor movement) or
2007 * if the disp_ctrl mode has been explicitly enabled.
2008 * Certain characters (as given by the CTRL_ALWAYS
2009 * bitmap) are always displayed as control characters,
2010 * as the console would be pretty useless without
2011 * them; to display an arbitrary font position use the
2012 * direct-to-font zone in UTF-8 mode.
2014 ok = tc && (c >= 32 ||
2015 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
2016 : CTRL_ACTION) >> c) & 1)))
2017 && (c != 127 || disp_ctrl)
2020 if (vc_state == ESnormal && ok) {
2021 /* Now try to find out how to display it */
2022 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
2024 /* If we got -4 (not found) then see if we have
2025 defined a replacement character (U+FFFD) */
2026 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
2028 /* One reason for the -4 can be that we just
2029 did a clear_unimap();
2030 try at least to show something. */
2033 } else if ( tc == -3 ) {
2034 /* Bad hash table -- hope for the best */
2038 continue; /* Conversion failed */
2040 if (need_wrap || decim)
2047 insert_char(currcons, 1);
2049 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2052 if (DO_UPDATE && draw_x < 0) {
2056 if (x == video_num_columns - 1) {
2066 do_con_trol(tty, currcons, c);
2069 console_conditional_schedule();
2070 release_console_sem();
2074 /* If the user requested something larger than
2075 * the CON_BUF_SIZE, and the tty is not stopped,
2078 if ((orig_count > CON_BUF_SIZE) && !tty->stopped) {
2079 orig_count -= CON_BUF_SIZE;
2080 orig_buf += CON_BUF_SIZE;
2094 * This is the console switching callback.
2096 * Doing console switching in a process context allows
2097 * us to do the switches asynchronously (needed when we want
2098 * to switch due to a keyboard interrupt). Synchronization
2099 * with other console code and prevention of re-entrancy is
2100 * ensured with console_sem.
2102 static void console_callback(void *ignored)
2104 acquire_console_sem();
2106 if (want_console >= 0) {
2107 if (want_console != fg_console &&
2108 vc_cons_allocated(want_console)) {
2109 hide_cursor(fg_console);
2110 change_console(want_console);
2111 /* we only changed when the console had already
2112 been allocated - a new console is not created
2113 in an interrupt routine */
2117 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2118 do_poke_blanked_console = 0;
2119 poke_blanked_console();
2121 if (scrollback_delta) {
2122 int currcons = fg_console;
2124 if (vcmode == KD_TEXT)
2125 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
2126 scrollback_delta = 0;
2128 if (blank_timer_expired) {
2130 blank_timer_expired = 0;
2133 release_console_sem();
2136 void set_console(int nr)
2139 schedule_console_callback();
2142 struct tty_driver *console_driver;
2144 #ifdef CONFIG_VT_CONSOLE
2147 * Console on virtual terminal
2149 * The console must be locked when we get here.
2152 void vt_console_print(struct console *co, const char *b, unsigned count)
2154 int currcons = fg_console;
2156 static unsigned long printing;
2157 const ushort *start;
2161 /* console busy or not yet initialized */
2162 if (!printable || test_and_set_bit(0, &printing))
2167 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2168 currcons = kmsg_redirect - 1;
2170 /* read `x' only after setting currcons properly (otherwise
2171 the `x' macro will read the x of the foreground console). */
2174 if (!vc_cons_allocated(currcons)) {
2176 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2180 if (vcmode != KD_TEXT)
2183 /* undraw cursor first */
2185 hide_cursor(currcons);
2187 start = (ushort *)pos;
2189 /* Contrived structure to try to emulate original need_wrap behaviour
2190 * Problems caused when we have need_wrap set on '\n' character */
2193 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2196 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2202 if (c == 8) { /* backspace */
2204 start = (ushort *)pos;
2211 start = (ushort *)pos;
2213 if (c == 10 || c == 13)
2216 scr_writew((attr << 8) + c, (unsigned short *) pos);
2218 if (myx == video_num_columns - 1) {
2227 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2229 if (x == video_num_columns) {
2234 set_cursor(currcons);
2236 if (!oops_in_progress)
2237 poke_blanked_console();
2240 clear_bit(0, &printing);
2243 static struct tty_driver *vt_console_device(struct console *c, int *index)
2245 *index = c->index ? c->index-1 : fg_console;
2246 return console_driver;
2249 struct console vt_console_driver = {
2251 .write = vt_console_print,
2252 .device = vt_console_device,
2253 .unblank = unblank_screen,
2254 .flags = CON_PRINTBUFFER,
2260 * Handling of Linux-specific VC ioctls
2264 * Generally a bit racy with respect to console_sem().
2266 * There are some functions which don't need it.
2268 * There are some functions which can sleep for arbitrary periods
2269 * (paste_selection) but we don't need the lock there anyway.
2271 * set_selection has locking, and definitely needs it
2274 int tioclinux(struct tty_struct *tty, unsigned long arg)
2277 char __user *p = (char __user *)arg;
2281 if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
2283 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2285 if (get_user(type, p))
2291 acquire_console_sem();
2292 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2293 release_console_sem();
2295 case TIOCL_PASTESEL:
2296 ret = paste_selection(tty);
2298 case TIOCL_UNBLANKSCREEN:
2301 case TIOCL_SELLOADLUT:
2302 ret = sel_loadlut(p);
2304 case TIOCL_GETSHIFTSTATE:
2307 * Make it possible to react to Shift+Mousebutton.
2308 * Note that 'shift_state' is an undocumented
2309 * kernel-internal variable; programs not closely
2310 * related to the kernel should not use this.
2313 ret = __put_user(data, p);
2315 case TIOCL_GETMOUSEREPORTING:
2316 data = mouse_reporting();
2317 ret = __put_user(data, p);
2319 case TIOCL_SETVESABLANK:
2320 set_vesa_blanking(p);
2322 case TIOCL_SETKMSGREDIRECT:
2323 if (!capable(CAP_SYS_ADMIN)) {
2326 if (get_user(data, p+1))
2329 kmsg_redirect = data;
2332 case TIOCL_GETFGCONSOLE:
2335 case TIOCL_SCROLLCONSOLE:
2336 if (get_user(lines, (s32 __user *)(p+4))) {
2343 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2347 case TIOCL_BLANKEDSCREEN:
2348 ret = console_blanked;
2358 * /dev/ttyN handling
2361 static int con_write(struct tty_struct *tty, int from_user,
2362 const unsigned char *buf, int count)
2367 retval = do_con_write(tty, from_user, buf, count);
2368 con_flush_chars(tty);
2373 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2376 return; /* n_r3964 calls put_char() from interrupt context */
2378 do_con_write(tty, 0, &ch, 1);
2381 static int con_write_room(struct tty_struct *tty)
2385 return 4096; /* No limit, really; we're not buffering */
2388 static int con_chars_in_buffer(struct tty_struct *tty)
2390 return 0; /* we're not buffering */
2394 * con_throttle and con_unthrottle are only used for
2395 * paste_selection(), which has to stuff in a large number of
2398 static void con_throttle(struct tty_struct *tty)
2402 static void con_unthrottle(struct tty_struct *tty)
2404 struct vt_struct *vt = tty->driver_data;
2406 wake_up_interruptible(&vt->paste_wait);
2410 * Turn the Scroll-Lock LED on when the tty is stopped
2412 static void con_stop(struct tty_struct *tty)
2417 console_num = tty->index;
2418 if (!vc_cons_allocated(console_num))
2420 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2425 * Turn the Scroll-Lock LED off when the console is started
2427 static void con_start(struct tty_struct *tty)
2432 console_num = tty->index;
2433 if (!vc_cons_allocated(console_num))
2435 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2439 static void con_flush_chars(struct tty_struct *tty)
2441 struct vt_struct *vt;
2443 if (in_interrupt()) /* from flush_to_ldisc */
2448 /* if we race with con_close(), vt may be null */
2449 acquire_console_sem();
2450 vt = tty->driver_data;
2452 set_cursor(vt->vc_num);
2453 release_console_sem();
2457 * Allocate the console screen memory.
2459 static int con_open(struct tty_struct *tty, struct file *filp)
2461 unsigned int currcons = tty->index;
2464 acquire_console_sem();
2465 if (tty->count == 1) {
2466 ret = vc_allocate(currcons);
2468 vt_cons[currcons]->vc_num = currcons;
2469 tty->driver_data = vt_cons[currcons];
2470 vc_cons[currcons].d->vc_tty = tty;
2472 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2473 tty->winsize.ws_row = video_num_lines;
2474 tty->winsize.ws_col = video_num_columns;
2476 release_console_sem();
2477 vcs_make_devfs(tty);
2481 release_console_sem();
2486 * We take tty_sem in here to prevent another thread from coming in via init_dev
2487 * and taking a ref against the tty while we're in the process of forgetting
2488 * about it and cleaning things up.
2490 * This is because vcs_remove_devfs() can sleep and will drop the BKL.
2492 static void con_close(struct tty_struct *tty, struct file *filp)
2495 acquire_console_sem();
2496 if (tty && tty->count == 1) {
2497 struct vt_struct *vt;
2499 vt = tty->driver_data;
2501 vc_cons[vt->vc_num].d->vc_tty = NULL;
2502 tty->driver_data = 0;
2503 release_console_sem();
2504 vcs_remove_devfs(tty);
2507 * tty_sem is released, but we still hold BKL, so there is
2508 * still exclusion against init_dev()
2512 release_console_sem();
2516 static void vc_init(unsigned int currcons, unsigned int rows,
2517 unsigned int cols, int do_clear)
2521 video_num_columns = cols;
2522 video_num_lines = rows;
2523 video_size_row = cols<<1;
2524 screenbuf_size = video_num_lines * video_size_row;
2526 set_origin(currcons);
2529 for (j=k=0; j<16; j++) {
2530 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2531 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2532 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2534 def_color = 0x07; /* white */
2535 ulcolor = 0x0f; /* bold white */
2536 halfcolor = 0x08; /* grey */
2537 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2538 reset_terminal(currcons, do_clear);
2542 * This routine initializes console interrupts, and does nothing
2543 * else. If you want the screen to clear, call tty_write with
2544 * the appropriate escape-sequence.
2547 static int __init con_init(void)
2549 const char *display_desc = NULL;
2550 unsigned int currcons = 0;
2552 acquire_console_sem();
2555 display_desc = conswitchp->con_startup();
2556 if (!display_desc) {
2558 release_console_sem();
2562 init_timer(&console_timer);
2563 console_timer.function = blank_screen_t;
2564 if (blankinterval) {
2565 blank_state = blank_normal_wait;
2566 mod_timer(&console_timer, jiffies + blankinterval);
2570 * kmalloc is not running yet - we use the bootmem allocator.
2572 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2573 vc_cons[currcons].d = (struct vc_data *)
2574 alloc_bootmem(sizeof(struct vc_data));
2575 vt_cons[currcons] = (struct vt_struct *)
2576 alloc_bootmem(sizeof(struct vt_struct));
2577 visual_init(currcons, 1);
2578 screenbuf = (unsigned short *) alloc_bootmem(screenbuf_size);
2580 vc_init(currcons, video_num_lines, video_num_columns,
2581 currcons || !sw->con_save_screen);
2583 currcons = fg_console = 0;
2584 master_display_fg = vc_cons[currcons].d;
2585 set_origin(currcons);
2586 save_screen(currcons);
2587 gotoxy(currcons,x,y);
2589 update_screen(fg_console);
2590 printk("Console: %s %s %dx%d",
2591 can_do_color ? "colour" : "mono",
2592 display_desc, video_num_columns, video_num_lines);
2596 release_console_sem();
2598 #ifdef CONFIG_VT_CONSOLE
2599 register_console(&vt_console_driver);
2603 console_initcall(con_init);
2605 static struct tty_operations con_ops = {
2609 .write_room = con_write_room,
2610 .put_char = con_put_char,
2611 .flush_chars = con_flush_chars,
2612 .chars_in_buffer = con_chars_in_buffer,
2616 .throttle = con_throttle,
2617 .unthrottle = con_unthrottle,
2620 int __init vty_init(void)
2624 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2625 if (!console_driver)
2626 panic("Couldn't allocate console driver\n");
2627 console_driver->owner = THIS_MODULE;
2628 console_driver->devfs_name = "vc/";
2629 console_driver->name = "tty";
2630 console_driver->name_base = 1;
2631 console_driver->major = TTY_MAJOR;
2632 console_driver->minor_start = 1;
2633 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2634 console_driver->init_termios = tty_std_termios;
2635 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2636 tty_set_operations(console_driver, &con_ops);
2637 if (tty_register_driver(console_driver))
2638 panic("Couldn't register console driver\n");
2642 #ifdef CONFIG_PROM_CONSOLE
2645 #ifdef CONFIG_MDA_CONSOLE
2648 #ifdef CONFIG_FRAMEBUFFER_CONSOLE
2654 #ifndef VT_SINGLE_DRIVER
2656 static void clear_buffer_attributes(int currcons)
2658 unsigned short *p = (unsigned short *) origin;
2659 int count = screenbuf_size/2;
2660 int mask = hi_font_mask | 0xff;
2662 for (; count > 0; count--, p++) {
2663 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2668 * If we support more console drivers, this function is used
2669 * when a driver wants to take over some existing consoles
2670 * and become default driver for newly opened ones.
2673 int take_over_console(const struct consw *csw, int first, int last, int deflt)
2677 struct module *owner;
2680 if (!try_module_get(owner))
2683 acquire_console_sem();
2685 desc = csw->con_startup();
2687 release_console_sem();
2693 module_put(conswitchp->owner);
2694 __module_get(owner);
2698 for (i = first; i <= last; i++) {
2702 if (con_driver_map[i])
2703 module_put(con_driver_map[i]->owner);
2704 __module_get(owner);
2705 con_driver_map[i] = csw;
2707 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2713 old_was_color = vc_cons[i].d->vc_can_do_color;
2714 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2718 /* If the console changed between mono <-> color, then
2719 * the attributes in the screenbuf will be wrong. The
2720 * following resets all attributes to something sane.
2722 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2723 clear_buffer_attributes(i);
2728 printk("Console: switching ");
2730 printk("consoles %d-%d ", first+1, last+1);
2732 printk("to %s %s %dx%d\n",
2733 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2734 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2736 printk("to %s\n", desc);
2738 release_console_sem();
2744 void give_up_console(const struct consw *csw)
2748 for(i = 0; i < MAX_NR_CONSOLES; i++)
2749 if (con_driver_map[i] == csw) {
2750 module_put(csw->owner);
2751 con_driver_map[i] = NULL;
2761 static void set_vesa_blanking(char __user *p)
2764 get_user(mode, p + 1);
2765 vesa_blank_mode = (mode < 4) ? mode : 0;
2769 * This is called by a timer handler
2771 static void vesa_powerdown(void)
2773 struct vc_data *c = vc_cons[fg_console].d;
2775 * Power down if currently suspended (1 or 2),
2776 * suspend if currently blanked (0),
2777 * else do nothing (i.e. already powered down (3)).
2778 * Called only if powerdown features are allowed.
2780 switch (vesa_blank_mode) {
2781 case VESA_NO_BLANKING:
2782 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1, 0);
2784 case VESA_VSYNC_SUSPEND:
2785 case VESA_HSYNC_SUSPEND:
2786 c->vc_sw->con_blank(c, VESA_POWERDOWN+1, 0);
2791 void do_blank_screen(int entering_gfx)
2793 int currcons = fg_console;
2796 WARN_CONSOLE_UNLOCKED();
2798 if (console_blanked) {
2799 if (blank_state == blank_vesa_wait) {
2800 blank_state = blank_off;
2806 if (blank_state != blank_normal_wait)
2808 blank_state = blank_off;
2810 /* entering graphics mode? */
2812 hide_cursor(currcons);
2813 save_screen(currcons);
2814 sw->con_blank(vc_cons[currcons].d, -1, 1);
2815 console_blanked = fg_console + 1;
2816 set_origin(currcons);
2820 /* don't blank graphics */
2821 if (vcmode != KD_TEXT) {
2822 console_blanked = fg_console + 1;
2826 hide_cursor(currcons);
2827 del_timer_sync(&console_timer);
2828 blank_timer_expired = 0;
2830 save_screen(currcons);
2831 /* In case we need to reset origin, blanking hook returns 1 */
2832 i = sw->con_blank(vc_cons[currcons].d, 1, 0);
2833 console_blanked = fg_console + 1;
2835 set_origin(currcons);
2837 if (console_blank_hook && console_blank_hook(1))
2840 if (vesa_off_interval) {
2841 blank_state = blank_vesa_wait,
2842 mod_timer(&console_timer, jiffies + vesa_off_interval);
2845 if (vesa_blank_mode)
2846 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1, 0);
2851 * Called by timer as well as from vt_console_driver
2853 void do_unblank_screen(int leaving_gfx)
2857 WARN_CONSOLE_UNLOCKED();
2860 if (!console_blanked)
2862 if (!vc_cons_allocated(fg_console)) {
2864 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2867 currcons = fg_console;
2868 if (vcmode != KD_TEXT)
2869 return; /* but leave console_blanked != 0 */
2871 if (blankinterval) {
2872 mod_timer(&console_timer, jiffies + blankinterval);
2873 blank_state = blank_normal_wait;
2876 console_blanked = 0;
2877 if (sw->con_blank(vc_cons[currcons].d, 0, leaving_gfx))
2878 /* Low-level driver cannot restore -> do it ourselves */
2879 update_screen(fg_console);
2880 if (console_blank_hook)
2881 console_blank_hook(0);
2882 set_palette(currcons);
2883 set_cursor(fg_console);
2887 * This is called by the outside world to cause a forced unblank, mostly for
2888 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
2889 * call it with 1 as an argument and so force a mode restore... that may kill
2890 * X or at least garbage the screen but would also make the Oops visible...
2892 void unblank_screen(void)
2894 do_unblank_screen(0);
2898 * We defer the timer blanking to work queue so it can take the console semaphore
2899 * (console operations can still happen at irq time, but only from printk which
2900 * has the console semaphore. Not perfect yet, but better than no locking
2902 static void blank_screen_t(unsigned long dummy)
2904 blank_timer_expired = 1;
2905 schedule_work(&console_work);
2908 void poke_blanked_console(void)
2910 WARN_CONSOLE_UNLOCKED();
2912 /* This isn't perfectly race free, but a race here would be mostly harmless,
2913 * at worse, we'll do a spurrious blank and it's unlikely
2915 del_timer(&console_timer);
2916 blank_timer_expired = 0;
2918 if (ignore_poke || !vt_cons[fg_console] || vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2920 if (console_blanked)
2922 else if (blankinterval) {
2923 mod_timer(&console_timer, jiffies + blankinterval);
2924 blank_state = blank_normal_wait;
2932 void set_palette(int currcons)
2934 WARN_CONSOLE_UNLOCKED();
2936 if (vcmode != KD_GRAPHICS)
2937 sw->con_set_palette(vc_cons[currcons].d, color_table);
2940 static int set_get_cmap(unsigned char __user *arg, int set)
2944 WARN_CONSOLE_UNLOCKED();
2946 for (i = 0; i < 16; i++)
2948 get_user(default_red[i], arg++);
2949 get_user(default_grn[i], arg++);
2950 get_user(default_blu[i], arg++);
2952 put_user(default_red[i], arg++);
2953 put_user(default_grn[i], arg++);
2954 put_user(default_blu[i], arg++);
2957 for (i = 0; i < MAX_NR_CONSOLES; i++)
2958 if (vc_cons_allocated(i)) {
2959 for (j = k = 0; j < 16; j++) {
2960 vc_cons[i].d->vc_palette[k++] = default_red[j];
2961 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2962 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2971 * Load palette into the DAC registers. arg points to a colour
2972 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2975 int con_set_cmap(unsigned char __user *arg)
2979 acquire_console_sem();
2980 rc = set_get_cmap (arg,1);
2981 release_console_sem();
2986 int con_get_cmap(unsigned char __user *arg)
2990 acquire_console_sem();
2991 rc = set_get_cmap (arg,0);
2992 release_console_sem();
2997 void reset_palette(int currcons)
3000 for (j=k=0; j<16; j++) {
3001 palette[k++] = default_red[j];
3002 palette[k++] = default_grn[j];
3003 palette[k++] = default_blu[j];
3005 set_palette(currcons);
3011 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3012 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3013 * depending on width) reserved for each character which is kinda wasty, but
3014 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3015 * is upto the actual low-level console-driver convert data into its favorite
3016 * format (maybe we should add a `fontoffset' field to the `display'
3017 * structure so we won't have to convert the fontdata all the time.
3021 #define max_font_size 65536
3023 int con_font_op(int currcons, struct console_font_op *op)
3026 int size = max_font_size, set;
3028 struct console_font_op old_op;
3030 if (vt_cons[currcons]->vc_mode != KD_TEXT)
3032 memcpy(&old_op, op, sizeof(old_op));
3033 if (op->op == KD_FONT_OP_SET) {
3036 if (op->charcount > 512)
3038 if (!op->height) { /* Need to guess font height [compat] */
3040 u8 __user *charmap = op->data;
3043 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
3044 so that we can get rid of this soon */
3045 if (!(op->flags & KD_FONT_FLAG_OLD))
3048 for (h = 32; h > 0; h--)
3049 for (i = 0; i < op->charcount; i++) {
3050 if (get_user(tmp, &charmap[32*i+h-1]))
3061 if (op->width > 32 || op->height > 32)
3063 size = (op->width+7)/8 * 32 * op->charcount;
3064 if (size > max_font_size)
3067 } else if (op->op == KD_FONT_OP_GET)
3070 acquire_console_sem();
3071 rc = sw->con_font_op(vc_cons[currcons].d, op);
3072 release_console_sem();
3076 temp = kmalloc(size, GFP_KERNEL);
3079 if (set && copy_from_user(temp, op->data, size)) {
3086 acquire_console_sem();
3087 rc = sw->con_font_op(vc_cons[currcons].d, op);
3088 release_console_sem();
3090 op->data = old_op.data;
3092 int c = (op->width+7)/8 * 32 * op->charcount;
3094 if (op->data && op->charcount > old_op.charcount)
3096 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3097 if (op->width > old_op.width ||
3098 op->height > old_op.height)
3103 else if ((old_op.height && op->height > old_op.height) ||
3107 if (!rc && op->data && copy_to_user(op->data, temp, c))
3116 * Interface exported to selection and vcs.
3119 /* used by selection */
3120 u16 screen_glyph(int currcons, int offset)
3122 u16 w = scr_readw(screenpos(currcons, offset, 1));
3125 if (w & hi_font_mask)
3130 /* used by vcs - note the word offset */
3131 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
3133 return screenpos(currcons, 2 * w_offset, viewed);
3136 void getconsxy(int currcons, unsigned char *p)
3142 void putconsxy(int currcons, unsigned char *p)
3144 gotoxy(currcons, p[0], p[1]);
3145 set_cursor(currcons);
3148 u16 vcs_scr_readw(int currcons, const u16 *org)
3150 if ((unsigned long)org == pos && softcursor_original != -1)
3151 return softcursor_original;
3152 return scr_readw(org);
3155 void vcs_scr_writew(int currcons, u16 val, u16 *org)
3157 scr_writew(val, org);
3158 if ((unsigned long)org == pos) {
3159 softcursor_original = -1;
3160 add_softcursor(currcons);
3164 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data)
3169 acquire_console_sem();
3171 release_console_sem();
3174 acquire_console_sem();
3176 release_console_sem();
3183 * Visible symbols for modules
3186 EXPORT_SYMBOL(color_table);
3187 EXPORT_SYMBOL(default_red);
3188 EXPORT_SYMBOL(default_grn);
3189 EXPORT_SYMBOL(default_blu);
3190 EXPORT_SYMBOL(vc_cons_allocated);
3191 EXPORT_SYMBOL(update_region);
3192 EXPORT_SYMBOL(redraw_screen);
3193 EXPORT_SYMBOL(vc_resize);
3194 EXPORT_SYMBOL(fg_console);
3195 EXPORT_SYMBOL(console_blank_hook);
3196 EXPORT_SYMBOL(console_blanked);
3197 EXPORT_SYMBOL(vt_cons);
3198 EXPORT_SYMBOL(vc_cons);
3199 #ifndef VT_SINGLE_DRIVER
3200 EXPORT_SYMBOL(take_over_console);
3201 EXPORT_SYMBOL(give_up_console);