Import 2.3.47pre4
[davej-history.git] / drivers / char / console.c
blobe8a87dd8d3a0b2369ca8c1062fef434eb659105b
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/devfs_fs_kernel.h>
85 #include <linux/vt_kern.h>
86 #include <linux/selection.h>
87 #include <linux/console_struct.h>
88 #include <linux/kbd_kern.h>
89 #include <linux/consolemap.h>
90 #include <linux/timer.h>
91 #include <linux/interrupt.h>
92 #include <linux/config.h>
93 #include <linux/version.h>
94 #include <linux/tqueue.h>
95 #include <linux/bootmem.h>
96 #include <linux/pm.h>
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 extern int tty_register_devfs (struct tty_driver *driver, unsigned int flags,
125 unsigned int minor);
126 extern void vcs_make_devfs (unsigned int index, int unregister);
128 #ifndef MIN
129 #define MIN(a,b) ((a) < (b) ? (a) : (b))
130 #endif
132 static struct tty_struct *console_table[MAX_NR_CONSOLES];
133 static struct termios *console_termios[MAX_NR_CONSOLES];
134 static struct termios *console_termios_locked[MAX_NR_CONSOLES];
135 struct vc vc_cons [MAX_NR_CONSOLES];
137 #ifndef VT_SINGLE_DRIVER
138 static struct consw *con_driver_map[MAX_NR_CONSOLES];
139 #endif
141 static int con_open(struct tty_struct *, struct file *);
142 static void vc_init(unsigned int console, unsigned int rows,
143 unsigned int cols, int do_clear);
144 static void blank_screen(void);
145 static void gotoxy(int currcons, int new_x, int new_y);
146 static void save_cur(int currcons);
147 static void reset_terminal(int currcons, int do_clear);
148 static void con_flush_chars(struct tty_struct *tty);
149 static void set_vesa_blanking(unsigned long arg);
150 static void set_cursor(int currcons);
151 static void hide_cursor(int currcons);
153 static int printable = 0; /* Is console ready for printing? */
155 int do_poke_blanked_console = 0;
156 int console_blanked = 0;
158 static int vesa_blank_mode = 0; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
159 static int blankinterval = 10*60*HZ;
160 static int vesa_off_interval = 0;
163 * fg_console is the current virtual console,
164 * last_console is the last used one,
165 * want_console is the console we want to switch to,
166 * kmsg_redirect is the console for kernel messages,
168 int fg_console = 0;
169 int last_console = 0;
170 int want_console = -1;
171 int kmsg_redirect = 0;
174 * For each existing display, we have a pointer to console currently visible
175 * on that display, allowing consoles other than fg_console to be refreshed
176 * appropriately. Unless the low-level driver supplies its own display_fg
177 * variable, we use this one for the "master display".
179 static struct vc_data *master_display_fg = NULL;
182 * Unfortunately, we need to delay tty echo when we're currently writing to the
183 * console since the code is (and always was) not re-entrant, so we insert
184 * all filp requests to con_task_queue instead of tq_timer and run it from
185 * the console_tasklet. The console_tasklet is protected by the IRQ
186 * protected console_lock.
188 DECLARE_TASK_QUEUE(con_task_queue);
191 * For the same reason, we defer scrollback to the console tasklet.
193 static int scrollback_delta = 0;
196 * Hook so that the power management routines can (un)blank
197 * the console on our behalf.
199 int (*console_blank_hook)(int) = NULL;
202 * Low-Level Functions
205 #define IS_FG (currcons == fg_console)
206 #define IS_VISIBLE CON_IS_VISIBLE(vc_cons[currcons].d)
208 #ifdef VT_BUF_VRAM_ONLY
209 #define DO_UPDATE 0
210 #else
211 #define DO_UPDATE IS_VISIBLE
212 #endif
214 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data);
215 static struct pm_dev *pm_con = NULL;
217 static inline unsigned short *screenpos(int currcons, int offset, int viewed)
219 unsigned short *p;
221 if (!viewed)
222 p = (unsigned short *)(origin + offset);
223 else if (!sw->con_screen_pos)
224 p = (unsigned short *)(visible_origin + offset);
225 else
226 p = sw->con_screen_pos(vc_cons[currcons].d, offset);
227 return p;
230 static inline void scrolldelta(int lines)
232 scrollback_delta += lines;
233 tasklet_schedule(&console_tasklet);
236 static void scrup(int currcons, unsigned int t, unsigned int b, int nr)
238 unsigned short *d, *s;
240 if (t+nr >= b)
241 nr = b - t - 1;
242 if (b > video_num_lines || t >= b || nr < 1)
243 return;
244 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_UP, nr))
245 return;
246 d = (unsigned short *) (origin+video_size_row*t);
247 s = (unsigned short *) (origin+video_size_row*(t+nr));
248 scr_memcpyw(d, s, (b-t-nr) * video_size_row);
249 scr_memsetw(d + (b-t-nr) * video_num_columns, video_erase_char, video_size_row*nr);
252 static void
253 scrdown(int currcons, unsigned int t, unsigned int b, int nr)
255 unsigned short *s;
256 unsigned int step;
258 if (t+nr >= b)
259 nr = b - t - 1;
260 if (b > video_num_lines || t >= b || nr < 1)
261 return;
262 if (IS_VISIBLE && sw->con_scroll(vc_cons[currcons].d, t, b, SM_DOWN, nr))
263 return;
264 s = (unsigned short *) (origin+video_size_row*t);
265 step = video_num_columns * nr;
266 scr_memmovew(s + step, s, (b-t-nr)*video_size_row);
267 scr_memsetw(s, video_erase_char, 2*step);
270 static void do_update_region(int currcons, unsigned long start, int count)
272 #ifndef VT_BUF_VRAM_ONLY
273 unsigned int xx, yy, offset;
274 u16 *p;
276 p = (u16 *) start;
277 if (!sw->con_getxy) {
278 offset = (start - origin) / 2;
279 xx = offset % video_num_columns;
280 yy = offset / video_num_columns;
281 } else {
282 int nxx, nyy;
283 start = sw->con_getxy(vc_cons[currcons].d, start, &nxx, &nyy);
284 xx = nxx; yy = nyy;
286 for(;;) {
287 u16 attrib = scr_readw(p) & 0xff00;
288 int startx = xx;
289 u16 *q = p;
290 while (xx < video_num_columns && count) {
291 if (attrib != (scr_readw(p) & 0xff00)) {
292 if (p > q)
293 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
294 startx = xx;
295 q = p;
296 attrib = scr_readw(p) & 0xff00;
298 p++;
299 xx++;
300 count--;
302 if (p > q)
303 sw->con_putcs(vc_cons[currcons].d, q, p-q, yy, startx);
304 if (!count)
305 break;
306 xx = 0;
307 yy++;
308 if (sw->con_getxy) {
309 p = (u16 *)start;
310 start = sw->con_getxy(vc_cons[currcons].d, start, NULL, NULL);
313 #endif
316 void update_region(int currcons, unsigned long start, int count)
318 if (DO_UPDATE) {
319 hide_cursor(currcons);
320 do_update_region(currcons, start, count);
321 set_cursor(currcons);
325 /* Structure of attributes is hardware-dependent */
327 static u8 build_attr(int currcons, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
329 if (sw->con_build_attr)
330 return sw->con_build_attr(vc_cons[currcons].d, _color, _intensity, _blink, _underline, _reverse);
332 #ifndef VT_BUF_VRAM_ONLY
334 * ++roman: I completely changed the attribute format for monochrome
335 * mode (!can_do_color). The formerly used MDA (monochrome display
336 * adapter) format didn't allow the combination of certain effects.
337 * Now the attribute is just a bit vector:
338 * Bit 0..1: intensity (0..2)
339 * Bit 2 : underline
340 * Bit 3 : reverse
341 * Bit 7 : blink
344 u8 a = color;
345 if (!can_do_color)
346 return _intensity |
347 (_underline ? 4 : 0) |
348 (_reverse ? 8 : 0) |
349 (_blink ? 0x80 : 0);
350 if (_underline)
351 a = (a & 0xf0) | ulcolor;
352 else if (_intensity == 0)
353 a = (a & 0xf0) | halfcolor;
354 if (_reverse)
355 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
356 if (_blink)
357 a ^= 0x80;
358 if (_intensity == 2)
359 a ^= 0x08;
360 if (hi_font_mask == 0x100)
361 a <<= 1;
362 return a;
364 #else
365 return 0;
366 #endif
369 static void update_attr(int currcons)
371 attr = build_attr(currcons, color, intensity, blink, underline, reverse ^ decscnm);
372 video_erase_char = (build_attr(currcons, color, 1, 0, 0, decscnm) << 8) | ' ';
375 /* Note: inverting the screen twice should revert to the original state */
377 void invert_screen(int currcons, int offset, int count, int viewed)
379 unsigned short *p;
381 count /= 2;
382 p = screenpos(currcons, offset, viewed);
383 if (sw->con_invert_region)
384 sw->con_invert_region(vc_cons[currcons].d, p, count);
385 #ifndef VT_BUF_VRAM_ONLY
386 else {
387 u16 *q = p;
388 int cnt = count;
390 if (!can_do_color) {
391 while (cnt--) *q++ ^= 0x0800;
392 } else if (hi_font_mask == 0x100) {
393 while (cnt--) {
394 u16 a = *q;
395 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
396 *q++ = a;
398 } else {
399 while (cnt--) {
400 u16 a = *q;
401 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
402 *q++ = a;
406 #endif
407 if (DO_UPDATE)
408 do_update_region(currcons, (unsigned long) p, count);
411 /* used by selection: complement pointer position */
412 void complement_pos(int currcons, int offset)
414 static unsigned short *p = NULL;
415 static unsigned short old = 0;
416 static unsigned short oldx = 0, oldy = 0;
418 if (p) {
419 scr_writew(old, p);
420 if (DO_UPDATE)
421 sw->con_putc(vc_cons[currcons].d, old, oldy, oldx);
423 if (offset == -1)
424 p = NULL;
425 else {
426 unsigned short new;
427 p = screenpos(currcons, offset, 1);
428 old = scr_readw(p);
429 new = old ^ complement_mask;
430 scr_writew(new, p);
431 if (DO_UPDATE) {
432 oldx = (offset >> 1) % video_num_columns;
433 oldy = (offset >> 1) / video_num_columns;
434 sw->con_putc(vc_cons[currcons].d, new, oldy, oldx);
439 static void insert_char(int currcons, unsigned int nr)
441 unsigned short *p, *q = (unsigned short *) pos;
443 p = q + video_num_columns - nr - x;
444 while (--p >= q)
445 scr_writew(scr_readw(p), p + nr);
446 scr_memsetw(q, video_erase_char, nr*2);
447 need_wrap = 0;
448 if (DO_UPDATE) {
449 unsigned short oldattr = attr;
450 sw->con_bmove(vc_cons[currcons].d,y,x,y,x+nr,1,
451 video_num_columns-x-nr);
452 attr = video_erase_char >> 8;
453 while (nr--)
454 sw->con_putc(vc_cons[currcons].d,
455 video_erase_char,y,x+nr);
456 attr = oldattr;
460 static void delete_char(int currcons, unsigned int nr)
462 unsigned int i = x;
463 unsigned short *p = (unsigned short *) pos;
465 while (++i <= video_num_columns - nr) {
466 scr_writew(scr_readw(p+nr), p);
467 p++;
469 scr_memsetw(p, video_erase_char, nr*2);
470 need_wrap = 0;
471 if (DO_UPDATE) {
472 unsigned short oldattr = attr;
473 sw->con_bmove(vc_cons[currcons].d, y, x+nr, y, x, 1,
474 video_num_columns-x-nr);
475 attr = video_erase_char >> 8;
476 while (nr--)
477 sw->con_putc(vc_cons[currcons].d,
478 video_erase_char, y,
479 video_num_columns-1-nr);
480 attr = oldattr;
484 static int softcursor_original;
486 static void add_softcursor(int currcons)
488 int i = scr_readw((u16 *) pos);
489 u32 type = cursor_type;
491 if (! (type & 0x10)) return;
492 if (softcursor_original != -1) return;
493 softcursor_original = i;
494 i |= ((type >> 8) & 0xff00 );
495 i ^= ((type) & 0xff00 );
496 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
497 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
498 scr_writew(i, (u16 *) pos);
499 if (DO_UPDATE)
500 sw->con_putc(vc_cons[currcons].d, i, y, x);
503 static void hide_cursor(int currcons)
505 if (currcons == sel_cons)
506 clear_selection();
507 if (softcursor_original != -1) {
508 scr_writew(softcursor_original,(u16 *) pos);
509 if (DO_UPDATE)
510 sw->con_putc(vc_cons[currcons].d, softcursor_original, y, x);
511 softcursor_original = -1;
513 sw->con_cursor(vc_cons[currcons].d,CM_ERASE);
516 static void set_cursor(int currcons)
518 if (!IS_FG || console_blanked || vcmode == KD_GRAPHICS)
519 return;
520 if (deccm) {
521 if (currcons == sel_cons)
522 clear_selection();
523 add_softcursor(currcons);
524 if ((cursor_type & 0x0f) != 1)
525 sw->con_cursor(vc_cons[currcons].d,CM_DRAW);
526 } else
527 hide_cursor(currcons);
530 static void set_origin(int currcons)
532 if (!IS_VISIBLE ||
533 !sw->con_set_origin ||
534 !sw->con_set_origin(vc_cons[currcons].d))
535 origin = (unsigned long) screenbuf;
536 visible_origin = origin;
537 scr_end = origin + screenbuf_size;
538 pos = origin + video_size_row*y + 2*x;
541 static inline void save_screen(int currcons)
543 if (sw->con_save_screen)
544 sw->con_save_screen(vc_cons[currcons].d);
548 * Redrawing of screen
551 void redraw_screen(int new_console, int is_switch)
553 int redraw = 1;
554 int currcons, old_console;
556 if (!vc_cons_allocated(new_console)) {
557 /* strange ... */
558 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
559 return;
562 if (is_switch) {
563 currcons = fg_console;
564 hide_cursor(currcons);
565 if (fg_console != new_console) {
566 struct vc_data **display = vc_cons[new_console].d->vc_display_fg;
567 old_console = (*display) ? (*display)->vc_num : fg_console;
568 *display = vc_cons[new_console].d;
569 fg_console = new_console;
570 currcons = old_console;
571 if (!IS_VISIBLE) {
572 save_screen(currcons);
573 set_origin(currcons);
575 currcons = new_console;
576 if (old_console == new_console)
577 redraw = 0;
579 } else {
580 currcons = new_console;
581 hide_cursor(currcons);
584 if (redraw) {
585 set_origin(currcons);
586 set_palette(currcons);
587 if (sw->con_switch(vc_cons[currcons].d) && vcmode != KD_GRAPHICS)
588 /* Update the screen contents */
589 do_update_region(currcons, origin, screenbuf_size/2);
591 set_cursor(currcons);
592 if (is_switch) {
593 set_leds();
594 compute_shiftstate();
599 * Allocation, freeing and resizing of VTs.
602 int vc_cons_allocated(unsigned int i)
604 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
607 static void visual_init(int currcons, int init)
609 /* ++Geert: sw->con_init determines console size */
610 sw = conswitchp;
611 #ifndef VT_SINGLE_DRIVER
612 if (con_driver_map[currcons])
613 sw = con_driver_map[currcons];
614 #endif
615 cons_num = currcons;
616 display_fg = &master_display_fg;
617 vc_cons[currcons].d->vc_uni_pagedir_loc = &vc_cons[currcons].d->vc_uni_pagedir;
618 vc_cons[currcons].d->vc_uni_pagedir = 0;
619 hi_font_mask = 0;
620 complement_mask = 0;
621 can_do_color = 0;
622 sw->con_init(vc_cons[currcons].d, init);
623 if (!complement_mask)
624 complement_mask = can_do_color ? 0x7700 : 0x0800;
625 s_complement_mask = complement_mask;
626 video_size_row = video_num_columns<<1;
627 screenbuf_size = video_num_lines*video_size_row;
630 int vc_allocate(unsigned int currcons) /* return 0 on success */
632 if (currcons >= MAX_NR_CONSOLES)
633 return -ENXIO;
634 if (!vc_cons[currcons].d) {
635 long p, q;
637 /* prevent users from taking too much memory */
638 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
639 return -EPERM;
641 /* due to the granularity of kmalloc, we waste some memory here */
642 /* the alloc is done in two steps, to optimize the common situation
643 of a 25x80 console (structsize=216, screenbuf_size=4000) */
644 /* although the numbers above are not valid since long ago, the
645 point is still up-to-date and the comment still has its value
646 even if only as a historical artifact. --mj, July 1998 */
647 p = (long) kmalloc(structsize, GFP_KERNEL);
648 if (!p)
649 return -ENOMEM;
650 vc_cons[currcons].d = (struct vc_data *)p;
651 vt_cons[currcons] = (struct vt_struct *)(p+sizeof(struct vc_data));
652 visual_init(currcons, 1);
653 if (!*vc_cons[currcons].d->vc_uni_pagedir_loc)
654 con_set_default_unimap(currcons);
655 q = (long)kmalloc(screenbuf_size, GFP_KERNEL);
656 if (!q) {
657 kfree_s((char *) p, structsize);
658 vc_cons[currcons].d = NULL;
659 vt_cons[currcons] = NULL;
660 return -ENOMEM;
662 screenbuf = (unsigned short *) q;
663 kmalloced = 1;
664 vc_init(currcons, video_num_lines, video_num_columns, 1);
666 if (!pm_con) {
667 pm_con = pm_register(PM_SYS_DEV,
668 PM_SYS_VGA,
669 pm_con_request);
672 return 0;
676 * Change # of rows and columns (0 means unchanged/the size of fg_console)
677 * [this is to be used together with some user program
678 * like resize that changes the hardware videomode]
680 int vc_resize(unsigned int lines, unsigned int cols,
681 unsigned int first, unsigned int last)
683 unsigned int cc, ll, ss, sr, todo = 0;
684 unsigned int currcons = fg_console, i;
685 unsigned short *newscreens[MAX_NR_CONSOLES];
687 cc = (cols ? cols : video_num_columns);
688 ll = (lines ? lines : video_num_lines);
689 sr = cc << 1;
690 ss = sr * ll;
692 for (currcons = first; currcons <= last; currcons++) {
693 if (!vc_cons_allocated(currcons) ||
694 (cc == video_num_columns && ll == video_num_lines))
695 newscreens[currcons] = NULL;
696 else {
697 unsigned short *p = (unsigned short *) kmalloc(ss, GFP_USER);
698 if (!p) {
699 for (i = first; i < currcons; i++)
700 if (newscreens[i])
701 kfree_s(newscreens[i], ss);
702 return -ENOMEM;
704 newscreens[currcons] = p;
705 todo++;
708 if (!todo)
709 return 0;
711 for (currcons = first; currcons <= last; currcons++) {
712 unsigned int occ, oll, oss, osr;
713 unsigned long ol, nl, nlend, rlth, rrem;
714 if (!newscreens[currcons] || !vc_cons_allocated(currcons))
715 continue;
717 oll = video_num_lines;
718 occ = video_num_columns;
719 osr = video_size_row;
720 oss = screenbuf_size;
722 video_num_lines = ll;
723 video_num_columns = cc;
724 video_size_row = sr;
725 screenbuf_size = ss;
727 rlth = MIN(osr, sr);
728 rrem = sr - rlth;
729 ol = origin;
730 nl = (long) newscreens[currcons];
731 nlend = nl + ss;
732 if (ll < oll)
733 ol += (oll - ll) * osr;
735 update_attr(currcons);
737 while (ol < scr_end) {
738 scr_memcpyw((unsigned short *) nl, (unsigned short *) ol, rlth);
739 if (rrem)
740 scr_memsetw((void *)(nl + rlth), video_erase_char, rrem);
741 ol += osr;
742 nl += sr;
744 if (nlend > nl)
745 scr_memsetw((void *) nl, video_erase_char, nlend - nl);
746 if (kmalloced)
747 kfree_s(screenbuf, oss);
748 screenbuf = newscreens[currcons];
749 kmalloced = 1;
750 screenbuf_size = ss;
751 set_origin(currcons);
753 /* do part of a reset_terminal() */
754 top = 0;
755 bottom = video_num_lines;
756 gotoxy(currcons, x, y);
757 save_cur(currcons);
759 if (console_table[currcons]) {
760 struct winsize ws, *cws = &console_table[currcons]->winsize;
761 memset(&ws, 0, sizeof(ws));
762 ws.ws_row = video_num_lines;
763 ws.ws_col = video_num_columns;
764 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
765 console_table[currcons]->pgrp > 0)
766 kill_pg(console_table[currcons]->pgrp, SIGWINCH, 1);
767 *cws = ws;
770 if (IS_VISIBLE)
771 update_screen(currcons);
774 return 0;
778 void vc_disallocate(unsigned int currcons)
780 if (vc_cons_allocated(currcons)) {
781 sw->con_deinit(vc_cons[currcons].d);
782 if (kmalloced)
783 kfree_s(screenbuf, screenbuf_size);
784 if (currcons >= MIN_NR_CONSOLES)
785 kfree_s(vc_cons[currcons].d, structsize);
786 vc_cons[currcons].d = NULL;
791 * VT102 emulator
794 #define set_kbd(x) set_vc_kbd_mode(kbd_table+currcons,x)
795 #define clr_kbd(x) clr_vc_kbd_mode(kbd_table+currcons,x)
796 #define is_kbd(x) vc_kbd_mode(kbd_table+currcons,x)
798 #define decarm VC_REPEAT
799 #define decckm VC_CKMODE
800 #define kbdapplic VC_APPLIC
801 #define lnm VC_CRLF
804 * this is what the terminal answers to a ESC-Z or csi0c query.
806 #define VT100ID "\033[?1;2c"
807 #define VT102ID "\033[?6c"
809 unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
810 8,12,10,14, 9,13,11,15 };
812 /* the default colour table, for VGA+ colour systems */
813 int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
814 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
815 int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
816 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
817 int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
818 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
821 * gotoxy() must verify all boundaries, because the arguments
822 * might also be negative. If the given position is out of
823 * bounds, the cursor is placed at the nearest margin.
825 static void gotoxy(int currcons, int new_x, int new_y)
827 int min_y, max_y;
829 if (new_x < 0)
830 x = 0;
831 else
832 if (new_x >= video_num_columns)
833 x = video_num_columns - 1;
834 else
835 x = new_x;
836 if (decom) {
837 min_y = top;
838 max_y = bottom;
839 } else {
840 min_y = 0;
841 max_y = video_num_lines;
843 if (new_y < min_y)
844 y = min_y;
845 else if (new_y >= max_y)
846 y = max_y - 1;
847 else
848 y = new_y;
849 pos = origin + y*video_size_row + (x<<1);
850 need_wrap = 0;
853 /* for absolute user moves, when decom is set */
854 static void gotoxay(int currcons, int new_x, int new_y)
856 gotoxy(currcons, new_x, decom ? (top+new_y) : new_y);
859 void scrollback(int lines)
861 int currcons = fg_console;
863 if (!lines)
864 lines = video_num_lines/2;
865 scrolldelta(-lines);
868 void scrollfront(int lines)
870 int currcons = fg_console;
872 if (!lines)
873 lines = video_num_lines/2;
874 scrolldelta(lines);
877 static void lf(int currcons)
879 /* don't scroll if above bottom of scrolling region, or
880 * if below scrolling region
882 if (y+1 == bottom)
883 scrup(currcons,top,bottom,1);
884 else if (y < video_num_lines-1) {
885 y++;
886 pos += video_size_row;
888 need_wrap = 0;
891 static void ri(int currcons)
893 /* don't scroll if below top of scrolling region, or
894 * if above scrolling region
896 if (y == top)
897 scrdown(currcons,top,bottom,1);
898 else if (y > 0) {
899 y--;
900 pos -= video_size_row;
902 need_wrap = 0;
905 static inline void cr(int currcons)
907 pos -= x<<1;
908 need_wrap = x = 0;
911 static inline void bs(int currcons)
913 if (x) {
914 pos -= 2;
915 x--;
916 need_wrap = 0;
920 static inline void del(int currcons)
922 /* ignored */
925 static void csi_J(int currcons, int vpar)
927 unsigned int count;
928 unsigned short * start;
930 switch (vpar) {
931 case 0: /* erase from cursor to end of display */
932 count = (scr_end-pos)>>1;
933 start = (unsigned short *) pos;
934 if (DO_UPDATE) {
935 /* do in two stages */
936 sw->con_clear(vc_cons[currcons].d, y, x, 1,
937 video_num_columns-x);
938 sw->con_clear(vc_cons[currcons].d, y+1, 0,
939 video_num_lines-y-1,
940 video_num_columns);
942 break;
943 case 1: /* erase from start to cursor */
944 count = ((pos-origin)>>1)+1;
945 start = (unsigned short *) origin;
946 if (DO_UPDATE) {
947 /* do in two stages */
948 sw->con_clear(vc_cons[currcons].d, 0, 0, y,
949 video_num_columns);
950 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
951 x + 1);
953 break;
954 case 2: /* erase whole display */
955 count = video_num_columns * video_num_lines;
956 start = (unsigned short *) origin;
957 if (DO_UPDATE)
958 sw->con_clear(vc_cons[currcons].d, 0, 0,
959 video_num_lines,
960 video_num_columns);
961 break;
962 default:
963 return;
965 scr_memsetw(start, video_erase_char, 2*count);
966 need_wrap = 0;
969 static void csi_K(int currcons, int vpar)
971 unsigned int count;
972 unsigned short * start;
974 switch (vpar) {
975 case 0: /* erase from cursor to end of line */
976 count = video_num_columns-x;
977 start = (unsigned short *) pos;
978 if (DO_UPDATE)
979 sw->con_clear(vc_cons[currcons].d, y, x, 1,
980 video_num_columns-x);
981 break;
982 case 1: /* erase from start of line to cursor */
983 start = (unsigned short *) (pos - (x<<1));
984 count = x+1;
985 if (DO_UPDATE)
986 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
987 x + 1);
988 break;
989 case 2: /* erase whole line */
990 start = (unsigned short *) (pos - (x<<1));
991 count = video_num_columns;
992 if (DO_UPDATE)
993 sw->con_clear(vc_cons[currcons].d, y, 0, 1,
994 video_num_columns);
995 break;
996 default:
997 return;
999 scr_memsetw(start, video_erase_char, 2 * count);
1000 need_wrap = 0;
1003 static void csi_X(int currcons, int vpar) /* erase the following vpar positions */
1004 { /* not vt100? */
1005 int count;
1007 if (!vpar)
1008 vpar++;
1009 count = (vpar > video_num_columns-x) ? (video_num_columns-x) : vpar;
1011 scr_memsetw((unsigned short *) pos, video_erase_char, 2 * count);
1012 if (DO_UPDATE)
1013 sw->con_clear(vc_cons[currcons].d, y, x, 1, count);
1014 need_wrap = 0;
1017 static void default_attr(int currcons)
1019 intensity = 1;
1020 underline = 0;
1021 reverse = 0;
1022 blink = 0;
1023 color = def_color;
1026 static void csi_m(int currcons)
1028 int i;
1030 for (i=0;i<=npar;i++)
1031 switch (par[i]) {
1032 case 0: /* all attributes off */
1033 default_attr(currcons);
1034 break;
1035 case 1:
1036 intensity = 2;
1037 break;
1038 case 2:
1039 intensity = 0;
1040 break;
1041 case 4:
1042 underline = 1;
1043 break;
1044 case 5:
1045 blink = 1;
1046 break;
1047 case 7:
1048 reverse = 1;
1049 break;
1050 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1051 * Select primary font, don't display
1052 * control chars if defined, don't set
1053 * bit 8 on output.
1055 translate = set_translate(charset == 0
1056 ? G0_charset
1057 : G1_charset,currcons);
1058 disp_ctrl = 0;
1059 toggle_meta = 0;
1060 break;
1061 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1062 * Select first alternate font, lets
1063 * chars < 32 be displayed as ROM chars.
1065 translate = set_translate(IBMPC_MAP,currcons);
1066 disp_ctrl = 1;
1067 toggle_meta = 0;
1068 break;
1069 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1070 * Select second alternate font, toggle
1071 * high bit before displaying as ROM char.
1073 translate = set_translate(IBMPC_MAP,currcons);
1074 disp_ctrl = 1;
1075 toggle_meta = 1;
1076 break;
1077 case 21:
1078 case 22:
1079 intensity = 1;
1080 break;
1081 case 24:
1082 underline = 0;
1083 break;
1084 case 25:
1085 blink = 0;
1086 break;
1087 case 27:
1088 reverse = 0;
1089 break;
1090 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1091 * Enables underscore, white foreground
1092 * with white underscore (Linux - use
1093 * default foreground).
1095 color = (def_color & 0x0f) | background;
1096 underline = 1;
1097 break;
1098 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1099 * Disable underline option.
1100 * Reset colour to default? It did this
1101 * before...
1103 color = (def_color & 0x0f) | background;
1104 underline = 0;
1105 break;
1106 case 49:
1107 color = (def_color & 0xf0) | foreground;
1108 break;
1109 default:
1110 if (par[i] >= 30 && par[i] <= 37)
1111 color = color_table[par[i]-30]
1112 | background;
1113 else if (par[i] >= 40 && par[i] <= 47)
1114 color = (color_table[par[i]-40]<<4)
1115 | foreground;
1116 break;
1118 update_attr(currcons);
1121 static void respond_string(const char * p, struct tty_struct * tty)
1123 while (*p) {
1124 tty_insert_flip_char(tty, *p, 0);
1125 p++;
1127 con_schedule_flip(tty);
1130 static void cursor_report(int currcons, struct tty_struct * tty)
1132 char buf[40];
1134 sprintf(buf, "\033[%d;%dR", y + (decom ? top+1 : 1), x+1);
1135 respond_string(buf, tty);
1138 static inline void status_report(struct tty_struct * tty)
1140 respond_string("\033[0n", tty); /* Terminal ok */
1143 static inline void respond_ID(struct tty_struct * tty)
1145 respond_string(VT102ID, tty);
1148 void mouse_report(struct tty_struct * tty, int butt, int mrx, int mry)
1150 char buf[8];
1152 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1153 (char)('!' + mry));
1154 respond_string(buf, tty);
1157 /* invoked via ioctl(TIOCLINUX) and through set_selection */
1158 int mouse_reporting(void)
1160 int currcons = fg_console;
1162 return report_mouse;
1165 static void set_mode(int currcons, int on_off)
1167 int i;
1169 for (i=0; i<=npar; i++)
1170 if (ques) switch(par[i]) { /* DEC private modes set/reset */
1171 case 1: /* Cursor keys send ^[Ox/^[[x */
1172 if (on_off)
1173 set_kbd(decckm);
1174 else
1175 clr_kbd(decckm);
1176 break;
1177 case 3: /* 80/132 mode switch unimplemented */
1178 deccolm = on_off;
1179 #if 0
1180 (void) vc_resize(video_num_lines, deccolm ? 132 : 80);
1181 /* this alone does not suffice; some user mode
1182 utility has to change the hardware regs */
1183 #endif
1184 break;
1185 case 5: /* Inverted screen on/off */
1186 if (decscnm != on_off) {
1187 decscnm = on_off;
1188 invert_screen(currcons, 0, screenbuf_size, 0);
1189 update_attr(currcons);
1191 break;
1192 case 6: /* Origin relative/absolute */
1193 decom = on_off;
1194 gotoxay(currcons,0,0);
1195 break;
1196 case 7: /* Autowrap on/off */
1197 decawm = on_off;
1198 break;
1199 case 8: /* Autorepeat on/off */
1200 if (on_off)
1201 set_kbd(decarm);
1202 else
1203 clr_kbd(decarm);
1204 break;
1205 case 9:
1206 report_mouse = on_off ? 1 : 0;
1207 break;
1208 case 25: /* Cursor on/off */
1209 deccm = on_off;
1210 break;
1211 case 1000:
1212 report_mouse = on_off ? 2 : 0;
1213 break;
1214 } else switch(par[i]) { /* ANSI modes set/reset */
1215 case 3: /* Monitor (display ctrls) */
1216 disp_ctrl = on_off;
1217 break;
1218 case 4: /* Insert Mode on/off */
1219 decim = on_off;
1220 break;
1221 case 20: /* Lf, Enter == CrLf/Lf */
1222 if (on_off)
1223 set_kbd(lnm);
1224 else
1225 clr_kbd(lnm);
1226 break;
1230 static void setterm_command(int currcons)
1232 switch(par[0]) {
1233 case 1: /* set color for underline mode */
1234 if (can_do_color && par[1] < 16) {
1235 ulcolor = color_table[par[1]];
1236 if (underline)
1237 update_attr(currcons);
1239 break;
1240 case 2: /* set color for half intensity mode */
1241 if (can_do_color && par[1] < 16) {
1242 halfcolor = color_table[par[1]];
1243 if (intensity == 0)
1244 update_attr(currcons);
1246 break;
1247 case 8: /* store colors as defaults */
1248 def_color = attr;
1249 if (hi_font_mask == 0x100)
1250 def_color >>= 1;
1251 default_attr(currcons);
1252 update_attr(currcons);
1253 break;
1254 case 9: /* set blanking interval */
1255 blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1256 poke_blanked_console();
1257 break;
1258 case 10: /* set bell frequency in Hz */
1259 if (npar >= 1)
1260 bell_pitch = par[1];
1261 else
1262 bell_pitch = DEFAULT_BELL_PITCH;
1263 break;
1264 case 11: /* set bell duration in msec */
1265 if (npar >= 1)
1266 bell_duration = (par[1] < 2000) ?
1267 par[1]*HZ/1000 : 0;
1268 else
1269 bell_duration = DEFAULT_BELL_DURATION;
1270 break;
1271 case 12: /* bring specified console to the front */
1272 if (par[1] >= 1 && vc_cons_allocated(par[1]-1))
1273 set_console(par[1] - 1);
1274 break;
1275 case 13: /* unblank the screen */
1276 poke_blanked_console();
1277 break;
1278 case 14: /* set vesa powerdown interval */
1279 vesa_off_interval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
1280 break;
1284 static void insert_line(int currcons, unsigned int nr)
1286 scrdown(currcons,y,bottom,nr);
1287 need_wrap = 0;
1291 static void delete_line(int currcons, unsigned int nr)
1293 scrup(currcons,y,bottom,nr);
1294 need_wrap = 0;
1297 static void csi_at(int currcons, unsigned int nr)
1299 if (nr > video_num_columns - x)
1300 nr = video_num_columns - x;
1301 else if (!nr)
1302 nr = 1;
1303 insert_char(currcons, nr);
1306 static void csi_L(int currcons, unsigned int nr)
1308 if (nr > video_num_lines - y)
1309 nr = video_num_lines - y;
1310 else if (!nr)
1311 nr = 1;
1312 insert_line(currcons, nr);
1315 static void csi_P(int currcons, unsigned int nr)
1317 if (nr > video_num_columns - x)
1318 nr = video_num_columns - x;
1319 else if (!nr)
1320 nr = 1;
1321 delete_char(currcons, nr);
1324 static void csi_M(int currcons, unsigned int nr)
1326 if (nr > video_num_lines - y)
1327 nr = video_num_lines - y;
1328 else if (!nr)
1329 nr=1;
1330 delete_line(currcons, nr);
1333 static void save_cur(int currcons)
1335 saved_x = x;
1336 saved_y = y;
1337 s_intensity = intensity;
1338 s_underline = underline;
1339 s_blink = blink;
1340 s_reverse = reverse;
1341 s_charset = charset;
1342 s_color = color;
1343 saved_G0 = G0_charset;
1344 saved_G1 = G1_charset;
1347 static void restore_cur(int currcons)
1349 gotoxy(currcons,saved_x,saved_y);
1350 intensity = s_intensity;
1351 underline = s_underline;
1352 blink = s_blink;
1353 reverse = s_reverse;
1354 charset = s_charset;
1355 color = s_color;
1356 G0_charset = saved_G0;
1357 G1_charset = saved_G1;
1358 translate = set_translate(charset ? G1_charset : G0_charset,currcons);
1359 update_attr(currcons);
1360 need_wrap = 0;
1363 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1364 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1365 ESpalette };
1367 static void reset_terminal(int currcons, int do_clear)
1369 top = 0;
1370 bottom = video_num_lines;
1371 vc_state = ESnormal;
1372 ques = 0;
1373 translate = set_translate(LAT1_MAP,currcons);
1374 G0_charset = LAT1_MAP;
1375 G1_charset = GRAF_MAP;
1376 charset = 0;
1377 need_wrap = 0;
1378 report_mouse = 0;
1379 utf = 0;
1380 utf_count = 0;
1382 disp_ctrl = 0;
1383 toggle_meta = 0;
1385 decscnm = 0;
1386 decom = 0;
1387 decawm = 1;
1388 deccm = 1;
1389 decim = 0;
1391 set_kbd(decarm);
1392 clr_kbd(decckm);
1393 clr_kbd(kbdapplic);
1394 clr_kbd(lnm);
1395 kbd_table[currcons].lockstate = 0;
1396 kbd_table[currcons].slockstate = 0;
1397 kbd_table[currcons].ledmode = LED_SHOW_FLAGS;
1398 kbd_table[currcons].ledflagstate = kbd_table[currcons].default_ledflagstate;
1399 set_leds();
1401 cursor_type = CUR_DEFAULT;
1402 complement_mask = s_complement_mask;
1404 default_attr(currcons);
1405 update_attr(currcons);
1407 tab_stop[0] = 0x01010100;
1408 tab_stop[1] =
1409 tab_stop[2] =
1410 tab_stop[3] =
1411 tab_stop[4] = 0x01010101;
1413 bell_pitch = DEFAULT_BELL_PITCH;
1414 bell_duration = DEFAULT_BELL_DURATION;
1416 gotoxy(currcons,0,0);
1417 save_cur(currcons);
1418 if (do_clear)
1419 csi_J(currcons,2);
1422 static void do_con_trol(struct tty_struct *tty, unsigned int currcons, int c)
1425 * Control characters can be used in the _middle_
1426 * of an escape sequence.
1428 switch (c) {
1429 case 0:
1430 return;
1431 case 7:
1432 if (bell_duration)
1433 kd_mksound(bell_pitch, bell_duration);
1434 return;
1435 case 8:
1436 bs(currcons);
1437 return;
1438 case 9:
1439 pos -= (x << 1);
1440 while (x < video_num_columns - 1) {
1441 x++;
1442 if (tab_stop[x >> 5] & (1 << (x & 31)))
1443 break;
1445 pos += (x << 1);
1446 return;
1447 case 10: case 11: case 12:
1448 lf(currcons);
1449 if (!is_kbd(lnm))
1450 return;
1451 case 13:
1452 cr(currcons);
1453 return;
1454 case 14:
1455 charset = 1;
1456 translate = set_translate(G1_charset,currcons);
1457 disp_ctrl = 1;
1458 return;
1459 case 15:
1460 charset = 0;
1461 translate = set_translate(G0_charset,currcons);
1462 disp_ctrl = 0;
1463 return;
1464 case 24: case 26:
1465 vc_state = ESnormal;
1466 return;
1467 case 27:
1468 vc_state = ESesc;
1469 return;
1470 case 127:
1471 del(currcons);
1472 return;
1473 case 128+27:
1474 vc_state = ESsquare;
1475 return;
1477 switch(vc_state) {
1478 case ESesc:
1479 vc_state = ESnormal;
1480 switch (c) {
1481 case '[':
1482 vc_state = ESsquare;
1483 return;
1484 case ']':
1485 vc_state = ESnonstd;
1486 return;
1487 case '%':
1488 vc_state = ESpercent;
1489 return;
1490 case 'E':
1491 cr(currcons);
1492 lf(currcons);
1493 return;
1494 case 'M':
1495 ri(currcons);
1496 return;
1497 case 'D':
1498 lf(currcons);
1499 return;
1500 case 'H':
1501 tab_stop[x >> 5] |= (1 << (x & 31));
1502 return;
1503 case 'Z':
1504 respond_ID(tty);
1505 return;
1506 case '7':
1507 save_cur(currcons);
1508 return;
1509 case '8':
1510 restore_cur(currcons);
1511 return;
1512 case '(':
1513 vc_state = ESsetG0;
1514 return;
1515 case ')':
1516 vc_state = ESsetG1;
1517 return;
1518 case '#':
1519 vc_state = EShash;
1520 return;
1521 case 'c':
1522 reset_terminal(currcons,1);
1523 return;
1524 case '>': /* Numeric keypad */
1525 clr_kbd(kbdapplic);
1526 return;
1527 case '=': /* Appl. keypad */
1528 set_kbd(kbdapplic);
1529 return;
1531 return;
1532 case ESnonstd:
1533 if (c=='P') { /* palette escape sequence */
1534 for (npar=0; npar<NPAR; npar++)
1535 par[npar] = 0 ;
1536 npar = 0 ;
1537 vc_state = ESpalette;
1538 return;
1539 } else if (c=='R') { /* reset palette */
1540 reset_palette(currcons);
1541 vc_state = ESnormal;
1542 } else
1543 vc_state = ESnormal;
1544 return;
1545 case ESpalette:
1546 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1547 par[npar++] = (c>'9' ? (c&0xDF)-'A'+10 : c-'0') ;
1548 if (npar==7) {
1549 int i = par[0]*3, j = 1;
1550 palette[i] = 16*par[j++];
1551 palette[i++] += par[j++];
1552 palette[i] = 16*par[j++];
1553 palette[i++] += par[j++];
1554 palette[i] = 16*par[j++];
1555 palette[i] += par[j];
1556 set_palette(currcons);
1557 vc_state = ESnormal;
1559 } else
1560 vc_state = ESnormal;
1561 return;
1562 case ESsquare:
1563 for(npar = 0 ; npar < NPAR ; npar++)
1564 par[npar] = 0;
1565 npar = 0;
1566 vc_state = ESgetpars;
1567 if (c == '[') { /* Function key */
1568 vc_state=ESfunckey;
1569 return;
1571 ques = (c=='?');
1572 if (ques)
1573 return;
1574 case ESgetpars:
1575 if (c==';' && npar<NPAR-1) {
1576 npar++;
1577 return;
1578 } else if (c>='0' && c<='9') {
1579 par[npar] *= 10;
1580 par[npar] += c-'0';
1581 return;
1582 } else vc_state=ESgotpars;
1583 case ESgotpars:
1584 vc_state = ESnormal;
1585 switch(c) {
1586 case 'h':
1587 set_mode(currcons,1);
1588 return;
1589 case 'l':
1590 set_mode(currcons,0);
1591 return;
1592 case 'c':
1593 if (ques) {
1594 if (par[0])
1595 cursor_type = par[0] | (par[1]<<8) | (par[2]<<16);
1596 else
1597 cursor_type = CUR_DEFAULT;
1598 return;
1600 break;
1601 case 'm':
1602 if (ques) {
1603 clear_selection();
1604 if (par[0])
1605 complement_mask = par[0]<<8 | par[1];
1606 else
1607 complement_mask = s_complement_mask;
1608 return;
1610 break;
1611 case 'n':
1612 if (!ques) {
1613 if (par[0] == 5)
1614 status_report(tty);
1615 else if (par[0] == 6)
1616 cursor_report(currcons,tty);
1618 return;
1620 if (ques) {
1621 ques = 0;
1622 return;
1624 switch(c) {
1625 case 'G': case '`':
1626 if (par[0]) par[0]--;
1627 gotoxy(currcons,par[0],y);
1628 return;
1629 case 'A':
1630 if (!par[0]) par[0]++;
1631 gotoxy(currcons,x,y-par[0]);
1632 return;
1633 case 'B': case 'e':
1634 if (!par[0]) par[0]++;
1635 gotoxy(currcons,x,y+par[0]);
1636 return;
1637 case 'C': case 'a':
1638 if (!par[0]) par[0]++;
1639 gotoxy(currcons,x+par[0],y);
1640 return;
1641 case 'D':
1642 if (!par[0]) par[0]++;
1643 gotoxy(currcons,x-par[0],y);
1644 return;
1645 case 'E':
1646 if (!par[0]) par[0]++;
1647 gotoxy(currcons,0,y+par[0]);
1648 return;
1649 case 'F':
1650 if (!par[0]) par[0]++;
1651 gotoxy(currcons,0,y-par[0]);
1652 return;
1653 case 'd':
1654 if (par[0]) par[0]--;
1655 gotoxay(currcons,x,par[0]);
1656 return;
1657 case 'H': case 'f':
1658 if (par[0]) par[0]--;
1659 if (par[1]) par[1]--;
1660 gotoxay(currcons,par[1],par[0]);
1661 return;
1662 case 'J':
1663 csi_J(currcons,par[0]);
1664 return;
1665 case 'K':
1666 csi_K(currcons,par[0]);
1667 return;
1668 case 'L':
1669 csi_L(currcons,par[0]);
1670 return;
1671 case 'M':
1672 csi_M(currcons,par[0]);
1673 return;
1674 case 'P':
1675 csi_P(currcons,par[0]);
1676 return;
1677 case 'c':
1678 if (!par[0])
1679 respond_ID(tty);
1680 return;
1681 case 'g':
1682 if (!par[0])
1683 tab_stop[x >> 5] &= ~(1 << (x & 31));
1684 else if (par[0] == 3) {
1685 tab_stop[0] =
1686 tab_stop[1] =
1687 tab_stop[2] =
1688 tab_stop[3] =
1689 tab_stop[4] = 0;
1691 return;
1692 case 'm':
1693 csi_m(currcons);
1694 return;
1695 case 'q': /* DECLL - but only 3 leds */
1696 /* map 0,1,2,3 to 0,1,2,4 */
1697 if (par[0] < 4)
1698 setledstate(kbd_table + currcons,
1699 (par[0] < 3) ? par[0] : 4);
1700 return;
1701 case 'r':
1702 if (!par[0])
1703 par[0]++;
1704 if (!par[1])
1705 par[1] = video_num_lines;
1706 /* Minimum allowed region is 2 lines */
1707 if (par[0] < par[1] &&
1708 par[1] <= video_num_lines) {
1709 top=par[0]-1;
1710 bottom=par[1];
1711 gotoxay(currcons,0,0);
1713 return;
1714 case 's':
1715 save_cur(currcons);
1716 return;
1717 case 'u':
1718 restore_cur(currcons);
1719 return;
1720 case 'X':
1721 csi_X(currcons, par[0]);
1722 return;
1723 case '@':
1724 csi_at(currcons,par[0]);
1725 return;
1726 case ']': /* setterm functions */
1727 setterm_command(currcons);
1728 return;
1730 return;
1731 case ESpercent:
1732 vc_state = ESnormal;
1733 switch (c) {
1734 case '@': /* defined in ISO 2022 */
1735 utf = 0;
1736 return;
1737 case 'G': /* prelim official escape code */
1738 case '8': /* retained for compatibility */
1739 utf = 1;
1740 return;
1742 return;
1743 case ESfunckey:
1744 vc_state = ESnormal;
1745 return;
1746 case EShash:
1747 vc_state = ESnormal;
1748 if (c == '8') {
1749 /* DEC screen alignment test. kludge :-) */
1750 video_erase_char =
1751 (video_erase_char & 0xff00) | 'E';
1752 csi_J(currcons, 2);
1753 video_erase_char =
1754 (video_erase_char & 0xff00) | ' ';
1755 do_update_region(currcons, origin, screenbuf_size/2);
1757 return;
1758 case ESsetG0:
1759 if (c == '0')
1760 G0_charset = GRAF_MAP;
1761 else if (c == 'B')
1762 G0_charset = LAT1_MAP;
1763 else if (c == 'U')
1764 G0_charset = IBMPC_MAP;
1765 else if (c == 'K')
1766 G0_charset = USER_MAP;
1767 if (charset == 0)
1768 translate = set_translate(G0_charset,currcons);
1769 vc_state = ESnormal;
1770 return;
1771 case ESsetG1:
1772 if (c == '0')
1773 G1_charset = GRAF_MAP;
1774 else if (c == 'B')
1775 G1_charset = LAT1_MAP;
1776 else if (c == 'U')
1777 G1_charset = IBMPC_MAP;
1778 else if (c == 'K')
1779 G1_charset = USER_MAP;
1780 if (charset == 1)
1781 translate = set_translate(G1_charset,currcons);
1782 vc_state = ESnormal;
1783 return;
1784 default:
1785 vc_state = ESnormal;
1789 /* This is a temporary buffer used to prepare a tty console write
1790 * so that we can easily avoid touching user space while holding the
1791 * console spinlock. It is allocated in con_init and is shared by
1792 * this code and the vc_screen read/write tty calls.
1794 * We have to allocate this statically in the kernel data section
1795 * since console_init (and thus con_init) are called before any
1796 * kernel memory allocation is available.
1798 char con_buf[PAGE_SIZE];
1799 #define CON_BUF_SIZE PAGE_SIZE
1800 DECLARE_MUTEX(con_buf_sem);
1802 static int do_con_write(struct tty_struct * tty, int from_user,
1803 const unsigned char *buf, int count)
1805 #ifdef VT_BUF_VRAM_ONLY
1806 #define FLUSH do { } while(0);
1807 #else
1808 #define FLUSH if (draw_x >= 0) { \
1809 sw->con_putcs(vc_cons[currcons].d, (u16 *)draw_from, (u16 *)draw_to-(u16 *)draw_from, y, draw_x); \
1810 draw_x = -1; \
1812 #endif
1814 int c, tc, ok, n = 0, draw_x = -1;
1815 unsigned int currcons;
1816 unsigned long draw_from = 0, draw_to = 0;
1817 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
1818 u16 himask, charmask;
1819 const unsigned char *orig_buf = NULL;
1820 int orig_count;
1822 currcons = vt->vc_num;
1823 if (!vc_cons_allocated(currcons)) {
1824 /* could this happen? */
1825 static int error = 0;
1826 if (!error) {
1827 error = 1;
1828 printk("con_write: tty %d not allocated\n", currcons+1);
1830 return 0;
1833 orig_buf = buf;
1834 orig_count = count;
1836 if (from_user) {
1837 down(&con_buf_sem);
1839 again:
1840 if (count > CON_BUF_SIZE)
1841 count = CON_BUF_SIZE;
1842 if (copy_from_user(con_buf, buf, count)) {
1843 n = 0; /* ?? are error codes legal here ?? */
1844 goto out;
1847 buf = con_buf;
1850 /* At this point 'buf' is guarenteed to be a kernel buffer
1851 * and therefore no access to userspace (and therefore sleeping)
1852 * will be needed. The con_buf_sem serializes all tty based
1853 * console rendering and vcs write/read operations. We hold
1854 * the console spinlock during the entire write.
1857 spin_lock_irq(&console_lock);
1859 himask = hi_font_mask;
1860 charmask = himask ? 0x1ff : 0xff;
1862 /* undraw cursor first */
1863 if (IS_FG)
1864 hide_cursor(currcons);
1866 while (!tty->stopped && count) {
1867 c = *buf;
1868 buf++;
1869 n++;
1870 count--;
1872 if (utf) {
1873 /* Combine UTF-8 into Unicode */
1874 /* Incomplete characters silently ignored */
1875 if(c > 0x7f) {
1876 if (utf_count > 0 && (c & 0xc0) == 0x80) {
1877 utf_char = (utf_char << 6) | (c & 0x3f);
1878 utf_count--;
1879 if (utf_count == 0)
1880 tc = c = utf_char;
1881 else continue;
1882 } else {
1883 if ((c & 0xe0) == 0xc0) {
1884 utf_count = 1;
1885 utf_char = (c & 0x1f);
1886 } else if ((c & 0xf0) == 0xe0) {
1887 utf_count = 2;
1888 utf_char = (c & 0x0f);
1889 } else if ((c & 0xf8) == 0xf0) {
1890 utf_count = 3;
1891 utf_char = (c & 0x07);
1892 } else if ((c & 0xfc) == 0xf8) {
1893 utf_count = 4;
1894 utf_char = (c & 0x03);
1895 } else if ((c & 0xfe) == 0xfc) {
1896 utf_count = 5;
1897 utf_char = (c & 0x01);
1898 } else
1899 utf_count = 0;
1900 continue;
1902 } else {
1903 tc = c;
1904 utf_count = 0;
1906 } else { /* no utf */
1907 tc = translate[toggle_meta ? (c|0x80) : c];
1910 /* If the original code was a control character we
1911 * only allow a glyph to be displayed if the code is
1912 * not normally used (such as for cursor movement) or
1913 * if the disp_ctrl mode has been explicitly enabled.
1914 * Certain characters (as given by the CTRL_ALWAYS
1915 * bitmap) are always displayed as control characters,
1916 * as the console would be pretty useless without
1917 * them; to display an arbitrary font position use the
1918 * direct-to-font zone in UTF-8 mode.
1920 ok = tc && (c >= 32 ||
1921 (!utf && !(((disp_ctrl ? CTRL_ALWAYS
1922 : CTRL_ACTION) >> c) & 1)))
1923 && (c != 127 || disp_ctrl)
1924 && (c != 128+27);
1926 if (vc_state == ESnormal && ok) {
1927 /* Now try to find out how to display it */
1928 tc = conv_uni_to_pc(vc_cons[currcons].d, tc);
1929 if ( tc == -4 ) {
1930 /* If we got -4 (not found) then see if we have
1931 defined a replacement character (U+FFFD) */
1932 tc = conv_uni_to_pc(vc_cons[currcons].d, 0xfffd);
1934 /* One reason for the -4 can be that we just
1935 did a clear_unimap();
1936 try at least to show something. */
1937 if (tc == -4)
1938 tc = c;
1939 } else if ( tc == -3 ) {
1940 /* Bad hash table -- hope for the best */
1941 tc = c;
1943 if (tc & ~charmask)
1944 continue; /* Conversion failed */
1946 if (need_wrap || decim)
1947 FLUSH
1948 if (need_wrap) {
1949 cr(currcons);
1950 lf(currcons);
1952 if (decim)
1953 insert_char(currcons, 1);
1954 scr_writew(himask ?
1955 ((attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
1956 (attr << 8) + tc,
1957 (u16 *) pos);
1958 if (DO_UPDATE && draw_x < 0) {
1959 draw_x = x;
1960 draw_from = pos;
1962 if (x == video_num_columns - 1) {
1963 need_wrap = decawm;
1964 draw_to = pos+2;
1965 } else {
1966 x++;
1967 draw_to = (pos+=2);
1969 continue;
1971 FLUSH
1972 do_con_trol(tty, currcons, c);
1974 FLUSH
1975 spin_unlock_irq(&console_lock);
1977 out:
1978 if (from_user) {
1979 /* If the user requested something larger than
1980 * the CON_BUF_SIZE, and the tty is not stopped,
1981 * keep going.
1983 if ((orig_count > CON_BUF_SIZE) && !tty->stopped) {
1984 orig_count -= CON_BUF_SIZE;
1985 orig_buf += CON_BUF_SIZE;
1986 count = orig_count;
1987 buf = orig_buf;
1988 goto again;
1991 up(&con_buf_sem);
1994 return n;
1995 #undef FLUSH
1999 * This is the console switching tasklet.
2001 * Doing console switching in a tasklet allows
2002 * us to do the switches asynchronously (needed when we want
2003 * to switch due to a keyboard interrupt). Synchronization
2004 * with other console code and prevention of re-entrancy is
2005 * ensured with console_lock.
2007 static void console_softint(unsigned long ignored)
2009 /* Runs the task queue outside of the console lock. These
2010 * callbacks can come back into the console code and thus
2011 * will perform their own locking.
2013 run_task_queue(&con_task_queue);
2015 spin_lock_irq(&console_lock);
2017 if (want_console >= 0) {
2018 if (want_console != fg_console && vc_cons_allocated(want_console)) {
2019 hide_cursor(fg_console);
2020 change_console(want_console);
2021 /* we only changed when the console had already
2022 been allocated - a new console is not created
2023 in an interrupt routine */
2025 want_console = -1;
2027 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2028 do_poke_blanked_console = 0;
2029 poke_blanked_console();
2031 if (scrollback_delta) {
2032 int currcons = fg_console;
2033 clear_selection();
2034 if (vcmode == KD_TEXT)
2035 sw->con_scrolldelta(vc_cons[currcons].d, scrollback_delta);
2036 scrollback_delta = 0;
2039 spin_unlock_irq(&console_lock);
2042 #ifdef CONFIG_VT_CONSOLE
2045 * Console on virtual terminal
2047 * The console_lock must be held when we get here.
2050 void vt_console_print(struct console *co, const char * b, unsigned count)
2052 int currcons = fg_console;
2053 unsigned char c;
2054 static unsigned long printing = 0;
2055 const ushort *start;
2056 ushort cnt = 0;
2057 ushort myx;
2059 /* console busy or not yet initialized */
2060 if (!printable || test_and_set_bit(0, &printing))
2061 return;
2063 pm_access(pm_con);
2065 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2066 currcons = kmsg_redirect - 1;
2068 /* read `x' only after setting currecons properly (otherwise
2069 the `x' macro will read the x of the foreground console). */
2070 myx = x;
2072 if (!vc_cons_allocated(currcons)) {
2073 /* impossible */
2074 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2075 goto quit;
2078 if (vcmode != KD_TEXT)
2079 goto quit;
2081 /* undraw cursor first */
2082 if (IS_FG)
2083 hide_cursor(currcons);
2085 start = (ushort *)pos;
2087 /* Contrived structure to try to emulate original need_wrap behaviour
2088 * Problems caused when we have need_wrap set on '\n' character */
2089 while (count--) {
2090 c = *b++;
2091 if (c == 10 || c == 13 || c == 8 || need_wrap) {
2092 if (cnt > 0) {
2093 if (IS_VISIBLE)
2094 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2095 x += cnt;
2096 if (need_wrap)
2097 x--;
2098 cnt = 0;
2100 if (c == 8) { /* backspace */
2101 bs(currcons);
2102 start = (ushort *)pos;
2103 myx = x;
2104 continue;
2106 if (c != 13)
2107 lf(currcons);
2108 cr(currcons);
2109 start = (ushort *)pos;
2110 myx = x;
2111 if (c == 10 || c == 13)
2112 continue;
2114 scr_writew((attr << 8) + c, (unsigned short *) pos);
2115 cnt++;
2116 if (myx == video_num_columns - 1) {
2117 need_wrap = 1;
2118 continue;
2120 pos+=2;
2121 myx++;
2123 if (cnt > 0) {
2124 if (IS_VISIBLE)
2125 sw->con_putcs(vc_cons[currcons].d, start, cnt, y, x);
2126 x += cnt;
2127 if (x == video_num_columns) {
2128 x--;
2129 need_wrap = 1;
2132 set_cursor(currcons);
2133 poke_blanked_console();
2135 quit:
2136 clear_bit(0, &printing);
2139 static kdev_t vt_console_device(struct console *c)
2141 return MKDEV(TTY_MAJOR, c->index ? c->index : fg_console + 1);
2144 struct console vt_console_driver = {
2145 "tty",
2146 vt_console_print,
2147 NULL,
2148 vt_console_device,
2149 keyboard_wait_for_keypress,
2150 unblank_screen,
2151 NULL,
2152 CON_PRINTBUFFER,
2155 NULL
2157 #endif
2160 * Handling of Linux-specific VC ioctls
2163 int tioclinux(struct tty_struct *tty, unsigned long arg)
2165 char type, data;
2167 if (tty->driver.type != TTY_DRIVER_TYPE_CONSOLE)
2168 return -EINVAL;
2169 if (current->tty != tty && !suser())
2170 return -EPERM;
2171 if (get_user(type, (char *)arg))
2172 return -EFAULT;
2173 switch (type)
2175 case 2:
2176 return set_selection(arg, tty, 1);
2177 case 3:
2178 return paste_selection(tty);
2179 case 4:
2180 unblank_screen();
2181 return 0;
2182 case 5:
2183 return sel_loadlut(arg);
2184 case 6:
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.
2192 data = shift_state;
2193 return __put_user(data, (char *) arg);
2194 case 7:
2195 data = mouse_reporting();
2196 return __put_user(data, (char *) arg);
2197 case 10:
2198 set_vesa_blanking(arg);
2199 return 0;
2200 case 11: /* set kmsg redirect */
2201 if (!suser())
2202 return -EPERM;
2203 if (get_user(data, (char *)arg+1))
2204 return -EFAULT;
2205 kmsg_redirect = data;
2206 return 0;
2207 case 12: /* get fg_console */
2208 return fg_console;
2210 return -EINVAL;
2214 * /dev/ttyN handling
2217 static int con_write(struct tty_struct * tty, int from_user,
2218 const unsigned char *buf, int count)
2220 int retval;
2222 pm_access(pm_con);
2223 retval = do_con_write(tty, from_user, buf, count);
2224 con_flush_chars(tty);
2226 return retval;
2229 static void con_put_char(struct tty_struct *tty, unsigned char ch)
2231 pm_access(pm_con);
2232 do_con_write(tty, 0, &ch, 1);
2235 static int con_write_room(struct tty_struct *tty)
2237 if (tty->stopped)
2238 return 0;
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
2250 * characters...
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)
2268 int console_num;
2269 if (!tty)
2270 return;
2271 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2272 if (!vc_cons_allocated(console_num))
2273 return;
2274 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2275 set_leds();
2279 * Turn the Scroll-Lock LED off when the console is started
2281 static void con_start(struct tty_struct *tty)
2283 int console_num;
2284 if (!tty)
2285 return;
2286 console_num = MINOR(tty->device) - (tty->driver.minor_start);
2287 if (!vc_cons_allocated(console_num))
2288 return;
2289 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2290 set_leds();
2293 static void con_flush_chars(struct tty_struct *tty)
2295 struct vt_struct *vt = (struct vt_struct *)tty->driver_data;
2297 pm_access(pm_con);
2298 set_cursor(vt->vc_num);
2302 * Allocate the console screen memory.
2304 static int con_open(struct tty_struct *tty, struct file * filp)
2306 unsigned int currcons;
2307 int i;
2309 currcons = MINOR(tty->device) - tty->driver.minor_start;
2311 i = vc_allocate(currcons);
2312 if (i)
2313 return i;
2315 vt_cons[currcons]->vc_num = currcons;
2316 tty->driver_data = vt_cons[currcons];
2318 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2319 tty->winsize.ws_row = video_num_lines;
2320 tty->winsize.ws_col = video_num_columns;
2322 if (tty->count == 1)
2323 vcs_make_devfs (currcons, 0);
2324 return 0;
2327 static void con_close(struct tty_struct *tty, struct file * filp)
2329 if (!tty)
2330 return;
2331 if (tty->count != 1) return;
2332 vcs_make_devfs (MINOR (tty->device) - tty->driver.minor_start, 1);
2333 tty->driver_data = 0;
2336 static void vc_init(unsigned int currcons, unsigned int rows, unsigned int cols, int do_clear)
2338 int j, k ;
2340 video_num_columns = cols;
2341 video_num_lines = rows;
2342 video_size_row = cols<<1;
2343 screenbuf_size = video_num_lines * video_size_row;
2345 set_origin(currcons);
2346 pos = origin;
2347 reset_vc(currcons);
2348 for (j=k=0; j<16; j++) {
2349 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2350 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2351 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2353 def_color = 0x07; /* white */
2354 ulcolor = 0x0f; /* bold white */
2355 halfcolor = 0x08; /* grey */
2356 init_waitqueue_head(&vt_cons[currcons]->paste_wait);
2357 reset_terminal(currcons, do_clear);
2361 * This routine initializes console interrupts, and does nothing
2362 * else. If you want the screen to clear, call tty_write with
2363 * the appropriate escape-sequence.
2366 struct tty_driver console_driver;
2367 static int console_refcount;
2369 DECLARE_TASKLET_DISABLED(console_tasklet, console_softint, 0);
2371 void __init con_init(void)
2373 const char *display_desc = NULL;
2374 unsigned int currcons = 0;
2376 if (conswitchp)
2377 display_desc = conswitchp->con_startup();
2378 if (!display_desc) {
2379 fg_console = 0;
2380 return;
2383 memset(&console_driver, 0, sizeof(struct tty_driver));
2384 console_driver.magic = TTY_DRIVER_MAGIC;
2385 console_driver.name = "vc/%d";
2386 console_driver.name_base = 1;
2387 console_driver.major = TTY_MAJOR;
2388 console_driver.minor_start = 1;
2389 console_driver.num = MAX_NR_CONSOLES;
2390 console_driver.type = TTY_DRIVER_TYPE_CONSOLE;
2391 console_driver.init_termios = tty_std_termios;
2392 console_driver.flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2393 /* Tell tty_register_driver() to skip consoles because they are
2394 * registered before kmalloc() is ready. We'll patch them in later.
2395 * See comments at console_init(); see also con_init_devfs().
2397 console_driver.flags |= TTY_DRIVER_NO_DEVFS;
2398 console_driver.refcount = &console_refcount;
2399 console_driver.table = console_table;
2400 console_driver.termios = console_termios;
2401 console_driver.termios_locked = console_termios_locked;
2403 console_driver.open = con_open;
2404 console_driver.close = con_close;
2405 console_driver.write = con_write;
2406 console_driver.write_room = con_write_room;
2407 console_driver.put_char = con_put_char;
2408 console_driver.flush_chars = con_flush_chars;
2409 console_driver.chars_in_buffer = con_chars_in_buffer;
2410 console_driver.ioctl = vt_ioctl;
2411 console_driver.stop = con_stop;
2412 console_driver.start = con_start;
2413 console_driver.throttle = con_throttle;
2414 console_driver.unthrottle = con_unthrottle;
2416 if (tty_register_driver(&console_driver))
2417 panic("Couldn't register console driver\n");
2419 timer_table[BLANK_TIMER].fn = blank_screen;
2420 timer_table[BLANK_TIMER].expires = 0;
2421 if (blankinterval) {
2422 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2423 timer_active |= 1<<BLANK_TIMER;
2427 * kmalloc is not running yet - we use the bootmem allocator.
2429 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2430 int j, k ;
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);
2438 kmalloced = 0;
2439 vc_init(currcons, video_num_lines, video_num_columns,
2440 currcons || !sw->con_save_screen);
2441 for (j=k=0; j<16; j++) {
2442 vc_cons[currcons].d->vc_palette[k++] = default_red[j] ;
2443 vc_cons[currcons].d->vc_palette[k++] = default_grn[j] ;
2444 vc_cons[currcons].d->vc_palette[k++] = default_blu[j] ;
2447 currcons = fg_console = 0;
2448 master_display_fg = vc_cons[currcons].d;
2449 set_origin(currcons);
2450 save_screen(currcons);
2451 gotoxy(currcons,x,y);
2452 csi_J(currcons, 0);
2453 update_screen(fg_console);
2454 printk("Console: %s %s %dx%d",
2455 can_do_color ? "colour" : "mono",
2456 display_desc, video_num_columns, video_num_lines);
2457 printable = 1;
2458 printk("\n");
2460 #ifdef CONFIG_VT_CONSOLE
2461 register_console(&vt_console_driver);
2462 #endif
2464 tasklet_enable(&console_tasklet);
2465 tasklet_schedule(&console_tasklet);
2468 #ifndef VT_SINGLE_DRIVER
2470 static void clear_buffer_attributes(int currcons)
2472 unsigned short *p = (unsigned short *) origin;
2473 int count = screenbuf_size/2;
2474 int mask = hi_font_mask | 0xff;
2476 for (; count > 0; count--, p++) {
2477 scr_writew((scr_readw(p)&mask) | (video_erase_char&~mask), p);
2482 * If we support more console drivers, this function is used
2483 * when a driver wants to take over some existing consoles
2484 * and become default driver for newly opened ones.
2487 void take_over_console(struct consw *csw, int first, int last, int deflt)
2489 int i, j = -1;
2490 const char *desc;
2492 desc = csw->con_startup();
2493 if (!desc) return;
2494 if (deflt)
2495 conswitchp = csw;
2497 for (i = first; i <= last; i++) {
2498 int old_was_color;
2499 int currcons = i;
2501 con_driver_map[i] = csw;
2503 if (!vc_cons[i].d || !vc_cons[i].d->vc_sw)
2504 continue;
2506 j = i;
2507 if (IS_VISIBLE)
2508 save_screen(i);
2509 old_was_color = vc_cons[i].d->vc_can_do_color;
2510 vc_cons[i].d->vc_sw->con_deinit(vc_cons[i].d);
2511 visual_init(i, 0);
2512 update_attr(i);
2514 /* If the console changed between mono <-> color, then
2515 * the attributes in the screenbuf will be wrong. The
2516 * following resets all attributes to something sane.
2518 if (old_was_color != vc_cons[i].d->vc_can_do_color)
2519 clear_buffer_attributes(i);
2521 if (IS_VISIBLE)
2522 update_screen(i);
2524 printk("Console: switching ");
2525 if (!deflt)
2526 printk("consoles %d-%d ", first+1, last+1);
2527 if (j >= 0)
2528 printk("to %s %s %dx%d\n",
2529 vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
2530 desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
2531 else
2532 printk("to %s\n", desc);
2535 void give_up_console(struct consw *csw)
2537 int i;
2539 for(i = 0; i < MAX_NR_CONSOLES; i++)
2540 if (con_driver_map[i] == csw)
2541 con_driver_map[i] = NULL;
2544 #endif
2547 * Screen blanking
2550 static void set_vesa_blanking(unsigned long arg)
2552 char *argp = (char *)arg + 1;
2553 unsigned int mode;
2554 get_user(mode, argp);
2555 vesa_blank_mode = (mode < 4) ? mode : 0;
2558 /* We can't register the console with devfs during con_init(), because it
2559 * is called before kmalloc() works. This function is called later to
2560 * do the registration.
2562 void __init con_init_devfs (void)
2564 int i;
2566 for (i = 0; i < console_driver.num; i++)
2567 tty_register_devfs (&console_driver, 0,
2568 console_driver.minor_start + i);
2571 static void vesa_powerdown(void)
2573 struct vc_data *c = vc_cons[fg_console].d;
2575 * Power down if currently suspended (1 or 2),
2576 * suspend if currently blanked (0),
2577 * else do nothing (i.e. already powered down (3)).
2578 * Called only if powerdown features are allowed.
2580 switch (vesa_blank_mode) {
2581 case VESA_NO_BLANKING:
2582 c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1);
2583 break;
2584 case VESA_VSYNC_SUSPEND:
2585 case VESA_HSYNC_SUSPEND:
2586 c->vc_sw->con_blank(c, VESA_POWERDOWN+1);
2587 break;
2591 static void vesa_powerdown_screen(void)
2593 timer_active &= ~(1<<BLANK_TIMER);
2594 timer_table[BLANK_TIMER].fn = unblank_screen;
2596 vesa_powerdown();
2599 void do_blank_screen(int entering_gfx)
2601 int currcons = fg_console;
2602 int i;
2604 if (console_blanked)
2605 return;
2607 /* entering graphics mode? */
2608 if (entering_gfx) {
2609 hide_cursor(currcons);
2610 save_screen(currcons);
2611 sw->con_blank(vc_cons[currcons].d, -1);
2612 console_blanked = fg_console + 1;
2613 set_origin(currcons);
2614 return;
2617 /* don't blank graphics */
2618 if (vcmode != KD_TEXT) {
2619 console_blanked = fg_console + 1;
2620 return;
2623 hide_cursor(currcons);
2624 if (vesa_off_interval) {
2625 timer_table[BLANK_TIMER].fn = vesa_powerdown_screen;
2626 timer_table[BLANK_TIMER].expires = jiffies + vesa_off_interval;
2627 timer_active |= (1<<BLANK_TIMER);
2628 } else {
2629 timer_active &= ~(1<<BLANK_TIMER);
2630 timer_table[BLANK_TIMER].fn = unblank_screen;
2633 save_screen(currcons);
2634 /* In case we need to reset origin, blanking hook returns 1 */
2635 i = sw->con_blank(vc_cons[currcons].d, 1);
2636 console_blanked = fg_console + 1;
2637 if (i)
2638 set_origin(currcons);
2640 if (console_blank_hook && console_blank_hook(1))
2641 return;
2642 if (vesa_blank_mode)
2643 sw->con_blank(vc_cons[currcons].d, vesa_blank_mode + 1);
2646 void unblank_screen(void)
2648 int currcons;
2650 if (!console_blanked)
2651 return;
2652 if (!vc_cons_allocated(fg_console)) {
2653 /* impossible */
2654 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
2655 return;
2657 timer_table[BLANK_TIMER].fn = blank_screen;
2658 if (blankinterval) {
2659 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2660 timer_active |= 1<<BLANK_TIMER;
2663 currcons = fg_console;
2664 console_blanked = 0;
2665 if (console_blank_hook)
2666 console_blank_hook(0);
2667 if (sw->con_blank(vc_cons[currcons].d, 0))
2668 /* Low-level driver cannot restore -> do it ourselves */
2669 update_screen(fg_console);
2670 set_cursor(fg_console);
2673 static void blank_screen(void)
2675 do_blank_screen(0);
2678 void poke_blanked_console(void)
2680 timer_active &= ~(1<<BLANK_TIMER);
2681 if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)
2682 return;
2683 if (console_blanked) {
2684 timer_table[BLANK_TIMER].fn = unblank_screen;
2685 timer_table[BLANK_TIMER].expires = jiffies; /* Now */
2686 timer_active |= 1<<BLANK_TIMER;
2687 } else if (blankinterval) {
2688 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
2689 timer_active |= 1<<BLANK_TIMER;
2694 * Palettes
2697 void set_palette(int currcons)
2699 if (vcmode != KD_GRAPHICS)
2700 sw->con_set_palette(vc_cons[currcons].d, color_table);
2703 static int set_get_cmap(unsigned char *arg, int set)
2705 int i, j, k;
2707 for (i = 0; i < 16; i++)
2708 if (set) {
2709 get_user(default_red[i], arg++);
2710 get_user(default_grn[i], arg++);
2711 get_user(default_blu[i], arg++);
2712 } else {
2713 put_user(default_red[i], arg++);
2714 put_user(default_grn[i], arg++);
2715 put_user(default_blu[i], arg++);
2717 if (set) {
2718 for (i = 0; i < MAX_NR_CONSOLES; i++)
2719 if (vc_cons_allocated(i)) {
2720 for (j = k = 0; j < 16; j++) {
2721 vc_cons[i].d->vc_palette[k++] = default_red[j];
2722 vc_cons[i].d->vc_palette[k++] = default_grn[j];
2723 vc_cons[i].d->vc_palette[k++] = default_blu[j];
2725 set_palette(i);
2728 return 0;
2732 * Load palette into the DAC registers. arg points to a colour
2733 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
2736 int con_set_cmap(unsigned char *arg)
2738 return set_get_cmap (arg,1);
2741 int con_get_cmap(unsigned char *arg)
2743 return set_get_cmap (arg,0);
2746 void reset_palette(int currcons)
2748 int j, k;
2749 for (j=k=0; j<16; j++) {
2750 palette[k++] = default_red[j];
2751 palette[k++] = default_grn[j];
2752 palette[k++] = default_blu[j];
2754 set_palette(currcons);
2758 * Font switching
2760 * Currently we only support fonts up to 32 pixels wide, at a maximum height
2761 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
2762 * depending on width) reserved for each character which is kinda wasty, but
2763 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
2764 * is upto the actual low-level console-driver convert data into its favorite
2765 * format (maybe we should add a `fontoffset' field to the `display'
2766 * structure so we wont have to convert the fontdata all the time.
2767 * /Jes
2770 #define max_font_size 65536
2772 int con_font_op(int currcons, struct console_font_op *op)
2774 int rc = -EINVAL;
2775 int size = max_font_size, set;
2776 u8 *temp = NULL;
2777 struct console_font_op old_op;
2779 if (vt_cons[currcons]->vc_mode != KD_TEXT)
2780 goto quit;
2781 memcpy(&old_op, op, sizeof(old_op));
2782 if (op->op == KD_FONT_OP_SET) {
2783 if (!op->data)
2784 return -EINVAL;
2785 if (op->charcount > 512)
2786 goto quit;
2787 if (!op->height) { /* Need to guess font height [compat] */
2788 int h, i;
2789 u8 *charmap = op->data, tmp;
2791 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
2792 so that we can get rid of this soon */
2793 if (!(op->flags & KD_FONT_FLAG_OLD))
2794 goto quit;
2795 rc = -EFAULT;
2796 for (h = 32; h > 0; h--)
2797 for (i = 0; i < op->charcount; i++) {
2798 if (get_user(tmp, &charmap[32*i+h-1]))
2799 goto quit;
2800 if (tmp)
2801 goto nonzero;
2803 rc = -EINVAL;
2804 goto quit;
2805 nonzero:
2806 rc = -EINVAL;
2807 op->height = h;
2809 if (op->width > 32 || op->height > 32)
2810 goto quit;
2811 size = (op->width+7)/8 * 32 * op->charcount;
2812 if (size > max_font_size)
2813 return -ENOSPC;
2814 set = 1;
2815 } else if (op->op == KD_FONT_OP_GET)
2816 set = 0;
2817 else
2818 return sw->con_font_op(vc_cons[currcons].d, op);
2819 if (op->data) {
2820 temp = kmalloc(size, GFP_KERNEL);
2821 if (!temp)
2822 return -ENOMEM;
2823 if (set && copy_from_user(temp, op->data, size)) {
2824 rc = -EFAULT;
2825 goto quit;
2827 op->data = temp;
2830 spin_lock_irq(&console_lock);
2831 rc = sw->con_font_op(vc_cons[currcons].d, op);
2832 spin_unlock_irq(&console_lock);
2834 op->data = old_op.data;
2835 if (!rc && !set) {
2836 int c = (op->width+7)/8 * 32 * op->charcount;
2838 if (op->data && op->charcount > old_op.charcount)
2839 rc = -ENOSPC;
2840 if (!(op->flags & KD_FONT_FLAG_OLD)) {
2841 if (op->width > old_op.width ||
2842 op->height > old_op.height)
2843 rc = -ENOSPC;
2844 } else {
2845 if (op->width != 8)
2846 rc = -EIO;
2847 else if ((old_op.height && op->height > old_op.height) ||
2848 op->height > 32)
2849 rc = -ENOSPC;
2851 if (!rc && op->data && copy_to_user(op->data, temp, c))
2852 rc = -EFAULT;
2854 quit: if (temp)
2855 kfree_s(temp, size);
2856 return rc;
2860 * Interface exported to selection and vcs.
2863 /* used by selection */
2864 u16 screen_glyph(int currcons, int offset)
2866 u16 w = scr_readw(screenpos(currcons, offset, 1));
2867 u16 c = w & 0xff;
2869 if (w & hi_font_mask)
2870 c |= 0x100;
2871 return c;
2874 /* used by vcs - note the word offset */
2875 unsigned short *screen_pos(int currcons, int w_offset, int viewed)
2877 return screenpos(currcons, 2 * w_offset, viewed);
2880 void getconsxy(int currcons, char *p)
2882 p[0] = x;
2883 p[1] = y;
2886 void putconsxy(int currcons, char *p)
2888 gotoxy(currcons, p[0], p[1]);
2889 set_cursor(currcons);
2892 u16 vcs_scr_readw(int currcons, const u16 *org)
2894 if ((unsigned long)org == pos && softcursor_original != -1)
2895 return softcursor_original;
2896 return scr_readw(org);
2899 void vcs_scr_writew(int currcons, u16 val, u16 *org)
2901 scr_writew(val, org);
2902 if ((unsigned long)org == pos) {
2903 softcursor_original = -1;
2904 add_softcursor(currcons);
2908 static int pm_con_request(struct pm_dev *dev, pm_request_t rqst, void *data)
2910 switch (rqst)
2912 case PM_RESUME:
2913 unblank_screen();
2914 break;
2915 case PM_SUSPEND:
2916 do_blank_screen(0);
2917 break;
2919 return 0;
2923 * Visible symbols for modules
2926 EXPORT_SYMBOL(color_table);
2927 EXPORT_SYMBOL(default_red);
2928 EXPORT_SYMBOL(default_grn);
2929 EXPORT_SYMBOL(default_blu);
2930 EXPORT_SYMBOL(video_font_height);
2931 EXPORT_SYMBOL(video_scan_lines);
2932 EXPORT_SYMBOL(vc_resize);
2933 EXPORT_SYMBOL(fg_console);
2935 #ifndef VT_SINGLE_DRIVER
2936 EXPORT_SYMBOL(take_over_console);
2937 EXPORT_SYMBOL(give_up_console);
2938 #endif