blob: 9f67ad919a4ad775b29a0f3c5fc90415730635f9 [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
Francois Camie1f8e872008-10-15 22:01:59 -070062 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040092#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/tiocl.h>
94#include <linux/kbd_kern.h>
95#include <linux/consolemap.h>
96#include <linux/timer.h>
97#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/pm.h>
100#include <linux/font.h>
101#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700102#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100103#include <linux/device.h>
104#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <asm/system.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100106#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500107#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300108#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700110#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700112#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700113#define CON_DRIVER_FLAG_INIT 2
114#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700115
116struct con_driver {
117 const struct consw *con;
118 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700119 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700120 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700121 int first;
122 int last;
123 int flag;
124};
125
126static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127const struct consw *conswitchp;
128
129/* A bitmap for codes <32. A bit of 1 indicates that the code
130 * corresponding to that bit number invokes some special action
131 * (such as cursor movement) and should not be displayed as a
132 * glyph unless the disp_ctrl mode is explicitly enabled.
133 */
134#define CTRL_ACTION 0x0d00ff81
135#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
136
137/*
138 * Here is the default bell parameters: 750HZ, 1/8th of a second
139 */
140#define DEFAULT_BELL_PITCH 750
141#define DEFAULT_BELL_DURATION (HZ/8)
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143struct vc vc_cons [MAX_NR_CONSOLES];
144
145#ifndef VT_SINGLE_DRIVER
146static const struct consw *con_driver_map[MAX_NR_CONSOLES];
147#endif
148
149static int con_open(struct tty_struct *, struct file *);
150static void vc_init(struct vc_data *vc, unsigned int rows,
151 unsigned int cols, int do_clear);
152static void gotoxy(struct vc_data *vc, int new_x, int new_y);
153static void save_cur(struct vc_data *vc);
154static void reset_terminal(struct vc_data *vc, int do_clear);
155static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800156static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void set_cursor(struct vc_data *vc);
158static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000159static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void blank_screen_t(unsigned long dummy);
161static void set_palette(struct vc_data *vc);
162
163static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700164int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700165module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500166int global_cursor_default = -1;
167module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800169static int cur_default = CUR_DEFAULT;
170module_param(cur_default, int, S_IRUGO | S_IWUSR);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ignore_poke: don't unblank the screen when things are typed. This is
174 * mainly for the privacy of braille terminal users.
175 */
176static int ignore_poke;
177
178int do_poke_blanked_console;
179int console_blanked;
180
181static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int vesa_off_interval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700183static int blankinterval = 10*60;
184core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howells65f27f32006-11-22 14:55:48 +0000186static DECLARE_WORK(console_work, console_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
189 * fg_console is the current virtual console,
190 * last_console is the last used one,
191 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500192 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194int fg_console;
195int last_console;
196int want_console = -1;
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500197int saved_fg_console;
198int saved_last_console;
199int saved_want_console;
200int saved_vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * For each existing display, we have a pointer to console currently visible
204 * on that display, allowing consoles other than fg_console to be refreshed
205 * appropriately. Unless the low-level driver supplies its own display_fg
206 * variable, we use this one for the "master display".
207 */
208static struct vc_data *master_display_fg;
209
210/*
211 * Unfortunately, we need to delay tty echo when we're currently writing to the
212 * console since the code is (and always was) not re-entrant, so we schedule
213 * all flip requests to process context with schedule-task() and run it from
214 * console_callback().
215 */
216
217/*
218 * For the same reason, we defer scrollback to the console callback.
219 */
220static int scrollback_delta;
221
222/*
223 * Hook so that the power management routines can (un)blank
224 * the console on our behalf.
225 */
226int (*console_blank_hook)(int);
227
Jiri Slaby40565f12007-02-12 00:52:31 -0800228static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int blank_state;
230static int blank_timer_expired;
231enum {
232 blank_off = 0,
233 blank_normal_wait,
234 blank_vesa_wait,
235};
236
237/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700238 * Notifier list for console events.
239 */
240static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
241
242int register_vt_notifier(struct notifier_block *nb)
243{
244 return atomic_notifier_chain_register(&vt_notifier_list, nb);
245}
246EXPORT_SYMBOL_GPL(register_vt_notifier);
247
248int unregister_vt_notifier(struct notifier_block *nb)
249{
250 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
251}
252EXPORT_SYMBOL_GPL(unregister_vt_notifier);
253
254static void notify_write(struct vc_data *vc, unsigned int unicode)
255{
256 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
257 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
258}
259
260static void notify_update(struct vc_data *vc)
261{
262 struct vt_notifier_param param = { .vc = vc };
263 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
264}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * Low-Level Functions
267 */
268
269#define IS_FG(vc) ((vc)->vc_num == fg_console)
270
271#ifdef VT_BUF_VRAM_ONLY
272#define DO_UPDATE(vc) 0
273#else
Stefano Stabellinif700d6e2008-07-23 21:29:59 -0700274#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276
277static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
278{
279 unsigned short *p;
280
281 if (!viewed)
282 p = (unsigned short *)(vc->vc_origin + offset);
283 else if (!vc->vc_sw->con_screen_pos)
284 p = (unsigned short *)(vc->vc_visible_origin + offset);
285 else
286 p = vc->vc_sw->con_screen_pos(vc, offset);
287 return p;
288}
289
Alan Coxe33ac1c2010-06-01 22:52:54 +0200290/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291static inline void scrolldelta(int lines)
292{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200293 /* FIXME */
294 /* scrolldelta needs some kind of consistency lock, but the BKL was
295 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 scrollback_delta += lines;
297 schedule_console_callback();
298}
299
300void schedule_console_callback(void)
301{
302 schedule_work(&console_work);
303}
304
305static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
306{
307 unsigned short *d, *s;
308
309 if (t+nr >= b)
310 nr = b - t - 1;
311 if (b > vc->vc_rows || t >= b || nr < 1)
312 return;
313 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
314 return;
315 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
316 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
317 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds386f40c2010-06-06 20:44:04 -0700318 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 vc->vc_size_row * nr);
320}
321
322static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
323{
324 unsigned short *s;
325 unsigned int step;
326
327 if (t+nr >= b)
328 nr = b - t - 1;
329 if (b > vc->vc_rows || t >= b || nr < 1)
330 return;
331 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
332 return;
333 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
334 step = vc->vc_cols * nr;
335 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds93f78da2008-10-14 12:12:02 -0700336 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
339static void do_update_region(struct vc_data *vc, unsigned long start, int count)
340{
341#ifndef VT_BUF_VRAM_ONLY
342 unsigned int xx, yy, offset;
343 u16 *p;
344
345 p = (u16 *) start;
346 if (!vc->vc_sw->con_getxy) {
347 offset = (start - vc->vc_origin) / 2;
348 xx = offset % vc->vc_cols;
349 yy = offset / vc->vc_cols;
350 } else {
351 int nxx, nyy;
352 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
353 xx = nxx; yy = nyy;
354 }
355 for(;;) {
356 u16 attrib = scr_readw(p) & 0xff00;
357 int startx = xx;
358 u16 *q = p;
359 while (xx < vc->vc_cols && count) {
360 if (attrib != (scr_readw(p) & 0xff00)) {
361 if (p > q)
362 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
363 startx = xx;
364 q = p;
365 attrib = scr_readw(p) & 0xff00;
366 }
367 p++;
368 xx++;
369 count--;
370 }
371 if (p > q)
372 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
373 if (!count)
374 break;
375 xx = 0;
376 yy++;
377 if (vc->vc_sw->con_getxy) {
378 p = (u16 *)start;
379 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
380 }
381 }
382#endif
383}
384
385void update_region(struct vc_data *vc, unsigned long start, int count)
386{
387 WARN_CONSOLE_UNLOCKED();
388
389 if (DO_UPDATE(vc)) {
390 hide_cursor(vc);
391 do_update_region(vc, start, count);
392 set_cursor(vc);
393 }
394}
395
396/* Structure of attributes is hardware-dependent */
397
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700398static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
399 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700402 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
403 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405#ifndef VT_BUF_VRAM_ONLY
406/*
407 * ++roman: I completely changed the attribute format for monochrome
408 * mode (!can_do_color). The formerly used MDA (monochrome display
409 * adapter) format didn't allow the combination of certain effects.
410 * Now the attribute is just a bit vector:
411 * Bit 0..1: intensity (0..2)
412 * Bit 2 : underline
413 * Bit 3 : reverse
414 * Bit 7 : blink
415 */
416 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700417 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (!vc->vc_can_do_color)
419 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700420 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 (_underline ? 4 : 0) |
422 (_reverse ? 8 : 0) |
423 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700424 if (_italic)
425 a = (a & 0xF0) | vc->vc_itcolor;
426 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 a = (a & 0xf0) | vc->vc_ulcolor;
428 else if (_intensity == 0)
429 a = (a & 0xf0) | vc->vc_ulcolor;
430 if (_reverse)
431 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
432 if (_blink)
433 a ^= 0x80;
434 if (_intensity == 2)
435 a ^= 0x08;
436 if (vc->vc_hi_font_mask == 0x100)
437 a <<= 1;
438 return a;
439 }
440#else
441 return 0;
442#endif
443}
444
445static void update_attr(struct vc_data *vc)
446{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700447 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
448 vc->vc_blink, vc->vc_underline,
449 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
450 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 -0700451}
452
453/* Note: inverting the screen twice should revert to the original state */
454void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
455{
456 unsigned short *p;
457
458 WARN_CONSOLE_UNLOCKED();
459
460 count /= 2;
461 p = screenpos(vc, offset, viewed);
462 if (vc->vc_sw->con_invert_region)
463 vc->vc_sw->con_invert_region(vc, p, count);
464#ifndef VT_BUF_VRAM_ONLY
465 else {
466 u16 *q = p;
467 int cnt = count;
468 u16 a;
469
470 if (!vc->vc_can_do_color) {
471 while (cnt--) {
472 a = scr_readw(q);
473 a ^= 0x0800;
474 scr_writew(a, q);
475 q++;
476 }
477 } else if (vc->vc_hi_font_mask == 0x100) {
478 while (cnt--) {
479 a = scr_readw(q);
480 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
481 scr_writew(a, q);
482 q++;
483 }
484 } else {
485 while (cnt--) {
486 a = scr_readw(q);
487 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
488 scr_writew(a, q);
489 q++;
490 }
491 }
492 }
493#endif
494 if (DO_UPDATE(vc))
495 do_update_region(vc, (unsigned long) p, count);
496}
497
498/* used by selection: complement pointer position */
499void complement_pos(struct vc_data *vc, int offset)
500{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700501 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 static unsigned short old;
503 static unsigned short oldx, oldy;
504
505 WARN_CONSOLE_UNLOCKED();
506
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700507 if (old_offset != -1 && old_offset >= 0 &&
508 old_offset < vc->vc_screenbuf_size) {
509 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 if (DO_UPDATE(vc))
511 vc->vc_sw->con_putc(vc, old, oldy, oldx);
512 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700513
514 old_offset = offset;
515
516 if (offset != -1 && offset >= 0 &&
517 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700519 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 p = screenpos(vc, offset, 1);
521 old = scr_readw(p);
522 new = old ^ vc->vc_complement_mask;
523 scr_writew(new, p);
524 if (DO_UPDATE(vc)) {
525 oldx = (offset >> 1) % vc->vc_cols;
526 oldy = (offset >> 1) / vc->vc_cols;
527 vc->vc_sw->con_putc(vc, new, oldy, oldx);
528 }
529 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
533static void insert_char(struct vc_data *vc, unsigned int nr)
534{
535 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
536
537 p = q + vc->vc_cols - nr - vc->vc_x;
538 while (--p >= q)
539 scr_writew(scr_readw(p), p + nr);
540 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
541 vc->vc_need_wrap = 0;
542 if (DO_UPDATE(vc)) {
543 unsigned short oldattr = vc->vc_attr;
544 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
545 vc->vc_cols - vc->vc_x - nr);
546 vc->vc_attr = vc->vc_video_erase_char >> 8;
547 while (nr--)
548 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
549 vc->vc_attr = oldattr;
550 }
551}
552
553static void delete_char(struct vc_data *vc, unsigned int nr)
554{
555 unsigned int i = vc->vc_x;
556 unsigned short *p = (unsigned short *)vc->vc_pos;
557
558 while (++i <= vc->vc_cols - nr) {
559 scr_writew(scr_readw(p+nr), p);
560 p++;
561 }
562 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
563 vc->vc_need_wrap = 0;
564 if (DO_UPDATE(vc)) {
565 unsigned short oldattr = vc->vc_attr;
566 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
567 vc->vc_cols - vc->vc_x - nr);
568 vc->vc_attr = vc->vc_video_erase_char >> 8;
569 while (nr--)
570 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
571 vc->vc_cols - 1 - nr);
572 vc->vc_attr = oldattr;
573 }
574}
575
576static int softcursor_original;
577
578static void add_softcursor(struct vc_data *vc)
579{
580 int i = scr_readw((u16 *) vc->vc_pos);
581 u32 type = vc->vc_cursor_type;
582
583 if (! (type & 0x10)) return;
584 if (softcursor_original != -1) return;
585 softcursor_original = i;
586 i |= ((type >> 8) & 0xff00 );
587 i ^= ((type) & 0xff00 );
588 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
589 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
590 scr_writew(i, (u16 *) vc->vc_pos);
591 if (DO_UPDATE(vc))
592 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
593}
594
595static void hide_softcursor(struct vc_data *vc)
596{
597 if (softcursor_original != -1) {
598 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
599 if (DO_UPDATE(vc))
600 vc->vc_sw->con_putc(vc, softcursor_original,
601 vc->vc_y, vc->vc_x);
602 softcursor_original = -1;
603 }
604}
605
606static void hide_cursor(struct vc_data *vc)
607{
608 if (vc == sel_cons)
609 clear_selection();
610 vc->vc_sw->con_cursor(vc, CM_ERASE);
611 hide_softcursor(vc);
612}
613
614static void set_cursor(struct vc_data *vc)
615{
616 if (!IS_FG(vc) || console_blanked ||
617 vc->vc_mode == KD_GRAPHICS)
618 return;
619 if (vc->vc_deccm) {
620 if (vc == sel_cons)
621 clear_selection();
622 add_softcursor(vc);
623 if ((vc->vc_cursor_type & 0x0f) != 1)
624 vc->vc_sw->con_cursor(vc, CM_DRAW);
625 } else
626 hide_cursor(vc);
627}
628
629static void set_origin(struct vc_data *vc)
630{
631 WARN_CONSOLE_UNLOCKED();
632
633 if (!CON_IS_VISIBLE(vc) ||
634 !vc->vc_sw->con_set_origin ||
635 !vc->vc_sw->con_set_origin(vc))
636 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
637 vc->vc_visible_origin = vc->vc_origin;
638 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
639 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
640}
641
642static inline void save_screen(struct vc_data *vc)
643{
644 WARN_CONSOLE_UNLOCKED();
645
646 if (vc->vc_sw->con_save_screen)
647 vc->vc_sw->con_save_screen(vc);
648}
649
650/*
651 * Redrawing of screen
652 */
653
654static void clear_buffer_attributes(struct vc_data *vc)
655{
656 unsigned short *p = (unsigned short *)vc->vc_origin;
657 int count = vc->vc_screenbuf_size / 2;
658 int mask = vc->vc_hi_font_mask | 0xff;
659
660 for (; count > 0; count--, p++) {
661 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
662 }
663}
664
665void redraw_screen(struct vc_data *vc, int is_switch)
666{
667 int redraw = 0;
668
669 WARN_CONSOLE_UNLOCKED();
670
671 if (!vc) {
672 /* strange ... */
673 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
674 return;
675 }
676
677 if (is_switch) {
678 struct vc_data *old_vc = vc_cons[fg_console].d;
679 if (old_vc == vc)
680 return;
681 if (!CON_IS_VISIBLE(vc))
682 redraw = 1;
683 *vc->vc_display_fg = vc;
684 fg_console = vc->vc_num;
685 hide_cursor(old_vc);
686 if (!CON_IS_VISIBLE(old_vc)) {
687 save_screen(old_vc);
688 set_origin(old_vc);
689 }
690 } else {
691 hide_cursor(vc);
692 redraw = 1;
693 }
694
695 if (redraw) {
696 int update;
697 int old_was_color = vc->vc_can_do_color;
698
699 set_origin(vc);
700 update = vc->vc_sw->con_switch(vc);
701 set_palette(vc);
702 /*
703 * If console changed from mono<->color, the best we can do
704 * is to clear the buffer attributes. As it currently stands,
705 * rebuilding new attributes from the old buffer is not doable
706 * without overly complex code.
707 */
708 if (old_was_color != vc->vc_can_do_color) {
709 update_attr(vc);
710 clear_buffer_attributes(vc);
711 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700712
713 /* Forcibly update if we're panicing */
714 if ((update && vc->vc_mode != KD_GRAPHICS) ||
715 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
717 }
718 set_cursor(vc);
719 if (is_switch) {
720 set_leds();
721 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800722 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 }
724}
725
726/*
727 * Allocation, freeing and resizing of VTs.
728 */
729
730int vc_cons_allocated(unsigned int i)
731{
732 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
733}
734
735static void visual_init(struct vc_data *vc, int num, int init)
736{
737 /* ++Geert: vc->vc_sw->con_init determines console size */
738 if (vc->vc_sw)
739 module_put(vc->vc_sw->owner);
740 vc->vc_sw = conswitchp;
741#ifndef VT_SINGLE_DRIVER
742 if (con_driver_map[num])
743 vc->vc_sw = con_driver_map[num];
744#endif
745 __module_get(vc->vc_sw->owner);
746 vc->vc_num = num;
747 vc->vc_display_fg = &master_display_fg;
748 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
749 vc->vc_uni_pagedir = 0;
750 vc->vc_hi_font_mask = 0;
751 vc->vc_complement_mask = 0;
752 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700753 vc->vc_panic_force_write = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 vc->vc_sw->con_init(vc, init);
755 if (!vc->vc_complement_mask)
756 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
757 vc->vc_s_complement_mask = vc->vc_complement_mask;
758 vc->vc_size_row = vc->vc_cols << 1;
759 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
760}
761
762int vc_allocate(unsigned int currcons) /* return 0 on success */
763{
764 WARN_CONSOLE_UNLOCKED();
765
766 if (currcons >= MAX_NR_CONSOLES)
767 return -ENXIO;
768 if (!vc_cons[currcons].d) {
769 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700770 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772 /* prevent users from taking too much memory */
773 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
774 return -EPERM;
775
776 /* due to the granularity of kmalloc, we waste some memory here */
777 /* the alloc is done in two steps, to optimize the common situation
778 of a 25x80 console (structsize=216, screenbuf_size=4000) */
779 /* although the numbers above are not valid since long ago, the
780 point is still up-to-date and the comment still has its value
781 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700782 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!vc)
784 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 vc_cons[currcons].d = vc;
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800786 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 visual_init(vc, currcons, 1);
788 if (!*vc->vc_uni_pagedir_loc)
789 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100790 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 if (!vc->vc_screenbuf) {
792 kfree(vc);
793 vc_cons[currcons].d = NULL;
794 return -ENOMEM;
795 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500796
797 /* If no drivers have overridden us and the user didn't pass a
798 boot option, default to displaying the cursor */
799 if (global_cursor_default == -1)
800 global_cursor_default = 1;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100803 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700804 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806 return 0;
807}
808
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700809static inline int resize_screen(struct vc_data *vc, int width, int height,
810 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 /* Resizes the resolution of the display adapater */
813 int err = 0;
814
815 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700816 err = vc->vc_sw->con_resize(vc, width, height, user);
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 return err;
819}
820
821/*
822 * Change # of rows and columns (0 means unchanged/the size of fg_console)
823 * [this is to be used together with some user program
824 * like resize that changes the hardware videomode]
825 */
826#define VC_RESIZE_MAXCOL (32767)
827#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100828
829/**
830 * vc_do_resize - resizing method for the tty
831 * @tty: tty being resized
832 * @real_tty: real tty (different to tty if a pty/tty pair)
833 * @vc: virtual console private data
834 * @cols: columns
835 * @lines: lines
836 *
837 * Resize a virtual console, clipping according to the actual constraints.
838 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800839 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100840 *
841 * Caller must hold the console semaphore. Takes the termios mutex and
842 * ctrl_lock of the tty IFF a tty is passed.
843 */
844
Alan Coxfc6f6232009-01-02 13:43:17 +0000845static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
846 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700849 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
851 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700852 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 unsigned short *newscreen;
854
855 WARN_CONSOLE_UNLOCKED();
856
857 if (!vc)
858 return -ENXIO;
859
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700860 user = vc->vc_resize_user;
861 vc->vc_resize_user = 0;
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
864 return -EINVAL;
865
866 new_cols = (cols ? cols : vc->vc_cols);
867 new_rows = (lines ? lines : vc->vc_rows);
868 new_row_size = new_cols << 1;
869 new_screen_size = new_row_size * new_rows;
870
871 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
872 return 0;
873
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800874 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (!newscreen)
876 return -ENOMEM;
877
878 old_rows = vc->vc_rows;
879 old_cols = vc->vc_cols;
880 old_row_size = vc->vc_size_row;
881 old_screen_size = vc->vc_screenbuf_size;
882
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700883 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (err) {
885 kfree(newscreen);
886 return err;
887 }
888
889 vc->vc_rows = new_rows;
890 vc->vc_cols = new_cols;
891 vc->vc_size_row = new_row_size;
892 vc->vc_screenbuf_size = new_screen_size;
893
894 rlth = min(old_row_size, new_row_size);
895 rrem = new_row_size - rlth;
896 old_origin = vc->vc_origin;
897 new_origin = (long) newscreen;
898 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700899
900 if (vc->vc_y > new_rows) {
901 if (old_rows - vc->vc_y < new_rows) {
902 /*
903 * Cursor near the bottom, copy contents from the
904 * bottom of buffer
905 */
906 old_origin += (old_rows - new_rows) * old_row_size;
907 end = vc->vc_scr_end;
908 } else {
909 /*
910 * Cursor is in no man's land, copy 1/2 screenful
911 * from the top and bottom of cursor position
912 */
913 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800914 end = old_origin + (old_row_size * new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700915 }
916 } else
917 /*
918 * Cursor near the top, copy contents from the top of buffer
919 */
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800920 end = (old_rows > new_rows) ? old_origin +
921 (old_row_size * new_rows) :
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 vc->vc_scr_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 update_attr(vc);
925
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700926 while (old_origin < end) {
927 scr_memcpyw((unsigned short *) new_origin,
928 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700930 scr_memsetw((void *)(new_origin + rlth),
931 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 old_origin += old_row_size;
933 new_origin += new_row_size;
934 }
935 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700936 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
937 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100938 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 vc->vc_screenbuf_size = new_screen_size;
941 set_origin(vc);
942
943 /* do part of a reset_terminal() */
944 vc->vc_top = 0;
945 vc->vc_bottom = vc->vc_rows;
946 gotoxy(vc, vc->vc_x, vc->vc_y);
947 save_cur(vc);
948
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100949 if (tty) {
950 /* Rewrite the requested winsize data with the actual
951 resulting sizes */
952 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 memset(&ws, 0, sizeof(ws));
954 ws.ws_row = vc->vc_rows;
955 ws.ws_col = vc->vc_cols;
956 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000957 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 }
959
960 if (CON_IS_VISIBLE(vc))
961 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700962 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 return err;
964}
965
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100966/**
967 * vc_resize - resize a VT
968 * @vc: virtual console
969 * @cols: columns
970 * @rows: rows
971 *
972 * Resize a virtual console as seen from the console end of things. We
973 * use the common vc_do_resize methods to update the structures. The
974 * caller must hold the console sem to protect console internals and
975 * vc->vc_tty
976 */
977
978int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700979{
Alan Coxfc6f6232009-01-02 13:43:17 +0000980 return vc_do_resize(vc->vc_tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100981}
982
983/**
984 * vt_resize - resize a VT
985 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100986 * @ws: winsize attributes
987 *
988 * Resize a virtual terminal. This is called by the tty layer as we
989 * register our own handler for resizing. The mutual helper does all
990 * the actual work.
991 *
992 * Takes the console sem and the called methods then take the tty
993 * termios_mutex and the tty ctrl_lock in that order.
994 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800995static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100996{
997 struct vc_data *vc = tty->driver_data;
998 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Alan Coxca9bda02006-09-29 02:00:03 -07001000 acquire_console_sem();
Alan Coxfc6f6232009-01-02 13:43:17 +00001001 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Alan Coxca9bda02006-09-29 02:00:03 -07001002 release_console_sem();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001003 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001004}
1005
1006void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007{
1008 WARN_CONSOLE_UNLOCKED();
1009
1010 if (vc_cons_allocated(currcons)) {
1011 struct vc_data *vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001012 struct vt_notifier_param param = { .vc = vc };
Kay Sievers4995f8e2009-03-09 14:18:52 +01001013
Samuel Thibaultb293d752007-10-18 23:39:17 -07001014 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001015 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001017 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001018 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001019 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 if (currcons >= MIN_NR_CONSOLES)
1021 kfree(vc);
1022 vc_cons[currcons].d = NULL;
1023 }
1024}
1025
1026/*
1027 * VT102 emulator
1028 */
1029
1030#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1031#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1032#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1033
1034#define decarm VC_REPEAT
1035#define decckm VC_CKMODE
1036#define kbdapplic VC_APPLIC
1037#define lnm VC_CRLF
1038
1039/*
1040 * this is what the terminal answers to a ESC-Z or csi0c query.
1041 */
1042#define VT100ID "\033[?1;2c"
1043#define VT102ID "\033[?6c"
1044
1045unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1046 8,12,10,14, 9,13,11,15 };
1047
1048/* the default colour table, for VGA+ colour systems */
1049int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1050 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1051int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1052 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1053int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1054 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1055
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001056module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1057module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1058module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/*
1061 * gotoxy() must verify all boundaries, because the arguments
1062 * might also be negative. If the given position is out of
1063 * bounds, the cursor is placed at the nearest margin.
1064 */
1065static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1066{
1067 int min_y, max_y;
1068
1069 if (new_x < 0)
1070 vc->vc_x = 0;
1071 else {
1072 if (new_x >= vc->vc_cols)
1073 vc->vc_x = vc->vc_cols - 1;
1074 else
1075 vc->vc_x = new_x;
1076 }
1077
1078 if (vc->vc_decom) {
1079 min_y = vc->vc_top;
1080 max_y = vc->vc_bottom;
1081 } else {
1082 min_y = 0;
1083 max_y = vc->vc_rows;
1084 }
1085 if (new_y < min_y)
1086 vc->vc_y = min_y;
1087 else if (new_y >= max_y)
1088 vc->vc_y = max_y - 1;
1089 else
1090 vc->vc_y = new_y;
1091 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1092 vc->vc_need_wrap = 0;
1093}
1094
1095/* for absolute user moves, when decom is set */
1096static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1097{
1098 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1099}
1100
1101void scrollback(struct vc_data *vc, int lines)
1102{
1103 if (!lines)
1104 lines = vc->vc_rows / 2;
1105 scrolldelta(-lines);
1106}
1107
1108void scrollfront(struct vc_data *vc, int lines)
1109{
1110 if (!lines)
1111 lines = vc->vc_rows / 2;
1112 scrolldelta(lines);
1113}
1114
1115static void lf(struct vc_data *vc)
1116{
1117 /* don't scroll if above bottom of scrolling region, or
1118 * if below scrolling region
1119 */
1120 if (vc->vc_y + 1 == vc->vc_bottom)
1121 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1122 else if (vc->vc_y < vc->vc_rows - 1) {
1123 vc->vc_y++;
1124 vc->vc_pos += vc->vc_size_row;
1125 }
1126 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001127 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128}
1129
1130static void ri(struct vc_data *vc)
1131{
1132 /* don't scroll if below top of scrolling region, or
1133 * if above scrolling region
1134 */
1135 if (vc->vc_y == vc->vc_top)
1136 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1137 else if (vc->vc_y > 0) {
1138 vc->vc_y--;
1139 vc->vc_pos -= vc->vc_size_row;
1140 }
1141 vc->vc_need_wrap = 0;
1142}
1143
1144static inline void cr(struct vc_data *vc)
1145{
1146 vc->vc_pos -= vc->vc_x << 1;
1147 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001148 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149}
1150
1151static inline void bs(struct vc_data *vc)
1152{
1153 if (vc->vc_x) {
1154 vc->vc_pos -= 2;
1155 vc->vc_x--;
1156 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001157 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159}
1160
1161static inline void del(struct vc_data *vc)
1162{
1163 /* ignored */
1164}
1165
1166static void csi_J(struct vc_data *vc, int vpar)
1167{
1168 unsigned int count;
1169 unsigned short * start;
1170
1171 switch (vpar) {
1172 case 0: /* erase from cursor to end of display */
1173 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1174 start = (unsigned short *)vc->vc_pos;
1175 if (DO_UPDATE(vc)) {
1176 /* do in two stages */
1177 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1178 vc->vc_cols - vc->vc_x);
1179 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1180 vc->vc_rows - vc->vc_y - 1,
1181 vc->vc_cols);
1182 }
1183 break;
1184 case 1: /* erase from start to cursor */
1185 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1186 start = (unsigned short *)vc->vc_origin;
1187 if (DO_UPDATE(vc)) {
1188 /* do in two stages */
1189 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1190 vc->vc_cols);
1191 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1192 vc->vc_x + 1);
1193 }
1194 break;
1195 case 2: /* erase whole display */
1196 count = vc->vc_cols * vc->vc_rows;
1197 start = (unsigned short *)vc->vc_origin;
1198 if (DO_UPDATE(vc))
1199 vc->vc_sw->con_clear(vc, 0, 0,
1200 vc->vc_rows,
1201 vc->vc_cols);
1202 break;
1203 default:
1204 return;
1205 }
1206 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1207 vc->vc_need_wrap = 0;
1208}
1209
1210static void csi_K(struct vc_data *vc, int vpar)
1211{
1212 unsigned int count;
1213 unsigned short * start;
1214
1215 switch (vpar) {
1216 case 0: /* erase from cursor to end of line */
1217 count = vc->vc_cols - vc->vc_x;
1218 start = (unsigned short *)vc->vc_pos;
1219 if (DO_UPDATE(vc))
1220 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1221 vc->vc_cols - vc->vc_x);
1222 break;
1223 case 1: /* erase from start of line to cursor */
1224 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1225 count = vc->vc_x + 1;
1226 if (DO_UPDATE(vc))
1227 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1228 vc->vc_x + 1);
1229 break;
1230 case 2: /* erase whole line */
1231 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1232 count = vc->vc_cols;
1233 if (DO_UPDATE(vc))
1234 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1235 vc->vc_cols);
1236 break;
1237 default:
1238 return;
1239 }
1240 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1241 vc->vc_need_wrap = 0;
1242}
1243
1244static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1245{ /* not vt100? */
1246 int count;
1247
1248 if (!vpar)
1249 vpar++;
1250 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1251
1252 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1253 if (DO_UPDATE(vc))
1254 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1255 vc->vc_need_wrap = 0;
1256}
1257
1258static void default_attr(struct vc_data *vc)
1259{
1260 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001261 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 vc->vc_underline = 0;
1263 vc->vc_reverse = 0;
1264 vc->vc_blink = 0;
1265 vc->vc_color = vc->vc_def_color;
1266}
1267
1268/* console_sem is held */
1269static void csi_m(struct vc_data *vc)
1270{
1271 int i;
1272
1273 for (i = 0; i <= vc->vc_npar; i++)
1274 switch (vc->vc_par[i]) {
1275 case 0: /* all attributes off */
1276 default_attr(vc);
1277 break;
1278 case 1:
1279 vc->vc_intensity = 2;
1280 break;
1281 case 2:
1282 vc->vc_intensity = 0;
1283 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001284 case 3:
1285 vc->vc_italic = 1;
1286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 case 4:
1288 vc->vc_underline = 1;
1289 break;
1290 case 5:
1291 vc->vc_blink = 1;
1292 break;
1293 case 7:
1294 vc->vc_reverse = 1;
1295 break;
1296 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1297 * Select primary font, don't display
1298 * control chars if defined, don't set
1299 * bit 8 on output.
1300 */
1301 vc->vc_translate = set_translate(vc->vc_charset == 0
1302 ? vc->vc_G0_charset
1303 : vc->vc_G1_charset, vc);
1304 vc->vc_disp_ctrl = 0;
1305 vc->vc_toggle_meta = 0;
1306 break;
1307 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1308 * Select first alternate font, lets
1309 * chars < 32 be displayed as ROM chars.
1310 */
1311 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1312 vc->vc_disp_ctrl = 1;
1313 vc->vc_toggle_meta = 0;
1314 break;
1315 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1316 * Select second alternate font, toggle
1317 * high bit before displaying as ROM char.
1318 */
1319 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1320 vc->vc_disp_ctrl = 1;
1321 vc->vc_toggle_meta = 1;
1322 break;
1323 case 21:
1324 case 22:
1325 vc->vc_intensity = 1;
1326 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001327 case 23:
1328 vc->vc_italic = 0;
1329 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 case 24:
1331 vc->vc_underline = 0;
1332 break;
1333 case 25:
1334 vc->vc_blink = 0;
1335 break;
1336 case 27:
1337 vc->vc_reverse = 0;
1338 break;
1339 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1340 * Enables underscore, white foreground
1341 * with white underscore (Linux - use
1342 * default foreground).
1343 */
1344 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1345 vc->vc_underline = 1;
1346 break;
1347 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1348 * Disable underline option.
1349 * Reset colour to default? It did this
1350 * before...
1351 */
1352 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1353 vc->vc_underline = 0;
1354 break;
1355 case 49:
1356 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1357 break;
1358 default:
1359 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1360 vc->vc_color = color_table[vc->vc_par[i] - 30]
1361 | (vc->vc_color & 0xf0);
1362 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1363 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1364 | (vc->vc_color & 0x0f);
1365 break;
1366 }
1367 update_attr(vc);
1368}
1369
1370static void respond_string(const char *p, struct tty_struct *tty)
1371{
1372 while (*p) {
1373 tty_insert_flip_char(tty, *p, 0);
1374 p++;
1375 }
1376 con_schedule_flip(tty);
1377}
1378
1379static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1380{
1381 char buf[40];
1382
1383 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1384 respond_string(buf, tty);
1385}
1386
1387static inline void status_report(struct tty_struct *tty)
1388{
1389 respond_string("\033[0n", tty); /* Terminal ok */
1390}
1391
1392static inline void respond_ID(struct tty_struct * tty)
1393{
1394 respond_string(VT102ID, tty);
1395}
1396
1397void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1398{
1399 char buf[8];
1400
1401 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1402 (char)('!' + mry));
1403 respond_string(buf, tty);
1404}
1405
1406/* invoked via ioctl(TIOCLINUX) and through set_selection */
1407int mouse_reporting(void)
1408{
1409 return vc_cons[fg_console].d->vc_report_mouse;
1410}
1411
1412/* console_sem is held */
1413static void set_mode(struct vc_data *vc, int on_off)
1414{
1415 int i;
1416
1417 for (i = 0; i <= vc->vc_npar; i++)
1418 if (vc->vc_ques) {
1419 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1420 case 1: /* Cursor keys send ^[Ox/^[[x */
1421 if (on_off)
1422 set_kbd(vc, decckm);
1423 else
1424 clr_kbd(vc, decckm);
1425 break;
1426 case 3: /* 80/132 mode switch unimplemented */
1427 vc->vc_deccolm = on_off;
1428#if 0
1429 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1430 /* this alone does not suffice; some user mode
1431 utility has to change the hardware regs */
1432#endif
1433 break;
1434 case 5: /* Inverted screen on/off */
1435 if (vc->vc_decscnm != on_off) {
1436 vc->vc_decscnm = on_off;
1437 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1438 update_attr(vc);
1439 }
1440 break;
1441 case 6: /* Origin relative/absolute */
1442 vc->vc_decom = on_off;
1443 gotoxay(vc, 0, 0);
1444 break;
1445 case 7: /* Autowrap on/off */
1446 vc->vc_decawm = on_off;
1447 break;
1448 case 8: /* Autorepeat on/off */
1449 if (on_off)
1450 set_kbd(vc, decarm);
1451 else
1452 clr_kbd(vc, decarm);
1453 break;
1454 case 9:
1455 vc->vc_report_mouse = on_off ? 1 : 0;
1456 break;
1457 case 25: /* Cursor on/off */
1458 vc->vc_deccm = on_off;
1459 break;
1460 case 1000:
1461 vc->vc_report_mouse = on_off ? 2 : 0;
1462 break;
1463 }
1464 } else {
1465 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1466 case 3: /* Monitor (display ctrls) */
1467 vc->vc_disp_ctrl = on_off;
1468 break;
1469 case 4: /* Insert Mode on/off */
1470 vc->vc_decim = on_off;
1471 break;
1472 case 20: /* Lf, Enter == CrLf/Lf */
1473 if (on_off)
1474 set_kbd(vc, lnm);
1475 else
1476 clr_kbd(vc, lnm);
1477 break;
1478 }
1479 }
1480}
1481
1482/* console_sem is held */
1483static void setterm_command(struct vc_data *vc)
1484{
1485 switch(vc->vc_par[0]) {
1486 case 1: /* set color for underline mode */
1487 if (vc->vc_can_do_color &&
1488 vc->vc_par[1] < 16) {
1489 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1490 if (vc->vc_underline)
1491 update_attr(vc);
1492 }
1493 break;
1494 case 2: /* set color for half intensity mode */
1495 if (vc->vc_can_do_color &&
1496 vc->vc_par[1] < 16) {
1497 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1498 if (vc->vc_intensity == 0)
1499 update_attr(vc);
1500 }
1501 break;
1502 case 8: /* store colors as defaults */
1503 vc->vc_def_color = vc->vc_attr;
1504 if (vc->vc_hi_font_mask == 0x100)
1505 vc->vc_def_color >>= 1;
1506 default_attr(vc);
1507 update_attr(vc);
1508 break;
1509 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001510 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 poke_blanked_console();
1512 break;
1513 case 10: /* set bell frequency in Hz */
1514 if (vc->vc_npar >= 1)
1515 vc->vc_bell_pitch = vc->vc_par[1];
1516 else
1517 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1518 break;
1519 case 11: /* set bell duration in msec */
1520 if (vc->vc_npar >= 1)
1521 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1522 vc->vc_par[1] * HZ / 1000 : 0;
1523 else
1524 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1525 break;
1526 case 12: /* bring specified console to the front */
1527 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1528 set_console(vc->vc_par[1] - 1);
1529 break;
1530 case 13: /* unblank the screen */
1531 poke_blanked_console();
1532 break;
1533 case 14: /* set vesa powerdown interval */
1534 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1535 break;
1536 case 15: /* activate the previous console */
1537 set_console(last_console);
1538 break;
1539 }
1540}
1541
1542/* console_sem is held */
1543static void csi_at(struct vc_data *vc, unsigned int nr)
1544{
1545 if (nr > vc->vc_cols - vc->vc_x)
1546 nr = vc->vc_cols - vc->vc_x;
1547 else if (!nr)
1548 nr = 1;
1549 insert_char(vc, nr);
1550}
1551
1552/* console_sem is held */
1553static void csi_L(struct vc_data *vc, unsigned int nr)
1554{
1555 if (nr > vc->vc_rows - vc->vc_y)
1556 nr = vc->vc_rows - vc->vc_y;
1557 else if (!nr)
1558 nr = 1;
1559 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1560 vc->vc_need_wrap = 0;
1561}
1562
1563/* console_sem is held */
1564static void csi_P(struct vc_data *vc, unsigned int nr)
1565{
1566 if (nr > vc->vc_cols - vc->vc_x)
1567 nr = vc->vc_cols - vc->vc_x;
1568 else if (!nr)
1569 nr = 1;
1570 delete_char(vc, nr);
1571}
1572
1573/* console_sem is held */
1574static void csi_M(struct vc_data *vc, unsigned int nr)
1575{
1576 if (nr > vc->vc_rows - vc->vc_y)
1577 nr = vc->vc_rows - vc->vc_y;
1578 else if (!nr)
1579 nr=1;
1580 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1581 vc->vc_need_wrap = 0;
1582}
1583
1584/* console_sem is held (except via vc_init->reset_terminal */
1585static void save_cur(struct vc_data *vc)
1586{
1587 vc->vc_saved_x = vc->vc_x;
1588 vc->vc_saved_y = vc->vc_y;
1589 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001590 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 vc->vc_s_underline = vc->vc_underline;
1592 vc->vc_s_blink = vc->vc_blink;
1593 vc->vc_s_reverse = vc->vc_reverse;
1594 vc->vc_s_charset = vc->vc_charset;
1595 vc->vc_s_color = vc->vc_color;
1596 vc->vc_saved_G0 = vc->vc_G0_charset;
1597 vc->vc_saved_G1 = vc->vc_G1_charset;
1598}
1599
1600/* console_sem is held */
1601static void restore_cur(struct vc_data *vc)
1602{
1603 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1604 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001605 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 vc->vc_underline = vc->vc_s_underline;
1607 vc->vc_blink = vc->vc_s_blink;
1608 vc->vc_reverse = vc->vc_s_reverse;
1609 vc->vc_charset = vc->vc_s_charset;
1610 vc->vc_color = vc->vc_s_color;
1611 vc->vc_G0_charset = vc->vc_saved_G0;
1612 vc->vc_G1_charset = vc->vc_saved_G1;
1613 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1614 update_attr(vc);
1615 vc->vc_need_wrap = 0;
1616}
1617
1618enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1619 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1620 ESpalette };
1621
1622/* console_sem is held (except via vc_init()) */
1623static void reset_terminal(struct vc_data *vc, int do_clear)
1624{
1625 vc->vc_top = 0;
1626 vc->vc_bottom = vc->vc_rows;
1627 vc->vc_state = ESnormal;
1628 vc->vc_ques = 0;
1629 vc->vc_translate = set_translate(LAT1_MAP, vc);
1630 vc->vc_G0_charset = LAT1_MAP;
1631 vc->vc_G1_charset = GRAF_MAP;
1632 vc->vc_charset = 0;
1633 vc->vc_need_wrap = 0;
1634 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001635 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 vc->vc_utf_count = 0;
1637
1638 vc->vc_disp_ctrl = 0;
1639 vc->vc_toggle_meta = 0;
1640
1641 vc->vc_decscnm = 0;
1642 vc->vc_decom = 0;
1643 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001644 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 vc->vc_decim = 0;
1646
1647 set_kbd(vc, decarm);
1648 clr_kbd(vc, decckm);
1649 clr_kbd(vc, kbdapplic);
1650 clr_kbd(vc, lnm);
1651 kbd_table[vc->vc_num].lockstate = 0;
1652 kbd_table[vc->vc_num].slockstate = 0;
1653 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1654 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1655 /* do not do set_leds here because this causes an endless tasklet loop
1656 when the keyboard hasn't been initialized yet */
1657
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001658 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 vc->vc_complement_mask = vc->vc_s_complement_mask;
1660
1661 default_attr(vc);
1662 update_attr(vc);
1663
1664 vc->vc_tab_stop[0] = 0x01010100;
1665 vc->vc_tab_stop[1] =
1666 vc->vc_tab_stop[2] =
1667 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001668 vc->vc_tab_stop[4] =
1669 vc->vc_tab_stop[5] =
1670 vc->vc_tab_stop[6] =
1671 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1674 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1675
1676 gotoxy(vc, 0, 0);
1677 save_cur(vc);
1678 if (do_clear)
1679 csi_J(vc, 2);
1680}
1681
1682/* console_sem is held */
1683static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1684{
1685 /*
1686 * Control characters can be used in the _middle_
1687 * of an escape sequence.
1688 */
1689 switch (c) {
1690 case 0:
1691 return;
1692 case 7:
1693 if (vc->vc_bell_duration)
1694 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1695 return;
1696 case 8:
1697 bs(vc);
1698 return;
1699 case 9:
1700 vc->vc_pos -= (vc->vc_x << 1);
1701 while (vc->vc_x < vc->vc_cols - 1) {
1702 vc->vc_x++;
1703 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1704 break;
1705 }
1706 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001707 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 return;
1709 case 10: case 11: case 12:
1710 lf(vc);
1711 if (!is_kbd(vc, lnm))
1712 return;
1713 case 13:
1714 cr(vc);
1715 return;
1716 case 14:
1717 vc->vc_charset = 1;
1718 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1719 vc->vc_disp_ctrl = 1;
1720 return;
1721 case 15:
1722 vc->vc_charset = 0;
1723 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1724 vc->vc_disp_ctrl = 0;
1725 return;
1726 case 24: case 26:
1727 vc->vc_state = ESnormal;
1728 return;
1729 case 27:
1730 vc->vc_state = ESesc;
1731 return;
1732 case 127:
1733 del(vc);
1734 return;
1735 case 128+27:
1736 vc->vc_state = ESsquare;
1737 return;
1738 }
1739 switch(vc->vc_state) {
1740 case ESesc:
1741 vc->vc_state = ESnormal;
1742 switch (c) {
1743 case '[':
1744 vc->vc_state = ESsquare;
1745 return;
1746 case ']':
1747 vc->vc_state = ESnonstd;
1748 return;
1749 case '%':
1750 vc->vc_state = ESpercent;
1751 return;
1752 case 'E':
1753 cr(vc);
1754 lf(vc);
1755 return;
1756 case 'M':
1757 ri(vc);
1758 return;
1759 case 'D':
1760 lf(vc);
1761 return;
1762 case 'H':
1763 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1764 return;
1765 case 'Z':
1766 respond_ID(tty);
1767 return;
1768 case '7':
1769 save_cur(vc);
1770 return;
1771 case '8':
1772 restore_cur(vc);
1773 return;
1774 case '(':
1775 vc->vc_state = ESsetG0;
1776 return;
1777 case ')':
1778 vc->vc_state = ESsetG1;
1779 return;
1780 case '#':
1781 vc->vc_state = EShash;
1782 return;
1783 case 'c':
1784 reset_terminal(vc, 1);
1785 return;
1786 case '>': /* Numeric keypad */
1787 clr_kbd(vc, kbdapplic);
1788 return;
1789 case '=': /* Appl. keypad */
1790 set_kbd(vc, kbdapplic);
1791 return;
1792 }
1793 return;
1794 case ESnonstd:
1795 if (c=='P') { /* palette escape sequence */
1796 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1797 vc->vc_par[vc->vc_npar] = 0;
1798 vc->vc_npar = 0;
1799 vc->vc_state = ESpalette;
1800 return;
1801 } else if (c=='R') { /* reset palette */
1802 reset_palette(vc);
1803 vc->vc_state = ESnormal;
1804 } else
1805 vc->vc_state = ESnormal;
1806 return;
1807 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001808 if (isxdigit(c)) {
1809 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 if (vc->vc_npar == 7) {
1811 int i = vc->vc_par[0] * 3, j = 1;
1812 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1813 vc->vc_palette[i++] += vc->vc_par[j++];
1814 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1815 vc->vc_palette[i++] += vc->vc_par[j++];
1816 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1817 vc->vc_palette[i] += vc->vc_par[j];
1818 set_palette(vc);
1819 vc->vc_state = ESnormal;
1820 }
1821 } else
1822 vc->vc_state = ESnormal;
1823 return;
1824 case ESsquare:
1825 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1826 vc->vc_par[vc->vc_npar] = 0;
1827 vc->vc_npar = 0;
1828 vc->vc_state = ESgetpars;
1829 if (c == '[') { /* Function key */
1830 vc->vc_state=ESfunckey;
1831 return;
1832 }
1833 vc->vc_ques = (c == '?');
1834 if (vc->vc_ques)
1835 return;
1836 case ESgetpars:
1837 if (c == ';' && vc->vc_npar < NPAR - 1) {
1838 vc->vc_npar++;
1839 return;
1840 } else if (c>='0' && c<='9') {
1841 vc->vc_par[vc->vc_npar] *= 10;
1842 vc->vc_par[vc->vc_npar] += c - '0';
1843 return;
1844 } else
1845 vc->vc_state = ESgotpars;
1846 case ESgotpars:
1847 vc->vc_state = ESnormal;
1848 switch(c) {
1849 case 'h':
1850 set_mode(vc, 1);
1851 return;
1852 case 'l':
1853 set_mode(vc, 0);
1854 return;
1855 case 'c':
1856 if (vc->vc_ques) {
1857 if (vc->vc_par[0])
1858 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1859 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001860 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return;
1862 }
1863 break;
1864 case 'm':
1865 if (vc->vc_ques) {
1866 clear_selection();
1867 if (vc->vc_par[0])
1868 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1869 else
1870 vc->vc_complement_mask = vc->vc_s_complement_mask;
1871 return;
1872 }
1873 break;
1874 case 'n':
1875 if (!vc->vc_ques) {
1876 if (vc->vc_par[0] == 5)
1877 status_report(tty);
1878 else if (vc->vc_par[0] == 6)
1879 cursor_report(vc, tty);
1880 }
1881 return;
1882 }
1883 if (vc->vc_ques) {
1884 vc->vc_ques = 0;
1885 return;
1886 }
1887 switch(c) {
1888 case 'G': case '`':
1889 if (vc->vc_par[0])
1890 vc->vc_par[0]--;
1891 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1892 return;
1893 case 'A':
1894 if (!vc->vc_par[0])
1895 vc->vc_par[0]++;
1896 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1897 return;
1898 case 'B': case 'e':
1899 if (!vc->vc_par[0])
1900 vc->vc_par[0]++;
1901 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1902 return;
1903 case 'C': case 'a':
1904 if (!vc->vc_par[0])
1905 vc->vc_par[0]++;
1906 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1907 return;
1908 case 'D':
1909 if (!vc->vc_par[0])
1910 vc->vc_par[0]++;
1911 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1912 return;
1913 case 'E':
1914 if (!vc->vc_par[0])
1915 vc->vc_par[0]++;
1916 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1917 return;
1918 case 'F':
1919 if (!vc->vc_par[0])
1920 vc->vc_par[0]++;
1921 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1922 return;
1923 case 'd':
1924 if (vc->vc_par[0])
1925 vc->vc_par[0]--;
1926 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1927 return;
1928 case 'H': case 'f':
1929 if (vc->vc_par[0])
1930 vc->vc_par[0]--;
1931 if (vc->vc_par[1])
1932 vc->vc_par[1]--;
1933 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1934 return;
1935 case 'J':
1936 csi_J(vc, vc->vc_par[0]);
1937 return;
1938 case 'K':
1939 csi_K(vc, vc->vc_par[0]);
1940 return;
1941 case 'L':
1942 csi_L(vc, vc->vc_par[0]);
1943 return;
1944 case 'M':
1945 csi_M(vc, vc->vc_par[0]);
1946 return;
1947 case 'P':
1948 csi_P(vc, vc->vc_par[0]);
1949 return;
1950 case 'c':
1951 if (!vc->vc_par[0])
1952 respond_ID(tty);
1953 return;
1954 case 'g':
1955 if (!vc->vc_par[0])
1956 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1957 else if (vc->vc_par[0] == 3) {
1958 vc->vc_tab_stop[0] =
1959 vc->vc_tab_stop[1] =
1960 vc->vc_tab_stop[2] =
1961 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001962 vc->vc_tab_stop[4] =
1963 vc->vc_tab_stop[5] =
1964 vc->vc_tab_stop[6] =
1965 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 }
1967 return;
1968 case 'm':
1969 csi_m(vc);
1970 return;
1971 case 'q': /* DECLL - but only 3 leds */
1972 /* map 0,1,2,3 to 0,1,2,4 */
1973 if (vc->vc_par[0] < 4)
1974 setledstate(kbd_table + vc->vc_num,
1975 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1976 return;
1977 case 'r':
1978 if (!vc->vc_par[0])
1979 vc->vc_par[0]++;
1980 if (!vc->vc_par[1])
1981 vc->vc_par[1] = vc->vc_rows;
1982 /* Minimum allowed region is 2 lines */
1983 if (vc->vc_par[0] < vc->vc_par[1] &&
1984 vc->vc_par[1] <= vc->vc_rows) {
1985 vc->vc_top = vc->vc_par[0] - 1;
1986 vc->vc_bottom = vc->vc_par[1];
1987 gotoxay(vc, 0, 0);
1988 }
1989 return;
1990 case 's':
1991 save_cur(vc);
1992 return;
1993 case 'u':
1994 restore_cur(vc);
1995 return;
1996 case 'X':
1997 csi_X(vc, vc->vc_par[0]);
1998 return;
1999 case '@':
2000 csi_at(vc, vc->vc_par[0]);
2001 return;
2002 case ']': /* setterm functions */
2003 setterm_command(vc);
2004 return;
2005 }
2006 return;
2007 case ESpercent:
2008 vc->vc_state = ESnormal;
2009 switch (c) {
2010 case '@': /* defined in ISO 2022 */
2011 vc->vc_utf = 0;
2012 return;
2013 case 'G': /* prelim official escape code */
2014 case '8': /* retained for compatibility */
2015 vc->vc_utf = 1;
2016 return;
2017 }
2018 return;
2019 case ESfunckey:
2020 vc->vc_state = ESnormal;
2021 return;
2022 case EShash:
2023 vc->vc_state = ESnormal;
2024 if (c == '8') {
2025 /* DEC screen alignment test. kludge :-) */
2026 vc->vc_video_erase_char =
2027 (vc->vc_video_erase_char & 0xff00) | 'E';
2028 csi_J(vc, 2);
2029 vc->vc_video_erase_char =
2030 (vc->vc_video_erase_char & 0xff00) | ' ';
2031 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2032 }
2033 return;
2034 case ESsetG0:
2035 if (c == '0')
2036 vc->vc_G0_charset = GRAF_MAP;
2037 else if (c == 'B')
2038 vc->vc_G0_charset = LAT1_MAP;
2039 else if (c == 'U')
2040 vc->vc_G0_charset = IBMPC_MAP;
2041 else if (c == 'K')
2042 vc->vc_G0_charset = USER_MAP;
2043 if (vc->vc_charset == 0)
2044 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2045 vc->vc_state = ESnormal;
2046 return;
2047 case ESsetG1:
2048 if (c == '0')
2049 vc->vc_G1_charset = GRAF_MAP;
2050 else if (c == 'B')
2051 vc->vc_G1_charset = LAT1_MAP;
2052 else if (c == 'U')
2053 vc->vc_G1_charset = IBMPC_MAP;
2054 else if (c == 'K')
2055 vc->vc_G1_charset = USER_MAP;
2056 if (vc->vc_charset == 1)
2057 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2058 vc->vc_state = ESnormal;
2059 return;
2060 default:
2061 vc->vc_state = ESnormal;
2062 }
2063}
2064
2065/* This is a temporary buffer used to prepare a tty console write
2066 * so that we can easily avoid touching user space while holding the
2067 * console spinlock. It is allocated in con_init and is shared by
2068 * this code and the vc_screen read/write tty calls.
2069 *
2070 * We have to allocate this statically in the kernel data section
2071 * since console_init (and thus con_init) are called before any
2072 * kernel memory allocation is available.
2073 */
2074char con_buf[CON_BUF_SIZE];
Matthias Kaehlckec831c332007-05-08 00:39:49 -07002075DEFINE_MUTEX(con_buf_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002077/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002078 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002079 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2080 */
2081struct interval {
2082 uint32_t first;
2083 uint32_t last;
2084};
2085
2086static int bisearch(uint32_t ucs, const struct interval *table, int max)
2087{
2088 int min = 0;
2089 int mid;
2090
2091 if (ucs < table[0].first || ucs > table[max].last)
2092 return 0;
2093 while (max >= min) {
2094 mid = (min + max) / 2;
2095 if (ucs > table[mid].last)
2096 min = mid + 1;
2097 else if (ucs < table[mid].first)
2098 max = mid - 1;
2099 else
2100 return 1;
2101 }
2102 return 0;
2103}
2104
2105static int is_double_width(uint32_t ucs)
2106{
2107 static const struct interval double_width[] = {
2108 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2109 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002110 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2111 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002112 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002113 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002114}
2115
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116/* acquires console_sem */
2117static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2118{
2119#ifdef VT_BUF_VRAM_ONLY
2120#define FLUSH do { } while(0);
2121#else
2122#define FLUSH if (draw_x >= 0) { \
2123 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2124 draw_x = -1; \
2125 }
2126#endif
2127
2128 int c, tc, ok, n = 0, draw_x = -1;
2129 unsigned int currcons;
2130 unsigned long draw_from = 0, draw_to = 0;
2131 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002132 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002133 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002134 uint8_t rescan;
2135 uint8_t inverse;
2136 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 if (in_interrupt())
2140 return count;
2141
2142 might_sleep();
2143
2144 acquire_console_sem();
2145 vc = tty->driver_data;
2146 if (vc == NULL) {
2147 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2148 release_console_sem();
2149 return 0;
2150 }
2151
2152 currcons = vc->vc_num;
2153 if (!vc_cons_allocated(currcons)) {
2154 /* could this happen? */
Marcin Slusarz9074d962009-08-09 21:54:03 +02002155 printk_once("con_write: tty %d not allocated\n", currcons+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 release_console_sem();
2157 return 0;
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 himask = vc->vc_hi_font_mask;
2161 charmask = himask ? 0x1ff : 0xff;
2162
2163 /* undraw cursor first */
2164 if (IS_FG(vc))
2165 hide_cursor(vc);
2166
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002167 param.vc = vc;
2168
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 while (!tty->stopped && count) {
2170 int orig = *buf;
2171 c = orig;
2172 buf++;
2173 n++;
2174 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002175 rescan = 0;
2176 inverse = 0;
2177 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 /* Do no translation at all in control states */
2180 if (vc->vc_state != ESnormal) {
2181 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002182 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002183 /* Combine UTF-8 into Unicode in vc_utf_char.
2184 * vc_utf_count is the number of continuation bytes still
2185 * expected to arrive.
2186 * vc_npar is the number of continuation bytes arrived so
2187 * far
2188 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002189rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002190 if ((c & 0xc0) == 0x80) {
2191 /* Continuation byte received */
2192 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002193 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002194 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2195 vc->vc_npar++;
2196 if (--vc->vc_utf_count) {
2197 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002199 }
2200 /* Got a whole character */
2201 c = vc->vc_utf_char;
2202 /* Reject overlong sequences */
2203 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2204 c > utf8_length_changes[vc->vc_npar])
2205 c = 0xfffd;
2206 } else {
2207 /* Unexpected continuation byte */
2208 vc->vc_utf_count = 0;
2209 c = 0xfffd;
2210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002212 /* Single ASCII byte or first byte of a sequence received */
2213 if (vc->vc_utf_count) {
2214 /* Continuation byte expected */
2215 rescan = 1;
2216 vc->vc_utf_count = 0;
2217 c = 0xfffd;
2218 } else if (c > 0x7f) {
2219 /* First byte of a multibyte sequence received */
2220 vc->vc_npar = 0;
2221 if ((c & 0xe0) == 0xc0) {
2222 vc->vc_utf_count = 1;
2223 vc->vc_utf_char = (c & 0x1f);
2224 } else if ((c & 0xf0) == 0xe0) {
2225 vc->vc_utf_count = 2;
2226 vc->vc_utf_char = (c & 0x0f);
2227 } else if ((c & 0xf8) == 0xf0) {
2228 vc->vc_utf_count = 3;
2229 vc->vc_utf_char = (c & 0x07);
2230 } else if ((c & 0xfc) == 0xf8) {
2231 vc->vc_utf_count = 4;
2232 vc->vc_utf_char = (c & 0x03);
2233 } else if ((c & 0xfe) == 0xfc) {
2234 vc->vc_utf_count = 5;
2235 vc->vc_utf_char = (c & 0x01);
2236 } else {
2237 /* 254 and 255 are invalid */
2238 c = 0xfffd;
2239 }
2240 if (vc->vc_utf_count) {
2241 /* Still need some bytes */
2242 continue;
2243 }
2244 }
2245 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002247 /* End of UTF-8 decoding. */
2248 /* c is the received character, or U+FFFD for invalid sequences. */
2249 /* Replace invalid Unicode code points with U+FFFD too */
2250 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2251 c = 0xfffd;
2252 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002253 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002254 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002257 param.c = tc;
2258 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2259 &param) == NOTIFY_STOP)
2260 continue;
2261
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 /* If the original code was a control character we
2263 * only allow a glyph to be displayed if the code is
2264 * not normally used (such as for cursor movement) or
2265 * if the disp_ctrl mode has been explicitly enabled.
2266 * Certain characters (as given by the CTRL_ALWAYS
2267 * bitmap) are always displayed as control characters,
2268 * as the console would be pretty useless without
2269 * them; to display an arbitrary font position use the
2270 * direct-to-font zone in UTF-8 mode.
2271 */
2272 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002273 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2274 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 && (c != 127 || vc->vc_disp_ctrl)
2276 && (c != 128+27);
2277
2278 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002279 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2280 if (is_double_width(c))
2281 width = 2;
2282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /* Now try to find out how to display it */
2284 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002285 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002286 if (tc == -1 || tc == -2) {
2287 continue; /* nothing to display */
2288 }
2289 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002290 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002291 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002292 This would make absolutely no sense with Unicode in mind,
2293 but do this for ASCII characters since a font may lack
2294 Unicode mapping info and we don't want to end up with
2295 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002296 tc = c;
2297 } else {
2298 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2299 tc = conv_uni_to_pc(vc, 0xfffd);
2300 if (tc < 0) {
2301 inverse = 1;
2302 tc = conv_uni_to_pc(vc, '?');
2303 if (tc < 0) tc = '?';
2304 }
2305 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002308 if (!inverse) {
2309 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002311 /* invert vc_attr */
2312 if (!vc->vc_can_do_color) {
2313 vc_attr = (vc->vc_attr) ^ 0x08;
2314 } else if (vc->vc_hi_font_mask == 0x100) {
2315 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2316 } else {
2317 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002318 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002319 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002320 }
2321
2322 while (1) {
2323 if (vc->vc_need_wrap || vc->vc_decim)
2324 FLUSH
2325 if (vc->vc_need_wrap) {
2326 cr(vc);
2327 lf(vc);
2328 }
2329 if (vc->vc_decim)
2330 insert_char(vc, 1);
2331 scr_writew(himask ?
2332 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2333 (vc_attr << 8) + tc,
2334 (u16 *) vc->vc_pos);
2335 if (DO_UPDATE(vc) && draw_x < 0) {
2336 draw_x = vc->vc_x;
2337 draw_from = vc->vc_pos;
2338 }
2339 if (vc->vc_x == vc->vc_cols - 1) {
2340 vc->vc_need_wrap = vc->vc_decawm;
2341 draw_to = vc->vc_pos + 2;
2342 } else {
2343 vc->vc_x++;
2344 draw_to = (vc->vc_pos += 2);
2345 }
2346
2347 if (!--width) break;
2348
2349 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2350 if (tc < 0) tc = ' ';
2351 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002352 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002353
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002354 if (inverse) {
2355 FLUSH
2356 }
2357
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002358 if (rescan) {
2359 rescan = 0;
2360 inverse = 0;
2361 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002362 c = orig;
2363 goto rescan_last_byte;
2364 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 continue;
2366 }
2367 FLUSH
2368 do_con_trol(tty, vc, orig);
2369 }
2370 FLUSH
2371 console_conditional_schedule();
2372 release_console_sem();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002373 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return n;
2375#undef FLUSH
2376}
2377
2378/*
2379 * This is the console switching callback.
2380 *
2381 * Doing console switching in a process context allows
2382 * us to do the switches asynchronously (needed when we want
2383 * to switch due to a keyboard interrupt). Synchronization
2384 * with other console code and prevention of re-entrancy is
2385 * ensured with console_sem.
2386 */
David Howells65f27f32006-11-22 14:55:48 +00002387static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
2389 acquire_console_sem();
2390
2391 if (want_console >= 0) {
2392 if (want_console != fg_console &&
2393 vc_cons_allocated(want_console)) {
2394 hide_cursor(vc_cons[fg_console].d);
2395 change_console(vc_cons[want_console].d);
2396 /* we only changed when the console had already
2397 been allocated - a new console is not created
2398 in an interrupt routine */
2399 }
2400 want_console = -1;
2401 }
2402 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2403 do_poke_blanked_console = 0;
2404 poke_blanked_console();
2405 }
2406 if (scrollback_delta) {
2407 struct vc_data *vc = vc_cons[fg_console].d;
2408 clear_selection();
2409 if (vc->vc_mode == KD_TEXT)
2410 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2411 scrollback_delta = 0;
2412 }
2413 if (blank_timer_expired) {
2414 do_blank_screen(0);
2415 blank_timer_expired = 0;
2416 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002417 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 release_console_sem();
2420}
2421
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002422int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002424 struct vc_data *vc = vc_cons[fg_console].d;
2425
2426 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2427 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2428
2429 /*
2430 * Console switch will fail in console_callback() or
2431 * change_console() so there is no point scheduling
2432 * the callback
2433 *
2434 * Existing set_console() users don't check the return
2435 * value so this shouldn't break anything
2436 */
2437 return -EINVAL;
2438 }
2439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 want_console = nr;
2441 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002442
2443 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444}
2445
2446struct tty_driver *console_driver;
2447
2448#ifdef CONFIG_VT_CONSOLE
2449
Bernhard Walle5ada9182009-12-14 18:00:43 -08002450/**
2451 * vt_kmsg_redirect() - Sets/gets the kernel message console
2452 * @new: The new virtual terminal number or -1 if the console should stay
2453 * unchanged
2454 *
2455 * By default, the kernel messages are always printed on the current virtual
2456 * console. However, the user may modify that default with the
2457 * TIOCL_SETKMSGREDIRECT ioctl call.
2458 *
2459 * This function sets the kernel message console to be @new. It returns the old
2460 * virtual console number. The virtual terminal number 0 (both as parameter and
2461 * return value) means no redirection (i.e. always printed on the currently
2462 * active console).
2463 *
2464 * The parameter -1 means that only the current console is returned, but the
2465 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2466 * case to make the code more understandable.
2467 *
2468 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2469 * the parameter and always returns 0.
2470 */
2471int vt_kmsg_redirect(int new)
2472{
2473 static int kmsg_con;
2474
2475 if (new != -1)
2476 return xchg(&kmsg_con, new);
2477 else
2478 return kmsg_con;
2479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
2482 * Console on virtual terminal
2483 *
2484 * The console must be locked when we get here.
2485 */
2486
2487static void vt_console_print(struct console *co, const char *b, unsigned count)
2488{
2489 struct vc_data *vc = vc_cons[fg_console].d;
2490 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002491 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 const ushort *start;
2493 ushort cnt = 0;
2494 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002495 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496
2497 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002498 if (!printable)
2499 return;
2500 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 return;
2502
Bernhard Walle5ada9182009-12-14 18:00:43 -08002503 kmsg_console = vt_get_kmsg_redirect();
2504 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2505 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
2507 /* read `x' only after setting currcons properly (otherwise
2508 the `x' macro will read the x of the foreground console). */
2509 myx = vc->vc_x;
2510
2511 if (!vc_cons_allocated(fg_console)) {
2512 /* impossible */
2513 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2514 goto quit;
2515 }
2516
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002517 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 goto quit;
2519
2520 /* undraw cursor first */
2521 if (IS_FG(vc))
2522 hide_cursor(vc);
2523
2524 start = (ushort *)vc->vc_pos;
2525
2526 /* Contrived structure to try to emulate original need_wrap behaviour
2527 * Problems caused when we have need_wrap set on '\n' character */
2528 while (count--) {
2529 c = *b++;
2530 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2531 if (cnt > 0) {
2532 if (CON_IS_VISIBLE(vc))
2533 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2534 vc->vc_x += cnt;
2535 if (vc->vc_need_wrap)
2536 vc->vc_x--;
2537 cnt = 0;
2538 }
2539 if (c == 8) { /* backspace */
2540 bs(vc);
2541 start = (ushort *)vc->vc_pos;
2542 myx = vc->vc_x;
2543 continue;
2544 }
2545 if (c != 13)
2546 lf(vc);
2547 cr(vc);
2548 start = (ushort *)vc->vc_pos;
2549 myx = vc->vc_x;
2550 if (c == 10 || c == 13)
2551 continue;
2552 }
2553 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002554 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555 cnt++;
2556 if (myx == vc->vc_cols - 1) {
2557 vc->vc_need_wrap = 1;
2558 continue;
2559 }
2560 vc->vc_pos += 2;
2561 myx++;
2562 }
2563 if (cnt > 0) {
2564 if (CON_IS_VISIBLE(vc))
2565 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2566 vc->vc_x += cnt;
2567 if (vc->vc_x == vc->vc_cols) {
2568 vc->vc_x--;
2569 vc->vc_need_wrap = 1;
2570 }
2571 }
2572 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002573 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
2575quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002576 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
2579static struct tty_driver *vt_console_device(struct console *c, int *index)
2580{
2581 *index = c->index ? c->index-1 : fg_console;
2582 return console_driver;
2583}
2584
2585static struct console vt_console_driver = {
2586 .name = "tty",
2587 .write = vt_console_print,
2588 .device = vt_console_device,
2589 .unblank = unblank_screen,
2590 .flags = CON_PRINTBUFFER,
2591 .index = -1,
2592};
2593#endif
2594
2595/*
2596 * Handling of Linux-specific VC ioctls
2597 */
2598
2599/*
2600 * Generally a bit racy with respect to console_sem().
2601 *
2602 * There are some functions which don't need it.
2603 *
2604 * There are some functions which can sleep for arbitrary periods
2605 * (paste_selection) but we don't need the lock there anyway.
2606 *
2607 * set_selection has locking, and definitely needs it
2608 */
2609
2610int tioclinux(struct tty_struct *tty, unsigned long arg)
2611{
2612 char type, data;
2613 char __user *p = (char __user *)arg;
2614 int lines;
2615 int ret;
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2618 return -EPERM;
2619 if (get_user(type, p))
2620 return -EFAULT;
2621 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002622
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 switch (type)
2624 {
2625 case TIOCL_SETSEL:
2626 acquire_console_sem();
2627 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2628 release_console_sem();
2629 break;
2630 case TIOCL_PASTESEL:
2631 ret = paste_selection(tty);
2632 break;
2633 case TIOCL_UNBLANKSCREEN:
Stephane Doyon2d237c62005-09-06 15:17:31 -07002634 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 unblank_screen();
Stephane Doyon2d237c62005-09-06 15:17:31 -07002636 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 break;
2638 case TIOCL_SELLOADLUT:
2639 ret = sel_loadlut(p);
2640 break;
2641 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * Make it possible to react to Shift+Mousebutton.
2645 * Note that 'shift_state' is an undocumented
2646 * kernel-internal variable; programs not closely
2647 * related to the kernel should not use this.
2648 */
2649 data = shift_state;
2650 ret = __put_user(data, p);
2651 break;
2652 case TIOCL_GETMOUSEREPORTING:
2653 data = mouse_reporting();
2654 ret = __put_user(data, p);
2655 break;
2656 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002657 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002659 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002660 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002661 ret = __put_user(data, p);
2662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 case TIOCL_SETKMSGREDIRECT:
2664 if (!capable(CAP_SYS_ADMIN)) {
2665 ret = -EPERM;
2666 } else {
2667 if (get_user(data, p+1))
2668 ret = -EFAULT;
2669 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002670 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
2672 break;
2673 case TIOCL_GETFGCONSOLE:
2674 ret = fg_console;
2675 break;
2676 case TIOCL_SCROLLCONSOLE:
2677 if (get_user(lines, (s32 __user *)(p+4))) {
2678 ret = -EFAULT;
2679 } else {
2680 scrollfront(vc_cons[fg_console].d, lines);
2681 ret = 0;
2682 }
2683 break;
2684 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Stephane Doyon2d237c62005-09-06 15:17:31 -07002685 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 ignore_poke = 1;
2687 do_blank_screen(0);
Stephane Doyon2d237c62005-09-06 15:17:31 -07002688 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 break;
2690 case TIOCL_BLANKEDSCREEN:
2691 ret = console_blanked;
2692 break;
2693 default:
2694 ret = -EINVAL;
2695 break;
2696 }
2697 return ret;
2698}
2699
2700/*
2701 * /dev/ttyN handling
2702 */
2703
2704static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2705{
2706 int retval;
2707
2708 retval = do_con_write(tty, buf, count);
2709 con_flush_chars(tty);
2710
2711 return retval;
2712}
2713
Alan Cox5d19f542008-04-30 00:54:08 -07002714static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
2716 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002717 return 0; /* n_r3964 calls put_char() from interrupt context */
2718 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719}
2720
2721static int con_write_room(struct tty_struct *tty)
2722{
2723 if (tty->stopped)
2724 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002725 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726}
2727
2728static int con_chars_in_buffer(struct tty_struct *tty)
2729{
2730 return 0; /* we're not buffering */
2731}
2732
2733/*
2734 * con_throttle and con_unthrottle are only used for
2735 * paste_selection(), which has to stuff in a large number of
2736 * characters...
2737 */
2738static void con_throttle(struct tty_struct *tty)
2739{
2740}
2741
2742static void con_unthrottle(struct tty_struct *tty)
2743{
2744 struct vc_data *vc = tty->driver_data;
2745
2746 wake_up_interruptible(&vc->paste_wait);
2747}
2748
2749/*
2750 * Turn the Scroll-Lock LED on when the tty is stopped
2751 */
2752static void con_stop(struct tty_struct *tty)
2753{
2754 int console_num;
2755 if (!tty)
2756 return;
2757 console_num = tty->index;
2758 if (!vc_cons_allocated(console_num))
2759 return;
2760 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2761 set_leds();
2762}
2763
2764/*
2765 * Turn the Scroll-Lock LED off when the console is started
2766 */
2767static void con_start(struct tty_struct *tty)
2768{
2769 int console_num;
2770 if (!tty)
2771 return;
2772 console_num = tty->index;
2773 if (!vc_cons_allocated(console_num))
2774 return;
2775 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2776 set_leds();
2777}
2778
2779static void con_flush_chars(struct tty_struct *tty)
2780{
2781 struct vc_data *vc;
2782
2783 if (in_interrupt()) /* from flush_to_ldisc */
2784 return;
2785
2786 /* if we race with con_close(), vt may be null */
2787 acquire_console_sem();
2788 vc = tty->driver_data;
2789 if (vc)
2790 set_cursor(vc);
2791 release_console_sem();
2792}
2793
2794/*
2795 * Allocate the console screen memory.
2796 */
2797static int con_open(struct tty_struct *tty, struct file *filp)
2798{
2799 unsigned int currcons = tty->index;
2800 int ret = 0;
2801
2802 acquire_console_sem();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002803 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 ret = vc_allocate(currcons);
2805 if (ret == 0) {
2806 struct vc_data *vc = vc_cons[currcons].d;
Alan Coxfeebed62008-10-13 10:41:30 +01002807
2808 /* Still being freed */
2809 if (vc->vc_tty) {
2810 release_console_sem();
2811 return -ERESTARTSYS;
2812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 tty->driver_data = vc;
2814 vc->vc_tty = tty;
2815
2816 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2817 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2818 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2819 }
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002820 if (vc->vc_utf)
2821 tty->termios->c_iflag |= IUTF8;
2822 else
2823 tty->termios->c_iflag &= ~IUTF8;
Jiri Slabye0426e62008-07-23 21:29:58 -07002824 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 return ret;
2826 }
2827 }
2828 release_console_sem();
2829 return ret;
2830}
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832static void con_close(struct tty_struct *tty, struct file *filp)
2833{
Alan Coxfeebed62008-10-13 10:41:30 +01002834 /* Nothing to do - we defer to shutdown */
2835}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Alan Coxfeebed62008-10-13 10:41:30 +01002837static void con_shutdown(struct tty_struct *tty)
2838{
2839 struct vc_data *vc = tty->driver_data;
2840 BUG_ON(vc == NULL);
2841 acquire_console_sem();
2842 vc->vc_tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 release_console_sem();
Alan Coxfeebed62008-10-13 10:41:30 +01002844 tty_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845}
2846
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002847static int default_italic_color = 2; // green (ASCII)
2848static int default_underline_color = 3; // cyan (ASCII)
2849module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2850module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2851
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852static void vc_init(struct vc_data *vc, unsigned int rows,
2853 unsigned int cols, int do_clear)
2854{
2855 int j, k ;
2856
2857 vc->vc_cols = cols;
2858 vc->vc_rows = rows;
2859 vc->vc_size_row = cols << 1;
2860 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2861
2862 set_origin(vc);
2863 vc->vc_pos = vc->vc_origin;
2864 reset_vc(vc);
2865 for (j=k=0; j<16; j++) {
2866 vc->vc_palette[k++] = default_red[j] ;
2867 vc->vc_palette[k++] = default_grn[j] ;
2868 vc->vc_palette[k++] = default_blu[j] ;
2869 }
2870 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002871 vc->vc_ulcolor = default_underline_color;
2872 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 vc->vc_halfcolor = 0x08; /* grey */
2874 init_waitqueue_head(&vc->paste_wait);
2875 reset_terminal(vc, do_clear);
2876}
2877
2878/*
2879 * This routine initializes console interrupts, and does nothing
2880 * else. If you want the screen to clear, call tty_write with
2881 * the appropriate escape-sequence.
2882 */
2883
2884static int __init con_init(void)
2885{
2886 const char *display_desc = NULL;
2887 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002888 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889
2890 acquire_console_sem();
2891
2892 if (conswitchp)
2893 display_desc = conswitchp->con_startup();
2894 if (!display_desc) {
2895 fg_console = 0;
2896 release_console_sem();
2897 return 0;
2898 }
2899
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002900 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2901 struct con_driver *con_driver = &registered_con_driver[i];
2902
2903 if (con_driver->con == NULL) {
2904 con_driver->con = conswitchp;
2905 con_driver->desc = display_desc;
2906 con_driver->flag = CON_DRIVER_FLAG_INIT;
2907 con_driver->first = 0;
2908 con_driver->last = MAX_NR_CONSOLES - 1;
2909 break;
2910 }
2911 }
2912
2913 for (i = 0; i < MAX_NR_CONSOLES; i++)
2914 con_driver_map[i] = conswitchp;
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 if (blankinterval) {
2917 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002918 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 }
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002922 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002923 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002925 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 vc_init(vc, vc->vc_rows, vc->vc_cols,
2927 currcons || !vc->vc_sw->con_save_screen);
2928 }
2929 currcons = fg_console = 0;
2930 master_display_fg = vc = vc_cons[currcons].d;
2931 set_origin(vc);
2932 save_screen(vc);
2933 gotoxy(vc, vc->vc_x, vc->vc_y);
2934 csi_J(vc, 0);
2935 update_screen(vc);
2936 printk("Console: %s %s %dx%d",
2937 vc->vc_can_do_color ? "colour" : "mono",
2938 display_desc, vc->vc_cols, vc->vc_rows);
2939 printable = 1;
2940 printk("\n");
2941
2942 release_console_sem();
2943
2944#ifdef CONFIG_VT_CONSOLE
2945 register_console(&vt_console_driver);
2946#endif
2947 return 0;
2948}
2949console_initcall(con_init);
2950
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002951static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 .open = con_open,
2953 .close = con_close,
2954 .write = con_write,
2955 .write_room = con_write_room,
2956 .put_char = con_put_char,
2957 .flush_chars = con_flush_chars,
2958 .chars_in_buffer = con_chars_in_buffer,
2959 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02002960#ifdef CONFIG_COMPAT
2961 .compat_ioctl = vt_compat_ioctl,
2962#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 .stop = con_stop,
2964 .start = con_start,
2965 .throttle = con_throttle,
2966 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002967 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01002968 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969};
2970
Alan Coxd81ed102008-10-13 10:41:42 +01002971static struct cdev vc0_cdev;
2972
2973int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
Alan Coxd81ed102008-10-13 10:41:42 +01002975 cdev_init(&vc0_cdev, console_fops);
2976 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2977 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2978 panic("Couldn't register /dev/tty0 driver\n");
2979 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 vcs_init();
2982
2983 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2984 if (!console_driver)
2985 panic("Couldn't allocate console driver\n");
2986 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 console_driver->name = "tty";
2988 console_driver->name_base = 1;
2989 console_driver->major = TTY_MAJOR;
2990 console_driver->minor_start = 1;
2991 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2992 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002993 if (default_utf8)
2994 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2996 tty_set_operations(console_driver, &con_ops);
2997 if (tty_register_driver(console_driver))
2998 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 kbd_init();
3000 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001#ifdef CONFIG_MDA_CONSOLE
3002 mda_console_init();
3003#endif
3004 return 0;
3005}
3006
3007#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003008
3009static struct class *vtconsole_class;
3010
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003011static int bind_con_driver(const struct consw *csw, int first, int last,
3012 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003014 struct module *owner = csw->owner;
3015 const char *desc = NULL;
3016 struct con_driver *con_driver;
3017 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 if (!try_module_get(owner))
3020 return -ENODEV;
3021
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003022 acquire_console_sem();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003023
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003024 /* check if driver is registered */
3025 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3026 con_driver = &registered_con_driver[i];
3027
3028 if (con_driver->con == csw) {
3029 desc = con_driver->desc;
3030 retval = 0;
3031 break;
3032 }
3033 }
3034
3035 if (retval)
3036 goto err;
3037
3038 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3039 csw->con_startup();
3040 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003042
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043 if (deflt) {
3044 if (conswitchp)
3045 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 __module_get(owner);
3048 conswitchp = csw;
3049 }
3050
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003051 first = max(first, con_driver->first);
3052 last = min(last, con_driver->last);
3053
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 for (i = first; i <= last; i++) {
3055 int old_was_color;
3056 struct vc_data *vc = vc_cons[i].d;
3057
3058 if (con_driver_map[i])
3059 module_put(con_driver_map[i]->owner);
3060 __module_get(owner);
3061 con_driver_map[i] = csw;
3062
3063 if (!vc || !vc->vc_sw)
3064 continue;
3065
3066 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003067
David Hollister4ee1acc2006-06-26 00:26:41 -07003068 if (CON_IS_VISIBLE(vc)) {
3069 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003071 }
3072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 old_was_color = vc->vc_can_do_color;
3074 vc->vc_sw->con_deinit(vc);
qiaochong02f07772010-08-09 17:21:23 -07003075 if (!vc->vc_origin)
3076 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003078 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 update_attr(vc);
3080
3081 /* If the console changed between mono <-> color, then
3082 * the attributes in the screenbuf will be wrong. The
3083 * following resets all attributes to something sane.
3084 */
3085 if (old_was_color != vc->vc_can_do_color)
3086 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 printk("Console: switching ");
3090 if (!deflt)
3091 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003092 if (j >= 0) {
3093 struct vc_data *vc = vc_cons[j].d;
3094
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003096 vc->vc_can_do_color ? "colour" : "mono",
3097 desc, vc->vc_cols, vc->vc_rows);
3098
3099 if (k >= 0) {
3100 vc = vc_cons[k].d;
3101 update_screen(vc);
3102 }
3103 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 printk("to %s\n", desc);
3105
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003106 retval = 0;
3107err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003110 return retval;
3111};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003113#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3114static int con_is_graphics(const struct consw *csw, int first, int last)
3115{
3116 int i, retval = 0;
3117
3118 for (i = first; i <= last; i++) {
3119 struct vc_data *vc = vc_cons[i].d;
3120
3121 if (vc && vc->vc_mode == KD_GRAPHICS) {
3122 retval = 1;
3123 break;
3124 }
3125 }
3126
3127 return retval;
3128}
3129
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003130/**
3131 * unbind_con_driver - unbind a console driver
3132 * @csw: pointer to console driver to unregister
3133 * @first: first in range of consoles that @csw should be unbound from
3134 * @last: last in range of consoles that @csw should be unbound from
3135 * @deflt: should next bound console driver be default after @csw is unbound?
3136 *
3137 * To unbind a driver from all possible consoles, pass 0 as @first and
3138 * %MAX_NR_CONSOLES as @last.
3139 *
3140 * @deflt controls whether the console that ends up replacing @csw should be
3141 * the default console.
3142 *
3143 * RETURNS:
3144 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3145 * or 0 on success.
3146 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003147int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003149 struct module *owner = csw->owner;
3150 const struct consw *defcsw = NULL;
3151 struct con_driver *con_driver = NULL, *con_back = NULL;
3152 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003154 if (!try_module_get(owner))
3155 return -ENODEV;
3156
3157 acquire_console_sem();
3158
3159 /* check if driver is registered and if it is unbindable */
3160 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3161 con_driver = &registered_con_driver[i];
3162
3163 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003164 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003165 retval = 0;
3166 break;
3167 }
3168 }
3169
3170 if (retval) {
3171 release_console_sem();
3172 goto err;
3173 }
3174
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003175 retval = -ENODEV;
3176
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003177 /* check if backup driver exists */
3178 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3179 con_back = &registered_con_driver[i];
3180
3181 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003182 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003183 defcsw = con_back->con;
3184 retval = 0;
3185 break;
3186 }
3187 }
3188
3189 if (retval) {
3190 release_console_sem();
3191 goto err;
3192 }
3193
3194 if (!con_is_bound(csw)) {
3195 release_console_sem();
3196 goto err;
3197 }
3198
3199 first = max(first, con_driver->first);
3200 last = min(last, con_driver->last);
3201
3202 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 if (con_driver_map[i] == csw) {
3204 module_put(csw->owner);
3205 con_driver_map[i] = NULL;
3206 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003207 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003208
3209 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003210 const struct consw *defconsw = conswitchp;
3211
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003212 defcsw->con_startup();
3213 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003214 /*
3215 * vgacon may change the default driver to point
3216 * to dummycon, we restore it here...
3217 */
3218 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003219 }
3220
3221 if (!con_is_bound(csw))
3222 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3223
3224 release_console_sem();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003225 /* ignore return value, binding should not fail */
3226 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003227err:
3228 module_put(owner);
3229 return retval;
3230
3231}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003232EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003233
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003234static int vt_bind(struct con_driver *con)
3235{
3236 const struct consw *defcsw = NULL, *csw = NULL;
3237 int i, more = 1, first = -1, last = -1, deflt = 0;
3238
3239 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3240 con_is_graphics(con->con, con->first, con->last))
3241 goto err;
3242
3243 csw = con->con;
3244
3245 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3246 struct con_driver *con = &registered_con_driver[i];
3247
3248 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3249 defcsw = con->con;
3250 break;
3251 }
3252 }
3253
3254 if (!defcsw)
3255 goto err;
3256
3257 while (more) {
3258 more = 0;
3259
3260 for (i = con->first; i <= con->last; i++) {
3261 if (con_driver_map[i] == defcsw) {
3262 if (first == -1)
3263 first = i;
3264 last = i;
3265 more = 1;
3266 } else if (first != -1)
3267 break;
3268 }
3269
3270 if (first == 0 && last == MAX_NR_CONSOLES -1)
3271 deflt = 1;
3272
3273 if (first != -1)
3274 bind_con_driver(csw, first, last, deflt);
3275
3276 first = -1;
3277 last = -1;
3278 deflt = 0;
3279 }
3280
3281err:
3282 return 0;
3283}
3284
3285static int vt_unbind(struct con_driver *con)
3286{
3287 const struct consw *csw = NULL;
3288 int i, more = 1, first = -1, last = -1, deflt = 0;
3289
3290 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3291 con_is_graphics(con->con, con->first, con->last))
3292 goto err;
3293
3294 csw = con->con;
3295
3296 while (more) {
3297 more = 0;
3298
3299 for (i = con->first; i <= con->last; i++) {
3300 if (con_driver_map[i] == csw) {
3301 if (first == -1)
3302 first = i;
3303 last = i;
3304 more = 1;
3305 } else if (first != -1)
3306 break;
3307 }
3308
3309 if (first == 0 && last == MAX_NR_CONSOLES -1)
3310 deflt = 1;
3311
3312 if (first != -1)
3313 unbind_con_driver(csw, first, last, deflt);
3314
3315 first = -1;
3316 last = -1;
3317 deflt = 0;
3318 }
3319
3320err:
3321 return 0;
3322}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003323#else
3324static inline int vt_bind(struct con_driver *con)
3325{
3326 return 0;
3327}
3328static inline int vt_unbind(struct con_driver *con)
3329{
3330 return 0;
3331}
3332#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003333
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003334static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003335 const char *buf, size_t count)
3336{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003337 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003338 int bind = simple_strtoul(buf, NULL, 0);
3339
3340 if (bind)
3341 vt_bind(con);
3342 else
3343 vt_unbind(con);
3344
3345 return count;
3346}
3347
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003348static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3349 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003350{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003351 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003352 int bind = con_is_bound(con->con);
3353
3354 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3355}
3356
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003357static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3358 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003359{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003360 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003361
3362 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3363 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3364 con->desc);
3365
3366}
3367
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003368static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003369 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3370 __ATTR(name, S_IRUGO, show_name, NULL),
3371};
3372
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003373static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003374{
3375 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003376 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003377
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003378 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003379 dev_set_drvdata(con->dev, con);
3380 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3381 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003382 if (error)
3383 break;
3384 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003385
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003386 if (error) {
3387 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003388 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003389 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3390 }
3391
3392 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003393}
3394
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003395static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003396{
3397 int i;
3398
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003399 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003400 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3401 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003402 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3403 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003404}
3405
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003406/**
3407 * con_is_bound - checks if driver is bound to the console
3408 * @csw: console driver
3409 *
3410 * RETURNS: zero if unbound, nonzero if bound
3411 *
3412 * Drivers can call this and if zero, they should release
3413 * all resources allocated on con_startup()
3414 */
3415int con_is_bound(const struct consw *csw)
3416{
3417 int i, bound = 0;
3418
3419 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3420 if (con_driver_map[i] == csw) {
3421 bound = 1;
3422 break;
3423 }
3424 }
3425
3426 return bound;
3427}
3428EXPORT_SYMBOL(con_is_bound);
3429
3430/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003431 * con_debug_enter - prepare the console for the kernel debugger
3432 * @sw: console driver
3433 *
3434 * Called when the console is taken over by the kernel debugger, this
3435 * function needs to save the current console state, then put the console
3436 * into a state suitable for the kernel debugger.
3437 *
3438 * RETURNS:
3439 * Zero on success, nonzero if a failure occurred when trying to prepare
3440 * the console for the debugger.
3441 */
3442int con_debug_enter(struct vc_data *vc)
3443{
3444 int ret = 0;
3445
3446 saved_fg_console = fg_console;
3447 saved_last_console = last_console;
3448 saved_want_console = want_console;
3449 saved_vc_mode = vc->vc_mode;
3450 vc->vc_mode = KD_TEXT;
3451 console_blanked = 0;
3452 if (vc->vc_sw->con_debug_enter)
3453 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003454#ifdef CONFIG_KGDB_KDB
3455 /* Set the initial LINES variable if it is not already set */
3456 if (vc->vc_rows < 999) {
3457 int linecount;
3458 char lns[4];
3459 const char *setargs[3] = {
3460 "set",
3461 "LINES",
3462 lns,
3463 };
3464 if (kdbgetintenv(setargs[0], &linecount)) {
3465 snprintf(lns, 4, "%i", vc->vc_rows);
3466 kdb_set(2, setargs);
3467 }
3468 }
3469#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003470 return ret;
3471}
3472EXPORT_SYMBOL_GPL(con_debug_enter);
3473
3474/**
3475 * con_debug_leave - restore console state
3476 * @sw: console driver
3477 *
3478 * Restore the console state to what it was before the kernel debugger
3479 * was invoked.
3480 *
3481 * RETURNS:
3482 * Zero on success, nonzero if a failure occurred when trying to restore
3483 * the console.
3484 */
3485int con_debug_leave(void)
3486{
3487 struct vc_data *vc;
3488 int ret = 0;
3489
3490 fg_console = saved_fg_console;
3491 last_console = saved_last_console;
3492 want_console = saved_want_console;
3493 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3494
3495 vc = vc_cons[fg_console].d;
3496 if (vc->vc_sw->con_debug_leave)
3497 ret = vc->vc_sw->con_debug_leave(vc);
3498 return ret;
3499}
3500EXPORT_SYMBOL_GPL(con_debug_leave);
3501
3502/**
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003503 * register_con_driver - register console driver to console layer
3504 * @csw: console driver
3505 * @first: the first console to take over, minimum value is 0
3506 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3507 *
3508 * DESCRIPTION: This function registers a console driver which can later
3509 * bind to a range of consoles specified by @first and @last. It will
3510 * also initialize the console driver by calling con_startup().
3511 */
3512int register_con_driver(const struct consw *csw, int first, int last)
3513{
3514 struct module *owner = csw->owner;
3515 struct con_driver *con_driver;
3516 const char *desc;
3517 int i, retval = 0;
3518
3519 if (!try_module_get(owner))
3520 return -ENODEV;
3521
3522 acquire_console_sem();
3523
3524 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3525 con_driver = &registered_con_driver[i];
3526
3527 /* already registered */
3528 if (con_driver->con == csw)
3529 retval = -EINVAL;
3530 }
3531
3532 if (retval)
3533 goto err;
3534
3535 desc = csw->con_startup();
3536
3537 if (!desc)
3538 goto err;
3539
3540 retval = -EINVAL;
3541
3542 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3543 con_driver = &registered_con_driver[i];
3544
3545 if (con_driver->con == NULL) {
3546 con_driver->con = csw;
3547 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003548 con_driver->node = i;
3549 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003550 CON_DRIVER_FLAG_INIT;
3551 con_driver->first = first;
3552 con_driver->last = last;
3553 retval = 0;
3554 break;
3555 }
3556 }
3557
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003558 if (retval)
3559 goto err;
3560
Alan Coxd81ed102008-10-13 10:41:42 +01003561 con_driver->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003562 MKDEV(0, con_driver->node),
3563 NULL, "vtcon%i",
3564 con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003565
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003566 if (IS_ERR(con_driver->dev)) {
3567 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003568 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003569 PTR_ERR(con_driver->dev));
3570 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003571 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003572 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003573 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003574
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003575err:
3576 release_console_sem();
3577 module_put(owner);
3578 return retval;
3579}
3580EXPORT_SYMBOL(register_con_driver);
3581
3582/**
3583 * unregister_con_driver - unregister console driver from console layer
3584 * @csw: console driver
3585 *
3586 * DESCRIPTION: All drivers that registers to the console layer must
3587 * call this function upon exit, or if the console driver is in a state
3588 * where it won't be able to handle console services, such as the
3589 * framebuffer console without loaded framebuffer drivers.
3590 *
3591 * The driver must unbind first prior to unregistration.
3592 */
3593int unregister_con_driver(const struct consw *csw)
3594{
3595 int i, retval = -ENODEV;
3596
3597 acquire_console_sem();
3598
3599 /* cannot unregister a bound driver */
3600 if (con_is_bound(csw))
3601 goto err;
3602
3603 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3604 struct con_driver *con_driver = &registered_con_driver[i];
3605
3606 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003607 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003608 vtconsole_deinit_device(con_driver);
3609 device_destroy(vtconsole_class,
3610 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003611 con_driver->con = NULL;
3612 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003613 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003614 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003615 con_driver->flag = 0;
3616 con_driver->first = 0;
3617 con_driver->last = 0;
3618 retval = 0;
3619 break;
3620 }
3621 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003622err:
3623 release_console_sem();
3624 return retval;
3625}
3626EXPORT_SYMBOL(unregister_con_driver);
3627
3628/*
3629 * If we support more console drivers, this function is used
3630 * when a driver wants to take over some existing consoles
3631 * and become default driver for newly opened ones.
3632 *
3633 * take_over_console is basically a register followed by unbind
3634 */
3635int take_over_console(const struct consw *csw, int first, int last, int deflt)
3636{
3637 int err;
3638
3639 err = register_con_driver(csw, first, last);
3640
3641 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003642 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003643
3644 return err;
3645}
3646
3647/*
3648 * give_up_console is a wrapper to unregister_con_driver. It will only
3649 * work if driver is fully unbound.
3650 */
3651void give_up_console(const struct consw *csw)
3652{
3653 unregister_con_driver(csw);
3654}
3655
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003656static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003657{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003658 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003659
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003660 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3661 if (IS_ERR(vtconsole_class)) {
3662 printk(KERN_WARNING "Unable to create vt console class; "
3663 "errno = %ld\n", PTR_ERR(vtconsole_class));
3664 vtconsole_class = NULL;
3665 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003666
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003667 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003668 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3669 struct con_driver *con = &registered_con_driver[i];
3670
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003671 if (con->con && !con->dev) {
Alan Coxd81ed102008-10-13 10:41:42 +01003672 con->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003673 MKDEV(0, con->node),
3674 NULL, "vtcon%i",
3675 con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003676
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003677 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003678 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003679 "device for %s; errno = %ld\n",
3680 con->desc, PTR_ERR(con->dev));
3681 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003682 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003683 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003684 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003685 }
3686 }
3687
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003688 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003689}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003690postcore_initcall(vtconsole_class_init);
3691
3692#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003693
3694/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * Screen blanking
3696 */
3697
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003698static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003700 unsigned int mode;
3701
3702 if (get_user(mode, p + 1))
3703 return -EFAULT;
3704
3705 vesa_blank_mode = (mode < 4) ? mode : 0;
3706 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707}
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709void do_blank_screen(int entering_gfx)
3710{
3711 struct vc_data *vc = vc_cons[fg_console].d;
3712 int i;
3713
3714 WARN_CONSOLE_UNLOCKED();
3715
3716 if (console_blanked) {
3717 if (blank_state == blank_vesa_wait) {
3718 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003719 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721 return;
3722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723
3724 /* entering graphics mode? */
3725 if (entering_gfx) {
3726 hide_cursor(vc);
3727 save_screen(vc);
3728 vc->vc_sw->con_blank(vc, -1, 1);
3729 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003730 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 set_origin(vc);
3732 return;
3733 }
3734
izumib6e8f002007-07-17 04:05:49 -07003735 if (blank_state != blank_normal_wait)
3736 return;
3737 blank_state = blank_off;
3738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 /* don't blank graphics */
3740 if (vc->vc_mode != KD_TEXT) {
3741 console_blanked = fg_console + 1;
3742 return;
3743 }
3744
3745 hide_cursor(vc);
3746 del_timer_sync(&console_timer);
3747 blank_timer_expired = 0;
3748
3749 save_screen(vc);
3750 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003751 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 console_blanked = fg_console + 1;
3753 if (i)
3754 set_origin(vc);
3755
3756 if (console_blank_hook && console_blank_hook(1))
3757 return;
3758
Ville Syrjalad060a322006-01-09 20:53:49 -08003759 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003760 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 mod_timer(&console_timer, jiffies + vesa_off_interval);
3762 }
Alan Cox8b92e872009-09-19 13:13:24 -07003763 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764}
3765EXPORT_SYMBOL(do_blank_screen);
3766
3767/*
3768 * Called by timer as well as from vt_console_driver
3769 */
3770void do_unblank_screen(int leaving_gfx)
3771{
3772 struct vc_data *vc;
3773
3774 /* This should now always be called from a "sane" (read: can schedule)
3775 * context for the sake of the low level drivers, except in the special
3776 * case of oops_in_progress
3777 */
3778 if (!oops_in_progress)
3779 might_sleep();
3780
3781 WARN_CONSOLE_UNLOCKED();
3782
3783 ignore_poke = 0;
3784 if (!console_blanked)
3785 return;
3786 if (!vc_cons_allocated(fg_console)) {
3787 /* impossible */
3788 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3789 return;
3790 }
3791 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003792 /* Try to unblank in oops case too */
3793 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 return; /* but leave console_blanked != 0 */
3795
3796 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003797 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 blank_state = blank_normal_wait;
3799 }
3800
3801 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003802 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 /* Low-level driver cannot restore -> do it ourselves */
3804 update_screen(vc);
3805 if (console_blank_hook)
3806 console_blank_hook(0);
3807 set_palette(vc);
3808 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003809 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811EXPORT_SYMBOL(do_unblank_screen);
3812
3813/*
3814 * This is called by the outside world to cause a forced unblank, mostly for
3815 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3816 * call it with 1 as an argument and so force a mode restore... that may kill
3817 * X or at least garbage the screen but would also make the Oops visible...
3818 */
3819void unblank_screen(void)
3820{
3821 do_unblank_screen(0);
3822}
3823
3824/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003825 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003827 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828 */
3829static void blank_screen_t(unsigned long dummy)
3830{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003831 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003832 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003833 return;
3834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 blank_timer_expired = 1;
3836 schedule_work(&console_work);
3837}
3838
3839void poke_blanked_console(void)
3840{
3841 WARN_CONSOLE_UNLOCKED();
3842
3843 /* Add this so we quickly catch whoever might call us in a non
3844 * safe context. Nowadays, unblank_screen() isn't to be called in
3845 * atomic contexts and is allowed to schedule (with the special case
3846 * of oops_in_progress, but that isn't of any concern for this
3847 * function. --BenH.
3848 */
3849 might_sleep();
3850
3851 /* This isn't perfectly race free, but a race here would be mostly harmless,
3852 * at worse, we'll do a spurrious blank and it's unlikely
3853 */
3854 del_timer(&console_timer);
3855 blank_timer_expired = 0;
3856
3857 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3858 return;
3859 if (console_blanked)
3860 unblank_screen();
3861 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003862 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 blank_state = blank_normal_wait;
3864 }
3865}
3866
3867/*
3868 * Palettes
3869 */
3870
3871static void set_palette(struct vc_data *vc)
3872{
3873 WARN_CONSOLE_UNLOCKED();
3874
3875 if (vc->vc_mode != KD_GRAPHICS)
3876 vc->vc_sw->con_set_palette(vc, color_table);
3877}
3878
3879static int set_get_cmap(unsigned char __user *arg, int set)
3880{
3881 int i, j, k;
3882
3883 WARN_CONSOLE_UNLOCKED();
3884
3885 for (i = 0; i < 16; i++)
3886 if (set) {
3887 get_user(default_red[i], arg++);
3888 get_user(default_grn[i], arg++);
3889 get_user(default_blu[i], arg++);
3890 } else {
3891 put_user(default_red[i], arg++);
3892 put_user(default_grn[i], arg++);
3893 put_user(default_blu[i], arg++);
3894 }
3895 if (set) {
3896 for (i = 0; i < MAX_NR_CONSOLES; i++)
3897 if (vc_cons_allocated(i)) {
3898 for (j = k = 0; j < 16; j++) {
3899 vc_cons[i].d->vc_palette[k++] = default_red[j];
3900 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3901 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3902 }
3903 set_palette(vc_cons[i].d);
3904 }
3905 }
3906 return 0;
3907}
3908
3909/*
3910 * Load palette into the DAC registers. arg points to a colour
3911 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3912 */
3913
3914int con_set_cmap(unsigned char __user *arg)
3915{
3916 int rc;
3917
3918 acquire_console_sem();
3919 rc = set_get_cmap (arg,1);
3920 release_console_sem();
3921
3922 return rc;
3923}
3924
3925int con_get_cmap(unsigned char __user *arg)
3926{
3927 int rc;
3928
3929 acquire_console_sem();
3930 rc = set_get_cmap (arg,0);
3931 release_console_sem();
3932
3933 return rc;
3934}
3935
3936void reset_palette(struct vc_data *vc)
3937{
3938 int j, k;
3939 for (j=k=0; j<16; j++) {
3940 vc->vc_palette[k++] = default_red[j];
3941 vc->vc_palette[k++] = default_grn[j];
3942 vc->vc_palette[k++] = default_blu[j];
3943 }
3944 set_palette(vc);
3945}
3946
3947/*
3948 * Font switching
3949 *
3950 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3951 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3952 * depending on width) reserved for each character which is kinda wasty, but
3953 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3954 * is upto the actual low-level console-driver convert data into its favorite
3955 * format (maybe we should add a `fontoffset' field to the `display'
3956 * structure so we won't have to convert the fontdata all the time.
3957 * /Jes
3958 */
3959
3960#define max_font_size 65536
3961
3962static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3963{
3964 struct console_font font;
3965 int rc = -EINVAL;
3966 int c;
3967
3968 if (vc->vc_mode != KD_TEXT)
3969 return -EINVAL;
3970
3971 if (op->data) {
3972 font.data = kmalloc(max_font_size, GFP_KERNEL);
3973 if (!font.data)
3974 return -ENOMEM;
3975 } else
3976 font.data = NULL;
3977
3978 acquire_console_sem();
3979 if (vc->vc_sw->con_font_get)
3980 rc = vc->vc_sw->con_font_get(vc, &font);
3981 else
3982 rc = -ENOSYS;
3983 release_console_sem();
3984
3985 if (rc)
3986 goto out;
3987
3988 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07003989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 if (op->data && font.charcount > op->charcount)
3991 rc = -ENOSPC;
3992 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3993 if (font.width > op->width || font.height > op->height)
3994 rc = -ENOSPC;
3995 } else {
3996 if (font.width != 8)
3997 rc = -EIO;
3998 else if ((op->height && font.height > op->height) ||
3999 font.height > 32)
4000 rc = -ENOSPC;
4001 }
4002 if (rc)
4003 goto out;
4004
4005 op->height = font.height;
4006 op->width = font.width;
4007 op->charcount = font.charcount;
4008
4009 if (op->data && copy_to_user(op->data, font.data, c))
4010 rc = -EFAULT;
4011
4012out:
4013 kfree(font.data);
4014 return rc;
4015}
4016
4017static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4018{
4019 struct console_font font;
4020 int rc = -EINVAL;
4021 int size;
4022
4023 if (vc->vc_mode != KD_TEXT)
4024 return -EINVAL;
4025 if (!op->data)
4026 return -EINVAL;
4027 if (op->charcount > 512)
4028 return -EINVAL;
4029 if (!op->height) { /* Need to guess font height [compat] */
4030 int h, i;
4031 u8 __user *charmap = op->data;
4032 u8 tmp;
4033
4034 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4035 so that we can get rid of this soon */
4036 if (!(op->flags & KD_FONT_FLAG_OLD))
4037 return -EINVAL;
4038 for (h = 32; h > 0; h--)
4039 for (i = 0; i < op->charcount; i++) {
4040 if (get_user(tmp, &charmap[32*i+h-1]))
4041 return -EFAULT;
4042 if (tmp)
4043 goto nonzero;
4044 }
4045 return -EINVAL;
4046 nonzero:
4047 op->height = h;
4048 }
4049 if (op->width <= 0 || op->width > 32 || op->height > 32)
4050 return -EINVAL;
4051 size = (op->width+7)/8 * 32 * op->charcount;
4052 if (size > max_font_size)
4053 return -ENOSPC;
4054 font.charcount = op->charcount;
4055 font.height = op->height;
4056 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004057 font.data = memdup_user(op->data, size);
4058 if (IS_ERR(font.data))
4059 return PTR_ERR(font.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 acquire_console_sem();
4061 if (vc->vc_sw->con_font_set)
4062 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4063 else
4064 rc = -ENOSYS;
4065 release_console_sem();
4066 kfree(font.data);
4067 return rc;
4068}
4069
4070static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4071{
4072 struct console_font font = {.width = op->width, .height = op->height};
4073 char name[MAX_FONT_NAME];
4074 char *s = name;
4075 int rc;
4076
4077 if (vc->vc_mode != KD_TEXT)
4078 return -EINVAL;
4079
4080 if (!op->data)
4081 s = NULL;
4082 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4083 return -EFAULT;
4084 else
4085 name[MAX_FONT_NAME - 1] = 0;
4086
4087 acquire_console_sem();
4088 if (vc->vc_sw->con_font_default)
4089 rc = vc->vc_sw->con_font_default(vc, &font, s);
4090 else
4091 rc = -ENOSYS;
4092 release_console_sem();
4093 if (!rc) {
4094 op->width = font.width;
4095 op->height = font.height;
4096 }
4097 return rc;
4098}
4099
4100static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4101{
4102 int con = op->height;
4103 int rc;
4104
4105 if (vc->vc_mode != KD_TEXT)
4106 return -EINVAL;
4107
4108 acquire_console_sem();
4109 if (!vc->vc_sw->con_font_copy)
4110 rc = -ENOSYS;
4111 else if (con < 0 || !vc_cons_allocated(con))
4112 rc = -ENOTTY;
4113 else if (con == vc->vc_num) /* nothing to do */
4114 rc = 0;
4115 else
4116 rc = vc->vc_sw->con_font_copy(vc, con);
4117 release_console_sem();
4118 return rc;
4119}
4120
4121int con_font_op(struct vc_data *vc, struct console_font_op *op)
4122{
4123 switch (op->op) {
4124 case KD_FONT_OP_SET:
4125 return con_font_set(vc, op);
4126 case KD_FONT_OP_GET:
4127 return con_font_get(vc, op);
4128 case KD_FONT_OP_SET_DEFAULT:
4129 return con_font_default(vc, op);
4130 case KD_FONT_OP_COPY:
4131 return con_font_copy(vc, op);
4132 }
4133 return -ENOSYS;
4134}
4135
4136/*
4137 * Interface exported to selection and vcs.
4138 */
4139
4140/* used by selection */
4141u16 screen_glyph(struct vc_data *vc, int offset)
4142{
4143 u16 w = scr_readw(screenpos(vc, offset, 1));
4144 u16 c = w & 0xff;
4145
4146 if (w & vc->vc_hi_font_mask)
4147 c |= 0x100;
4148 return c;
4149}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004150EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152/* used by vcs - note the word offset */
4153unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4154{
4155 return screenpos(vc, 2 * w_offset, viewed);
4156}
4157
4158void getconsxy(struct vc_data *vc, unsigned char *p)
4159{
4160 p[0] = vc->vc_x;
4161 p[1] = vc->vc_y;
4162}
4163
4164void putconsxy(struct vc_data *vc, unsigned char *p)
4165{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004166 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 gotoxy(vc, p[0], p[1]);
4168 set_cursor(vc);
4169}
4170
4171u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4172{
4173 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4174 return softcursor_original;
4175 return scr_readw(org);
4176}
4177
4178void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4179{
4180 scr_writew(val, org);
4181 if ((unsigned long)org == vc->vc_pos) {
4182 softcursor_original = -1;
4183 add_softcursor(vc);
4184 }
4185}
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187/*
4188 * Visible symbols for modules
4189 */
4190
4191EXPORT_SYMBOL(color_table);
4192EXPORT_SYMBOL(default_red);
4193EXPORT_SYMBOL(default_grn);
4194EXPORT_SYMBOL(default_blu);
4195EXPORT_SYMBOL(update_region);
4196EXPORT_SYMBOL(redraw_screen);
4197EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198EXPORT_SYMBOL(fg_console);
4199EXPORT_SYMBOL(console_blank_hook);
4200EXPORT_SYMBOL(console_blanked);
4201EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004202EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203#ifndef VT_SINGLE_DRIVER
4204EXPORT_SYMBOL(take_over_console);
4205EXPORT_SYMBOL(give_up_console);
4206#endif