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>
74 #include <linux/module.h>
75 #include <linux/sched.h>
76 #include <linux/tty.h>
77 #include <linux/tty_flip.h>
78 #include <linux/kernel.h>
79 #include <linux/string.h>
80 #include <linux/errno.h>
82 #include <linux/malloc.h>
83 #include <linux/major.h>
85 #include <linux/console.h>
86 #include <linux/init.h>
87 #include <linux/devfs_fs_kernel.h>
88 #include <linux/vt_kern.h>
89 #include <linux/selection.h>
90 #include <linux/console_struct.h>
91 #include <linux/kbd_kern.h>
92 #include <linux/consolemap.h>
93 #include <linux/timer.h>
94 #include <linux/interrupt.h>
95 #include <linux/config.h>
96 #include <linux/version.h>
97 #include <linux/tqueue.h>
98 #include <linux/bootmem.h>
102 #include <asm/system.h>
103 #include <asm/uaccess.h>
104 #include <asm/bitops.h>
106 #include <asm/linux_logo.h>
108 #include "console_macros.h"
111 const struct consw
*conswitchp
;
113 /* A bitmap for codes <32. A bit of 1 indicates that the code
114 * corresponding to that bit number invokes some special action
115 * (such as cursor movement) and should not be displayed as a
116 * glyph unless the disp_ctrl mode is explicitly enabled.
118 #define CTRL_ACTION 0x0d00ff81
119 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
122 * Here is the default bell parameters: 750HZ, 1/8th of a second
124 #define DEFAULT_BELL_PITCH 750
125 #define DEFAULT_BELL_DURATION (HZ/8)
127 extern void vcs_make_devfs (unsigned int index
, int unregister
);
130 #define MIN(a,b) ((a) < (b) ? (a) : (b))
133 static struct tty_struct
*console_table
[MAX_NR_CONSOLES
];
134 static struct termios
*console_termios
[MAX_NR_CONSOLES
];
135 static struct termios
*console_termios_locked
[MAX_NR_CONSOLES
];
136 struct vc vc_cons
[MAX_NR_CONSOLES
];
138 #ifndef VT_SINGLE_DRIVER
139 static const struct consw
*con_driver_map
[MAX_NR_CONSOLES
];
142 static int con_open(struct tty_struct
*, struct file
*);
143 static void vc_init(unsigned int console
, unsigned int rows
,
144 unsigned int cols
, int do_clear
);
145 static void blank_screen(unsigned long dummy
);
146 static void gotoxy(int currcons
, int new_x
, int new_y
);
147 static void save_cur(int currcons
);
148 static void reset_terminal(int currcons
, int do_clear
);
149 static void con_flush_chars(struct tty_struct
*tty
);
150 static void set_vesa_blanking(unsigned long arg
);
151 static void set_cursor(int currcons
);
152 static void hide_cursor(int currcons
);
153 static void unblank_screen_t(unsigned long dummy
);
155 static int printable
; /* Is console ready for printing? */
157 int do_poke_blanked_console
;
160 static int vesa_blank_mode
; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
161 static int blankinterval
= 10*60*HZ
;
162 static int vesa_off_interval
;
165 * fg_console is the current virtual console,
166 * last_console is the last used one,
167 * want_console is the console we want to switch to,
168 * kmsg_redirect is the console for kernel messages,
172 int want_console
= -1;
176 * For each existing display, we have a pointer to console currently visible
177 * on that display, allowing consoles other than fg_console to be refreshed
178 * appropriately. Unless the low-level driver supplies its own display_fg
179 * variable, we use this one for the "master display".
181 static struct vc_data
*master_display_fg
;
184 * Unfortunately, we need to delay tty echo when we're currently writing to the
185 * console since the code is (and always was) not re-entrant, so we insert
186 * all filp requests to con_task_queue instead of tq_timer and run it from
187 * the console_tasklet. The console_tasklet is protected by the IRQ
188 * protected console_lock.
190 DECLARE_TASK_QUEUE(con_task_queue
);
193 * For the same reason, we defer scrollback to the console tasklet.
195 static int scrollback_delta
;
198 * Hook so that the power management routines can (un)blank
199 * the console on our behalf.
201 int (*console_blank_hook
)(int);
203 static struct timer_list console_timer
;
206 * Low-Level Functions
209 #define IS_FG (currcons == fg_console)
210 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
212 #ifdef VT_BUF_VRAM_ONLY
215 #define DO_UPDATE IS_VISIBLE
218 static int pm_con_request(struct pm_dev
*dev
, pm_request_t rqst
, void *data
);
219 static struct pm_dev
*pm_con
;
221 static inline unsigned short *screenpos(int currcons
, int offset
, int viewed
)
226 p
= (unsigned short *)(origin
+ offset
);
227 else if (!sw
->con_screen_pos
)
228 p
= (unsigned short *)(visible_origin
+ offset
);
230 p
= sw
->con_screen_pos(vc_cons
[currcons
].d
, offset
);
234 static inline void scrolldelta(int lines
)
236 scrollback_delta
+= lines
;
237 tasklet_schedule(&console_tasklet
);
240 static void scrup(int currcons
, unsigned int t
, unsigned int b
, int nr
)
242 unsigned short *d
, *s
;
246 if (b
> video_num_lines
|| t
>= b
|| nr
< 1)
248 if (IS_VISIBLE
&& sw
->con_scroll(vc_cons
[currcons
].d
, t
, b
, SM_UP
, nr
))
250 d
= (unsigned short *) (origin
+video_size_row
*t
);
251 s
= (unsigned short *) (origin
+video_size_row
*(t
+nr
));
252 scr_memcpyw(d
, s
, (b
-t
-nr
) * video_size_row
);
253 scr_memsetw(d
+ (b
-t
-nr
) * video_num_columns
, video_erase_char
, video_size_row
*nr
);
257 scrdown(int currcons
, unsigned int t
, unsigned int b
, int nr
)
264 if (b
> video_num_lines
|| t
>= b
|| nr
< 1)
266 if (IS_VISIBLE
&& sw
->con_scroll(vc_cons
[currcons
].d
, t
, b
, SM_DOWN
, nr
))
268 s
= (unsigned short *) (origin
+video_size_row
*t
);
269 step
= video_num_columns
* nr
;
270 scr_memmovew(s
+ step
, s
, (b
-t
-nr
)*video_size_row
);
271 scr_memsetw(s
, video_erase_char
, 2*step
);
274 static void do_update_region(int currcons
, unsigned long start
, int count
)
276 #ifndef VT_BUF_VRAM_ONLY
277 unsigned int xx
, yy
, offset
;
281 if (!sw
->con_getxy
) {
282 offset
= (start
- origin
) / 2;
283 xx
= offset
% video_num_columns
;
284 yy
= offset
/ video_num_columns
;
287 start
= sw
->con_getxy(vc_cons
[currcons
].d
, start
, &nxx
, &nyy
);
291 u16 attrib
= scr_readw(p
) & 0xff00;
294 while (xx
< video_num_columns
&& count
) {
295 if (attrib
!= (scr_readw(p
) & 0xff00)) {
297 sw
->con_putcs(vc_cons
[currcons
].d
, q
, p
-q
, yy
, startx
);
300 attrib
= scr_readw(p
) & 0xff00;
307 sw
->con_putcs(vc_cons
[currcons
].d
, q
, p
-q
, yy
, startx
);
314 start
= sw
->con_getxy(vc_cons
[currcons
].d
, start
, NULL
, NULL
);
320 void update_region(int currcons
, unsigned long start
, int count
)
323 hide_cursor(currcons
);
324 do_update_region(currcons
, start
, count
);
325 set_cursor(currcons
);
329 /* Structure of attributes is hardware-dependent */
331 static u8
build_attr(int currcons
, u8 _color
, u8 _intensity
, u8 _blink
, u8 _underline
, u8 _reverse
)
333 if (sw
->con_build_attr
)
334 return sw
->con_build_attr(vc_cons
[currcons
].d
, _color
, _intensity
, _blink
, _underline
, _reverse
);
336 #ifndef VT_BUF_VRAM_ONLY
338 * ++roman: I completely changed the attribute format for monochrome
339 * mode (!can_do_color). The formerly used MDA (monochrome display
340 * adapter) format didn't allow the combination of certain effects.
341 * Now the attribute is just a bit vector:
342 * Bit 0..1: intensity (0..2)
351 (_underline
? 4 : 0) |
355 a
= (a
& 0xf0) | ulcolor
;
356 else if (_intensity
== 0)
357 a
= (a
& 0xf0) | halfcolor
;
359 a
= ((a
) & 0x88) | ((((a
) >> 4) | ((a
) << 4)) & 0x77);
364 if (hi_font_mask
== 0x100)
373 static void update_attr(int currcons
)
375 attr
= build_attr(currcons
, color
, intensity
, blink
, underline
, reverse
^ decscnm
);
376 video_erase_char
= (build_attr(currcons
, color
, 1, blink
, 0, decscnm
) << 8) | ' ';
379 /* Note: inverting the screen twice should revert to the original state */
381 void invert_screen(int currcons
, int offset
, int count
, int viewed
)
386 p
= screenpos(currcons
, offset
, viewed
);
387 if (sw
->con_invert_region
)
388 sw
->con_invert_region(vc_cons
[currcons
].d
, p
, count
);
389 #ifndef VT_BUF_VRAM_ONLY
395 while (cnt
--) *q
++ ^= 0x0800;
396 } else if (hi_font_mask
== 0x100) {
399 a
= ((a
) & 0x11ff) | (((a
) & 0xe000) >> 4) | (((a
) & 0x0e00) << 4);
405 a
= ((a
) & 0x88ff) | (((a
) & 0x7000) >> 4) | (((a
) & 0x0700) << 4);
412 do_update_region(currcons
, (unsigned long) p
, count
);
415 /* used by selection: complement pointer position */
416 void complement_pos(int currcons
, int offset
)
418 static unsigned short *p
;
419 static unsigned short old
;
420 static unsigned short oldx
, oldy
;
425 sw
->con_putc(vc_cons
[currcons
].d
, old
, oldy
, oldx
);
431 p
= screenpos(currcons
, offset
, 1);
433 new = old
^ complement_mask
;
436 oldx
= (offset
>> 1) % video_num_columns
;
437 oldy
= (offset
>> 1) / video_num_columns
;
438 sw
->con_putc(vc_cons
[currcons
].d
, new, oldy
, oldx
);
443 static void insert_char(int currcons
, unsigned int nr
)
445 unsigned short *p
, *q
= (unsigned short *) pos
;
447 p
= q
+ video_num_columns
- nr
- x
;
449 scr_writew(scr_readw(p
), p
+ nr
);
450 scr_memsetw(q
, video_erase_char
, nr
*2);
453 unsigned short oldattr
= attr
;
454 sw
->con_bmove(vc_cons
[currcons
].d
,y
,x
,y
,x
+nr
,1,
455 video_num_columns
-x
-nr
);
456 attr
= video_erase_char
>> 8;
458 sw
->con_putc(vc_cons
[currcons
].d
,
459 video_erase_char
,y
,x
+nr
);
464 static void delete_char(int currcons
, unsigned int nr
)
467 unsigned short *p
= (unsigned short *) pos
;
469 while (++i
<= video_num_columns
- nr
) {
470 scr_writew(scr_readw(p
+nr
), p
);
473 scr_memsetw(p
, video_erase_char
, nr
*2);
476 unsigned short oldattr
= attr
;
477 sw
->con_bmove(vc_cons
[currcons
].d
, y
, x
+nr
, y
, x
, 1,
478 video_num_columns
-x
-nr
);
479 attr
= video_erase_char
>> 8;
481 sw
->con_putc(vc_cons
[currcons
].d
,
483 video_num_columns
-1-nr
);
488 static int softcursor_original
;
490 static void add_softcursor(int currcons
)
492 int i
= scr_readw((u16
*) pos
);
493 u32 type
= cursor_type
;
495 if (! (type
& 0x10)) return;
496 if (softcursor_original
!= -1) return;
497 softcursor_original
= i
;
498 i
|= ((type
>> 8) & 0xff00 );
499 i
^= ((type
) & 0xff00 );
500 if ((type
& 0x20) && ((softcursor_original
& 0x7000) == (i
& 0x7000))) i
^= 0x7000;
501 if ((type
& 0x40) && ((i
& 0x700) == ((i
& 0x7000) >> 4))) i
^= 0x0700;
502 scr_writew(i
, (u16
*) pos
);
504 sw
->con_putc(vc_cons
[currcons
].d
, i
, y
, x
);
507 static void hide_cursor(int currcons
)
509 if (currcons
== sel_cons
)
511 if (softcursor_original
!= -1) {
512 scr_writew(softcursor_original
,(u16
*) pos
);
514 sw
->con_putc(vc_cons
[currcons
].d
, softcursor_original
, y
, x
);
515 softcursor_original
= -1;
517 sw
->con_cursor(vc_cons
[currcons
].d
,CM_ERASE
);
520 static void set_cursor(int currcons
)
522 if (!IS_FG
|| console_blanked
|| vcmode
== KD_GRAPHICS
)
525 if (currcons
== sel_cons
)
527 add_softcursor(currcons
);
528 if ((cursor_type
& 0x0f) != 1)
529 sw
->con_cursor(vc_cons
[currcons
].d
,CM_DRAW
);
531 hide_cursor(currcons
);
534 static void set_origin(int currcons
)
537 !sw
->con_set_origin
||
538 !sw
->con_set_origin(vc_cons
[currcons
].d
))
539 origin
= (unsigned long) screenbuf
;
540 visible_origin
= origin
;
541 scr_end
= origin
+ screenbuf_size
;
542 pos
= origin
+ video_size_row
*y
+ 2*x
;
545 static inline void save_screen(int currcons
)
547 if (sw
->con_save_screen
)
548 sw
->con_save_screen(vc_cons
[currcons
].d
);
552 * Redrawing of screen
555 void redraw_screen(int new_console
, int is_switch
)
558 int currcons
, old_console
;
560 if (!vc_cons_allocated(new_console
)) {
562 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
567 currcons
= fg_console
;
568 hide_cursor(currcons
);
569 if (fg_console
!= new_console
) {
570 struct vc_data
**display
= vc_cons
[new_console
].d
->vc_display_fg
;
571 old_console
= (*display
) ? (*display
)->vc_num
: fg_console
;
572 *display
= vc_cons
[new_console
].d
;
573 fg_console
= new_console
;
574 currcons
= old_console
;
576 save_screen(currcons
);
577 set_origin(currcons
);
579 currcons
= new_console
;
580 if (old_console
== new_console
)
584 currcons
= new_console
;
585 hide_cursor(currcons
);
590 set_origin(currcons
);
591 update
= sw
->con_switch(vc_cons
[currcons
].d
);
592 set_palette(currcons
);
593 if (update
&& vcmode
!= KD_GRAPHICS
)
594 do_update_region(currcons
, origin
, screenbuf_size
/2);
596 set_cursor(currcons
);
599 compute_shiftstate();
604 * Allocation, freeing and resizing of VTs.
607 int vc_cons_allocated(unsigned int i
)
609 return (i
< MAX_NR_CONSOLES
&& vc_cons
[i
].d
);
612 static void visual_init(int currcons
, int init
)
614 /* ++Geert: sw->con_init determines console size */
616 #ifndef VT_SINGLE_DRIVER
617 if (con_driver_map
[currcons
])
618 sw
= con_driver_map
[currcons
];
621 display_fg
= &master_display_fg
;
622 vc_cons
[currcons
].d
->vc_uni_pagedir_loc
= &vc_cons
[currcons
].d
->vc_uni_pagedir
;
623 vc_cons
[currcons
].d
->vc_uni_pagedir
= 0;
627 sw
->con_init(vc_cons
[currcons
].d
, init
);
628 if (!complement_mask
)
629 complement_mask
= can_do_color
? 0x7700 : 0x0800;
630 s_complement_mask
= complement_mask
;
631 video_size_row
= video_num_columns
<<1;
632 screenbuf_size
= video_num_lines
*video_size_row
;
635 int vc_allocate(unsigned int currcons
) /* return 0 on success */
637 if (currcons
>= MAX_NR_CONSOLES
)
639 if (!vc_cons
[currcons
].d
) {
642 /* prevent users from taking too much memory */
643 if (currcons
>= MAX_NR_USER_CONSOLES
&& !capable(CAP_SYS_RESOURCE
))
646 /* due to the granularity of kmalloc, we waste some memory here */
647 /* the alloc is done in two steps, to optimize the common situation
648 of a 25x80 console (structsize=216, screenbuf_size=4000) */
649 /* although the numbers above are not valid since long ago, the
650 point is still up-to-date and the comment still has its value
651 even if only as a historical artifact. --mj, July 1998 */
652 p
= (long) kmalloc(structsize
, GFP_KERNEL
);
655 vc_cons
[currcons
].d
= (struct vc_data
*)p
;
656 vt_cons
[currcons
] = (struct vt_struct
*)(p
+sizeof(struct vc_data
));
657 visual_init(currcons
, 1);
658 if (!*vc_cons
[currcons
].d
->vc_uni_pagedir_loc
)
659 con_set_default_unimap(currcons
);
660 q
= (long)kmalloc(screenbuf_size
, GFP_KERNEL
);
663 vc_cons
[currcons
].d
= NULL
;
664 vt_cons
[currcons
] = NULL
;
667 screenbuf
= (unsigned short *) q
;
669 vc_init(currcons
, video_num_lines
, video_num_columns
, 1);
672 pm_con
= pm_register(PM_SYS_DEV
,
681 * Change # of rows and columns (0 means unchanged/the size of fg_console)
682 * [this is to be used together with some user program
683 * like resize that changes the hardware videomode]
685 int vc_resize(unsigned int lines
, unsigned int cols
,
686 unsigned int first
, unsigned int last
)
688 unsigned int cc
, ll
, ss
, sr
, todo
= 0;
689 unsigned int currcons
= fg_console
, i
;
690 unsigned short *newscreens
[MAX_NR_CONSOLES
];
692 cc
= (cols
? cols
: video_num_columns
);
693 ll
= (lines
? lines
: video_num_lines
);
697 for (currcons
= first
; currcons
<= last
; currcons
++) {
698 if (!vc_cons_allocated(currcons
) ||
699 (cc
== video_num_columns
&& ll
== video_num_lines
))
700 newscreens
[currcons
] = NULL
;
702 unsigned short *p
= (unsigned short *) kmalloc(ss
, GFP_USER
);
704 for (i
= first
; i
< currcons
; i
++)
706 kfree(newscreens
[i
]);
709 newscreens
[currcons
] = p
;
716 for (currcons
= first
; currcons
<= last
; currcons
++) {
717 unsigned int occ
, oll
, oss
, osr
;
718 unsigned long ol
, nl
, nlend
, rlth
, rrem
;
719 if (!newscreens
[currcons
] || !vc_cons_allocated(currcons
))
722 oll
= video_num_lines
;
723 occ
= video_num_columns
;
724 osr
= video_size_row
;
725 oss
= screenbuf_size
;
727 video_num_lines
= ll
;
728 video_num_columns
= cc
;
735 nl
= (long) newscreens
[currcons
];
738 ol
+= (oll
- ll
) * osr
;
740 update_attr(currcons
);
742 while (ol
< scr_end
) {
743 scr_memcpyw((unsigned short *) nl
, (unsigned short *) ol
, rlth
);
745 scr_memsetw((void *)(nl
+ rlth
), video_erase_char
, rrem
);
750 scr_memsetw((void *) nl
, video_erase_char
, nlend
- nl
);
753 screenbuf
= newscreens
[currcons
];
756 set_origin(currcons
);
758 /* do part of a reset_terminal() */
760 bottom
= video_num_lines
;
761 gotoxy(currcons
, x
, y
);
764 if (console_table
[currcons
]) {
765 struct winsize ws
, *cws
= &console_table
[currcons
]->winsize
;
766 memset(&ws
, 0, sizeof(ws
));
767 ws
.ws_row
= video_num_lines
;
768 ws
.ws_col
= video_num_columns
;
769 if ((ws
.ws_row
!= cws
->ws_row
|| ws
.ws_col
!= cws
->ws_col
) &&
770 console_table
[currcons
]->pgrp
> 0)
771 kill_pg(console_table
[currcons
]->pgrp
, SIGWINCH
, 1);
776 update_screen(currcons
);
783 void vc_disallocate(unsigned int currcons
)
785 if (vc_cons_allocated(currcons
)) {
786 sw
->con_deinit(vc_cons
[currcons
].d
);
789 if (currcons
>= MIN_NR_CONSOLES
)
790 kfree(vc_cons
[currcons
].d
);
791 vc_cons
[currcons
].d
= NULL
;
799 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
800 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
801 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
803 #define decarm VC_REPEAT
804 #define decckm VC_CKMODE
805 #define kbdapplic VC_APPLIC
809 * this is what the terminal answers to a ESC-Z or csi0c query.
811 #define VT100ID "\033[?1;2c"
812 #define VT102ID "\033[?6c"
814 unsigned char color_table
[] = { 0, 4, 2, 6, 1, 5, 3, 7,
815 8,12,10,14, 9,13,11,15 };
817 /* the default colour table, for VGA+ colour systems */
818 int default_red
[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
819 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
820 int default_grn
[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
821 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
822 int default_blu
[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
823 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
826 * gotoxy() must verify all boundaries, because the arguments
827 * might also be negative. If the given position is out of
828 * bounds, the cursor is placed at the nearest margin.
830 static void gotoxy(int currcons
, int new_x
, int new_y
)
837 if (new_x
>= video_num_columns
)
838 x
= video_num_columns
- 1;
846 max_y
= video_num_lines
;
850 else if (new_y
>= max_y
)
854 pos
= origin
+ y
*video_size_row
+ (x
<<1);
858 /* for absolute user moves, when decom is set */
859 static void gotoxay(int currcons
, int new_x
, int new_y
)
861 gotoxy(currcons
, new_x
, decom
? (top
+new_y
) : new_y
);
864 void scrollback(int lines
)
866 int currcons
= fg_console
;
869 lines
= video_num_lines
/2;
873 void scrollfront(int lines
)
875 int currcons
= fg_console
;
878 lines
= video_num_lines
/2;
882 static void lf(int currcons
)
884 /* don't scroll if above bottom of scrolling region, or
885 * if below scrolling region
888 scrup(currcons
,top
,bottom
,1);
889 else if (y
< video_num_lines
-1) {
891 pos
+= video_size_row
;
896 static void ri(int currcons
)
898 /* don't scroll if below top of scrolling region, or
899 * if above scrolling region
902 scrdown(currcons
,top
,bottom
,1);
905 pos
-= video_size_row
;
910 static inline void cr(int currcons
)
916 static inline void bs(int currcons
)
925 static inline void del(int currcons
)
930 static void csi_J(int currcons
, int vpar
)
933 unsigned short * start
;
936 case 0: /* erase from cursor to end of display */
937 count
= (scr_end
-pos
)>>1;
938 start
= (unsigned short *) pos
;
940 /* do in two stages */
941 sw
->con_clear(vc_cons
[currcons
].d
, y
, x
, 1,
942 video_num_columns
-x
);
943 sw
->con_clear(vc_cons
[currcons
].d
, y
+1, 0,
948 case 1: /* erase from start to cursor */
949 count
= ((pos
-origin
)>>1)+1;
950 start
= (unsigned short *) origin
;
952 /* do in two stages */
953 sw
->con_clear(vc_cons
[currcons
].d
, 0, 0, y
,
955 sw
->con_clear(vc_cons
[currcons
].d
, y
, 0, 1,
959 case 2: /* erase whole display */
960 count
= video_num_columns
* video_num_lines
;
961 start
= (unsigned short *) origin
;
963 sw
->con_clear(vc_cons
[currcons
].d
, 0, 0,
970 scr_memsetw(start
, video_erase_char
, 2*count
);
974 static void csi_K(int currcons
, int vpar
)
977 unsigned short * start
;
980 case 0: /* erase from cursor to end of line */
981 count
= video_num_columns
-x
;
982 start
= (unsigned short *) pos
;
984 sw
->con_clear(vc_cons
[currcons
].d
, y
, x
, 1,
985 video_num_columns
-x
);
987 case 1: /* erase from start of line to cursor */
988 start
= (unsigned short *) (pos
- (x
<<1));
991 sw
->con_clear(vc_cons
[currcons
].d
, y
, 0, 1,
994 case 2: /* erase whole line */
995 start
= (unsigned short *) (pos
- (x
<<1));
996 count
= video_num_columns
;
998 sw
->con_clear(vc_cons
[currcons
].d
, y
, 0, 1,
1004 scr_memsetw(start
, video_erase_char
, 2 * count
);
1008 static void csi_X(int currcons
, int vpar
) /* erase the following vpar positions */
1014 count
= (vpar
> video_num_columns
-x
) ? (video_num_columns
-x
) : vpar
;
1016 scr_memsetw((unsigned short *) pos
, video_erase_char
, 2 * count
);
1018 sw
->con_clear(vc_cons
[currcons
].d
, y
, x
, 1, count
);
1022 static void default_attr(int currcons
)
1031 static void csi_m(int currcons
)
1035 for (i
=0;i
<=npar
;i
++)
1037 case 0: /* all attributes off */
1038 default_attr(currcons
);
1055 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1056 * Select primary font, don't display
1057 * control chars if defined, don't set
1060 translate
= set_translate(charset
== 0
1062 : G1_charset
,currcons
);
1066 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1067 * Select first alternate font, lets
1068 * chars < 32 be displayed as ROM chars.
1070 translate
= set_translate(IBMPC_MAP
,currcons
);
1074 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1075 * Select second alternate font, toggle
1076 * high bit before displaying as ROM char.
1078 translate
= set_translate(IBMPC_MAP
,currcons
);
1095 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1096 * Enables underscore, white foreground
1097 * with white underscore (Linux - use
1098 * default foreground).
1100 color
= (def_color
& 0x0f) | background
;
1103 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1104 * Disable underline option.
1105 * Reset colour to default? It did this
1108 color
= (def_color
& 0x0f) | background
;
1112 color
= (def_color
& 0xf0) | foreground
;
1115 if (par
[i
] >= 30 && par
[i
] <= 37)
1116 color
= color_table
[par
[i
]-30]
1118 else if (par
[i
] >= 40 && par
[i
] <= 47)
1119 color
= (color_table
[par
[i
]-40]<<4)
1123 update_attr(currcons
);
1126 static void respond_string(const char * p
, struct tty_struct
* tty
)
1129 tty_insert_flip_char(tty
, *p
, 0);
1132 con_schedule_flip(tty
);
1135 static void cursor_report(int currcons
, struct tty_struct
* tty
)
1139 sprintf(buf
, "\033[%d;%dR", y
+ (decom
? top
+1 : 1), x
+1);
1140 respond_string(buf
, tty
);
1143 static inline void status_report(struct tty_struct
* tty
)
1145 respond_string("\033[0n", tty
); /* Terminal ok */
1148 static inline void respond_ID(struct tty_struct
* tty
)
1150 respond_string(VT102ID
, tty
);
1153 void mouse_report(struct tty_struct
* tty
, int butt
, int mrx
, int mry
)
1157 sprintf(buf
, "\033[M%c%c%c", (char)(' ' + butt
), (char)('!' + mrx
),
1159 respond_string(buf
, tty
);
1162 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1163 int mouse_reporting(void)
1165 int currcons
= fg_console
;
1167 return report_mouse
;
1170 static void set_mode(int currcons
, int on_off
)
1174 for (i
=0; i
<=npar
; i
++)
1175 if (ques
) switch(par
[i
]) { /* DEC private modes set/reset */
1176 case 1: /* Cursor keys send ^[Ox/^[[x */
1182 case 3: /* 80/132 mode switch unimplemented */
1185 (void) vc_resize(video_num_lines
, deccolm
? 132 : 80);
1186 /* this alone does not suffice; some user mode
1187 utility has to change the hardware regs */
1190 case 5: /* Inverted screen on/off */
1191 if (decscnm
!= on_off
) {
1193 invert_screen(currcons
, 0, screenbuf_size
, 0);
1194 update_attr(currcons
);
1197 case 6: /* Origin relative/absolute */
1199 gotoxay(currcons
,0,0);
1201 case 7: /* Autowrap on/off */
1204 case 8: /* Autorepeat on/off */
1211 report_mouse
= on_off
? 1 : 0;
1213 case 25: /* Cursor on/off */
1217 report_mouse
= on_off
? 2 : 0;
1219 } else switch(par
[i
]) { /* ANSI modes set/reset */
1220 case 3: /* Monitor (display ctrls) */
1223 case 4: /* Insert Mode on/off */
1226 case 20: /* Lf, Enter == CrLf/Lf */
1235 static void setterm_command(int currcons
)
1238 case 1: /* set color for underline mode */
1239 if (can_do_color
&& par
[1] < 16) {
1240 ulcolor
= color_table
[par
[1]];
1242 update_attr(currcons
);
1245 case 2: /* set color for half intensity mode */
1246 if (can_do_color
&& par
[1] < 16) {
1247 halfcolor
= color_table
[par
[1]];
1249 update_attr(currcons
);
1252 case 8: /* store colors as defaults */
1254 if (hi_font_mask
== 0x100)
1256 default_attr(currcons
);
1257 update_attr(currcons
);
1259 case 9: /* set blanking interval */
1260 blankinterval
= ((par
[1] < 60) ? par
[1] : 60) * 60 * HZ
;
1261 poke_blanked_console();
1263 case 10: /* set bell frequency in Hz */
1265 bell_pitch
= par
[1];
1267 bell_pitch
= DEFAULT_BELL_PITCH
;
1269 case 11: /* set bell duration in msec */
1271 bell_duration
= (par
[1] < 2000) ?
1274 bell_duration
= DEFAULT_BELL_DURATION
;
1276 case 12: /* bring specified console to the front */
1277 if (par
[1] >= 1 && vc_cons_allocated(par
[1]-1))
1278 set_console(par
[1] - 1);
1280 case 13: /* unblank the screen */
1281 poke_blanked_console();
1283 case 14: /* set vesa powerdown interval */
1284 vesa_off_interval
= ((par
[1] < 60) ? par
[1] : 60) * 60 * HZ
;
1289 static void insert_line(int currcons
, unsigned int nr
)
1291 scrdown(currcons
,y
,bottom
,nr
);
1296 static void delete_line(int currcons
, unsigned int nr
)
1298 scrup(currcons
,y
,bottom
,nr
);
1302 static void csi_at(int currcons
, unsigned int nr
)
1304 if (nr
> video_num_columns
- x
)
1305 nr
= video_num_columns
- x
;
1308 insert_char(currcons
, nr
);
1311 static void csi_L(int currcons
, unsigned int nr
)
1313 if (nr
> video_num_lines
- y
)
1314 nr
= video_num_lines
- y
;
1317 insert_line(currcons
, nr
);
1320 static void csi_P(int currcons
, unsigned int nr
)
1322 if (nr
> video_num_columns
- x
)
1323 nr
= video_num_columns
- x
;
1326 delete_char(currcons
, nr
);
1329 static void csi_M(int currcons
, unsigned int nr
)
1331 if (nr
> video_num_lines
- y
)
1332 nr
= video_num_lines
- y
;
1335 delete_line(currcons
, nr
);
1338 static void save_cur(int currcons
)
1342 s_intensity
= intensity
;
1343 s_underline
= underline
;
1345 s_reverse
= reverse
;
1346 s_charset
= charset
;
1348 saved_G0
= G0_charset
;
1349 saved_G1
= G1_charset
;
1352 static void restore_cur(int currcons
)
1354 gotoxy(currcons
,saved_x
,saved_y
);
1355 intensity
= s_intensity
;
1356 underline
= s_underline
;
1358 reverse
= s_reverse
;
1359 charset
= s_charset
;
1361 G0_charset
= saved_G0
;
1362 G1_charset
= saved_G1
;
1363 translate
= set_translate(charset
? G1_charset
: G0_charset
,currcons
);
1364 update_attr(currcons
);
1368 enum { ESnormal
, ESesc
, ESsquare
, ESgetpars
, ESgotpars
, ESfunckey
,
1369 EShash
, ESsetG0
, ESsetG1
, ESpercent
, ESignore
, ESnonstd
,
1372 static void reset_terminal(int currcons
, int do_clear
)
1375 bottom
= video_num_lines
;
1376 vc_state
= ESnormal
;
1378 translate
= set_translate(LAT1_MAP
,currcons
);
1379 G0_charset
= LAT1_MAP
;
1380 G1_charset
= GRAF_MAP
;
1400 kbd_table
[currcons
].lockstate
= 0;
1401 kbd_table
[currcons
].slockstate
= 0;
1402 kbd_table
[currcons
].ledmode
= LED_SHOW_FLAGS
;
1403 kbd_table
[currcons
].ledflagstate
= kbd_table
[currcons
].default_ledflagstate
;
1406 cursor_type
= CUR_DEFAULT
;
1407 complement_mask
= s_complement_mask
;
1409 default_attr(currcons
);
1410 update_attr(currcons
);
1412 tab_stop
[0] = 0x01010100;
1416 tab_stop
[4] = 0x01010101;
1418 bell_pitch
= DEFAULT_BELL_PITCH
;
1419 bell_duration
= DEFAULT_BELL_DURATION
;
1421 gotoxy(currcons
,0,0);
1427 static void do_con_trol(struct tty_struct
*tty
, unsigned int currcons
, int c
)
1430 * Control characters can be used in the _middle_
1431 * of an escape sequence.
1438 kd_mksound(bell_pitch
, bell_duration
);
1445 while (x
< video_num_columns
- 1) {
1447 if (tab_stop
[x
>> 5] & (1 << (x
& 31)))
1452 case 10: case 11: case 12:
1461 translate
= set_translate(G1_charset
,currcons
);
1466 translate
= set_translate(G0_charset
,currcons
);
1470 vc_state
= ESnormal
;
1479 vc_state
= ESsquare
;
1484 vc_state
= ESnormal
;
1487 vc_state
= ESsquare
;
1490 vc_state
= ESnonstd
;
1493 vc_state
= ESpercent
;
1506 tab_stop
[x
>> 5] |= (1 << (x
& 31));
1515 restore_cur(currcons
);
1527 reset_terminal(currcons
,1);
1529 case '>': /* Numeric keypad */
1532 case '=': /* Appl. keypad */
1538 if (c
=='P') { /* palette escape sequence */
1539 for (npar
=0; npar
<NPAR
; npar
++)
1542 vc_state
= ESpalette
;
1544 } else if (c
=='R') { /* reset palette */
1545 reset_palette(currcons
);
1546 vc_state
= ESnormal
;
1548 vc_state
= ESnormal
;
1551 if ( (c
>='0'&&c
<='9') || (c
>='A'&&c
<='F') || (c
>='a'&&c
<='f') ) {
1552 par
[npar
++] = (c
>'9' ? (c
&0xDF)-'A'+10 : c
-'0') ;
1554 int i
= par
[0]*3, j
= 1;
1555 palette
[i
] = 16*par
[j
++];
1556 palette
[i
++] += par
[j
++];
1557 palette
[i
] = 16*par
[j
++];
1558 palette
[i
++] += par
[j
++];
1559 palette
[i
] = 16*par
[j
++];
1560 palette
[i
] += par
[j
];
1561 set_palette(currcons
);
1562 vc_state
= ESnormal
;
1565 vc_state
= ESnormal
;
1568 for(npar
= 0 ; npar
< NPAR
; npar
++)
1571 vc_state
= ESgetpars
;
1572 if (c
== '[') { /* Function key */
1580 if (c
==';' && npar
<NPAR
-1) {
1583 } else if (c
>='0' && c
<='9') {
1587 } else vc_state
=ESgotpars
;
1589 vc_state
= ESnormal
;
1592 set_mode(currcons
,1);
1595 set_mode(currcons
,0);
1600 cursor_type
= par
[0] | (par
[1]<<8) | (par
[2]<<16);
1602 cursor_type
= CUR_DEFAULT
;
1610 complement_mask
= par
[0]<<8 | par
[1];
1612 complement_mask
= s_complement_mask
;
1620 else if (par
[0] == 6)
1621 cursor_report(currcons
,tty
);
1631 if (par
[0]) par
[0]--;
1632 gotoxy(currcons
,par
[0],y
);
1635 if (!par
[0]) par
[0]++;
1636 gotoxy(currcons
,x
,y
-par
[0]);
1639 if (!par
[0]) par
[0]++;
1640 gotoxy(currcons
,x
,y
+par
[0]);
1643 if (!par
[0]) par
[0]++;
1644 gotoxy(currcons
,x
+par
[0],y
);
1647 if (!par
[0]) par
[0]++;
1648 gotoxy(currcons
,x
-par
[0],y
);
1651 if (!par
[0]) par
[0]++;
1652 gotoxy(currcons
,0,y
+par
[0]);
1655 if (!par
[0]) par
[0]++;
1656 gotoxy(currcons
,0,y
-par
[0]);
1659 if (par
[0]) par
[0]--;
1660 gotoxay(currcons
,x
,par
[0]);
1663 if (par
[0]) par
[0]--;
1664 if (par
[1]) par
[1]--;
1665 gotoxay(currcons
,par
[1],par
[0]);
1668 csi_J(currcons
,par
[0]);
1671 csi_K(currcons
,par
[0]);
1674 csi_L(currcons
,par
[0]);
1677 csi_M(currcons
,par
[0]);
1680 csi_P(currcons
,par
[0]);
1688 tab_stop
[x
>> 5] &= ~(1 << (x
& 31));
1689 else if (par
[0] == 3) {
1700 case 'q': /* DECLL - but only 3 leds */
1701 /* map 0,1,2,3 to 0,1,2,4 */
1703 setledstate(kbd_table
+ currcons
,
1704 (par
[0] < 3) ? par
[0] : 4);
1710 par
[1] = video_num_lines
;
1711 /* Minimum allowed region is 2 lines */
1712 if (par
[0] < par
[1] &&
1713 par
[1] <= video_num_lines
) {
1716 gotoxay(currcons
,0,0);
1723 restore_cur(currcons
);
1726 csi_X(currcons
, par
[0]);
1729 csi_at(currcons
,par
[0]);
1731 case ']': /* setterm functions */
1732 setterm_command(currcons
);
1737 vc_state
= ESnormal
;
1739 case '@': /* defined in ISO 2022 */
1742 case 'G': /* prelim official escape code */
1743 case '8': /* retained for compatibility */
1749 vc_state
= ESnormal
;
1752 vc_state
= ESnormal
;
1754 /* DEC screen alignment test. kludge :-) */
1756 (video_erase_char
& 0xff00) | 'E';
1759 (video_erase_char
& 0xff00) | ' ';
1760 do_update_region(currcons
, origin
, screenbuf_size
/2);
1765 G0_charset
= GRAF_MAP
;
1767 G0_charset
= LAT1_MAP
;
1769 G0_charset
= IBMPC_MAP
;
1771 G0_charset
= USER_MAP
;
1773 translate
= set_translate(G0_charset
,currcons
);
1774 vc_state
= ESnormal
;
1778 G1_charset
= GRAF_MAP
;
1780 G1_charset
= LAT1_MAP
;
1782 G1_charset
= IBMPC_MAP
;
1784 G1_charset
= USER_MAP
;
1786 translate
= set_translate(G1_charset
,currcons
);
1787 vc_state
= ESnormal
;
1790 vc_state
= ESnormal
;
1794 /* This is a temporary buffer used to prepare a tty console write
1795 * so that we can easily avoid touching user space while holding the
1796 * console spinlock. It is allocated in con_init and is shared by
1797 * this code and the vc_screen read/write tty calls.
1799 * We have to allocate this statically in the kernel data section
1800 * since console_init (and thus con_init) are called before any
1801 * kernel memory allocation is available.
1803 char con_buf
[PAGE_SIZE
];
1804 #define CON_BUF_SIZE PAGE_SIZE
1805 DECLARE_MUTEX(con_buf_sem
);
1807 static int do_con_write(struct tty_struct
* tty
, int from_user
,
1808 const unsigned char *buf
, int count
)
1810 #ifdef VT_BUF_VRAM_ONLY
1811 #define FLUSH do { } while(0);
1813 #define FLUSH if (draw_x >= 0) { \
1814 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1819 int c
, tc
, ok
, n
= 0, draw_x
= -1;
1820 unsigned int currcons
;
1821 unsigned long draw_from
= 0, draw_to
= 0;
1822 struct vt_struct
*vt
= (struct vt_struct
*)tty
->driver_data
;
1823 u16 himask
, charmask
;
1824 const unsigned char *orig_buf
= NULL
;
1827 currcons
= vt
->vc_num
;
1828 if (!vc_cons_allocated(currcons
)) {
1829 /* could this happen? */
1830 static int error
= 0;
1833 printk("con_write: tty %d not allocated\n", currcons
+1);
1845 if (count
> CON_BUF_SIZE
)
1846 count
= CON_BUF_SIZE
;
1847 if (copy_from_user(con_buf
, buf
, count
)) {
1848 n
= 0; /* ?? are error codes legal here ?? */
1855 /* At this point 'buf' is guarenteed to be a kernel buffer
1856 * and therefore no access to userspace (and therefore sleeping)
1857 * will be needed. The con_buf_sem serializes all tty based
1858 * console rendering and vcs write/read operations. We hold
1859 * the console spinlock during the entire write.
1862 spin_lock_irq(&console_lock
);
1864 himask
= hi_font_mask
;
1865 charmask
= himask
? 0x1ff : 0xff;
1867 /* undraw cursor first */
1869 hide_cursor(currcons
);
1871 while (!tty
->stopped
&& count
) {
1878 /* Combine UTF-8 into Unicode */
1879 /* Incomplete characters silently ignored */
1881 if (utf_count
> 0 && (c
& 0xc0) == 0x80) {
1882 utf_char
= (utf_char
<< 6) | (c
& 0x3f);
1888 if ((c
& 0xe0) == 0xc0) {
1890 utf_char
= (c
& 0x1f);
1891 } else if ((c
& 0xf0) == 0xe0) {
1893 utf_char
= (c
& 0x0f);
1894 } else if ((c
& 0xf8) == 0xf0) {
1896 utf_char
= (c
& 0x07);
1897 } else if ((c
& 0xfc) == 0xf8) {
1899 utf_char
= (c
& 0x03);
1900 } else if ((c
& 0xfe) == 0xfc) {
1902 utf_char
= (c
& 0x01);
1911 } else { /* no utf */
1912 tc
= translate
[toggle_meta
? (c
|0x80) : c
];
1915 /* If the original code was a control character we
1916 * only allow a glyph to be displayed if the code is
1917 * not normally used (such as for cursor movement) or
1918 * if the disp_ctrl mode has been explicitly enabled.
1919 * Certain characters (as given by the CTRL_ALWAYS
1920 * bitmap) are always displayed as control characters,
1921 * as the console would be pretty useless without
1922 * them; to display an arbitrary font position use the
1923 * direct-to-font zone in UTF-8 mode.
1925 ok
= tc
&& (c
>= 32 ||
1926 (!utf
&& !(((disp_ctrl
? CTRL_ALWAYS
1927 : CTRL_ACTION
) >> c
) & 1)))
1928 && (c
!= 127 || disp_ctrl
)
1931 if (vc_state
== ESnormal
&& ok
) {
1932 /* Now try to find out how to display it */
1933 tc
= conv_uni_to_pc(vc_cons
[currcons
].d
, tc
);
1935 /* If we got -4 (not found) then see if we have
1936 defined a replacement character (U+FFFD) */
1937 tc
= conv_uni_to_pc(vc_cons
[currcons
].d
, 0xfffd);
1939 /* One reason for the -4 can be that we just
1940 did a clear_unimap();
1941 try at least to show something. */
1944 } else if ( tc
== -3 ) {
1945 /* Bad hash table -- hope for the best */
1949 continue; /* Conversion failed */
1951 if (need_wrap
|| decim
)
1958 insert_char(currcons
, 1);
1960 ((attr
<< 8) & ~himask
) + ((tc
& 0x100) ? himask
: 0) + (tc
& 0xff) :
1963 if (DO_UPDATE
&& draw_x
< 0) {
1967 if (x
== video_num_columns
- 1) {
1977 do_con_trol(tty
, currcons
, c
);
1980 spin_unlock_irq(&console_lock
);
1984 /* If the user requested something larger than
1985 * the CON_BUF_SIZE, and the tty is not stopped,
1988 if ((orig_count
> CON_BUF_SIZE
) && !tty
->stopped
) {
1989 orig_count
-= CON_BUF_SIZE
;
1990 orig_buf
+= CON_BUF_SIZE
;
2004 * This is the console switching tasklet.
2006 * Doing console switching in a tasklet allows
2007 * us to do the switches asynchronously (needed when we want
2008 * to switch due to a keyboard interrupt). Synchronization
2009 * with other console code and prevention of re-entrancy is
2010 * ensured with console_lock.
2012 static void console_softint(unsigned long ignored
)
2014 /* Runs the task queue outside of the console lock. These
2015 * callbacks can come back into the console code and thus
2016 * will perform their own locking.
2018 run_task_queue(&con_task_queue
);
2020 spin_lock_irq(&console_lock
);
2022 if (want_console
>= 0) {
2023 if (want_console
!= fg_console
&& vc_cons_allocated(want_console
)) {
2024 hide_cursor(fg_console
);
2025 change_console(want_console
);
2026 /* we only changed when the console had already
2027 been allocated - a new console is not created
2028 in an interrupt routine */
2032 if (do_poke_blanked_console
) { /* do not unblank for a LED change */
2033 do_poke_blanked_console
= 0;
2034 poke_blanked_console();
2036 if (scrollback_delta
) {
2037 int currcons
= fg_console
;
2039 if (vcmode
== KD_TEXT
)
2040 sw
->con_scrolldelta(vc_cons
[currcons
].d
, scrollback_delta
);
2041 scrollback_delta
= 0;
2044 spin_unlock_irq(&console_lock
);
2047 #ifdef CONFIG_VT_CONSOLE
2050 * Console on virtual terminal
2052 * The console_lock must be held when we get here.
2055 void vt_console_print(struct console
*co
, const char * b
, unsigned count
)
2057 int currcons
= fg_console
;
2059 static unsigned long printing
;
2060 const ushort
*start
;
2064 /* console busy or not yet initialized */
2065 if (!printable
|| test_and_set_bit(0, &printing
))
2070 if (kmsg_redirect
&& vc_cons_allocated(kmsg_redirect
- 1))
2071 currcons
= kmsg_redirect
- 1;
2073 /* read `x' only after setting currecons properly (otherwise
2074 the `x' macro will read the x of the foreground console). */
2077 if (!vc_cons_allocated(currcons
)) {
2079 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2083 if (vcmode
!= KD_TEXT
)
2086 /* undraw cursor first */
2088 hide_cursor(currcons
);
2090 start
= (ushort
*)pos
;
2092 /* Contrived structure to try to emulate original need_wrap behaviour
2093 * Problems caused when we have need_wrap set on '\n' character */
2096 if (c
== 10 || c
== 13 || c
== 8 || need_wrap
) {
2099 sw
->con_putcs(vc_cons
[currcons
].d
, start
, cnt
, y
, x
);
2105 if (c
== 8) { /* backspace */
2107 start
= (ushort
*)pos
;
2114 start
= (ushort
*)pos
;
2116 if (c
== 10 || c
== 13)
2119 scr_writew((attr
<< 8) + c
, (unsigned short *) pos
);
2121 if (myx
== video_num_columns
- 1) {
2130 sw
->con_putcs(vc_cons
[currcons
].d
, start
, cnt
, y
, x
);
2132 if (x
== video_num_columns
) {
2137 set_cursor(currcons
);
2140 clear_bit(0, &printing
);
2143 static kdev_t
vt_console_device(struct console
*c
)
2145 return MKDEV(TTY_MAJOR
, c
->index
? c
->index
: fg_console
+ 1);
2148 struct console vt_console_driver
= {
2150 write
: vt_console_print
,
2151 device
: vt_console_device
,
2152 wait_key
: keyboard_wait_for_keypress
,
2153 unblank
: unblank_screen
,
2154 flags
: CON_PRINTBUFFER
,
2160 * Handling of Linux-specific VC ioctls
2163 int tioclinux(struct tty_struct
*tty
, unsigned long arg
)
2167 if (tty
->driver
.type
!= TTY_DRIVER_TYPE_CONSOLE
)
2169 if (current
->tty
!= tty
&& !suser())
2171 if (get_user(type
, (char *)arg
))
2176 return set_selection(arg
, tty
, 1);
2178 return paste_selection(tty
);
2183 return sel_loadlut(arg
);
2187 * Make it possible to react to Shift+Mousebutton.
2188 * Note that 'shift_state' is an undocumented
2189 * kernel-internal variable; programs not closely
2190 * related to the kernel should not use this.
2193 return __put_user(data
, (char *) arg
);
2195 data
= mouse_reporting();
2196 return __put_user(data
, (char *) arg
);
2198 set_vesa_blanking(arg
);
2200 case 11: /* set kmsg redirect */
2203 if (get_user(data
, (char *)arg
+1))
2205 kmsg_redirect
= data
;
2207 case 12: /* get fg_console */
2214 * /dev/ttyN handling
2217 static int con_write(struct tty_struct
* tty
, int from_user
,
2218 const unsigned char *buf
, int count
)
2223 retval
= do_con_write(tty
, from_user
, buf
, count
);
2224 con_flush_chars(tty
);
2229 static void con_put_char(struct tty_struct
*tty
, unsigned char ch
)
2232 do_con_write(tty
, 0, &ch
, 1);
2235 static int con_write_room(struct tty_struct
*tty
)
2239 return 4096; /* No limit, really; we're not buffering */
2242 static int con_chars_in_buffer(struct tty_struct
*tty
)
2244 return 0; /* we're not buffering */
2248 * con_throttle and con_unthrottle are only used for
2249 * paste_selection(), which has to stuff in a large number of
2252 static void con_throttle(struct tty_struct
*tty
)
2256 static void con_unthrottle(struct tty_struct
*tty
)
2258 struct vt_struct
*vt
= (struct vt_struct
*) tty
->driver_data
;
2260 wake_up_interruptible(&vt
->paste_wait
);
2264 * Turn the Scroll-Lock LED on when the tty is stopped
2266 static void con_stop(struct tty_struct
*tty
)
2271 console_num
= MINOR(tty
->device
) - (tty
->driver
.minor_start
);
2272 if (!vc_cons_allocated(console_num
))
2274 set_vc_kbd_led(kbd_table
+ console_num
, VC_SCROLLOCK
);
2279 * Turn the Scroll-Lock LED off when the console is started
2281 static void con_start(struct tty_struct
*tty
)
2286 console_num
= MINOR(tty
->device
) - (tty
->driver
.minor_start
);
2287 if (!vc_cons_allocated(console_num
))
2289 clr_vc_kbd_led(kbd_table
+ console_num
, VC_SCROLLOCK
);
2293 static void con_flush_chars(struct tty_struct
*tty
)
2295 unsigned long flags
;
2296 struct vt_struct
*vt
= (struct vt_struct
*)tty
->driver_data
;
2299 spin_lock_irqsave(&console_lock
, flags
);
2300 set_cursor(vt
->vc_num
);
2301 spin_unlock_irqrestore(&console_lock
, flags
);
2305 * Allocate the console screen memory.
2307 static int con_open(struct tty_struct
*tty
, struct file
* filp
)
2309 unsigned int currcons
;
2312 currcons
= MINOR(tty
->device
) - tty
->driver
.minor_start
;
2314 i
= vc_allocate(currcons
);
2318 vt_cons
[currcons
]->vc_num
= currcons
;
2319 tty
->driver_data
= vt_cons
[currcons
];
2321 if (!tty
->winsize
.ws_row
&& !tty
->winsize
.ws_col
) {
2322 tty
->winsize
.ws_row
= video_num_lines
;
2323 tty
->winsize
.ws_col
= video_num_columns
;
2325 if (tty
->count
== 1)
2326 vcs_make_devfs (currcons
, 0);
2330 static void con_close(struct tty_struct
*tty
, struct file
* filp
)
2334 if (tty
->count
!= 1) return;
2335 vcs_make_devfs (MINOR (tty
->device
) - tty
->driver
.minor_start
, 1);
2336 tty
->driver_data
= 0;
2339 static void vc_init(unsigned int currcons
, unsigned int rows
, unsigned int cols
, int do_clear
)
2343 video_num_columns
= cols
;
2344 video_num_lines
= rows
;
2345 video_size_row
= cols
<<1;
2346 screenbuf_size
= video_num_lines
* video_size_row
;
2348 set_origin(currcons
);
2351 for (j
=k
=0; j
<16; j
++) {
2352 vc_cons
[currcons
].d
->vc_palette
[k
++] = default_red
[j
] ;
2353 vc_cons
[currcons
].d
->vc_palette
[k
++] = default_grn
[j
] ;
2354 vc_cons
[currcons
].d
->vc_palette
[k
++] = default_blu
[j
] ;
2356 def_color
= 0x07; /* white */
2357 ulcolor
= 0x0f; /* bold white */
2358 halfcolor
= 0x08; /* grey */
2359 init_waitqueue_head(&vt_cons
[currcons
]->paste_wait
);
2360 reset_terminal(currcons
, do_clear
);
2364 * This routine initializes console interrupts, and does nothing
2365 * else. If you want the screen to clear, call tty_write with
2366 * the appropriate escape-sequence.
2369 struct tty_driver console_driver
;
2370 static int console_refcount
;
2372 DECLARE_TASKLET_DISABLED(console_tasklet
, console_softint
, 0);
2374 void __init
con_init(void)
2376 const char *display_desc
= NULL
;
2377 unsigned int currcons
= 0;
2380 display_desc
= conswitchp
->con_startup();
2381 if (!display_desc
) {
2386 memset(&console_driver
, 0, sizeof(struct tty_driver
));
2387 console_driver
.magic
= TTY_DRIVER_MAGIC
;
2388 console_driver
.name
= "vc/%d";
2389 console_driver
.name_base
= 1;
2390 console_driver
.major
= TTY_MAJOR
;
2391 console_driver
.minor_start
= 1;
2392 console_driver
.num
= MAX_NR_CONSOLES
;
2393 console_driver
.type
= TTY_DRIVER_TYPE_CONSOLE
;
2394 console_driver
.init_termios
= tty_std_termios
;
2395 console_driver
.flags
= TTY_DRIVER_REAL_RAW
| TTY_DRIVER_RESET_TERMIOS
;
2396 /* Tell tty_register_driver() to skip consoles because they are
2397 * registered before kmalloc() is ready. We'll patch them in later.
2398 * See comments at console_init(); see also con_init_devfs().
2400 console_driver
.flags
|= TTY_DRIVER_NO_DEVFS
;
2401 console_driver
.refcount
= &console_refcount
;
2402 console_driver
.table
= console_table
;
2403 console_driver
.termios
= console_termios
;
2404 console_driver
.termios_locked
= console_termios_locked
;
2406 console_driver
.open
= con_open
;
2407 console_driver
.close
= con_close
;
2408 console_driver
.write
= con_write
;
2409 console_driver
.write_room
= con_write_room
;
2410 console_driver
.put_char
= con_put_char
;
2411 console_driver
.flush_chars
= con_flush_chars
;
2412 console_driver
.chars_in_buffer
= con_chars_in_buffer
;
2413 console_driver
.ioctl
= vt_ioctl
;
2414 console_driver
.stop
= con_stop
;
2415 console_driver
.start
= con_start
;
2416 console_driver
.throttle
= con_throttle
;
2417 console_driver
.unthrottle
= con_unthrottle
;
2419 if (tty_register_driver(&console_driver
))
2420 panic("Couldn't register console driver\n");
2422 init_timer(&console_timer
);
2423 console_timer
.function
= blank_screen
;
2424 if (blankinterval
) {
2425 mod_timer(&console_timer
, jiffies
+ blankinterval
);
2429 * kmalloc is not running yet - we use the bootmem allocator.
2431 for (currcons
= 0; currcons
< MIN_NR_CONSOLES
; currcons
++) {
2432 vc_cons
[currcons
].d
= (struct vc_data
*)
2433 alloc_bootmem(sizeof(struct vc_data
));
2434 vt_cons
[currcons
] = (struct vt_struct
*)
2435 alloc_bootmem(sizeof(struct vt_struct
));
2436 visual_init(currcons
, 1);
2437 screenbuf
= (unsigned short *) alloc_bootmem(screenbuf_size
);
2439 vc_init(currcons
, video_num_lines
, video_num_columns
,
2440 currcons
|| !sw
->con_save_screen
);
2442 currcons
= fg_console
= 0;
2443 master_display_fg
= vc_cons
[currcons
].d
;
2444 set_origin(currcons
);
2445 save_screen(currcons
);
2446 gotoxy(currcons
,x
,y
);
2448 update_screen(fg_console
);
2449 printk("Console: %s %s %dx%d",
2450 can_do_color
? "colour" : "mono",
2451 display_desc
, video_num_columns
, video_num_lines
);
2455 #ifdef CONFIG_VT_CONSOLE
2456 register_console(&vt_console_driver
);
2459 tasklet_enable(&console_tasklet
);
2460 tasklet_schedule(&console_tasklet
);
2463 #ifndef VT_SINGLE_DRIVER
2465 static void clear_buffer_attributes(int currcons
)
2467 unsigned short *p
= (unsigned short *) origin
;
2468 int count
= screenbuf_size
/2;
2469 int mask
= hi_font_mask
| 0xff;
2471 for (; count
> 0; count
--, p
++) {
2472 scr_writew((scr_readw(p
)&mask
) | (video_erase_char
&~mask
), p
);
2477 * If we support more console drivers, this function is used
2478 * when a driver wants to take over some existing consoles
2479 * and become default driver for newly opened ones.
2482 void take_over_console(const struct consw
*csw
, int first
, int last
, int deflt
)
2487 desc
= csw
->con_startup();
2492 for (i
= first
; i
<= last
; i
++) {
2496 con_driver_map
[i
] = csw
;
2498 if (!vc_cons
[i
].d
|| !vc_cons
[i
].d
->vc_sw
)
2504 old_was_color
= vc_cons
[i
].d
->vc_can_do_color
;
2505 vc_cons
[i
].d
->vc_sw
->con_deinit(vc_cons
[i
].d
);
2509 /* If the console changed between mono <-> color, then
2510 * the attributes in the screenbuf will be wrong. The
2511 * following resets all attributes to something sane.
2513 if (old_was_color
!= vc_cons
[i
].d
->vc_can_do_color
)
2514 clear_buffer_attributes(i
);
2519 printk("Console: switching ");
2521 printk("consoles %d-%d ", first
+1, last
+1);
2523 printk("to %s %s %dx%d\n",
2524 vc_cons
[j
].d
->vc_can_do_color
? "colour" : "mono",
2525 desc
, vc_cons
[j
].d
->vc_cols
, vc_cons
[j
].d
->vc_rows
);
2527 printk("to %s\n", desc
);
2530 void give_up_console(const struct consw
*csw
)
2534 for(i
= 0; i
< MAX_NR_CONSOLES
; i
++)
2535 if (con_driver_map
[i
] == csw
)
2536 con_driver_map
[i
] = NULL
;
2545 static void set_vesa_blanking(unsigned long arg
)
2547 char *argp
= (char *)arg
+ 1;
2549 get_user(mode
, argp
);
2550 vesa_blank_mode
= (mode
< 4) ? mode
: 0;
2553 /* We can't register the console with devfs during con_init(), because it
2554 * is called before kmalloc() works. This function is called later to
2555 * do the registration.
2557 void __init
con_init_devfs (void)
2561 for (i
= 0; i
< console_driver
.num
; i
++)
2562 tty_register_devfs (&console_driver
, DEVFS_FL_AOPEN_NOTIFY
,
2563 console_driver
.minor_start
+ i
);
2566 static void vesa_powerdown(void)
2568 struct vc_data
*c
= vc_cons
[fg_console
].d
;
2570 * Power down if currently suspended (1 or 2),
2571 * suspend if currently blanked (0),
2572 * else do nothing (i.e. already powered down (3)).
2573 * Called only if powerdown features are allowed.
2575 switch (vesa_blank_mode
) {
2576 case VESA_NO_BLANKING
:
2577 c
->vc_sw
->con_blank(c
, VESA_VSYNC_SUSPEND
+1);
2579 case VESA_VSYNC_SUSPEND
:
2580 case VESA_HSYNC_SUSPEND
:
2581 c
->vc_sw
->con_blank(c
, VESA_POWERDOWN
+1);
2586 static void vesa_powerdown_screen(unsigned long dummy
)
2588 console_timer
.function
= unblank_screen_t
; /* I don't have a clue why this is necessary */
2593 static void timer_do_blank_screen(int entering_gfx
, int from_timer_handler
)
2595 int currcons
= fg_console
;
2598 if (console_blanked
)
2601 /* entering graphics mode? */
2603 hide_cursor(currcons
);
2604 save_screen(currcons
);
2605 sw
->con_blank(vc_cons
[currcons
].d
, -1);
2606 console_blanked
= fg_console
+ 1;
2607 set_origin(currcons
);
2611 /* don't blank graphics */
2612 if (vcmode
!= KD_TEXT
) {
2613 console_blanked
= fg_console
+ 1;
2617 hide_cursor(currcons
);
2618 if (!from_timer_handler
)
2619 del_timer_sync(&console_timer
);
2620 if (vesa_off_interval
) {
2621 console_timer
.function
= vesa_powerdown_screen
;
2622 mod_timer(&console_timer
, jiffies
+ vesa_off_interval
);
2624 if (!from_timer_handler
)
2625 del_timer_sync(&console_timer
);
2626 console_timer
.function
= unblank_screen_t
;
2629 save_screen(currcons
);
2630 /* In case we need to reset origin, blanking hook returns 1 */
2631 i
= sw
->con_blank(vc_cons
[currcons
].d
, 1);
2632 console_blanked
= fg_console
+ 1;
2634 set_origin(currcons
);
2636 if (console_blank_hook
&& console_blank_hook(1))
2638 if (vesa_blank_mode
)
2639 sw
->con_blank(vc_cons
[currcons
].d
, vesa_blank_mode
+ 1);
2642 void do_blank_screen(int entering_gfx
)
2644 timer_do_blank_screen(entering_gfx
, 0);
2647 static void unblank_screen_t(unsigned long dummy
)
2652 void unblank_screen(void)
2656 if (!console_blanked
)
2658 if (!vc_cons_allocated(fg_console
)) {
2660 printk("unblank_screen: tty %d not allocated ??\n", fg_console
+1);
2663 console_timer
.function
= blank_screen
;
2664 if (blankinterval
) {
2665 mod_timer(&console_timer
, jiffies
+ blankinterval
);
2668 currcons
= fg_console
;
2669 console_blanked
= 0;
2670 if (console_blank_hook
)
2671 console_blank_hook(0);
2672 set_palette(currcons
);
2673 if (sw
->con_blank(vc_cons
[currcons
].d
, 0))
2674 /* Low-level driver cannot restore -> do it ourselves */
2675 update_screen(fg_console
);
2676 set_cursor(fg_console
);
2679 static void blank_screen(unsigned long dummy
)
2681 timer_do_blank_screen(0, 1);
2684 void poke_blanked_console(void)
2686 del_timer(&console_timer
); /* Can't use _sync here: called from tasklet */
2687 if (vt_cons
[fg_console
]->vc_mode
== KD_GRAPHICS
)
2689 if (console_blanked
) {
2690 console_timer
.function
= unblank_screen_t
;
2691 mod_timer(&console_timer
, jiffies
); /* Now */
2692 } else if (blankinterval
) {
2693 mod_timer(&console_timer
, jiffies
+ blankinterval
);
2701 void set_palette(int currcons
)
2703 if (vcmode
!= KD_GRAPHICS
)
2704 sw
->con_set_palette(vc_cons
[currcons
].d
, color_table
);
2707 static int set_get_cmap(unsigned char *arg
, int set
)
2711 for (i
= 0; i
< 16; i
++)
2713 get_user(default_red
[i
], arg
++);
2714 get_user(default_grn
[i
], arg
++);
2715 get_user(default_blu
[i
], arg
++);
2717 put_user(default_red
[i
], arg
++);
2718 put_user(default_grn
[i
], arg
++);
2719 put_user(default_blu
[i
], arg
++);
2722 for (i
= 0; i
< MAX_NR_CONSOLES
; i
++)
2723 if (vc_cons_allocated(i
)) {
2724 for (j
= k
= 0; j
< 16; j
++) {
2725 vc_cons
[i
].d
->vc_palette
[k
++] = default_red
[j
];
2726 vc_cons
[i
].d
->vc_palette
[k
++] = default_grn
[j
];
2727 vc_cons
[i
].d
->vc_palette
[k
++] = default_blu
[j
];
2736 * Load palette into the DAC registers. arg points to a colour
2737 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2740 int con_set_cmap(unsigned char *arg
)
2742 return set_get_cmap (arg
,1);
2745 int con_get_cmap(unsigned char *arg
)
2747 return set_get_cmap (arg
,0);
2750 void reset_palette(int currcons
)
2753 for (j
=k
=0; j
<16; j
++) {
2754 palette
[k
++] = default_red
[j
];
2755 palette
[k
++] = default_grn
[j
];
2756 palette
[k
++] = default_blu
[j
];
2758 set_palette(currcons
);
2764 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2765 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2766 * depending on width) reserved for each character which is kinda wasty, but
2767 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2768 * is upto the actual low-level console-driver convert data into its favorite
2769 * format (maybe we should add a `fontoffset' field to the `display'
2770 * structure so we wont have to convert the fontdata all the time.
2774 #define max_font_size 65536
2776 int con_font_op(int currcons
, struct console_font_op
*op
)
2779 int size
= max_font_size
, set
;
2781 struct console_font_op old_op
;
2783 if (vt_cons
[currcons
]->vc_mode
!= KD_TEXT
)
2785 memcpy(&old_op
, op
, sizeof(old_op
));
2786 if (op
->op
== KD_FONT_OP_SET
) {
2789 if (op
->charcount
> 512)
2791 if (!op
->height
) { /* Need to guess font height [compat] */
2793 u8
*charmap
= op
->data
, tmp
;
2795 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2796 so that we can get rid of this soon */
2797 if (!(op
->flags
& KD_FONT_FLAG_OLD
))
2800 for (h
= 32; h
> 0; h
--)
2801 for (i
= 0; i
< op
->charcount
; i
++) {
2802 if (get_user(tmp
, &charmap
[32*i
+h
-1]))
2813 if (op
->width
> 32 || op
->height
> 32)
2815 size
= (op
->width
+7)/8 * 32 * op
->charcount
;
2816 if (size
> max_font_size
)
2819 } else if (op
->op
== KD_FONT_OP_GET
)
2822 return sw
->con_font_op(vc_cons
[currcons
].d
, op
);
2824 temp
= kmalloc(size
, GFP_KERNEL
);
2827 if (set
&& copy_from_user(temp
, op
->data
, size
)) {
2834 spin_lock_irq(&console_lock
);
2835 rc
= sw
->con_font_op(vc_cons
[currcons
].d
, op
);
2836 spin_unlock_irq(&console_lock
);
2838 op
->data
= old_op
.data
;
2840 int c
= (op
->width
+7)/8 * 32 * op
->charcount
;
2842 if (op
->data
&& op
->charcount
> old_op
.charcount
)
2844 if (!(op
->flags
& KD_FONT_FLAG_OLD
)) {
2845 if (op
->width
> old_op
.width
||
2846 op
->height
> old_op
.height
)
2851 else if ((old_op
.height
&& op
->height
> old_op
.height
) ||
2855 if (!rc
&& op
->data
&& copy_to_user(op
->data
, temp
, c
))
2864 * Interface exported to selection and vcs.
2867 /* used by selection */
2868 u16
screen_glyph(int currcons
, int offset
)
2870 u16 w
= scr_readw(screenpos(currcons
, offset
, 1));
2873 if (w
& hi_font_mask
)
2878 /* used by vcs - note the word offset */
2879 unsigned short *screen_pos(int currcons
, int w_offset
, int viewed
)
2881 return screenpos(currcons
, 2 * w_offset
, viewed
);
2884 void getconsxy(int currcons
, char *p
)
2890 void putconsxy(int currcons
, char *p
)
2892 gotoxy(currcons
, p
[0], p
[1]);
2893 set_cursor(currcons
);
2896 u16
vcs_scr_readw(int currcons
, const u16
*org
)
2898 if ((unsigned long)org
== pos
&& softcursor_original
!= -1)
2899 return softcursor_original
;
2900 return scr_readw(org
);
2903 void vcs_scr_writew(int currcons
, u16 val
, u16
*org
)
2905 scr_writew(val
, org
);
2906 if ((unsigned long)org
== pos
) {
2907 softcursor_original
= -1;
2908 add_softcursor(currcons
);
2912 static int pm_con_request(struct pm_dev
*dev
, pm_request_t rqst
, void *data
)
2927 * Visible symbols for modules
2930 EXPORT_SYMBOL(color_table
);
2931 EXPORT_SYMBOL(default_red
);
2932 EXPORT_SYMBOL(default_grn
);
2933 EXPORT_SYMBOL(default_blu
);
2934 EXPORT_SYMBOL(video_font_height
);
2935 EXPORT_SYMBOL(video_scan_lines
);
2936 EXPORT_SYMBOL(vc_resize
);
2937 EXPORT_SYMBOL(fg_console
);
2938 EXPORT_SYMBOL(console_blank_hook
);
2940 EXPORT_SYMBOL(vt_cons
);
2942 #ifndef VT_SINGLE_DRIVER
2943 EXPORT_SYMBOL(take_over_console
);
2944 EXPORT_SYMBOL(give_up_console
);