2 * linux/drivers/char/console.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/sched.h>
79 #include <linux/tty.h>
80 #include <linux/tty_flip.h>
81 #include <linux/kernel.h>
82 #include <linux/string.h>
83 #include <linux/errno.h>
85 #include <linux/slab.h>
86 #include <linux/major.h>
88 #include <linux/console.h>
89 #include <linux/init.h>
90 #include <linux/devfs_fs_kernel.h>
91 #include <linux/vt_kern.h>
92 #include <linux/selection.h>
93 #include <linux/console_struct.h>
94 #include <linux/kbd_kern.h>
95 #include <linux/consolemap.h>
96 #include <linux/timer.h>
97 #include <linux/interrupt.h>
98 #include <linux/config.h>
99 #include <linux/version.h>
100 #include <linux/tqueue.h>
101 #include <linux/bootmem.h>
102 #include <linux/pm.h>
103 #include <linux/smp_lock.h>
106 #include <asm/system.h>
107 #include <asm/uaccess.h>
108 #include <asm/bitops.h>
110 #include "console_macros.h"
113 const struct consw *conswitchp;
115 /* A bitmap for codes <32. A bit of 1 indicates that the code
116 * corresponding to that bit number invokes some special action
117 * (such as cursor movement) and should not be displayed as a
118 * glyph unless the disp_ctrl mode is explicitly enabled.
120 #define CTRL_ACTION 0x0d00ff81
121 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
124 * Here is the default bell parameters: 750HZ, 1/8th of a second
126 #define DEFAULT_BELL_PITCH 750
127 #define DEFAULT_BELL_DURATION (HZ/8)
129 extern void vcs_make_devfs (unsigned int index, int unregister);
132 #define MIN(a,b) ((a) < (b) ? (a) : (b))
135 static struct tty_struct *console_table[MAX_NR_CONSOLES];
136 static struct termios *console_termios[MAX_NR_CONSOLES];
137 static struct termios *console_termios_locked[MAX_NR_CONSOLES];
138 struct vc vc_cons [MAX_NR_CONSOLES];
140 #ifndef VT_SINGLE_DRIVER
141 static const struct consw *con_driver_map[MAX_NR_CONSOLES];
144 static int con_open(struct tty_struct *, struct file *);
145 static void vc_init(unsigned int console, unsigned int rows,
146 unsigned int cols, int do_clear);
147 static void blank_screen(unsigned long dummy);
148 static void gotoxy(int currcons, int new_x, int new_y);
149 static void save_cur(int currcons);
150 static void reset_terminal(int currcons, int do_clear);
151 static void con_flush_chars(struct tty_struct *tty);
152 static void set_vesa_blanking(unsigned long arg);
153 static void set_cursor(int currcons);
154 static void hide_cursor(int currcons);
155 static void unblank_screen_t(unsigned long dummy);
156 static void console_callback(void *ignored);
158 static int printable; /* Is console ready for printing? */
160 int do_poke_blanked_console;
163 static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
164 static int blankinterval = 10*60*HZ;
165 static int vesa_off_interval;
167 static struct tq_struct console_callback_tq = {
168 routine: console_callback,
172 * fg_console is the current virtual console,
173 * last_console is the last used one,
174 * want_console is the console we want to switch to,
175 * kmsg_redirect is the console for kernel messages,
179 int want_console = -1;
183 * For each existing display, we have a pointer to console currently visible
184 * on that display, allowing consoles other than fg_console to be refreshed
185 * appropriately. Unless the low-level driver supplies its own display_fg
186 * variable, we use this one for the "master display".
188 static struct vc_data *master_display_fg;
191 * Unfortunately, we need to delay tty echo when we're currently writing to the
192 * console since the code is (and always was) not re-entrant, so we schedule
193 * all flip requests to process context with schedule-task() and run it from
194 * console_callback().
198 * For the same reason, we defer scrollback to the console callback.
200 static int scrollback_delta;
203 * Hook so that the power management routines can (un)blank
204 * the console on our behalf.
206 int (*console_blank_hook)(int);
208 static struct timer_list console_timer;
211 * Low-Level Functions
214 #define IS_FG (currcons == fg_console)
215 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
217 #ifdef VT_BUF_VRAM_ONLY
220 #define DO_UPDATE IS_VISIBLE
223 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data);
224 static struct pm_dev *pm_con;
226 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
231 p = (unsigned short *)(origin + offset);
232 else if (!sw->con_screen_pos)
233 p = (unsigned short *)(visible_origin + offset);
235 p = sw->con_screen_pos(vc_cons[currcons].d, offset);
239 static inline void scrolldelta(int lines)
241 scrollback_delta += lines;
242 schedule_console_callback();
245 void schedule_console_callback(void)
247 schedule_task(&console_callback_tq);
250 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
252 unsigned short *d, *s;
256 if (b > video_num_lines || t >= b || nr < 1)
258 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
260 d = (unsigned short *) (origin+video_size_row*t);
261 s = (unsigned short *) (origin+video_size_row*(t+nr));
262 scr_memcpyw(d, s, (b-t-nr) * video_size_row);
263 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
267 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
274 if (b > video_num_lines || t >= b || nr < 1)
276 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
278 s = (unsigned short *) (origin+video_size_row*t);
279 step = video_num_columns * nr;
280 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
281 scr_memsetw(s, video_erase_char, 2*step);
284 static void do_update_region(int currcons, unsigned long start, int count)
286 #ifndef VT_BUF_VRAM_ONLY
287 unsigned int xx, yy, offset;
291 if (!sw->con_getxy) {
292 offset = (start - origin) / 2;
293 xx = offset % video_num_columns;
294 yy = offset / video_num_columns;
297 start = sw->con_getxy(vc_cons[currcons].d, start, &nxx, &nyy);
301 u16 attrib = scr_readw(p) & 0xff00;
304 while (xx < video_num_columns && count) {
305 if (attrib != (scr_readw(p) & 0xff00)) {
307 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
310 attrib = scr_readw(p) & 0xff00;
317 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
324 start = sw->con_getxy(vc_cons[currcons].d, start, NULL, NULL);
330 void update_region(int currcons, unsigned long start, int count)
333 hide_cursor(currcons);
334 do_update_region(currcons, start, count);
335 set_cursor(currcons);
339 /* Structure of attributes is hardware-dependent */
341 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
343 if (sw->con_build_attr)
344 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
346 #ifndef VT_BUF_VRAM_ONLY
348 * ++roman: I completely changed the attribute format for monochrome
349 * mode (!can_do_color). The formerly used MDA (monochrome display
350 * adapter) format didn't allow the combination of certain effects.
351 * Now the attribute is just a bit vector:
352 * Bit 0..1: intensity (0..2)
361 (_underline ? 4 : 0) |
365 a = (a & 0xf0) | ulcolor;
366 else if (_intensity == 0)
367 a = (a & 0xf0) | halfcolor;
369 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
374 if (hi_font_mask == 0x100)
383 static void update_attr(int currcons)
385 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
386 video_erase_char = (build_attr(currcons, color, 1, blink, 0, decscnm) << 8) | ' ';
389 /* Note: inverting the screen twice should revert to the original state */
391 void invert_screen(int currcons, int offset, int count, int viewed)
396 p = screenpos(currcons, offset, viewed);
397 if (sw->con_invert_region)
398 sw->con_invert_region(vc_cons[currcons].d, p, count);
399 #ifndef VT_BUF_VRAM_ONLY
412 } else if (hi_font_mask == 0x100) {
415 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
422 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
430 do_update_region(currcons, (unsigned long) p, count);
433 /* used by selection: complement pointer position */
434 void complement_pos(int currcons, int offset)
436 static unsigned short *p;
437 static unsigned short old;
438 static unsigned short oldx, oldy;
443 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
449 p = screenpos(currcons, offset, 1);
451 new = old ^ complement_mask;
454 oldx = (offset >> 1) % video_num_columns;
455 oldy = (offset >> 1) / video_num_columns;
456 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
461 static void insert_char(int currcons, unsigned int nr)
463 unsigned short *p, *q = (unsigned short *) pos;
465 p = q + video_num_columns - nr - x;
467 scr_writew(scr_readw(p), p + nr);
468 scr_memsetw(q, video_erase_char, nr*2);
471 unsigned short oldattr = attr;
472 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
473 video_num_columns-x-nr);
474 attr = video_erase_char >> 8;
476 sw->con_putc(vc_cons[currcons].d,
477 video_erase_char,y,x+nr);
482 static void delete_char(int currcons, unsigned int nr)
485 unsigned short *p = (unsigned short *) pos;
487 while (++i <= video_num_columns - nr) {
488 scr_writew(scr_readw(p+nr), p);
491 scr_memsetw(p, video_erase_char, nr*2);
494 unsigned short oldattr = attr;
495 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
496 video_num_columns-x-nr);
497 attr = video_erase_char >> 8;
499 sw->con_putc(vc_cons[currcons].d,
501 video_num_columns-1-nr);
506 static int softcursor_original;
508 static void add_softcursor(int currcons)
510 int i = scr_readw((u16 *) pos);
511 u32 type = cursor_type;
513 if (! (type & 0x10)) return;
514 if (softcursor_original != -1) return;
515 softcursor_original = i;
516 i |= ((type >> 8) & 0xff00 );
517 i ^= ((type) & 0xff00 );
518 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
519 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
520 scr_writew(i, (u16 *) pos);
522 sw->con_putc(vc_cons[currcons].d, i, y, x);
525 static void hide_cursor(int currcons)
527 if (currcons == sel_cons)
529 if (softcursor_original != -1) {
530 scr_writew(softcursor_original,(u16 *) pos);
532 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
533 softcursor_original = -1;
535 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
538 static void set_cursor(int currcons)
540 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
543 if (currcons == sel_cons)
545 add_softcursor(currcons);
546 if ((cursor_type & 0x0f) != 1)
547 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
549 hide_cursor(currcons);
552 static void set_origin(int currcons)
555 !sw->con_set_origin ||
556 !sw->con_set_origin(vc_cons[currcons].d))
557 origin = (unsigned long) screenbuf;
558 visible_origin = origin;
559 scr_end = origin + screenbuf_size;
560 pos = origin + video_size_row*y + 2*x;
563 static inline void save_screen(int currcons)
565 if (sw->con_save_screen)
566 sw->con_save_screen(vc_cons[currcons].d);
570 * Redrawing of screen
573 void redraw_screen(int new_console, int is_switch)
576 int currcons, old_console;
578 if (!vc_cons_allocated(new_console)) {
580 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
585 currcons = fg_console;
586 hide_cursor(currcons);
587 if (fg_console != new_console) {
588 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
589 old_console = (*display) ? (*display)->vc_num : fg_console;
590 *display = vc_cons[new_console].d;
591 fg_console = new_console;
592 currcons = old_console;
594 save_screen(currcons);
595 set_origin(currcons);
597 currcons = new_console;
598 if (old_console == new_console)
602 currcons = new_console;
603 hide_cursor(currcons);
608 set_origin(currcons);
609 update = sw->con_switch(vc_cons[currcons].d);
610 set_palette(currcons);
611 if (update && vcmode != KD_GRAPHICS)
612 do_update_region(currcons, origin, screenbuf_size/2);
614 set_cursor(currcons);
617 compute_shiftstate();
622 * Allocation, freeing and resizing of VTs.
625 int vc_cons_allocated(unsigned int i)
627 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
630 static void visual_init(int currcons, int init)
632 /* ++Geert: sw->con_init determines console size */
634 #ifndef VT_SINGLE_DRIVER
635 if (con_driver_map[currcons])
636 sw = con_driver_map[currcons];
639 display_fg = &master_display_fg;
640 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
641 vc_cons[currcons].d->vc_uni_pagedir = 0;
645 sw->con_init(vc_cons[currcons].d, init);
646 if (!complement_mask)
647 complement_mask = can_do_color ? 0x7700 : 0x0800;
648 s_complement_mask = complement_mask;
649 video_size_row = video_num_columns<<1;
650 screenbuf_size = video_num_lines*video_size_row;
653 int vc_allocate(unsigned int currcons) /* return 0 on success */
655 if (currcons >= MAX_NR_CONSOLES)
657 if (!vc_cons[currcons].d) {
660 /* prevent users from taking too much memory */
661 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
664 /* due to the granularity of kmalloc, we waste some memory here */
665 /* the alloc is done in two steps, to optimize the common situation
666 of a 25x80 console (structsize=216, screenbuf_size=4000) */
667 /* although the numbers above are not valid since long ago, the
668 point is still up-to-date and the comment still has its value
669 even if only as a historical artifact. --mj, July 1998 */
670 p = (long) kmalloc(structsize, GFP_KERNEL);
673 memset((void *)p, 0, structsize);
674 vc_cons[currcons].d = (struct vc_data *)p;
675 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
676 visual_init(currcons, 1);
677 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
678 con_set_default_unimap(currcons);
679 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
682 vc_cons[currcons].d = NULL;
683 vt_cons[currcons] = NULL;
686 screenbuf = (unsigned short *) q;
688 vc_init(currcons, video_num_lines, video_num_columns, 1);
691 pm_con = pm_register(PM_SYS_DEV,
700 * Change # of rows and columns (0 means unchanged/the size of fg_console)
701 * [this is to be used together with some user program
702 * like resize that changes the hardware videomode]
704 int vc_resize(unsigned int lines, unsigned int cols,
705 unsigned int first, unsigned int last)
707 unsigned int cc, ll, ss, sr, todo = 0;
708 unsigned int currcons = fg_console, i;
709 unsigned short *newscreens[MAX_NR_CONSOLES];
711 cc = (cols ? cols : video_num_columns);
712 ll = (lines ? lines : video_num_lines);
716 for (currcons = first; currcons <= last; currcons++) {
717 if (!vc_cons_allocated(currcons) ||
718 (cc == video_num_columns && ll == video_num_lines))
719 newscreens[currcons] = NULL;
721 unsigned short *p = (unsigned short *) kmalloc(ss, GFP_USER);
723 for (i = first; i < currcons; i++)
725 kfree(newscreens[i]);
728 newscreens[currcons] = p;
735 for (currcons = first; currcons <= last; currcons++) {
736 unsigned int occ, oll, oss, osr;
737 unsigned long ol, nl, nlend, rlth, rrem;
738 if (!newscreens[currcons] || !vc_cons_allocated(currcons))
741 oll = video_num_lines;
742 occ = video_num_columns;
743 osr = video_size_row;
744 oss = screenbuf_size;
746 video_num_lines = ll;
747 video_num_columns = cc;
754 nl = (long) newscreens[currcons];
757 ol += (oll - ll) * osr;
759 update_attr(currcons);
761 while (ol < scr_end) {
762 scr_memcpyw((unsigned short *) nl, (unsigned short *) ol, rlth);
764 scr_memsetw((void *)(nl + rlth), video_erase_char, rrem);
769 scr_memsetw((void *) nl, video_erase_char, nlend - nl);
772 screenbuf = newscreens[currcons];
775 set_origin(currcons);
777 /* do part of a reset_terminal() */
779 bottom = video_num_lines;
780 gotoxy(currcons, x, y);
783 if (console_table[currcons]) {
784 struct winsize ws, *cws = &console_table[currcons]->winsize;
785 memset(&ws, 0, sizeof(ws));
786 ws.ws_row = video_num_lines;
787 ws.ws_col = video_num_columns;
788 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
789 console_table[currcons]->pgrp > 0)
790 kill_pg(console_table[currcons]->pgrp, SIGWINCH, 1);
795 update_screen(currcons);
802 void vc_disallocate(unsigned int currcons)
804 acquire_console_sem();
805 if (vc_cons_allocated(currcons)) {
806 sw->con_deinit(vc_cons[currcons].d);
809 if (currcons >= MIN_NR_CONSOLES)
810 kfree(vc_cons[currcons].d);
811 vc_cons[currcons].d = NULL;
813 release_console_sem();
820 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
821 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
822 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
824 #define decarm VC_REPEAT
825 #define decckm VC_CKMODE
826 #define kbdapplic VC_APPLIC
830 * this is what the terminal answers to a ESC-Z or csi0c query.
832 #define VT100ID "\033[?1;2c"
833 #define VT102ID "\033[?6c"
835 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
836 8,12,10,14, 9,13,11,15 };
838 /* the default colour table, for VGA+ colour systems */
839 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
840 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
841 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
842 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
843 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
844 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
847 * gotoxy() must verify all boundaries, because the arguments
848 * might also be negative. If the given position is out of
849 * bounds, the cursor is placed at the nearest margin.
851 static void gotoxy(int currcons, int new_x, int new_y)
858 if (new_x >= video_num_columns)
859 x = video_num_columns - 1;
867 max_y = video_num_lines;
871 else if (new_y >= max_y)
875 pos = origin + y*video_size_row + (x<<1);
879 /* for absolute user moves, when decom is set */
880 static void gotoxay(int currcons, int new_x, int new_y)
882 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
885 void scrollback(int lines)
887 int currcons = fg_console;
890 lines = video_num_lines/2;
894 void scrollfront(int lines)
896 int currcons = fg_console;
899 lines = video_num_lines/2;
903 static void lf(int currcons)
905 /* don't scroll if above bottom of scrolling region, or
906 * if below scrolling region
909 scrup(currcons,top,bottom,1);
910 else if (y < video_num_lines-1) {
912 pos += video_size_row;
917 static void ri(int currcons)
919 /* don't scroll if below top of scrolling region, or
920 * if above scrolling region
923 scrdown(currcons,top,bottom,1);
926 pos -= video_size_row;
931 static inline void cr(int currcons)
937 static inline void bs(int currcons)
946 static inline void del(int currcons)
951 static void csi_J(int currcons, int vpar)
954 unsigned short * start;
957 case 0: /* erase from cursor to end of display */
958 count = (scr_end-pos)>>1;
959 start = (unsigned short *) pos;
961 /* do in two stages */
962 sw->con_clear(vc_cons[currcons].d, y, x, 1,
963 video_num_columns-x);
964 sw->con_clear(vc_cons[currcons].d, y+1, 0,
969 case 1: /* erase from start to cursor */
970 count = ((pos-origin)>>1)+1;
971 start = (unsigned short *) origin;
973 /* do in two stages */
974 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
976 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
980 case 2: /* erase whole display */
981 count = video_num_columns * video_num_lines;
982 start = (unsigned short *) origin;
984 sw->con_clear(vc_cons[currcons].d, 0, 0,
991 scr_memsetw(start, video_erase_char, 2*count);
995 static void csi_K(int currcons, int vpar)
998 unsigned short * start;
1001 case 0: /* erase from cursor to end of line */
1002 count = video_num_columns-x;
1003 start = (unsigned short *) pos;
1005 sw->con_clear(vc_cons[currcons].d, y, x, 1,
1006 video_num_columns-x);
1008 case 1: /* erase from start of line to cursor */
1009 start = (unsigned short *) (pos - (x<<1));
1012 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1015 case 2: /* erase whole line */
1016 start = (unsigned short *) (pos - (x<<1));
1017 count = video_num_columns;
1019 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
1025 scr_memsetw(start, video_erase_char, 2 * count);
1029 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
1035 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
1037 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
1039 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
1043 static void default_attr(int currcons)
1052 /* console_sem is held */
1053 static void csi_m(int currcons)
1057 for (i=0;i<=npar;i++)
1059 case 0: /* all attributes off */
1060 default_attr(currcons);
1077 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1078 * Select primary font, don't display
1079 * control chars if defined, don't set
1082 translate = set_translate(charset == 0
1084 : G1_charset,currcons);
1088 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1089 * Select first alternate font, lets
1090 * chars < 32 be displayed as ROM chars.
1092 translate = set_translate(IBMPC_MAP,currcons);
1096 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1097 * Select second alternate font, toggle
1098 * high bit before displaying as ROM char.
1100 translate = set_translate(IBMPC_MAP,currcons);
1117 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1118 * Enables underscore, white foreground
1119 * with white underscore (Linux - use
1120 * default foreground).
1122 color = (def_color & 0x0f) | background;
1125 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1126 * Disable underline option.
1127 * Reset colour to default? It did this
1130 color = (def_color & 0x0f) | background;
1134 color = (def_color & 0xf0) | foreground;
1137 if (par[i] >= 30 && par[i] <= 37)
1138 color = color_table[par[i]-30]
1140 else if (par[i] >= 40 && par[i] <= 47)
1141 color = (color_table[par[i]-40]<<4)
1145 update_attr(currcons);
1148 static void respond_string(const char * p, struct tty_struct * tty)
1151 tty_insert_flip_char(tty, *p, 0);
1154 con_schedule_flip(tty);
1157 static void cursor_report(int currcons, struct tty_struct * tty)
1161 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1162 respond_string(buf, tty);
1165 static inline void status_report(struct tty_struct * tty)
1167 respond_string("\033[0n", tty); /* Terminal ok */
1170 static inline void respond_ID(struct tty_struct * tty)
1172 respond_string(VT102ID, tty);
1175 void mouse_report(struct tty_struct * tty, int butt, int mrx, int mry)
1179 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1181 respond_string(buf, tty);
1184 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1185 int mouse_reporting(void)
1187 int currcons = fg_console;
1189 return report_mouse;
1192 /* console_sem is held */
1193 static void set_mode(int currcons, int on_off)
1197 for (i=0; i<=npar; i++)
1198 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1199 case 1: /* Cursor keys send ^[Ox/^[[x */
1205 case 3: /* 80/132 mode switch unimplemented */
1208 (void) vc_resize(video_num_lines, deccolm ? 132 : 80);
1209 /* this alone does not suffice; some user mode
1210 utility has to change the hardware regs */
1213 case 5: /* Inverted screen on/off */
1214 if (decscnm != on_off) {
1216 invert_screen(currcons, 0, screenbuf_size, 0);
1217 update_attr(currcons);
1220 case 6: /* Origin relative/absolute */
1222 gotoxay(currcons,0,0);
1224 case 7: /* Autowrap on/off */
1227 case 8: /* Autorepeat on/off */
1234 report_mouse = on_off ? 1 : 0;
1236 case 25: /* Cursor on/off */
1240 report_mouse = on_off ? 2 : 0;
1242 } else switch(par[i]) { /* ANSI modes set/reset */
1243 case 3: /* Monitor (display ctrls) */
1246 case 4: /* Insert Mode on/off */
1249 case 20: /* Lf, Enter == CrLf/Lf */
1258 /* console_sem is held */
1259 static void setterm_command(int currcons)
1262 case 1: /* set color for underline mode */
1263 if (can_do_color && par[1] < 16) {
1264 ulcolor = color_table[par[1]];
1266 update_attr(currcons);
1269 case 2: /* set color for half intensity mode */
1270 if (can_do_color && par[1] < 16) {
1271 halfcolor = color_table[par[1]];
1273 update_attr(currcons);
1276 case 8: /* store colors as defaults */
1278 if (hi_font_mask == 0x100)
1280 default_attr(currcons);
1281 update_attr(currcons);
1283 case 9: /* set blanking interval */
1284 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1285 poke_blanked_console();
1287 case 10: /* set bell frequency in Hz */
1289 bell_pitch = par[1];
1291 bell_pitch = DEFAULT_BELL_PITCH;
1293 case 11: /* set bell duration in msec */
1295 bell_duration = (par[1] < 2000) ?
1298 bell_duration = DEFAULT_BELL_DURATION;
1300 case 12: /* bring specified console to the front */
1301 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1302 set_console(par[1] - 1);
1304 case 13: /* unblank the screen */
1305 poke_blanked_console();
1307 case 14: /* set vesa powerdown interval */
1308 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1313 /* console_sem is held */
1314 static void csi_at(int currcons, unsigned int nr)
1316 if (nr > video_num_columns - x)
1317 nr = video_num_columns - x;
1320 insert_char(currcons, nr);
1323 /* console_sem is held */
1324 static void csi_L(int currcons, unsigned int nr)
1326 if (nr > video_num_lines - y)
1327 nr = video_num_lines - y;
1330 scrdown(currcons,y,bottom,nr);
1334 /* console_sem is held */
1335 static void csi_P(int currcons, unsigned int nr)
1337 if (nr > video_num_columns - x)
1338 nr = video_num_columns - x;
1341 delete_char(currcons, nr);
1344 /* console_sem is held */
1345 static void csi_M(int currcons, unsigned int nr)
1347 if (nr > video_num_lines - y)
1348 nr = video_num_lines - y;
1351 scrup(currcons,y,bottom,nr);
1355 /* console_sem is held (except via vc_init->reset_terminal */
1356 static void save_cur(int currcons)
1360 s_intensity = intensity;
1361 s_underline = underline;
1363 s_reverse = reverse;
1364 s_charset = charset;
1366 saved_G0 = G0_charset;
1367 saved_G1 = G1_charset;
1370 /* console_sem is held */
1371 static void restore_cur(int currcons)
1373 gotoxy(currcons,saved_x,saved_y);
1374 intensity = s_intensity;
1375 underline = s_underline;
1377 reverse = s_reverse;
1378 charset = s_charset;
1380 G0_charset = saved_G0;
1381 G1_charset = saved_G1;
1382 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1383 update_attr(currcons);
1387 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1388 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1391 /* console_sem is held (except via vc_init()) */
1392 static void reset_terminal(int currcons, int do_clear)
1395 bottom = video_num_lines;
1396 vc_state = ESnormal;
1398 translate = set_translate(LAT1_MAP,currcons);
1399 G0_charset = LAT1_MAP;
1400 G1_charset = GRAF_MAP;
1420 kbd_table[currcons].lockstate = 0;
1421 kbd_table[currcons].slockstate = 0;
1422 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1423 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1425 /* Only schedule the keyboard_tasklet if it is enabled. */
1426 if(!atomic_read(&keyboard_tasklet.count))
1429 cursor_type = CUR_DEFAULT;
1430 complement_mask = s_complement_mask;
1432 default_attr(currcons);
1433 update_attr(currcons);
1435 tab_stop[0] = 0x01010100;
1439 tab_stop[4] = 0x01010101;
1441 bell_pitch = DEFAULT_BELL_PITCH;
1442 bell_duration = DEFAULT_BELL_DURATION;
1444 gotoxy(currcons,0,0);
1450 /* console_sem is held */
1451 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1454 * Control characters can be used in the _middle_
1455 * of an escape sequence.
1462 kd_mksound(bell_pitch, bell_duration);
1469 while (x < video_num_columns - 1) {
1471 if (tab_stop[x >> 5] & (1 << (x & 31)))
1476 case 10: case 11: case 12:
1485 translate = set_translate(G1_charset,currcons);
1490 translate = set_translate(G0_charset,currcons);
1494 vc_state = ESnormal;
1503 vc_state = ESsquare;
1508 vc_state = ESnormal;
1511 vc_state = ESsquare;
1514 vc_state = ESnonstd;
1517 vc_state = ESpercent;
1530 tab_stop[x >> 5] |= (1 << (x & 31));
1539 restore_cur(currcons);
1551 reset_terminal(currcons,1);
1553 case '>': /* Numeric keypad */
1556 case '=': /* Appl. keypad */
1562 if (c=='P') { /* palette escape sequence */
1563 for (npar=0; npar<NPAR; npar++)
1566 vc_state = ESpalette;
1568 } else if (c=='R') { /* reset palette */
1569 reset_palette(currcons);
1570 vc_state = ESnormal;
1572 vc_state = ESnormal;
1575 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1576 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1578 int i = par[0]*3, j = 1;
1579 palette[i] = 16*par[j++];
1580 palette[i++] += par[j++];
1581 palette[i] = 16*par[j++];
1582 palette[i++] += par[j++];
1583 palette[i] = 16*par[j++];
1584 palette[i] += par[j];
1585 set_palette(currcons);
1586 vc_state = ESnormal;
1589 vc_state = ESnormal;
1592 for(npar = 0 ; npar < NPAR ; npar++)
1595 vc_state = ESgetpars;
1596 if (c == '[') { /* Function key */
1604 if (c==';' && npar<NPAR-1) {
1607 } else if (c>='0' && c<='9') {
1611 } else vc_state=ESgotpars;
1613 vc_state = ESnormal;
1616 set_mode(currcons,1);
1619 set_mode(currcons,0);
1624 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1626 cursor_type = CUR_DEFAULT;
1634 complement_mask = par[0]<<8 | par[1];
1636 complement_mask = s_complement_mask;
1644 else if (par[0] == 6)
1645 cursor_report(currcons,tty);
1655 if (par[0]) par[0]--;
1656 gotoxy(currcons,par[0],y);
1659 if (!par[0]) par[0]++;
1660 gotoxy(currcons,x,y-par[0]);
1663 if (!par[0]) par[0]++;
1664 gotoxy(currcons,x,y+par[0]);
1667 if (!par[0]) par[0]++;
1668 gotoxy(currcons,x+par[0],y);
1671 if (!par[0]) par[0]++;
1672 gotoxy(currcons,x-par[0],y);
1675 if (!par[0]) par[0]++;
1676 gotoxy(currcons,0,y+par[0]);
1679 if (!par[0]) par[0]++;
1680 gotoxy(currcons,0,y-par[0]);
1683 if (par[0]) par[0]--;
1684 gotoxay(currcons,x,par[0]);
1687 if (par[0]) par[0]--;
1688 if (par[1]) par[1]--;
1689 gotoxay(currcons,par[1],par[0]);
1692 csi_J(currcons,par[0]);
1695 csi_K(currcons,par[0]);
1698 csi_L(currcons,par[0]);
1701 csi_M(currcons,par[0]);
1704 csi_P(currcons,par[0]);
1712 tab_stop[x >> 5] &= ~(1 << (x & 31));
1713 else if (par[0] == 3) {
1724 case 'q': /* DECLL - but only 3 leds */
1725 /* map 0,1,2,3 to 0,1,2,4 */
1727 setledstate(kbd_table + currcons,
1728 (par[0] < 3) ? par[0] : 4);
1734 par[1] = video_num_lines;
1735 /* Minimum allowed region is 2 lines */
1736 if (par[0] < par[1] &&
1737 par[1] <= video_num_lines) {
1740 gotoxay(currcons,0,0);
1747 restore_cur(currcons);
1750 csi_X(currcons, par[0]);
1753 csi_at(currcons,par[0]);
1755 case ']': /* setterm functions */
1756 setterm_command(currcons);
1761 vc_state = ESnormal;
1763 case '@': /* defined in ISO 2022 */
1766 case 'G': /* prelim official escape code */
1767 case '8': /* retained for compatibility */
1773 vc_state = ESnormal;
1776 vc_state = ESnormal;
1778 /* DEC screen alignment test. kludge :-) */
1780 (video_erase_char & 0xff00) | 'E';
1783 (video_erase_char & 0xff00) | ' ';
1784 do_update_region(currcons, origin, screenbuf_size/2);
1789 G0_charset = GRAF_MAP;
1791 G0_charset = LAT1_MAP;
1793 G0_charset = IBMPC_MAP;
1795 G0_charset = USER_MAP;
1797 translate = set_translate(G0_charset,currcons);
1798 vc_state = ESnormal;
1802 G1_charset = GRAF_MAP;
1804 G1_charset = LAT1_MAP;
1806 G1_charset = IBMPC_MAP;
1808 G1_charset = USER_MAP;
1810 translate = set_translate(G1_charset,currcons);
1811 vc_state = ESnormal;
1814 vc_state = ESnormal;
1818 /* This is a temporary buffer used to prepare a tty console write
1819 * so that we can easily avoid touching user space while holding the
1820 * console spinlock. It is allocated in con_init and is shared by
1821 * this code and the vc_screen read/write tty calls.
1823 * We have to allocate this statically in the kernel data section
1824 * since console_init (and thus con_init) are called before any
1825 * kernel memory allocation is available.
1827 char con_buf[PAGE_SIZE];
1828 #define CON_BUF_SIZE PAGE_SIZE
1829 DECLARE_MUTEX(con_buf_sem);
1831 /* acquires console_sem */
1832 static int do_con_write(struct tty_struct * tty, int from_user,
1833 const unsigned char *buf, int count)
1835 #ifdef VT_BUF_VRAM_ONLY
1836 #define FLUSH do { } while(0);
1838 #define FLUSH if (draw_x >= 0) { \
1839 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1844 int c, tc, ok, n = 0, draw_x = -1;
1845 unsigned int currcons;
1846 unsigned long draw_from = 0, draw_to = 0;
1847 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
1848 u16 himask, charmask;
1849 const unsigned char *orig_buf = NULL;
1855 currcons = vt->vc_num;
1856 if (!vc_cons_allocated(currcons)) {
1857 /* could this happen? */
1858 static int error = 0;
1861 printk("con_write: tty %d not allocated\n", currcons+1);
1873 if (count > CON_BUF_SIZE)
1874 count = CON_BUF_SIZE;
1875 console_conditional_schedule();
1876 if (copy_from_user(con_buf, buf, count)) {
1877 n = 0; /* ?? are error codes legal here ?? */
1884 /* At this point 'buf' is guarenteed to be a kernel buffer
1885 * and therefore no access to userspace (and therefore sleeping)
1886 * will be needed. The con_buf_sem serializes all tty based
1887 * console rendering and vcs write/read operations. We hold
1888 * the console spinlock during the entire write.
1891 acquire_console_sem();
1893 himask = hi_font_mask;
1894 charmask = himask ? 0x1ff : 0xff;
1896 /* undraw cursor first */
1898 hide_cursor(currcons);
1900 while (!tty->stopped && count) {
1907 /* Combine UTF-8 into Unicode */
1908 /* Incomplete characters silently ignored */
1910 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1911 utf_char = (utf_char << 6) | (c & 0x3f);
1917 if ((c & 0xe0) == 0xc0) {
1919 utf_char = (c & 0x1f);
1920 } else if ((c & 0xf0) == 0xe0) {
1922 utf_char = (c & 0x0f);
1923 } else if ((c & 0xf8) == 0xf0) {
1925 utf_char = (c & 0x07);
1926 } else if ((c & 0xfc) == 0xf8) {
1928 utf_char = (c & 0x03);
1929 } else if ((c & 0xfe) == 0xfc) {
1931 utf_char = (c & 0x01);
1940 } else { /* no utf */
1941 tc = translate[toggle_meta ? (c|0x80) : c];
1944 /* If the original code was a control character we
1945 * only allow a glyph to be displayed if the code is
1946 * not normally used (such as for cursor movement) or
1947 * if the disp_ctrl mode has been explicitly enabled.
1948 * Certain characters (as given by the CTRL_ALWAYS
1949 * bitmap) are always displayed as control characters,
1950 * as the console would be pretty useless without
1951 * them; to display an arbitrary font position use the
1952 * direct-to-font zone in UTF-8 mode.
1954 ok = tc && (c >= 32 ||
1955 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
1956 : CTRL_ACTION) >> c) & 1)))
1957 && (c != 127 || disp_ctrl)
1960 if (vc_state == ESnormal && ok) {
1961 /* Now try to find out how to display it */
1962 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
1964 /* If we got -4 (not found) then see if we have
1965 defined a replacement character (U+FFFD) */
1966 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
1968 /* One reason for the -4 can be that we just
1969 did a clear_unimap();
1970 try at least to show something. */
1973 } else if ( tc == -3 ) {
1974 /* Bad hash table -- hope for the best */
1978 continue; /* Conversion failed */
1980 if (need_wrap || decim)
1987 insert_char(currcons, 1);
1989 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
1992 if (DO_UPDATE && draw_x < 0) {
1996 if (x == video_num_columns - 1) {
2006 do_con_trol(tty, currcons, c);
2009 console_conditional_schedule();
2010 release_console_sem();
2014 /* If the user requested something larger than
2015 * the CON_BUF_SIZE, and the tty is not stopped,
2018 if ((orig_count > CON_BUF_SIZE) && !tty->stopped) {
2019 orig_count -= CON_BUF_SIZE;
2020 orig_buf += CON_BUF_SIZE;
2034 * This is the console switching callback.
2036 * Doing console switching in a process context allows
2037 * us to do the switches asynchronously (needed when we want
2038 * to switch due to a keyboard interrupt). Synchronization
2039 * with other console code and prevention of re-entrancy is
2040 * ensured with console_sem.
2042 static void console_callback(void *ignored)
2044 acquire_console_sem();
2046 if (want_console >= 0) {
2047 if (want_console != fg_console && vc_cons_allocated(want_console)) {
2048 hide_cursor(fg_console);
2049 change_console(want_console);
2050 /* we only changed when the console had already
2051 been allocated - a new console is not created
2052 in an interrupt routine */
2056 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2057 do_poke_blanked_console = 0;
2058 poke_blanked_console();
2060 if (scrollback_delta) {
2061 int currcons = fg_console;
2063 if (vcmode == KD_TEXT)
2064 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
2065 scrollback_delta = 0;
2068 release_console_sem();
2071 void set_console(int nr)
2074 schedule_console_callback();
2077 #ifdef CONFIG_VT_CONSOLE
2080 * Console on virtual terminal
2082 * The console must be locked when we get here.
2085 void vt_console_print(struct console *co, const char * b, unsigned count)
2087 int currcons = fg_console;
2089 static unsigned long printing;
2090 const ushort *start;
2094 /* console busy or not yet initialized */
2095 if (!printable || test_and_set_bit(0, &printing))
2100 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2101 currcons = kmsg_redirect - 1;
2103 /* read `x' only after setting currecons properly (otherwise
2104 the `x' macro will read the x of the foreground console). */
2107 if (!vc_cons_allocated(currcons)) {
2109 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2113 if (vcmode != KD_TEXT)
2116 /* undraw cursor first */
2118 hide_cursor(currcons);
2120 start = (ushort *)pos;
2122 /* Contrived structure to try to emulate original need_wrap behaviour
2123 * Problems caused when we have need_wrap set on '\n' character */
2126 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2129 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2135 if (c == 8) { /* backspace */
2137 start = (ushort *)pos;
2144 start = (ushort *)pos;
2146 if (c == 10 || c == 13)
2149 scr_writew((attr << 8) + c, (unsigned short *) pos);
2151 if (myx == video_num_columns - 1) {
2160 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2162 if (x == video_num_columns) {
2167 set_cursor(currcons);
2169 if (!oops_in_progress)
2170 poke_blanked_console();
2173 clear_bit(0, &printing);
2176 static kdev_t vt_console_device(struct console *c)
2178 return MKDEV(TTY_MAJOR, c->index ? c->index : fg_console + 1);
2181 struct console vt_console_driver = {
2183 write: vt_console_print,
2184 device: vt_console_device,
2185 unblank: unblank_screen,
2186 flags: CON_PRINTBUFFER,
2192 * Handling of Linux-specific VC ioctls
2196 * Generally a bit racy with respect to console_sem().
2198 * There are some functions which don't need it.
2200 * There are some functions which can sleep for arbitrary periods (paste_selection)
2201 * but we don't need the lock there anyway.
2203 * set_selection has locking, and definitely needs it
2206 int tioclinux(struct tty_struct *tty, unsigned long arg)
2211 if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
2213 if (current->tty != tty && !capable(CAP_SYS_ADMIN))
2215 if (get_user(type, (char *)arg))
2221 acquire_console_sem();
2222 ret = set_selection(arg, tty, 1);
2223 release_console_sem();
2226 ret = paste_selection(tty);
2232 ret = sel_loadlut(arg);
2237 * Make it possible to react to Shift+Mousebutton.
2238 * Note that 'shift_state' is an undocumented
2239 * kernel-internal variable; programs not closely
2240 * related to the kernel should not use this.
2243 ret = __put_user(data, (char *) arg);
2246 data = mouse_reporting();
2247 ret = __put_user(data, (char *) arg);
2250 set_vesa_blanking(arg);
2252 case 11: /* set kmsg redirect */
2253 if (!capable(CAP_SYS_ADMIN)) {
2256 if (get_user(data, (char *)arg+1))
2259 kmsg_redirect = data;
2262 case 12: /* get fg_console */
2273 * /dev/ttyN handling
2276 static int con_write(struct tty_struct * tty, int from_user,
2277 const unsigned char *buf, int count)
2282 retval = do_con_write(tty, from_user, buf, count);
2283 con_flush_chars(tty);
2288 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2291 return; /* n_r3964 calls put_char() from interrupt context */
2293 do_con_write(tty, 0, &ch, 1);
2296 static int con_write_room(struct tty_struct *tty)
2300 return 4096; /* No limit, really; we're not buffering */
2303 static int con_chars_in_buffer(struct tty_struct *tty)
2305 return 0; /* we're not buffering */
2309 * con_throttle and con_unthrottle are only used for
2310 * paste_selection(), which has to stuff in a large number of
2313 static void con_throttle(struct tty_struct *tty)
2317 static void con_unthrottle(struct tty_struct *tty)
2319 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
2321 wake_up_interruptible(&vt->paste_wait);
2325 * Turn the Scroll-Lock LED on when the tty is stopped
2327 static void con_stop(struct tty_struct *tty)
2332 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2333 if (!vc_cons_allocated(console_num))
2335 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2340 * Turn the Scroll-Lock LED off when the console is started
2342 static void con_start(struct tty_struct *tty)
2347 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2348 if (!vc_cons_allocated(console_num))
2350 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2355 * we can race here against con_close, so we grab the bkl
2356 * and check the pointer before calling set_cursor
2358 static void con_flush_chars(struct tty_struct *tty)
2360 struct vt_struct *vt;
2362 if (in_interrupt()) /* from flush_to_ldisc */
2367 acquire_console_sem();
2368 vt = (struct vt_struct *)tty->driver_data;
2370 set_cursor(vt->vc_num);
2371 release_console_sem();
2376 * Allocate the console screen memory.
2378 static int con_open(struct tty_struct *tty, struct file * filp)
2380 unsigned int currcons;
2383 currcons = MINOR(tty->device) - tty->driver.minor_start;
2385 i = vc_allocate(currcons);
2389 vt_cons[currcons]->vc_num = currcons;
2390 tty->driver_data = vt_cons[currcons];
2392 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2393 tty->winsize.ws_row = video_num_lines;
2394 tty->winsize.ws_col = video_num_columns;
2396 if (tty->count == 1)
2397 vcs_make_devfs (currcons, 0);
2401 static void con_close(struct tty_struct *tty, struct file * filp)
2405 if (tty->count != 1) return;
2406 vcs_make_devfs (MINOR (tty->device) - tty->driver.minor_start, 1);
2407 tty->driver_data = 0;
2410 static void vc_init(unsigned int currcons, unsigned int rows, unsigned int cols, int do_clear)
2414 video_num_columns = cols;
2415 video_num_lines = rows;
2416 video_size_row = cols<<1;
2417 screenbuf_size = video_num_lines * video_size_row;
2419 set_origin(currcons);
2422 for (j=k=0; j<16; j++) {
2423 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2424 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2425 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2427 def_color = 0x07; /* white */
2428 ulcolor = 0x0f; /* bold white */
2429 halfcolor = 0x08; /* grey */
2430 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2431 reset_terminal(currcons, do_clear);
2435 * This routine initializes console interrupts, and does nothing
2436 * else. If you want the screen to clear, call tty_write with
2437 * the appropriate escape-sequence.
2440 struct tty_driver console_driver;
2441 static int console_refcount;
2443 void __init con_init(void)
2445 const char *display_desc = NULL;
2446 unsigned int currcons = 0;
2449 display_desc = conswitchp->con_startup();
2450 if (!display_desc) {
2455 memset(&console_driver, 0, sizeof(struct tty_driver));
2456 console_driver.magic = TTY_DRIVER_MAGIC;
2457 console_driver.name = "vc/%d";
2458 console_driver.name_base = 1;
2459 console_driver.major = TTY_MAJOR;
2460 console_driver.minor_start = 1;
2461 console_driver.num = MAX_NR_CONSOLES;
2462 console_driver.type = TTY_DRIVER_TYPE_CONSOLE;
2463 console_driver.init_termios = tty_std_termios;
2464 console_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2465 /* Tell tty_register_driver() to skip consoles because they are
2466 * registered before kmalloc() is ready. We'll patch them in later.
2467 * See comments at console_init(); see also con_init_devfs().
2469 console_driver.flags |= TTY_DRIVER_NO_DEVFS;
2470 console_driver.refcount = &console_refcount;
2471 console_driver.table = console_table;
2472 console_driver.termios = console_termios;
2473 console_driver.termios_locked = console_termios_locked;
2475 console_driver.open = con_open;
2476 console_driver.close = con_close;
2477 console_driver.write = con_write;
2478 console_driver.write_room = con_write_room;
2479 console_driver.put_char = con_put_char;
2480 console_driver.flush_chars = con_flush_chars;
2481 console_driver.chars_in_buffer = con_chars_in_buffer;
2482 console_driver.ioctl = vt_ioctl;
2483 console_driver.stop = con_stop;
2484 console_driver.start = con_start;
2485 console_driver.throttle = con_throttle;
2486 console_driver.unthrottle = con_unthrottle;
2488 if (tty_register_driver(&console_driver))
2489 panic("Couldn't register console driver\n");
2491 init_timer(&console_timer);
2492 console_timer.function = blank_screen;
2493 if (blankinterval) {
2494 mod_timer(&console_timer, jiffies + blankinterval);
2498 * kmalloc is not running yet - we use the bootmem allocator.
2500 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2501 vc_cons[currcons].d = (struct vc_data *)
2502 alloc_bootmem(sizeof(struct vc_data));
2503 vt_cons[currcons] = (struct vt_struct *)
2504 alloc_bootmem(sizeof(struct vt_struct));
2505 visual_init(currcons, 1);
2506 screenbuf = (unsigned short *) alloc_bootmem(screenbuf_size);
2508 vc_init(currcons, video_num_lines, video_num_columns,
2509 currcons || !sw->con_save_screen);
2511 currcons = fg_console = 0;
2512 master_display_fg = vc_cons[currcons].d;
2513 set_origin(currcons);
2514 save_screen(currcons);
2515 gotoxy(currcons,x,y);
2517 update_screen(fg_console);
2518 printk("Console: %s %s %dx%d",
2519 can_do_color ? "colour" : "mono",
2520 display_desc, video_num_columns, video_num_lines);
2524 #ifdef CONFIG_VT_CONSOLE
2525 register_console(&vt_console_driver);
2529 #ifndef VT_SINGLE_DRIVER
2531 static void clear_buffer_attributes(int currcons)
2533 unsigned short *p = (unsigned short *) origin;
2534 int count = screenbuf_size/2;
2535 int mask = hi_font_mask | 0xff;
2537 for (; count > 0; count--, p++) {
2538 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2543 * If we support more console drivers, this function is used
2544 * when a driver wants to take over some existing consoles
2545 * and become default driver for newly opened ones.
2548 void take_over_console(const struct consw *csw, int first, int last, int deflt)
2553 desc = csw->con_startup();
2558 for (i = first; i <= last; i++) {
2562 con_driver_map[i] = csw;
2564 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2570 old_was_color = vc_cons[i].d->vc_can_do_color;
2571 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2575 /* If the console changed between mono <-> color, then
2576 * the attributes in the screenbuf will be wrong. The
2577 * following resets all attributes to something sane.
2579 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2580 clear_buffer_attributes(i);
2585 printk("Console: switching ");
2587 printk("consoles %d-%d ", first+1, last+1);
2589 printk("to %s %s %dx%d\n",
2590 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2591 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2593 printk("to %s\n", desc);
2596 void give_up_console(const struct consw *csw)
2600 for(i = 0; i < MAX_NR_CONSOLES; i++)
2601 if (con_driver_map[i] == csw)
2602 con_driver_map[i] = NULL;
2611 static void set_vesa_blanking(unsigned long arg)
2613 char *argp = (char *)arg + 1;
2615 if (get_user(mode, argp) == 0)
2616 vesa_blank_mode = (mode < 4) ? mode : 0;
2619 /* We can't register the console with devfs during con_init(), because it
2620 * is called before kmalloc() works. This function is called later to
2621 * do the registration.
2623 void __init con_init_devfs (void)
2627 for (i = 0; i < console_driver.num; i++)
2628 tty_register_devfs (&console_driver, DEVFS_FL_AOPEN_NOTIFY,
2629 console_driver.minor_start + i);
2633 * This is called by a timer handler
2635 static void vesa_powerdown(void)
2637 struct vc_data *c = vc_cons[fg_console].d;
2639 * Power down if currently suspended (1 or 2),
2640 * suspend if currently blanked (0),
2641 * else do nothing (i.e. already powered down (3)).
2642 * Called only if powerdown features are allowed.
2644 switch (vesa_blank_mode) {
2645 case VESA_NO_BLANKING:
2646 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1);
2648 case VESA_VSYNC_SUSPEND:
2649 case VESA_HSYNC_SUSPEND:
2650 c->vc_sw->con_blank(c, VESA_POWERDOWN+1);
2656 * This is a timer handler
2658 static void vesa_powerdown_screen(unsigned long dummy)
2660 console_timer.function = unblank_screen_t;
2665 static void timer_do_blank_screen(int entering_gfx, int from_timer_handler)
2667 int currcons = fg_console;
2670 if (console_blanked)
2673 /* entering graphics mode? */
2675 hide_cursor(currcons);
2676 save_screen(currcons);
2677 sw->con_blank(vc_cons[currcons].d, -1);
2678 console_blanked = fg_console + 1;
2679 set_origin(currcons);
2683 /* don't blank graphics */
2684 if (vcmode != KD_TEXT) {
2685 console_blanked = fg_console + 1;
2689 hide_cursor(currcons);
2690 if (!from_timer_handler)
2691 del_timer_sync(&console_timer);
2692 if (vesa_off_interval) {
2693 console_timer.function = vesa_powerdown_screen;
2694 mod_timer(&console_timer, jiffies + vesa_off_interval);
2696 if (!from_timer_handler)
2697 del_timer_sync(&console_timer);
2698 console_timer.function = unblank_screen_t;
2701 save_screen(currcons);
2702 /* In case we need to reset origin, blanking hook returns 1 */
2703 i = sw->con_blank(vc_cons[currcons].d, 1);
2704 console_blanked = fg_console + 1;
2706 set_origin(currcons);
2708 if (console_blank_hook && console_blank_hook(1))
2710 if (vesa_blank_mode)
2711 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1);
2714 void do_blank_screen(int entering_gfx)
2716 timer_do_blank_screen(entering_gfx, 0);
2720 * This is a timer handler
2722 static void unblank_screen_t(unsigned long dummy)
2728 * Called by timer as well as from vt_console_driver
2730 void unblank_screen(void)
2734 if (!console_blanked)
2736 if (!vc_cons_allocated(fg_console)) {
2738 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2741 currcons = fg_console;
2742 if (vcmode != KD_TEXT)
2743 return; /* but leave console_blanked != 0 */
2745 console_timer.function = blank_screen;
2746 if (blankinterval) {
2747 mod_timer(&console_timer, jiffies + blankinterval);
2750 console_blanked = 0;
2751 if (console_blank_hook)
2752 console_blank_hook(0);
2753 set_palette(currcons);
2754 if (sw->con_blank(vc_cons[currcons].d, 0))
2755 /* Low-level driver cannot restore -> do it ourselves */
2756 update_screen(fg_console);
2757 set_cursor(fg_console);
2761 * This is both a user-level callable and a timer handler
2763 static void blank_screen(unsigned long dummy)
2765 timer_do_blank_screen(0, 1);
2768 void poke_blanked_console(void)
2770 del_timer(&console_timer);
2771 if (!vt_cons[fg_console] || vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2773 if (console_blanked) {
2774 console_timer.function = unblank_screen_t;
2775 mod_timer(&console_timer, jiffies); /* Now */
2776 } else if (blankinterval) {
2777 mod_timer(&console_timer, jiffies + blankinterval);
2785 void set_palette(int currcons)
2787 if (vcmode != KD_GRAPHICS)
2788 sw->con_set_palette(vc_cons[currcons].d, color_table);
2791 static int set_get_cmap(unsigned char *arg, int set)
2795 for (i = 0; i < 16; i++)
2797 get_user(default_red[i], arg++);
2798 get_user(default_grn[i], arg++);
2799 get_user(default_blu[i], arg++);
2801 put_user(default_red[i], arg++);
2802 put_user(default_grn[i], arg++);
2803 put_user(default_blu[i], arg++);
2806 for (i = 0; i < MAX_NR_CONSOLES; i++)
2807 if (vc_cons_allocated(i)) {
2808 for (j = k = 0; j < 16; j++) {
2809 vc_cons[i].d->vc_palette[k++] = default_red[j];
2810 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2811 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2820 * Load palette into the DAC registers. arg points to a colour
2821 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2824 int con_set_cmap(unsigned char *arg)
2826 return set_get_cmap (arg,1);
2829 int con_get_cmap(unsigned char *arg)
2831 return set_get_cmap (arg,0);
2834 void reset_palette(int currcons)
2837 for (j=k=0; j<16; j++) {
2838 palette[k++] = default_red[j];
2839 palette[k++] = default_grn[j];
2840 palette[k++] = default_blu[j];
2842 set_palette(currcons);
2848 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2849 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2850 * depending on width) reserved for each character which is kinda wasty, but
2851 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2852 * is upto the actual low-level console-driver convert data into its favorite
2853 * format (maybe we should add a `fontoffset' field to the `display'
2854 * structure so we wont have to convert the fontdata all the time.
2858 #define max_font_size 65536
2860 int con_font_op(int currcons, struct console_font_op *op)
2863 int size = max_font_size, set;
2865 struct console_font_op old_op;
2867 if (vt_cons[currcons]->vc_mode != KD_TEXT)
2869 memcpy(&old_op, op, sizeof(old_op));
2870 if (op->op == KD_FONT_OP_SET) {
2873 if (op->charcount > 512)
2875 if (!op->height) { /* Need to guess font height [compat] */
2877 u8 *charmap = op->data, tmp;
2879 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2880 so that we can get rid of this soon */
2881 if (!(op->flags & KD_FONT_FLAG_OLD))
2884 for (h = 32; h > 0; h--)
2885 for (i = 0; i < op->charcount; i++) {
2886 if (get_user(tmp, &charmap[32*i+h-1]))
2897 if (op->width > 32 || op->height > 32)
2899 size = (op->width+7)/8 * 32 * op->charcount;
2900 if (size > max_font_size)
2903 } else if (op->op == KD_FONT_OP_GET)
2906 return sw->con_font_op(vc_cons[currcons].d, op);
2908 temp = kmalloc(size, GFP_KERNEL);
2911 if (set && copy_from_user(temp, op->data, size)) {
2918 acquire_console_sem();
2919 rc = sw->con_font_op(vc_cons[currcons].d, op);
2920 release_console_sem();
2922 op->data = old_op.data;
2924 int c = (op->width+7)/8 * 32 * op->charcount;
2926 if (op->data && op->charcount > old_op.charcount)
2928 if (!(op->flags & KD_FONT_FLAG_OLD)) {
2929 if (op->width > old_op.width ||
2930 op->height > old_op.height)
2935 else if ((old_op.height && op->height > old_op.height) ||
2939 if (!rc && op->data && copy_to_user(op->data, temp, c))
2948 * Interface exported to selection and vcs.
2951 /* used by selection */
2952 u16 screen_glyph(int currcons, int offset)
2954 u16 w = scr_readw(screenpos(currcons, offset, 1));
2957 if (w & hi_font_mask)
2962 /* used by vcs - note the word offset */
2963 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
2965 return screenpos(currcons, 2 * w_offset, viewed);
2968 void getconsxy(int currcons, char *p)
2974 void putconsxy(int currcons, char *p)
2976 gotoxy(currcons, p[0], p[1]);
2977 set_cursor(currcons);
2980 u16 vcs_scr_readw(int currcons, const u16 *org)
2982 if ((unsigned long)org == pos && softcursor_original != -1)
2983 return softcursor_original;
2984 return scr_readw(org);
2987 void vcs_scr_writew(int currcons, u16 val, u16 *org)
2989 scr_writew(val, org);
2990 if ((unsigned long)org == pos) {
2991 softcursor_original = -1;
2992 add_softcursor(currcons);
2996 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data)
2998 int save_vesa_interval;
3006 /* We temporarily set vesa_off_interval to prevent
3007 * firing up the console timer during suspend
3009 save_vesa_interval = vesa_off_interval;
3010 vesa_off_interval = 0;
3012 vesa_off_interval = save_vesa_interval;
3019 * Visible symbols for modules
3022 EXPORT_SYMBOL(color_table);
3023 EXPORT_SYMBOL(default_red);
3024 EXPORT_SYMBOL(default_grn);
3025 EXPORT_SYMBOL(default_blu);
3026 EXPORT_SYMBOL(video_font_height);
3027 EXPORT_SYMBOL(video_scan_lines);
3028 EXPORT_SYMBOL(vc_resize);
3029 EXPORT_SYMBOL(fg_console);
3030 EXPORT_SYMBOL(console_blank_hook);
3032 EXPORT_SYMBOL(vt_cons);
3034 #ifndef VT_SINGLE_DRIVER
3035 EXPORT_SYMBOL(take_over_console);
3036 EXPORT_SYMBOL(give_up_console);