* better
[mascara-docs.git] / i386 / linux-2.3.21 / drivers / char / console.c
blobf452237ab979b73a7c8564a986b396c023f10032
1 /*
2 * linux/drivers/char/console.c
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
7 /*
8 * Hopefully this will be a rather complete VT102 implementation.
10 * Beeping thanks to John T Kohl.
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <geert@linux-m68k.org>, Jan 1997.
38 * Original m68k console driver modifications by
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
43 * Note that the abstract console driver allows all consoles to be of
44 * potentially different sizes, so the following variables depend on the
45 * current console (currcons):
47 * - video_num_columns
48 * - video_num_lines
49 * - video_size_row
50 * - can_do_color
52 * The abstract console driver provides a generic interface for a text
53 * console. It supports VGA text mode, frame buffer based graphical consoles
54 * and special graphics processors that are only accessible through some
55 * registers (e.g. a TMS340x0 GSP).
57 * The interface to the hardware is specified using a special structure
58 * (struct consw) which contains function pointers to console operations
59 * (see <linux/console.h> for more information).
61 * Support for changeable cursor shape
62 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
64 * Ported to i386 and con_scrolldelta fixed
65 * by Emmanuel Marty <core@ggi-project.org>, April 1998
67 * Resurrected character buffers in videoram plus lots of other trickery
68 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
71 #include <linux/module.h>
72 #include <linux/sched.h>
73 #include <linux/tty.h>
74 #include <linux/tty_flip.h>
75 #include <linux/kernel.h>
76 #include <linux/string.h>
77 #include <linux/errno.h>
78 #include <linux/kd.h>
79 #include <linux/malloc.h>
80 #include <linux/major.h>
81 #include <linux/mm.h>
82 #include <linux/console.h>
83 #include <linux/init.h>
84 #include <linux/vt_kern.h>
85 #include <linux/selection.h>
86 #include <linux/console_struct.h>
87 #include <linux/kbd_kern.h>
88 #include <linux/consolemap.h>
89 #include <linux/timer.h>
90 #include <linux/interrupt.h>
91 #include <linux/config.h>
92 #include <linux/version.h>
93 #include <linux/tqueue.h>
94 #ifdef CONFIG_APM
95 #include <linux/apm_bios.h>
96 #endif
98 #include <asm/io.h>
99 #include <asm/system.h>
100 #include <asm/uaccess.h>
101 #include <asm/bitops.h>
103 #include <asm/linux_logo.h>
105 #include "console_macros.h"
108 struct consw *conswitchp = NULL;
110 /* A bitmap for codes <32. A bit of 1 indicates that the code
111 * corresponding to that bit number invokes some special action
112 * (such as cursor movement) and should not be displayed as a
113 * glyph unless the disp_ctrl mode is explicitly enabled.
115 #define CTRL_ACTION 0x0d00ff81
116 #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
119 * Here is the default bell parameters: 750HZ, 1/8th of a second
121 #define DEFAULT_BELL_PITCH 750
122 #define DEFAULT_BELL_DURATION (HZ/8)
124 #ifndef MIN
125 #define MIN(a,b) ((a) < (b) ? (a) : (b))
126 #endif
128 static struct tty_struct *console_table[MAX_NR_CONSOLES];
129 static struct termios *console_termios[MAX_NR_CONSOLES];
130 static struct termios *console_termios_locked[MAX_NR_CONSOLES];
131 struct vc vc_cons [MAX_NR_CONSOLES];
133 #ifndef VT_SINGLE_DRIVER
134 static struct consw *con_driver_map[MAX_NR_CONSOLES];
135 #endif
137 static int con_open(struct tty_struct *, struct file *);
138 static void vc_init(unsigned int console, unsigned int rows,
139 unsigned int cols, int do_clear);
140 static void blank_screen(void);
141 static void gotoxy(int currcons, int new_x, int new_y);
142 static void save_cur(int currcons);
143 static void reset_terminal(int currcons, int do_clear);
144 static void con_flush_chars(struct tty_struct *tty);
145 static void set_vesa_blanking(unsigned long arg);
146 static void set_cursor(int currcons);
147 static void hide_cursor(int currcons);
149 static int printable = 0; /* Is console ready for printing? */
151 int do_poke_blanked_console = 0;
152 int console_blanked = 0;
154 static int vesa_blank_mode = 0; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
155 static int blankinterval = 10*60*HZ;
156 static int vesa_off_interval = 0;
159 * fg_console is the current virtual console,
160 * last_console is the last used one,
161 * want_console is the console we want to switch to,
162 * kmsg_redirect is the console for kernel messages,
164 int fg_console = 0;
165 int last_console = 0;
166 int want_console = -1;
167 int kmsg_redirect = 0;
170 * For each existing display, we have a pointer to console currently visible
171 * on that display, allowing consoles other than fg_console to be refreshed
172 * appropriately. Unless the low-level driver supplies its own display_fg
173 * variable, we use this one for the "master display".
175 static struct vc_data *master_display_fg = NULL;
178 * Unfortunately, we need to delay tty echo when we're currently writing to the
179 * console since the code is (and always was) not re-entrant, so we insert
180 * all filp requests to con_task_queue instead of tq_timer and run it from
181 * the console_bh.
183 DECLARE_TASK_QUEUE(con_task_queue);
186 * For the same reason, we defer scrollback to the console_bh.
188 static int scrollback_delta = 0;
191 * Low-Level Functions
194 #define IS_FG (currcons == fg_console)
195 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
197 #ifdef VT_BUF_VRAM_ONLY
198 #define DO_UPDATE 0
199 #else
200 #define DO_UPDATE IS_VISIBLE
201 #endif
203 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
205 unsigned short *p;
207 if (!viewed)
208 p = (unsigned short *)(origin + offset);
209 else if (!sw->con_screen_pos)
210 p = (unsigned short *)(visible_origin + offset);
211 else
212 p = sw->con_screen_pos(vc_cons[currcons].d, offset);
213 return p;
216 static inline void scrolldelta(int lines)
218 scrollback_delta += lines;
219 mark_bh(CONSOLE_BH);
222 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
224 unsigned short *d, *s;
226 if (t+nr >= b)
227 nr = b - t - 1;
228 if (b > video_num_lines || t >= b || nr < 1)
229 return;
230 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
231 return;
232 d = (unsigned short *) (origin+video_size_row*t);
233 s = (unsigned short *) (origin+video_size_row*(t+nr));
234 scr_memcpyw(d, s, (b-t-nr) * video_size_row);
235 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
238 static void
239 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
241 unsigned short *s;
242 unsigned int step;
244 if (t+nr >= b)
245 nr = b - t - 1;
246 if (b > video_num_lines || t >= b || nr < 1)
247 return;
248 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
249 return;
250 s = (unsigned short *) (origin+video_size_row*t);
251 step = video_num_columns * nr;
252 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
253 scr_memsetw(s, video_erase_char, 2*step);
256 static void do_update_region(int currcons, unsigned long start, int count)
258 #ifndef VT_BUF_VRAM_ONLY
259 unsigned int xx, yy, offset;
260 u16 *p;
262 p = (u16 *) start;
263 if (!sw->con_getxy) {
264 offset = (start - origin) / 2;
265 xx = offset % video_num_columns;
266 yy = offset / video_num_columns;
267 } else {
268 int nxx, nyy;
269 start = sw->con_getxy(vc_cons[currcons].d, start, &nxx, &nyy);
270 xx = nxx; yy = nyy;
272 for(;;) {
273 u16 attrib = scr_readw(p) & 0xff00;
274 int startx = xx;
275 u16 *q = p;
276 while (xx < video_num_columns && count) {
277 if (attrib != (scr_readw(p) & 0xff00)) {
278 if (p > q)
279 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
280 startx = xx;
281 q = p;
282 attrib = scr_readw(p) & 0xff00;
284 p++;
285 xx++;
286 count--;
288 if (p > q)
289 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
290 if (!count)
291 break;
292 xx = 0;
293 yy++;
294 if (sw->con_getxy) {
295 p = (u16 *)start;
296 start = sw->con_getxy(vc_cons[currcons].d, start, NULL, NULL);
299 #endif
302 void update_region(int currcons, unsigned long start, int count)
304 if (DO_UPDATE) {
305 hide_cursor(currcons);
306 do_update_region(currcons, start, count);
307 set_cursor(currcons);
311 /* Structure of attributes is hardware-dependent */
313 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
315 if (sw->con_build_attr)
316 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
318 #ifndef VT_BUF_VRAM_ONLY
320 * ++roman: I completely changed the attribute format for monochrome
321 * mode (!can_do_color). The formerly used MDA (monochrome display
322 * adapter) format didn't allow the combination of certain effects.
323 * Now the attribute is just a bit vector:
324 * Bit 0..1: intensity (0..2)
325 * Bit 2 : underline
326 * Bit 3 : reverse
327 * Bit 7 : blink
330 u8 a = color;
331 if (!can_do_color)
332 return _intensity |
333 (_underline ? 4 : 0) |
334 (_reverse ? 8 : 0) |
335 (_blink ? 0x80 : 0);
336 if (_underline)
337 a = (a & 0xf0) | ulcolor;
338 else if (_intensity == 0)
339 a = (a & 0xf0) | halfcolor;
340 if (_reverse)
341 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
342 if (_blink)
343 a ^= 0x80;
344 if (_intensity == 2)
345 a ^= 0x08;
346 if (hi_font_mask == 0x100)
347 a <<= 1;
348 return a;
350 #else
351 return 0;
352 #endif
355 static void update_attr(int currcons)
357 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
358 video_erase_char = (build_attr(currcons, color, 1, 0, 0, decscnm) << 8) | ' ';
361 /* Note: inverting the screen twice should revert to the original state */
363 void invert_screen(int currcons, int offset, int count, int viewed)
365 unsigned short *p;
367 count /= 2;
368 p = screenpos(currcons, offset, viewed);
369 if (sw->con_invert_region)
370 sw->con_invert_region(vc_cons[currcons].d, p, count);
371 #ifndef VT_BUF_VRAM_ONLY
372 else {
373 u16 *q = p;
374 int cnt = count;
376 if (!can_do_color) {
377 while (cnt--) *q++ ^= 0x0800;
378 } else if (hi_font_mask == 0x100) {
379 while (cnt--) {
380 u16 a = *q;
381 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
382 *q++ = a;
384 } else {
385 while (cnt--) {
386 u16 a = *q;
387 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
388 *q++ = a;
392 #endif
393 if (DO_UPDATE)
394 do_update_region(currcons, (unsigned long) p, count);
397 /* used by selection: complement pointer position */
398 void complement_pos(int currcons, int offset)
400 static unsigned short *p = NULL;
401 static unsigned short old = 0;
402 static unsigned short oldx = 0, oldy = 0;
404 if (p) {
405 scr_writew(old, p);
406 if (DO_UPDATE)
407 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
409 if (offset == -1)
410 p = NULL;
411 else {
412 unsigned short new;
413 p = screenpos(currcons, offset, 1);
414 old = scr_readw(p);
415 new = old ^ complement_mask;
416 scr_writew(new, p);
417 if (DO_UPDATE) {
418 oldx = (offset >> 1) % video_num_columns;
419 oldy = (offset >> 1) / video_num_columns;
420 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
425 static void insert_char(int currcons, unsigned int nr)
427 unsigned short *p, *q = (unsigned short *) pos;
429 p = q + video_num_columns - nr - x;
430 while (--p >= q)
431 scr_writew(scr_readw(p), p + nr);
432 scr_memsetw(q, video_erase_char, nr*2);
433 need_wrap = 0;
434 if (DO_UPDATE) {
435 unsigned short oldattr = attr;
436 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
437 video_num_columns-x-nr);
438 attr = video_erase_char >> 8;
439 while (nr--)
440 sw->con_putc(vc_cons[currcons].d,
441 video_erase_char,y,x+nr);
442 attr = oldattr;
446 static void delete_char(int currcons, unsigned int nr)
448 unsigned int i = x;
449 unsigned short *p = (unsigned short *) pos;
451 while (++i <= video_num_columns - nr) {
452 scr_writew(scr_readw(p+nr), p);
453 p++;
455 scr_memsetw(p, video_erase_char, nr*2);
456 need_wrap = 0;
457 if (DO_UPDATE) {
458 unsigned short oldattr = attr;
459 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
460 video_num_columns-x-nr);
461 attr = video_erase_char >> 8;
462 while (nr--)
463 sw->con_putc(vc_cons[currcons].d,
464 video_erase_char, y,
465 video_num_columns-1-nr);
466 attr = oldattr;
470 static int softcursor_original;
472 static void add_softcursor(int currcons)
474 int i = scr_readw((u16 *) pos);
475 u32 type = cursor_type;
477 if (! (type & 0x10)) return;
478 if (softcursor_original != -1) return;
479 softcursor_original = i;
480 i |= ((type >> 8) & 0xff00 );
481 i ^= ((type) & 0xff00 );
482 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
483 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
484 scr_writew(i, (u16 *) pos);
485 if (DO_UPDATE)
486 sw->con_putc(vc_cons[currcons].d, i, y, x);
489 static void hide_cursor(int currcons)
491 if (currcons == sel_cons)
492 clear_selection();
493 if (softcursor_original != -1) {
494 scr_writew(softcursor_original,(u16 *) pos);
495 if (DO_UPDATE)
496 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
497 softcursor_original = -1;
499 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
502 static void set_cursor(int currcons)
504 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
505 return;
506 if (deccm) {
507 if (currcons == sel_cons)
508 clear_selection();
509 add_softcursor(currcons);
510 if ((cursor_type & 0x0f) != 1)
511 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
512 } else
513 hide_cursor(currcons);
516 static void set_origin(int currcons)
518 if (!IS_VISIBLE ||
519 !sw->con_set_origin ||
520 !sw->con_set_origin(vc_cons[currcons].d))
521 origin = (unsigned long) screenbuf;
522 visible_origin = origin;
523 scr_end = origin + screenbuf_size;
524 pos = origin + video_size_row*y + 2*x;
527 static inline void save_screen(int currcons)
529 if (sw->con_save_screen)
530 sw->con_save_screen(vc_cons[currcons].d);
534 * Redrawing of screen
537 void redraw_screen(int new_console, int is_switch)
539 int redraw = 1;
540 int currcons, old_console;
541 static int lock = 0;
543 if (lock)
544 return;
545 if (!vc_cons_allocated(new_console)) {
546 /* strange ... */
547 printk("redraw_screen: tty %d not allocated ??\n", new_console+1);
548 return;
550 lock = 1;
552 if (is_switch) {
553 currcons = fg_console;
554 hide_cursor(currcons);
555 if (fg_console != new_console) {
556 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
557 old_console = (*display) ? (*display)->vc_num : fg_console;
558 *display = vc_cons[new_console].d;
559 fg_console = new_console;
560 currcons = old_console;
561 if (!IS_VISIBLE) {
562 save_screen(currcons);
563 set_origin(currcons);
565 currcons = new_console;
566 if (old_console == new_console)
567 redraw = 0;
569 } else {
570 currcons = new_console;
571 hide_cursor(currcons);
574 if (redraw) {
575 set_origin(currcons);
576 set_palette(currcons);
577 if (sw->con_switch(vc_cons[currcons].d) && vcmode != KD_GRAPHICS)
578 /* Update the screen contents */
579 do_update_region(currcons, origin, screenbuf_size/2);
581 set_cursor(currcons);
582 if (is_switch) {
583 set_leds();
584 compute_shiftstate();
586 lock = 0;
590 * Allocation, freeing and resizing of VTs.
593 int vc_cons_allocated(unsigned int i)
595 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
598 static void visual_init(int currcons, int init)
600 /* ++Geert: sw->con_init determines console size */
601 sw = conswitchp;
602 #ifndef VT_SINGLE_DRIVER
603 if (con_driver_map[currcons])
604 sw = con_driver_map[currcons];
605 #endif
606 cons_num = currcons;
607 display_fg = &master_display_fg;
608 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
609 vc_cons[currcons].d->vc_uni_pagedir = 0;
610 hi_font_mask = 0;
611 complement_mask = 0;
612 can_do_color = 0;
613 sw->con_init(vc_cons[currcons].d, init);
614 if (!complement_mask)
615 complement_mask = can_do_color ? 0x7700 : 0x0800;
616 s_complement_mask = complement_mask;
617 video_size_row = video_num_columns<<1;
618 screenbuf_size = video_num_lines*video_size_row;
621 int vc_allocate(unsigned int currcons) /* return 0 on success */
623 if (currcons >= MAX_NR_CONSOLES)
624 return -ENXIO;
625 if (!vc_cons[currcons].d) {
626 long p, q;
628 /* prevent users from taking too much memory */
629 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
630 return -EPERM;
632 /* due to the granularity of kmalloc, we waste some memory here */
633 /* the alloc is done in two steps, to optimize the common situation
634 of a 25x80 console (structsize=216, screenbuf_size=4000) */
635 /* although the numbers above are not valid since long ago, the
636 point is still up-to-date and the comment still has its value
637 even if only as a historical artifact. --mj, July 1998 */
638 p = (long) kmalloc(structsize, GFP_KERNEL);
639 if (!p)
640 return -ENOMEM;
641 vc_cons[currcons].d = (struct vc_data *)p;
642 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
643 visual_init(currcons, 1);
644 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
645 con_set_default_unimap(currcons);
646 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
647 if (!q) {
648 kfree_s((char *) p, structsize);
649 vc_cons[currcons].d = NULL;
650 vt_cons[currcons] = NULL;
651 return -ENOMEM;
653 screenbuf = (unsigned short *) q;
654 kmalloced = 1;
655 vc_init(currcons, video_num_lines, video_num_columns, 1);
657 return 0;
661 * Change # of rows and columns (0 means unchanged/the size of fg_console)
662 * [this is to be used together with some user program
663 * like resize that changes the hardware videomode]
665 int vc_resize(unsigned int lines, unsigned int cols,
666 unsigned int first, unsigned int last)
668 unsigned int cc, ll, ss, sr, todo = 0;
669 unsigned int currcons = fg_console, i;
670 unsigned short *newscreens[MAX_NR_CONSOLES];
672 cc = (cols ? cols : video_num_columns);
673 ll = (lines ? lines : video_num_lines);
674 sr = cc << 1;
675 ss = sr * ll;
677 for (currcons = first; currcons <= last; currcons++) {
678 if (!vc_cons_allocated(currcons) ||
679 (cc == video_num_columns && ll == video_num_lines))
680 newscreens[currcons] = NULL;
681 else {
682 unsigned short *p = (unsigned short *) kmalloc(ss, GFP_USER);
683 if (!p) {
684 for (i = first; i < currcons; i++)
685 if (newscreens[i])
686 kfree_s(newscreens[i], ss);
687 return -ENOMEM;
689 newscreens[currcons] = p;
690 todo++;
693 if (!todo)
694 return 0;
696 for (currcons = first; currcons <= last; currcons++) {
697 unsigned int occ, oll, oss, osr;
698 unsigned long ol, nl, nlend, rlth, rrem;
699 if (!newscreens[currcons] || !vc_cons_allocated(currcons))
700 continue;
702 oll = video_num_lines;
703 occ = video_num_columns;
704 osr = video_size_row;
705 oss = screenbuf_size;
707 video_num_lines = ll;
708 video_num_columns = cc;
709 video_size_row = sr;
710 screenbuf_size = ss;
712 rlth = MIN(osr, sr);
713 rrem = sr - rlth;
714 ol = origin;
715 nl = (long) newscreens[currcons];
716 nlend = nl + ss;
717 if (ll < oll)
718 ol += (oll - ll) * osr;
720 update_attr(currcons);
722 while (ol < scr_end) {
723 scr_memcpyw((unsigned short *) nl, (unsigned short *) ol, rlth);
724 if (rrem)
725 scr_memsetw((void *)(nl + rlth), video_erase_char, rrem);
726 ol += osr;
727 nl += sr;
729 if (nlend > nl)
730 scr_memsetw((void *) nl, video_erase_char, nlend - nl);
731 if (kmalloced)
732 kfree_s(screenbuf, oss);
733 screenbuf = newscreens[currcons];
734 kmalloced = 1;
735 screenbuf_size = ss;
736 set_origin(currcons);
738 /* do part of a reset_terminal() */
739 top = 0;
740 bottom = video_num_lines;
741 gotoxy(currcons, x, y);
742 save_cur(currcons);
744 if (console_table[currcons]) {
745 struct winsize ws, *cws = &console_table[currcons]->winsize;
746 memset(&ws, 0, sizeof(ws));
747 ws.ws_row = video_num_lines;
748 ws.ws_col = video_num_columns;
749 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
750 console_table[currcons]->pgrp > 0)
751 kill_pg(console_table[currcons]->pgrp, SIGWINCH, 1);
752 *cws = ws;
755 if (IS_VISIBLE)
756 update_screen(currcons);
759 return 0;
763 void vc_disallocate(unsigned int currcons)
765 if (vc_cons_allocated(currcons)) {
766 sw->con_deinit(vc_cons[currcons].d);
767 if (kmalloced)
768 kfree_s(screenbuf, screenbuf_size);
769 if (currcons >= MIN_NR_CONSOLES)
770 kfree_s(vc_cons[currcons].d, structsize);
771 vc_cons[currcons].d = NULL;
776 * VT102 emulator
779 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
780 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
781 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
783 #define decarm VC_REPEAT
784 #define decckm VC_CKMODE
785 #define kbdapplic VC_APPLIC
786 #define lnm VC_CRLF
789 * this is what the terminal answers to a ESC-Z or csi0c query.
791 #define VT100ID "\033[?1;2c"
792 #define VT102ID "\033[?6c"
794 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
795 8,12,10,14, 9,13,11,15 };
797 /* the default colour table, for VGA+ colour systems */
798 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
799 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
800 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
801 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
802 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
803 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
806 * gotoxy() must verify all boundaries, because the arguments
807 * might also be negative. If the given position is out of
808 * bounds, the cursor is placed at the nearest margin.
810 static void gotoxy(int currcons, int new_x, int new_y)
812 int min_y, max_y;
814 if (new_x < 0)
815 x = 0;
816 else
817 if (new_x >= video_num_columns)
818 x = video_num_columns - 1;
819 else
820 x = new_x;
821 if (decom) {
822 min_y = top;
823 max_y = bottom;
824 } else {
825 min_y = 0;
826 max_y = video_num_lines;
828 if (new_y < min_y)
829 y = min_y;
830 else if (new_y >= max_y)
831 y = max_y - 1;
832 else
833 y = new_y;
834 pos = origin + y*video_size_row + (x<<1);
835 need_wrap = 0;
838 /* for absolute user moves, when decom is set */
839 static void gotoxay(int currcons, int new_x, int new_y)
841 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
844 void scrollback(int lines)
846 int currcons = fg_console;
848 if (!lines)
849 lines = video_num_lines/2;
850 scrolldelta(-lines);
853 void scrollfront(int lines)
855 int currcons = fg_console;
857 if (!lines)
858 lines = video_num_lines/2;
859 scrolldelta(lines);
862 static void lf(int currcons)
864 /* don't scroll if above bottom of scrolling region, or
865 * if below scrolling region
867 if (y+1 == bottom)
868 scrup(currcons,top,bottom,1);
869 else if (y < video_num_lines-1) {
870 y++;
871 pos += video_size_row;
873 need_wrap = 0;
876 static void ri(int currcons)
878 /* don't scroll if below top of scrolling region, or
879 * if above scrolling region
881 if (y == top)
882 scrdown(currcons,top,bottom,1);
883 else if (y > 0) {
884 y--;
885 pos -= video_size_row;
887 need_wrap = 0;
890 static inline void cr(int currcons)
892 pos -= x<<1;
893 need_wrap = x = 0;
896 static inline void bs(int currcons)
898 if (x) {
899 pos -= 2;
900 x--;
901 need_wrap = 0;
905 static inline void del(int currcons)
907 /* ignored */
910 static void csi_J(int currcons, int vpar)
912 unsigned int count;
913 unsigned short * start;
915 switch (vpar) {
916 case 0: /* erase from cursor to end of display */
917 count = (scr_end-pos)>>1;
918 start = (unsigned short *) pos;
919 if (DO_UPDATE) {
920 /* do in two stages */
921 sw->con_clear(vc_cons[currcons].d, y, x, 1,
922 video_num_columns-x);
923 sw->con_clear(vc_cons[currcons].d, y+1, 0,
924 video_num_lines-y-1,
925 video_num_columns);
927 break;
928 case 1: /* erase from start to cursor */
929 count = ((pos-origin)>>1)+1;
930 start = (unsigned short *) origin;
931 if (DO_UPDATE) {
932 /* do in two stages */
933 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
934 video_num_columns);
935 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
936 x + 1);
938 break;
939 case 2: /* erase whole display */
940 count = video_num_columns * video_num_lines;
941 start = (unsigned short *) origin;
942 if (DO_UPDATE)
943 sw->con_clear(vc_cons[currcons].d, 0, 0,
944 video_num_lines,
945 video_num_columns);
946 break;
947 default:
948 return;
950 scr_memsetw(start, video_erase_char, 2*count);
951 need_wrap = 0;
954 static void csi_K(int currcons, int vpar)
956 unsigned int count;
957 unsigned short * start;
959 switch (vpar) {
960 case 0: /* erase from cursor to end of line */
961 count = video_num_columns-x;
962 start = (unsigned short *) pos;
963 if (DO_UPDATE)
964 sw->con_clear(vc_cons[currcons].d, y, x, 1,
965 video_num_columns-x);
966 break;
967 case 1: /* erase from start of line to cursor */
968 start = (unsigned short *) (pos - (x<<1));
969 count = x+1;
970 if (DO_UPDATE)
971 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
972 x + 1);
973 break;
974 case 2: /* erase whole line */
975 start = (unsigned short *) (pos - (x<<1));
976 count = video_num_columns;
977 if (DO_UPDATE)
978 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
979 video_num_columns);
980 break;
981 default:
982 return;
984 scr_memsetw(start, video_erase_char, 2 * count);
985 need_wrap = 0;
988 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
989 { /* not vt100? */
990 int count;
992 if (!vpar)
993 vpar++;
994 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
996 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
997 if (DO_UPDATE)
998 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
999 need_wrap = 0;
1002 static void default_attr(int currcons)
1004 intensity = 1;
1005 underline = 0;
1006 reverse = 0;
1007 blink = 0;
1008 color = def_color;
1011 static void csi_m(int currcons)
1013 int i;
1015 for (i=0;i<=npar;i++)
1016 switch (par[i]) {
1017 case 0: /* all attributes off */
1018 default_attr(currcons);
1019 break;
1020 case 1:
1021 intensity = 2;
1022 break;
1023 case 2:
1024 intensity = 0;
1025 break;
1026 case 4:
1027 underline = 1;
1028 break;
1029 case 5:
1030 blink = 1;
1031 break;
1032 case 7:
1033 reverse = 1;
1034 break;
1035 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1036 * Select primary font, don't display
1037 * control chars if defined, don't set
1038 * bit 8 on output.
1040 translate = set_translate(charset == 0
1041 ? G0_charset
1042 : G1_charset,currcons);
1043 disp_ctrl = 0;
1044 toggle_meta = 0;
1045 break;
1046 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1047 * Select first alternate font, lets
1048 * chars < 32 be displayed as ROM chars.
1050 translate = set_translate(IBMPC_MAP,currcons);
1051 disp_ctrl = 1;
1052 toggle_meta = 0;
1053 break;
1054 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1055 * Select second alternate font, toggle
1056 * high bit before displaying as ROM char.
1058 translate = set_translate(IBMPC_MAP,currcons);
1059 disp_ctrl = 1;
1060 toggle_meta = 1;
1061 break;
1062 case 21:
1063 case 22:
1064 intensity = 1;
1065 break;
1066 case 24:
1067 underline = 0;
1068 break;
1069 case 25:
1070 blink = 0;
1071 break;
1072 case 27:
1073 reverse = 0;
1074 break;
1075 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1076 * Enables underscore, white foreground
1077 * with white underscore (Linux - use
1078 * default foreground).
1080 color = (def_color & 0x0f) | background;
1081 underline = 1;
1082 break;
1083 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1084 * Disable underline option.
1085 * Reset colour to default? It did this
1086 * before...
1088 color = (def_color & 0x0f) | background;
1089 underline = 0;
1090 break;
1091 case 49:
1092 color = (def_color & 0xf0) | foreground;
1093 break;
1094 default:
1095 if (par[i] >= 30 && par[i] <= 37)
1096 color = color_table[par[i]-30]
1097 | background;
1098 else if (par[i] >= 40 && par[i] <= 47)
1099 color = (color_table[par[i]-40]<<4)
1100 | foreground;
1101 break;
1103 update_attr(currcons);
1106 static void respond_string(const char * p, struct tty_struct * tty)
1108 while (*p) {
1109 tty_insert_flip_char(tty, *p, 0);
1110 p++;
1112 con_schedule_flip(tty);
1115 static void cursor_report(int currcons, struct tty_struct * tty)
1117 char buf[40];
1119 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1120 respond_string(buf, tty);
1123 static inline void status_report(struct tty_struct * tty)
1125 respond_string("\033[0n", tty); /* Terminal ok */
1128 static inline void respond_ID(struct tty_struct * tty)
1130 respond_string(VT102ID, tty);
1133 void mouse_report(struct tty_struct * tty, int butt, int mrx, int mry)
1135 char buf[8];
1137 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1138 (char)('!' + mry));
1139 respond_string(buf, tty);
1142 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1143 int mouse_reporting(void)
1145 int currcons = fg_console;
1147 return report_mouse;
1150 static void set_mode(int currcons, int on_off)
1152 int i;
1154 for (i=0; i<=npar; i++)
1155 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1156 case 1: /* Cursor keys send ^[Ox/^[[x */
1157 if (on_off)
1158 set_kbd(decckm);
1159 else
1160 clr_kbd(decckm);
1161 break;
1162 case 3: /* 80/132 mode switch unimplemented */
1163 deccolm = on_off;
1164 #if 0
1165 (void) vc_resize(video_num_lines, deccolm ? 132 : 80);
1166 /* this alone does not suffice; some user mode
1167 utility has to change the hardware regs */
1168 #endif
1169 break;
1170 case 5: /* Inverted screen on/off */
1171 if (decscnm != on_off) {
1172 decscnm = on_off;
1173 invert_screen(currcons, 0, screenbuf_size, 0);
1174 update_attr(currcons);
1176 break;
1177 case 6: /* Origin relative/absolute */
1178 decom = on_off;
1179 gotoxay(currcons,0,0);
1180 break;
1181 case 7: /* Autowrap on/off */
1182 decawm = on_off;
1183 break;
1184 case 8: /* Autorepeat on/off */
1185 if (on_off)
1186 set_kbd(decarm);
1187 else
1188 clr_kbd(decarm);
1189 break;
1190 case 9:
1191 report_mouse = on_off ? 1 : 0;
1192 break;
1193 case 25: /* Cursor on/off */
1194 deccm = on_off;
1195 break;
1196 case 1000:
1197 report_mouse = on_off ? 2 : 0;
1198 break;
1199 } else switch(par[i]) { /* ANSI modes set/reset */
1200 case 3: /* Monitor (display ctrls) */
1201 disp_ctrl = on_off;
1202 break;
1203 case 4: /* Insert Mode on/off */
1204 decim = on_off;
1205 break;
1206 case 20: /* Lf, Enter == CrLf/Lf */
1207 if (on_off)
1208 set_kbd(lnm);
1209 else
1210 clr_kbd(lnm);
1211 break;
1215 static void setterm_command(int currcons)
1217 switch(par[0]) {
1218 case 1: /* set color for underline mode */
1219 if (can_do_color && par[1] < 16) {
1220 ulcolor = color_table[par[1]];
1221 if (underline)
1222 update_attr(currcons);
1224 break;
1225 case 2: /* set color for half intensity mode */
1226 if (can_do_color && par[1] < 16) {
1227 halfcolor = color_table[par[1]];
1228 if (intensity == 0)
1229 update_attr(currcons);
1231 break;
1232 case 8: /* store colors as defaults */
1233 def_color = attr;
1234 if (hi_font_mask == 0x100)
1235 def_color >>= 1;
1236 default_attr(currcons);
1237 update_attr(currcons);
1238 break;
1239 case 9: /* set blanking interval */
1240 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1241 poke_blanked_console();
1242 break;
1243 case 10: /* set bell frequency in Hz */
1244 if (npar >= 1)
1245 bell_pitch = par[1];
1246 else
1247 bell_pitch = DEFAULT_BELL_PITCH;
1248 break;
1249 case 11: /* set bell duration in msec */
1250 if (npar >= 1)
1251 bell_duration = (par[1] < 2000) ?
1252 par[1]*HZ/1000 : 0;
1253 else
1254 bell_duration = DEFAULT_BELL_DURATION;
1255 break;
1256 case 12: /* bring specified console to the front */
1257 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1258 set_console(par[1] - 1);
1259 break;
1260 case 13: /* unblank the screen */
1261 poke_blanked_console();
1262 break;
1263 case 14: /* set vesa powerdown interval */
1264 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1265 break;
1269 static void insert_line(int currcons, unsigned int nr)
1271 scrdown(currcons,y,bottom,nr);
1272 need_wrap = 0;
1276 static void delete_line(int currcons, unsigned int nr)
1278 scrup(currcons,y,bottom,nr);
1279 need_wrap = 0;
1282 static void csi_at(int currcons, unsigned int nr)
1284 if (nr > video_num_columns - x)
1285 nr = video_num_columns - x;
1286 else if (!nr)
1287 nr = 1;
1288 insert_char(currcons, nr);
1291 static void csi_L(int currcons, unsigned int nr)
1293 if (nr > video_num_lines - y)
1294 nr = video_num_lines - y;
1295 else if (!nr)
1296 nr = 1;
1297 insert_line(currcons, nr);
1300 static void csi_P(int currcons, unsigned int nr)
1302 if (nr > video_num_columns - x)
1303 nr = video_num_columns - x;
1304 else if (!nr)
1305 nr = 1;
1306 delete_char(currcons, nr);
1309 static void csi_M(int currcons, unsigned int nr)
1311 if (nr > video_num_lines - y)
1312 nr = video_num_lines - y;
1313 else if (!nr)
1314 nr=1;
1315 delete_line(currcons, nr);
1318 static void save_cur(int currcons)
1320 saved_x = x;
1321 saved_y = y;
1322 s_intensity = intensity;
1323 s_underline = underline;
1324 s_blink = blink;
1325 s_reverse = reverse;
1326 s_charset = charset;
1327 s_color = color;
1328 saved_G0 = G0_charset;
1329 saved_G1 = G1_charset;
1332 static void restore_cur(int currcons)
1334 gotoxy(currcons,saved_x,saved_y);
1335 intensity = s_intensity;
1336 underline = s_underline;
1337 blink = s_blink;
1338 reverse = s_reverse;
1339 charset = s_charset;
1340 color = s_color;
1341 G0_charset = saved_G0;
1342 G1_charset = saved_G1;
1343 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1344 update_attr(currcons);
1345 need_wrap = 0;
1348 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1349 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1350 ESpalette };
1352 static void reset_terminal(int currcons, int do_clear)
1354 top = 0;
1355 bottom = video_num_lines;
1356 vc_state = ESnormal;
1357 ques = 0;
1358 translate = set_translate(LAT1_MAP,currcons);
1359 G0_charset = LAT1_MAP;
1360 G1_charset = GRAF_MAP;
1361 charset = 0;
1362 need_wrap = 0;
1363 report_mouse = 0;
1364 utf = 0;
1365 utf_count = 0;
1367 disp_ctrl = 0;
1368 toggle_meta = 0;
1370 decscnm = 0;
1371 decom = 0;
1372 decawm = 1;
1373 deccm = 1;
1374 decim = 0;
1376 set_kbd(decarm);
1377 clr_kbd(decckm);
1378 clr_kbd(kbdapplic);
1379 clr_kbd(lnm);
1380 kbd_table[currcons].lockstate = 0;
1381 kbd_table[currcons].slockstate = 0;
1382 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1383 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1384 set_leds();
1386 cursor_type = CUR_DEFAULT;
1387 complement_mask = s_complement_mask;
1389 default_attr(currcons);
1390 update_attr(currcons);
1392 tab_stop[0] = 0x01010100;
1393 tab_stop[1] =
1394 tab_stop[2] =
1395 tab_stop[3] =
1396 tab_stop[4] = 0x01010101;
1398 bell_pitch = DEFAULT_BELL_PITCH;
1399 bell_duration = DEFAULT_BELL_DURATION;
1401 gotoxy(currcons,0,0);
1402 save_cur(currcons);
1403 if (do_clear)
1404 csi_J(currcons,2);
1407 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1410 * Control characters can be used in the _middle_
1411 * of an escape sequence.
1413 switch (c) {
1414 case 0:
1415 return;
1416 case 7:
1417 if (bell_duration)
1418 kd_mksound(bell_pitch, bell_duration);
1419 return;
1420 case 8:
1421 bs(currcons);
1422 return;
1423 case 9:
1424 pos -= (x << 1);
1425 while (x < video_num_columns - 1) {
1426 x++;
1427 if (tab_stop[x >> 5] & (1 << (x & 31)))
1428 break;
1430 pos += (x << 1);
1431 return;
1432 case 10: case 11: case 12:
1433 lf(currcons);
1434 if (!is_kbd(lnm))
1435 return;
1436 case 13:
1437 cr(currcons);
1438 return;
1439 case 14:
1440 charset = 1;
1441 translate = set_translate(G1_charset,currcons);
1442 disp_ctrl = 1;
1443 return;
1444 case 15:
1445 charset = 0;
1446 translate = set_translate(G0_charset,currcons);
1447 disp_ctrl = 0;
1448 return;
1449 case 24: case 26:
1450 vc_state = ESnormal;
1451 return;
1452 case 27:
1453 vc_state = ESesc;
1454 return;
1455 case 127:
1456 del(currcons);
1457 return;
1458 case 128+27:
1459 vc_state = ESsquare;
1460 return;
1462 switch(vc_state) {
1463 case ESesc:
1464 vc_state = ESnormal;
1465 switch (c) {
1466 case '[':
1467 vc_state = ESsquare;
1468 return;
1469 case ']':
1470 vc_state = ESnonstd;
1471 return;
1472 case '%':
1473 vc_state = ESpercent;
1474 return;
1475 case 'E':
1476 cr(currcons);
1477 lf(currcons);
1478 return;
1479 case 'M':
1480 ri(currcons);
1481 return;
1482 case 'D':
1483 lf(currcons);
1484 return;
1485 case 'H':
1486 tab_stop[x >> 5] |= (1 << (x & 31));
1487 return;
1488 case 'Z':
1489 respond_ID(tty);
1490 return;
1491 case '7':
1492 save_cur(currcons);
1493 return;
1494 case '8':
1495 restore_cur(currcons);
1496 return;
1497 case '(':
1498 vc_state = ESsetG0;
1499 return;
1500 case ')':
1501 vc_state = ESsetG1;
1502 return;
1503 case '#':
1504 vc_state = EShash;
1505 return;
1506 case 'c':
1507 reset_terminal(currcons,1);
1508 return;
1509 case '>': /* Numeric keypad */
1510 clr_kbd(kbdapplic);
1511 return;
1512 case '=': /* Appl. keypad */
1513 set_kbd(kbdapplic);
1514 return;
1516 return;
1517 case ESnonstd:
1518 if (c=='P') { /* palette escape sequence */
1519 for (npar=0; npar<NPAR; npar++)
1520 par[npar] = 0 ;
1521 npar = 0 ;
1522 vc_state = ESpalette;
1523 return;
1524 } else if (c=='R') { /* reset palette */
1525 reset_palette(currcons);
1526 vc_state = ESnormal;
1527 } else
1528 vc_state = ESnormal;
1529 return;
1530 case ESpalette:
1531 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1532 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1533 if (npar==7) {
1534 int i = par[0]*3, j = 1;
1535 palette[i] = 16*par[j++];
1536 palette[i++] += par[j++];
1537 palette[i] = 16*par[j++];
1538 palette[i++] += par[j++];
1539 palette[i] = 16*par[j++];
1540 palette[i] += par[j];
1541 set_palette(currcons);
1542 vc_state = ESnormal;
1544 } else
1545 vc_state = ESnormal;
1546 return;
1547 case ESsquare:
1548 for(npar = 0 ; npar < NPAR ; npar++)
1549 par[npar] = 0;
1550 npar = 0;
1551 vc_state = ESgetpars;
1552 if (c == '[') { /* Function key */
1553 vc_state=ESfunckey;
1554 return;
1556 ques = (c=='?');
1557 if (ques)
1558 return;
1559 case ESgetpars:
1560 if (c==';' && npar<NPAR-1) {
1561 npar++;
1562 return;
1563 } else if (c>='0' && c<='9') {
1564 par[npar] *= 10;
1565 par[npar] += c-'0';
1566 return;
1567 } else vc_state=ESgotpars;
1568 case ESgotpars:
1569 vc_state = ESnormal;
1570 switch(c) {
1571 case 'h':
1572 set_mode(currcons,1);
1573 return;
1574 case 'l':
1575 set_mode(currcons,0);
1576 return;
1577 case 'c':
1578 if (ques) {
1579 if (par[0])
1580 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1581 else
1582 cursor_type = CUR_DEFAULT;
1583 return;
1585 break;
1586 case 'm':
1587 if (ques) {
1588 clear_selection();
1589 if (par[0])
1590 complement_mask = par[0]<<8 | par[1];
1591 else
1592 complement_mask = s_complement_mask;
1593 return;
1595 break;
1596 case 'n':
1597 if (!ques) {
1598 if (par[0] == 5)
1599 status_report(tty);
1600 else if (par[0] == 6)
1601 cursor_report(currcons,tty);
1603 return;
1605 if (ques) {
1606 ques = 0;
1607 return;
1609 switch(c) {
1610 case 'G': case '`':
1611 if (par[0]) par[0]--;
1612 gotoxy(currcons,par[0],y);
1613 return;
1614 case 'A':
1615 if (!par[0]) par[0]++;
1616 gotoxy(currcons,x,y-par[0]);
1617 return;
1618 case 'B': case 'e':
1619 if (!par[0]) par[0]++;
1620 gotoxy(currcons,x,y+par[0]);
1621 return;
1622 case 'C': case 'a':
1623 if (!par[0]) par[0]++;
1624 gotoxy(currcons,x+par[0],y);
1625 return;
1626 case 'D':
1627 if (!par[0]) par[0]++;
1628 gotoxy(currcons,x-par[0],y);
1629 return;
1630 case 'E':
1631 if (!par[0]) par[0]++;
1632 gotoxy(currcons,0,y+par[0]);
1633 return;
1634 case 'F':
1635 if (!par[0]) par[0]++;
1636 gotoxy(currcons,0,y-par[0]);
1637 return;
1638 case 'd':
1639 if (par[0]) par[0]--;
1640 gotoxay(currcons,x,par[0]);
1641 return;
1642 case 'H': case 'f':
1643 if (par[0]) par[0]--;
1644 if (par[1]) par[1]--;
1645 gotoxay(currcons,par[1],par[0]);
1646 return;
1647 case 'J':
1648 csi_J(currcons,par[0]);
1649 return;
1650 case 'K':
1651 csi_K(currcons,par[0]);
1652 return;
1653 case 'L':
1654 csi_L(currcons,par[0]);
1655 return;
1656 case 'M':
1657 csi_M(currcons,par[0]);
1658 return;
1659 case 'P':
1660 csi_P(currcons,par[0]);
1661 return;
1662 case 'c':
1663 if (!par[0])
1664 respond_ID(tty);
1665 return;
1666 case 'g':
1667 if (!par[0])
1668 tab_stop[x >> 5] &= ~(1 << (x & 31));
1669 else if (par[0] == 3) {
1670 tab_stop[0] =
1671 tab_stop[1] =
1672 tab_stop[2] =
1673 tab_stop[3] =
1674 tab_stop[4] = 0;
1676 return;
1677 case 'm':
1678 csi_m(currcons);
1679 return;
1680 case 'q': /* DECLL - but only 3 leds */
1681 /* map 0,1,2,3 to 0,1,2,4 */
1682 if (par[0] < 4)
1683 setledstate(kbd_table + currcons,
1684 (par[0] < 3) ? par[0] : 4);
1685 return;
1686 case 'r':
1687 if (!par[0])
1688 par[0]++;
1689 if (!par[1])
1690 par[1] = video_num_lines;
1691 /* Minimum allowed region is 2 lines */
1692 if (par[0] < par[1] &&
1693 par[1] <= video_num_lines) {
1694 top=par[0]-1;
1695 bottom=par[1];
1696 gotoxay(currcons,0,0);
1698 return;
1699 case 's':
1700 save_cur(currcons);
1701 return;
1702 case 'u':
1703 restore_cur(currcons);
1704 return;
1705 case 'X':
1706 csi_X(currcons, par[0]);
1707 return;
1708 case '@':
1709 csi_at(currcons,par[0]);
1710 return;
1711 case ']': /* setterm functions */
1712 setterm_command(currcons);
1713 return;
1715 return;
1716 case ESpercent:
1717 vc_state = ESnormal;
1718 switch (c) {
1719 case '@': /* defined in ISO 2022 */
1720 utf = 0;
1721 return;
1722 case 'G': /* prelim official escape code */
1723 case '8': /* retained for compatibility */
1724 utf = 1;
1725 return;
1727 return;
1728 case ESfunckey:
1729 vc_state = ESnormal;
1730 return;
1731 case EShash:
1732 vc_state = ESnormal;
1733 if (c == '8') {
1734 /* DEC screen alignment test. kludge :-) */
1735 video_erase_char =
1736 (video_erase_char & 0xff00) | 'E';
1737 csi_J(currcons, 2);
1738 video_erase_char =
1739 (video_erase_char & 0xff00) | ' ';
1740 do_update_region(currcons, origin, screenbuf_size/2);
1742 return;
1743 case ESsetG0:
1744 if (c == '0')
1745 G0_charset = GRAF_MAP;
1746 else if (c == 'B')
1747 G0_charset = LAT1_MAP;
1748 else if (c == 'U')
1749 G0_charset = IBMPC_MAP;
1750 else if (c == 'K')
1751 G0_charset = USER_MAP;
1752 if (charset == 0)
1753 translate = set_translate(G0_charset,currcons);
1754 vc_state = ESnormal;
1755 return;
1756 case ESsetG1:
1757 if (c == '0')
1758 G1_charset = GRAF_MAP;
1759 else if (c == 'B')
1760 G1_charset = LAT1_MAP;
1761 else if (c == 'U')
1762 G1_charset = IBMPC_MAP;
1763 else if (c == 'K')
1764 G1_charset = USER_MAP;
1765 if (charset == 1)
1766 translate = set_translate(G1_charset,currcons);
1767 vc_state = ESnormal;
1768 return;
1769 default:
1770 vc_state = ESnormal;
1774 static int do_con_write(struct tty_struct * tty, int from_user,
1775 const unsigned char *buf, int count)
1777 #ifdef VT_BUF_VRAM_ONLY
1778 #define FLUSH do { } while(0);
1779 #else
1780 #define FLUSH if (draw_x >= 0) { \
1781 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1782 draw_x = -1; \
1784 #endif
1786 int c, tc, ok, n = 0, draw_x = -1;
1787 unsigned int currcons;
1788 unsigned long draw_from = 0, draw_to = 0;
1789 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
1790 u16 himask, charmask;
1792 currcons = vt->vc_num;
1793 if (!vc_cons_allocated(currcons)) {
1794 /* could this happen? */
1795 static int error = 0;
1796 if (!error) {
1797 error = 1;
1798 printk("con_write: tty %d not allocated\n", currcons+1);
1800 return 0;
1803 if (from_user) {
1804 /* just to make sure that noone lurks at places he shouldn't see. */
1805 if (verify_area(VERIFY_READ, buf, count))
1806 return 0; /* ?? are error codes legal here ?? */
1809 himask = hi_font_mask;
1810 charmask = himask ? 0x1ff : 0xff;
1812 /* undraw cursor first */
1813 if (IS_FG)
1814 hide_cursor(currcons);
1816 disable_bh(CONSOLE_BH);
1817 while (!tty->stopped && count) {
1818 enable_bh(CONSOLE_BH);
1819 if (from_user)
1820 __get_user(c, buf);
1821 else
1822 c = *buf;
1823 buf++; n++; count--;
1824 disable_bh(CONSOLE_BH);
1826 if (utf) {
1827 /* Combine UTF-8 into Unicode */
1828 /* Incomplete characters silently ignored */
1829 if(c > 0x7f) {
1830 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1831 utf_char = (utf_char << 6) | (c & 0x3f);
1832 utf_count--;
1833 if (utf_count == 0)
1834 tc = c = utf_char;
1835 else continue;
1836 } else {
1837 if ((c & 0xe0) == 0xc0) {
1838 utf_count = 1;
1839 utf_char = (c & 0x1f);
1840 } else if ((c & 0xf0) == 0xe0) {
1841 utf_count = 2;
1842 utf_char = (c & 0x0f);
1843 } else if ((c & 0xf8) == 0xf0) {
1844 utf_count = 3;
1845 utf_char = (c & 0x07);
1846 } else if ((c & 0xfc) == 0xf8) {
1847 utf_count = 4;
1848 utf_char = (c & 0x03);
1849 } else if ((c & 0xfe) == 0xfc) {
1850 utf_count = 5;
1851 utf_char = (c & 0x01);
1852 } else
1853 utf_count = 0;
1854 continue;
1856 } else {
1857 tc = c;
1858 utf_count = 0;
1860 } else { /* no utf */
1861 tc = translate[toggle_meta ? (c|0x80) : c];
1864 /* If the original code was a control character we
1865 * only allow a glyph to be displayed if the code is
1866 * not normally used (such as for cursor movement) or
1867 * if the disp_ctrl mode has been explicitly enabled.
1868 * Certain characters (as given by the CTRL_ALWAYS
1869 * bitmap) are always displayed as control characters,
1870 * as the console would be pretty useless without
1871 * them; to display an arbitrary font position use the
1872 * direct-to-font zone in UTF-8 mode.
1874 ok = tc && (c >= 32 ||
1875 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
1876 : CTRL_ACTION) >> c) & 1)))
1877 && (c != 127 || disp_ctrl)
1878 && (c != 128+27);
1880 if (vc_state == ESnormal && ok) {
1881 /* Now try to find out how to display it */
1882 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
1883 if ( tc == -4 ) {
1884 /* If we got -4 (not found) then see if we have
1885 defined a replacement character (U+FFFD) */
1886 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
1888 /* One reason for the -4 can be that we just
1889 did a clear_unimap();
1890 try at least to show something. */
1891 if (tc == -4)
1892 tc = c;
1893 } else if ( tc == -3 ) {
1894 /* Bad hash table -- hope for the best */
1895 tc = c;
1897 if (tc & ~charmask)
1898 continue; /* Conversion failed */
1900 if (need_wrap || decim)
1901 FLUSH
1902 if (need_wrap) {
1903 cr(currcons);
1904 lf(currcons);
1906 if (decim)
1907 insert_char(currcons, 1);
1908 scr_writew(himask ?
1909 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
1910 (attr << 8) + tc,
1911 (u16 *) pos);
1912 if (DO_UPDATE && draw_x < 0) {
1913 draw_x = x;
1914 draw_from = pos;
1916 if (x == video_num_columns - 1) {
1917 need_wrap = decawm;
1918 draw_to = pos+2;
1919 } else {
1920 x++;
1921 draw_to = (pos+=2);
1923 continue;
1925 FLUSH
1926 do_con_trol(tty, currcons, c);
1928 FLUSH
1929 enable_bh(CONSOLE_BH);
1930 return n;
1931 #undef FLUSH
1935 * This is the console switching bottom half handler.
1937 * Doing console switching in a bottom half handler allows
1938 * us to do the switches asynchronously (needed when we want
1939 * to switch due to a keyboard interrupt), while still giving
1940 * us the option to easily disable it to avoid races when we
1941 * need to write to the console.
1943 static void console_bh(void)
1945 run_task_queue(&con_task_queue);
1946 if (want_console >= 0) {
1947 if (want_console != fg_console && vc_cons_allocated(want_console)) {
1948 hide_cursor(fg_console);
1949 change_console(want_console);
1950 /* we only changed when the console had already
1951 been allocated - a new console is not created
1952 in an interrupt routine */
1954 want_console = -1;
1956 if (do_poke_blanked_console) { /* do not unblank for a LED change */
1957 do_poke_blanked_console = 0;
1958 poke_blanked_console();
1960 if (scrollback_delta) {
1961 int currcons = fg_console;
1962 clear_selection();
1963 if (vcmode == KD_TEXT)
1964 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
1965 scrollback_delta = 0;
1969 #ifdef CONFIG_VT_CONSOLE
1972 * Console on virtual terminal
1974 * NOTE NOTE NOTE! This code can do no global locking. In particular,
1975 * we can't disable interrupts or bottom half handlers globally, because
1976 * we can be called from contexts that hold critical spinlocks, and
1977 * trying do get a global lock at this point will lead to deadlocks.
1980 void vt_console_print(struct console *co, const char * b, unsigned count)
1982 int currcons = fg_console;
1983 unsigned char c;
1984 static unsigned long printing = 0;
1985 const ushort *start;
1986 ushort cnt = 0;
1987 ushort myx;
1989 /* console busy or not yet initialized */
1990 if (!printable || test_and_set_bit(0, &printing))
1991 return;
1993 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
1994 currcons = kmsg_redirect - 1;
1996 /* read `x' only after setting currecons properly (otherwise
1997 the `x' macro will read the x of the foreground console). */
1998 myx = x;
2000 if (!vc_cons_allocated(currcons)) {
2001 /* impossible */
2002 printk("vt_console_print: tty %d not allocated ??\n", currcons+1);
2003 goto quit;
2006 if (vcmode != KD_TEXT)
2007 goto quit;
2009 /* undraw cursor first */
2010 if (IS_FG)
2011 hide_cursor(currcons);
2013 start = (ushort *)pos;
2015 /* Contrived structure to try to emulate original need_wrap behaviour
2016 * Problems caused when we have need_wrap set on '\n' character */
2017 while (count--) {
2018 c = *b++;
2019 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2020 if (cnt > 0) {
2021 if (IS_VISIBLE)
2022 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2023 x += cnt;
2024 if (need_wrap)
2025 x--;
2026 cnt = 0;
2028 if (c == 8) { /* backspace */
2029 bs(currcons);
2030 start = (ushort *)pos;
2031 myx = x;
2032 continue;
2034 if (c != 13)
2035 lf(currcons);
2036 cr(currcons);
2037 start = (ushort *)pos;
2038 myx = x;
2039 if (c == 10 || c == 13)
2040 continue;
2042 scr_writew((attr << 8) + c, (unsigned short *) pos);
2043 cnt++;
2044 if (myx == video_num_columns - 1) {
2045 need_wrap = 1;
2046 continue;
2048 pos+=2;
2049 myx++;
2051 if (cnt > 0) {
2052 if (IS_VISIBLE)
2053 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2054 x += cnt;
2055 if (x == video_num_columns) {
2056 x--;
2057 need_wrap = 1;
2060 set_cursor(currcons);
2061 poke_blanked_console();
2063 quit:
2064 clear_bit(0, &printing);
2067 static kdev_t vt_console_device(struct console *c)
2069 return MKDEV(TTY_MAJOR, c->index ? c->index : fg_console + 1);
2072 struct console vt_console_driver = {
2073 "tty",
2074 vt_console_print,
2075 NULL,
2076 vt_console_device,
2077 keyboard_wait_for_keypress,
2078 unblank_screen,
2079 NULL,
2080 CON_PRINTBUFFER,
2083 NULL
2085 #endif
2088 * Handling of Linux-specific VC ioctls
2091 int tioclinux(struct tty_struct *tty, unsigned long arg)
2093 char type, data;
2095 if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
2096 return -EINVAL;
2097 if (current->tty != tty && !suser())
2098 return -EPERM;
2099 if (get_user(type, (char *)arg))
2100 return -EFAULT;
2101 switch (type)
2103 case 2:
2104 return set_selection(arg, tty, 1);
2105 case 3:
2106 return paste_selection(tty);
2107 case 4:
2108 unblank_screen();
2109 return 0;
2110 case 5:
2111 return sel_loadlut(arg);
2112 case 6:
2115 * Make it possible to react to Shift+Mousebutton.
2116 * Note that 'shift_state' is an undocumented
2117 * kernel-internal variable; programs not closely
2118 * related to the kernel should not use this.
2120 data = shift_state;
2121 return __put_user(data, (char *) arg);
2122 case 7:
2123 data = mouse_reporting();
2124 return __put_user(data, (char *) arg);
2125 case 10:
2126 set_vesa_blanking(arg);
2127 return 0;
2128 case 11: /* set kmsg redirect */
2129 if (!suser())
2130 return -EPERM;
2131 if (get_user(data, (char *)arg+1))
2132 return -EFAULT;
2133 kmsg_redirect = data;
2134 return 0;
2135 case 12: /* get fg_console */
2136 return fg_console;
2138 return -EINVAL;
2142 * /dev/ttyN handling
2145 static int con_write(struct tty_struct * tty, int from_user,
2146 const unsigned char *buf, int count)
2148 int retval;
2150 retval = do_con_write(tty, from_user, buf, count);
2151 con_flush_chars(tty);
2153 return retval;
2156 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2158 do_con_write(tty, 0, &ch, 1);
2161 static int con_write_room(struct tty_struct *tty)
2163 if (tty->stopped)
2164 return 0;
2165 return 4096; /* No limit, really; we're not buffering */
2168 static int con_chars_in_buffer(struct tty_struct *tty)
2170 return 0; /* we're not buffering */
2174 * con_throttle and con_unthrottle are only used for
2175 * paste_selection(), which has to stuff in a large number of
2176 * characters...
2178 static void con_throttle(struct tty_struct *tty)
2182 static void con_unthrottle(struct tty_struct *tty)
2184 struct vt_struct *vt = (struct vt_struct *) tty->driver_data;
2186 wake_up_interruptible(&vt->paste_wait);
2190 * Turn the Scroll-Lock LED on when the tty is stopped
2192 static void con_stop(struct tty_struct *tty)
2194 int console_num;
2195 if (!tty)
2196 return;
2197 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2198 if (!vc_cons_allocated(console_num))
2199 return;
2200 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2201 set_leds();
2205 * Turn the Scroll-Lock LED off when the console is started
2207 static void con_start(struct tty_struct *tty)
2209 int console_num;
2210 if (!tty)
2211 return;
2212 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2213 if (!vc_cons_allocated(console_num))
2214 return;
2215 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2216 set_leds();
2219 static void con_flush_chars(struct tty_struct *tty)
2221 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
2223 set_cursor(vt->vc_num);
2227 * Allocate the console screen memory.
2229 static int con_open(struct tty_struct *tty, struct file * filp)
2231 unsigned int currcons;
2232 int i;
2234 currcons = MINOR(tty->device) - tty->driver.minor_start;
2236 i = vc_allocate(currcons);
2237 if (i)
2238 return i;
2240 vt_cons[currcons]->vc_num = currcons;
2241 tty->driver_data = vt_cons[currcons];
2243 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2244 tty->winsize.ws_row = video_num_lines;
2245 tty->winsize.ws_col = video_num_columns;
2247 return 0;
2250 static void con_close(struct tty_struct *tty, struct file * filp)
2252 if (tty->count == 1)
2253 tty->driver_data = 0;
2256 static void vc_init(unsigned int currcons, unsigned int rows, unsigned int cols, int do_clear)
2258 int j, k ;
2260 video_num_columns = cols;
2261 video_num_lines = rows;
2262 video_size_row = cols<<1;
2263 screenbuf_size = video_num_lines * video_size_row;
2265 set_origin(currcons);
2266 pos = origin;
2267 reset_vc(currcons);
2268 for (j=k=0; j<16; j++) {
2269 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2270 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2271 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2273 def_color = 0x07; /* white */
2274 ulcolor = 0x0f; /* bold white */
2275 halfcolor = 0x08; /* grey */
2276 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2277 reset_terminal(currcons, do_clear);
2281 * This routine initializes console interrupts, and does nothing
2282 * else. If you want the screen to clear, call tty_write with
2283 * the appropriate escape-sequence.
2286 struct tty_driver console_driver;
2287 static int console_refcount;
2289 unsigned long __init con_init(unsigned long kmem_start)
2291 const char *display_desc = NULL;
2292 unsigned int currcons = 0;
2294 if (conswitchp)
2295 display_desc = conswitchp->con_startup();
2296 if (!display_desc) {
2297 fg_console = 0;
2298 return kmem_start;
2301 memset(&console_driver, 0, sizeof(struct tty_driver));
2302 console_driver.magic = TTY_DRIVER_MAGIC;
2303 console_driver.name = "tty";
2304 console_driver.name_base = 1;
2305 console_driver.major = TTY_MAJOR;
2306 console_driver.minor_start = 1;
2307 console_driver.num = MAX_NR_CONSOLES;
2308 console_driver.type = TTY_DRIVER_TYPE_CONSOLE;
2309 console_driver.init_termios = tty_std_termios;
2310 console_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2311 console_driver.refcount = &console_refcount;
2312 console_driver.table = console_table;
2313 console_driver.termios = console_termios;
2314 console_driver.termios_locked = console_termios_locked;
2316 console_driver.open = con_open;
2317 console_driver.close = con_close;
2318 console_driver.write = con_write;
2319 console_driver.write_room = con_write_room;
2320 console_driver.put_char = con_put_char;
2321 console_driver.flush_chars = con_flush_chars;
2322 console_driver.chars_in_buffer = con_chars_in_buffer;
2323 console_driver.ioctl = vt_ioctl;
2324 console_driver.stop = con_stop;
2325 console_driver.start = con_start;
2326 console_driver.throttle = con_throttle;
2327 console_driver.unthrottle = con_unthrottle;
2329 if (tty_register_driver(&console_driver))
2330 panic("Couldn't register console driver\n");
2332 timer_table[BLANK_TIMER].fn = blank_screen;
2333 timer_table[BLANK_TIMER].expires = 0;
2334 if (blankinterval) {
2335 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2336 timer_active |= 1<<BLANK_TIMER;
2339 /* Unfortunately, kmalloc is not running yet */
2340 /* Due to kmalloc roundup allocating statically is more efficient -
2341 so provide MIN_NR_CONSOLES for people with very little memory */
2342 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2343 int j, k ;
2345 vc_cons[currcons].d = (struct vc_data *) kmem_start;
2346 kmem_start += sizeof(struct vc_data);
2347 vt_cons[currcons] = (struct vt_struct *) kmem_start;
2348 kmem_start += sizeof(struct vt_struct);
2349 visual_init(currcons, 1);
2350 screenbuf = (unsigned short *) kmem_start;
2351 kmem_start += screenbuf_size;
2352 kmalloced = 0;
2353 vc_init(currcons, video_num_lines, video_num_columns,
2354 currcons || !sw->con_save_screen);
2355 for (j=k=0; j<16; j++) {
2356 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2357 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2358 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2361 currcons = fg_console = 0;
2362 master_display_fg = vc_cons[currcons].d;
2363 set_origin(currcons);
2364 save_screen(currcons);
2365 gotoxy(currcons,x,y);
2366 csi_J(currcons, 0);
2367 update_screen(fg_console);
2368 printk("Console: %s %s %dx%d",
2369 can_do_color ? "colour" : "mono",
2370 display_desc, video_num_columns, video_num_lines);
2371 printable = 1;
2372 printk("\n");
2374 #ifdef CONFIG_VT_CONSOLE
2375 register_console(&vt_console_driver);
2376 #endif
2378 init_bh(CONSOLE_BH, console_bh);
2380 return kmem_start;
2383 #ifndef VT_SINGLE_DRIVER
2385 static void clear_buffer_attributes(int currcons)
2387 unsigned short *p = (unsigned short *) origin;
2388 int count = screenbuf_size/2;
2389 int mask = hi_font_mask | 0xff;
2391 for (; count > 0; count--, p++) {
2392 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2397 * If we support more console drivers, this function is used
2398 * when a driver wants to take over some existing consoles
2399 * and become default driver for newly opened ones.
2402 void take_over_console(struct consw *csw, int first, int last, int deflt)
2404 int i, j = -1;
2405 const char *desc;
2407 desc = csw->con_startup();
2408 if (!desc) return;
2409 if (deflt)
2410 conswitchp = csw;
2412 for (i = first; i <= last; i++) {
2413 int old_was_color;
2414 int currcons = i;
2416 con_driver_map[i] = csw;
2418 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2419 continue;
2421 j = i;
2422 if (IS_VISIBLE)
2423 save_screen(i);
2424 old_was_color = vc_cons[i].d->vc_can_do_color;
2425 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2426 visual_init(i, 0);
2427 update_attr(i);
2429 /* If the console changed between mono <-> color, then
2430 * the attributes in the screenbuf will be wrong. The
2431 * following resets all attributes to something sane.
2433 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2434 clear_buffer_attributes(i);
2436 if (IS_VISIBLE)
2437 update_screen(i);
2439 printk("Console: switching ");
2440 if (!deflt)
2441 printk("consoles %d-%d ", first+1, last+1);
2442 if (j >= 0)
2443 printk("to %s %s %dx%d\n",
2444 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2445 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2446 else
2447 printk("to %s\n", desc);
2450 void give_up_console(struct consw *csw)
2452 int i;
2454 for(i = 0; i < MAX_NR_CONSOLES; i++)
2455 if (con_driver_map[i] == csw)
2456 con_driver_map[i] = NULL;
2459 #endif
2462 * Screen blanking
2465 static void set_vesa_blanking(unsigned long arg)
2467 char *argp = (char *)arg + 1;
2468 unsigned int mode;
2469 get_user(mode, argp);
2470 vesa_blank_mode = (mode < 4) ? mode : 0;
2473 static void vesa_powerdown(void)
2475 struct vc_data *c = vc_cons[fg_console].d;
2477 * Power down if currently suspended (1 or 2),
2478 * suspend if currently blanked (0),
2479 * else do nothing (i.e. already powered down (3)).
2480 * Called only if powerdown features are allowed.
2482 switch (vesa_blank_mode) {
2483 case VESA_NO_BLANKING:
2484 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1);
2485 break;
2486 case VESA_VSYNC_SUSPEND:
2487 case VESA_HSYNC_SUSPEND:
2488 c->vc_sw->con_blank(c, VESA_POWERDOWN+1);
2489 break;
2493 static void vesa_powerdown_screen(void)
2495 timer_active &= ~(1<<BLANK_TIMER);
2496 timer_table[BLANK_TIMER].fn = unblank_screen;
2498 vesa_powerdown();
2501 void do_blank_screen(int entering_gfx)
2503 int currcons = fg_console;
2504 int i;
2506 if (console_blanked)
2507 return;
2509 /* entering graphics mode? */
2510 if (entering_gfx) {
2511 hide_cursor(currcons);
2512 save_screen(currcons);
2513 sw->con_blank(vc_cons[currcons].d, -1);
2514 console_blanked = fg_console + 1;
2515 set_origin(currcons);
2516 return;
2519 /* don't blank graphics */
2520 if (vcmode != KD_TEXT) {
2521 console_blanked = fg_console + 1;
2522 return;
2525 hide_cursor(currcons);
2526 if (vesa_off_interval) {
2527 timer_table[BLANK_TIMER].fn = vesa_powerdown_screen;
2528 timer_table[BLANK_TIMER].expires = jiffies + vesa_off_interval;
2529 timer_active |= (1<<BLANK_TIMER);
2530 } else {
2531 timer_active &= ~(1<<BLANK_TIMER);
2532 timer_table[BLANK_TIMER].fn = unblank_screen;
2535 save_screen(currcons);
2536 /* In case we need to reset origin, blanking hook returns 1 */
2537 i = sw->con_blank(vc_cons[currcons].d, 1);
2538 console_blanked = fg_console + 1;
2539 if (i)
2540 set_origin(currcons);
2542 #ifdef CONFIG_APM
2543 if (apm_display_blank())
2544 return;
2545 #endif
2546 if (vesa_blank_mode)
2547 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1);
2550 void unblank_screen(void)
2552 int currcons;
2554 if (!console_blanked)
2555 return;
2556 if (!vc_cons_allocated(fg_console)) {
2557 /* impossible */
2558 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2559 return;
2561 timer_table[BLANK_TIMER].fn = blank_screen;
2562 if (blankinterval) {
2563 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2564 timer_active |= 1<<BLANK_TIMER;
2567 currcons = fg_console;
2568 console_blanked = 0;
2569 #ifdef CONFIG_APM
2570 apm_display_unblank();
2571 #endif
2572 if (sw->con_blank(vc_cons[currcons].d, 0))
2573 /* Low-level driver cannot restore -> do it ourselves */
2574 update_screen(fg_console);
2575 set_cursor(fg_console);
2578 static void blank_screen(void)
2580 do_blank_screen(0);
2583 void poke_blanked_console(void)
2585 timer_active &= ~(1<<BLANK_TIMER);
2586 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2587 return;
2588 if (console_blanked) {
2589 timer_table[BLANK_TIMER].fn = unblank_screen;
2590 timer_table[BLANK_TIMER].expires = jiffies; /* Now */
2591 timer_active |= 1<<BLANK_TIMER;
2592 } else if (blankinterval) {
2593 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2594 timer_active |= 1<<BLANK_TIMER;
2599 * Palettes
2602 void set_palette(int currcons)
2604 if (vcmode != KD_GRAPHICS)
2605 sw->con_set_palette(vc_cons[currcons].d, color_table);
2608 static int set_get_cmap(unsigned char *arg, int set)
2610 int i, j, k;
2612 for (i = 0; i < 16; i++)
2613 if (set) {
2614 get_user(default_red[i], arg++);
2615 get_user(default_grn[i], arg++);
2616 get_user(default_blu[i], arg++);
2617 } else {
2618 put_user(default_red[i], arg++);
2619 put_user(default_grn[i], arg++);
2620 put_user(default_blu[i], arg++);
2622 if (set) {
2623 for (i = 0; i < MAX_NR_CONSOLES; i++)
2624 if (vc_cons_allocated(i)) {
2625 for (j = k = 0; j < 16; j++) {
2626 vc_cons[i].d->vc_palette[k++] = default_red[j];
2627 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2628 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2630 set_palette(i);
2633 return 0;
2637 * Load palette into the DAC registers. arg points to a colour
2638 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2641 int con_set_cmap(unsigned char *arg)
2643 return set_get_cmap (arg,1);
2646 int con_get_cmap(unsigned char *arg)
2648 return set_get_cmap (arg,0);
2651 void reset_palette(int currcons)
2653 int j, k;
2654 for (j=k=0; j<16; j++) {
2655 palette[k++] = default_red[j];
2656 palette[k++] = default_grn[j];
2657 palette[k++] = default_blu[j];
2659 set_palette(currcons);
2663 * Font switching
2665 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2666 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2667 * depending on width) reserved for each character which is kinda wasty, but
2668 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2669 * is upto the actual low-level console-driver convert data into its favorite
2670 * format (maybe we should add a `fontoffset' field to the `display'
2671 * structure so we wont have to convert the fontdata all the time.
2672 * /Jes
2675 #define max_font_size 65536
2677 int con_font_op(int currcons, struct console_font_op *op)
2679 int rc = -EINVAL;
2680 int size = max_font_size, set;
2681 u8 *temp = NULL;
2682 struct console_font_op old_op;
2684 if (vt_cons[currcons]->vc_mode != KD_TEXT)
2685 goto quit;
2686 memcpy(&old_op, op, sizeof(old_op));
2687 if (op->op == KD_FONT_OP_SET) {
2688 if (!op->data)
2689 return -EINVAL;
2690 if (op->charcount > 512)
2691 goto quit;
2692 if (!op->height) { /* Need to guess font height [compat] */
2693 int h, i;
2694 u8 *charmap = op->data, tmp;
2696 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2697 so that we can get rid of this soon */
2698 if (!(op->flags & KD_FONT_FLAG_OLD))
2699 goto quit;
2700 rc = -EFAULT;
2701 for (h = 32; h > 0; h--)
2702 for (i = 0; i < op->charcount; i++) {
2703 if (get_user(tmp, &charmap[32*i+h-1]))
2704 goto quit;
2705 if (tmp)
2706 goto nonzero;
2708 rc = -EINVAL;
2709 goto quit;
2710 nonzero:
2711 rc = -EINVAL;
2712 op->height = h;
2714 if (op->width > 32 || op->height > 32)
2715 goto quit;
2716 size = (op->width+7)/8 * 32 * op->charcount;
2717 if (size > max_font_size)
2718 return -ENOSPC;
2719 set = 1;
2720 } else if (op->op == KD_FONT_OP_GET)
2721 set = 0;
2722 else
2723 return sw->con_font_op(vc_cons[currcons].d, op);
2724 if (op->data) {
2725 temp = kmalloc(size, GFP_KERNEL);
2726 if (!temp)
2727 return -ENOMEM;
2728 if (set && copy_from_user(temp, op->data, size)) {
2729 rc = -EFAULT;
2730 goto quit;
2732 op->data = temp;
2734 disable_bh(CONSOLE_BH);
2735 rc = sw->con_font_op(vc_cons[currcons].d, op);
2736 enable_bh(CONSOLE_BH);
2737 op->data = old_op.data;
2738 if (!rc && !set) {
2739 int c = (op->width+7)/8 * 32 * op->charcount;
2741 if (op->data && op->charcount > old_op.charcount)
2742 rc = -ENOSPC;
2743 if (!(op->flags & KD_FONT_FLAG_OLD)) {
2744 if (op->width > old_op.width ||
2745 op->height > old_op.height)
2746 rc = -ENOSPC;
2747 } else {
2748 if (op->width != 8)
2749 rc = -EIO;
2750 else if ((old_op.height && op->height > old_op.height) ||
2751 op->height > 32)
2752 rc = -ENOSPC;
2754 if (!rc && op->data && copy_to_user(op->data, temp, c))
2755 rc = -EFAULT;
2757 quit: if (temp)
2758 kfree_s(temp, size);
2759 return rc;
2763 * Interface exported to selection and vcs.
2766 /* used by selection */
2767 u16 screen_glyph(int currcons, int offset)
2769 u16 w = scr_readw(screenpos(currcons, offset, 1));
2770 u16 c = w & 0xff;
2772 if (w & hi_font_mask)
2773 c |= 0x100;
2774 return c;
2777 /* used by vcs - note the word offset */
2778 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
2780 return screenpos(currcons, 2 * w_offset, viewed);
2783 void getconsxy(int currcons, char *p)
2785 p[0] = x;
2786 p[1] = y;
2789 void putconsxy(int currcons, char *p)
2791 gotoxy(currcons, p[0], p[1]);
2792 set_cursor(currcons);
2795 u16 vcs_scr_readw(int currcons, const u16 *org)
2797 if ((unsigned long)org == pos && softcursor_original != -1)
2798 return softcursor_original;
2799 return scr_readw(org);
2802 void vcs_scr_writew(int currcons, u16 val, u16 *org)
2804 scr_writew(val, org);
2805 if ((unsigned long)org == pos) {
2806 softcursor_original = -1;
2807 add_softcursor(currcons);
2813 * Visible symbols for modules
2816 EXPORT_SYMBOL(color_table);
2817 EXPORT_SYMBOL(default_red);
2818 EXPORT_SYMBOL(default_grn);
2819 EXPORT_SYMBOL(default_blu);
2820 EXPORT_SYMBOL(video_font_height);
2821 EXPORT_SYMBOL(video_scan_lines);
2822 EXPORT_SYMBOL(vc_resize);
2824 #ifndef VT_SINGLE_DRIVER
2825 EXPORT_SYMBOL(take_over_console);
2826 EXPORT_SYMBOL(give_up_console);
2827 #endif