blob: 2178efcec1b56ff9d82a4fffafcb32ba81fe97bd [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>
92#include <linux/tiocl.h>
93#include <linux/kbd_kern.h>
94#include <linux/consolemap.h>
95#include <linux/timer.h>
96#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/pm.h>
99#include <linux/font.h>
100#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700101#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100102#include <linux/device.h>
103#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <asm/system.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100105#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500106#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300107#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700109#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700111#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700112#define CON_DRIVER_FLAG_INIT 2
113#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700114
115struct con_driver {
116 const struct consw *con;
117 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700118 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700119 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700120 int first;
121 int last;
122 int flag;
123};
124
125static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126const struct consw *conswitchp;
127
128/* A bitmap for codes <32. A bit of 1 indicates that the code
129 * corresponding to that bit number invokes some special action
130 * (such as cursor movement) and should not be displayed as a
131 * glyph unless the disp_ctrl mode is explicitly enabled.
132 */
133#define CTRL_ACTION 0x0d00ff81
134#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
135
136/*
137 * Here is the default bell parameters: 750HZ, 1/8th of a second
138 */
139#define DEFAULT_BELL_PITCH 750
140#define DEFAULT_BELL_DURATION (HZ/8)
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct vc vc_cons [MAX_NR_CONSOLES];
143
144#ifndef VT_SINGLE_DRIVER
145static const struct consw *con_driver_map[MAX_NR_CONSOLES];
146#endif
147
148static int con_open(struct tty_struct *, struct file *);
149static void vc_init(struct vc_data *vc, unsigned int rows,
150 unsigned int cols, int do_clear);
151static void gotoxy(struct vc_data *vc, int new_x, int new_y);
152static void save_cur(struct vc_data *vc);
153static void reset_terminal(struct vc_data *vc, int do_clear);
154static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800155static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void set_cursor(struct vc_data *vc);
157static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000158static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void blank_screen_t(unsigned long dummy);
160static void set_palette(struct vc_data *vc);
161
162static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700163int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700164module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500165int global_cursor_default = -1;
166module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800168static int cur_default = CUR_DEFAULT;
169module_param(cur_default, int, S_IRUGO | S_IWUSR);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * ignore_poke: don't unblank the screen when things are typed. This is
173 * mainly for the privacy of braille terminal users.
174 */
175static int ignore_poke;
176
177int do_poke_blanked_console;
178int console_blanked;
179
180static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static int vesa_off_interval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700182static int blankinterval = 10*60;
183core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Howells65f27f32006-11-22 14:55:48 +0000185static DECLARE_WORK(console_work, console_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/*
188 * fg_console is the current virtual console,
189 * last_console is the last used one,
190 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500191 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193int fg_console;
194int last_console;
195int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500196static int saved_fg_console;
197static int saved_last_console;
198static int saved_want_console;
199static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500200static int saved_console_blanked;
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/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100238 * /sys/class/tty/tty0/
239 *
240 * the attribute 'active' contains the name of the current vc
241 * console and it supports poll() to detect vc switches
242 */
243static struct device *tty0dev;
244
245/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700246 * Notifier list for console events.
247 */
248static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
249
250int register_vt_notifier(struct notifier_block *nb)
251{
252 return atomic_notifier_chain_register(&vt_notifier_list, nb);
253}
254EXPORT_SYMBOL_GPL(register_vt_notifier);
255
256int unregister_vt_notifier(struct notifier_block *nb)
257{
258 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
259}
260EXPORT_SYMBOL_GPL(unregister_vt_notifier);
261
262static void notify_write(struct vc_data *vc, unsigned int unicode)
263{
264 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
265 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
266}
267
268static void notify_update(struct vc_data *vc)
269{
270 struct vt_notifier_param param = { .vc = vc };
271 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
272}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * Low-Level Functions
275 */
276
277#define IS_FG(vc) ((vc)->vc_num == fg_console)
278
279#ifdef VT_BUF_VRAM_ONLY
280#define DO_UPDATE(vc) 0
281#else
Stefano Stabellinif700d6e2008-07-23 21:29:59 -0700282#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
284
285static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
286{
287 unsigned short *p;
288
289 if (!viewed)
290 p = (unsigned short *)(vc->vc_origin + offset);
291 else if (!vc->vc_sw->con_screen_pos)
292 p = (unsigned short *)(vc->vc_visible_origin + offset);
293 else
294 p = vc->vc_sw->con_screen_pos(vc, offset);
295 return p;
296}
297
Alan Coxe33ac1c2010-06-01 22:52:54 +0200298/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static inline void scrolldelta(int lines)
300{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200301 /* FIXME */
302 /* scrolldelta needs some kind of consistency lock, but the BKL was
303 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 scrollback_delta += lines;
305 schedule_console_callback();
306}
307
308void schedule_console_callback(void)
309{
310 schedule_work(&console_work);
311}
312
313static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
314{
315 unsigned short *d, *s;
316
317 if (t+nr >= b)
318 nr = b - t - 1;
319 if (b > vc->vc_rows || t >= b || nr < 1)
320 return;
321 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
322 return;
323 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
324 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
325 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds386f40c2010-06-06 20:44:04 -0700326 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 vc->vc_size_row * nr);
328}
329
330static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
331{
332 unsigned short *s;
333 unsigned int step;
334
335 if (t+nr >= b)
336 nr = b - t - 1;
337 if (b > vc->vc_rows || t >= b || nr < 1)
338 return;
339 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
340 return;
341 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
342 step = vc->vc_cols * nr;
343 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds93f78da2008-10-14 12:12:02 -0700344 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static void do_update_region(struct vc_data *vc, unsigned long start, int count)
348{
349#ifndef VT_BUF_VRAM_ONLY
350 unsigned int xx, yy, offset;
351 u16 *p;
352
353 p = (u16 *) start;
354 if (!vc->vc_sw->con_getxy) {
355 offset = (start - vc->vc_origin) / 2;
356 xx = offset % vc->vc_cols;
357 yy = offset / vc->vc_cols;
358 } else {
359 int nxx, nyy;
360 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
361 xx = nxx; yy = nyy;
362 }
363 for(;;) {
364 u16 attrib = scr_readw(p) & 0xff00;
365 int startx = xx;
366 u16 *q = p;
367 while (xx < vc->vc_cols && count) {
368 if (attrib != (scr_readw(p) & 0xff00)) {
369 if (p > q)
370 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
371 startx = xx;
372 q = p;
373 attrib = scr_readw(p) & 0xff00;
374 }
375 p++;
376 xx++;
377 count--;
378 }
379 if (p > q)
380 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
381 if (!count)
382 break;
383 xx = 0;
384 yy++;
385 if (vc->vc_sw->con_getxy) {
386 p = (u16 *)start;
387 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
388 }
389 }
390#endif
391}
392
393void update_region(struct vc_data *vc, unsigned long start, int count)
394{
395 WARN_CONSOLE_UNLOCKED();
396
397 if (DO_UPDATE(vc)) {
398 hide_cursor(vc);
399 do_update_region(vc, start, count);
400 set_cursor(vc);
401 }
402}
403
404/* Structure of attributes is hardware-dependent */
405
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700406static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
407 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700410 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
411 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#ifndef VT_BUF_VRAM_ONLY
414/*
415 * ++roman: I completely changed the attribute format for monochrome
416 * mode (!can_do_color). The formerly used MDA (monochrome display
417 * adapter) format didn't allow the combination of certain effects.
418 * Now the attribute is just a bit vector:
419 * Bit 0..1: intensity (0..2)
420 * Bit 2 : underline
421 * Bit 3 : reverse
422 * Bit 7 : blink
423 */
424 {
Jan Engelhardtc9e587a2008-04-29 00:59:46 -0700425 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!vc->vc_can_do_color)
427 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700428 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 (_underline ? 4 : 0) |
430 (_reverse ? 8 : 0) |
431 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700432 if (_italic)
433 a = (a & 0xF0) | vc->vc_itcolor;
434 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 a = (a & 0xf0) | vc->vc_ulcolor;
436 else if (_intensity == 0)
437 a = (a & 0xf0) | vc->vc_ulcolor;
438 if (_reverse)
439 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
440 if (_blink)
441 a ^= 0x80;
442 if (_intensity == 2)
443 a ^= 0x08;
444 if (vc->vc_hi_font_mask == 0x100)
445 a <<= 1;
446 return a;
447 }
448#else
449 return 0;
450#endif
451}
452
453static void update_attr(struct vc_data *vc)
454{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700455 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
456 vc->vc_blink, vc->vc_underline,
457 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
458 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 -0700459}
460
461/* Note: inverting the screen twice should revert to the original state */
462void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
463{
464 unsigned short *p;
465
466 WARN_CONSOLE_UNLOCKED();
467
468 count /= 2;
469 p = screenpos(vc, offset, viewed);
470 if (vc->vc_sw->con_invert_region)
471 vc->vc_sw->con_invert_region(vc, p, count);
472#ifndef VT_BUF_VRAM_ONLY
473 else {
474 u16 *q = p;
475 int cnt = count;
476 u16 a;
477
478 if (!vc->vc_can_do_color) {
479 while (cnt--) {
480 a = scr_readw(q);
481 a ^= 0x0800;
482 scr_writew(a, q);
483 q++;
484 }
485 } else if (vc->vc_hi_font_mask == 0x100) {
486 while (cnt--) {
487 a = scr_readw(q);
488 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
489 scr_writew(a, q);
490 q++;
491 }
492 } else {
493 while (cnt--) {
494 a = scr_readw(q);
495 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
496 scr_writew(a, q);
497 q++;
498 }
499 }
500 }
501#endif
502 if (DO_UPDATE(vc))
503 do_update_region(vc, (unsigned long) p, count);
504}
505
506/* used by selection: complement pointer position */
507void complement_pos(struct vc_data *vc, int offset)
508{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700509 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 static unsigned short old;
511 static unsigned short oldx, oldy;
512
513 WARN_CONSOLE_UNLOCKED();
514
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700515 if (old_offset != -1 && old_offset >= 0 &&
516 old_offset < vc->vc_screenbuf_size) {
517 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (DO_UPDATE(vc))
519 vc->vc_sw->con_putc(vc, old, oldy, oldx);
520 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700521
522 old_offset = offset;
523
524 if (offset != -1 && offset >= 0 &&
525 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700527 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 p = screenpos(vc, offset, 1);
529 old = scr_readw(p);
530 new = old ^ vc->vc_complement_mask;
531 scr_writew(new, p);
532 if (DO_UPDATE(vc)) {
533 oldx = (offset >> 1) % vc->vc_cols;
534 oldy = (offset >> 1) / vc->vc_cols;
535 vc->vc_sw->con_putc(vc, new, oldy, oldx);
536 }
537 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static void insert_char(struct vc_data *vc, unsigned int nr)
542{
543 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
544
545 p = q + vc->vc_cols - nr - vc->vc_x;
546 while (--p >= q)
547 scr_writew(scr_readw(p), p + nr);
548 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
549 vc->vc_need_wrap = 0;
550 if (DO_UPDATE(vc)) {
551 unsigned short oldattr = vc->vc_attr;
552 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
553 vc->vc_cols - vc->vc_x - nr);
554 vc->vc_attr = vc->vc_video_erase_char >> 8;
555 while (nr--)
556 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
557 vc->vc_attr = oldattr;
558 }
559}
560
561static void delete_char(struct vc_data *vc, unsigned int nr)
562{
563 unsigned int i = vc->vc_x;
564 unsigned short *p = (unsigned short *)vc->vc_pos;
565
566 while (++i <= vc->vc_cols - nr) {
567 scr_writew(scr_readw(p+nr), p);
568 p++;
569 }
570 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
571 vc->vc_need_wrap = 0;
572 if (DO_UPDATE(vc)) {
573 unsigned short oldattr = vc->vc_attr;
574 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
575 vc->vc_cols - vc->vc_x - nr);
576 vc->vc_attr = vc->vc_video_erase_char >> 8;
577 while (nr--)
578 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
579 vc->vc_cols - 1 - nr);
580 vc->vc_attr = oldattr;
581 }
582}
583
584static int softcursor_original;
585
586static void add_softcursor(struct vc_data *vc)
587{
588 int i = scr_readw((u16 *) vc->vc_pos);
589 u32 type = vc->vc_cursor_type;
590
591 if (! (type & 0x10)) return;
592 if (softcursor_original != -1) return;
593 softcursor_original = i;
594 i |= ((type >> 8) & 0xff00 );
595 i ^= ((type) & 0xff00 );
596 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
597 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
598 scr_writew(i, (u16 *) vc->vc_pos);
599 if (DO_UPDATE(vc))
600 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
601}
602
603static void hide_softcursor(struct vc_data *vc)
604{
605 if (softcursor_original != -1) {
606 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
607 if (DO_UPDATE(vc))
608 vc->vc_sw->con_putc(vc, softcursor_original,
609 vc->vc_y, vc->vc_x);
610 softcursor_original = -1;
611 }
612}
613
614static void hide_cursor(struct vc_data *vc)
615{
616 if (vc == sel_cons)
617 clear_selection();
618 vc->vc_sw->con_cursor(vc, CM_ERASE);
619 hide_softcursor(vc);
620}
621
622static void set_cursor(struct vc_data *vc)
623{
624 if (!IS_FG(vc) || console_blanked ||
625 vc->vc_mode == KD_GRAPHICS)
626 return;
627 if (vc->vc_deccm) {
628 if (vc == sel_cons)
629 clear_selection();
630 add_softcursor(vc);
631 if ((vc->vc_cursor_type & 0x0f) != 1)
632 vc->vc_sw->con_cursor(vc, CM_DRAW);
633 } else
634 hide_cursor(vc);
635}
636
637static void set_origin(struct vc_data *vc)
638{
639 WARN_CONSOLE_UNLOCKED();
640
641 if (!CON_IS_VISIBLE(vc) ||
642 !vc->vc_sw->con_set_origin ||
643 !vc->vc_sw->con_set_origin(vc))
644 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
645 vc->vc_visible_origin = vc->vc_origin;
646 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
647 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
648}
649
650static inline void save_screen(struct vc_data *vc)
651{
652 WARN_CONSOLE_UNLOCKED();
653
654 if (vc->vc_sw->con_save_screen)
655 vc->vc_sw->con_save_screen(vc);
656}
657
658/*
659 * Redrawing of screen
660 */
661
662static void clear_buffer_attributes(struct vc_data *vc)
663{
664 unsigned short *p = (unsigned short *)vc->vc_origin;
665 int count = vc->vc_screenbuf_size / 2;
666 int mask = vc->vc_hi_font_mask | 0xff;
667
668 for (; count > 0; count--, p++) {
669 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
670 }
671}
672
673void redraw_screen(struct vc_data *vc, int is_switch)
674{
675 int redraw = 0;
676
677 WARN_CONSOLE_UNLOCKED();
678
679 if (!vc) {
680 /* strange ... */
681 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
682 return;
683 }
684
685 if (is_switch) {
686 struct vc_data *old_vc = vc_cons[fg_console].d;
687 if (old_vc == vc)
688 return;
689 if (!CON_IS_VISIBLE(vc))
690 redraw = 1;
691 *vc->vc_display_fg = vc;
692 fg_console = vc->vc_num;
693 hide_cursor(old_vc);
694 if (!CON_IS_VISIBLE(old_vc)) {
695 save_screen(old_vc);
696 set_origin(old_vc);
697 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100698 if (tty0dev)
699 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 hide_cursor(vc);
702 redraw = 1;
703 }
704
705 if (redraw) {
706 int update;
707 int old_was_color = vc->vc_can_do_color;
708
709 set_origin(vc);
710 update = vc->vc_sw->con_switch(vc);
711 set_palette(vc);
712 /*
713 * If console changed from mono<->color, the best we can do
714 * is to clear the buffer attributes. As it currently stands,
715 * rebuilding new attributes from the old buffer is not doable
716 * without overly complex code.
717 */
718 if (old_was_color != vc->vc_can_do_color) {
719 update_attr(vc);
720 clear_buffer_attributes(vc);
721 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700722
723 /* Forcibly update if we're panicing */
724 if ((update && vc->vc_mode != KD_GRAPHICS) ||
725 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
727 }
728 set_cursor(vc);
729 if (is_switch) {
730 set_leds();
731 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800732 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734}
735
736/*
737 * Allocation, freeing and resizing of VTs.
738 */
739
740int vc_cons_allocated(unsigned int i)
741{
742 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
743}
744
745static void visual_init(struct vc_data *vc, int num, int init)
746{
747 /* ++Geert: vc->vc_sw->con_init determines console size */
748 if (vc->vc_sw)
749 module_put(vc->vc_sw->owner);
750 vc->vc_sw = conswitchp;
751#ifndef VT_SINGLE_DRIVER
752 if (con_driver_map[num])
753 vc->vc_sw = con_driver_map[num];
754#endif
755 __module_get(vc->vc_sw->owner);
756 vc->vc_num = num;
757 vc->vc_display_fg = &master_display_fg;
758 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
759 vc->vc_uni_pagedir = 0;
760 vc->vc_hi_font_mask = 0;
761 vc->vc_complement_mask = 0;
762 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700763 vc->vc_panic_force_write = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 vc->vc_sw->con_init(vc, init);
765 if (!vc->vc_complement_mask)
766 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
767 vc->vc_s_complement_mask = vc->vc_complement_mask;
768 vc->vc_size_row = vc->vc_cols << 1;
769 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
770}
771
772int vc_allocate(unsigned int currcons) /* return 0 on success */
773{
774 WARN_CONSOLE_UNLOCKED();
775
776 if (currcons >= MAX_NR_CONSOLES)
777 return -ENXIO;
778 if (!vc_cons[currcons].d) {
779 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700780 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* prevent users from taking too much memory */
783 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
784 return -EPERM;
785
786 /* due to the granularity of kmalloc, we waste some memory here */
787 /* the alloc is done in two steps, to optimize the common situation
788 of a 25x80 console (structsize=216, screenbuf_size=4000) */
789 /* although the numbers above are not valid since long ago, the
790 point is still up-to-date and the comment still has its value
791 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700792 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!vc)
794 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 vc_cons[currcons].d = vc;
Alan Coxff917ba2010-06-01 22:52:55 +0200796 tty_port_init(&vc->port);
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800797 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 visual_init(vc, currcons, 1);
799 if (!*vc->vc_uni_pagedir_loc)
800 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100801 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!vc->vc_screenbuf) {
803 kfree(vc);
804 vc_cons[currcons].d = NULL;
805 return -ENOMEM;
806 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500807
808 /* If no drivers have overridden us and the user didn't pass a
809 boot option, default to displaying the cursor */
810 if (global_cursor_default == -1)
811 global_cursor_default = 1;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100814 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700815 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 return 0;
818}
819
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700820static inline int resize_screen(struct vc_data *vc, int width, int height,
821 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 /* Resizes the resolution of the display adapater */
824 int err = 0;
825
826 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700827 err = vc->vc_sw->con_resize(vc, width, height, user);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return err;
830}
831
832/*
833 * Change # of rows and columns (0 means unchanged/the size of fg_console)
834 * [this is to be used together with some user program
835 * like resize that changes the hardware videomode]
836 */
837#define VC_RESIZE_MAXCOL (32767)
838#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100839
840/**
841 * vc_do_resize - resizing method for the tty
842 * @tty: tty being resized
843 * @real_tty: real tty (different to tty if a pty/tty pair)
844 * @vc: virtual console private data
845 * @cols: columns
846 * @lines: lines
847 *
848 * Resize a virtual console, clipping according to the actual constraints.
849 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800850 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100851 *
852 * Caller must hold the console semaphore. Takes the termios mutex and
853 * ctrl_lock of the tty IFF a tty is passed.
854 */
855
Alan Coxfc6f6232009-01-02 13:43:17 +0000856static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
857 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700860 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
862 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700863 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned short *newscreen;
865
866 WARN_CONSOLE_UNLOCKED();
867
868 if (!vc)
869 return -ENXIO;
870
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700871 user = vc->vc_resize_user;
872 vc->vc_resize_user = 0;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
875 return -EINVAL;
876
877 new_cols = (cols ? cols : vc->vc_cols);
878 new_rows = (lines ? lines : vc->vc_rows);
879 new_row_size = new_cols << 1;
880 new_screen_size = new_row_size * new_rows;
881
882 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
883 return 0;
884
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800885 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!newscreen)
887 return -ENOMEM;
888
889 old_rows = vc->vc_rows;
890 old_cols = vc->vc_cols;
891 old_row_size = vc->vc_size_row;
892 old_screen_size = vc->vc_screenbuf_size;
893
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700894 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (err) {
896 kfree(newscreen);
897 return err;
898 }
899
900 vc->vc_rows = new_rows;
901 vc->vc_cols = new_cols;
902 vc->vc_size_row = new_row_size;
903 vc->vc_screenbuf_size = new_screen_size;
904
905 rlth = min(old_row_size, new_row_size);
906 rrem = new_row_size - rlth;
907 old_origin = vc->vc_origin;
908 new_origin = (long) newscreen;
909 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700910
911 if (vc->vc_y > new_rows) {
912 if (old_rows - vc->vc_y < new_rows) {
913 /*
914 * Cursor near the bottom, copy contents from the
915 * bottom of buffer
916 */
917 old_origin += (old_rows - new_rows) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700918 } else {
919 /*
920 * Cursor is in no man's land, copy 1/2 screenful
921 * from the top and bottom of cursor position
922 */
923 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700924 }
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +0200925 }
926
927 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 update_attr(vc);
930
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700931 while (old_origin < end) {
932 scr_memcpyw((unsigned short *) new_origin,
933 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700935 scr_memsetw((void *)(new_origin + rlth),
936 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 old_origin += old_row_size;
938 new_origin += new_row_size;
939 }
940 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700941 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
942 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100943 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 vc->vc_screenbuf_size = new_screen_size;
946 set_origin(vc);
947
948 /* do part of a reset_terminal() */
949 vc->vc_top = 0;
950 vc->vc_bottom = vc->vc_rows;
951 gotoxy(vc, vc->vc_x, vc->vc_y);
952 save_cur(vc);
953
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100954 if (tty) {
955 /* Rewrite the requested winsize data with the actual
956 resulting sizes */
957 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 memset(&ws, 0, sizeof(ws));
959 ws.ws_row = vc->vc_rows;
960 ws.ws_col = vc->vc_cols;
961 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000962 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 if (CON_IS_VISIBLE(vc))
966 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700967 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return err;
969}
970
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100971/**
972 * vc_resize - resize a VT
973 * @vc: virtual console
974 * @cols: columns
975 * @rows: rows
976 *
977 * Resize a virtual console as seen from the console end of things. We
978 * use the common vc_do_resize methods to update the structures. The
979 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +0200980 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100981 */
982
983int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700984{
Alan Cox8ce73262010-06-01 22:52:56 +0200985 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100986}
987
988/**
989 * vt_resize - resize a VT
990 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100991 * @ws: winsize attributes
992 *
993 * Resize a virtual terminal. This is called by the tty layer as we
994 * register our own handler for resizing. The mutual helper does all
995 * the actual work.
996 *
997 * Takes the console sem and the called methods then take the tty
998 * termios_mutex and the tty ctrl_lock in that order.
999 */
Roel Kluinda2bdf92009-01-07 18:09:15 -08001000static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001001{
1002 struct vc_data *vc = tty->driver_data;
1003 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Torben Hohnac751ef2011-01-25 15:07:35 -08001005 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001006 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001007 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001008 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001009}
1010
1011void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012{
1013 WARN_CONSOLE_UNLOCKED();
1014
1015 if (vc_cons_allocated(currcons)) {
1016 struct vc_data *vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001017 struct vt_notifier_param param = { .vc = vc };
Kay Sievers4995f8e2009-03-09 14:18:52 +01001018
Samuel Thibaultb293d752007-10-18 23:39:17 -07001019 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001020 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001022 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001023 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001024 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (currcons >= MIN_NR_CONSOLES)
1026 kfree(vc);
1027 vc_cons[currcons].d = NULL;
1028 }
1029}
1030
1031/*
1032 * VT102 emulator
1033 */
1034
1035#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1036#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1037#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1038
1039#define decarm VC_REPEAT
1040#define decckm VC_CKMODE
1041#define kbdapplic VC_APPLIC
1042#define lnm VC_CRLF
1043
1044/*
1045 * this is what the terminal answers to a ESC-Z or csi0c query.
1046 */
1047#define VT100ID "\033[?1;2c"
1048#define VT102ID "\033[?6c"
1049
1050unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1051 8,12,10,14, 9,13,11,15 };
1052
1053/* the default colour table, for VGA+ colour systems */
1054int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1055 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1056int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1057 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1058int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1059 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1060
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001061module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1062module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1063module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065/*
1066 * gotoxy() must verify all boundaries, because the arguments
1067 * might also be negative. If the given position is out of
1068 * bounds, the cursor is placed at the nearest margin.
1069 */
1070static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1071{
1072 int min_y, max_y;
1073
1074 if (new_x < 0)
1075 vc->vc_x = 0;
1076 else {
1077 if (new_x >= vc->vc_cols)
1078 vc->vc_x = vc->vc_cols - 1;
1079 else
1080 vc->vc_x = new_x;
1081 }
1082
1083 if (vc->vc_decom) {
1084 min_y = vc->vc_top;
1085 max_y = vc->vc_bottom;
1086 } else {
1087 min_y = 0;
1088 max_y = vc->vc_rows;
1089 }
1090 if (new_y < min_y)
1091 vc->vc_y = min_y;
1092 else if (new_y >= max_y)
1093 vc->vc_y = max_y - 1;
1094 else
1095 vc->vc_y = new_y;
1096 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1097 vc->vc_need_wrap = 0;
1098}
1099
1100/* for absolute user moves, when decom is set */
1101static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1102{
1103 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1104}
1105
1106void scrollback(struct vc_data *vc, int lines)
1107{
1108 if (!lines)
1109 lines = vc->vc_rows / 2;
1110 scrolldelta(-lines);
1111}
1112
1113void scrollfront(struct vc_data *vc, int lines)
1114{
1115 if (!lines)
1116 lines = vc->vc_rows / 2;
1117 scrolldelta(lines);
1118}
1119
1120static void lf(struct vc_data *vc)
1121{
1122 /* don't scroll if above bottom of scrolling region, or
1123 * if below scrolling region
1124 */
1125 if (vc->vc_y + 1 == vc->vc_bottom)
1126 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1127 else if (vc->vc_y < vc->vc_rows - 1) {
1128 vc->vc_y++;
1129 vc->vc_pos += vc->vc_size_row;
1130 }
1131 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001132 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
1135static void ri(struct vc_data *vc)
1136{
1137 /* don't scroll if below top of scrolling region, or
1138 * if above scrolling region
1139 */
1140 if (vc->vc_y == vc->vc_top)
1141 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1142 else if (vc->vc_y > 0) {
1143 vc->vc_y--;
1144 vc->vc_pos -= vc->vc_size_row;
1145 }
1146 vc->vc_need_wrap = 0;
1147}
1148
1149static inline void cr(struct vc_data *vc)
1150{
1151 vc->vc_pos -= vc->vc_x << 1;
1152 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001153 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
1156static inline void bs(struct vc_data *vc)
1157{
1158 if (vc->vc_x) {
1159 vc->vc_pos -= 2;
1160 vc->vc_x--;
1161 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001162 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164}
1165
1166static inline void del(struct vc_data *vc)
1167{
1168 /* ignored */
1169}
1170
1171static void csi_J(struct vc_data *vc, int vpar)
1172{
1173 unsigned int count;
1174 unsigned short * start;
1175
1176 switch (vpar) {
1177 case 0: /* erase from cursor to end of display */
1178 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1179 start = (unsigned short *)vc->vc_pos;
1180 if (DO_UPDATE(vc)) {
1181 /* do in two stages */
1182 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1183 vc->vc_cols - vc->vc_x);
1184 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1185 vc->vc_rows - vc->vc_y - 1,
1186 vc->vc_cols);
1187 }
1188 break;
1189 case 1: /* erase from start to cursor */
1190 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1191 start = (unsigned short *)vc->vc_origin;
1192 if (DO_UPDATE(vc)) {
1193 /* do in two stages */
1194 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1195 vc->vc_cols);
1196 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1197 vc->vc_x + 1);
1198 }
1199 break;
1200 case 2: /* erase whole display */
1201 count = vc->vc_cols * vc->vc_rows;
1202 start = (unsigned short *)vc->vc_origin;
1203 if (DO_UPDATE(vc))
1204 vc->vc_sw->con_clear(vc, 0, 0,
1205 vc->vc_rows,
1206 vc->vc_cols);
1207 break;
1208 default:
1209 return;
1210 }
1211 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1212 vc->vc_need_wrap = 0;
1213}
1214
1215static void csi_K(struct vc_data *vc, int vpar)
1216{
1217 unsigned int count;
1218 unsigned short * start;
1219
1220 switch (vpar) {
1221 case 0: /* erase from cursor to end of line */
1222 count = vc->vc_cols - vc->vc_x;
1223 start = (unsigned short *)vc->vc_pos;
1224 if (DO_UPDATE(vc))
1225 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1226 vc->vc_cols - vc->vc_x);
1227 break;
1228 case 1: /* erase from start of line to cursor */
1229 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1230 count = vc->vc_x + 1;
1231 if (DO_UPDATE(vc))
1232 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1233 vc->vc_x + 1);
1234 break;
1235 case 2: /* erase whole line */
1236 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1237 count = vc->vc_cols;
1238 if (DO_UPDATE(vc))
1239 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1240 vc->vc_cols);
1241 break;
1242 default:
1243 return;
1244 }
1245 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1246 vc->vc_need_wrap = 0;
1247}
1248
1249static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1250{ /* not vt100? */
1251 int count;
1252
1253 if (!vpar)
1254 vpar++;
1255 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1256
1257 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1258 if (DO_UPDATE(vc))
1259 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1260 vc->vc_need_wrap = 0;
1261}
1262
1263static void default_attr(struct vc_data *vc)
1264{
1265 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001266 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 vc->vc_underline = 0;
1268 vc->vc_reverse = 0;
1269 vc->vc_blink = 0;
1270 vc->vc_color = vc->vc_def_color;
1271}
1272
Torben Hohnac751ef2011-01-25 15:07:35 -08001273/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274static void csi_m(struct vc_data *vc)
1275{
1276 int i;
1277
1278 for (i = 0; i <= vc->vc_npar; i++)
1279 switch (vc->vc_par[i]) {
1280 case 0: /* all attributes off */
1281 default_attr(vc);
1282 break;
1283 case 1:
1284 vc->vc_intensity = 2;
1285 break;
1286 case 2:
1287 vc->vc_intensity = 0;
1288 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001289 case 3:
1290 vc->vc_italic = 1;
1291 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 case 4:
1293 vc->vc_underline = 1;
1294 break;
1295 case 5:
1296 vc->vc_blink = 1;
1297 break;
1298 case 7:
1299 vc->vc_reverse = 1;
1300 break;
1301 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1302 * Select primary font, don't display
1303 * control chars if defined, don't set
1304 * bit 8 on output.
1305 */
1306 vc->vc_translate = set_translate(vc->vc_charset == 0
1307 ? vc->vc_G0_charset
1308 : vc->vc_G1_charset, vc);
1309 vc->vc_disp_ctrl = 0;
1310 vc->vc_toggle_meta = 0;
1311 break;
1312 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1313 * Select first alternate font, lets
1314 * chars < 32 be displayed as ROM chars.
1315 */
1316 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1317 vc->vc_disp_ctrl = 1;
1318 vc->vc_toggle_meta = 0;
1319 break;
1320 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1321 * Select second alternate font, toggle
1322 * high bit before displaying as ROM char.
1323 */
1324 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1325 vc->vc_disp_ctrl = 1;
1326 vc->vc_toggle_meta = 1;
1327 break;
1328 case 21:
1329 case 22:
1330 vc->vc_intensity = 1;
1331 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001332 case 23:
1333 vc->vc_italic = 0;
1334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 case 24:
1336 vc->vc_underline = 0;
1337 break;
1338 case 25:
1339 vc->vc_blink = 0;
1340 break;
1341 case 27:
1342 vc->vc_reverse = 0;
1343 break;
1344 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1345 * Enables underscore, white foreground
1346 * with white underscore (Linux - use
1347 * default foreground).
1348 */
1349 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1350 vc->vc_underline = 1;
1351 break;
1352 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1353 * Disable underline option.
1354 * Reset colour to default? It did this
1355 * before...
1356 */
1357 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1358 vc->vc_underline = 0;
1359 break;
1360 case 49:
1361 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1362 break;
1363 default:
1364 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1365 vc->vc_color = color_table[vc->vc_par[i] - 30]
1366 | (vc->vc_color & 0xf0);
1367 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1368 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1369 | (vc->vc_color & 0x0f);
1370 break;
1371 }
1372 update_attr(vc);
1373}
1374
1375static void respond_string(const char *p, struct tty_struct *tty)
1376{
1377 while (*p) {
1378 tty_insert_flip_char(tty, *p, 0);
1379 p++;
1380 }
1381 con_schedule_flip(tty);
1382}
1383
1384static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1385{
1386 char buf[40];
1387
1388 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1389 respond_string(buf, tty);
1390}
1391
1392static inline void status_report(struct tty_struct *tty)
1393{
1394 respond_string("\033[0n", tty); /* Terminal ok */
1395}
1396
1397static inline void respond_ID(struct tty_struct * tty)
1398{
1399 respond_string(VT102ID, tty);
1400}
1401
1402void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1403{
1404 char buf[8];
1405
1406 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1407 (char)('!' + mry));
1408 respond_string(buf, tty);
1409}
1410
1411/* invoked via ioctl(TIOCLINUX) and through set_selection */
1412int mouse_reporting(void)
1413{
1414 return vc_cons[fg_console].d->vc_report_mouse;
1415}
1416
Torben Hohnac751ef2011-01-25 15:07:35 -08001417/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static void set_mode(struct vc_data *vc, int on_off)
1419{
1420 int i;
1421
1422 for (i = 0; i <= vc->vc_npar; i++)
1423 if (vc->vc_ques) {
1424 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1425 case 1: /* Cursor keys send ^[Ox/^[[x */
1426 if (on_off)
1427 set_kbd(vc, decckm);
1428 else
1429 clr_kbd(vc, decckm);
1430 break;
1431 case 3: /* 80/132 mode switch unimplemented */
1432 vc->vc_deccolm = on_off;
1433#if 0
1434 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1435 /* this alone does not suffice; some user mode
1436 utility has to change the hardware regs */
1437#endif
1438 break;
1439 case 5: /* Inverted screen on/off */
1440 if (vc->vc_decscnm != on_off) {
1441 vc->vc_decscnm = on_off;
1442 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1443 update_attr(vc);
1444 }
1445 break;
1446 case 6: /* Origin relative/absolute */
1447 vc->vc_decom = on_off;
1448 gotoxay(vc, 0, 0);
1449 break;
1450 case 7: /* Autowrap on/off */
1451 vc->vc_decawm = on_off;
1452 break;
1453 case 8: /* Autorepeat on/off */
1454 if (on_off)
1455 set_kbd(vc, decarm);
1456 else
1457 clr_kbd(vc, decarm);
1458 break;
1459 case 9:
1460 vc->vc_report_mouse = on_off ? 1 : 0;
1461 break;
1462 case 25: /* Cursor on/off */
1463 vc->vc_deccm = on_off;
1464 break;
1465 case 1000:
1466 vc->vc_report_mouse = on_off ? 2 : 0;
1467 break;
1468 }
1469 } else {
1470 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1471 case 3: /* Monitor (display ctrls) */
1472 vc->vc_disp_ctrl = on_off;
1473 break;
1474 case 4: /* Insert Mode on/off */
1475 vc->vc_decim = on_off;
1476 break;
1477 case 20: /* Lf, Enter == CrLf/Lf */
1478 if (on_off)
1479 set_kbd(vc, lnm);
1480 else
1481 clr_kbd(vc, lnm);
1482 break;
1483 }
1484 }
1485}
1486
Torben Hohnac751ef2011-01-25 15:07:35 -08001487/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488static void setterm_command(struct vc_data *vc)
1489{
1490 switch(vc->vc_par[0]) {
1491 case 1: /* set color for underline mode */
1492 if (vc->vc_can_do_color &&
1493 vc->vc_par[1] < 16) {
1494 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1495 if (vc->vc_underline)
1496 update_attr(vc);
1497 }
1498 break;
1499 case 2: /* set color for half intensity mode */
1500 if (vc->vc_can_do_color &&
1501 vc->vc_par[1] < 16) {
1502 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1503 if (vc->vc_intensity == 0)
1504 update_attr(vc);
1505 }
1506 break;
1507 case 8: /* store colors as defaults */
1508 vc->vc_def_color = vc->vc_attr;
1509 if (vc->vc_hi_font_mask == 0x100)
1510 vc->vc_def_color >>= 1;
1511 default_attr(vc);
1512 update_attr(vc);
1513 break;
1514 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001515 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 poke_blanked_console();
1517 break;
1518 case 10: /* set bell frequency in Hz */
1519 if (vc->vc_npar >= 1)
1520 vc->vc_bell_pitch = vc->vc_par[1];
1521 else
1522 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1523 break;
1524 case 11: /* set bell duration in msec */
1525 if (vc->vc_npar >= 1)
1526 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1527 vc->vc_par[1] * HZ / 1000 : 0;
1528 else
1529 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1530 break;
1531 case 12: /* bring specified console to the front */
1532 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1533 set_console(vc->vc_par[1] - 1);
1534 break;
1535 case 13: /* unblank the screen */
1536 poke_blanked_console();
1537 break;
1538 case 14: /* set vesa powerdown interval */
1539 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1540 break;
1541 case 15: /* activate the previous console */
1542 set_console(last_console);
1543 break;
1544 }
1545}
1546
Torben Hohnac751ef2011-01-25 15:07:35 -08001547/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548static void csi_at(struct vc_data *vc, unsigned int nr)
1549{
1550 if (nr > vc->vc_cols - vc->vc_x)
1551 nr = vc->vc_cols - vc->vc_x;
1552 else if (!nr)
1553 nr = 1;
1554 insert_char(vc, nr);
1555}
1556
Torben Hohnac751ef2011-01-25 15:07:35 -08001557/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558static void csi_L(struct vc_data *vc, unsigned int nr)
1559{
1560 if (nr > vc->vc_rows - vc->vc_y)
1561 nr = vc->vc_rows - vc->vc_y;
1562 else if (!nr)
1563 nr = 1;
1564 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1565 vc->vc_need_wrap = 0;
1566}
1567
Torben Hohnac751ef2011-01-25 15:07:35 -08001568/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569static void csi_P(struct vc_data *vc, unsigned int nr)
1570{
1571 if (nr > vc->vc_cols - vc->vc_x)
1572 nr = vc->vc_cols - vc->vc_x;
1573 else if (!nr)
1574 nr = 1;
1575 delete_char(vc, nr);
1576}
1577
Torben Hohnac751ef2011-01-25 15:07:35 -08001578/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579static void csi_M(struct vc_data *vc, unsigned int nr)
1580{
1581 if (nr > vc->vc_rows - vc->vc_y)
1582 nr = vc->vc_rows - vc->vc_y;
1583 else if (!nr)
1584 nr=1;
1585 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1586 vc->vc_need_wrap = 0;
1587}
1588
Torben Hohnac751ef2011-01-25 15:07:35 -08001589/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590static void save_cur(struct vc_data *vc)
1591{
1592 vc->vc_saved_x = vc->vc_x;
1593 vc->vc_saved_y = vc->vc_y;
1594 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001595 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 vc->vc_s_underline = vc->vc_underline;
1597 vc->vc_s_blink = vc->vc_blink;
1598 vc->vc_s_reverse = vc->vc_reverse;
1599 vc->vc_s_charset = vc->vc_charset;
1600 vc->vc_s_color = vc->vc_color;
1601 vc->vc_saved_G0 = vc->vc_G0_charset;
1602 vc->vc_saved_G1 = vc->vc_G1_charset;
1603}
1604
Torben Hohnac751ef2011-01-25 15:07:35 -08001605/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606static void restore_cur(struct vc_data *vc)
1607{
1608 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1609 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001610 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 vc->vc_underline = vc->vc_s_underline;
1612 vc->vc_blink = vc->vc_s_blink;
1613 vc->vc_reverse = vc->vc_s_reverse;
1614 vc->vc_charset = vc->vc_s_charset;
1615 vc->vc_color = vc->vc_s_color;
1616 vc->vc_G0_charset = vc->vc_saved_G0;
1617 vc->vc_G1_charset = vc->vc_saved_G1;
1618 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1619 update_attr(vc);
1620 vc->vc_need_wrap = 0;
1621}
1622
1623enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1624 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1625 ESpalette };
1626
Torben Hohnac751ef2011-01-25 15:07:35 -08001627/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628static void reset_terminal(struct vc_data *vc, int do_clear)
1629{
1630 vc->vc_top = 0;
1631 vc->vc_bottom = vc->vc_rows;
1632 vc->vc_state = ESnormal;
1633 vc->vc_ques = 0;
1634 vc->vc_translate = set_translate(LAT1_MAP, vc);
1635 vc->vc_G0_charset = LAT1_MAP;
1636 vc->vc_G1_charset = GRAF_MAP;
1637 vc->vc_charset = 0;
1638 vc->vc_need_wrap = 0;
1639 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001640 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 vc->vc_utf_count = 0;
1642
1643 vc->vc_disp_ctrl = 0;
1644 vc->vc_toggle_meta = 0;
1645
1646 vc->vc_decscnm = 0;
1647 vc->vc_decom = 0;
1648 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001649 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 vc->vc_decim = 0;
1651
1652 set_kbd(vc, decarm);
1653 clr_kbd(vc, decckm);
1654 clr_kbd(vc, kbdapplic);
1655 clr_kbd(vc, lnm);
1656 kbd_table[vc->vc_num].lockstate = 0;
1657 kbd_table[vc->vc_num].slockstate = 0;
1658 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1659 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1660 /* do not do set_leds here because this causes an endless tasklet loop
1661 when the keyboard hasn't been initialized yet */
1662
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001663 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 vc->vc_complement_mask = vc->vc_s_complement_mask;
1665
1666 default_attr(vc);
1667 update_attr(vc);
1668
1669 vc->vc_tab_stop[0] = 0x01010100;
1670 vc->vc_tab_stop[1] =
1671 vc->vc_tab_stop[2] =
1672 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001673 vc->vc_tab_stop[4] =
1674 vc->vc_tab_stop[5] =
1675 vc->vc_tab_stop[6] =
1676 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
1678 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1679 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1680
1681 gotoxy(vc, 0, 0);
1682 save_cur(vc);
1683 if (do_clear)
1684 csi_J(vc, 2);
1685}
1686
Torben Hohnac751ef2011-01-25 15:07:35 -08001687/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1689{
1690 /*
1691 * Control characters can be used in the _middle_
1692 * of an escape sequence.
1693 */
1694 switch (c) {
1695 case 0:
1696 return;
1697 case 7:
1698 if (vc->vc_bell_duration)
1699 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1700 return;
1701 case 8:
1702 bs(vc);
1703 return;
1704 case 9:
1705 vc->vc_pos -= (vc->vc_x << 1);
1706 while (vc->vc_x < vc->vc_cols - 1) {
1707 vc->vc_x++;
1708 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1709 break;
1710 }
1711 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001712 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 return;
1714 case 10: case 11: case 12:
1715 lf(vc);
1716 if (!is_kbd(vc, lnm))
1717 return;
1718 case 13:
1719 cr(vc);
1720 return;
1721 case 14:
1722 vc->vc_charset = 1;
1723 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1724 vc->vc_disp_ctrl = 1;
1725 return;
1726 case 15:
1727 vc->vc_charset = 0;
1728 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1729 vc->vc_disp_ctrl = 0;
1730 return;
1731 case 24: case 26:
1732 vc->vc_state = ESnormal;
1733 return;
1734 case 27:
1735 vc->vc_state = ESesc;
1736 return;
1737 case 127:
1738 del(vc);
1739 return;
1740 case 128+27:
1741 vc->vc_state = ESsquare;
1742 return;
1743 }
1744 switch(vc->vc_state) {
1745 case ESesc:
1746 vc->vc_state = ESnormal;
1747 switch (c) {
1748 case '[':
1749 vc->vc_state = ESsquare;
1750 return;
1751 case ']':
1752 vc->vc_state = ESnonstd;
1753 return;
1754 case '%':
1755 vc->vc_state = ESpercent;
1756 return;
1757 case 'E':
1758 cr(vc);
1759 lf(vc);
1760 return;
1761 case 'M':
1762 ri(vc);
1763 return;
1764 case 'D':
1765 lf(vc);
1766 return;
1767 case 'H':
1768 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1769 return;
1770 case 'Z':
1771 respond_ID(tty);
1772 return;
1773 case '7':
1774 save_cur(vc);
1775 return;
1776 case '8':
1777 restore_cur(vc);
1778 return;
1779 case '(':
1780 vc->vc_state = ESsetG0;
1781 return;
1782 case ')':
1783 vc->vc_state = ESsetG1;
1784 return;
1785 case '#':
1786 vc->vc_state = EShash;
1787 return;
1788 case 'c':
1789 reset_terminal(vc, 1);
1790 return;
1791 case '>': /* Numeric keypad */
1792 clr_kbd(vc, kbdapplic);
1793 return;
1794 case '=': /* Appl. keypad */
1795 set_kbd(vc, kbdapplic);
1796 return;
1797 }
1798 return;
1799 case ESnonstd:
1800 if (c=='P') { /* palette escape sequence */
1801 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1802 vc->vc_par[vc->vc_npar] = 0;
1803 vc->vc_npar = 0;
1804 vc->vc_state = ESpalette;
1805 return;
1806 } else if (c=='R') { /* reset palette */
1807 reset_palette(vc);
1808 vc->vc_state = ESnormal;
1809 } else
1810 vc->vc_state = ESnormal;
1811 return;
1812 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001813 if (isxdigit(c)) {
1814 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (vc->vc_npar == 7) {
1816 int i = vc->vc_par[0] * 3, j = 1;
1817 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1818 vc->vc_palette[i++] += vc->vc_par[j++];
1819 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1820 vc->vc_palette[i++] += vc->vc_par[j++];
1821 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1822 vc->vc_palette[i] += vc->vc_par[j];
1823 set_palette(vc);
1824 vc->vc_state = ESnormal;
1825 }
1826 } else
1827 vc->vc_state = ESnormal;
1828 return;
1829 case ESsquare:
1830 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1831 vc->vc_par[vc->vc_npar] = 0;
1832 vc->vc_npar = 0;
1833 vc->vc_state = ESgetpars;
1834 if (c == '[') { /* Function key */
1835 vc->vc_state=ESfunckey;
1836 return;
1837 }
1838 vc->vc_ques = (c == '?');
1839 if (vc->vc_ques)
1840 return;
1841 case ESgetpars:
1842 if (c == ';' && vc->vc_npar < NPAR - 1) {
1843 vc->vc_npar++;
1844 return;
1845 } else if (c>='0' && c<='9') {
1846 vc->vc_par[vc->vc_npar] *= 10;
1847 vc->vc_par[vc->vc_npar] += c - '0';
1848 return;
1849 } else
1850 vc->vc_state = ESgotpars;
1851 case ESgotpars:
1852 vc->vc_state = ESnormal;
1853 switch(c) {
1854 case 'h':
1855 set_mode(vc, 1);
1856 return;
1857 case 'l':
1858 set_mode(vc, 0);
1859 return;
1860 case 'c':
1861 if (vc->vc_ques) {
1862 if (vc->vc_par[0])
1863 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1864 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001865 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 return;
1867 }
1868 break;
1869 case 'm':
1870 if (vc->vc_ques) {
1871 clear_selection();
1872 if (vc->vc_par[0])
1873 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1874 else
1875 vc->vc_complement_mask = vc->vc_s_complement_mask;
1876 return;
1877 }
1878 break;
1879 case 'n':
1880 if (!vc->vc_ques) {
1881 if (vc->vc_par[0] == 5)
1882 status_report(tty);
1883 else if (vc->vc_par[0] == 6)
1884 cursor_report(vc, tty);
1885 }
1886 return;
1887 }
1888 if (vc->vc_ques) {
1889 vc->vc_ques = 0;
1890 return;
1891 }
1892 switch(c) {
1893 case 'G': case '`':
1894 if (vc->vc_par[0])
1895 vc->vc_par[0]--;
1896 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1897 return;
1898 case 'A':
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 'B': case 'e':
1904 if (!vc->vc_par[0])
1905 vc->vc_par[0]++;
1906 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1907 return;
1908 case 'C': case 'a':
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 'D':
1914 if (!vc->vc_par[0])
1915 vc->vc_par[0]++;
1916 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1917 return;
1918 case 'E':
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 'F':
1924 if (!vc->vc_par[0])
1925 vc->vc_par[0]++;
1926 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1927 return;
1928 case 'd':
1929 if (vc->vc_par[0])
1930 vc->vc_par[0]--;
1931 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1932 return;
1933 case 'H': case 'f':
1934 if (vc->vc_par[0])
1935 vc->vc_par[0]--;
1936 if (vc->vc_par[1])
1937 vc->vc_par[1]--;
1938 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1939 return;
1940 case 'J':
1941 csi_J(vc, vc->vc_par[0]);
1942 return;
1943 case 'K':
1944 csi_K(vc, vc->vc_par[0]);
1945 return;
1946 case 'L':
1947 csi_L(vc, vc->vc_par[0]);
1948 return;
1949 case 'M':
1950 csi_M(vc, vc->vc_par[0]);
1951 return;
1952 case 'P':
1953 csi_P(vc, vc->vc_par[0]);
1954 return;
1955 case 'c':
1956 if (!vc->vc_par[0])
1957 respond_ID(tty);
1958 return;
1959 case 'g':
1960 if (!vc->vc_par[0])
1961 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1962 else if (vc->vc_par[0] == 3) {
1963 vc->vc_tab_stop[0] =
1964 vc->vc_tab_stop[1] =
1965 vc->vc_tab_stop[2] =
1966 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001967 vc->vc_tab_stop[4] =
1968 vc->vc_tab_stop[5] =
1969 vc->vc_tab_stop[6] =
1970 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 return;
1973 case 'm':
1974 csi_m(vc);
1975 return;
1976 case 'q': /* DECLL - but only 3 leds */
1977 /* map 0,1,2,3 to 0,1,2,4 */
1978 if (vc->vc_par[0] < 4)
1979 setledstate(kbd_table + vc->vc_num,
1980 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1981 return;
1982 case 'r':
1983 if (!vc->vc_par[0])
1984 vc->vc_par[0]++;
1985 if (!vc->vc_par[1])
1986 vc->vc_par[1] = vc->vc_rows;
1987 /* Minimum allowed region is 2 lines */
1988 if (vc->vc_par[0] < vc->vc_par[1] &&
1989 vc->vc_par[1] <= vc->vc_rows) {
1990 vc->vc_top = vc->vc_par[0] - 1;
1991 vc->vc_bottom = vc->vc_par[1];
1992 gotoxay(vc, 0, 0);
1993 }
1994 return;
1995 case 's':
1996 save_cur(vc);
1997 return;
1998 case 'u':
1999 restore_cur(vc);
2000 return;
2001 case 'X':
2002 csi_X(vc, vc->vc_par[0]);
2003 return;
2004 case '@':
2005 csi_at(vc, vc->vc_par[0]);
2006 return;
2007 case ']': /* setterm functions */
2008 setterm_command(vc);
2009 return;
2010 }
2011 return;
2012 case ESpercent:
2013 vc->vc_state = ESnormal;
2014 switch (c) {
2015 case '@': /* defined in ISO 2022 */
2016 vc->vc_utf = 0;
2017 return;
2018 case 'G': /* prelim official escape code */
2019 case '8': /* retained for compatibility */
2020 vc->vc_utf = 1;
2021 return;
2022 }
2023 return;
2024 case ESfunckey:
2025 vc->vc_state = ESnormal;
2026 return;
2027 case EShash:
2028 vc->vc_state = ESnormal;
2029 if (c == '8') {
2030 /* DEC screen alignment test. kludge :-) */
2031 vc->vc_video_erase_char =
2032 (vc->vc_video_erase_char & 0xff00) | 'E';
2033 csi_J(vc, 2);
2034 vc->vc_video_erase_char =
2035 (vc->vc_video_erase_char & 0xff00) | ' ';
2036 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2037 }
2038 return;
2039 case ESsetG0:
2040 if (c == '0')
2041 vc->vc_G0_charset = GRAF_MAP;
2042 else if (c == 'B')
2043 vc->vc_G0_charset = LAT1_MAP;
2044 else if (c == 'U')
2045 vc->vc_G0_charset = IBMPC_MAP;
2046 else if (c == 'K')
2047 vc->vc_G0_charset = USER_MAP;
2048 if (vc->vc_charset == 0)
2049 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2050 vc->vc_state = ESnormal;
2051 return;
2052 case ESsetG1:
2053 if (c == '0')
2054 vc->vc_G1_charset = GRAF_MAP;
2055 else if (c == 'B')
2056 vc->vc_G1_charset = LAT1_MAP;
2057 else if (c == 'U')
2058 vc->vc_G1_charset = IBMPC_MAP;
2059 else if (c == 'K')
2060 vc->vc_G1_charset = USER_MAP;
2061 if (vc->vc_charset == 1)
2062 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2063 vc->vc_state = ESnormal;
2064 return;
2065 default:
2066 vc->vc_state = ESnormal;
2067 }
2068}
2069
2070/* This is a temporary buffer used to prepare a tty console write
2071 * so that we can easily avoid touching user space while holding the
2072 * console spinlock. It is allocated in con_init and is shared by
2073 * this code and the vc_screen read/write tty calls.
2074 *
2075 * We have to allocate this statically in the kernel data section
2076 * since console_init (and thus con_init) are called before any
2077 * kernel memory allocation is available.
2078 */
2079char con_buf[CON_BUF_SIZE];
Matthias Kaehlckec831c332007-05-08 00:39:49 -07002080DEFINE_MUTEX(con_buf_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002082/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002083 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002084 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2085 */
2086struct interval {
2087 uint32_t first;
2088 uint32_t last;
2089};
2090
2091static int bisearch(uint32_t ucs, const struct interval *table, int max)
2092{
2093 int min = 0;
2094 int mid;
2095
2096 if (ucs < table[0].first || ucs > table[max].last)
2097 return 0;
2098 while (max >= min) {
2099 mid = (min + max) / 2;
2100 if (ucs > table[mid].last)
2101 min = mid + 1;
2102 else if (ucs < table[mid].first)
2103 max = mid - 1;
2104 else
2105 return 1;
2106 }
2107 return 0;
2108}
2109
2110static int is_double_width(uint32_t ucs)
2111{
2112 static const struct interval double_width[] = {
2113 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2114 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002115 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2116 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002117 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002118 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002119}
2120
Torben Hohnac751ef2011-01-25 15:07:35 -08002121/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2123{
2124#ifdef VT_BUF_VRAM_ONLY
2125#define FLUSH do { } while(0);
2126#else
2127#define FLUSH if (draw_x >= 0) { \
2128 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2129 draw_x = -1; \
2130 }
2131#endif
2132
2133 int c, tc, ok, n = 0, draw_x = -1;
2134 unsigned int currcons;
2135 unsigned long draw_from = 0, draw_to = 0;
2136 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002137 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002138 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002139 uint8_t rescan;
2140 uint8_t inverse;
2141 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 if (in_interrupt())
2145 return count;
2146
2147 might_sleep();
2148
Torben Hohnac751ef2011-01-25 15:07:35 -08002149 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 vc = tty->driver_data;
2151 if (vc == NULL) {
2152 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002153 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 return 0;
2155 }
2156
2157 currcons = vc->vc_num;
2158 if (!vc_cons_allocated(currcons)) {
2159 /* could this happen? */
Marcin Slusarz9074d962009-08-09 21:54:03 +02002160 printk_once("con_write: tty %d not allocated\n", currcons+1);
Torben Hohnac751ef2011-01-25 15:07:35 -08002161 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return 0;
2163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 himask = vc->vc_hi_font_mask;
2166 charmask = himask ? 0x1ff : 0xff;
2167
2168 /* undraw cursor first */
2169 if (IS_FG(vc))
2170 hide_cursor(vc);
2171
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002172 param.vc = vc;
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 while (!tty->stopped && count) {
2175 int orig = *buf;
2176 c = orig;
2177 buf++;
2178 n++;
2179 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002180 rescan = 0;
2181 inverse = 0;
2182 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 /* Do no translation at all in control states */
2185 if (vc->vc_state != ESnormal) {
2186 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002187 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002188 /* Combine UTF-8 into Unicode in vc_utf_char.
2189 * vc_utf_count is the number of continuation bytes still
2190 * expected to arrive.
2191 * vc_npar is the number of continuation bytes arrived so
2192 * far
2193 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002194rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002195 if ((c & 0xc0) == 0x80) {
2196 /* Continuation byte received */
2197 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002198 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002199 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2200 vc->vc_npar++;
2201 if (--vc->vc_utf_count) {
2202 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002204 }
2205 /* Got a whole character */
2206 c = vc->vc_utf_char;
2207 /* Reject overlong sequences */
2208 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2209 c > utf8_length_changes[vc->vc_npar])
2210 c = 0xfffd;
2211 } else {
2212 /* Unexpected continuation byte */
2213 vc->vc_utf_count = 0;
2214 c = 0xfffd;
2215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002217 /* Single ASCII byte or first byte of a sequence received */
2218 if (vc->vc_utf_count) {
2219 /* Continuation byte expected */
2220 rescan = 1;
2221 vc->vc_utf_count = 0;
2222 c = 0xfffd;
2223 } else if (c > 0x7f) {
2224 /* First byte of a multibyte sequence received */
2225 vc->vc_npar = 0;
2226 if ((c & 0xe0) == 0xc0) {
2227 vc->vc_utf_count = 1;
2228 vc->vc_utf_char = (c & 0x1f);
2229 } else if ((c & 0xf0) == 0xe0) {
2230 vc->vc_utf_count = 2;
2231 vc->vc_utf_char = (c & 0x0f);
2232 } else if ((c & 0xf8) == 0xf0) {
2233 vc->vc_utf_count = 3;
2234 vc->vc_utf_char = (c & 0x07);
2235 } else if ((c & 0xfc) == 0xf8) {
2236 vc->vc_utf_count = 4;
2237 vc->vc_utf_char = (c & 0x03);
2238 } else if ((c & 0xfe) == 0xfc) {
2239 vc->vc_utf_count = 5;
2240 vc->vc_utf_char = (c & 0x01);
2241 } else {
2242 /* 254 and 255 are invalid */
2243 c = 0xfffd;
2244 }
2245 if (vc->vc_utf_count) {
2246 /* Still need some bytes */
2247 continue;
2248 }
2249 }
2250 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002252 /* End of UTF-8 decoding. */
2253 /* c is the received character, or U+FFFD for invalid sequences. */
2254 /* Replace invalid Unicode code points with U+FFFD too */
2255 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2256 c = 0xfffd;
2257 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002258 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf62008-06-03 14:59:40 +01002259 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 }
2261
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002262 param.c = tc;
2263 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2264 &param) == NOTIFY_STOP)
2265 continue;
2266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* If the original code was a control character we
2268 * only allow a glyph to be displayed if the code is
2269 * not normally used (such as for cursor movement) or
2270 * if the disp_ctrl mode has been explicitly enabled.
2271 * Certain characters (as given by the CTRL_ALWAYS
2272 * bitmap) are always displayed as control characters,
2273 * as the console would be pretty useless without
2274 * them; to display an arbitrary font position use the
2275 * direct-to-font zone in UTF-8 mode.
2276 */
2277 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002278 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2279 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 && (c != 127 || vc->vc_disp_ctrl)
2281 && (c != 128+27);
2282
2283 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002284 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2285 if (is_double_width(c))
2286 width = 2;
2287 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 /* Now try to find out how to display it */
2289 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002290 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002291 if (tc == -1 || tc == -2) {
2292 continue; /* nothing to display */
2293 }
2294 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002295 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002296 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002297 This would make absolutely no sense with Unicode in mind,
2298 but do this for ASCII characters since a font may lack
2299 Unicode mapping info and we don't want to end up with
2300 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002301 tc = c;
2302 } else {
2303 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2304 tc = conv_uni_to_pc(vc, 0xfffd);
2305 if (tc < 0) {
2306 inverse = 1;
2307 tc = conv_uni_to_pc(vc, '?');
2308 if (tc < 0) tc = '?';
2309 }
2310 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002313 if (!inverse) {
2314 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002316 /* invert vc_attr */
2317 if (!vc->vc_can_do_color) {
2318 vc_attr = (vc->vc_attr) ^ 0x08;
2319 } else if (vc->vc_hi_font_mask == 0x100) {
2320 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2321 } else {
2322 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002323 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002324 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002325 }
2326
2327 while (1) {
2328 if (vc->vc_need_wrap || vc->vc_decim)
2329 FLUSH
2330 if (vc->vc_need_wrap) {
2331 cr(vc);
2332 lf(vc);
2333 }
2334 if (vc->vc_decim)
2335 insert_char(vc, 1);
2336 scr_writew(himask ?
2337 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2338 (vc_attr << 8) + tc,
2339 (u16 *) vc->vc_pos);
2340 if (DO_UPDATE(vc) && draw_x < 0) {
2341 draw_x = vc->vc_x;
2342 draw_from = vc->vc_pos;
2343 }
2344 if (vc->vc_x == vc->vc_cols - 1) {
2345 vc->vc_need_wrap = vc->vc_decawm;
2346 draw_to = vc->vc_pos + 2;
2347 } else {
2348 vc->vc_x++;
2349 draw_to = (vc->vc_pos += 2);
2350 }
2351
2352 if (!--width) break;
2353
2354 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2355 if (tc < 0) tc = ' ';
2356 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002357 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002358
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002359 if (inverse) {
2360 FLUSH
2361 }
2362
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002363 if (rescan) {
2364 rescan = 0;
2365 inverse = 0;
2366 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002367 c = orig;
2368 goto rescan_last_byte;
2369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 continue;
2371 }
2372 FLUSH
2373 do_con_trol(tty, vc, orig);
2374 }
2375 FLUSH
2376 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002377 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002378 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return n;
2380#undef FLUSH
2381}
2382
2383/*
2384 * This is the console switching callback.
2385 *
2386 * Doing console switching in a process context allows
2387 * us to do the switches asynchronously (needed when we want
2388 * to switch due to a keyboard interrupt). Synchronization
2389 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002390 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 */
David Howells65f27f32006-11-22 14:55:48 +00002392static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393{
Torben Hohnac751ef2011-01-25 15:07:35 -08002394 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 if (want_console >= 0) {
2397 if (want_console != fg_console &&
2398 vc_cons_allocated(want_console)) {
2399 hide_cursor(vc_cons[fg_console].d);
2400 change_console(vc_cons[want_console].d);
2401 /* we only changed when the console had already
2402 been allocated - a new console is not created
2403 in an interrupt routine */
2404 }
2405 want_console = -1;
2406 }
2407 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2408 do_poke_blanked_console = 0;
2409 poke_blanked_console();
2410 }
2411 if (scrollback_delta) {
2412 struct vc_data *vc = vc_cons[fg_console].d;
2413 clear_selection();
2414 if (vc->vc_mode == KD_TEXT)
2415 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2416 scrollback_delta = 0;
2417 }
2418 if (blank_timer_expired) {
2419 do_blank_screen(0);
2420 blank_timer_expired = 0;
2421 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002422 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423
Torben Hohnac751ef2011-01-25 15:07:35 -08002424 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425}
2426
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002427int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002429 struct vc_data *vc = vc_cons[fg_console].d;
2430
2431 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2432 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2433
2434 /*
2435 * Console switch will fail in console_callback() or
2436 * change_console() so there is no point scheduling
2437 * the callback
2438 *
2439 * Existing set_console() users don't check the return
2440 * value so this shouldn't break anything
2441 */
2442 return -EINVAL;
2443 }
2444
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 want_console = nr;
2446 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002447
2448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451struct tty_driver *console_driver;
2452
2453#ifdef CONFIG_VT_CONSOLE
2454
Bernhard Walle5ada9182009-12-14 18:00:43 -08002455/**
2456 * vt_kmsg_redirect() - Sets/gets the kernel message console
2457 * @new: The new virtual terminal number or -1 if the console should stay
2458 * unchanged
2459 *
2460 * By default, the kernel messages are always printed on the current virtual
2461 * console. However, the user may modify that default with the
2462 * TIOCL_SETKMSGREDIRECT ioctl call.
2463 *
2464 * This function sets the kernel message console to be @new. It returns the old
2465 * virtual console number. The virtual terminal number 0 (both as parameter and
2466 * return value) means no redirection (i.e. always printed on the currently
2467 * active console).
2468 *
2469 * The parameter -1 means that only the current console is returned, but the
2470 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2471 * case to make the code more understandable.
2472 *
2473 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2474 * the parameter and always returns 0.
2475 */
2476int vt_kmsg_redirect(int new)
2477{
2478 static int kmsg_con;
2479
2480 if (new != -1)
2481 return xchg(&kmsg_con, new);
2482 else
2483 return kmsg_con;
2484}
2485
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486/*
2487 * Console on virtual terminal
2488 *
2489 * The console must be locked when we get here.
2490 */
2491
2492static void vt_console_print(struct console *co, const char *b, unsigned count)
2493{
2494 struct vc_data *vc = vc_cons[fg_console].d;
2495 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002496 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 const ushort *start;
2498 ushort cnt = 0;
2499 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002500 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
2502 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002503 if (!printable)
2504 return;
2505 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 return;
2507
Bernhard Walle5ada9182009-12-14 18:00:43 -08002508 kmsg_console = vt_get_kmsg_redirect();
2509 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2510 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 /* read `x' only after setting currcons properly (otherwise
2513 the `x' macro will read the x of the foreground console). */
2514 myx = vc->vc_x;
2515
2516 if (!vc_cons_allocated(fg_console)) {
2517 /* impossible */
2518 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2519 goto quit;
2520 }
2521
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002522 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 goto quit;
2524
2525 /* undraw cursor first */
2526 if (IS_FG(vc))
2527 hide_cursor(vc);
2528
2529 start = (ushort *)vc->vc_pos;
2530
2531 /* Contrived structure to try to emulate original need_wrap behaviour
2532 * Problems caused when we have need_wrap set on '\n' character */
2533 while (count--) {
2534 c = *b++;
2535 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2536 if (cnt > 0) {
2537 if (CON_IS_VISIBLE(vc))
2538 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2539 vc->vc_x += cnt;
2540 if (vc->vc_need_wrap)
2541 vc->vc_x--;
2542 cnt = 0;
2543 }
2544 if (c == 8) { /* backspace */
2545 bs(vc);
2546 start = (ushort *)vc->vc_pos;
2547 myx = vc->vc_x;
2548 continue;
2549 }
2550 if (c != 13)
2551 lf(vc);
2552 cr(vc);
2553 start = (ushort *)vc->vc_pos;
2554 myx = vc->vc_x;
2555 if (c == 10 || c == 13)
2556 continue;
2557 }
2558 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002559 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 cnt++;
2561 if (myx == vc->vc_cols - 1) {
2562 vc->vc_need_wrap = 1;
2563 continue;
2564 }
2565 vc->vc_pos += 2;
2566 myx++;
2567 }
2568 if (cnt > 0) {
2569 if (CON_IS_VISIBLE(vc))
2570 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2571 vc->vc_x += cnt;
2572 if (vc->vc_x == vc->vc_cols) {
2573 vc->vc_x--;
2574 vc->vc_need_wrap = 1;
2575 }
2576 }
2577 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002578 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002581 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
2584static struct tty_driver *vt_console_device(struct console *c, int *index)
2585{
2586 *index = c->index ? c->index-1 : fg_console;
2587 return console_driver;
2588}
2589
2590static struct console vt_console_driver = {
2591 .name = "tty",
2592 .write = vt_console_print,
2593 .device = vt_console_device,
2594 .unblank = unblank_screen,
2595 .flags = CON_PRINTBUFFER,
2596 .index = -1,
2597};
2598#endif
2599
2600/*
2601 * Handling of Linux-specific VC ioctls
2602 */
2603
2604/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002605 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 *
2607 * There are some functions which don't need it.
2608 *
2609 * There are some functions which can sleep for arbitrary periods
2610 * (paste_selection) but we don't need the lock there anyway.
2611 *
2612 * set_selection has locking, and definitely needs it
2613 */
2614
2615int tioclinux(struct tty_struct *tty, unsigned long arg)
2616{
2617 char type, data;
2618 char __user *p = (char __user *)arg;
2619 int lines;
2620 int ret;
2621
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2623 return -EPERM;
2624 if (get_user(type, p))
2625 return -EFAULT;
2626 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002627
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 switch (type)
2629 {
2630 case TIOCL_SETSEL:
Torben Hohnac751ef2011-01-25 15:07:35 -08002631 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Torben Hohnac751ef2011-01-25 15:07:35 -08002633 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 break;
2635 case TIOCL_PASTESEL:
2636 ret = paste_selection(tty);
2637 break;
2638 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002639 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002641 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 break;
2643 case TIOCL_SELLOADLUT:
2644 ret = sel_loadlut(p);
2645 break;
2646 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002647
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 /*
2649 * Make it possible to react to Shift+Mousebutton.
2650 * Note that 'shift_state' is an undocumented
2651 * kernel-internal variable; programs not closely
2652 * related to the kernel should not use this.
2653 */
2654 data = shift_state;
2655 ret = __put_user(data, p);
2656 break;
2657 case TIOCL_GETMOUSEREPORTING:
2658 data = mouse_reporting();
2659 ret = __put_user(data, p);
2660 break;
2661 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002662 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002664 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002665 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002666 ret = __put_user(data, p);
2667 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 case TIOCL_SETKMSGREDIRECT:
2669 if (!capable(CAP_SYS_ADMIN)) {
2670 ret = -EPERM;
2671 } else {
2672 if (get_user(data, p+1))
2673 ret = -EFAULT;
2674 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002675 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 }
2677 break;
2678 case TIOCL_GETFGCONSOLE:
2679 ret = fg_console;
2680 break;
2681 case TIOCL_SCROLLCONSOLE:
2682 if (get_user(lines, (s32 __user *)(p+4))) {
2683 ret = -EFAULT;
2684 } else {
2685 scrollfront(vc_cons[fg_console].d, lines);
2686 ret = 0;
2687 }
2688 break;
2689 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08002690 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 ignore_poke = 1;
2692 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08002693 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 break;
2695 case TIOCL_BLANKEDSCREEN:
2696 ret = console_blanked;
2697 break;
2698 default:
2699 ret = -EINVAL;
2700 break;
2701 }
2702 return ret;
2703}
2704
2705/*
2706 * /dev/ttyN handling
2707 */
2708
2709static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2710{
2711 int retval;
2712
2713 retval = do_con_write(tty, buf, count);
2714 con_flush_chars(tty);
2715
2716 return retval;
2717}
2718
Alan Cox5d19f542008-04-30 00:54:08 -07002719static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002722 return 0; /* n_r3964 calls put_char() from interrupt context */
2723 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
2726static int con_write_room(struct tty_struct *tty)
2727{
2728 if (tty->stopped)
2729 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002730 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
2733static int con_chars_in_buffer(struct tty_struct *tty)
2734{
2735 return 0; /* we're not buffering */
2736}
2737
2738/*
2739 * con_throttle and con_unthrottle are only used for
2740 * paste_selection(), which has to stuff in a large number of
2741 * characters...
2742 */
2743static void con_throttle(struct tty_struct *tty)
2744{
2745}
2746
2747static void con_unthrottle(struct tty_struct *tty)
2748{
2749 struct vc_data *vc = tty->driver_data;
2750
2751 wake_up_interruptible(&vc->paste_wait);
2752}
2753
2754/*
2755 * Turn the Scroll-Lock LED on when the tty is stopped
2756 */
2757static void con_stop(struct tty_struct *tty)
2758{
2759 int console_num;
2760 if (!tty)
2761 return;
2762 console_num = tty->index;
2763 if (!vc_cons_allocated(console_num))
2764 return;
2765 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2766 set_leds();
2767}
2768
2769/*
2770 * Turn the Scroll-Lock LED off when the console is started
2771 */
2772static void con_start(struct tty_struct *tty)
2773{
2774 int console_num;
2775 if (!tty)
2776 return;
2777 console_num = tty->index;
2778 if (!vc_cons_allocated(console_num))
2779 return;
2780 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2781 set_leds();
2782}
2783
2784static void con_flush_chars(struct tty_struct *tty)
2785{
2786 struct vc_data *vc;
2787
2788 if (in_interrupt()) /* from flush_to_ldisc */
2789 return;
2790
2791 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08002792 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 vc = tty->driver_data;
2794 if (vc)
2795 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08002796 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
2799/*
2800 * Allocate the console screen memory.
2801 */
2802static int con_open(struct tty_struct *tty, struct file *filp)
2803{
2804 unsigned int currcons = tty->index;
2805 int ret = 0;
2806
Torben Hohnac751ef2011-01-25 15:07:35 -08002807 console_lock();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002808 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 ret = vc_allocate(currcons);
2810 if (ret == 0) {
2811 struct vc_data *vc = vc_cons[currcons].d;
Alan Coxfeebed62008-10-13 10:41:30 +01002812
2813 /* Still being freed */
Alan Cox8ce73262010-06-01 22:52:56 +02002814 if (vc->port.tty) {
Torben Hohnac751ef2011-01-25 15:07:35 -08002815 console_unlock();
Alan Coxfeebed62008-10-13 10:41:30 +01002816 return -ERESTARTSYS;
2817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 tty->driver_data = vc;
Alan Cox8ce73262010-06-01 22:52:56 +02002819 vc->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2822 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2823 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2824 }
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002825 if (vc->vc_utf)
2826 tty->termios->c_iflag |= IUTF8;
2827 else
2828 tty->termios->c_iflag &= ~IUTF8;
Torben Hohnac751ef2011-01-25 15:07:35 -08002829 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 return ret;
2831 }
2832 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002833 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 return ret;
2835}
2836
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837static void con_close(struct tty_struct *tty, struct file *filp)
2838{
Alan Coxfeebed62008-10-13 10:41:30 +01002839 /* Nothing to do - we defer to shutdown */
2840}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Alan Coxfeebed62008-10-13 10:41:30 +01002842static void con_shutdown(struct tty_struct *tty)
2843{
2844 struct vc_data *vc = tty->driver_data;
2845 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08002846 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02002847 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08002848 console_unlock();
Alan Coxfeebed62008-10-13 10:41:30 +01002849 tty_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}
2851
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002852static int default_italic_color = 2; // green (ASCII)
2853static int default_underline_color = 3; // cyan (ASCII)
2854module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2855module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2856
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857static void vc_init(struct vc_data *vc, unsigned int rows,
2858 unsigned int cols, int do_clear)
2859{
2860 int j, k ;
2861
2862 vc->vc_cols = cols;
2863 vc->vc_rows = rows;
2864 vc->vc_size_row = cols << 1;
2865 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2866
2867 set_origin(vc);
2868 vc->vc_pos = vc->vc_origin;
2869 reset_vc(vc);
2870 for (j=k=0; j<16; j++) {
2871 vc->vc_palette[k++] = default_red[j] ;
2872 vc->vc_palette[k++] = default_grn[j] ;
2873 vc->vc_palette[k++] = default_blu[j] ;
2874 }
2875 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002876 vc->vc_ulcolor = default_underline_color;
2877 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 vc->vc_halfcolor = 0x08; /* grey */
2879 init_waitqueue_head(&vc->paste_wait);
2880 reset_terminal(vc, do_clear);
2881}
2882
2883/*
2884 * This routine initializes console interrupts, and does nothing
2885 * else. If you want the screen to clear, call tty_write with
2886 * the appropriate escape-sequence.
2887 */
2888
2889static int __init con_init(void)
2890{
2891 const char *display_desc = NULL;
2892 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002893 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894
Torben Hohnac751ef2011-01-25 15:07:35 -08002895 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
2897 if (conswitchp)
2898 display_desc = conswitchp->con_startup();
2899 if (!display_desc) {
2900 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002901 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 return 0;
2903 }
2904
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002905 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2906 struct con_driver *con_driver = &registered_con_driver[i];
2907
2908 if (con_driver->con == NULL) {
2909 con_driver->con = conswitchp;
2910 con_driver->desc = display_desc;
2911 con_driver->flag = CON_DRIVER_FLAG_INIT;
2912 con_driver->first = 0;
2913 con_driver->last = MAX_NR_CONSOLES - 1;
2914 break;
2915 }
2916 }
2917
2918 for (i = 0; i < MAX_NR_CONSOLES; i++)
2919 con_driver_map[i] = conswitchp;
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (blankinterval) {
2922 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002923 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002927 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002928 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02002929 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002931 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 vc_init(vc, vc->vc_rows, vc->vc_cols,
2933 currcons || !vc->vc_sw->con_save_screen);
2934 }
2935 currcons = fg_console = 0;
2936 master_display_fg = vc = vc_cons[currcons].d;
2937 set_origin(vc);
2938 save_screen(vc);
2939 gotoxy(vc, vc->vc_x, vc->vc_y);
2940 csi_J(vc, 0);
2941 update_screen(vc);
2942 printk("Console: %s %s %dx%d",
2943 vc->vc_can_do_color ? "colour" : "mono",
2944 display_desc, vc->vc_cols, vc->vc_rows);
2945 printable = 1;
2946 printk("\n");
2947
Torben Hohnac751ef2011-01-25 15:07:35 -08002948 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
2950#ifdef CONFIG_VT_CONSOLE
2951 register_console(&vt_console_driver);
2952#endif
2953 return 0;
2954}
2955console_initcall(con_init);
2956
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002957static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 .open = con_open,
2959 .close = con_close,
2960 .write = con_write,
2961 .write_room = con_write_room,
2962 .put_char = con_put_char,
2963 .flush_chars = con_flush_chars,
2964 .chars_in_buffer = con_chars_in_buffer,
2965 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02002966#ifdef CONFIG_COMPAT
2967 .compat_ioctl = vt_compat_ioctl,
2968#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .stop = con_stop,
2970 .start = con_start,
2971 .throttle = con_throttle,
2972 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002973 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01002974 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975};
2976
Alan Coxd81ed102008-10-13 10:41:42 +01002977static struct cdev vc0_cdev;
2978
Kay Sieversfbc92a32010-12-01 18:51:05 +01002979static ssize_t show_tty_active(struct device *dev,
2980 struct device_attribute *attr, char *buf)
2981{
2982 return sprintf(buf, "tty%d\n", fg_console + 1);
2983}
2984static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2985
Alan Coxd81ed102008-10-13 10:41:42 +01002986int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987{
Alan Coxd81ed102008-10-13 10:41:42 +01002988 cdev_init(&vc0_cdev, console_fops);
2989 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2990 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2991 panic("Couldn't register /dev/tty0 driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01002992 tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2993 if (IS_ERR(tty0dev))
2994 tty0dev = NULL;
2995 else
Kay Sieversa2a6a822011-01-09 16:39:14 +01002996 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
Alan Coxd81ed102008-10-13 10:41:42 +01002997
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 vcs_init();
2999
3000 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3001 if (!console_driver)
3002 panic("Couldn't allocate console driver\n");
3003 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 console_driver->name = "tty";
3005 console_driver->name_base = 1;
3006 console_driver->major = TTY_MAJOR;
3007 console_driver->minor_start = 1;
3008 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3009 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003010 if (default_utf8)
3011 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3013 tty_set_operations(console_driver, &con_ops);
3014 if (tty_register_driver(console_driver))
3015 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 kbd_init();
3017 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018#ifdef CONFIG_MDA_CONSOLE
3019 mda_console_init();
3020#endif
3021 return 0;
3022}
3023
3024#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003025
3026static struct class *vtconsole_class;
3027
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003028static int bind_con_driver(const struct consw *csw, int first, int last,
3029 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003031 struct module *owner = csw->owner;
3032 const char *desc = NULL;
3033 struct con_driver *con_driver;
3034 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 if (!try_module_get(owner))
3037 return -ENODEV;
3038
Torben Hohnac751ef2011-01-25 15:07:35 -08003039 console_lock();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003040
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003041 /* check if driver is registered */
3042 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3043 con_driver = &registered_con_driver[i];
3044
3045 if (con_driver->con == csw) {
3046 desc = con_driver->desc;
3047 retval = 0;
3048 break;
3049 }
3050 }
3051
3052 if (retval)
3053 goto err;
3054
3055 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3056 csw->con_startup();
3057 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003059
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 if (deflt) {
3061 if (conswitchp)
3062 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003063
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 __module_get(owner);
3065 conswitchp = csw;
3066 }
3067
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003068 first = max(first, con_driver->first);
3069 last = min(last, con_driver->last);
3070
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 for (i = first; i <= last; i++) {
3072 int old_was_color;
3073 struct vc_data *vc = vc_cons[i].d;
3074
3075 if (con_driver_map[i])
3076 module_put(con_driver_map[i]->owner);
3077 __module_get(owner);
3078 con_driver_map[i] = csw;
3079
3080 if (!vc || !vc->vc_sw)
3081 continue;
3082
3083 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003084
David Hollister4ee1acc2006-06-26 00:26:41 -07003085 if (CON_IS_VISIBLE(vc)) {
3086 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003088 }
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 old_was_color = vc->vc_can_do_color;
3091 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003092 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003094 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 update_attr(vc);
3096
3097 /* If the console changed between mono <-> color, then
3098 * the attributes in the screenbuf will be wrong. The
3099 * following resets all attributes to something sane.
3100 */
3101 if (old_was_color != vc->vc_can_do_color)
3102 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 printk("Console: switching ");
3106 if (!deflt)
3107 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003108 if (j >= 0) {
3109 struct vc_data *vc = vc_cons[j].d;
3110
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003112 vc->vc_can_do_color ? "colour" : "mono",
3113 desc, vc->vc_cols, vc->vc_rows);
3114
3115 if (k >= 0) {
3116 vc = vc_cons[k].d;
3117 update_screen(vc);
3118 }
3119 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 printk("to %s\n", desc);
3121
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003122 retval = 0;
3123err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003124 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003126 return retval;
3127};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003129#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3130static int con_is_graphics(const struct consw *csw, int first, int last)
3131{
3132 int i, retval = 0;
3133
3134 for (i = first; i <= last; i++) {
3135 struct vc_data *vc = vc_cons[i].d;
3136
3137 if (vc && vc->vc_mode == KD_GRAPHICS) {
3138 retval = 1;
3139 break;
3140 }
3141 }
3142
3143 return retval;
3144}
3145
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003146/**
3147 * unbind_con_driver - unbind a console driver
3148 * @csw: pointer to console driver to unregister
3149 * @first: first in range of consoles that @csw should be unbound from
3150 * @last: last in range of consoles that @csw should be unbound from
3151 * @deflt: should next bound console driver be default after @csw is unbound?
3152 *
3153 * To unbind a driver from all possible consoles, pass 0 as @first and
3154 * %MAX_NR_CONSOLES as @last.
3155 *
3156 * @deflt controls whether the console that ends up replacing @csw should be
3157 * the default console.
3158 *
3159 * RETURNS:
3160 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3161 * or 0 on success.
3162 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003163int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003165 struct module *owner = csw->owner;
3166 const struct consw *defcsw = NULL;
3167 struct con_driver *con_driver = NULL, *con_back = NULL;
3168 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003170 if (!try_module_get(owner))
3171 return -ENODEV;
3172
Torben Hohnac751ef2011-01-25 15:07:35 -08003173 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003174
3175 /* check if driver is registered and if it is unbindable */
3176 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3177 con_driver = &registered_con_driver[i];
3178
3179 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003180 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003181 retval = 0;
3182 break;
3183 }
3184 }
3185
3186 if (retval) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003187 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003188 goto err;
3189 }
3190
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003191 retval = -ENODEV;
3192
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003193 /* check if backup driver exists */
3194 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3195 con_back = &registered_con_driver[i];
3196
3197 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003198 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003199 defcsw = con_back->con;
3200 retval = 0;
3201 break;
3202 }
3203 }
3204
3205 if (retval) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003206 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003207 goto err;
3208 }
3209
3210 if (!con_is_bound(csw)) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003211 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003212 goto err;
3213 }
3214
3215 first = max(first, con_driver->first);
3216 last = min(last, con_driver->last);
3217
3218 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 if (con_driver_map[i] == csw) {
3220 module_put(csw->owner);
3221 con_driver_map[i] = NULL;
3222 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003223 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003224
3225 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003226 const struct consw *defconsw = conswitchp;
3227
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003228 defcsw->con_startup();
3229 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003230 /*
3231 * vgacon may change the default driver to point
3232 * to dummycon, we restore it here...
3233 */
3234 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003235 }
3236
3237 if (!con_is_bound(csw))
3238 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3239
Torben Hohnac751ef2011-01-25 15:07:35 -08003240 console_unlock();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003241 /* ignore return value, binding should not fail */
3242 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003243err:
3244 module_put(owner);
3245 return retval;
3246
3247}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003248EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003249
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003250static int vt_bind(struct con_driver *con)
3251{
3252 const struct consw *defcsw = NULL, *csw = NULL;
3253 int i, more = 1, first = -1, last = -1, deflt = 0;
3254
3255 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3256 con_is_graphics(con->con, con->first, con->last))
3257 goto err;
3258
3259 csw = con->con;
3260
3261 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3262 struct con_driver *con = &registered_con_driver[i];
3263
3264 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3265 defcsw = con->con;
3266 break;
3267 }
3268 }
3269
3270 if (!defcsw)
3271 goto err;
3272
3273 while (more) {
3274 more = 0;
3275
3276 for (i = con->first; i <= con->last; i++) {
3277 if (con_driver_map[i] == defcsw) {
3278 if (first == -1)
3279 first = i;
3280 last = i;
3281 more = 1;
3282 } else if (first != -1)
3283 break;
3284 }
3285
3286 if (first == 0 && last == MAX_NR_CONSOLES -1)
3287 deflt = 1;
3288
3289 if (first != -1)
3290 bind_con_driver(csw, first, last, deflt);
3291
3292 first = -1;
3293 last = -1;
3294 deflt = 0;
3295 }
3296
3297err:
3298 return 0;
3299}
3300
3301static int vt_unbind(struct con_driver *con)
3302{
3303 const struct consw *csw = NULL;
3304 int i, more = 1, first = -1, last = -1, deflt = 0;
3305
3306 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3307 con_is_graphics(con->con, con->first, con->last))
3308 goto err;
3309
3310 csw = con->con;
3311
3312 while (more) {
3313 more = 0;
3314
3315 for (i = con->first; i <= con->last; i++) {
3316 if (con_driver_map[i] == csw) {
3317 if (first == -1)
3318 first = i;
3319 last = i;
3320 more = 1;
3321 } else if (first != -1)
3322 break;
3323 }
3324
3325 if (first == 0 && last == MAX_NR_CONSOLES -1)
3326 deflt = 1;
3327
3328 if (first != -1)
3329 unbind_con_driver(csw, first, last, deflt);
3330
3331 first = -1;
3332 last = -1;
3333 deflt = 0;
3334 }
3335
3336err:
3337 return 0;
3338}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003339#else
3340static inline int vt_bind(struct con_driver *con)
3341{
3342 return 0;
3343}
3344static inline int vt_unbind(struct con_driver *con)
3345{
3346 return 0;
3347}
3348#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003349
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003350static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003351 const char *buf, size_t count)
3352{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003353 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003354 int bind = simple_strtoul(buf, NULL, 0);
3355
3356 if (bind)
3357 vt_bind(con);
3358 else
3359 vt_unbind(con);
3360
3361 return count;
3362}
3363
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003364static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3365 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003366{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003367 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003368 int bind = con_is_bound(con->con);
3369
3370 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3371}
3372
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003373static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3374 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003375{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003376 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003377
3378 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3379 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3380 con->desc);
3381
3382}
3383
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003384static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003385 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3386 __ATTR(name, S_IRUGO, show_name, NULL),
3387};
3388
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003389static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003390{
3391 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003392 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003393
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003394 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003395 dev_set_drvdata(con->dev, con);
3396 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3397 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003398 if (error)
3399 break;
3400 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003401
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003402 if (error) {
3403 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003404 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003405 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3406 }
3407
3408 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003409}
3410
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003411static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003412{
3413 int i;
3414
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003415 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003416 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3417 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003418 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3419 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003420}
3421
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003422/**
3423 * con_is_bound - checks if driver is bound to the console
3424 * @csw: console driver
3425 *
3426 * RETURNS: zero if unbound, nonzero if bound
3427 *
3428 * Drivers can call this and if zero, they should release
3429 * all resources allocated on con_startup()
3430 */
3431int con_is_bound(const struct consw *csw)
3432{
3433 int i, bound = 0;
3434
3435 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3436 if (con_driver_map[i] == csw) {
3437 bound = 1;
3438 break;
3439 }
3440 }
3441
3442 return bound;
3443}
3444EXPORT_SYMBOL(con_is_bound);
3445
3446/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003447 * con_debug_enter - prepare the console for the kernel debugger
3448 * @sw: console driver
3449 *
3450 * Called when the console is taken over by the kernel debugger, this
3451 * function needs to save the current console state, then put the console
3452 * into a state suitable for the kernel debugger.
3453 *
3454 * RETURNS:
3455 * Zero on success, nonzero if a failure occurred when trying to prepare
3456 * the console for the debugger.
3457 */
3458int con_debug_enter(struct vc_data *vc)
3459{
3460 int ret = 0;
3461
3462 saved_fg_console = fg_console;
3463 saved_last_console = last_console;
3464 saved_want_console = want_console;
3465 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003466 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003467 vc->vc_mode = KD_TEXT;
3468 console_blanked = 0;
3469 if (vc->vc_sw->con_debug_enter)
3470 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003471#ifdef CONFIG_KGDB_KDB
3472 /* Set the initial LINES variable if it is not already set */
3473 if (vc->vc_rows < 999) {
3474 int linecount;
3475 char lns[4];
3476 const char *setargs[3] = {
3477 "set",
3478 "LINES",
3479 lns,
3480 };
3481 if (kdbgetintenv(setargs[0], &linecount)) {
3482 snprintf(lns, 4, "%i", vc->vc_rows);
3483 kdb_set(2, setargs);
3484 }
3485 }
3486#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003487 return ret;
3488}
3489EXPORT_SYMBOL_GPL(con_debug_enter);
3490
3491/**
3492 * con_debug_leave - restore console state
3493 * @sw: console driver
3494 *
3495 * Restore the console state to what it was before the kernel debugger
3496 * was invoked.
3497 *
3498 * RETURNS:
3499 * Zero on success, nonzero if a failure occurred when trying to restore
3500 * the console.
3501 */
3502int con_debug_leave(void)
3503{
3504 struct vc_data *vc;
3505 int ret = 0;
3506
3507 fg_console = saved_fg_console;
3508 last_console = saved_last_console;
3509 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003510 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003511 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3512
3513 vc = vc_cons[fg_console].d;
3514 if (vc->vc_sw->con_debug_leave)
3515 ret = vc->vc_sw->con_debug_leave(vc);
3516 return ret;
3517}
3518EXPORT_SYMBOL_GPL(con_debug_leave);
3519
3520/**
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003521 * register_con_driver - register console driver to console layer
3522 * @csw: console driver
3523 * @first: the first console to take over, minimum value is 0
3524 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3525 *
3526 * DESCRIPTION: This function registers a console driver which can later
3527 * bind to a range of consoles specified by @first and @last. It will
3528 * also initialize the console driver by calling con_startup().
3529 */
3530int register_con_driver(const struct consw *csw, int first, int last)
3531{
3532 struct module *owner = csw->owner;
3533 struct con_driver *con_driver;
3534 const char *desc;
3535 int i, retval = 0;
3536
3537 if (!try_module_get(owner))
3538 return -ENODEV;
3539
Torben Hohnac751ef2011-01-25 15:07:35 -08003540 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003541
3542 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3543 con_driver = &registered_con_driver[i];
3544
3545 /* already registered */
3546 if (con_driver->con == csw)
Dave Airliec55c63c2011-01-07 09:57:41 +10003547 retval = -EBUSY;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003548 }
3549
3550 if (retval)
3551 goto err;
3552
3553 desc = csw->con_startup();
3554
3555 if (!desc)
3556 goto err;
3557
3558 retval = -EINVAL;
3559
3560 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3561 con_driver = &registered_con_driver[i];
3562
3563 if (con_driver->con == NULL) {
3564 con_driver->con = csw;
3565 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003566 con_driver->node = i;
3567 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003568 CON_DRIVER_FLAG_INIT;
3569 con_driver->first = first;
3570 con_driver->last = last;
3571 retval = 0;
3572 break;
3573 }
3574 }
3575
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003576 if (retval)
3577 goto err;
3578
Alan Coxd81ed102008-10-13 10:41:42 +01003579 con_driver->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003580 MKDEV(0, con_driver->node),
3581 NULL, "vtcon%i",
3582 con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003583
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003584 if (IS_ERR(con_driver->dev)) {
3585 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003586 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003587 PTR_ERR(con_driver->dev));
3588 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003589 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003590 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003591 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003592
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003593err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003594 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003595 module_put(owner);
3596 return retval;
3597}
3598EXPORT_SYMBOL(register_con_driver);
3599
3600/**
3601 * unregister_con_driver - unregister console driver from console layer
3602 * @csw: console driver
3603 *
3604 * DESCRIPTION: All drivers that registers to the console layer must
3605 * call this function upon exit, or if the console driver is in a state
3606 * where it won't be able to handle console services, such as the
3607 * framebuffer console without loaded framebuffer drivers.
3608 *
3609 * The driver must unbind first prior to unregistration.
3610 */
3611int unregister_con_driver(const struct consw *csw)
3612{
3613 int i, retval = -ENODEV;
3614
Torben Hohnac751ef2011-01-25 15:07:35 -08003615 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003616
3617 /* cannot unregister a bound driver */
3618 if (con_is_bound(csw))
3619 goto err;
3620
3621 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3622 struct con_driver *con_driver = &registered_con_driver[i];
3623
3624 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003625 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003626 vtconsole_deinit_device(con_driver);
3627 device_destroy(vtconsole_class,
3628 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003629 con_driver->con = NULL;
3630 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003631 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003632 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003633 con_driver->flag = 0;
3634 con_driver->first = 0;
3635 con_driver->last = 0;
3636 retval = 0;
3637 break;
3638 }
3639 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003640err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003641 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003642 return retval;
3643}
3644EXPORT_SYMBOL(unregister_con_driver);
3645
3646/*
3647 * If we support more console drivers, this function is used
3648 * when a driver wants to take over some existing consoles
3649 * and become default driver for newly opened ones.
3650 *
3651 * take_over_console is basically a register followed by unbind
3652 */
3653int take_over_console(const struct consw *csw, int first, int last, int deflt)
3654{
3655 int err;
3656
3657 err = register_con_driver(csw, first, last);
Dave Airliec55c63c2011-01-07 09:57:41 +10003658 /* if we get an busy error we still want to bind the console driver
3659 * and return success, as we may have unbound the console driver
3660  * but not unregistered it.
3661 */
3662 if (err == -EBUSY)
3663 err = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003664 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003665 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003666
3667 return err;
3668}
3669
3670/*
3671 * give_up_console is a wrapper to unregister_con_driver. It will only
3672 * work if driver is fully unbound.
3673 */
3674void give_up_console(const struct consw *csw)
3675{
3676 unregister_con_driver(csw);
3677}
3678
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003679static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003680{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003681 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003682
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003683 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3684 if (IS_ERR(vtconsole_class)) {
3685 printk(KERN_WARNING "Unable to create vt console class; "
3686 "errno = %ld\n", PTR_ERR(vtconsole_class));
3687 vtconsole_class = NULL;
3688 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003689
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003690 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003691 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3692 struct con_driver *con = &registered_con_driver[i];
3693
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003694 if (con->con && !con->dev) {
Alan Coxd81ed102008-10-13 10:41:42 +01003695 con->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003696 MKDEV(0, con->node),
3697 NULL, "vtcon%i",
3698 con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003699
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003700 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003701 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003702 "device for %s; errno = %ld\n",
3703 con->desc, PTR_ERR(con->dev));
3704 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003705 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003706 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003707 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003708 }
3709 }
3710
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003711 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003712}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003713postcore_initcall(vtconsole_class_init);
3714
3715#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003716
3717/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 * Screen blanking
3719 */
3720
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003721static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003723 unsigned int mode;
3724
3725 if (get_user(mode, p + 1))
3726 return -EFAULT;
3727
3728 vesa_blank_mode = (mode < 4) ? mode : 0;
3729 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730}
3731
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732void do_blank_screen(int entering_gfx)
3733{
3734 struct vc_data *vc = vc_cons[fg_console].d;
3735 int i;
3736
3737 WARN_CONSOLE_UNLOCKED();
3738
3739 if (console_blanked) {
3740 if (blank_state == blank_vesa_wait) {
3741 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003742 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 }
3744 return;
3745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746
3747 /* entering graphics mode? */
3748 if (entering_gfx) {
3749 hide_cursor(vc);
3750 save_screen(vc);
3751 vc->vc_sw->con_blank(vc, -1, 1);
3752 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003753 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 set_origin(vc);
3755 return;
3756 }
3757
izumib6e8f002007-07-17 04:05:49 -07003758 if (blank_state != blank_normal_wait)
3759 return;
3760 blank_state = blank_off;
3761
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 /* don't blank graphics */
3763 if (vc->vc_mode != KD_TEXT) {
3764 console_blanked = fg_console + 1;
3765 return;
3766 }
3767
3768 hide_cursor(vc);
3769 del_timer_sync(&console_timer);
3770 blank_timer_expired = 0;
3771
3772 save_screen(vc);
3773 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003774 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775 console_blanked = fg_console + 1;
3776 if (i)
3777 set_origin(vc);
3778
3779 if (console_blank_hook && console_blank_hook(1))
3780 return;
3781
Ville Syrjalad060a322006-01-09 20:53:49 -08003782 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003783 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 mod_timer(&console_timer, jiffies + vesa_off_interval);
3785 }
Alan Cox8b92e872009-09-19 13:13:24 -07003786 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787}
3788EXPORT_SYMBOL(do_blank_screen);
3789
3790/*
3791 * Called by timer as well as from vt_console_driver
3792 */
3793void do_unblank_screen(int leaving_gfx)
3794{
3795 struct vc_data *vc;
3796
3797 /* This should now always be called from a "sane" (read: can schedule)
3798 * context for the sake of the low level drivers, except in the special
3799 * case of oops_in_progress
3800 */
3801 if (!oops_in_progress)
3802 might_sleep();
3803
3804 WARN_CONSOLE_UNLOCKED();
3805
3806 ignore_poke = 0;
3807 if (!console_blanked)
3808 return;
3809 if (!vc_cons_allocated(fg_console)) {
3810 /* impossible */
3811 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3812 return;
3813 }
3814 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003815 /* Try to unblank in oops case too */
3816 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 return; /* but leave console_blanked != 0 */
3818
3819 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003820 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 blank_state = blank_normal_wait;
3822 }
3823
3824 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003825 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 /* Low-level driver cannot restore -> do it ourselves */
3827 update_screen(vc);
3828 if (console_blank_hook)
3829 console_blank_hook(0);
3830 set_palette(vc);
3831 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003832 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833}
3834EXPORT_SYMBOL(do_unblank_screen);
3835
3836/*
3837 * This is called by the outside world to cause a forced unblank, mostly for
3838 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3839 * call it with 1 as an argument and so force a mode restore... that may kill
3840 * X or at least garbage the screen but would also make the Oops visible...
3841 */
3842void unblank_screen(void)
3843{
3844 do_unblank_screen(0);
3845}
3846
3847/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003848 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003850 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 */
3852static void blank_screen_t(unsigned long dummy)
3853{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003854 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003855 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003856 return;
3857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 blank_timer_expired = 1;
3859 schedule_work(&console_work);
3860}
3861
3862void poke_blanked_console(void)
3863{
3864 WARN_CONSOLE_UNLOCKED();
3865
3866 /* Add this so we quickly catch whoever might call us in a non
3867 * safe context. Nowadays, unblank_screen() isn't to be called in
3868 * atomic contexts and is allowed to schedule (with the special case
3869 * of oops_in_progress, but that isn't of any concern for this
3870 * function. --BenH.
3871 */
3872 might_sleep();
3873
3874 /* This isn't perfectly race free, but a race here would be mostly harmless,
3875 * at worse, we'll do a spurrious blank and it's unlikely
3876 */
3877 del_timer(&console_timer);
3878 blank_timer_expired = 0;
3879
3880 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3881 return;
3882 if (console_blanked)
3883 unblank_screen();
3884 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003885 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 blank_state = blank_normal_wait;
3887 }
3888}
3889
3890/*
3891 * Palettes
3892 */
3893
3894static void set_palette(struct vc_data *vc)
3895{
3896 WARN_CONSOLE_UNLOCKED();
3897
3898 if (vc->vc_mode != KD_GRAPHICS)
3899 vc->vc_sw->con_set_palette(vc, color_table);
3900}
3901
3902static int set_get_cmap(unsigned char __user *arg, int set)
3903{
3904 int i, j, k;
3905
3906 WARN_CONSOLE_UNLOCKED();
3907
3908 for (i = 0; i < 16; i++)
3909 if (set) {
3910 get_user(default_red[i], arg++);
3911 get_user(default_grn[i], arg++);
3912 get_user(default_blu[i], arg++);
3913 } else {
3914 put_user(default_red[i], arg++);
3915 put_user(default_grn[i], arg++);
3916 put_user(default_blu[i], arg++);
3917 }
3918 if (set) {
3919 for (i = 0; i < MAX_NR_CONSOLES; i++)
3920 if (vc_cons_allocated(i)) {
3921 for (j = k = 0; j < 16; j++) {
3922 vc_cons[i].d->vc_palette[k++] = default_red[j];
3923 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3924 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3925 }
3926 set_palette(vc_cons[i].d);
3927 }
3928 }
3929 return 0;
3930}
3931
3932/*
3933 * Load palette into the DAC registers. arg points to a colour
3934 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3935 */
3936
3937int con_set_cmap(unsigned char __user *arg)
3938{
3939 int rc;
3940
Torben Hohnac751ef2011-01-25 15:07:35 -08003941 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 rc = set_get_cmap (arg,1);
Torben Hohnac751ef2011-01-25 15:07:35 -08003943 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 return rc;
3946}
3947
3948int con_get_cmap(unsigned char __user *arg)
3949{
3950 int rc;
3951
Torben Hohnac751ef2011-01-25 15:07:35 -08003952 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 rc = set_get_cmap (arg,0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003954 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
3956 return rc;
3957}
3958
3959void reset_palette(struct vc_data *vc)
3960{
3961 int j, k;
3962 for (j=k=0; j<16; j++) {
3963 vc->vc_palette[k++] = default_red[j];
3964 vc->vc_palette[k++] = default_grn[j];
3965 vc->vc_palette[k++] = default_blu[j];
3966 }
3967 set_palette(vc);
3968}
3969
3970/*
3971 * Font switching
3972 *
3973 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3974 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3975 * depending on width) reserved for each character which is kinda wasty, but
3976 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3977 * is upto the actual low-level console-driver convert data into its favorite
3978 * format (maybe we should add a `fontoffset' field to the `display'
3979 * structure so we won't have to convert the fontdata all the time.
3980 * /Jes
3981 */
3982
3983#define max_font_size 65536
3984
3985static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3986{
3987 struct console_font font;
3988 int rc = -EINVAL;
3989 int c;
3990
3991 if (vc->vc_mode != KD_TEXT)
3992 return -EINVAL;
3993
3994 if (op->data) {
3995 font.data = kmalloc(max_font_size, GFP_KERNEL);
3996 if (!font.data)
3997 return -ENOMEM;
3998 } else
3999 font.data = NULL;
4000
Torben Hohnac751ef2011-01-25 15:07:35 -08004001 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 if (vc->vc_sw->con_font_get)
4003 rc = vc->vc_sw->con_font_get(vc, &font);
4004 else
4005 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004006 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008 if (rc)
4009 goto out;
4010
4011 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004012
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013 if (op->data && font.charcount > op->charcount)
4014 rc = -ENOSPC;
4015 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4016 if (font.width > op->width || font.height > op->height)
4017 rc = -ENOSPC;
4018 } else {
4019 if (font.width != 8)
4020 rc = -EIO;
4021 else if ((op->height && font.height > op->height) ||
4022 font.height > 32)
4023 rc = -ENOSPC;
4024 }
4025 if (rc)
4026 goto out;
4027
4028 op->height = font.height;
4029 op->width = font.width;
4030 op->charcount = font.charcount;
4031
4032 if (op->data && copy_to_user(op->data, font.data, c))
4033 rc = -EFAULT;
4034
4035out:
4036 kfree(font.data);
4037 return rc;
4038}
4039
4040static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4041{
4042 struct console_font font;
4043 int rc = -EINVAL;
4044 int size;
4045
4046 if (vc->vc_mode != KD_TEXT)
4047 return -EINVAL;
4048 if (!op->data)
4049 return -EINVAL;
4050 if (op->charcount > 512)
4051 return -EINVAL;
4052 if (!op->height) { /* Need to guess font height [compat] */
4053 int h, i;
4054 u8 __user *charmap = op->data;
4055 u8 tmp;
4056
4057 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4058 so that we can get rid of this soon */
4059 if (!(op->flags & KD_FONT_FLAG_OLD))
4060 return -EINVAL;
4061 for (h = 32; h > 0; h--)
4062 for (i = 0; i < op->charcount; i++) {
4063 if (get_user(tmp, &charmap[32*i+h-1]))
4064 return -EFAULT;
4065 if (tmp)
4066 goto nonzero;
4067 }
4068 return -EINVAL;
4069 nonzero:
4070 op->height = h;
4071 }
4072 if (op->width <= 0 || op->width > 32 || op->height > 32)
4073 return -EINVAL;
4074 size = (op->width+7)/8 * 32 * op->charcount;
4075 if (size > max_font_size)
4076 return -ENOSPC;
4077 font.charcount = op->charcount;
4078 font.height = op->height;
4079 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004080 font.data = memdup_user(op->data, size);
4081 if (IS_ERR(font.data))
4082 return PTR_ERR(font.data);
Torben Hohnac751ef2011-01-25 15:07:35 -08004083 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (vc->vc_sw->con_font_set)
4085 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4086 else
4087 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004088 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004089 kfree(font.data);
4090 return rc;
4091}
4092
4093static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4094{
4095 struct console_font font = {.width = op->width, .height = op->height};
4096 char name[MAX_FONT_NAME];
4097 char *s = name;
4098 int rc;
4099
4100 if (vc->vc_mode != KD_TEXT)
4101 return -EINVAL;
4102
4103 if (!op->data)
4104 s = NULL;
4105 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4106 return -EFAULT;
4107 else
4108 name[MAX_FONT_NAME - 1] = 0;
4109
Torben Hohnac751ef2011-01-25 15:07:35 -08004110 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (vc->vc_sw->con_font_default)
4112 rc = vc->vc_sw->con_font_default(vc, &font, s);
4113 else
4114 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004115 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (!rc) {
4117 op->width = font.width;
4118 op->height = font.height;
4119 }
4120 return rc;
4121}
4122
4123static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4124{
4125 int con = op->height;
4126 int rc;
4127
4128 if (vc->vc_mode != KD_TEXT)
4129 return -EINVAL;
4130
Torben Hohnac751ef2011-01-25 15:07:35 -08004131 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 if (!vc->vc_sw->con_font_copy)
4133 rc = -ENOSYS;
4134 else if (con < 0 || !vc_cons_allocated(con))
4135 rc = -ENOTTY;
4136 else if (con == vc->vc_num) /* nothing to do */
4137 rc = 0;
4138 else
4139 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004140 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 return rc;
4142}
4143
4144int con_font_op(struct vc_data *vc, struct console_font_op *op)
4145{
4146 switch (op->op) {
4147 case KD_FONT_OP_SET:
4148 return con_font_set(vc, op);
4149 case KD_FONT_OP_GET:
4150 return con_font_get(vc, op);
4151 case KD_FONT_OP_SET_DEFAULT:
4152 return con_font_default(vc, op);
4153 case KD_FONT_OP_COPY:
4154 return con_font_copy(vc, op);
4155 }
4156 return -ENOSYS;
4157}
4158
4159/*
4160 * Interface exported to selection and vcs.
4161 */
4162
4163/* used by selection */
4164u16 screen_glyph(struct vc_data *vc, int offset)
4165{
4166 u16 w = scr_readw(screenpos(vc, offset, 1));
4167 u16 c = w & 0xff;
4168
4169 if (w & vc->vc_hi_font_mask)
4170 c |= 0x100;
4171 return c;
4172}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004173EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174
4175/* used by vcs - note the word offset */
4176unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4177{
4178 return screenpos(vc, 2 * w_offset, viewed);
4179}
4180
4181void getconsxy(struct vc_data *vc, unsigned char *p)
4182{
4183 p[0] = vc->vc_x;
4184 p[1] = vc->vc_y;
4185}
4186
4187void putconsxy(struct vc_data *vc, unsigned char *p)
4188{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004189 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 gotoxy(vc, p[0], p[1]);
4191 set_cursor(vc);
4192}
4193
4194u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4195{
4196 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4197 return softcursor_original;
4198 return scr_readw(org);
4199}
4200
4201void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4202{
4203 scr_writew(val, org);
4204 if ((unsigned long)org == vc->vc_pos) {
4205 softcursor_original = -1;
4206 add_softcursor(vc);
4207 }
4208}
4209
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004210void vcs_scr_updated(struct vc_data *vc)
4211{
4212 notify_update(vc);
4213}
4214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215/*
4216 * Visible symbols for modules
4217 */
4218
4219EXPORT_SYMBOL(color_table);
4220EXPORT_SYMBOL(default_red);
4221EXPORT_SYMBOL(default_grn);
4222EXPORT_SYMBOL(default_blu);
4223EXPORT_SYMBOL(update_region);
4224EXPORT_SYMBOL(redraw_screen);
4225EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226EXPORT_SYMBOL(fg_console);
4227EXPORT_SYMBOL(console_blank_hook);
4228EXPORT_SYMBOL(console_blanked);
4229EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004230EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231#ifndef VT_SINGLE_DRIVER
4232EXPORT_SYMBOL(take_over_console);
4233EXPORT_SYMBOL(give_up_console);
4234#endif