blob: 645ad9808982526cf8f7ae4fdf682e2a433ed72e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vt.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Hopefully this will be a rather complete VT102 implementation.
9 *
10 * Beeping thanks to John T Kohl.
11 *
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
14 *
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
17 *
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
20 *
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
23 *
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
26 *
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
29 *
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
32 *
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
34 *
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <geert@linux-m68k.org>, Jan 1997.
37 *
38 * Original m68k console driver modifications by
39 *
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
42 *
43 * The abstract console driver provides a generic interface for a text
44 * console. It supports VGA text mode, frame buffer based graphical consoles
45 * and special graphics processors that are only accessible through some
46 * registers (e.g. a TMS340x0 GSP).
47 *
48 * The interface to the hardware is specified using a special structure
49 * (struct consw) which contains function pointers to console operations
50 * (see <linux/console.h> for more information).
51 *
52 * Support for changeable cursor shape
53 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
54 *
55 * Ported to i386 and con_scrolldelta fixed
56 * by Emmanuel Marty <core@ggi-project.org>, April 1998
57 *
58 * Resurrected character buffers in videoram plus lots of other trickery
59 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
60 *
61 * Removed old-style timers, introduced console_timer, made timer
62 * deletion SMP-safe. 17Jun00, Andrew Morton <andrewm@uow.edu.au>
63 *
64 * Removed console_lock, enabled interrupts across all console operations
65 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070066 *
67 * Fixed UTF-8 mode so alternate charset modes always work according
68 * to control sequences interpreted in do_con_trol function
69 * preserving backward VT100 semigraphics compatibility,
70 * malformed UTF sequences represented as sequences of replacement glyphs,
71 * original codes or '?' as a last resort if replacement glyph is undefined
72 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
75#include <linux/module.h>
76#include <linux/types.h>
77#include <linux/sched.h>
78#include <linux/tty.h>
79#include <linux/tty_flip.h>
80#include <linux/kernel.h>
81#include <linux/string.h>
82#include <linux/errno.h>
83#include <linux/kd.h>
84#include <linux/slab.h>
85#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070089#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/vt_kern.h>
91#include <linux/selection.h>
92#include <linux/tiocl.h>
93#include <linux/kbd_kern.h>
94#include <linux/consolemap.h>
95#include <linux/timer.h>
96#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/workqueue.h>
98#include <linux/bootmem.h>
99#include <linux/pm.h>
100#include <linux/font.h>
101#include <linux/bitops.h>
102
103#include <asm/io.h>
104#include <asm/system.h>
105#include <asm/uaccess.h>
106
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700107#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700109#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700110#define CON_DRIVER_FLAG_INIT 2
111#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700112
113struct con_driver {
114 const struct consw *con;
115 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700116 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700117 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700118 int first;
119 int last;
120 int flag;
121};
122
123static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124const struct consw *conswitchp;
125
126/* A bitmap for codes <32. A bit of 1 indicates that the code
127 * corresponding to that bit number invokes some special action
128 * (such as cursor movement) and should not be displayed as a
129 * glyph unless the disp_ctrl mode is explicitly enabled.
130 */
131#define CTRL_ACTION 0x0d00ff81
132#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
133
134/*
135 * Here is the default bell parameters: 750HZ, 1/8th of a second
136 */
137#define DEFAULT_BELL_PITCH 750
138#define DEFAULT_BELL_DURATION (HZ/8)
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct vc vc_cons [MAX_NR_CONSOLES];
141
142#ifndef VT_SINGLE_DRIVER
143static const struct consw *con_driver_map[MAX_NR_CONSOLES];
144#endif
145
146static int con_open(struct tty_struct *, struct file *);
147static void vc_init(struct vc_data *vc, unsigned int rows,
148 unsigned int cols, int do_clear);
149static void gotoxy(struct vc_data *vc, int new_x, int new_y);
150static void save_cur(struct vc_data *vc);
151static void reset_terminal(struct vc_data *vc, int do_clear);
152static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800153static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static void set_cursor(struct vc_data *vc);
155static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000156static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void blank_screen_t(unsigned long dummy);
158static void set_palette(struct vc_data *vc);
159
160static int printable; /* Is console ready for printing? */
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700161#ifdef CONFIG_VT_UNICODE
162int default_utf8 = 1;
163#else
164int default_utf8;
165#endif
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700166module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168/*
169 * ignore_poke: don't unblank the screen when things are typed. This is
170 * mainly for the privacy of braille terminal users.
171 */
172static int ignore_poke;
173
174int do_poke_blanked_console;
175int console_blanked;
176
177static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
178static int blankinterval = 10*60*HZ;
179static int vesa_off_interval;
180
David Howells65f27f32006-11-22 14:55:48 +0000181static DECLARE_WORK(console_work, console_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183/*
184 * fg_console is the current virtual console,
185 * last_console is the last used one,
186 * want_console is the console we want to switch to,
187 * kmsg_redirect is the console for kernel messages,
188 */
189int fg_console;
190int last_console;
191int want_console = -1;
192int kmsg_redirect;
193
194/*
195 * For each existing display, we have a pointer to console currently visible
196 * on that display, allowing consoles other than fg_console to be refreshed
197 * appropriately. Unless the low-level driver supplies its own display_fg
198 * variable, we use this one for the "master display".
199 */
200static struct vc_data *master_display_fg;
201
202/*
203 * Unfortunately, we need to delay tty echo when we're currently writing to the
204 * console since the code is (and always was) not re-entrant, so we schedule
205 * all flip requests to process context with schedule-task() and run it from
206 * console_callback().
207 */
208
209/*
210 * For the same reason, we defer scrollback to the console callback.
211 */
212static int scrollback_delta;
213
214/*
215 * Hook so that the power management routines can (un)blank
216 * the console on our behalf.
217 */
218int (*console_blank_hook)(int);
219
Jiri Slaby40565f12007-02-12 00:52:31 -0800220static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221static int blank_state;
222static int blank_timer_expired;
223enum {
224 blank_off = 0,
225 blank_normal_wait,
226 blank_vesa_wait,
227};
228
229/*
230 * Low-Level Functions
231 */
232
233#define IS_FG(vc) ((vc)->vc_num == fg_console)
234
235#ifdef VT_BUF_VRAM_ONLY
236#define DO_UPDATE(vc) 0
237#else
238#define DO_UPDATE(vc) CON_IS_VISIBLE(vc)
239#endif
240
241static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
242{
243 unsigned short *p;
244
245 if (!viewed)
246 p = (unsigned short *)(vc->vc_origin + offset);
247 else if (!vc->vc_sw->con_screen_pos)
248 p = (unsigned short *)(vc->vc_visible_origin + offset);
249 else
250 p = vc->vc_sw->con_screen_pos(vc, offset);
251 return p;
252}
253
254static inline void scrolldelta(int lines)
255{
256 scrollback_delta += lines;
257 schedule_console_callback();
258}
259
260void schedule_console_callback(void)
261{
262 schedule_work(&console_work);
263}
264
265static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
266{
267 unsigned short *d, *s;
268
269 if (t+nr >= b)
270 nr = b - t - 1;
271 if (b > vc->vc_rows || t >= b || nr < 1)
272 return;
273 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
274 return;
275 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
276 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
277 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
278 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
279 vc->vc_size_row * nr);
280}
281
282static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
283{
284 unsigned short *s;
285 unsigned int step;
286
287 if (t+nr >= b)
288 nr = b - t - 1;
289 if (b > vc->vc_rows || t >= b || nr < 1)
290 return;
291 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
292 return;
293 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
294 step = vc->vc_cols * nr;
295 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
296 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
297}
298
299static void do_update_region(struct vc_data *vc, unsigned long start, int count)
300{
301#ifndef VT_BUF_VRAM_ONLY
302 unsigned int xx, yy, offset;
303 u16 *p;
304
305 p = (u16 *) start;
306 if (!vc->vc_sw->con_getxy) {
307 offset = (start - vc->vc_origin) / 2;
308 xx = offset % vc->vc_cols;
309 yy = offset / vc->vc_cols;
310 } else {
311 int nxx, nyy;
312 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
313 xx = nxx; yy = nyy;
314 }
315 for(;;) {
316 u16 attrib = scr_readw(p) & 0xff00;
317 int startx = xx;
318 u16 *q = p;
319 while (xx < vc->vc_cols && count) {
320 if (attrib != (scr_readw(p) & 0xff00)) {
321 if (p > q)
322 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
323 startx = xx;
324 q = p;
325 attrib = scr_readw(p) & 0xff00;
326 }
327 p++;
328 xx++;
329 count--;
330 }
331 if (p > q)
332 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
333 if (!count)
334 break;
335 xx = 0;
336 yy++;
337 if (vc->vc_sw->con_getxy) {
338 p = (u16 *)start;
339 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
340 }
341 }
342#endif
343}
344
345void update_region(struct vc_data *vc, unsigned long start, int count)
346{
347 WARN_CONSOLE_UNLOCKED();
348
349 if (DO_UPDATE(vc)) {
350 hide_cursor(vc);
351 do_update_region(vc, start, count);
352 set_cursor(vc);
353 }
354}
355
356/* Structure of attributes is hardware-dependent */
357
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700358static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
359 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
361 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700362 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
363 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365#ifndef VT_BUF_VRAM_ONLY
366/*
367 * ++roman: I completely changed the attribute format for monochrome
368 * mode (!can_do_color). The formerly used MDA (monochrome display
369 * adapter) format didn't allow the combination of certain effects.
370 * Now the attribute is just a bit vector:
371 * Bit 0..1: intensity (0..2)
372 * Bit 2 : underline
373 * Bit 3 : reverse
374 * Bit 7 : blink
375 */
376 {
377 u8 a = vc->vc_color;
378 if (!vc->vc_can_do_color)
379 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700380 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 (_underline ? 4 : 0) |
382 (_reverse ? 8 : 0) |
383 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700384 if (_italic)
385 a = (a & 0xF0) | vc->vc_itcolor;
386 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 a = (a & 0xf0) | vc->vc_ulcolor;
388 else if (_intensity == 0)
389 a = (a & 0xf0) | vc->vc_ulcolor;
390 if (_reverse)
391 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
392 if (_blink)
393 a ^= 0x80;
394 if (_intensity == 2)
395 a ^= 0x08;
396 if (vc->vc_hi_font_mask == 0x100)
397 a <<= 1;
398 return a;
399 }
400#else
401 return 0;
402#endif
403}
404
405static void update_attr(struct vc_data *vc)
406{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700407 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
408 vc->vc_blink, vc->vc_underline,
409 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
410 vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm, 0) << 8) | ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/* Note: inverting the screen twice should revert to the original state */
414void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
415{
416 unsigned short *p;
417
418 WARN_CONSOLE_UNLOCKED();
419
420 count /= 2;
421 p = screenpos(vc, offset, viewed);
422 if (vc->vc_sw->con_invert_region)
423 vc->vc_sw->con_invert_region(vc, p, count);
424#ifndef VT_BUF_VRAM_ONLY
425 else {
426 u16 *q = p;
427 int cnt = count;
428 u16 a;
429
430 if (!vc->vc_can_do_color) {
431 while (cnt--) {
432 a = scr_readw(q);
433 a ^= 0x0800;
434 scr_writew(a, q);
435 q++;
436 }
437 } else if (vc->vc_hi_font_mask == 0x100) {
438 while (cnt--) {
439 a = scr_readw(q);
440 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
441 scr_writew(a, q);
442 q++;
443 }
444 } else {
445 while (cnt--) {
446 a = scr_readw(q);
447 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
448 scr_writew(a, q);
449 q++;
450 }
451 }
452 }
453#endif
454 if (DO_UPDATE(vc))
455 do_update_region(vc, (unsigned long) p, count);
456}
457
458/* used by selection: complement pointer position */
459void complement_pos(struct vc_data *vc, int offset)
460{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700461 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 static unsigned short old;
463 static unsigned short oldx, oldy;
464
465 WARN_CONSOLE_UNLOCKED();
466
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700467 if (old_offset != -1 && old_offset >= 0 &&
468 old_offset < vc->vc_screenbuf_size) {
469 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (DO_UPDATE(vc))
471 vc->vc_sw->con_putc(vc, old, oldy, oldx);
472 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700473
474 old_offset = offset;
475
476 if (offset != -1 && offset >= 0 &&
477 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700479 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 p = screenpos(vc, offset, 1);
481 old = scr_readw(p);
482 new = old ^ vc->vc_complement_mask;
483 scr_writew(new, p);
484 if (DO_UPDATE(vc)) {
485 oldx = (offset >> 1) % vc->vc_cols;
486 oldy = (offset >> 1) / vc->vc_cols;
487 vc->vc_sw->con_putc(vc, new, oldy, oldx);
488 }
489 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static void insert_char(struct vc_data *vc, unsigned int nr)
494{
495 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
496
497 p = q + vc->vc_cols - nr - vc->vc_x;
498 while (--p >= q)
499 scr_writew(scr_readw(p), p + nr);
500 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
501 vc->vc_need_wrap = 0;
502 if (DO_UPDATE(vc)) {
503 unsigned short oldattr = vc->vc_attr;
504 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
505 vc->vc_cols - vc->vc_x - nr);
506 vc->vc_attr = vc->vc_video_erase_char >> 8;
507 while (nr--)
508 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
509 vc->vc_attr = oldattr;
510 }
511}
512
513static void delete_char(struct vc_data *vc, unsigned int nr)
514{
515 unsigned int i = vc->vc_x;
516 unsigned short *p = (unsigned short *)vc->vc_pos;
517
518 while (++i <= vc->vc_cols - nr) {
519 scr_writew(scr_readw(p+nr), p);
520 p++;
521 }
522 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
523 vc->vc_need_wrap = 0;
524 if (DO_UPDATE(vc)) {
525 unsigned short oldattr = vc->vc_attr;
526 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
527 vc->vc_cols - vc->vc_x - nr);
528 vc->vc_attr = vc->vc_video_erase_char >> 8;
529 while (nr--)
530 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
531 vc->vc_cols - 1 - nr);
532 vc->vc_attr = oldattr;
533 }
534}
535
536static int softcursor_original;
537
538static void add_softcursor(struct vc_data *vc)
539{
540 int i = scr_readw((u16 *) vc->vc_pos);
541 u32 type = vc->vc_cursor_type;
542
543 if (! (type & 0x10)) return;
544 if (softcursor_original != -1) return;
545 softcursor_original = i;
546 i |= ((type >> 8) & 0xff00 );
547 i ^= ((type) & 0xff00 );
548 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
549 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
550 scr_writew(i, (u16 *) vc->vc_pos);
551 if (DO_UPDATE(vc))
552 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
553}
554
555static void hide_softcursor(struct vc_data *vc)
556{
557 if (softcursor_original != -1) {
558 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
559 if (DO_UPDATE(vc))
560 vc->vc_sw->con_putc(vc, softcursor_original,
561 vc->vc_y, vc->vc_x);
562 softcursor_original = -1;
563 }
564}
565
566static void hide_cursor(struct vc_data *vc)
567{
568 if (vc == sel_cons)
569 clear_selection();
570 vc->vc_sw->con_cursor(vc, CM_ERASE);
571 hide_softcursor(vc);
572}
573
574static void set_cursor(struct vc_data *vc)
575{
576 if (!IS_FG(vc) || console_blanked ||
577 vc->vc_mode == KD_GRAPHICS)
578 return;
579 if (vc->vc_deccm) {
580 if (vc == sel_cons)
581 clear_selection();
582 add_softcursor(vc);
583 if ((vc->vc_cursor_type & 0x0f) != 1)
584 vc->vc_sw->con_cursor(vc, CM_DRAW);
585 } else
586 hide_cursor(vc);
587}
588
589static void set_origin(struct vc_data *vc)
590{
591 WARN_CONSOLE_UNLOCKED();
592
593 if (!CON_IS_VISIBLE(vc) ||
594 !vc->vc_sw->con_set_origin ||
595 !vc->vc_sw->con_set_origin(vc))
596 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
597 vc->vc_visible_origin = vc->vc_origin;
598 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
599 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
600}
601
602static inline void save_screen(struct vc_data *vc)
603{
604 WARN_CONSOLE_UNLOCKED();
605
606 if (vc->vc_sw->con_save_screen)
607 vc->vc_sw->con_save_screen(vc);
608}
609
610/*
611 * Redrawing of screen
612 */
613
614static void clear_buffer_attributes(struct vc_data *vc)
615{
616 unsigned short *p = (unsigned short *)vc->vc_origin;
617 int count = vc->vc_screenbuf_size / 2;
618 int mask = vc->vc_hi_font_mask | 0xff;
619
620 for (; count > 0; count--, p++) {
621 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
622 }
623}
624
625void redraw_screen(struct vc_data *vc, int is_switch)
626{
627 int redraw = 0;
628
629 WARN_CONSOLE_UNLOCKED();
630
631 if (!vc) {
632 /* strange ... */
633 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
634 return;
635 }
636
637 if (is_switch) {
638 struct vc_data *old_vc = vc_cons[fg_console].d;
639 if (old_vc == vc)
640 return;
641 if (!CON_IS_VISIBLE(vc))
642 redraw = 1;
643 *vc->vc_display_fg = vc;
644 fg_console = vc->vc_num;
645 hide_cursor(old_vc);
646 if (!CON_IS_VISIBLE(old_vc)) {
647 save_screen(old_vc);
648 set_origin(old_vc);
649 }
650 } else {
651 hide_cursor(vc);
652 redraw = 1;
653 }
654
655 if (redraw) {
656 int update;
657 int old_was_color = vc->vc_can_do_color;
658
659 set_origin(vc);
660 update = vc->vc_sw->con_switch(vc);
661 set_palette(vc);
662 /*
663 * If console changed from mono<->color, the best we can do
664 * is to clear the buffer attributes. As it currently stands,
665 * rebuilding new attributes from the old buffer is not doable
666 * without overly complex code.
667 */
668 if (old_was_color != vc->vc_can_do_color) {
669 update_attr(vc);
670 clear_buffer_attributes(vc);
671 }
672 if (update && vc->vc_mode != KD_GRAPHICS)
673 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
674 }
675 set_cursor(vc);
676 if (is_switch) {
677 set_leds();
678 compute_shiftstate();
679 }
680}
681
682/*
683 * Allocation, freeing and resizing of VTs.
684 */
685
686int vc_cons_allocated(unsigned int i)
687{
688 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
689}
690
691static void visual_init(struct vc_data *vc, int num, int init)
692{
693 /* ++Geert: vc->vc_sw->con_init determines console size */
694 if (vc->vc_sw)
695 module_put(vc->vc_sw->owner);
696 vc->vc_sw = conswitchp;
697#ifndef VT_SINGLE_DRIVER
698 if (con_driver_map[num])
699 vc->vc_sw = con_driver_map[num];
700#endif
701 __module_get(vc->vc_sw->owner);
702 vc->vc_num = num;
703 vc->vc_display_fg = &master_display_fg;
704 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
705 vc->vc_uni_pagedir = 0;
706 vc->vc_hi_font_mask = 0;
707 vc->vc_complement_mask = 0;
708 vc->vc_can_do_color = 0;
709 vc->vc_sw->con_init(vc, init);
710 if (!vc->vc_complement_mask)
711 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
712 vc->vc_s_complement_mask = vc->vc_complement_mask;
713 vc->vc_size_row = vc->vc_cols << 1;
714 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
715}
716
717int vc_allocate(unsigned int currcons) /* return 0 on success */
718{
719 WARN_CONSOLE_UNLOCKED();
720
721 if (currcons >= MAX_NR_CONSOLES)
722 return -ENXIO;
723 if (!vc_cons[currcons].d) {
724 struct vc_data *vc;
725
726 /* prevent users from taking too much memory */
727 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
728 return -EPERM;
729
730 /* due to the granularity of kmalloc, we waste some memory here */
731 /* the alloc is done in two steps, to optimize the common situation
732 of a 25x80 console (structsize=216, screenbuf_size=4000) */
733 /* although the numbers above are not valid since long ago, the
734 point is still up-to-date and the comment still has its value
735 even if only as a historical artifact. --mj, July 1998 */
Jiri Slabya1152932007-07-17 04:05:21 -0700736 vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!vc)
738 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 vc_cons[currcons].d = vc;
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800740 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 visual_init(vc, currcons, 1);
742 if (!*vc->vc_uni_pagedir_loc)
743 con_set_default_unimap(vc);
Catalin Marinas254e9482006-09-29 02:00:25 -0700744 if (!vc->vc_kmalloced)
745 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!vc->vc_screenbuf) {
747 kfree(vc);
748 vc_cons[currcons].d = NULL;
749 return -ENOMEM;
750 }
751 vc->vc_kmalloced = 1;
752 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
753 }
754 return 0;
755}
756
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700757static inline int resize_screen(struct vc_data *vc, int width, int height,
758 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759{
760 /* Resizes the resolution of the display adapater */
761 int err = 0;
762
763 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700764 err = vc->vc_sw->con_resize(vc, width, height, user);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 return err;
767}
768
769/*
770 * Change # of rows and columns (0 means unchanged/the size of fg_console)
771 * [this is to be used together with some user program
772 * like resize that changes the hardware videomode]
773 */
774#define VC_RESIZE_MAXCOL (32767)
775#define VC_RESIZE_MAXROW (32767)
776int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int lines)
777{
778 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
779 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
780 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700781 unsigned int end, user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 unsigned short *newscreen;
783
784 WARN_CONSOLE_UNLOCKED();
785
786 if (!vc)
787 return -ENXIO;
788
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700789 user = vc->vc_resize_user;
790 vc->vc_resize_user = 0;
791
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
793 return -EINVAL;
794
795 new_cols = (cols ? cols : vc->vc_cols);
796 new_rows = (lines ? lines : vc->vc_rows);
797 new_row_size = new_cols << 1;
798 new_screen_size = new_row_size * new_rows;
799
800 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
801 return 0;
802
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800803 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (!newscreen)
805 return -ENOMEM;
806
807 old_rows = vc->vc_rows;
808 old_cols = vc->vc_cols;
809 old_row_size = vc->vc_size_row;
810 old_screen_size = vc->vc_screenbuf_size;
811
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700812 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (err) {
814 kfree(newscreen);
815 return err;
816 }
817
818 vc->vc_rows = new_rows;
819 vc->vc_cols = new_cols;
820 vc->vc_size_row = new_row_size;
821 vc->vc_screenbuf_size = new_screen_size;
822
823 rlth = min(old_row_size, new_row_size);
824 rrem = new_row_size - rlth;
825 old_origin = vc->vc_origin;
826 new_origin = (long) newscreen;
827 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700828
829 if (vc->vc_y > new_rows) {
830 if (old_rows - vc->vc_y < new_rows) {
831 /*
832 * Cursor near the bottom, copy contents from the
833 * bottom of buffer
834 */
835 old_origin += (old_rows - new_rows) * old_row_size;
836 end = vc->vc_scr_end;
837 } else {
838 /*
839 * Cursor is in no man's land, copy 1/2 screenful
840 * from the top and bottom of cursor position
841 */
842 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800843 end = old_origin + (old_row_size * new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700844 }
845 } else
846 /*
847 * Cursor near the top, copy contents from the top of buffer
848 */
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800849 end = (old_rows > new_rows) ? old_origin +
850 (old_row_size * new_rows) :
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700851 vc->vc_scr_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 update_attr(vc);
854
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700855 while (old_origin < end) {
856 scr_memcpyw((unsigned short *) new_origin,
857 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700859 scr_memsetw((void *)(new_origin + rlth),
860 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 old_origin += old_row_size;
862 new_origin += new_row_size;
863 }
864 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700865 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
866 new_scr_end - new_origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (vc->vc_kmalloced)
868 kfree(vc->vc_screenbuf);
869 vc->vc_screenbuf = newscreen;
870 vc->vc_kmalloced = 1;
871 vc->vc_screenbuf_size = new_screen_size;
872 set_origin(vc);
873
874 /* do part of a reset_terminal() */
875 vc->vc_top = 0;
876 vc->vc_bottom = vc->vc_rows;
877 gotoxy(vc, vc->vc_x, vc->vc_y);
878 save_cur(vc);
879
880 if (vc->vc_tty) {
881 struct winsize ws, *cws = &vc->vc_tty->winsize;
882
883 memset(&ws, 0, sizeof(ws));
884 ws.ws_row = vc->vc_rows;
885 ws.ws_col = vc->vc_cols;
886 ws.ws_ypixel = vc->vc_scan_lines;
887 if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
Eric W. Biedermanab521dc2007-02-12 00:53:00 -0800888 vc->vc_tty->pgrp)
889 kill_pgrp(vc->vc_tty->pgrp, SIGWINCH, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 *cws = ws;
891 }
892
893 if (CON_IS_VISIBLE(vc))
894 update_screen(vc);
895 return err;
896}
897
Alan Coxca9bda02006-09-29 02:00:03 -0700898int vc_lock_resize(struct vc_data *vc, unsigned int cols, unsigned int lines)
899{
900 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Alan Coxca9bda02006-09-29 02:00:03 -0700902 acquire_console_sem();
903 rc = vc_resize(vc, cols, lines);
904 release_console_sem();
905 return rc;
906}
907
908void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909{
910 WARN_CONSOLE_UNLOCKED();
911
912 if (vc_cons_allocated(currcons)) {
913 struct vc_data *vc = vc_cons[currcons].d;
914 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -0700915 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -0700916 module_put(vc->vc_sw->owner);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 if (vc->vc_kmalloced)
918 kfree(vc->vc_screenbuf);
919 if (currcons >= MIN_NR_CONSOLES)
920 kfree(vc);
921 vc_cons[currcons].d = NULL;
922 }
923}
924
925/*
926 * VT102 emulator
927 */
928
929#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
930#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
931#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
932
933#define decarm VC_REPEAT
934#define decckm VC_CKMODE
935#define kbdapplic VC_APPLIC
936#define lnm VC_CRLF
937
938/*
939 * this is what the terminal answers to a ESC-Z or csi0c query.
940 */
941#define VT100ID "\033[?1;2c"
942#define VT102ID "\033[?6c"
943
944unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
945 8,12,10,14, 9,13,11,15 };
946
947/* the default colour table, for VGA+ colour systems */
948int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
949 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
950int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
951 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
952int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
953 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
954
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -0700955module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
956module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
957module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959/*
960 * gotoxy() must verify all boundaries, because the arguments
961 * might also be negative. If the given position is out of
962 * bounds, the cursor is placed at the nearest margin.
963 */
964static void gotoxy(struct vc_data *vc, int new_x, int new_y)
965{
966 int min_y, max_y;
967
968 if (new_x < 0)
969 vc->vc_x = 0;
970 else {
971 if (new_x >= vc->vc_cols)
972 vc->vc_x = vc->vc_cols - 1;
973 else
974 vc->vc_x = new_x;
975 }
976
977 if (vc->vc_decom) {
978 min_y = vc->vc_top;
979 max_y = vc->vc_bottom;
980 } else {
981 min_y = 0;
982 max_y = vc->vc_rows;
983 }
984 if (new_y < min_y)
985 vc->vc_y = min_y;
986 else if (new_y >= max_y)
987 vc->vc_y = max_y - 1;
988 else
989 vc->vc_y = new_y;
990 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
991 vc->vc_need_wrap = 0;
992}
993
994/* for absolute user moves, when decom is set */
995static void gotoxay(struct vc_data *vc, int new_x, int new_y)
996{
997 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
998}
999
1000void scrollback(struct vc_data *vc, int lines)
1001{
1002 if (!lines)
1003 lines = vc->vc_rows / 2;
1004 scrolldelta(-lines);
1005}
1006
1007void scrollfront(struct vc_data *vc, int lines)
1008{
1009 if (!lines)
1010 lines = vc->vc_rows / 2;
1011 scrolldelta(lines);
1012}
1013
1014static void lf(struct vc_data *vc)
1015{
1016 /* don't scroll if above bottom of scrolling region, or
1017 * if below scrolling region
1018 */
1019 if (vc->vc_y + 1 == vc->vc_bottom)
1020 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1021 else if (vc->vc_y < vc->vc_rows - 1) {
1022 vc->vc_y++;
1023 vc->vc_pos += vc->vc_size_row;
1024 }
1025 vc->vc_need_wrap = 0;
1026}
1027
1028static void ri(struct vc_data *vc)
1029{
1030 /* don't scroll if below top of scrolling region, or
1031 * if above scrolling region
1032 */
1033 if (vc->vc_y == vc->vc_top)
1034 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1035 else if (vc->vc_y > 0) {
1036 vc->vc_y--;
1037 vc->vc_pos -= vc->vc_size_row;
1038 }
1039 vc->vc_need_wrap = 0;
1040}
1041
1042static inline void cr(struct vc_data *vc)
1043{
1044 vc->vc_pos -= vc->vc_x << 1;
1045 vc->vc_need_wrap = vc->vc_x = 0;
1046}
1047
1048static inline void bs(struct vc_data *vc)
1049{
1050 if (vc->vc_x) {
1051 vc->vc_pos -= 2;
1052 vc->vc_x--;
1053 vc->vc_need_wrap = 0;
1054 }
1055}
1056
1057static inline void del(struct vc_data *vc)
1058{
1059 /* ignored */
1060}
1061
1062static void csi_J(struct vc_data *vc, int vpar)
1063{
1064 unsigned int count;
1065 unsigned short * start;
1066
1067 switch (vpar) {
1068 case 0: /* erase from cursor to end of display */
1069 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1070 start = (unsigned short *)vc->vc_pos;
1071 if (DO_UPDATE(vc)) {
1072 /* do in two stages */
1073 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1074 vc->vc_cols - vc->vc_x);
1075 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1076 vc->vc_rows - vc->vc_y - 1,
1077 vc->vc_cols);
1078 }
1079 break;
1080 case 1: /* erase from start to cursor */
1081 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1082 start = (unsigned short *)vc->vc_origin;
1083 if (DO_UPDATE(vc)) {
1084 /* do in two stages */
1085 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1086 vc->vc_cols);
1087 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1088 vc->vc_x + 1);
1089 }
1090 break;
1091 case 2: /* erase whole display */
1092 count = vc->vc_cols * vc->vc_rows;
1093 start = (unsigned short *)vc->vc_origin;
1094 if (DO_UPDATE(vc))
1095 vc->vc_sw->con_clear(vc, 0, 0,
1096 vc->vc_rows,
1097 vc->vc_cols);
1098 break;
1099 default:
1100 return;
1101 }
1102 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1103 vc->vc_need_wrap = 0;
1104}
1105
1106static void csi_K(struct vc_data *vc, int vpar)
1107{
1108 unsigned int count;
1109 unsigned short * start;
1110
1111 switch (vpar) {
1112 case 0: /* erase from cursor to end of line */
1113 count = vc->vc_cols - vc->vc_x;
1114 start = (unsigned short *)vc->vc_pos;
1115 if (DO_UPDATE(vc))
1116 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1117 vc->vc_cols - vc->vc_x);
1118 break;
1119 case 1: /* erase from start of line to cursor */
1120 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1121 count = vc->vc_x + 1;
1122 if (DO_UPDATE(vc))
1123 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1124 vc->vc_x + 1);
1125 break;
1126 case 2: /* erase whole line */
1127 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1128 count = vc->vc_cols;
1129 if (DO_UPDATE(vc))
1130 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1131 vc->vc_cols);
1132 break;
1133 default:
1134 return;
1135 }
1136 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1137 vc->vc_need_wrap = 0;
1138}
1139
1140static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1141{ /* not vt100? */
1142 int count;
1143
1144 if (!vpar)
1145 vpar++;
1146 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1147
1148 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1149 if (DO_UPDATE(vc))
1150 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1151 vc->vc_need_wrap = 0;
1152}
1153
1154static void default_attr(struct vc_data *vc)
1155{
1156 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001157 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 vc->vc_underline = 0;
1159 vc->vc_reverse = 0;
1160 vc->vc_blink = 0;
1161 vc->vc_color = vc->vc_def_color;
1162}
1163
1164/* console_sem is held */
1165static void csi_m(struct vc_data *vc)
1166{
1167 int i;
1168
1169 for (i = 0; i <= vc->vc_npar; i++)
1170 switch (vc->vc_par[i]) {
1171 case 0: /* all attributes off */
1172 default_attr(vc);
1173 break;
1174 case 1:
1175 vc->vc_intensity = 2;
1176 break;
1177 case 2:
1178 vc->vc_intensity = 0;
1179 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001180 case 3:
1181 vc->vc_italic = 1;
1182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 case 4:
1184 vc->vc_underline = 1;
1185 break;
1186 case 5:
1187 vc->vc_blink = 1;
1188 break;
1189 case 7:
1190 vc->vc_reverse = 1;
1191 break;
1192 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1193 * Select primary font, don't display
1194 * control chars if defined, don't set
1195 * bit 8 on output.
1196 */
1197 vc->vc_translate = set_translate(vc->vc_charset == 0
1198 ? vc->vc_G0_charset
1199 : vc->vc_G1_charset, vc);
1200 vc->vc_disp_ctrl = 0;
1201 vc->vc_toggle_meta = 0;
1202 break;
1203 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1204 * Select first alternate font, lets
1205 * chars < 32 be displayed as ROM chars.
1206 */
1207 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1208 vc->vc_disp_ctrl = 1;
1209 vc->vc_toggle_meta = 0;
1210 break;
1211 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1212 * Select second alternate font, toggle
1213 * high bit before displaying as ROM char.
1214 */
1215 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1216 vc->vc_disp_ctrl = 1;
1217 vc->vc_toggle_meta = 1;
1218 break;
1219 case 21:
1220 case 22:
1221 vc->vc_intensity = 1;
1222 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001223 case 23:
1224 vc->vc_italic = 0;
1225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 case 24:
1227 vc->vc_underline = 0;
1228 break;
1229 case 25:
1230 vc->vc_blink = 0;
1231 break;
1232 case 27:
1233 vc->vc_reverse = 0;
1234 break;
1235 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1236 * Enables underscore, white foreground
1237 * with white underscore (Linux - use
1238 * default foreground).
1239 */
1240 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1241 vc->vc_underline = 1;
1242 break;
1243 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1244 * Disable underline option.
1245 * Reset colour to default? It did this
1246 * before...
1247 */
1248 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1249 vc->vc_underline = 0;
1250 break;
1251 case 49:
1252 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1253 break;
1254 default:
1255 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1256 vc->vc_color = color_table[vc->vc_par[i] - 30]
1257 | (vc->vc_color & 0xf0);
1258 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1259 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1260 | (vc->vc_color & 0x0f);
1261 break;
1262 }
1263 update_attr(vc);
1264}
1265
1266static void respond_string(const char *p, struct tty_struct *tty)
1267{
1268 while (*p) {
1269 tty_insert_flip_char(tty, *p, 0);
1270 p++;
1271 }
1272 con_schedule_flip(tty);
1273}
1274
1275static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1276{
1277 char buf[40];
1278
1279 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1280 respond_string(buf, tty);
1281}
1282
1283static inline void status_report(struct tty_struct *tty)
1284{
1285 respond_string("\033[0n", tty); /* Terminal ok */
1286}
1287
1288static inline void respond_ID(struct tty_struct * tty)
1289{
1290 respond_string(VT102ID, tty);
1291}
1292
1293void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1294{
1295 char buf[8];
1296
1297 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1298 (char)('!' + mry));
1299 respond_string(buf, tty);
1300}
1301
1302/* invoked via ioctl(TIOCLINUX) and through set_selection */
1303int mouse_reporting(void)
1304{
1305 return vc_cons[fg_console].d->vc_report_mouse;
1306}
1307
1308/* console_sem is held */
1309static void set_mode(struct vc_data *vc, int on_off)
1310{
1311 int i;
1312
1313 for (i = 0; i <= vc->vc_npar; i++)
1314 if (vc->vc_ques) {
1315 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1316 case 1: /* Cursor keys send ^[Ox/^[[x */
1317 if (on_off)
1318 set_kbd(vc, decckm);
1319 else
1320 clr_kbd(vc, decckm);
1321 break;
1322 case 3: /* 80/132 mode switch unimplemented */
1323 vc->vc_deccolm = on_off;
1324#if 0
1325 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1326 /* this alone does not suffice; some user mode
1327 utility has to change the hardware regs */
1328#endif
1329 break;
1330 case 5: /* Inverted screen on/off */
1331 if (vc->vc_decscnm != on_off) {
1332 vc->vc_decscnm = on_off;
1333 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1334 update_attr(vc);
1335 }
1336 break;
1337 case 6: /* Origin relative/absolute */
1338 vc->vc_decom = on_off;
1339 gotoxay(vc, 0, 0);
1340 break;
1341 case 7: /* Autowrap on/off */
1342 vc->vc_decawm = on_off;
1343 break;
1344 case 8: /* Autorepeat on/off */
1345 if (on_off)
1346 set_kbd(vc, decarm);
1347 else
1348 clr_kbd(vc, decarm);
1349 break;
1350 case 9:
1351 vc->vc_report_mouse = on_off ? 1 : 0;
1352 break;
1353 case 25: /* Cursor on/off */
1354 vc->vc_deccm = on_off;
1355 break;
1356 case 1000:
1357 vc->vc_report_mouse = on_off ? 2 : 0;
1358 break;
1359 }
1360 } else {
1361 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1362 case 3: /* Monitor (display ctrls) */
1363 vc->vc_disp_ctrl = on_off;
1364 break;
1365 case 4: /* Insert Mode on/off */
1366 vc->vc_decim = on_off;
1367 break;
1368 case 20: /* Lf, Enter == CrLf/Lf */
1369 if (on_off)
1370 set_kbd(vc, lnm);
1371 else
1372 clr_kbd(vc, lnm);
1373 break;
1374 }
1375 }
1376}
1377
1378/* console_sem is held */
1379static void setterm_command(struct vc_data *vc)
1380{
1381 switch(vc->vc_par[0]) {
1382 case 1: /* set color for underline mode */
1383 if (vc->vc_can_do_color &&
1384 vc->vc_par[1] < 16) {
1385 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1386 if (vc->vc_underline)
1387 update_attr(vc);
1388 }
1389 break;
1390 case 2: /* set color for half intensity mode */
1391 if (vc->vc_can_do_color &&
1392 vc->vc_par[1] < 16) {
1393 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1394 if (vc->vc_intensity == 0)
1395 update_attr(vc);
1396 }
1397 break;
1398 case 8: /* store colors as defaults */
1399 vc->vc_def_color = vc->vc_attr;
1400 if (vc->vc_hi_font_mask == 0x100)
1401 vc->vc_def_color >>= 1;
1402 default_attr(vc);
1403 update_attr(vc);
1404 break;
1405 case 9: /* set blanking interval */
1406 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1407 poke_blanked_console();
1408 break;
1409 case 10: /* set bell frequency in Hz */
1410 if (vc->vc_npar >= 1)
1411 vc->vc_bell_pitch = vc->vc_par[1];
1412 else
1413 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1414 break;
1415 case 11: /* set bell duration in msec */
1416 if (vc->vc_npar >= 1)
1417 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1418 vc->vc_par[1] * HZ / 1000 : 0;
1419 else
1420 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1421 break;
1422 case 12: /* bring specified console to the front */
1423 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1424 set_console(vc->vc_par[1] - 1);
1425 break;
1426 case 13: /* unblank the screen */
1427 poke_blanked_console();
1428 break;
1429 case 14: /* set vesa powerdown interval */
1430 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1431 break;
1432 case 15: /* activate the previous console */
1433 set_console(last_console);
1434 break;
1435 }
1436}
1437
1438/* console_sem is held */
1439static void csi_at(struct vc_data *vc, unsigned int nr)
1440{
1441 if (nr > vc->vc_cols - vc->vc_x)
1442 nr = vc->vc_cols - vc->vc_x;
1443 else if (!nr)
1444 nr = 1;
1445 insert_char(vc, nr);
1446}
1447
1448/* console_sem is held */
1449static void csi_L(struct vc_data *vc, unsigned int nr)
1450{
1451 if (nr > vc->vc_rows - vc->vc_y)
1452 nr = vc->vc_rows - vc->vc_y;
1453 else if (!nr)
1454 nr = 1;
1455 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1456 vc->vc_need_wrap = 0;
1457}
1458
1459/* console_sem is held */
1460static void csi_P(struct vc_data *vc, unsigned int nr)
1461{
1462 if (nr > vc->vc_cols - vc->vc_x)
1463 nr = vc->vc_cols - vc->vc_x;
1464 else if (!nr)
1465 nr = 1;
1466 delete_char(vc, nr);
1467}
1468
1469/* console_sem is held */
1470static void csi_M(struct vc_data *vc, unsigned int nr)
1471{
1472 if (nr > vc->vc_rows - vc->vc_y)
1473 nr = vc->vc_rows - vc->vc_y;
1474 else if (!nr)
1475 nr=1;
1476 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1477 vc->vc_need_wrap = 0;
1478}
1479
1480/* console_sem is held (except via vc_init->reset_terminal */
1481static void save_cur(struct vc_data *vc)
1482{
1483 vc->vc_saved_x = vc->vc_x;
1484 vc->vc_saved_y = vc->vc_y;
1485 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001486 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 vc->vc_s_underline = vc->vc_underline;
1488 vc->vc_s_blink = vc->vc_blink;
1489 vc->vc_s_reverse = vc->vc_reverse;
1490 vc->vc_s_charset = vc->vc_charset;
1491 vc->vc_s_color = vc->vc_color;
1492 vc->vc_saved_G0 = vc->vc_G0_charset;
1493 vc->vc_saved_G1 = vc->vc_G1_charset;
1494}
1495
1496/* console_sem is held */
1497static void restore_cur(struct vc_data *vc)
1498{
1499 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1500 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001501 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 vc->vc_underline = vc->vc_s_underline;
1503 vc->vc_blink = vc->vc_s_blink;
1504 vc->vc_reverse = vc->vc_s_reverse;
1505 vc->vc_charset = vc->vc_s_charset;
1506 vc->vc_color = vc->vc_s_color;
1507 vc->vc_G0_charset = vc->vc_saved_G0;
1508 vc->vc_G1_charset = vc->vc_saved_G1;
1509 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1510 update_attr(vc);
1511 vc->vc_need_wrap = 0;
1512}
1513
1514enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1515 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1516 ESpalette };
1517
1518/* console_sem is held (except via vc_init()) */
1519static void reset_terminal(struct vc_data *vc, int do_clear)
1520{
1521 vc->vc_top = 0;
1522 vc->vc_bottom = vc->vc_rows;
1523 vc->vc_state = ESnormal;
1524 vc->vc_ques = 0;
1525 vc->vc_translate = set_translate(LAT1_MAP, vc);
1526 vc->vc_G0_charset = LAT1_MAP;
1527 vc->vc_G1_charset = GRAF_MAP;
1528 vc->vc_charset = 0;
1529 vc->vc_need_wrap = 0;
1530 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001531 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 vc->vc_utf_count = 0;
1533
1534 vc->vc_disp_ctrl = 0;
1535 vc->vc_toggle_meta = 0;
1536
1537 vc->vc_decscnm = 0;
1538 vc->vc_decom = 0;
1539 vc->vc_decawm = 1;
1540 vc->vc_deccm = 1;
1541 vc->vc_decim = 0;
1542
1543 set_kbd(vc, decarm);
1544 clr_kbd(vc, decckm);
1545 clr_kbd(vc, kbdapplic);
1546 clr_kbd(vc, lnm);
1547 kbd_table[vc->vc_num].lockstate = 0;
1548 kbd_table[vc->vc_num].slockstate = 0;
1549 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1550 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1551 /* do not do set_leds here because this causes an endless tasklet loop
1552 when the keyboard hasn't been initialized yet */
1553
1554 vc->vc_cursor_type = CUR_DEFAULT;
1555 vc->vc_complement_mask = vc->vc_s_complement_mask;
1556
1557 default_attr(vc);
1558 update_attr(vc);
1559
1560 vc->vc_tab_stop[0] = 0x01010100;
1561 vc->vc_tab_stop[1] =
1562 vc->vc_tab_stop[2] =
1563 vc->vc_tab_stop[3] =
1564 vc->vc_tab_stop[4] = 0x01010101;
1565
1566 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1567 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1568
1569 gotoxy(vc, 0, 0);
1570 save_cur(vc);
1571 if (do_clear)
1572 csi_J(vc, 2);
1573}
1574
1575/* console_sem is held */
1576static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1577{
1578 /*
1579 * Control characters can be used in the _middle_
1580 * of an escape sequence.
1581 */
1582 switch (c) {
1583 case 0:
1584 return;
1585 case 7:
1586 if (vc->vc_bell_duration)
1587 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1588 return;
1589 case 8:
1590 bs(vc);
1591 return;
1592 case 9:
1593 vc->vc_pos -= (vc->vc_x << 1);
1594 while (vc->vc_x < vc->vc_cols - 1) {
1595 vc->vc_x++;
1596 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1597 break;
1598 }
1599 vc->vc_pos += (vc->vc_x << 1);
1600 return;
1601 case 10: case 11: case 12:
1602 lf(vc);
1603 if (!is_kbd(vc, lnm))
1604 return;
1605 case 13:
1606 cr(vc);
1607 return;
1608 case 14:
1609 vc->vc_charset = 1;
1610 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1611 vc->vc_disp_ctrl = 1;
1612 return;
1613 case 15:
1614 vc->vc_charset = 0;
1615 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1616 vc->vc_disp_ctrl = 0;
1617 return;
1618 case 24: case 26:
1619 vc->vc_state = ESnormal;
1620 return;
1621 case 27:
1622 vc->vc_state = ESesc;
1623 return;
1624 case 127:
1625 del(vc);
1626 return;
1627 case 128+27:
1628 vc->vc_state = ESsquare;
1629 return;
1630 }
1631 switch(vc->vc_state) {
1632 case ESesc:
1633 vc->vc_state = ESnormal;
1634 switch (c) {
1635 case '[':
1636 vc->vc_state = ESsquare;
1637 return;
1638 case ']':
1639 vc->vc_state = ESnonstd;
1640 return;
1641 case '%':
1642 vc->vc_state = ESpercent;
1643 return;
1644 case 'E':
1645 cr(vc);
1646 lf(vc);
1647 return;
1648 case 'M':
1649 ri(vc);
1650 return;
1651 case 'D':
1652 lf(vc);
1653 return;
1654 case 'H':
1655 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1656 return;
1657 case 'Z':
1658 respond_ID(tty);
1659 return;
1660 case '7':
1661 save_cur(vc);
1662 return;
1663 case '8':
1664 restore_cur(vc);
1665 return;
1666 case '(':
1667 vc->vc_state = ESsetG0;
1668 return;
1669 case ')':
1670 vc->vc_state = ESsetG1;
1671 return;
1672 case '#':
1673 vc->vc_state = EShash;
1674 return;
1675 case 'c':
1676 reset_terminal(vc, 1);
1677 return;
1678 case '>': /* Numeric keypad */
1679 clr_kbd(vc, kbdapplic);
1680 return;
1681 case '=': /* Appl. keypad */
1682 set_kbd(vc, kbdapplic);
1683 return;
1684 }
1685 return;
1686 case ESnonstd:
1687 if (c=='P') { /* palette escape sequence */
1688 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1689 vc->vc_par[vc->vc_npar] = 0;
1690 vc->vc_npar = 0;
1691 vc->vc_state = ESpalette;
1692 return;
1693 } else if (c=='R') { /* reset palette */
1694 reset_palette(vc);
1695 vc->vc_state = ESnormal;
1696 } else
1697 vc->vc_state = ESnormal;
1698 return;
1699 case ESpalette:
1700 if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
1701 vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0');
1702 if (vc->vc_npar == 7) {
1703 int i = vc->vc_par[0] * 3, j = 1;
1704 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1705 vc->vc_palette[i++] += vc->vc_par[j++];
1706 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1707 vc->vc_palette[i++] += vc->vc_par[j++];
1708 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1709 vc->vc_palette[i] += vc->vc_par[j];
1710 set_palette(vc);
1711 vc->vc_state = ESnormal;
1712 }
1713 } else
1714 vc->vc_state = ESnormal;
1715 return;
1716 case ESsquare:
1717 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1718 vc->vc_par[vc->vc_npar] = 0;
1719 vc->vc_npar = 0;
1720 vc->vc_state = ESgetpars;
1721 if (c == '[') { /* Function key */
1722 vc->vc_state=ESfunckey;
1723 return;
1724 }
1725 vc->vc_ques = (c == '?');
1726 if (vc->vc_ques)
1727 return;
1728 case ESgetpars:
1729 if (c == ';' && vc->vc_npar < NPAR - 1) {
1730 vc->vc_npar++;
1731 return;
1732 } else if (c>='0' && c<='9') {
1733 vc->vc_par[vc->vc_npar] *= 10;
1734 vc->vc_par[vc->vc_npar] += c - '0';
1735 return;
1736 } else
1737 vc->vc_state = ESgotpars;
1738 case ESgotpars:
1739 vc->vc_state = ESnormal;
1740 switch(c) {
1741 case 'h':
1742 set_mode(vc, 1);
1743 return;
1744 case 'l':
1745 set_mode(vc, 0);
1746 return;
1747 case 'c':
1748 if (vc->vc_ques) {
1749 if (vc->vc_par[0])
1750 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1751 else
1752 vc->vc_cursor_type = CUR_DEFAULT;
1753 return;
1754 }
1755 break;
1756 case 'm':
1757 if (vc->vc_ques) {
1758 clear_selection();
1759 if (vc->vc_par[0])
1760 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1761 else
1762 vc->vc_complement_mask = vc->vc_s_complement_mask;
1763 return;
1764 }
1765 break;
1766 case 'n':
1767 if (!vc->vc_ques) {
1768 if (vc->vc_par[0] == 5)
1769 status_report(tty);
1770 else if (vc->vc_par[0] == 6)
1771 cursor_report(vc, tty);
1772 }
1773 return;
1774 }
1775 if (vc->vc_ques) {
1776 vc->vc_ques = 0;
1777 return;
1778 }
1779 switch(c) {
1780 case 'G': case '`':
1781 if (vc->vc_par[0])
1782 vc->vc_par[0]--;
1783 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1784 return;
1785 case 'A':
1786 if (!vc->vc_par[0])
1787 vc->vc_par[0]++;
1788 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1789 return;
1790 case 'B': case 'e':
1791 if (!vc->vc_par[0])
1792 vc->vc_par[0]++;
1793 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1794 return;
1795 case 'C': case 'a':
1796 if (!vc->vc_par[0])
1797 vc->vc_par[0]++;
1798 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1799 return;
1800 case 'D':
1801 if (!vc->vc_par[0])
1802 vc->vc_par[0]++;
1803 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1804 return;
1805 case 'E':
1806 if (!vc->vc_par[0])
1807 vc->vc_par[0]++;
1808 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1809 return;
1810 case 'F':
1811 if (!vc->vc_par[0])
1812 vc->vc_par[0]++;
1813 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1814 return;
1815 case 'd':
1816 if (vc->vc_par[0])
1817 vc->vc_par[0]--;
1818 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1819 return;
1820 case 'H': case 'f':
1821 if (vc->vc_par[0])
1822 vc->vc_par[0]--;
1823 if (vc->vc_par[1])
1824 vc->vc_par[1]--;
1825 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1826 return;
1827 case 'J':
1828 csi_J(vc, vc->vc_par[0]);
1829 return;
1830 case 'K':
1831 csi_K(vc, vc->vc_par[0]);
1832 return;
1833 case 'L':
1834 csi_L(vc, vc->vc_par[0]);
1835 return;
1836 case 'M':
1837 csi_M(vc, vc->vc_par[0]);
1838 return;
1839 case 'P':
1840 csi_P(vc, vc->vc_par[0]);
1841 return;
1842 case 'c':
1843 if (!vc->vc_par[0])
1844 respond_ID(tty);
1845 return;
1846 case 'g':
1847 if (!vc->vc_par[0])
1848 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1849 else if (vc->vc_par[0] == 3) {
1850 vc->vc_tab_stop[0] =
1851 vc->vc_tab_stop[1] =
1852 vc->vc_tab_stop[2] =
1853 vc->vc_tab_stop[3] =
1854 vc->vc_tab_stop[4] = 0;
1855 }
1856 return;
1857 case 'm':
1858 csi_m(vc);
1859 return;
1860 case 'q': /* DECLL - but only 3 leds */
1861 /* map 0,1,2,3 to 0,1,2,4 */
1862 if (vc->vc_par[0] < 4)
1863 setledstate(kbd_table + vc->vc_num,
1864 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1865 return;
1866 case 'r':
1867 if (!vc->vc_par[0])
1868 vc->vc_par[0]++;
1869 if (!vc->vc_par[1])
1870 vc->vc_par[1] = vc->vc_rows;
1871 /* Minimum allowed region is 2 lines */
1872 if (vc->vc_par[0] < vc->vc_par[1] &&
1873 vc->vc_par[1] <= vc->vc_rows) {
1874 vc->vc_top = vc->vc_par[0] - 1;
1875 vc->vc_bottom = vc->vc_par[1];
1876 gotoxay(vc, 0, 0);
1877 }
1878 return;
1879 case 's':
1880 save_cur(vc);
1881 return;
1882 case 'u':
1883 restore_cur(vc);
1884 return;
1885 case 'X':
1886 csi_X(vc, vc->vc_par[0]);
1887 return;
1888 case '@':
1889 csi_at(vc, vc->vc_par[0]);
1890 return;
1891 case ']': /* setterm functions */
1892 setterm_command(vc);
1893 return;
1894 }
1895 return;
1896 case ESpercent:
1897 vc->vc_state = ESnormal;
1898 switch (c) {
1899 case '@': /* defined in ISO 2022 */
1900 vc->vc_utf = 0;
1901 return;
1902 case 'G': /* prelim official escape code */
1903 case '8': /* retained for compatibility */
1904 vc->vc_utf = 1;
1905 return;
1906 }
1907 return;
1908 case ESfunckey:
1909 vc->vc_state = ESnormal;
1910 return;
1911 case EShash:
1912 vc->vc_state = ESnormal;
1913 if (c == '8') {
1914 /* DEC screen alignment test. kludge :-) */
1915 vc->vc_video_erase_char =
1916 (vc->vc_video_erase_char & 0xff00) | 'E';
1917 csi_J(vc, 2);
1918 vc->vc_video_erase_char =
1919 (vc->vc_video_erase_char & 0xff00) | ' ';
1920 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
1921 }
1922 return;
1923 case ESsetG0:
1924 if (c == '0')
1925 vc->vc_G0_charset = GRAF_MAP;
1926 else if (c == 'B')
1927 vc->vc_G0_charset = LAT1_MAP;
1928 else if (c == 'U')
1929 vc->vc_G0_charset = IBMPC_MAP;
1930 else if (c == 'K')
1931 vc->vc_G0_charset = USER_MAP;
1932 if (vc->vc_charset == 0)
1933 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1934 vc->vc_state = ESnormal;
1935 return;
1936 case ESsetG1:
1937 if (c == '0')
1938 vc->vc_G1_charset = GRAF_MAP;
1939 else if (c == 'B')
1940 vc->vc_G1_charset = LAT1_MAP;
1941 else if (c == 'U')
1942 vc->vc_G1_charset = IBMPC_MAP;
1943 else if (c == 'K')
1944 vc->vc_G1_charset = USER_MAP;
1945 if (vc->vc_charset == 1)
1946 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1947 vc->vc_state = ESnormal;
1948 return;
1949 default:
1950 vc->vc_state = ESnormal;
1951 }
1952}
1953
1954/* This is a temporary buffer used to prepare a tty console write
1955 * so that we can easily avoid touching user space while holding the
1956 * console spinlock. It is allocated in con_init and is shared by
1957 * this code and the vc_screen read/write tty calls.
1958 *
1959 * We have to allocate this statically in the kernel data section
1960 * since console_init (and thus con_init) are called before any
1961 * kernel memory allocation is available.
1962 */
1963char con_buf[CON_BUF_SIZE];
Matthias Kaehlckec831c332007-05-08 00:39:49 -07001964DEFINE_MUTEX(con_buf_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07001966/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07001967 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07001968 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
1969 */
1970struct interval {
1971 uint32_t first;
1972 uint32_t last;
1973};
1974
1975static int bisearch(uint32_t ucs, const struct interval *table, int max)
1976{
1977 int min = 0;
1978 int mid;
1979
1980 if (ucs < table[0].first || ucs > table[max].last)
1981 return 0;
1982 while (max >= min) {
1983 mid = (min + max) / 2;
1984 if (ucs > table[mid].last)
1985 min = mid + 1;
1986 else if (ucs < table[mid].first)
1987 max = mid - 1;
1988 else
1989 return 1;
1990 }
1991 return 0;
1992}
1993
1994static int is_double_width(uint32_t ucs)
1995{
1996 static const struct interval double_width[] = {
1997 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
1998 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07001999 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2000 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002001 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002002 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002003}
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005/* acquires console_sem */
2006static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2007{
2008#ifdef VT_BUF_VRAM_ONLY
2009#define FLUSH do { } while(0);
2010#else
2011#define FLUSH if (draw_x >= 0) { \
2012 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2013 draw_x = -1; \
2014 }
2015#endif
2016
2017 int c, tc, ok, n = 0, draw_x = -1;
2018 unsigned int currcons;
2019 unsigned long draw_from = 0, draw_to = 0;
2020 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002021 unsigned char vc_attr;
2022 uint8_t rescan;
2023 uint8_t inverse;
2024 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 u16 himask, charmask;
2026 const unsigned char *orig_buf = NULL;
2027 int orig_count;
2028
2029 if (in_interrupt())
2030 return count;
2031
2032 might_sleep();
2033
2034 acquire_console_sem();
2035 vc = tty->driver_data;
2036 if (vc == NULL) {
2037 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2038 release_console_sem();
2039 return 0;
2040 }
2041
2042 currcons = vc->vc_num;
2043 if (!vc_cons_allocated(currcons)) {
2044 /* could this happen? */
2045 static int error = 0;
2046 if (!error) {
2047 error = 1;
2048 printk("con_write: tty %d not allocated\n", currcons+1);
2049 }
2050 release_console_sem();
2051 return 0;
2052 }
2053 release_console_sem();
2054
2055 orig_buf = buf;
2056 orig_count = count;
2057
2058 /* At this point 'buf' is guaranteed to be a kernel buffer
2059 * and therefore no access to userspace (and therefore sleeping)
Matthias Kaehlckec831c332007-05-08 00:39:49 -07002060 * will be needed. The con_buf_mtx serializes all tty based
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 * console rendering and vcs write/read operations. We hold
2062 * the console spinlock during the entire write.
2063 */
2064
2065 acquire_console_sem();
2066
2067 vc = tty->driver_data;
2068 if (vc == NULL) {
2069 printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
2070 release_console_sem();
2071 goto out;
2072 }
2073
2074 himask = vc->vc_hi_font_mask;
2075 charmask = himask ? 0x1ff : 0xff;
2076
2077 /* undraw cursor first */
2078 if (IS_FG(vc))
2079 hide_cursor(vc);
2080
2081 while (!tty->stopped && count) {
2082 int orig = *buf;
2083 c = orig;
2084 buf++;
2085 n++;
2086 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002087 rescan = 0;
2088 inverse = 0;
2089 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 /* Do no translation at all in control states */
2092 if (vc->vc_state != ESnormal) {
2093 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002094 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002095 /* Combine UTF-8 into Unicode in vc_utf_char.
2096 * vc_utf_count is the number of continuation bytes still
2097 * expected to arrive.
2098 * vc_npar is the number of continuation bytes arrived so
2099 * far
2100 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002101rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002102 if ((c & 0xc0) == 0x80) {
2103 /* Continuation byte received */
2104 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002105 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002106 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2107 vc->vc_npar++;
2108 if (--vc->vc_utf_count) {
2109 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002111 }
2112 /* Got a whole character */
2113 c = vc->vc_utf_char;
2114 /* Reject overlong sequences */
2115 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2116 c > utf8_length_changes[vc->vc_npar])
2117 c = 0xfffd;
2118 } else {
2119 /* Unexpected continuation byte */
2120 vc->vc_utf_count = 0;
2121 c = 0xfffd;
2122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002124 /* Single ASCII byte or first byte of a sequence received */
2125 if (vc->vc_utf_count) {
2126 /* Continuation byte expected */
2127 rescan = 1;
2128 vc->vc_utf_count = 0;
2129 c = 0xfffd;
2130 } else if (c > 0x7f) {
2131 /* First byte of a multibyte sequence received */
2132 vc->vc_npar = 0;
2133 if ((c & 0xe0) == 0xc0) {
2134 vc->vc_utf_count = 1;
2135 vc->vc_utf_char = (c & 0x1f);
2136 } else if ((c & 0xf0) == 0xe0) {
2137 vc->vc_utf_count = 2;
2138 vc->vc_utf_char = (c & 0x0f);
2139 } else if ((c & 0xf8) == 0xf0) {
2140 vc->vc_utf_count = 3;
2141 vc->vc_utf_char = (c & 0x07);
2142 } else if ((c & 0xfc) == 0xf8) {
2143 vc->vc_utf_count = 4;
2144 vc->vc_utf_char = (c & 0x03);
2145 } else if ((c & 0xfe) == 0xfc) {
2146 vc->vc_utf_count = 5;
2147 vc->vc_utf_char = (c & 0x01);
2148 } else {
2149 /* 254 and 255 are invalid */
2150 c = 0xfffd;
2151 }
2152 if (vc->vc_utf_count) {
2153 /* Still need some bytes */
2154 continue;
2155 }
2156 }
2157 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002159 /* End of UTF-8 decoding. */
2160 /* c is the received character, or U+FFFD for invalid sequences. */
2161 /* Replace invalid Unicode code points with U+FFFD too */
2162 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2163 c = 0xfffd;
2164 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002165 } else { /* no utf or alternate charset mode */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002166 tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168
2169 /* If the original code was a control character we
2170 * only allow a glyph to be displayed if the code is
2171 * not normally used (such as for cursor movement) or
2172 * if the disp_ctrl mode has been explicitly enabled.
2173 * Certain characters (as given by the CTRL_ALWAYS
2174 * bitmap) are always displayed as control characters,
2175 * as the console would be pretty useless without
2176 * them; to display an arbitrary font position use the
2177 * direct-to-font zone in UTF-8 mode.
2178 */
2179 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002180 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2181 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 && (c != 127 || vc->vc_disp_ctrl)
2183 && (c != 128+27);
2184
2185 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002186 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2187 if (is_double_width(c))
2188 width = 2;
2189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 /* Now try to find out how to display it */
2191 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002192 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002193 if (tc == -1 || tc == -2) {
2194 continue; /* nothing to display */
2195 }
2196 /* Glyph not found */
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002197 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002198 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002199 This would make absolutely no sense with Unicode in mind,
2200 but do this for ASCII characters since a font may lack
2201 Unicode mapping info and we don't want to end up with
2202 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002203 tc = c;
2204 } else {
2205 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2206 tc = conv_uni_to_pc(vc, 0xfffd);
2207 if (tc < 0) {
2208 inverse = 1;
2209 tc = conv_uni_to_pc(vc, '?');
2210 if (tc < 0) tc = '?';
2211 }
2212 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002215 if (!inverse) {
2216 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002218 /* invert vc_attr */
2219 if (!vc->vc_can_do_color) {
2220 vc_attr = (vc->vc_attr) ^ 0x08;
2221 } else if (vc->vc_hi_font_mask == 0x100) {
2222 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2223 } else {
2224 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002225 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002226 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002227 }
2228
2229 while (1) {
2230 if (vc->vc_need_wrap || vc->vc_decim)
2231 FLUSH
2232 if (vc->vc_need_wrap) {
2233 cr(vc);
2234 lf(vc);
2235 }
2236 if (vc->vc_decim)
2237 insert_char(vc, 1);
2238 scr_writew(himask ?
2239 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2240 (vc_attr << 8) + tc,
2241 (u16 *) vc->vc_pos);
2242 if (DO_UPDATE(vc) && draw_x < 0) {
2243 draw_x = vc->vc_x;
2244 draw_from = vc->vc_pos;
2245 }
2246 if (vc->vc_x == vc->vc_cols - 1) {
2247 vc->vc_need_wrap = vc->vc_decawm;
2248 draw_to = vc->vc_pos + 2;
2249 } else {
2250 vc->vc_x++;
2251 draw_to = (vc->vc_pos += 2);
2252 }
2253
2254 if (!--width) break;
2255
2256 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2257 if (tc < 0) tc = ' ';
2258 }
2259
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002260 if (inverse) {
2261 FLUSH
2262 }
2263
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002264 if (rescan) {
2265 rescan = 0;
2266 inverse = 0;
2267 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002268 c = orig;
2269 goto rescan_last_byte;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 continue;
2272 }
2273 FLUSH
2274 do_con_trol(tty, vc, orig);
2275 }
2276 FLUSH
2277 console_conditional_schedule();
2278 release_console_sem();
2279
2280out:
2281 return n;
2282#undef FLUSH
2283}
2284
2285/*
2286 * This is the console switching callback.
2287 *
2288 * Doing console switching in a process context allows
2289 * us to do the switches asynchronously (needed when we want
2290 * to switch due to a keyboard interrupt). Synchronization
2291 * with other console code and prevention of re-entrancy is
2292 * ensured with console_sem.
2293 */
David Howells65f27f32006-11-22 14:55:48 +00002294static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295{
2296 acquire_console_sem();
2297
2298 if (want_console >= 0) {
2299 if (want_console != fg_console &&
2300 vc_cons_allocated(want_console)) {
2301 hide_cursor(vc_cons[fg_console].d);
2302 change_console(vc_cons[want_console].d);
2303 /* we only changed when the console had already
2304 been allocated - a new console is not created
2305 in an interrupt routine */
2306 }
2307 want_console = -1;
2308 }
2309 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2310 do_poke_blanked_console = 0;
2311 poke_blanked_console();
2312 }
2313 if (scrollback_delta) {
2314 struct vc_data *vc = vc_cons[fg_console].d;
2315 clear_selection();
2316 if (vc->vc_mode == KD_TEXT)
2317 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2318 scrollback_delta = 0;
2319 }
2320 if (blank_timer_expired) {
2321 do_blank_screen(0);
2322 blank_timer_expired = 0;
2323 }
2324
2325 release_console_sem();
2326}
2327
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002328int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002330 struct vc_data *vc = vc_cons[fg_console].d;
2331
2332 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2333 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2334
2335 /*
2336 * Console switch will fail in console_callback() or
2337 * change_console() so there is no point scheduling
2338 * the callback
2339 *
2340 * Existing set_console() users don't check the return
2341 * value so this shouldn't break anything
2342 */
2343 return -EINVAL;
2344 }
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 want_console = nr;
2347 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002348
2349 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
2352struct tty_driver *console_driver;
2353
2354#ifdef CONFIG_VT_CONSOLE
2355
2356/*
2357 * Console on virtual terminal
2358 *
2359 * The console must be locked when we get here.
2360 */
2361
2362static void vt_console_print(struct console *co, const char *b, unsigned count)
2363{
2364 struct vc_data *vc = vc_cons[fg_console].d;
2365 unsigned char c;
2366 static unsigned long printing;
2367 const ushort *start;
2368 ushort cnt = 0;
2369 ushort myx;
2370
2371 /* console busy or not yet initialized */
2372 if (!printable || test_and_set_bit(0, &printing))
2373 return;
2374
2375 if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
2376 vc = vc_cons[kmsg_redirect - 1].d;
2377
2378 /* read `x' only after setting currcons properly (otherwise
2379 the `x' macro will read the x of the foreground console). */
2380 myx = vc->vc_x;
2381
2382 if (!vc_cons_allocated(fg_console)) {
2383 /* impossible */
2384 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2385 goto quit;
2386 }
2387
2388 if (vc->vc_mode != KD_TEXT)
2389 goto quit;
2390
2391 /* undraw cursor first */
2392 if (IS_FG(vc))
2393 hide_cursor(vc);
2394
2395 start = (ushort *)vc->vc_pos;
2396
2397 /* Contrived structure to try to emulate original need_wrap behaviour
2398 * Problems caused when we have need_wrap set on '\n' character */
2399 while (count--) {
2400 c = *b++;
2401 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2402 if (cnt > 0) {
2403 if (CON_IS_VISIBLE(vc))
2404 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2405 vc->vc_x += cnt;
2406 if (vc->vc_need_wrap)
2407 vc->vc_x--;
2408 cnt = 0;
2409 }
2410 if (c == 8) { /* backspace */
2411 bs(vc);
2412 start = (ushort *)vc->vc_pos;
2413 myx = vc->vc_x;
2414 continue;
2415 }
2416 if (c != 13)
2417 lf(vc);
2418 cr(vc);
2419 start = (ushort *)vc->vc_pos;
2420 myx = vc->vc_x;
2421 if (c == 10 || c == 13)
2422 continue;
2423 }
2424 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2425 cnt++;
2426 if (myx == vc->vc_cols - 1) {
2427 vc->vc_need_wrap = 1;
2428 continue;
2429 }
2430 vc->vc_pos += 2;
2431 myx++;
2432 }
2433 if (cnt > 0) {
2434 if (CON_IS_VISIBLE(vc))
2435 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2436 vc->vc_x += cnt;
2437 if (vc->vc_x == vc->vc_cols) {
2438 vc->vc_x--;
2439 vc->vc_need_wrap = 1;
2440 }
2441 }
2442 set_cursor(vc);
2443
2444quit:
2445 clear_bit(0, &printing);
2446}
2447
2448static struct tty_driver *vt_console_device(struct console *c, int *index)
2449{
2450 *index = c->index ? c->index-1 : fg_console;
2451 return console_driver;
2452}
2453
2454static struct console vt_console_driver = {
2455 .name = "tty",
2456 .write = vt_console_print,
2457 .device = vt_console_device,
2458 .unblank = unblank_screen,
2459 .flags = CON_PRINTBUFFER,
2460 .index = -1,
2461};
2462#endif
2463
2464/*
2465 * Handling of Linux-specific VC ioctls
2466 */
2467
2468/*
2469 * Generally a bit racy with respect to console_sem().
2470 *
2471 * There are some functions which don't need it.
2472 *
2473 * There are some functions which can sleep for arbitrary periods
2474 * (paste_selection) but we don't need the lock there anyway.
2475 *
2476 * set_selection has locking, and definitely needs it
2477 */
2478
2479int tioclinux(struct tty_struct *tty, unsigned long arg)
2480{
2481 char type, data;
2482 char __user *p = (char __user *)arg;
2483 int lines;
2484 int ret;
2485
2486 if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
2487 return -EINVAL;
2488 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2489 return -EPERM;
2490 if (get_user(type, p))
2491 return -EFAULT;
2492 ret = 0;
2493 switch (type)
2494 {
2495 case TIOCL_SETSEL:
2496 acquire_console_sem();
2497 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2498 release_console_sem();
2499 break;
2500 case TIOCL_PASTESEL:
2501 ret = paste_selection(tty);
2502 break;
2503 case TIOCL_UNBLANKSCREEN:
Stephane Doyon2d237c62005-09-06 15:17:31 -07002504 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 unblank_screen();
Stephane Doyon2d237c62005-09-06 15:17:31 -07002506 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 break;
2508 case TIOCL_SELLOADLUT:
2509 ret = sel_loadlut(p);
2510 break;
2511 case TIOCL_GETSHIFTSTATE:
2512
2513 /*
2514 * Make it possible to react to Shift+Mousebutton.
2515 * Note that 'shift_state' is an undocumented
2516 * kernel-internal variable; programs not closely
2517 * related to the kernel should not use this.
2518 */
2519 data = shift_state;
2520 ret = __put_user(data, p);
2521 break;
2522 case TIOCL_GETMOUSEREPORTING:
2523 data = mouse_reporting();
2524 ret = __put_user(data, p);
2525 break;
2526 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002527 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002529 case TIOCL_GETKMSGREDIRECT:
2530 data = kmsg_redirect;
2531 ret = __put_user(data, p);
2532 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533 case TIOCL_SETKMSGREDIRECT:
2534 if (!capable(CAP_SYS_ADMIN)) {
2535 ret = -EPERM;
2536 } else {
2537 if (get_user(data, p+1))
2538 ret = -EFAULT;
2539 else
2540 kmsg_redirect = data;
2541 }
2542 break;
2543 case TIOCL_GETFGCONSOLE:
2544 ret = fg_console;
2545 break;
2546 case TIOCL_SCROLLCONSOLE:
2547 if (get_user(lines, (s32 __user *)(p+4))) {
2548 ret = -EFAULT;
2549 } else {
2550 scrollfront(vc_cons[fg_console].d, lines);
2551 ret = 0;
2552 }
2553 break;
2554 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Stephane Doyon2d237c62005-09-06 15:17:31 -07002555 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 ignore_poke = 1;
2557 do_blank_screen(0);
Stephane Doyon2d237c62005-09-06 15:17:31 -07002558 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 break;
2560 case TIOCL_BLANKEDSCREEN:
2561 ret = console_blanked;
2562 break;
2563 default:
2564 ret = -EINVAL;
2565 break;
2566 }
2567 return ret;
2568}
2569
2570/*
2571 * /dev/ttyN handling
2572 */
2573
2574static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2575{
2576 int retval;
2577
2578 retval = do_con_write(tty, buf, count);
2579 con_flush_chars(tty);
2580
2581 return retval;
2582}
2583
2584static void con_put_char(struct tty_struct *tty, unsigned char ch)
2585{
2586 if (in_interrupt())
2587 return; /* n_r3964 calls put_char() from interrupt context */
2588 do_con_write(tty, &ch, 1);
2589}
2590
2591static int con_write_room(struct tty_struct *tty)
2592{
2593 if (tty->stopped)
2594 return 0;
2595 return 4096; /* No limit, really; we're not buffering */
2596}
2597
2598static int con_chars_in_buffer(struct tty_struct *tty)
2599{
2600 return 0; /* we're not buffering */
2601}
2602
2603/*
2604 * con_throttle and con_unthrottle are only used for
2605 * paste_selection(), which has to stuff in a large number of
2606 * characters...
2607 */
2608static void con_throttle(struct tty_struct *tty)
2609{
2610}
2611
2612static void con_unthrottle(struct tty_struct *tty)
2613{
2614 struct vc_data *vc = tty->driver_data;
2615
2616 wake_up_interruptible(&vc->paste_wait);
2617}
2618
2619/*
2620 * Turn the Scroll-Lock LED on when the tty is stopped
2621 */
2622static void con_stop(struct tty_struct *tty)
2623{
2624 int console_num;
2625 if (!tty)
2626 return;
2627 console_num = tty->index;
2628 if (!vc_cons_allocated(console_num))
2629 return;
2630 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2631 set_leds();
2632}
2633
2634/*
2635 * Turn the Scroll-Lock LED off when the console is started
2636 */
2637static void con_start(struct tty_struct *tty)
2638{
2639 int console_num;
2640 if (!tty)
2641 return;
2642 console_num = tty->index;
2643 if (!vc_cons_allocated(console_num))
2644 return;
2645 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2646 set_leds();
2647}
2648
2649static void con_flush_chars(struct tty_struct *tty)
2650{
2651 struct vc_data *vc;
2652
2653 if (in_interrupt()) /* from flush_to_ldisc */
2654 return;
2655
2656 /* if we race with con_close(), vt may be null */
2657 acquire_console_sem();
2658 vc = tty->driver_data;
2659 if (vc)
2660 set_cursor(vc);
2661 release_console_sem();
2662}
2663
2664/*
2665 * Allocate the console screen memory.
2666 */
2667static int con_open(struct tty_struct *tty, struct file *filp)
2668{
2669 unsigned int currcons = tty->index;
2670 int ret = 0;
2671
2672 acquire_console_sem();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002673 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 ret = vc_allocate(currcons);
2675 if (ret == 0) {
2676 struct vc_data *vc = vc_cons[currcons].d;
2677 tty->driver_data = vc;
2678 vc->vc_tty = tty;
2679
2680 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2681 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2682 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2683 }
2684 release_console_sem();
Alan Coxd09d7dd2006-09-29 01:59:47 -07002685 vcs_make_sysfs(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 return ret;
2687 }
2688 }
2689 release_console_sem();
2690 return ret;
2691}
2692
2693/*
Ingo Molnar70522e12006-03-23 03:00:31 -08002694 * We take tty_mutex in here to prevent another thread from coming in via init_dev
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 * and taking a ref against the tty while we're in the process of forgetting
2696 * about it and cleaning things up.
2697 *
Alan Coxd09d7dd2006-09-29 01:59:47 -07002698 * This is because vcs_remove_sysfs() can sleep and will drop the BKL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 */
2700static void con_close(struct tty_struct *tty, struct file *filp)
2701{
Ingo Molnar70522e12006-03-23 03:00:31 -08002702 mutex_lock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 acquire_console_sem();
2704 if (tty && tty->count == 1) {
2705 struct vc_data *vc = tty->driver_data;
2706
2707 if (vc)
2708 vc->vc_tty = NULL;
2709 tty->driver_data = NULL;
2710 release_console_sem();
Alan Coxd09d7dd2006-09-29 01:59:47 -07002711 vcs_remove_sysfs(tty);
Ingo Molnar70522e12006-03-23 03:00:31 -08002712 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 /*
Ingo Molnar70522e12006-03-23 03:00:31 -08002714 * tty_mutex is released, but we still hold BKL, so there is
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * still exclusion against init_dev()
2716 */
2717 return;
2718 }
2719 release_console_sem();
Ingo Molnar70522e12006-03-23 03:00:31 -08002720 mutex_unlock(&tty_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002723static int default_italic_color = 2; // green (ASCII)
2724static int default_underline_color = 3; // cyan (ASCII)
2725module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2726module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728static void vc_init(struct vc_data *vc, unsigned int rows,
2729 unsigned int cols, int do_clear)
2730{
2731 int j, k ;
2732
2733 vc->vc_cols = cols;
2734 vc->vc_rows = rows;
2735 vc->vc_size_row = cols << 1;
2736 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2737
2738 set_origin(vc);
2739 vc->vc_pos = vc->vc_origin;
2740 reset_vc(vc);
2741 for (j=k=0; j<16; j++) {
2742 vc->vc_palette[k++] = default_red[j] ;
2743 vc->vc_palette[k++] = default_grn[j] ;
2744 vc->vc_palette[k++] = default_blu[j] ;
2745 }
2746 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002747 vc->vc_ulcolor = default_underline_color;
2748 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 vc->vc_halfcolor = 0x08; /* grey */
2750 init_waitqueue_head(&vc->paste_wait);
2751 reset_terminal(vc, do_clear);
2752}
2753
2754/*
2755 * This routine initializes console interrupts, and does nothing
2756 * else. If you want the screen to clear, call tty_write with
2757 * the appropriate escape-sequence.
2758 */
2759
2760static int __init con_init(void)
2761{
2762 const char *display_desc = NULL;
2763 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002764 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 acquire_console_sem();
2767
2768 if (conswitchp)
2769 display_desc = conswitchp->con_startup();
2770 if (!display_desc) {
2771 fg_console = 0;
2772 release_console_sem();
2773 return 0;
2774 }
2775
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002776 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2777 struct con_driver *con_driver = &registered_con_driver[i];
2778
2779 if (con_driver->con == NULL) {
2780 con_driver->con = conswitchp;
2781 con_driver->desc = display_desc;
2782 con_driver->flag = CON_DRIVER_FLAG_INIT;
2783 con_driver->first = 0;
2784 con_driver->last = MAX_NR_CONSOLES - 1;
2785 break;
2786 }
2787 }
2788
2789 for (i = 0; i < MAX_NR_CONSOLES; i++)
2790 con_driver_map[i] = conswitchp;
2791
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (blankinterval) {
2793 blank_state = blank_normal_wait;
2794 mod_timer(&console_timer, jiffies + blankinterval);
2795 }
2796
2797 /*
2798 * kmalloc is not running yet - we use the bootmem allocator.
2799 */
2800 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
2801 vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002802 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 visual_init(vc, currcons, 1);
2804 vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
2805 vc->vc_kmalloced = 0;
2806 vc_init(vc, vc->vc_rows, vc->vc_cols,
2807 currcons || !vc->vc_sw->con_save_screen);
2808 }
2809 currcons = fg_console = 0;
2810 master_display_fg = vc = vc_cons[currcons].d;
2811 set_origin(vc);
2812 save_screen(vc);
2813 gotoxy(vc, vc->vc_x, vc->vc_y);
2814 csi_J(vc, 0);
2815 update_screen(vc);
2816 printk("Console: %s %s %dx%d",
2817 vc->vc_can_do_color ? "colour" : "mono",
2818 display_desc, vc->vc_cols, vc->vc_rows);
2819 printable = 1;
2820 printk("\n");
2821
2822 release_console_sem();
2823
2824#ifdef CONFIG_VT_CONSOLE
2825 register_console(&vt_console_driver);
2826#endif
2827 return 0;
2828}
2829console_initcall(con_init);
2830
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002831static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 .open = con_open,
2833 .close = con_close,
2834 .write = con_write,
2835 .write_room = con_write_room,
2836 .put_char = con_put_char,
2837 .flush_chars = con_flush_chars,
2838 .chars_in_buffer = con_chars_in_buffer,
2839 .ioctl = vt_ioctl,
2840 .stop = con_stop,
2841 .start = con_start,
2842 .throttle = con_throttle,
2843 .unthrottle = con_unthrottle,
2844};
2845
2846int __init vty_init(void)
2847{
2848 vcs_init();
2849
2850 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2851 if (!console_driver)
2852 panic("Couldn't allocate console driver\n");
2853 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 console_driver->name = "tty";
2855 console_driver->name_base = 1;
2856 console_driver->major = TTY_MAJOR;
2857 console_driver->minor_start = 1;
2858 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2859 console_driver->init_termios = tty_std_termios;
2860 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2861 tty_set_operations(console_driver, &con_ops);
2862 if (tty_register_driver(console_driver))
2863 panic("Couldn't register console driver\n");
2864
2865 kbd_init();
2866 console_map_init();
2867#ifdef CONFIG_PROM_CONSOLE
2868 prom_con_init();
2869#endif
2870#ifdef CONFIG_MDA_CONSOLE
2871 mda_console_init();
2872#endif
2873 return 0;
2874}
2875
2876#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07002877#include <linux/device.h>
2878
2879static struct class *vtconsole_class;
2880
Jesse Barnesb7269dd2007-07-17 04:05:34 -07002881static int bind_con_driver(const struct consw *csw, int first, int last,
2882 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002884 struct module *owner = csw->owner;
2885 const char *desc = NULL;
2886 struct con_driver *con_driver;
2887 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 if (!try_module_get(owner))
2890 return -ENODEV;
2891
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002892 acquire_console_sem();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002893
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002894 /* check if driver is registered */
2895 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2896 con_driver = &registered_con_driver[i];
2897
2898 if (con_driver->con == csw) {
2899 desc = con_driver->desc;
2900 retval = 0;
2901 break;
2902 }
2903 }
2904
2905 if (retval)
2906 goto err;
2907
2908 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
2909 csw->con_startup();
2910 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002912
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 if (deflt) {
2914 if (conswitchp)
2915 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002916
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 __module_get(owner);
2918 conswitchp = csw;
2919 }
2920
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002921 first = max(first, con_driver->first);
2922 last = min(last, con_driver->last);
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 for (i = first; i <= last; i++) {
2925 int old_was_color;
2926 struct vc_data *vc = vc_cons[i].d;
2927
2928 if (con_driver_map[i])
2929 module_put(con_driver_map[i]->owner);
2930 __module_get(owner);
2931 con_driver_map[i] = csw;
2932
2933 if (!vc || !vc->vc_sw)
2934 continue;
2935
2936 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002937
David Hollister4ee1acc2006-06-26 00:26:41 -07002938 if (CON_IS_VISIBLE(vc)) {
2939 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07002941 }
2942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 old_was_color = vc->vc_can_do_color;
2944 vc->vc_sw->con_deinit(vc);
2945 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07002947 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 update_attr(vc);
2949
2950 /* If the console changed between mono <-> color, then
2951 * the attributes in the screenbuf will be wrong. The
2952 * following resets all attributes to something sane.
2953 */
2954 if (old_was_color != vc->vc_can_do_color)
2955 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
David Hollister4ee1acc2006-06-26 00:26:41 -07002957
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 printk("Console: switching ");
2959 if (!deflt)
2960 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07002961 if (j >= 0) {
2962 struct vc_data *vc = vc_cons[j].d;
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07002965 vc->vc_can_do_color ? "colour" : "mono",
2966 desc, vc->vc_cols, vc->vc_rows);
2967
2968 if (k >= 0) {
2969 vc = vc_cons[k].d;
2970 update_screen(vc);
2971 }
2972 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 printk("to %s\n", desc);
2974
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002975 retval = 0;
2976err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002979 return retval;
2980};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07002982#ifdef CONFIG_VT_HW_CONSOLE_BINDING
2983static int con_is_graphics(const struct consw *csw, int first, int last)
2984{
2985 int i, retval = 0;
2986
2987 for (i = first; i <= last; i++) {
2988 struct vc_data *vc = vc_cons[i].d;
2989
2990 if (vc && vc->vc_mode == KD_GRAPHICS) {
2991 retval = 1;
2992 break;
2993 }
2994 }
2995
2996 return retval;
2997}
2998
Jesse Barnesb7269dd2007-07-17 04:05:34 -07002999/**
3000 * unbind_con_driver - unbind a console driver
3001 * @csw: pointer to console driver to unregister
3002 * @first: first in range of consoles that @csw should be unbound from
3003 * @last: last in range of consoles that @csw should be unbound from
3004 * @deflt: should next bound console driver be default after @csw is unbound?
3005 *
3006 * To unbind a driver from all possible consoles, pass 0 as @first and
3007 * %MAX_NR_CONSOLES as @last.
3008 *
3009 * @deflt controls whether the console that ends up replacing @csw should be
3010 * the default console.
3011 *
3012 * RETURNS:
3013 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3014 * or 0 on success.
3015 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003016int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003018 struct module *owner = csw->owner;
3019 const struct consw *defcsw = NULL;
3020 struct con_driver *con_driver = NULL, *con_back = NULL;
3021 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003023 if (!try_module_get(owner))
3024 return -ENODEV;
3025
3026 acquire_console_sem();
3027
3028 /* check if driver is registered and if it is unbindable */
3029 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3030 con_driver = &registered_con_driver[i];
3031
3032 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003033 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003034 retval = 0;
3035 break;
3036 }
3037 }
3038
3039 if (retval) {
3040 release_console_sem();
3041 goto err;
3042 }
3043
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003044 retval = -ENODEV;
3045
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003046 /* check if backup driver exists */
3047 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3048 con_back = &registered_con_driver[i];
3049
3050 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003051 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003052 defcsw = con_back->con;
3053 retval = 0;
3054 break;
3055 }
3056 }
3057
3058 if (retval) {
3059 release_console_sem();
3060 goto err;
3061 }
3062
3063 if (!con_is_bound(csw)) {
3064 release_console_sem();
3065 goto err;
3066 }
3067
3068 first = max(first, con_driver->first);
3069 last = min(last, con_driver->last);
3070
3071 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 if (con_driver_map[i] == csw) {
3073 module_put(csw->owner);
3074 con_driver_map[i] = NULL;
3075 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003076 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003077
3078 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003079 const struct consw *defconsw = conswitchp;
3080
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003081 defcsw->con_startup();
3082 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003083 /*
3084 * vgacon may change the default driver to point
3085 * to dummycon, we restore it here...
3086 */
3087 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003088 }
3089
3090 if (!con_is_bound(csw))
3091 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3092
3093 release_console_sem();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003094 /* ignore return value, binding should not fail */
3095 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003096err:
3097 module_put(owner);
3098 return retval;
3099
3100}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003101EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003102
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003103static int vt_bind(struct con_driver *con)
3104{
3105 const struct consw *defcsw = NULL, *csw = NULL;
3106 int i, more = 1, first = -1, last = -1, deflt = 0;
3107
3108 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3109 con_is_graphics(con->con, con->first, con->last))
3110 goto err;
3111
3112 csw = con->con;
3113
3114 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3115 struct con_driver *con = &registered_con_driver[i];
3116
3117 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3118 defcsw = con->con;
3119 break;
3120 }
3121 }
3122
3123 if (!defcsw)
3124 goto err;
3125
3126 while (more) {
3127 more = 0;
3128
3129 for (i = con->first; i <= con->last; i++) {
3130 if (con_driver_map[i] == defcsw) {
3131 if (first == -1)
3132 first = i;
3133 last = i;
3134 more = 1;
3135 } else if (first != -1)
3136 break;
3137 }
3138
3139 if (first == 0 && last == MAX_NR_CONSOLES -1)
3140 deflt = 1;
3141
3142 if (first != -1)
3143 bind_con_driver(csw, first, last, deflt);
3144
3145 first = -1;
3146 last = -1;
3147 deflt = 0;
3148 }
3149
3150err:
3151 return 0;
3152}
3153
3154static int vt_unbind(struct con_driver *con)
3155{
3156 const struct consw *csw = NULL;
3157 int i, more = 1, first = -1, last = -1, deflt = 0;
3158
3159 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3160 con_is_graphics(con->con, con->first, con->last))
3161 goto err;
3162
3163 csw = con->con;
3164
3165 while (more) {
3166 more = 0;
3167
3168 for (i = con->first; i <= con->last; i++) {
3169 if (con_driver_map[i] == csw) {
3170 if (first == -1)
3171 first = i;
3172 last = i;
3173 more = 1;
3174 } else if (first != -1)
3175 break;
3176 }
3177
3178 if (first == 0 && last == MAX_NR_CONSOLES -1)
3179 deflt = 1;
3180
3181 if (first != -1)
3182 unbind_con_driver(csw, first, last, deflt);
3183
3184 first = -1;
3185 last = -1;
3186 deflt = 0;
3187 }
3188
3189err:
3190 return 0;
3191}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003192#else
3193static inline int vt_bind(struct con_driver *con)
3194{
3195 return 0;
3196}
3197static inline int vt_unbind(struct con_driver *con)
3198{
3199 return 0;
3200}
3201#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003202
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003203static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003204 const char *buf, size_t count)
3205{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003206 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003207 int bind = simple_strtoul(buf, NULL, 0);
3208
3209 if (bind)
3210 vt_bind(con);
3211 else
3212 vt_unbind(con);
3213
3214 return count;
3215}
3216
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003217static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3218 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003219{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003220 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003221 int bind = con_is_bound(con->con);
3222
3223 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3224}
3225
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003226static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3227 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003228{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003229 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003230
3231 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3232 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3233 con->desc);
3234
3235}
3236
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003237static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003238 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3239 __ATTR(name, S_IRUGO, show_name, NULL),
3240};
3241
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003242static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003243{
3244 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003245 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003246
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003247 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003248 dev_set_drvdata(con->dev, con);
3249 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3250 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003251 if (error)
3252 break;
3253 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003254
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003255 if (error) {
3256 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003257 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003258 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3259 }
3260
3261 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003262}
3263
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003264static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003265{
3266 int i;
3267
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003268 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003269 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3270 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003271 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3272 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003273}
3274
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003275/**
3276 * con_is_bound - checks if driver is bound to the console
3277 * @csw: console driver
3278 *
3279 * RETURNS: zero if unbound, nonzero if bound
3280 *
3281 * Drivers can call this and if zero, they should release
3282 * all resources allocated on con_startup()
3283 */
3284int con_is_bound(const struct consw *csw)
3285{
3286 int i, bound = 0;
3287
3288 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3289 if (con_driver_map[i] == csw) {
3290 bound = 1;
3291 break;
3292 }
3293 }
3294
3295 return bound;
3296}
3297EXPORT_SYMBOL(con_is_bound);
3298
3299/**
3300 * register_con_driver - register console driver to console layer
3301 * @csw: console driver
3302 * @first: the first console to take over, minimum value is 0
3303 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3304 *
3305 * DESCRIPTION: This function registers a console driver which can later
3306 * bind to a range of consoles specified by @first and @last. It will
3307 * also initialize the console driver by calling con_startup().
3308 */
3309int register_con_driver(const struct consw *csw, int first, int last)
3310{
3311 struct module *owner = csw->owner;
3312 struct con_driver *con_driver;
3313 const char *desc;
3314 int i, retval = 0;
3315
3316 if (!try_module_get(owner))
3317 return -ENODEV;
3318
3319 acquire_console_sem();
3320
3321 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3322 con_driver = &registered_con_driver[i];
3323
3324 /* already registered */
3325 if (con_driver->con == csw)
3326 retval = -EINVAL;
3327 }
3328
3329 if (retval)
3330 goto err;
3331
3332 desc = csw->con_startup();
3333
3334 if (!desc)
3335 goto err;
3336
3337 retval = -EINVAL;
3338
3339 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3340 con_driver = &registered_con_driver[i];
3341
3342 if (con_driver->con == NULL) {
3343 con_driver->con = csw;
3344 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003345 con_driver->node = i;
3346 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003347 CON_DRIVER_FLAG_INIT;
3348 con_driver->first = first;
3349 con_driver->last = last;
3350 retval = 0;
3351 break;
3352 }
3353 }
3354
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003355 if (retval)
3356 goto err;
3357
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003358 con_driver->dev = device_create(vtconsole_class, NULL,
3359 MKDEV(0, con_driver->node),
3360 "vtcon%i", con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003361
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003362 if (IS_ERR(con_driver->dev)) {
3363 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003364 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003365 PTR_ERR(con_driver->dev));
3366 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003367 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003368 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003369 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003370
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003371err:
3372 release_console_sem();
3373 module_put(owner);
3374 return retval;
3375}
3376EXPORT_SYMBOL(register_con_driver);
3377
3378/**
3379 * unregister_con_driver - unregister console driver from console layer
3380 * @csw: console driver
3381 *
3382 * DESCRIPTION: All drivers that registers to the console layer must
3383 * call this function upon exit, or if the console driver is in a state
3384 * where it won't be able to handle console services, such as the
3385 * framebuffer console without loaded framebuffer drivers.
3386 *
3387 * The driver must unbind first prior to unregistration.
3388 */
3389int unregister_con_driver(const struct consw *csw)
3390{
3391 int i, retval = -ENODEV;
3392
3393 acquire_console_sem();
3394
3395 /* cannot unregister a bound driver */
3396 if (con_is_bound(csw))
3397 goto err;
3398
3399 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3400 struct con_driver *con_driver = &registered_con_driver[i];
3401
3402 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003403 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003404 vtconsole_deinit_device(con_driver);
3405 device_destroy(vtconsole_class,
3406 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003407 con_driver->con = NULL;
3408 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003409 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003410 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003411 con_driver->flag = 0;
3412 con_driver->first = 0;
3413 con_driver->last = 0;
3414 retval = 0;
3415 break;
3416 }
3417 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003418err:
3419 release_console_sem();
3420 return retval;
3421}
3422EXPORT_SYMBOL(unregister_con_driver);
3423
3424/*
3425 * If we support more console drivers, this function is used
3426 * when a driver wants to take over some existing consoles
3427 * and become default driver for newly opened ones.
3428 *
3429 * take_over_console is basically a register followed by unbind
3430 */
3431int take_over_console(const struct consw *csw, int first, int last, int deflt)
3432{
3433 int err;
3434
3435 err = register_con_driver(csw, first, last);
3436
3437 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003438 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003439
3440 return err;
3441}
3442
3443/*
3444 * give_up_console is a wrapper to unregister_con_driver. It will only
3445 * work if driver is fully unbound.
3446 */
3447void give_up_console(const struct consw *csw)
3448{
3449 unregister_con_driver(csw);
3450}
3451
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003452static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003453{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003454 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003455
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003456 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3457 if (IS_ERR(vtconsole_class)) {
3458 printk(KERN_WARNING "Unable to create vt console class; "
3459 "errno = %ld\n", PTR_ERR(vtconsole_class));
3460 vtconsole_class = NULL;
3461 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003462
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003463 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003464 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3465 struct con_driver *con = &registered_con_driver[i];
3466
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003467 if (con->con && !con->dev) {
3468 con->dev = device_create(vtconsole_class, NULL,
3469 MKDEV(0, con->node),
3470 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003471
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003472 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003473 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003474 "device for %s; errno = %ld\n",
3475 con->desc, PTR_ERR(con->dev));
3476 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003477 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003478 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003479 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003480 }
3481 }
3482
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003483 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003484}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003485postcore_initcall(vtconsole_class_init);
3486
3487#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003488
3489/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 * Screen blanking
3491 */
3492
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003493static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003495 unsigned int mode;
3496
3497 if (get_user(mode, p + 1))
3498 return -EFAULT;
3499
3500 vesa_blank_mode = (mode < 4) ? mode : 0;
3501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502}
3503
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504void do_blank_screen(int entering_gfx)
3505{
3506 struct vc_data *vc = vc_cons[fg_console].d;
3507 int i;
3508
3509 WARN_CONSOLE_UNLOCKED();
3510
3511 if (console_blanked) {
3512 if (blank_state == blank_vesa_wait) {
3513 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003514 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
3516 return;
3517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518
3519 /* entering graphics mode? */
3520 if (entering_gfx) {
3521 hide_cursor(vc);
3522 save_screen(vc);
3523 vc->vc_sw->con_blank(vc, -1, 1);
3524 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003525 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 set_origin(vc);
3527 return;
3528 }
3529
izumib6e8f002007-07-17 04:05:49 -07003530 if (blank_state != blank_normal_wait)
3531 return;
3532 blank_state = blank_off;
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 /* don't blank graphics */
3535 if (vc->vc_mode != KD_TEXT) {
3536 console_blanked = fg_console + 1;
3537 return;
3538 }
3539
3540 hide_cursor(vc);
3541 del_timer_sync(&console_timer);
3542 blank_timer_expired = 0;
3543
3544 save_screen(vc);
3545 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003546 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 console_blanked = fg_console + 1;
3548 if (i)
3549 set_origin(vc);
3550
3551 if (console_blank_hook && console_blank_hook(1))
3552 return;
3553
Ville Syrjalad060a322006-01-09 20:53:49 -08003554 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003555 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 mod_timer(&console_timer, jiffies + vesa_off_interval);
3557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558}
3559EXPORT_SYMBOL(do_blank_screen);
3560
3561/*
3562 * Called by timer as well as from vt_console_driver
3563 */
3564void do_unblank_screen(int leaving_gfx)
3565{
3566 struct vc_data *vc;
3567
3568 /* This should now always be called from a "sane" (read: can schedule)
3569 * context for the sake of the low level drivers, except in the special
3570 * case of oops_in_progress
3571 */
3572 if (!oops_in_progress)
3573 might_sleep();
3574
3575 WARN_CONSOLE_UNLOCKED();
3576
3577 ignore_poke = 0;
3578 if (!console_blanked)
3579 return;
3580 if (!vc_cons_allocated(fg_console)) {
3581 /* impossible */
3582 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3583 return;
3584 }
3585 vc = vc_cons[fg_console].d;
3586 if (vc->vc_mode != KD_TEXT)
3587 return; /* but leave console_blanked != 0 */
3588
3589 if (blankinterval) {
3590 mod_timer(&console_timer, jiffies + blankinterval);
3591 blank_state = blank_normal_wait;
3592 }
3593
3594 console_blanked = 0;
3595 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
3596 /* Low-level driver cannot restore -> do it ourselves */
3597 update_screen(vc);
3598 if (console_blank_hook)
3599 console_blank_hook(0);
3600 set_palette(vc);
3601 set_cursor(vc);
3602}
3603EXPORT_SYMBOL(do_unblank_screen);
3604
3605/*
3606 * This is called by the outside world to cause a forced unblank, mostly for
3607 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3608 * call it with 1 as an argument and so force a mode restore... that may kill
3609 * X or at least garbage the screen but would also make the Oops visible...
3610 */
3611void unblank_screen(void)
3612{
3613 do_unblank_screen(0);
3614}
3615
3616/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003617 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003619 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 */
3621static void blank_screen_t(unsigned long dummy)
3622{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003623 if (unlikely(!keventd_up())) {
3624 mod_timer(&console_timer, jiffies + blankinterval);
3625 return;
3626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 blank_timer_expired = 1;
3628 schedule_work(&console_work);
3629}
3630
3631void poke_blanked_console(void)
3632{
3633 WARN_CONSOLE_UNLOCKED();
3634
3635 /* Add this so we quickly catch whoever might call us in a non
3636 * safe context. Nowadays, unblank_screen() isn't to be called in
3637 * atomic contexts and is allowed to schedule (with the special case
3638 * of oops_in_progress, but that isn't of any concern for this
3639 * function. --BenH.
3640 */
3641 might_sleep();
3642
3643 /* This isn't perfectly race free, but a race here would be mostly harmless,
3644 * at worse, we'll do a spurrious blank and it's unlikely
3645 */
3646 del_timer(&console_timer);
3647 blank_timer_expired = 0;
3648
3649 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3650 return;
3651 if (console_blanked)
3652 unblank_screen();
3653 else if (blankinterval) {
3654 mod_timer(&console_timer, jiffies + blankinterval);
3655 blank_state = blank_normal_wait;
3656 }
3657}
3658
3659/*
3660 * Palettes
3661 */
3662
3663static void set_palette(struct vc_data *vc)
3664{
3665 WARN_CONSOLE_UNLOCKED();
3666
3667 if (vc->vc_mode != KD_GRAPHICS)
3668 vc->vc_sw->con_set_palette(vc, color_table);
3669}
3670
3671static int set_get_cmap(unsigned char __user *arg, int set)
3672{
3673 int i, j, k;
3674
3675 WARN_CONSOLE_UNLOCKED();
3676
3677 for (i = 0; i < 16; i++)
3678 if (set) {
3679 get_user(default_red[i], arg++);
3680 get_user(default_grn[i], arg++);
3681 get_user(default_blu[i], arg++);
3682 } else {
3683 put_user(default_red[i], arg++);
3684 put_user(default_grn[i], arg++);
3685 put_user(default_blu[i], arg++);
3686 }
3687 if (set) {
3688 for (i = 0; i < MAX_NR_CONSOLES; i++)
3689 if (vc_cons_allocated(i)) {
3690 for (j = k = 0; j < 16; j++) {
3691 vc_cons[i].d->vc_palette[k++] = default_red[j];
3692 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3693 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3694 }
3695 set_palette(vc_cons[i].d);
3696 }
3697 }
3698 return 0;
3699}
3700
3701/*
3702 * Load palette into the DAC registers. arg points to a colour
3703 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3704 */
3705
3706int con_set_cmap(unsigned char __user *arg)
3707{
3708 int rc;
3709
3710 acquire_console_sem();
3711 rc = set_get_cmap (arg,1);
3712 release_console_sem();
3713
3714 return rc;
3715}
3716
3717int con_get_cmap(unsigned char __user *arg)
3718{
3719 int rc;
3720
3721 acquire_console_sem();
3722 rc = set_get_cmap (arg,0);
3723 release_console_sem();
3724
3725 return rc;
3726}
3727
3728void reset_palette(struct vc_data *vc)
3729{
3730 int j, k;
3731 for (j=k=0; j<16; j++) {
3732 vc->vc_palette[k++] = default_red[j];
3733 vc->vc_palette[k++] = default_grn[j];
3734 vc->vc_palette[k++] = default_blu[j];
3735 }
3736 set_palette(vc);
3737}
3738
3739/*
3740 * Font switching
3741 *
3742 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3743 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3744 * depending on width) reserved for each character which is kinda wasty, but
3745 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3746 * is upto the actual low-level console-driver convert data into its favorite
3747 * format (maybe we should add a `fontoffset' field to the `display'
3748 * structure so we won't have to convert the fontdata all the time.
3749 * /Jes
3750 */
3751
3752#define max_font_size 65536
3753
3754static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3755{
3756 struct console_font font;
3757 int rc = -EINVAL;
3758 int c;
3759
3760 if (vc->vc_mode != KD_TEXT)
3761 return -EINVAL;
3762
3763 if (op->data) {
3764 font.data = kmalloc(max_font_size, GFP_KERNEL);
3765 if (!font.data)
3766 return -ENOMEM;
3767 } else
3768 font.data = NULL;
3769
3770 acquire_console_sem();
3771 if (vc->vc_sw->con_font_get)
3772 rc = vc->vc_sw->con_font_get(vc, &font);
3773 else
3774 rc = -ENOSYS;
3775 release_console_sem();
3776
3777 if (rc)
3778 goto out;
3779
3780 c = (font.width+7)/8 * 32 * font.charcount;
3781
3782 if (op->data && font.charcount > op->charcount)
3783 rc = -ENOSPC;
3784 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3785 if (font.width > op->width || font.height > op->height)
3786 rc = -ENOSPC;
3787 } else {
3788 if (font.width != 8)
3789 rc = -EIO;
3790 else if ((op->height && font.height > op->height) ||
3791 font.height > 32)
3792 rc = -ENOSPC;
3793 }
3794 if (rc)
3795 goto out;
3796
3797 op->height = font.height;
3798 op->width = font.width;
3799 op->charcount = font.charcount;
3800
3801 if (op->data && copy_to_user(op->data, font.data, c))
3802 rc = -EFAULT;
3803
3804out:
3805 kfree(font.data);
3806 return rc;
3807}
3808
3809static int con_font_set(struct vc_data *vc, struct console_font_op *op)
3810{
3811 struct console_font font;
3812 int rc = -EINVAL;
3813 int size;
3814
3815 if (vc->vc_mode != KD_TEXT)
3816 return -EINVAL;
3817 if (!op->data)
3818 return -EINVAL;
3819 if (op->charcount > 512)
3820 return -EINVAL;
3821 if (!op->height) { /* Need to guess font height [compat] */
3822 int h, i;
3823 u8 __user *charmap = op->data;
3824 u8 tmp;
3825
3826 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
3827 so that we can get rid of this soon */
3828 if (!(op->flags & KD_FONT_FLAG_OLD))
3829 return -EINVAL;
3830 for (h = 32; h > 0; h--)
3831 for (i = 0; i < op->charcount; i++) {
3832 if (get_user(tmp, &charmap[32*i+h-1]))
3833 return -EFAULT;
3834 if (tmp)
3835 goto nonzero;
3836 }
3837 return -EINVAL;
3838 nonzero:
3839 op->height = h;
3840 }
3841 if (op->width <= 0 || op->width > 32 || op->height > 32)
3842 return -EINVAL;
3843 size = (op->width+7)/8 * 32 * op->charcount;
3844 if (size > max_font_size)
3845 return -ENOSPC;
3846 font.charcount = op->charcount;
3847 font.height = op->height;
3848 font.width = op->width;
3849 font.data = kmalloc(size, GFP_KERNEL);
3850 if (!font.data)
3851 return -ENOMEM;
3852 if (copy_from_user(font.data, op->data, size)) {
3853 kfree(font.data);
3854 return -EFAULT;
3855 }
3856 acquire_console_sem();
3857 if (vc->vc_sw->con_font_set)
3858 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
3859 else
3860 rc = -ENOSYS;
3861 release_console_sem();
3862 kfree(font.data);
3863 return rc;
3864}
3865
3866static int con_font_default(struct vc_data *vc, struct console_font_op *op)
3867{
3868 struct console_font font = {.width = op->width, .height = op->height};
3869 char name[MAX_FONT_NAME];
3870 char *s = name;
3871 int rc;
3872
3873 if (vc->vc_mode != KD_TEXT)
3874 return -EINVAL;
3875
3876 if (!op->data)
3877 s = NULL;
3878 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
3879 return -EFAULT;
3880 else
3881 name[MAX_FONT_NAME - 1] = 0;
3882
3883 acquire_console_sem();
3884 if (vc->vc_sw->con_font_default)
3885 rc = vc->vc_sw->con_font_default(vc, &font, s);
3886 else
3887 rc = -ENOSYS;
3888 release_console_sem();
3889 if (!rc) {
3890 op->width = font.width;
3891 op->height = font.height;
3892 }
3893 return rc;
3894}
3895
3896static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
3897{
3898 int con = op->height;
3899 int rc;
3900
3901 if (vc->vc_mode != KD_TEXT)
3902 return -EINVAL;
3903
3904 acquire_console_sem();
3905 if (!vc->vc_sw->con_font_copy)
3906 rc = -ENOSYS;
3907 else if (con < 0 || !vc_cons_allocated(con))
3908 rc = -ENOTTY;
3909 else if (con == vc->vc_num) /* nothing to do */
3910 rc = 0;
3911 else
3912 rc = vc->vc_sw->con_font_copy(vc, con);
3913 release_console_sem();
3914 return rc;
3915}
3916
3917int con_font_op(struct vc_data *vc, struct console_font_op *op)
3918{
3919 switch (op->op) {
3920 case KD_FONT_OP_SET:
3921 return con_font_set(vc, op);
3922 case KD_FONT_OP_GET:
3923 return con_font_get(vc, op);
3924 case KD_FONT_OP_SET_DEFAULT:
3925 return con_font_default(vc, op);
3926 case KD_FONT_OP_COPY:
3927 return con_font_copy(vc, op);
3928 }
3929 return -ENOSYS;
3930}
3931
3932/*
3933 * Interface exported to selection and vcs.
3934 */
3935
3936/* used by selection */
3937u16 screen_glyph(struct vc_data *vc, int offset)
3938{
3939 u16 w = scr_readw(screenpos(vc, offset, 1));
3940 u16 c = w & 0xff;
3941
3942 if (w & vc->vc_hi_font_mask)
3943 c |= 0x100;
3944 return c;
3945}
3946
3947/* used by vcs - note the word offset */
3948unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
3949{
3950 return screenpos(vc, 2 * w_offset, viewed);
3951}
3952
3953void getconsxy(struct vc_data *vc, unsigned char *p)
3954{
3955 p[0] = vc->vc_x;
3956 p[1] = vc->vc_y;
3957}
3958
3959void putconsxy(struct vc_data *vc, unsigned char *p)
3960{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08003961 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 gotoxy(vc, p[0], p[1]);
3963 set_cursor(vc);
3964}
3965
3966u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
3967{
3968 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
3969 return softcursor_original;
3970 return scr_readw(org);
3971}
3972
3973void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
3974{
3975 scr_writew(val, org);
3976 if ((unsigned long)org == vc->vc_pos) {
3977 softcursor_original = -1;
3978 add_softcursor(vc);
3979 }
3980}
3981
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982/*
3983 * Visible symbols for modules
3984 */
3985
3986EXPORT_SYMBOL(color_table);
3987EXPORT_SYMBOL(default_red);
3988EXPORT_SYMBOL(default_grn);
3989EXPORT_SYMBOL(default_blu);
3990EXPORT_SYMBOL(update_region);
3991EXPORT_SYMBOL(redraw_screen);
3992EXPORT_SYMBOL(vc_resize);
Alan Coxca9bda02006-09-29 02:00:03 -07003993EXPORT_SYMBOL(vc_lock_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994EXPORT_SYMBOL(fg_console);
3995EXPORT_SYMBOL(console_blank_hook);
3996EXPORT_SYMBOL(console_blanked);
3997EXPORT_SYMBOL(vc_cons);
3998#ifndef VT_SINGLE_DRIVER
3999EXPORT_SYMBOL(take_over_console);
4000EXPORT_SYMBOL(give_up_console);
4001#endif