blob: b075489f314e4e3c2cb4f38d3564db9dc2394938 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * Hopefully this will be a rather complete VT102 implementation.
7 *
8 * Beeping thanks to John T Kohl.
9 *
10 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
11 * Chars, and VT100 enhancements by Peter MacDonald.
12 *
13 * Copy and paste function by Andrew Haylett,
14 * some enhancements by Alessandro Rubini.
15 *
16 * Code to check for different video-cards mostly by Galen Hunt,
17 * <g-hunt@ee.utah.edu>
18 *
19 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
20 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
21 *
22 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
23 * Resizing of consoles, aeb, 940926
24 *
25 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
26 * <poe@daimi.aau.dk>
27 *
28 * User-defined bell sound, new setterm control sequences and printk
29 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
30 *
31 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
32 *
33 * Merge with the abstract console driver by Geert Uytterhoeven
34 * <geert@linux-m68k.org>, Jan 1997.
35 *
36 * Original m68k console driver modifications by
37 *
38 * - Arno Griffioen <arno@usn.nl>
39 * - David Carter <carter@cs.bris.ac.uk>
40 *
41 * The abstract console driver provides a generic interface for a text
42 * console. It supports VGA text mode, frame buffer based graphical consoles
43 * and special graphics processors that are only accessible through some
44 * registers (e.g. a TMS340x0 GSP).
45 *
46 * The interface to the hardware is specified using a special structure
47 * (struct consw) which contains function pointers to console operations
48 * (see <linux/console.h> for more information).
49 *
50 * Support for changeable cursor shape
51 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
52 *
53 * Ported to i386 and con_scrolldelta fixed
54 * by Emmanuel Marty <core@ggi-project.org>, April 1998
55 *
56 * Resurrected character buffers in videoram plus lots of other trickery
57 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
58 *
59 * Removed old-style timers, introduced console_timer, made timer
Francois Camie1f8e872008-10-15 22:01:59 -070060 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * Removed console_lock, enabled interrupts across all console operations
63 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070064 *
65 * Fixed UTF-8 mode so alternate charset modes always work according
66 * to control sequences interpreted in do_con_trol function
67 * preserving backward VT100 semigraphics compatibility,
68 * malformed UTF sequences represented as sequences of replacement glyphs,
69 * original codes or '?' as a last resort if replacement glyph is undefined
70 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 */
72
73#include <linux/module.h>
74#include <linux/types.h>
75#include <linux/sched.h>
76#include <linux/tty.h>
77#include <linux/tty_flip.h>
78#include <linux/kernel.h>
79#include <linux/string.h>
80#include <linux/errno.h>
81#include <linux/kd.h>
82#include <linux/slab.h>
83#include <linux/major.h>
84#include <linux/mm.h>
85#include <linux/console.h>
86#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070087#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070088#include <linux/vt_kern.h>
89#include <linux/selection.h>
90#include <linux/tiocl.h>
91#include <linux/kbd_kern.h>
92#include <linux/consolemap.h>
93#include <linux/timer.h>
94#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/pm.h>
97#include <linux/font.h>
98#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -070099#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100100#include <linux/device.h>
101#include <linux/io.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100102#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500103#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300104#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700106#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700108#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700109#define CON_DRIVER_FLAG_INIT 2
110#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700111
112struct con_driver {
113 const struct consw *con;
114 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700115 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700116 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700117 int first;
118 int last;
119 int flag;
120};
121
122static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123const struct consw *conswitchp;
124
125/* A bitmap for codes <32. A bit of 1 indicates that the code
126 * corresponding to that bit number invokes some special action
127 * (such as cursor movement) and should not be displayed as a
128 * glyph unless the disp_ctrl mode is explicitly enabled.
129 */
130#define CTRL_ACTION 0x0d00ff81
131#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
132
133/*
134 * Here is the default bell parameters: 750HZ, 1/8th of a second
135 */
136#define DEFAULT_BELL_PITCH 750
137#define DEFAULT_BELL_DURATION (HZ/8)
Scot Doylebd633642015-03-26 13:54:39 +0000138#define DEFAULT_CURSOR_BLINK_MS 200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140struct vc vc_cons [MAX_NR_CONSOLES];
141
142#ifndef VT_SINGLE_DRIVER
143static const struct consw *con_driver_map[MAX_NR_CONSOLES];
144#endif
145
146static int con_open(struct tty_struct *, struct file *);
147static void vc_init(struct vc_data *vc, unsigned int rows,
148 unsigned int cols, int do_clear);
149static void gotoxy(struct vc_data *vc, int new_x, int new_y);
150static void save_cur(struct vc_data *vc);
151static void reset_terminal(struct vc_data *vc, int do_clear);
152static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800153static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154static void set_cursor(struct vc_data *vc);
155static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000156static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void blank_screen_t(unsigned long dummy);
158static void set_palette(struct vc_data *vc);
159
Peter Hurley52c40fc2014-11-11 11:20:56 -0500160#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static 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{
Mathias Krause502fcb72011-07-30 14:01:59 +0200264 struct vt_notifier_param param = { .vc = vc, .c = unicode };
Samuel Thibaultb293d752007-10-18 23:39:17 -0700265 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 Engelhardtc9e587ab2008-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);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500504 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507/* used by selection: complement pointer position */
508void complement_pos(struct vc_data *vc, int offset)
509{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700510 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 static unsigned short old;
512 static unsigned short oldx, oldy;
513
514 WARN_CONSOLE_UNLOCKED();
515
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700516 if (old_offset != -1 && old_offset >= 0 &&
517 old_offset < vc->vc_screenbuf_size) {
518 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (DO_UPDATE(vc))
520 vc->vc_sw->con_putc(vc, old, oldy, oldx);
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500521 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700523
524 old_offset = offset;
525
526 if (offset != -1 && offset >= 0 &&
527 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700529 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 p = screenpos(vc, offset, 1);
531 old = scr_readw(p);
532 new = old ^ vc->vc_complement_mask;
533 scr_writew(new, p);
534 if (DO_UPDATE(vc)) {
535 oldx = (offset >> 1) % vc->vc_cols;
536 oldy = (offset >> 1) / vc->vc_cols;
537 vc->vc_sw->con_putc(vc, new, oldy, oldx);
538 }
Nicolas Pitre19e3ae62015-01-23 17:07:21 -0500539 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
541}
542
543static void insert_char(struct vc_data *vc, unsigned int nr)
544{
Jean-François Moine81732c32012-09-06 19:24:13 +0200545 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Nicolas Pitrea883b702013-02-24 20:06:09 -0500547 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200548 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 vc->vc_need_wrap = 0;
Jean-François Moine81732c32012-09-06 19:24:13 +0200550 if (DO_UPDATE(vc))
551 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100552 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
555static void delete_char(struct vc_data *vc, unsigned int nr)
556{
Jean-François Moine81732c32012-09-06 19:24:13 +0200557 unsigned short *p = (unsigned short *) vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100559 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
Jean-François Moine81732c32012-09-06 19:24:13 +0200560 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
561 nr * 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 vc->vc_need_wrap = 0;
Jean-François Moine81732c32012-09-06 19:24:13 +0200563 if (DO_UPDATE(vc))
564 do_update_region(vc, (unsigned long) p,
Jean-François Moineb1a925f2012-11-20 17:35:41 +0100565 vc->vc_cols - vc->vc_x);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static int softcursor_original;
569
570static void add_softcursor(struct vc_data *vc)
571{
572 int i = scr_readw((u16 *) vc->vc_pos);
573 u32 type = vc->vc_cursor_type;
574
575 if (! (type & 0x10)) return;
576 if (softcursor_original != -1) return;
577 softcursor_original = i;
578 i |= ((type >> 8) & 0xff00 );
579 i ^= ((type) & 0xff00 );
580 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
581 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
582 scr_writew(i, (u16 *) vc->vc_pos);
583 if (DO_UPDATE(vc))
584 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
585}
586
587static void hide_softcursor(struct vc_data *vc)
588{
589 if (softcursor_original != -1) {
590 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
591 if (DO_UPDATE(vc))
592 vc->vc_sw->con_putc(vc, softcursor_original,
593 vc->vc_y, vc->vc_x);
594 softcursor_original = -1;
595 }
596}
597
598static void hide_cursor(struct vc_data *vc)
599{
600 if (vc == sel_cons)
601 clear_selection();
602 vc->vc_sw->con_cursor(vc, CM_ERASE);
603 hide_softcursor(vc);
604}
605
606static void set_cursor(struct vc_data *vc)
607{
608 if (!IS_FG(vc) || console_blanked ||
609 vc->vc_mode == KD_GRAPHICS)
610 return;
611 if (vc->vc_deccm) {
612 if (vc == sel_cons)
613 clear_selection();
614 add_softcursor(vc);
615 if ((vc->vc_cursor_type & 0x0f) != 1)
616 vc->vc_sw->con_cursor(vc, CM_DRAW);
617 } else
618 hide_cursor(vc);
619}
620
621static void set_origin(struct vc_data *vc)
622{
623 WARN_CONSOLE_UNLOCKED();
624
625 if (!CON_IS_VISIBLE(vc) ||
626 !vc->vc_sw->con_set_origin ||
627 !vc->vc_sw->con_set_origin(vc))
628 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
629 vc->vc_visible_origin = vc->vc_origin;
630 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
631 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
632}
633
634static inline void save_screen(struct vc_data *vc)
635{
636 WARN_CONSOLE_UNLOCKED();
637
638 if (vc->vc_sw->con_save_screen)
639 vc->vc_sw->con_save_screen(vc);
640}
641
642/*
643 * Redrawing of screen
644 */
645
Dave Airlie2a248302013-01-24 14:14:19 +1000646void clear_buffer_attributes(struct vc_data *vc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 unsigned short *p = (unsigned short *)vc->vc_origin;
649 int count = vc->vc_screenbuf_size / 2;
650 int mask = vc->vc_hi_font_mask | 0xff;
651
652 for (; count > 0; count--, p++) {
653 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
654 }
655}
656
657void redraw_screen(struct vc_data *vc, int is_switch)
658{
659 int redraw = 0;
660
661 WARN_CONSOLE_UNLOCKED();
662
663 if (!vc) {
664 /* strange ... */
665 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
666 return;
667 }
668
669 if (is_switch) {
670 struct vc_data *old_vc = vc_cons[fg_console].d;
671 if (old_vc == vc)
672 return;
673 if (!CON_IS_VISIBLE(vc))
674 redraw = 1;
675 *vc->vc_display_fg = vc;
676 fg_console = vc->vc_num;
677 hide_cursor(old_vc);
678 if (!CON_IS_VISIBLE(old_vc)) {
679 save_screen(old_vc);
680 set_origin(old_vc);
681 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100682 if (tty0dev)
683 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 } else {
685 hide_cursor(vc);
686 redraw = 1;
687 }
688
689 if (redraw) {
690 int update;
691 int old_was_color = vc->vc_can_do_color;
692
693 set_origin(vc);
694 update = vc->vc_sw->con_switch(vc);
695 set_palette(vc);
696 /*
697 * If console changed from mono<->color, the best we can do
698 * is to clear the buffer attributes. As it currently stands,
699 * rebuilding new attributes from the old buffer is not doable
700 * without overly complex code.
701 */
702 if (old_was_color != vc->vc_can_do_color) {
703 update_attr(vc);
704 clear_buffer_attributes(vc);
705 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700706
707 /* Forcibly update if we're panicing */
708 if ((update && vc->vc_mode != KD_GRAPHICS) ||
709 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
711 }
712 set_cursor(vc);
713 if (is_switch) {
714 set_leds();
715 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800716 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
718}
719
720/*
721 * Allocation, freeing and resizing of VTs.
722 */
723
724int vc_cons_allocated(unsigned int i)
725{
726 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
727}
728
729static void visual_init(struct vc_data *vc, int num, int init)
730{
731 /* ++Geert: vc->vc_sw->con_init determines console size */
732 if (vc->vc_sw)
733 module_put(vc->vc_sw->owner);
734 vc->vc_sw = conswitchp;
735#ifndef VT_SINGLE_DRIVER
736 if (con_driver_map[num])
737 vc->vc_sw = con_driver_map[num];
738#endif
739 __module_get(vc->vc_sw->owner);
740 vc->vc_num = num;
741 vc->vc_display_fg = &master_display_fg;
742 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
Takashi Iwaie4bdab72014-05-13 12:09:28 +0200743 vc->vc_uni_pagedir = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 vc->vc_hi_font_mask = 0;
745 vc->vc_complement_mask = 0;
746 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700747 vc->vc_panic_force_write = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 vc->vc_sw->con_init(vc, init);
749 if (!vc->vc_complement_mask)
750 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
751 vc->vc_s_complement_mask = vc->vc_complement_mask;
752 vc->vc_size_row = vc->vc_cols << 1;
753 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
754}
755
756int vc_allocate(unsigned int currcons) /* return 0 on success */
757{
758 WARN_CONSOLE_UNLOCKED();
759
760 if (currcons >= MAX_NR_CONSOLES)
761 return -ENXIO;
762 if (!vc_cons[currcons].d) {
763 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700764 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /* prevent users from taking too much memory */
767 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
768 return -EPERM;
769
770 /* due to the granularity of kmalloc, we waste some memory here */
771 /* the alloc is done in two steps, to optimize the common situation
772 of a 25x80 console (structsize=216, screenbuf_size=4000) */
773 /* although the numbers above are not valid since long ago, the
774 point is still up-to-date and the comment still has its value
775 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700776 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 if (!vc)
778 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 vc_cons[currcons].d = vc;
Alan Coxff917ba2010-06-01 22:52:55 +0200780 tty_port_init(&vc->port);
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800781 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 visual_init(vc, currcons, 1);
783 if (!*vc->vc_uni_pagedir_loc)
784 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100785 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!vc->vc_screenbuf) {
787 kfree(vc);
788 vc_cons[currcons].d = NULL;
789 return -ENOMEM;
790 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500791
792 /* If no drivers have overridden us and the user didn't pass a
793 boot option, default to displaying the cursor */
794 if (global_cursor_default == -1)
795 global_cursor_default = 1;
796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100798 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700799 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 return 0;
802}
803
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700804static inline int resize_screen(struct vc_data *vc, int width, int height,
805 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
807 /* Resizes the resolution of the display adapater */
808 int err = 0;
809
810 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700811 err = vc->vc_sw->con_resize(vc, width, height, user);
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return err;
814}
815
816/*
817 * Change # of rows and columns (0 means unchanged/the size of fg_console)
818 * [this is to be used together with some user program
819 * like resize that changes the hardware videomode]
820 */
821#define VC_RESIZE_MAXCOL (32767)
822#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100823
824/**
825 * vc_do_resize - resizing method for the tty
826 * @tty: tty being resized
827 * @real_tty: real tty (different to tty if a pty/tty pair)
828 * @vc: virtual console private data
829 * @cols: columns
830 * @lines: lines
831 *
832 * Resize a virtual console, clipping according to the actual constraints.
833 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800834 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100835 *
Peter Hurley6a1c0682013-06-15 09:14:23 -0400836 * Caller must hold the console semaphore. Takes the termios rwsem and
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100837 * ctrl_lock of the tty IFF a tty is passed.
838 */
839
Alan Coxfc6f6232009-01-02 13:43:17 +0000840static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
841 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
843 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700844 unsigned long end;
Jiri Slabyf2ee4ae2011-03-23 09:49:57 +0100845 unsigned int old_rows, old_row_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700847 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unsigned short *newscreen;
849
850 WARN_CONSOLE_UNLOCKED();
851
852 if (!vc)
853 return -ENXIO;
854
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700855 user = vc->vc_resize_user;
856 vc->vc_resize_user = 0;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
859 return -EINVAL;
860
861 new_cols = (cols ? cols : vc->vc_cols);
862 new_rows = (lines ? lines : vc->vc_rows);
863 new_row_size = new_cols << 1;
864 new_screen_size = new_row_size * new_rows;
865
866 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
867 return 0;
868
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800869 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!newscreen)
871 return -ENOMEM;
872
873 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700876 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (err) {
878 kfree(newscreen);
879 return err;
880 }
881
882 vc->vc_rows = new_rows;
883 vc->vc_cols = new_cols;
884 vc->vc_size_row = new_row_size;
885 vc->vc_screenbuf_size = new_screen_size;
886
887 rlth = min(old_row_size, new_row_size);
888 rrem = new_row_size - rlth;
889 old_origin = vc->vc_origin;
890 new_origin = (long) newscreen;
891 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700892
893 if (vc->vc_y > new_rows) {
894 if (old_rows - vc->vc_y < new_rows) {
895 /*
896 * Cursor near the bottom, copy contents from the
897 * bottom of buffer
898 */
899 old_origin += (old_rows - new_rows) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700900 } else {
901 /*
902 * Cursor is in no man's land, copy 1/2 screenful
903 * from the top and bottom of cursor position
904 */
905 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700906 }
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +0200907 }
908
909 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 update_attr(vc);
912
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700913 while (old_origin < end) {
914 scr_memcpyw((unsigned short *) new_origin,
915 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700917 scr_memsetw((void *)(new_origin + rlth),
918 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 old_origin += old_row_size;
920 new_origin += new_row_size;
921 }
922 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700923 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
924 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100925 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 vc->vc_screenbuf_size = new_screen_size;
928 set_origin(vc);
929
930 /* do part of a reset_terminal() */
931 vc->vc_top = 0;
932 vc->vc_bottom = vc->vc_rows;
933 gotoxy(vc, vc->vc_x, vc->vc_y);
934 save_cur(vc);
935
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100936 if (tty) {
937 /* Rewrite the requested winsize data with the actual
938 resulting sizes */
939 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 memset(&ws, 0, sizeof(ws));
941 ws.ws_row = vc->vc_rows;
942 ws.ws_col = vc->vc_cols;
943 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000944 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946
947 if (CON_IS_VISIBLE(vc))
948 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700949 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 return err;
951}
952
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100953/**
954 * vc_resize - resize a VT
955 * @vc: virtual console
956 * @cols: columns
957 * @rows: rows
958 *
959 * Resize a virtual console as seen from the console end of things. We
960 * use the common vc_do_resize methods to update the structures. The
961 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +0200962 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100963 */
964
965int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700966{
Alan Cox8ce73262010-06-01 22:52:56 +0200967 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100968}
969
970/**
971 * vt_resize - resize a VT
972 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100973 * @ws: winsize attributes
974 *
975 * Resize a virtual terminal. This is called by the tty layer as we
976 * register our own handler for resizing. The mutual helper does all
977 * the actual work.
978 *
979 * Takes the console sem and the called methods then take the tty
Peter Hurley6a1c0682013-06-15 09:14:23 -0400980 * termios_rwsem and the tty ctrl_lock in that order.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100981 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800982static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100983{
984 struct vc_data *vc = tty->driver_data;
985 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Torben Hohnac751ef2011-01-25 15:07:35 -0800987 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +0000988 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -0800989 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100990 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -0700991}
992
Peter Hurley421b40a2013-05-17 12:41:03 -0400993struct vc_data *vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Peter Hurley421b40a2013-05-17 12:41:03 -0400995 struct vc_data *vc = NULL;
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 WARN_CONSOLE_UNLOCKED();
998
999 if (vc_cons_allocated(currcons)) {
Peter Hurley421b40a2013-05-17 12:41:03 -04001000 struct vt_notifier_param param;
Kay Sievers4995f8e2009-03-09 14:18:52 +01001001
Peter Hurley421b40a2013-05-17 12:41:03 -04001002 param.vc = vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001003 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001004 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001006 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001007 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001008 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 vc_cons[currcons].d = NULL;
1010 }
Peter Hurley421b40a2013-05-17 12:41:03 -04001011 return vc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012}
1013
1014/*
1015 * VT102 emulator
1016 */
1017
Alan Cox079c9532012-02-28 14:49:23 +00001018#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1019#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1020#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022#define decarm VC_REPEAT
1023#define decckm VC_CKMODE
1024#define kbdapplic VC_APPLIC
1025#define lnm VC_CRLF
1026
1027/*
1028 * this is what the terminal answers to a ESC-Z or csi0c query.
1029 */
1030#define VT100ID "\033[?1;2c"
1031#define VT102ID "\033[?6c"
1032
1033unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1034 8,12,10,14, 9,13,11,15 };
1035
1036/* the default colour table, for VGA+ colour systems */
1037int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1038 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1039int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1040 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1041int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1042 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1043
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001044module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1045module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1046module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048/*
1049 * gotoxy() must verify all boundaries, because the arguments
1050 * might also be negative. If the given position is out of
1051 * bounds, the cursor is placed at the nearest margin.
1052 */
1053static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1054{
1055 int min_y, max_y;
1056
1057 if (new_x < 0)
1058 vc->vc_x = 0;
1059 else {
1060 if (new_x >= vc->vc_cols)
1061 vc->vc_x = vc->vc_cols - 1;
1062 else
1063 vc->vc_x = new_x;
1064 }
1065
1066 if (vc->vc_decom) {
1067 min_y = vc->vc_top;
1068 max_y = vc->vc_bottom;
1069 } else {
1070 min_y = 0;
1071 max_y = vc->vc_rows;
1072 }
1073 if (new_y < min_y)
1074 vc->vc_y = min_y;
1075 else if (new_y >= max_y)
1076 vc->vc_y = max_y - 1;
1077 else
1078 vc->vc_y = new_y;
1079 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1080 vc->vc_need_wrap = 0;
1081}
1082
1083/* for absolute user moves, when decom is set */
1084static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1085{
1086 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1087}
1088
1089void scrollback(struct vc_data *vc, int lines)
1090{
1091 if (!lines)
1092 lines = vc->vc_rows / 2;
1093 scrolldelta(-lines);
1094}
1095
1096void scrollfront(struct vc_data *vc, int lines)
1097{
1098 if (!lines)
1099 lines = vc->vc_rows / 2;
1100 scrolldelta(lines);
1101}
1102
1103static void lf(struct vc_data *vc)
1104{
1105 /* don't scroll if above bottom of scrolling region, or
1106 * if below scrolling region
1107 */
1108 if (vc->vc_y + 1 == vc->vc_bottom)
1109 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1110 else if (vc->vc_y < vc->vc_rows - 1) {
1111 vc->vc_y++;
1112 vc->vc_pos += vc->vc_size_row;
1113 }
1114 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001115 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116}
1117
1118static void ri(struct vc_data *vc)
1119{
1120 /* don't scroll if below top of scrolling region, or
1121 * if above scrolling region
1122 */
1123 if (vc->vc_y == vc->vc_top)
1124 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1125 else if (vc->vc_y > 0) {
1126 vc->vc_y--;
1127 vc->vc_pos -= vc->vc_size_row;
1128 }
1129 vc->vc_need_wrap = 0;
1130}
1131
1132static inline void cr(struct vc_data *vc)
1133{
1134 vc->vc_pos -= vc->vc_x << 1;
1135 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001136 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137}
1138
1139static inline void bs(struct vc_data *vc)
1140{
1141 if (vc->vc_x) {
1142 vc->vc_pos -= 2;
1143 vc->vc_x--;
1144 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001145 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147}
1148
1149static inline void del(struct vc_data *vc)
1150{
1151 /* ignored */
1152}
1153
1154static void csi_J(struct vc_data *vc, int vpar)
1155{
1156 unsigned int count;
1157 unsigned short * start;
1158
1159 switch (vpar) {
1160 case 0: /* erase from cursor to end of display */
1161 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1162 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164 case 1: /* erase from start to cursor */
1165 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1166 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
Petr Písařf8df13e2011-04-15 10:08:08 +02001168 case 3: /* erase scroll-back buffer (and whole display) */
1169 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1170 vc->vc_screenbuf_size >> 1);
1171 set_origin(vc);
Petr Písař0930b092014-02-06 21:01:23 +01001172 if (CON_IS_VISIBLE(vc))
1173 update_screen(vc);
Petr Písařf8df13e2011-04-15 10:08:08 +02001174 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 case 2: /* erase whole display */
1176 count = vc->vc_cols * vc->vc_rows;
1177 start = (unsigned short *)vc->vc_origin;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 break;
1179 default:
1180 return;
1181 }
1182 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
Jean-François Moine81732c32012-09-06 19:24:13 +02001183 if (DO_UPDATE(vc))
1184 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 vc->vc_need_wrap = 0;
1186}
1187
1188static void csi_K(struct vc_data *vc, int vpar)
1189{
1190 unsigned int count;
1191 unsigned short * start;
1192
1193 switch (vpar) {
1194 case 0: /* erase from cursor to end of line */
1195 count = vc->vc_cols - vc->vc_x;
1196 start = (unsigned short *)vc->vc_pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
1198 case 1: /* erase from start of line to cursor */
1199 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1200 count = vc->vc_x + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
1202 case 2: /* erase whole line */
1203 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1204 count = vc->vc_cols;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 break;
1206 default:
1207 return;
1208 }
1209 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1210 vc->vc_need_wrap = 0;
Jean-François Moine81732c32012-09-06 19:24:13 +02001211 if (DO_UPDATE(vc))
1212 do_update_region(vc, (unsigned long) start, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
1215static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1216{ /* not vt100? */
1217 int count;
1218
1219 if (!vpar)
1220 vpar++;
1221 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1222
1223 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1224 if (DO_UPDATE(vc))
1225 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1226 vc->vc_need_wrap = 0;
1227}
1228
1229static void default_attr(struct vc_data *vc)
1230{
1231 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001232 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 vc->vc_underline = 0;
1234 vc->vc_reverse = 0;
1235 vc->vc_blink = 0;
1236 vc->vc_color = vc->vc_def_color;
1237}
1238
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001239struct rgb { u8 r; u8 g; u8 b; };
1240
Lad, Prabhakar678afb92015-02-04 18:18:03 +00001241static struct rgb rgb_from_256(int i)
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001242{
1243 struct rgb c;
1244 if (i < 8) { /* Standard colours. */
1245 c.r = i&1 ? 0xaa : 0x00;
1246 c.g = i&2 ? 0xaa : 0x00;
1247 c.b = i&4 ? 0xaa : 0x00;
1248 } else if (i < 16) {
1249 c.r = i&1 ? 0xff : 0x55;
1250 c.g = i&2 ? 0xff : 0x55;
1251 c.b = i&4 ? 0xff : 0x55;
1252 } else if (i < 232) { /* 6x6x6 colour cube. */
1253 c.r = (i - 16) / 36 * 85 / 2;
1254 c.g = (i - 16) / 6 % 6 * 85 / 2;
1255 c.b = (i - 16) % 6 * 85 / 2;
1256 } else /* Grayscale ramp. */
1257 c.r = c.g = c.b = i * 10 - 2312;
1258 return c;
1259}
1260
1261static void rgb_foreground(struct vc_data *vc, struct rgb c)
1262{
1263 u8 hue, max = c.r;
1264 if (c.g > max)
1265 max = c.g;
1266 if (c.b > max)
1267 max = c.b;
1268 hue = (c.r > max/2 ? 4 : 0)
1269 | (c.g > max/2 ? 2 : 0)
1270 | (c.b > max/2 ? 1 : 0);
1271 if (hue == 7 && max <= 0x55)
1272 hue = 0, vc->vc_intensity = 2;
1273 else
1274 vc->vc_intensity = (max > 0xaa) + 1;
1275 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1276}
1277
1278static void rgb_background(struct vc_data *vc, struct rgb c)
1279{
1280 /* For backgrounds, err on the dark side. */
1281 vc->vc_color = (vc->vc_color & 0x0f)
1282 | (c.r&0x80) >> 1 | (c.g&0x80) >> 2 | (c.b&0x80) >> 3;
1283}
1284
Torben Hohnac751ef2011-01-25 15:07:35 -08001285/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286static void csi_m(struct vc_data *vc)
1287{
1288 int i;
1289
1290 for (i = 0; i <= vc->vc_npar; i++)
1291 switch (vc->vc_par[i]) {
1292 case 0: /* all attributes off */
1293 default_attr(vc);
1294 break;
1295 case 1:
1296 vc->vc_intensity = 2;
1297 break;
1298 case 2:
1299 vc->vc_intensity = 0;
1300 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001301 case 3:
1302 vc->vc_italic = 1;
1303 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 case 4:
1305 vc->vc_underline = 1;
1306 break;
1307 case 5:
1308 vc->vc_blink = 1;
1309 break;
1310 case 7:
1311 vc->vc_reverse = 1;
1312 break;
1313 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1314 * Select primary font, don't display
1315 * control chars if defined, don't set
1316 * bit 8 on output.
1317 */
1318 vc->vc_translate = set_translate(vc->vc_charset == 0
1319 ? vc->vc_G0_charset
1320 : vc->vc_G1_charset, vc);
1321 vc->vc_disp_ctrl = 0;
1322 vc->vc_toggle_meta = 0;
1323 break;
1324 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1325 * Select first alternate font, lets
1326 * chars < 32 be displayed as ROM chars.
1327 */
1328 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1329 vc->vc_disp_ctrl = 1;
1330 vc->vc_toggle_meta = 0;
1331 break;
1332 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1333 * Select second alternate font, toggle
1334 * high bit before displaying as ROM char.
1335 */
1336 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1337 vc->vc_disp_ctrl = 1;
1338 vc->vc_toggle_meta = 1;
1339 break;
1340 case 21:
1341 case 22:
1342 vc->vc_intensity = 1;
1343 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001344 case 23:
1345 vc->vc_italic = 0;
1346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 case 24:
1348 vc->vc_underline = 0;
1349 break;
1350 case 25:
1351 vc->vc_blink = 0;
1352 break;
1353 case 27:
1354 vc->vc_reverse = 0;
1355 break;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001356 case 38: /* ITU T.416
Adam Borowski34150972013-07-12 22:23:41 +02001357 * Higher colour modes.
1358 * They break the usual properties of SGR codes
1359 * and thus need to be detected and ignored by
1360 * hand. Strictly speaking, that standard also
1361 * wants : rather than ; as separators, contrary
1362 * to ECMA-48, but no one produces such codes
1363 * and almost no one accepts them.
1364 */
1365 i++;
1366 if (i > vc->vc_npar)
1367 break;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001368 if (vc->vc_par[i] == 5 && /* 256 colours */
1369 i < vc->vc_npar) { /* ubiquitous */
1370 i++;
1371 rgb_foreground(vc,
1372 rgb_from_256(vc->vc_par[i]));
1373 } else if (vc->vc_par[i] == 2 && /* 24 bit */
1374 i <= vc->vc_npar + 3) {/* extremely rare */
Peng Fan085f4d82014-10-26 20:08:02 +08001375 struct rgb c = {
1376 .r = vc->vc_par[i + 1],
1377 .g = vc->vc_par[i + 2],
1378 .b = vc->vc_par[i + 3],
1379 };
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001380 rgb_foreground(vc, c);
1381 i += 3;
1382 }
Adam Borowski34150972013-07-12 22:23:41 +02001383 /* Subcommands 3 (CMY) and 4 (CMYK) are so insane
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001384 * there's no point in supporting them.
Adam Borowski34150972013-07-12 22:23:41 +02001385 */
1386 break;
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001387 case 48:
1388 i++;
1389 if (i > vc->vc_npar)
1390 break;
1391 if (vc->vc_par[i] == 5 && /* 256 colours */
1392 i < vc->vc_npar) {
1393 i++;
1394 rgb_background(vc,
1395 rgb_from_256(vc->vc_par[i]));
1396 } else if (vc->vc_par[i] == 2 && /* 24 bit */
1397 i <= vc->vc_npar + 3) {
Peng Fan085f4d82014-10-26 20:08:02 +08001398 struct rgb c = {
1399 .r = vc->vc_par[i + 1],
1400 .g = vc->vc_par[i + 2],
1401 .b = vc->vc_par[i + 3],
1402 };
Adam Borowskicec5b2a92014-05-23 01:33:08 +02001403 rgb_background(vc, c);
1404 i += 3;
1405 }
1406 break;
Adam Borowski64545882013-09-10 22:54:35 +02001407 case 39:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 case 49:
1411 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1412 break;
1413 default:
1414 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1415 vc->vc_color = color_table[vc->vc_par[i] - 30]
1416 | (vc->vc_color & 0xf0);
1417 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1418 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1419 | (vc->vc_color & 0x0f);
1420 break;
1421 }
1422 update_attr(vc);
1423}
1424
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001425static void respond_string(const char *p, struct tty_port *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426{
1427 while (*p) {
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001428 tty_insert_flip_char(port, *p, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 p++;
1430 }
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001431 tty_schedule_flip(port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432}
1433
1434static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1435{
1436 char buf[40];
1437
1438 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001439 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440}
1441
1442static inline void status_report(struct tty_struct *tty)
1443{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001444 respond_string("\033[0n", tty->port); /* Terminal ok */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445}
1446
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001447static inline void respond_ID(struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448{
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001449 respond_string(VT102ID, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450}
1451
1452void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1453{
1454 char buf[8];
1455
1456 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1457 (char)('!' + mry));
Jiri Slaby6732c8b2013-01-03 15:53:07 +01001458 respond_string(buf, tty->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459}
1460
1461/* invoked via ioctl(TIOCLINUX) and through set_selection */
1462int mouse_reporting(void)
1463{
1464 return vc_cons[fg_console].d->vc_report_mouse;
1465}
1466
Torben Hohnac751ef2011-01-25 15:07:35 -08001467/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468static void set_mode(struct vc_data *vc, int on_off)
1469{
1470 int i;
1471
1472 for (i = 0; i <= vc->vc_npar; i++)
1473 if (vc->vc_ques) {
1474 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1475 case 1: /* Cursor keys send ^[Ox/^[[x */
1476 if (on_off)
1477 set_kbd(vc, decckm);
1478 else
1479 clr_kbd(vc, decckm);
1480 break;
1481 case 3: /* 80/132 mode switch unimplemented */
1482 vc->vc_deccolm = on_off;
1483#if 0
1484 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1485 /* this alone does not suffice; some user mode
1486 utility has to change the hardware regs */
1487#endif
1488 break;
1489 case 5: /* Inverted screen on/off */
1490 if (vc->vc_decscnm != on_off) {
1491 vc->vc_decscnm = on_off;
1492 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1493 update_attr(vc);
1494 }
1495 break;
1496 case 6: /* Origin relative/absolute */
1497 vc->vc_decom = on_off;
1498 gotoxay(vc, 0, 0);
1499 break;
1500 case 7: /* Autowrap on/off */
1501 vc->vc_decawm = on_off;
1502 break;
1503 case 8: /* Autorepeat on/off */
1504 if (on_off)
1505 set_kbd(vc, decarm);
1506 else
1507 clr_kbd(vc, decarm);
1508 break;
1509 case 9:
1510 vc->vc_report_mouse = on_off ? 1 : 0;
1511 break;
1512 case 25: /* Cursor on/off */
1513 vc->vc_deccm = on_off;
1514 break;
1515 case 1000:
1516 vc->vc_report_mouse = on_off ? 2 : 0;
1517 break;
1518 }
1519 } else {
1520 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1521 case 3: /* Monitor (display ctrls) */
1522 vc->vc_disp_ctrl = on_off;
1523 break;
1524 case 4: /* Insert Mode on/off */
1525 vc->vc_decim = on_off;
1526 break;
1527 case 20: /* Lf, Enter == CrLf/Lf */
1528 if (on_off)
1529 set_kbd(vc, lnm);
1530 else
1531 clr_kbd(vc, lnm);
1532 break;
1533 }
1534 }
1535}
1536
Torben Hohnac751ef2011-01-25 15:07:35 -08001537/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538static void setterm_command(struct vc_data *vc)
1539{
1540 switch(vc->vc_par[0]) {
1541 case 1: /* set color for underline mode */
1542 if (vc->vc_can_do_color &&
1543 vc->vc_par[1] < 16) {
1544 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1545 if (vc->vc_underline)
1546 update_attr(vc);
1547 }
1548 break;
1549 case 2: /* set color for half intensity mode */
1550 if (vc->vc_can_do_color &&
1551 vc->vc_par[1] < 16) {
1552 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1553 if (vc->vc_intensity == 0)
1554 update_attr(vc);
1555 }
1556 break;
1557 case 8: /* store colors as defaults */
1558 vc->vc_def_color = vc->vc_attr;
1559 if (vc->vc_hi_font_mask == 0x100)
1560 vc->vc_def_color >>= 1;
1561 default_attr(vc);
1562 update_attr(vc);
1563 break;
1564 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001565 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 poke_blanked_console();
1567 break;
1568 case 10: /* set bell frequency in Hz */
1569 if (vc->vc_npar >= 1)
1570 vc->vc_bell_pitch = vc->vc_par[1];
1571 else
1572 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1573 break;
1574 case 11: /* set bell duration in msec */
1575 if (vc->vc_npar >= 1)
1576 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
Nicholas Mc Guire3372ec22015-02-09 10:54:10 -05001577 msecs_to_jiffies(vc->vc_par[1]) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 else
1579 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1580 break;
1581 case 12: /* bring specified console to the front */
1582 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1583 set_console(vc->vc_par[1] - 1);
1584 break;
1585 case 13: /* unblank the screen */
1586 poke_blanked_console();
1587 break;
1588 case 14: /* set vesa powerdown interval */
1589 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1590 break;
1591 case 15: /* activate the previous console */
1592 set_console(last_console);
1593 break;
Scot Doylebd633642015-03-26 13:54:39 +00001594 case 16: /* set cursor blink duration in msec */
1595 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1596 vc->vc_par[1] <= USHRT_MAX)
1597 vc->vc_cur_blink_ms = vc->vc_par[1];
1598 else
1599 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1600 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602}
1603
Torben Hohnac751ef2011-01-25 15:07:35 -08001604/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605static void csi_at(struct vc_data *vc, unsigned int nr)
1606{
1607 if (nr > vc->vc_cols - vc->vc_x)
1608 nr = vc->vc_cols - vc->vc_x;
1609 else if (!nr)
1610 nr = 1;
1611 insert_char(vc, nr);
1612}
1613
Torben Hohnac751ef2011-01-25 15:07:35 -08001614/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615static void csi_L(struct vc_data *vc, unsigned int nr)
1616{
1617 if (nr > vc->vc_rows - vc->vc_y)
1618 nr = vc->vc_rows - vc->vc_y;
1619 else if (!nr)
1620 nr = 1;
1621 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1622 vc->vc_need_wrap = 0;
1623}
1624
Torben Hohnac751ef2011-01-25 15:07:35 -08001625/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static void csi_P(struct vc_data *vc, unsigned int nr)
1627{
1628 if (nr > vc->vc_cols - vc->vc_x)
1629 nr = vc->vc_cols - vc->vc_x;
1630 else if (!nr)
1631 nr = 1;
1632 delete_char(vc, nr);
1633}
1634
Torben Hohnac751ef2011-01-25 15:07:35 -08001635/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636static void csi_M(struct vc_data *vc, unsigned int nr)
1637{
1638 if (nr > vc->vc_rows - vc->vc_y)
1639 nr = vc->vc_rows - vc->vc_y;
1640 else if (!nr)
1641 nr=1;
1642 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1643 vc->vc_need_wrap = 0;
1644}
1645
Torben Hohnac751ef2011-01-25 15:07:35 -08001646/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647static void save_cur(struct vc_data *vc)
1648{
1649 vc->vc_saved_x = vc->vc_x;
1650 vc->vc_saved_y = vc->vc_y;
1651 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001652 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 vc->vc_s_underline = vc->vc_underline;
1654 vc->vc_s_blink = vc->vc_blink;
1655 vc->vc_s_reverse = vc->vc_reverse;
1656 vc->vc_s_charset = vc->vc_charset;
1657 vc->vc_s_color = vc->vc_color;
1658 vc->vc_saved_G0 = vc->vc_G0_charset;
1659 vc->vc_saved_G1 = vc->vc_G1_charset;
1660}
1661
Torben Hohnac751ef2011-01-25 15:07:35 -08001662/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663static void restore_cur(struct vc_data *vc)
1664{
1665 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1666 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001667 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 vc->vc_underline = vc->vc_s_underline;
1669 vc->vc_blink = vc->vc_s_blink;
1670 vc->vc_reverse = vc->vc_s_reverse;
1671 vc->vc_charset = vc->vc_s_charset;
1672 vc->vc_color = vc->vc_s_color;
1673 vc->vc_G0_charset = vc->vc_saved_G0;
1674 vc->vc_G1_charset = vc->vc_saved_G1;
1675 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1676 update_attr(vc);
1677 vc->vc_need_wrap = 0;
1678}
1679
Adam Borowskib290af62014-02-19 01:40:16 +01001680enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
Adam Borowski63f3a162014-02-19 01:38:04 +01001682 ESpalette, ESosc };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Torben Hohnac751ef2011-01-25 15:07:35 -08001684/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685static void reset_terminal(struct vc_data *vc, int do_clear)
1686{
1687 vc->vc_top = 0;
1688 vc->vc_bottom = vc->vc_rows;
1689 vc->vc_state = ESnormal;
1690 vc->vc_ques = 0;
1691 vc->vc_translate = set_translate(LAT1_MAP, vc);
1692 vc->vc_G0_charset = LAT1_MAP;
1693 vc->vc_G1_charset = GRAF_MAP;
1694 vc->vc_charset = 0;
1695 vc->vc_need_wrap = 0;
1696 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001697 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 vc->vc_utf_count = 0;
1699
1700 vc->vc_disp_ctrl = 0;
1701 vc->vc_toggle_meta = 0;
1702
1703 vc->vc_decscnm = 0;
1704 vc->vc_decom = 0;
1705 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001706 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 vc->vc_decim = 0;
1708
Alan Cox079c9532012-02-28 14:49:23 +00001709 vt_reset_keyboard(vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001711 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 vc->vc_complement_mask = vc->vc_s_complement_mask;
1713
1714 default_attr(vc);
1715 update_attr(vc);
1716
1717 vc->vc_tab_stop[0] = 0x01010100;
1718 vc->vc_tab_stop[1] =
1719 vc->vc_tab_stop[2] =
1720 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001721 vc->vc_tab_stop[4] =
1722 vc->vc_tab_stop[5] =
1723 vc->vc_tab_stop[6] =
1724 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1727 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
Scot Doylebd633642015-03-26 13:54:39 +00001728 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 gotoxy(vc, 0, 0);
1731 save_cur(vc);
1732 if (do_clear)
1733 csi_J(vc, 2);
1734}
1735
Torben Hohnac751ef2011-01-25 15:07:35 -08001736/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1738{
1739 /*
1740 * Control characters can be used in the _middle_
1741 * of an escape sequence.
1742 */
Adam Borowski63f3a162014-02-19 01:38:04 +01001743 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1744 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 switch (c) {
1746 case 0:
1747 return;
1748 case 7:
Adam Borowski63f3a162014-02-19 01:38:04 +01001749 if (vc->vc_state == ESosc)
1750 vc->vc_state = ESnormal;
1751 else if (vc->vc_bell_duration)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1753 return;
1754 case 8:
1755 bs(vc);
1756 return;
1757 case 9:
1758 vc->vc_pos -= (vc->vc_x << 1);
1759 while (vc->vc_x < vc->vc_cols - 1) {
1760 vc->vc_x++;
1761 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1762 break;
1763 }
1764 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001765 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 return;
1767 case 10: case 11: case 12:
1768 lf(vc);
1769 if (!is_kbd(vc, lnm))
1770 return;
1771 case 13:
1772 cr(vc);
1773 return;
1774 case 14:
1775 vc->vc_charset = 1;
1776 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1777 vc->vc_disp_ctrl = 1;
1778 return;
1779 case 15:
1780 vc->vc_charset = 0;
1781 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1782 vc->vc_disp_ctrl = 0;
1783 return;
1784 case 24: case 26:
1785 vc->vc_state = ESnormal;
1786 return;
1787 case 27:
1788 vc->vc_state = ESesc;
1789 return;
1790 case 127:
1791 del(vc);
1792 return;
1793 case 128+27:
1794 vc->vc_state = ESsquare;
1795 return;
1796 }
1797 switch(vc->vc_state) {
1798 case ESesc:
1799 vc->vc_state = ESnormal;
1800 switch (c) {
1801 case '[':
1802 vc->vc_state = ESsquare;
1803 return;
1804 case ']':
1805 vc->vc_state = ESnonstd;
1806 return;
1807 case '%':
1808 vc->vc_state = ESpercent;
1809 return;
1810 case 'E':
1811 cr(vc);
1812 lf(vc);
1813 return;
1814 case 'M':
1815 ri(vc);
1816 return;
1817 case 'D':
1818 lf(vc);
1819 return;
1820 case 'H':
1821 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1822 return;
1823 case 'Z':
1824 respond_ID(tty);
1825 return;
1826 case '7':
1827 save_cur(vc);
1828 return;
1829 case '8':
1830 restore_cur(vc);
1831 return;
1832 case '(':
1833 vc->vc_state = ESsetG0;
1834 return;
1835 case ')':
1836 vc->vc_state = ESsetG1;
1837 return;
1838 case '#':
1839 vc->vc_state = EShash;
1840 return;
1841 case 'c':
1842 reset_terminal(vc, 1);
1843 return;
1844 case '>': /* Numeric keypad */
1845 clr_kbd(vc, kbdapplic);
1846 return;
1847 case '=': /* Appl. keypad */
1848 set_kbd(vc, kbdapplic);
1849 return;
1850 }
1851 return;
1852 case ESnonstd:
1853 if (c=='P') { /* palette escape sequence */
1854 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1855 vc->vc_par[vc->vc_npar] = 0;
1856 vc->vc_npar = 0;
1857 vc->vc_state = ESpalette;
1858 return;
1859 } else if (c=='R') { /* reset palette */
1860 reset_palette(vc);
1861 vc->vc_state = ESnormal;
Adam Borowski63f3a162014-02-19 01:38:04 +01001862 } else if (c>='0' && c<='9')
1863 vc->vc_state = ESosc;
1864 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 vc->vc_state = ESnormal;
1866 return;
1867 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001868 if (isxdigit(c)) {
1869 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 if (vc->vc_npar == 7) {
1871 int i = vc->vc_par[0] * 3, j = 1;
1872 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1873 vc->vc_palette[i++] += vc->vc_par[j++];
1874 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1875 vc->vc_palette[i++] += vc->vc_par[j++];
1876 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1877 vc->vc_palette[i] += vc->vc_par[j];
1878 set_palette(vc);
1879 vc->vc_state = ESnormal;
1880 }
1881 } else
1882 vc->vc_state = ESnormal;
1883 return;
1884 case ESsquare:
1885 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1886 vc->vc_par[vc->vc_npar] = 0;
1887 vc->vc_npar = 0;
1888 vc->vc_state = ESgetpars;
1889 if (c == '[') { /* Function key */
1890 vc->vc_state=ESfunckey;
1891 return;
1892 }
1893 vc->vc_ques = (c == '?');
1894 if (vc->vc_ques)
1895 return;
1896 case ESgetpars:
1897 if (c == ';' && vc->vc_npar < NPAR - 1) {
1898 vc->vc_npar++;
1899 return;
1900 } else if (c>='0' && c<='9') {
1901 vc->vc_par[vc->vc_npar] *= 10;
1902 vc->vc_par[vc->vc_npar] += c - '0';
1903 return;
Adam Borowskib290af62014-02-19 01:40:16 +01001904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 vc->vc_state = ESnormal;
1906 switch(c) {
1907 case 'h':
1908 set_mode(vc, 1);
1909 return;
1910 case 'l':
1911 set_mode(vc, 0);
1912 return;
1913 case 'c':
1914 if (vc->vc_ques) {
1915 if (vc->vc_par[0])
1916 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1917 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001918 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return;
1920 }
1921 break;
1922 case 'm':
1923 if (vc->vc_ques) {
1924 clear_selection();
1925 if (vc->vc_par[0])
1926 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1927 else
1928 vc->vc_complement_mask = vc->vc_s_complement_mask;
1929 return;
1930 }
1931 break;
1932 case 'n':
1933 if (!vc->vc_ques) {
1934 if (vc->vc_par[0] == 5)
1935 status_report(tty);
1936 else if (vc->vc_par[0] == 6)
1937 cursor_report(vc, tty);
1938 }
1939 return;
1940 }
1941 if (vc->vc_ques) {
1942 vc->vc_ques = 0;
1943 return;
1944 }
1945 switch(c) {
1946 case 'G': case '`':
1947 if (vc->vc_par[0])
1948 vc->vc_par[0]--;
1949 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1950 return;
1951 case 'A':
1952 if (!vc->vc_par[0])
1953 vc->vc_par[0]++;
1954 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1955 return;
1956 case 'B': case 'e':
1957 if (!vc->vc_par[0])
1958 vc->vc_par[0]++;
1959 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1960 return;
1961 case 'C': case 'a':
1962 if (!vc->vc_par[0])
1963 vc->vc_par[0]++;
1964 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1965 return;
1966 case 'D':
1967 if (!vc->vc_par[0])
1968 vc->vc_par[0]++;
1969 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1970 return;
1971 case 'E':
1972 if (!vc->vc_par[0])
1973 vc->vc_par[0]++;
1974 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1975 return;
1976 case 'F':
1977 if (!vc->vc_par[0])
1978 vc->vc_par[0]++;
1979 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1980 return;
1981 case 'd':
1982 if (vc->vc_par[0])
1983 vc->vc_par[0]--;
1984 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1985 return;
1986 case 'H': case 'f':
1987 if (vc->vc_par[0])
1988 vc->vc_par[0]--;
1989 if (vc->vc_par[1])
1990 vc->vc_par[1]--;
1991 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1992 return;
1993 case 'J':
1994 csi_J(vc, vc->vc_par[0]);
1995 return;
1996 case 'K':
1997 csi_K(vc, vc->vc_par[0]);
1998 return;
1999 case 'L':
2000 csi_L(vc, vc->vc_par[0]);
2001 return;
2002 case 'M':
2003 csi_M(vc, vc->vc_par[0]);
2004 return;
2005 case 'P':
2006 csi_P(vc, vc->vc_par[0]);
2007 return;
2008 case 'c':
2009 if (!vc->vc_par[0])
2010 respond_ID(tty);
2011 return;
2012 case 'g':
2013 if (!vc->vc_par[0])
2014 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
2015 else if (vc->vc_par[0] == 3) {
2016 vc->vc_tab_stop[0] =
2017 vc->vc_tab_stop[1] =
2018 vc->vc_tab_stop[2] =
2019 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08002020 vc->vc_tab_stop[4] =
2021 vc->vc_tab_stop[5] =
2022 vc->vc_tab_stop[6] =
2023 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 return;
2026 case 'm':
2027 csi_m(vc);
2028 return;
2029 case 'q': /* DECLL - but only 3 leds */
2030 /* map 0,1,2,3 to 0,1,2,4 */
2031 if (vc->vc_par[0] < 4)
Alan Cox079c9532012-02-28 14:49:23 +00002032 vt_set_led_state(vc->vc_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2034 return;
2035 case 'r':
2036 if (!vc->vc_par[0])
2037 vc->vc_par[0]++;
2038 if (!vc->vc_par[1])
2039 vc->vc_par[1] = vc->vc_rows;
2040 /* Minimum allowed region is 2 lines */
2041 if (vc->vc_par[0] < vc->vc_par[1] &&
2042 vc->vc_par[1] <= vc->vc_rows) {
2043 vc->vc_top = vc->vc_par[0] - 1;
2044 vc->vc_bottom = vc->vc_par[1];
2045 gotoxay(vc, 0, 0);
2046 }
2047 return;
2048 case 's':
2049 save_cur(vc);
2050 return;
2051 case 'u':
2052 restore_cur(vc);
2053 return;
2054 case 'X':
2055 csi_X(vc, vc->vc_par[0]);
2056 return;
2057 case '@':
2058 csi_at(vc, vc->vc_par[0]);
2059 return;
2060 case ']': /* setterm functions */
2061 setterm_command(vc);
2062 return;
2063 }
2064 return;
2065 case ESpercent:
2066 vc->vc_state = ESnormal;
2067 switch (c) {
2068 case '@': /* defined in ISO 2022 */
2069 vc->vc_utf = 0;
2070 return;
2071 case 'G': /* prelim official escape code */
2072 case '8': /* retained for compatibility */
2073 vc->vc_utf = 1;
2074 return;
2075 }
2076 return;
2077 case ESfunckey:
2078 vc->vc_state = ESnormal;
2079 return;
2080 case EShash:
2081 vc->vc_state = ESnormal;
2082 if (c == '8') {
2083 /* DEC screen alignment test. kludge :-) */
2084 vc->vc_video_erase_char =
2085 (vc->vc_video_erase_char & 0xff00) | 'E';
2086 csi_J(vc, 2);
2087 vc->vc_video_erase_char =
2088 (vc->vc_video_erase_char & 0xff00) | ' ';
2089 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2090 }
2091 return;
2092 case ESsetG0:
2093 if (c == '0')
2094 vc->vc_G0_charset = GRAF_MAP;
2095 else if (c == 'B')
2096 vc->vc_G0_charset = LAT1_MAP;
2097 else if (c == 'U')
2098 vc->vc_G0_charset = IBMPC_MAP;
2099 else if (c == 'K')
2100 vc->vc_G0_charset = USER_MAP;
2101 if (vc->vc_charset == 0)
2102 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2103 vc->vc_state = ESnormal;
2104 return;
2105 case ESsetG1:
2106 if (c == '0')
2107 vc->vc_G1_charset = GRAF_MAP;
2108 else if (c == 'B')
2109 vc->vc_G1_charset = LAT1_MAP;
2110 else if (c == 'U')
2111 vc->vc_G1_charset = IBMPC_MAP;
2112 else if (c == 'K')
2113 vc->vc_G1_charset = USER_MAP;
2114 if (vc->vc_charset == 1)
2115 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2116 vc->vc_state = ESnormal;
2117 return;
Adam Borowski63f3a162014-02-19 01:38:04 +01002118 case ESosc:
2119 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 default:
2121 vc->vc_state = ESnormal;
2122 }
2123}
2124
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002125/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002126 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002127 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2128 */
2129struct interval {
2130 uint32_t first;
2131 uint32_t last;
2132};
2133
2134static int bisearch(uint32_t ucs, const struct interval *table, int max)
2135{
2136 int min = 0;
2137 int mid;
2138
2139 if (ucs < table[0].first || ucs > table[max].last)
2140 return 0;
2141 while (max >= min) {
2142 mid = (min + max) / 2;
2143 if (ucs > table[mid].last)
2144 min = mid + 1;
2145 else if (ucs < table[mid].first)
2146 max = mid - 1;
2147 else
2148 return 1;
2149 }
2150 return 0;
2151}
2152
2153static int is_double_width(uint32_t ucs)
2154{
2155 static const struct interval double_width[] = {
2156 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2157 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002158 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2159 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002160 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002161 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002162}
2163
Torben Hohnac751ef2011-01-25 15:07:35 -08002164/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2166{
2167#ifdef VT_BUF_VRAM_ONLY
2168#define FLUSH do { } while(0);
2169#else
2170#define FLUSH if (draw_x >= 0) { \
2171 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2172 draw_x = -1; \
2173 }
2174#endif
2175
2176 int c, tc, ok, n = 0, draw_x = -1;
2177 unsigned int currcons;
2178 unsigned long draw_from = 0, draw_to = 0;
2179 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002180 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002181 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002182 uint8_t rescan;
2183 uint8_t inverse;
2184 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 if (in_interrupt())
2188 return count;
2189
2190 might_sleep();
2191
Torben Hohnac751ef2011-01-25 15:07:35 -08002192 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 vc = tty->driver_data;
2194 if (vc == NULL) {
2195 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002196 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return 0;
2198 }
2199
2200 currcons = vc->vc_num;
2201 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002202 /* could this happen? */
2203 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2204 console_unlock();
2205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 himask = vc->vc_hi_font_mask;
2209 charmask = himask ? 0x1ff : 0xff;
2210
2211 /* undraw cursor first */
2212 if (IS_FG(vc))
2213 hide_cursor(vc);
2214
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002215 param.vc = vc;
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 while (!tty->stopped && count) {
2218 int orig = *buf;
2219 c = orig;
2220 buf++;
2221 n++;
2222 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002223 rescan = 0;
2224 inverse = 0;
2225 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
2227 /* Do no translation at all in control states */
2228 if (vc->vc_state != ESnormal) {
2229 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002230 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002231 /* Combine UTF-8 into Unicode in vc_utf_char.
2232 * vc_utf_count is the number of continuation bytes still
2233 * expected to arrive.
2234 * vc_npar is the number of continuation bytes arrived so
2235 * far
2236 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002237rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002238 if ((c & 0xc0) == 0x80) {
2239 /* Continuation byte received */
2240 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002241 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002242 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2243 vc->vc_npar++;
2244 if (--vc->vc_utf_count) {
2245 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002247 }
2248 /* Got a whole character */
2249 c = vc->vc_utf_char;
2250 /* Reject overlong sequences */
2251 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2252 c > utf8_length_changes[vc->vc_npar])
2253 c = 0xfffd;
2254 } else {
2255 /* Unexpected continuation byte */
2256 vc->vc_utf_count = 0;
2257 c = 0xfffd;
2258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002260 /* Single ASCII byte or first byte of a sequence received */
2261 if (vc->vc_utf_count) {
2262 /* Continuation byte expected */
2263 rescan = 1;
2264 vc->vc_utf_count = 0;
2265 c = 0xfffd;
2266 } else if (c > 0x7f) {
2267 /* First byte of a multibyte sequence received */
2268 vc->vc_npar = 0;
2269 if ((c & 0xe0) == 0xc0) {
2270 vc->vc_utf_count = 1;
2271 vc->vc_utf_char = (c & 0x1f);
2272 } else if ((c & 0xf0) == 0xe0) {
2273 vc->vc_utf_count = 2;
2274 vc->vc_utf_char = (c & 0x0f);
2275 } else if ((c & 0xf8) == 0xf0) {
2276 vc->vc_utf_count = 3;
2277 vc->vc_utf_char = (c & 0x07);
2278 } else if ((c & 0xfc) == 0xf8) {
2279 vc->vc_utf_count = 4;
2280 vc->vc_utf_char = (c & 0x03);
2281 } else if ((c & 0xfe) == 0xfc) {
2282 vc->vc_utf_count = 5;
2283 vc->vc_utf_char = (c & 0x01);
2284 } else {
2285 /* 254 and 255 are invalid */
2286 c = 0xfffd;
2287 }
2288 if (vc->vc_utf_count) {
2289 /* Still need some bytes */
2290 continue;
2291 }
2292 }
2293 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002295 /* End of UTF-8 decoding. */
2296 /* c is the received character, or U+FFFD for invalid sequences. */
2297 /* Replace invalid Unicode code points with U+FFFD too */
2298 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2299 c = 0xfffd;
2300 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002301 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002302 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002305 param.c = tc;
2306 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2307 &param) == NOTIFY_STOP)
2308 continue;
2309
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 /* If the original code was a control character we
2311 * only allow a glyph to be displayed if the code is
2312 * not normally used (such as for cursor movement) or
2313 * if the disp_ctrl mode has been explicitly enabled.
2314 * Certain characters (as given by the CTRL_ALWAYS
2315 * bitmap) are always displayed as control characters,
2316 * as the console would be pretty useless without
2317 * them; to display an arbitrary font position use the
2318 * direct-to-font zone in UTF-8 mode.
2319 */
2320 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002321 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2322 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 && (c != 127 || vc->vc_disp_ctrl)
2324 && (c != 128+27);
2325
2326 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002327 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2328 if (is_double_width(c))
2329 width = 2;
2330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 /* Now try to find out how to display it */
2332 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002333 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002334 if (tc == -1 || tc == -2) {
2335 continue; /* nothing to display */
2336 }
2337 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002338 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002339 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002340 This would make absolutely no sense with Unicode in mind,
2341 but do this for ASCII characters since a font may lack
2342 Unicode mapping info and we don't want to end up with
2343 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002344 tc = c;
2345 } else {
2346 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2347 tc = conv_uni_to_pc(vc, 0xfffd);
2348 if (tc < 0) {
2349 inverse = 1;
2350 tc = conv_uni_to_pc(vc, '?');
2351 if (tc < 0) tc = '?';
2352 }
2353 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002356 if (!inverse) {
2357 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002359 /* invert vc_attr */
2360 if (!vc->vc_can_do_color) {
2361 vc_attr = (vc->vc_attr) ^ 0x08;
2362 } else if (vc->vc_hi_font_mask == 0x100) {
2363 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2364 } else {
2365 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002366 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002367 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002368 }
2369
2370 while (1) {
2371 if (vc->vc_need_wrap || vc->vc_decim)
2372 FLUSH
2373 if (vc->vc_need_wrap) {
2374 cr(vc);
2375 lf(vc);
2376 }
2377 if (vc->vc_decim)
2378 insert_char(vc, 1);
2379 scr_writew(himask ?
2380 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2381 (vc_attr << 8) + tc,
2382 (u16 *) vc->vc_pos);
2383 if (DO_UPDATE(vc) && draw_x < 0) {
2384 draw_x = vc->vc_x;
2385 draw_from = vc->vc_pos;
2386 }
2387 if (vc->vc_x == vc->vc_cols - 1) {
2388 vc->vc_need_wrap = vc->vc_decawm;
2389 draw_to = vc->vc_pos + 2;
2390 } else {
2391 vc->vc_x++;
2392 draw_to = (vc->vc_pos += 2);
2393 }
2394
2395 if (!--width) break;
2396
2397 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2398 if (tc < 0) tc = ' ';
2399 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002400 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002401
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002402 if (inverse) {
2403 FLUSH
2404 }
2405
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002406 if (rescan) {
2407 rescan = 0;
2408 inverse = 0;
2409 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002410 c = orig;
2411 goto rescan_last_byte;
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 continue;
2414 }
2415 FLUSH
2416 do_con_trol(tty, vc, orig);
2417 }
2418 FLUSH
2419 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002420 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002421 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 return n;
2423#undef FLUSH
2424}
2425
2426/*
2427 * This is the console switching callback.
2428 *
2429 * Doing console switching in a process context allows
2430 * us to do the switches asynchronously (needed when we want
2431 * to switch due to a keyboard interrupt). Synchronization
2432 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002433 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 */
David Howells65f27f32006-11-22 14:55:48 +00002435static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Torben Hohnac751ef2011-01-25 15:07:35 -08002437 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
2439 if (want_console >= 0) {
2440 if (want_console != fg_console &&
2441 vc_cons_allocated(want_console)) {
2442 hide_cursor(vc_cons[fg_console].d);
2443 change_console(vc_cons[want_console].d);
2444 /* we only changed when the console had already
2445 been allocated - a new console is not created
2446 in an interrupt routine */
2447 }
2448 want_console = -1;
2449 }
2450 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2451 do_poke_blanked_console = 0;
2452 poke_blanked_console();
2453 }
2454 if (scrollback_delta) {
2455 struct vc_data *vc = vc_cons[fg_console].d;
2456 clear_selection();
2457 if (vc->vc_mode == KD_TEXT)
2458 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2459 scrollback_delta = 0;
2460 }
2461 if (blank_timer_expired) {
2462 do_blank_screen(0);
2463 blank_timer_expired = 0;
2464 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002465 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
Torben Hohnac751ef2011-01-25 15:07:35 -08002467 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468}
2469
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002470int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002472 struct vc_data *vc = vc_cons[fg_console].d;
2473
2474 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2475 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2476
2477 /*
2478 * Console switch will fail in console_callback() or
2479 * change_console() so there is no point scheduling
2480 * the callback
2481 *
2482 * Existing set_console() users don't check the return
2483 * value so this shouldn't break anything
2484 */
2485 return -EINVAL;
2486 }
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 want_console = nr;
2489 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002490
2491 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494struct tty_driver *console_driver;
2495
2496#ifdef CONFIG_VT_CONSOLE
2497
Bernhard Walle5ada9182009-12-14 18:00:43 -08002498/**
2499 * vt_kmsg_redirect() - Sets/gets the kernel message console
2500 * @new: The new virtual terminal number or -1 if the console should stay
2501 * unchanged
2502 *
2503 * By default, the kernel messages are always printed on the current virtual
2504 * console. However, the user may modify that default with the
2505 * TIOCL_SETKMSGREDIRECT ioctl call.
2506 *
2507 * This function sets the kernel message console to be @new. It returns the old
2508 * virtual console number. The virtual terminal number 0 (both as parameter and
2509 * return value) means no redirection (i.e. always printed on the currently
2510 * active console).
2511 *
2512 * The parameter -1 means that only the current console is returned, but the
2513 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2514 * case to make the code more understandable.
2515 *
2516 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2517 * the parameter and always returns 0.
2518 */
2519int vt_kmsg_redirect(int new)
2520{
2521 static int kmsg_con;
2522
2523 if (new != -1)
2524 return xchg(&kmsg_con, new);
2525 else
2526 return kmsg_con;
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529/*
2530 * Console on virtual terminal
2531 *
2532 * The console must be locked when we get here.
2533 */
2534
2535static void vt_console_print(struct console *co, const char *b, unsigned count)
2536{
2537 struct vc_data *vc = vc_cons[fg_console].d;
2538 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002539 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 const ushort *start;
2541 ushort cnt = 0;
2542 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002543 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
2545 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002546 if (!printable)
2547 return;
2548 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 return;
2550
Bernhard Walle5ada9182009-12-14 18:00:43 -08002551 kmsg_console = vt_get_kmsg_redirect();
2552 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2553 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
2555 /* read `x' only after setting currcons properly (otherwise
2556 the `x' macro will read the x of the foreground console). */
2557 myx = vc->vc_x;
2558
2559 if (!vc_cons_allocated(fg_console)) {
2560 /* impossible */
2561 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2562 goto quit;
2563 }
2564
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002565 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 goto quit;
2567
2568 /* undraw cursor first */
2569 if (IS_FG(vc))
2570 hide_cursor(vc);
2571
2572 start = (ushort *)vc->vc_pos;
2573
2574 /* Contrived structure to try to emulate original need_wrap behaviour
2575 * Problems caused when we have need_wrap set on '\n' character */
2576 while (count--) {
2577 c = *b++;
2578 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2579 if (cnt > 0) {
2580 if (CON_IS_VISIBLE(vc))
2581 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2582 vc->vc_x += cnt;
2583 if (vc->vc_need_wrap)
2584 vc->vc_x--;
2585 cnt = 0;
2586 }
2587 if (c == 8) { /* backspace */
2588 bs(vc);
2589 start = (ushort *)vc->vc_pos;
2590 myx = vc->vc_x;
2591 continue;
2592 }
2593 if (c != 13)
2594 lf(vc);
2595 cr(vc);
2596 start = (ushort *)vc->vc_pos;
2597 myx = vc->vc_x;
2598 if (c == 10 || c == 13)
2599 continue;
2600 }
2601 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002602 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 cnt++;
2604 if (myx == vc->vc_cols - 1) {
2605 vc->vc_need_wrap = 1;
2606 continue;
2607 }
2608 vc->vc_pos += 2;
2609 myx++;
2610 }
2611 if (cnt > 0) {
2612 if (CON_IS_VISIBLE(vc))
2613 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2614 vc->vc_x += cnt;
2615 if (vc->vc_x == vc->vc_cols) {
2616 vc->vc_x--;
2617 vc->vc_need_wrap = 1;
2618 }
2619 }
2620 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002621 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002624 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
2627static struct tty_driver *vt_console_device(struct console *c, int *index)
2628{
2629 *index = c->index ? c->index-1 : fg_console;
2630 return console_driver;
2631}
2632
2633static struct console vt_console_driver = {
2634 .name = "tty",
2635 .write = vt_console_print,
2636 .device = vt_console_device,
2637 .unblank = unblank_screen,
2638 .flags = CON_PRINTBUFFER,
2639 .index = -1,
2640};
2641#endif
2642
2643/*
2644 * Handling of Linux-specific VC ioctls
2645 */
2646
2647/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002648 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 *
2650 * There are some functions which don't need it.
2651 *
2652 * There are some functions which can sleep for arbitrary periods
2653 * (paste_selection) but we don't need the lock there anyway.
2654 *
2655 * set_selection has locking, and definitely needs it
2656 */
2657
2658int tioclinux(struct tty_struct *tty, unsigned long arg)
2659{
2660 char type, data;
2661 char __user *p = (char __user *)arg;
2662 int lines;
2663 int ret;
2664
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2666 return -EPERM;
2667 if (get_user(type, p))
2668 return -EFAULT;
2669 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 switch (type)
2672 {
2673 case TIOCL_SETSEL:
Torben Hohnac751ef2011-01-25 15:07:35 -08002674 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Torben Hohnac751ef2011-01-25 15:07:35 -08002676 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 break;
2678 case TIOCL_PASTESEL:
2679 ret = paste_selection(tty);
2680 break;
2681 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002682 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002684 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 break;
2686 case TIOCL_SELLOADLUT:
Alan Cox52894752012-03-02 15:00:02 +00002687 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 ret = sel_loadlut(p);
Alan Cox52894752012-03-02 15:00:02 +00002689 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 break;
2691 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /*
2694 * Make it possible to react to Shift+Mousebutton.
2695 * Note that 'shift_state' is an undocumented
2696 * kernel-internal variable; programs not closely
2697 * related to the kernel should not use this.
2698 */
Alan Cox079c9532012-02-28 14:49:23 +00002699 data = vt_get_shift_state();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 ret = __put_user(data, p);
2701 break;
2702 case TIOCL_GETMOUSEREPORTING:
Alan Cox20f62572012-03-02 14:59:37 +00002703 console_lock(); /* May be overkill */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 data = mouse_reporting();
Alan Cox20f62572012-03-02 14:59:37 +00002705 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 ret = __put_user(data, p);
2707 break;
2708 case TIOCL_SETVESABLANK:
Alan Cox20f62572012-03-02 14:59:37 +00002709 console_lock();
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002710 ret = set_vesa_blanking(p);
Alan Cox20f62572012-03-02 14:59:37 +00002711 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002713 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002714 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002715 ret = __put_user(data, p);
2716 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 case TIOCL_SETKMSGREDIRECT:
2718 if (!capable(CAP_SYS_ADMIN)) {
2719 ret = -EPERM;
2720 } else {
2721 if (get_user(data, p+1))
2722 ret = -EFAULT;
2723 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002724 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 }
2726 break;
2727 case TIOCL_GETFGCONSOLE:
Alan Cox20f62572012-03-02 14:59:37 +00002728 /* No locking needed as this is a transiently
2729 correct return anyway if the caller hasn't
2730 disabled switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 ret = fg_console;
2732 break;
2733 case TIOCL_SCROLLCONSOLE:
2734 if (get_user(lines, (s32 __user *)(p+4))) {
2735 ret = -EFAULT;
2736 } else {
Alan Cox20f62572012-03-02 14:59:37 +00002737 /* Need the console lock here. Note that lots
2738 of other calls need fixing before the lock
2739 is actually useful ! */
2740 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741 scrollfront(vc_cons[fg_console].d, lines);
Alan Cox20f62572012-03-02 14:59:37 +00002742 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 ret = 0;
2744 }
2745 break;
2746 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08002747 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 ignore_poke = 1;
2749 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08002750 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 break;
2752 case TIOCL_BLANKEDSCREEN:
2753 ret = console_blanked;
2754 break;
2755 default:
2756 ret = -EINVAL;
2757 break;
2758 }
2759 return ret;
2760}
2761
2762/*
2763 * /dev/ttyN handling
2764 */
2765
2766static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2767{
2768 int retval;
2769
2770 retval = do_con_write(tty, buf, count);
2771 con_flush_chars(tty);
2772
2773 return retval;
2774}
2775
Alan Cox5d19f542008-04-30 00:54:08 -07002776static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
2778 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002779 return 0; /* n_r3964 calls put_char() from interrupt context */
2780 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783static int con_write_room(struct tty_struct *tty)
2784{
2785 if (tty->stopped)
2786 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002787 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
2790static int con_chars_in_buffer(struct tty_struct *tty)
2791{
2792 return 0; /* we're not buffering */
2793}
2794
2795/*
2796 * con_throttle and con_unthrottle are only used for
2797 * paste_selection(), which has to stuff in a large number of
2798 * characters...
2799 */
2800static void con_throttle(struct tty_struct *tty)
2801{
2802}
2803
2804static void con_unthrottle(struct tty_struct *tty)
2805{
2806 struct vc_data *vc = tty->driver_data;
2807
2808 wake_up_interruptible(&vc->paste_wait);
2809}
2810
2811/*
2812 * Turn the Scroll-Lock LED on when the tty is stopped
2813 */
2814static void con_stop(struct tty_struct *tty)
2815{
2816 int console_num;
2817 if (!tty)
2818 return;
2819 console_num = tty->index;
2820 if (!vc_cons_allocated(console_num))
2821 return;
Alan Cox079c9532012-02-28 14:49:23 +00002822 vt_kbd_con_stop(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823}
2824
2825/*
2826 * Turn the Scroll-Lock LED off when the console is started
2827 */
2828static void con_start(struct tty_struct *tty)
2829{
2830 int console_num;
2831 if (!tty)
2832 return;
2833 console_num = tty->index;
2834 if (!vc_cons_allocated(console_num))
2835 return;
Alan Cox079c9532012-02-28 14:49:23 +00002836 vt_kbd_con_start(console_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
2838
2839static void con_flush_chars(struct tty_struct *tty)
2840{
2841 struct vc_data *vc;
2842
2843 if (in_interrupt()) /* from flush_to_ldisc */
2844 return;
2845
2846 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08002847 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 vc = tty->driver_data;
2849 if (vc)
2850 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08002851 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852}
2853
2854/*
2855 * Allocate the console screen memory.
2856 */
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002857static int con_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858{
2859 unsigned int currcons = tty->index;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002860 struct vc_data *vc;
2861 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Torben Hohnac751ef2011-01-25 15:07:35 -08002863 console_lock();
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002864 ret = vc_allocate(currcons);
2865 if (ret)
2866 goto unlock;
Alan Coxfeebed62008-10-13 10:41:30 +01002867
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002868 vc = vc_cons[currcons].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002870 /* Still being freed */
2871 if (vc->port.tty) {
2872 ret = -ERESTARTSYS;
2873 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 }
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002875
2876 ret = tty_port_install(&vc->port, driver, tty);
2877 if (ret)
2878 goto unlock;
2879
2880 tty->driver_data = vc;
2881 vc->port.tty = tty;
2882
2883 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2884 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2885 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2886 }
2887 if (vc->vc_utf)
Alan Coxadc8d742012-07-14 15:31:47 +01002888 tty->termios.c_iflag |= IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002889 else
Alan Coxadc8d742012-07-14 15:31:47 +01002890 tty->termios.c_iflag &= ~IUTF8;
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002891unlock:
Torben Hohnac751ef2011-01-25 15:07:35 -08002892 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return ret;
2894}
2895
Jiri Slabybc1e99d2012-06-04 13:35:33 +02002896static int con_open(struct tty_struct *tty, struct file *filp)
2897{
2898 /* everything done in install */
2899 return 0;
2900}
2901
2902
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903static void con_close(struct tty_struct *tty, struct file *filp)
2904{
Alan Coxfeebed62008-10-13 10:41:30 +01002905 /* Nothing to do - we defer to shutdown */
2906}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Alan Coxfeebed62008-10-13 10:41:30 +01002908static void con_shutdown(struct tty_struct *tty)
2909{
2910 struct vc_data *vc = tty->driver_data;
2911 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08002912 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02002913 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08002914 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915}
2916
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002917static int default_color = 7; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002918static int default_italic_color = 2; // green (ASCII)
2919static int default_underline_color = 3; // cyan (ASCII)
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002920module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002921module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2922module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924static void vc_init(struct vc_data *vc, unsigned int rows,
2925 unsigned int cols, int do_clear)
2926{
2927 int j, k ;
2928
2929 vc->vc_cols = cols;
2930 vc->vc_rows = rows;
2931 vc->vc_size_row = cols << 1;
2932 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2933
2934 set_origin(vc);
2935 vc->vc_pos = vc->vc_origin;
2936 reset_vc(vc);
2937 for (j=k=0; j<16; j++) {
2938 vc->vc_palette[k++] = default_red[j] ;
2939 vc->vc_palette[k++] = default_grn[j] ;
2940 vc->vc_palette[k++] = default_blu[j] ;
2941 }
Clemens Ladisch3855ae12013-08-04 13:09:50 +02002942 vc->vc_def_color = default_color;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002943 vc->vc_ulcolor = default_underline_color;
2944 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 vc->vc_halfcolor = 0x08; /* grey */
2946 init_waitqueue_head(&vc->paste_wait);
2947 reset_terminal(vc, do_clear);
2948}
2949
2950/*
2951 * This routine initializes console interrupts, and does nothing
2952 * else. If you want the screen to clear, call tty_write with
2953 * the appropriate escape-sequence.
2954 */
2955
2956static int __init con_init(void)
2957{
2958 const char *display_desc = NULL;
2959 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002960 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
Torben Hohnac751ef2011-01-25 15:07:35 -08002962 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 if (conswitchp)
2965 display_desc = conswitchp->con_startup();
2966 if (!display_desc) {
2967 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002968 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 return 0;
2970 }
2971
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002972 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2973 struct con_driver *con_driver = &registered_con_driver[i];
2974
2975 if (con_driver->con == NULL) {
2976 con_driver->con = conswitchp;
2977 con_driver->desc = display_desc;
2978 con_driver->flag = CON_DRIVER_FLAG_INIT;
2979 con_driver->first = 0;
2980 con_driver->last = MAX_NR_CONSOLES - 1;
2981 break;
2982 }
2983 }
2984
2985 for (i = 0; i < MAX_NR_CONSOLES; i++)
2986 con_driver_map[i] = conswitchp;
2987
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 if (blankinterval) {
2989 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002990 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002994 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002995 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02002996 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002998 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 vc_init(vc, vc->vc_rows, vc->vc_cols,
3000 currcons || !vc->vc_sw->con_save_screen);
3001 }
3002 currcons = fg_console = 0;
3003 master_display_fg = vc = vc_cons[currcons].d;
3004 set_origin(vc);
3005 save_screen(vc);
3006 gotoxy(vc, vc->vc_x, vc->vc_y);
3007 csi_J(vc, 0);
3008 update_screen(vc);
Kay Sievers5da527a2012-04-03 03:18:23 +02003009 pr_info("Console: %s %s %dx%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 vc->vc_can_do_color ? "colour" : "mono",
3011 display_desc, vc->vc_cols, vc->vc_rows);
3012 printable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Torben Hohnac751ef2011-01-25 15:07:35 -08003014 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015
3016#ifdef CONFIG_VT_CONSOLE
3017 register_console(&vt_console_driver);
3018#endif
3019 return 0;
3020}
3021console_initcall(con_init);
3022
Jeff Dikeb68e31d2006-10-02 02:17:18 -07003023static const struct tty_operations con_ops = {
Jiri Slabybc1e99d2012-06-04 13:35:33 +02003024 .install = con_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 .open = con_open,
3026 .close = con_close,
3027 .write = con_write,
3028 .write_room = con_write_room,
3029 .put_char = con_put_char,
3030 .flush_chars = con_flush_chars,
3031 .chars_in_buffer = con_chars_in_buffer,
3032 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02003033#ifdef CONFIG_COMPAT
3034 .compat_ioctl = vt_compat_ioctl,
3035#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .stop = con_stop,
3037 .start = con_start,
3038 .throttle = con_throttle,
3039 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01003040 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01003041 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042};
3043
Alan Coxd81ed102008-10-13 10:41:42 +01003044static struct cdev vc0_cdev;
3045
Kay Sieversfbc92a32010-12-01 18:51:05 +01003046static ssize_t show_tty_active(struct device *dev,
3047 struct device_attribute *attr, char *buf)
3048{
3049 return sprintf(buf, "tty%d\n", fg_console + 1);
3050}
3051static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3052
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003053static struct attribute *vt_dev_attrs[] = {
3054 &dev_attr_active.attr,
3055 NULL
3056};
3057
3058ATTRIBUTE_GROUPS(vt_dev);
3059
Alan Coxd81ed102008-10-13 10:41:42 +01003060int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Alan Coxd81ed102008-10-13 10:41:42 +01003062 cdev_init(&vc0_cdev, console_fops);
3063 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3064 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3065 panic("Couldn't register /dev/tty0 driver\n");
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003066 tty0dev = device_create_with_groups(tty_class, NULL,
3067 MKDEV(TTY_MAJOR, 0), NULL,
3068 vt_dev_groups, "tty0");
Kay Sieversfbc92a32010-12-01 18:51:05 +01003069 if (IS_ERR(tty0dev))
3070 tty0dev = NULL;
Alan Coxd81ed102008-10-13 10:41:42 +01003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 vcs_init();
3073
3074 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3075 if (!console_driver)
3076 panic("Couldn't allocate console driver\n");
Jiri Slaby2f166692012-03-05 14:51:52 +01003077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 console_driver->name = "tty";
3079 console_driver->name_base = 1;
3080 console_driver->major = TTY_MAJOR;
3081 console_driver->minor_start = 1;
3082 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3083 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07003084 if (default_utf8)
3085 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3087 tty_set_operations(console_driver, &con_ops);
3088 if (tty_register_driver(console_driver))
3089 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 kbd_init();
3091 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092#ifdef CONFIG_MDA_CONSOLE
3093 mda_console_init();
3094#endif
3095 return 0;
3096}
3097
3098#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003099
3100static struct class *vtconsole_class;
3101
Alan Cox50e244c2013-01-25 10:28:15 +10003102static int do_bind_con_driver(const struct consw *csw, int first, int last,
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003103 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003105 struct module *owner = csw->owner;
3106 const char *desc = NULL;
3107 struct con_driver *con_driver;
3108 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 if (!try_module_get(owner))
3111 return -ENODEV;
3112
Alan Cox50e244c2013-01-25 10:28:15 +10003113 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003114
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003115 /* check if driver is registered */
3116 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3117 con_driver = &registered_con_driver[i];
3118
3119 if (con_driver->con == csw) {
3120 desc = con_driver->desc;
3121 retval = 0;
3122 break;
3123 }
3124 }
3125
3126 if (retval)
3127 goto err;
3128
3129 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3130 csw->con_startup();
3131 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 if (deflt) {
3135 if (conswitchp)
3136 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 __module_get(owner);
3139 conswitchp = csw;
3140 }
3141
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003142 first = max(first, con_driver->first);
3143 last = min(last, con_driver->last);
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145 for (i = first; i <= last; i++) {
3146 int old_was_color;
3147 struct vc_data *vc = vc_cons[i].d;
3148
3149 if (con_driver_map[i])
3150 module_put(con_driver_map[i]->owner);
3151 __module_get(owner);
3152 con_driver_map[i] = csw;
3153
3154 if (!vc || !vc->vc_sw)
3155 continue;
3156
3157 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003158
David Hollister4ee1acc2006-06-26 00:26:41 -07003159 if (CON_IS_VISIBLE(vc)) {
3160 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003162 }
3163
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 old_was_color = vc->vc_can_do_color;
3165 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003166 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003168 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 update_attr(vc);
3170
3171 /* If the console changed between mono <-> color, then
3172 * the attributes in the screenbuf will be wrong. The
3173 * following resets all attributes to something sane.
3174 */
3175 if (old_was_color != vc->vc_can_do_color)
3176 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003178
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003179 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (!deflt)
3181 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003182 if (j >= 0) {
3183 struct vc_data *vc = vc_cons[j].d;
3184
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003186 vc->vc_can_do_color ? "colour" : "mono",
3187 desc, vc->vc_cols, vc->vc_rows);
3188
3189 if (k >= 0) {
3190 vc = vc_cons[k].d;
3191 update_screen(vc);
3192 }
3193 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 printk("to %s\n", desc);
3195
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003196 retval = 0;
3197err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003199 return retval;
3200};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201
Alan Cox50e244c2013-01-25 10:28:15 +10003202
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003203#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3204static int con_is_graphics(const struct consw *csw, int first, int last)
3205{
3206 int i, retval = 0;
3207
3208 for (i = first; i <= last; i++) {
3209 struct vc_data *vc = vc_cons[i].d;
3210
3211 if (vc && vc->vc_mode == KD_GRAPHICS) {
3212 retval = 1;
3213 break;
3214 }
3215 }
3216
3217 return retval;
3218}
3219
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003220/* unlocked version of unbind_con_driver() */
3221int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3222{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003223 struct module *owner = csw->owner;
3224 const struct consw *defcsw = NULL;
3225 struct con_driver *con_driver = NULL, *con_back = NULL;
3226 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003228 if (!try_module_get(owner))
3229 return -ENODEV;
3230
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003231 WARN_CONSOLE_UNLOCKED();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003232
3233 /* check if driver is registered and if it is unbindable */
3234 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3235 con_driver = &registered_con_driver[i];
3236
3237 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003238 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003239 retval = 0;
3240 break;
3241 }
3242 }
3243
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003244 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003245 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003246
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003247 retval = -ENODEV;
3248
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003249 /* check if backup driver exists */
3250 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3251 con_back = &registered_con_driver[i];
3252
Daniel Vetter249f7b32014-06-05 16:24:47 +02003253 if (con_back->con && con_back->con != csw) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003254 defcsw = con_back->con;
3255 retval = 0;
3256 break;
3257 }
3258 }
3259
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003260 if (retval)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003261 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003262
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003263 if (!con_is_bound(csw))
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003264 goto err;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003265
3266 first = max(first, con_driver->first);
3267 last = min(last, con_driver->last);
3268
3269 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 if (con_driver_map[i] == csw) {
3271 module_put(csw->owner);
3272 con_driver_map[i] = NULL;
3273 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003274 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003275
3276 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003277 const struct consw *defconsw = conswitchp;
3278
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003279 defcsw->con_startup();
3280 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003281 /*
3282 * vgacon may change the default driver to point
3283 * to dummycon, we restore it here...
3284 */
3285 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003286 }
3287
3288 if (!con_is_bound(csw))
3289 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3290
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003291 /* ignore return value, binding should not fail */
Alan Cox50e244c2013-01-25 10:28:15 +10003292 do_bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003293err:
3294 module_put(owner);
3295 return retval;
3296
3297}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003298EXPORT_SYMBOL_GPL(do_unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003299
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003300static int vt_bind(struct con_driver *con)
3301{
3302 const struct consw *defcsw = NULL, *csw = NULL;
3303 int i, more = 1, first = -1, last = -1, deflt = 0;
3304
3305 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3306 con_is_graphics(con->con, con->first, con->last))
3307 goto err;
3308
3309 csw = con->con;
3310
3311 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3312 struct con_driver *con = &registered_con_driver[i];
3313
3314 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3315 defcsw = con->con;
3316 break;
3317 }
3318 }
3319
3320 if (!defcsw)
3321 goto err;
3322
3323 while (more) {
3324 more = 0;
3325
3326 for (i = con->first; i <= con->last; i++) {
3327 if (con_driver_map[i] == defcsw) {
3328 if (first == -1)
3329 first = i;
3330 last = i;
3331 more = 1;
3332 } else if (first != -1)
3333 break;
3334 }
3335
3336 if (first == 0 && last == MAX_NR_CONSOLES -1)
3337 deflt = 1;
3338
Imre Deak4c215fe2014-12-16 00:16:00 +02003339 if (first != -1)
Wang YanQingc62a1e52013-05-09 02:14:21 +08003340 do_bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003341
3342 first = -1;
3343 last = -1;
3344 deflt = 0;
3345 }
3346
3347err:
3348 return 0;
3349}
3350
3351static int vt_unbind(struct con_driver *con)
3352{
3353 const struct consw *csw = NULL;
3354 int i, more = 1, first = -1, last = -1, deflt = 0;
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003355 int ret;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003356
3357 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3358 con_is_graphics(con->con, con->first, con->last))
3359 goto err;
3360
3361 csw = con->con;
3362
3363 while (more) {
3364 more = 0;
3365
3366 for (i = con->first; i <= con->last; i++) {
3367 if (con_driver_map[i] == csw) {
3368 if (first == -1)
3369 first = i;
3370 last = i;
3371 more = 1;
3372 } else if (first != -1)
3373 break;
3374 }
3375
3376 if (first == 0 && last == MAX_NR_CONSOLES -1)
3377 deflt = 1;
3378
Wang YanQing618f2b92013-05-09 02:14:07 +08003379 if (first != -1) {
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003380 ret = do_unbind_con_driver(csw, first, last, deflt);
Daniel Vetterf418f2e2014-06-05 16:33:24 +02003381 if (ret != 0)
3382 return ret;
Wang YanQing618f2b92013-05-09 02:14:07 +08003383 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003384
3385 first = -1;
3386 last = -1;
3387 deflt = 0;
3388 }
3389
3390err:
3391 return 0;
3392}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003393#else
3394static inline int vt_bind(struct con_driver *con)
3395{
3396 return 0;
3397}
3398static inline int vt_unbind(struct con_driver *con)
3399{
3400 return 0;
3401}
3402#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003403
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003404static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003405 const char *buf, size_t count)
3406{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003407 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003408 int bind = simple_strtoul(buf, NULL, 0);
3409
Imre Deak4c215fe2014-12-16 00:16:00 +02003410 console_lock();
3411
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003412 if (bind)
3413 vt_bind(con);
3414 else
3415 vt_unbind(con);
3416
Imre Deak4c215fe2014-12-16 00:16:00 +02003417 console_unlock();
3418
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003419 return count;
3420}
3421
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003422static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3423 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003424{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003425 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003426 int bind = con_is_bound(con->con);
3427
3428 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3429}
3430
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003431static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3432 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003433{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003434 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003435
3436 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3437 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3438 con->desc);
3439
3440}
3441
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003442static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3443static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3444
3445static struct attribute *con_dev_attrs[] = {
3446 &dev_attr_bind.attr,
3447 &dev_attr_name.attr,
3448 NULL
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003449};
3450
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003451ATTRIBUTE_GROUPS(con_dev);
3452
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003453static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003454{
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003455 con->flag |= CON_DRIVER_FLAG_ATTR;
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003456 return 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003457}
3458
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003459static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003460{
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003461 con->flag &= ~CON_DRIVER_FLAG_ATTR;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003462}
3463
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003464/**
3465 * con_is_bound - checks if driver is bound to the console
3466 * @csw: console driver
3467 *
3468 * RETURNS: zero if unbound, nonzero if bound
3469 *
3470 * Drivers can call this and if zero, they should release
3471 * all resources allocated on con_startup()
3472 */
3473int con_is_bound(const struct consw *csw)
3474{
3475 int i, bound = 0;
3476
3477 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3478 if (con_driver_map[i] == csw) {
3479 bound = 1;
3480 break;
3481 }
3482 }
3483
3484 return bound;
3485}
3486EXPORT_SYMBOL(con_is_bound);
3487
3488/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003489 * con_debug_enter - prepare the console for the kernel debugger
3490 * @sw: console driver
3491 *
3492 * Called when the console is taken over by the kernel debugger, this
3493 * function needs to save the current console state, then put the console
3494 * into a state suitable for the kernel debugger.
3495 *
3496 * RETURNS:
3497 * Zero on success, nonzero if a failure occurred when trying to prepare
3498 * the console for the debugger.
3499 */
3500int con_debug_enter(struct vc_data *vc)
3501{
3502 int ret = 0;
3503
3504 saved_fg_console = fg_console;
3505 saved_last_console = last_console;
3506 saved_want_console = want_console;
3507 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003508 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003509 vc->vc_mode = KD_TEXT;
3510 console_blanked = 0;
3511 if (vc->vc_sw->con_debug_enter)
3512 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003513#ifdef CONFIG_KGDB_KDB
3514 /* Set the initial LINES variable if it is not already set */
3515 if (vc->vc_rows < 999) {
3516 int linecount;
3517 char lns[4];
3518 const char *setargs[3] = {
3519 "set",
3520 "LINES",
3521 lns,
3522 };
3523 if (kdbgetintenv(setargs[0], &linecount)) {
3524 snprintf(lns, 4, "%i", vc->vc_rows);
3525 kdb_set(2, setargs);
3526 }
3527 }
Jason Wessel17b572e82012-08-26 22:37:03 -05003528 if (vc->vc_cols < 999) {
3529 int colcount;
3530 char cols[4];
3531 const char *setargs[3] = {
3532 "set",
3533 "COLUMNS",
3534 cols,
3535 };
3536 if (kdbgetintenv(setargs[0], &colcount)) {
3537 snprintf(cols, 4, "%i", vc->vc_cols);
3538 kdb_set(2, setargs);
3539 }
3540 }
Jason Wessel81d44502010-08-05 09:22:30 -05003541#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003542 return ret;
3543}
3544EXPORT_SYMBOL_GPL(con_debug_enter);
3545
3546/**
3547 * con_debug_leave - restore console state
3548 * @sw: console driver
3549 *
3550 * Restore the console state to what it was before the kernel debugger
3551 * was invoked.
3552 *
3553 * RETURNS:
3554 * Zero on success, nonzero if a failure occurred when trying to restore
3555 * the console.
3556 */
3557int con_debug_leave(void)
3558{
3559 struct vc_data *vc;
3560 int ret = 0;
3561
3562 fg_console = saved_fg_console;
3563 last_console = saved_last_console;
3564 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003565 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003566 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3567
3568 vc = vc_cons[fg_console].d;
3569 if (vc->vc_sw->con_debug_leave)
3570 ret = vc->vc_sw->con_debug_leave(vc);
3571 return ret;
3572}
3573EXPORT_SYMBOL_GPL(con_debug_leave);
3574
Alan Cox50e244c2013-01-25 10:28:15 +10003575static int do_register_con_driver(const struct consw *csw, int first, int last)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003576{
3577 struct module *owner = csw->owner;
3578 struct con_driver *con_driver;
3579 const char *desc;
3580 int i, retval = 0;
3581
Alan Cox50e244c2013-01-25 10:28:15 +10003582 WARN_CONSOLE_UNLOCKED();
3583
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003584 if (!try_module_get(owner))
3585 return -ENODEV;
3586
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003587 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3588 con_driver = &registered_con_driver[i];
3589
3590 /* already registered */
3591 if (con_driver->con == csw)
Dave Airliec55c63c2011-01-07 09:57:41 +10003592 retval = -EBUSY;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003593 }
3594
3595 if (retval)
3596 goto err;
3597
3598 desc = csw->con_startup();
3599
3600 if (!desc)
3601 goto err;
3602
3603 retval = -EINVAL;
3604
3605 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3606 con_driver = &registered_con_driver[i];
3607
3608 if (con_driver->con == NULL) {
3609 con_driver->con = csw;
3610 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003611 con_driver->node = i;
3612 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003613 CON_DRIVER_FLAG_INIT;
3614 con_driver->first = first;
3615 con_driver->last = last;
3616 retval = 0;
3617 break;
3618 }
3619 }
3620
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003621 if (retval)
3622 goto err;
3623
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003624 con_driver->dev =
3625 device_create_with_groups(vtconsole_class, NULL,
3626 MKDEV(0, con_driver->node),
3627 con_driver, con_dev_groups,
3628 "vtcon%i", con_driver->node);
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003629 if (IS_ERR(con_driver->dev)) {
3630 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003631 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003632 PTR_ERR(con_driver->dev));
3633 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003634 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003635 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003636 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003637
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003638err:
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003639 module_put(owner);
3640 return retval;
3641}
Alan Cox50e244c2013-01-25 10:28:15 +10003642
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003643
3644/**
Wang YanQing50539dd2013-05-09 02:14:44 +08003645 * do_unregister_con_driver - unregister console driver from console layer
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003646 * @csw: console driver
3647 *
3648 * DESCRIPTION: All drivers that registers to the console layer must
3649 * call this function upon exit, or if the console driver is in a state
3650 * where it won't be able to handle console services, such as the
3651 * framebuffer console without loaded framebuffer drivers.
3652 *
3653 * The driver must unbind first prior to unregistration.
3654 */
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003655int do_unregister_con_driver(const struct consw *csw)
3656{
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003657 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003658
3659 /* cannot unregister a bound driver */
3660 if (con_is_bound(csw))
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003661 return -EBUSY;
3662
3663 if (csw == conswitchp)
3664 return -EINVAL;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003665
3666 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3667 struct con_driver *con_driver = &registered_con_driver[i];
3668
Imre Deak2cf30f72014-12-16 00:15:59 +02003669 if (con_driver->con == csw) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003670 vtconsole_deinit_device(con_driver);
3671 device_destroy(vtconsole_class,
3672 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003673 con_driver->con = NULL;
3674 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003675 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003676 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003677 con_driver->flag = 0;
3678 con_driver->first = 0;
3679 con_driver->last = 0;
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003680 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003681 }
3682 }
Daniel Vetterd9c660e2014-06-05 16:29:56 +02003683
3684 return -ENODEV;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003685}
Takashi Iwaie93a9a82013-01-25 10:28:18 +10003686EXPORT_SYMBOL_GPL(do_unregister_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003687
3688/*
3689 * If we support more console drivers, this function is used
3690 * when a driver wants to take over some existing consoles
3691 * and become default driver for newly opened ones.
3692 *
Wang YanQing155957f2013-05-21 13:15:12 +08003693 * do_take_over_console is basically a register followed by unbind
Alan Cox50e244c2013-01-25 10:28:15 +10003694 */
3695int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3696{
3697 int err;
3698
3699 err = do_register_con_driver(csw, first, last);
3700 /*
3701 * If we get an busy error we still want to bind the console driver
3702 * and return success, as we may have unbound the console driver
3703 * but not unregistered it.
3704 */
3705 if (err == -EBUSY)
3706 err = 0;
3707 if (!err)
3708 do_bind_con_driver(csw, first, last, deflt);
3709
3710 return err;
3711}
3712EXPORT_SYMBOL_GPL(do_take_over_console);
3713
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003714
3715/*
3716 * give_up_console is a wrapper to unregister_con_driver. It will only
3717 * work if driver is fully unbound.
3718 */
3719void give_up_console(const struct consw *csw)
3720{
Wang YanQing70125e72013-05-09 02:14:39 +08003721 console_lock();
3722 do_unregister_con_driver(csw);
3723 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003724}
3725
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003726static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003727{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003728 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003729
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003730 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3731 if (IS_ERR(vtconsole_class)) {
3732 printk(KERN_WARNING "Unable to create vt console class; "
3733 "errno = %ld\n", PTR_ERR(vtconsole_class));
3734 vtconsole_class = NULL;
3735 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003736
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003737 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003738 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3739 struct con_driver *con = &registered_con_driver[i];
3740
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003741 if (con->con && !con->dev) {
Takashi Iwai1083a7b2015-02-05 11:07:42 +01003742 con->dev =
3743 device_create_with_groups(vtconsole_class, NULL,
3744 MKDEV(0, con->node),
3745 con, con_dev_groups,
3746 "vtcon%i", con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003747
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003748 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003749 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003750 "device for %s; errno = %ld\n",
3751 con->desc, PTR_ERR(con->dev));
3752 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003753 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003754 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003755 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003756 }
3757 }
3758
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003759 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003760}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003761postcore_initcall(vtconsole_class_init);
3762
3763#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003764
3765/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 * Screen blanking
3767 */
3768
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003769static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003771 unsigned int mode;
3772
3773 if (get_user(mode, p + 1))
3774 return -EFAULT;
3775
3776 vesa_blank_mode = (mode < 4) ? mode : 0;
3777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778}
3779
Linus Torvalds1da177e2005-04-16 15:20:36 -07003780void do_blank_screen(int entering_gfx)
3781{
3782 struct vc_data *vc = vc_cons[fg_console].d;
3783 int i;
3784
3785 WARN_CONSOLE_UNLOCKED();
3786
3787 if (console_blanked) {
3788 if (blank_state == blank_vesa_wait) {
3789 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003790 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 }
3792 return;
3793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794
3795 /* entering graphics mode? */
3796 if (entering_gfx) {
3797 hide_cursor(vc);
3798 save_screen(vc);
3799 vc->vc_sw->con_blank(vc, -1, 1);
3800 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003801 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 set_origin(vc);
3803 return;
3804 }
3805
izumib6e8f002007-07-17 04:05:49 -07003806 if (blank_state != blank_normal_wait)
3807 return;
3808 blank_state = blank_off;
3809
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 /* don't blank graphics */
3811 if (vc->vc_mode != KD_TEXT) {
3812 console_blanked = fg_console + 1;
3813 return;
3814 }
3815
3816 hide_cursor(vc);
3817 del_timer_sync(&console_timer);
3818 blank_timer_expired = 0;
3819
3820 save_screen(vc);
3821 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003822 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823 console_blanked = fg_console + 1;
3824 if (i)
3825 set_origin(vc);
3826
3827 if (console_blank_hook && console_blank_hook(1))
3828 return;
3829
Ville Syrjalad060a322006-01-09 20:53:49 -08003830 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003831 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 mod_timer(&console_timer, jiffies + vesa_off_interval);
3833 }
Alan Cox8b92e872009-09-19 13:13:24 -07003834 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835}
3836EXPORT_SYMBOL(do_blank_screen);
3837
3838/*
3839 * Called by timer as well as from vt_console_driver
3840 */
3841void do_unblank_screen(int leaving_gfx)
3842{
3843 struct vc_data *vc;
3844
3845 /* This should now always be called from a "sane" (read: can schedule)
3846 * context for the sake of the low level drivers, except in the special
3847 * case of oops_in_progress
3848 */
3849 if (!oops_in_progress)
3850 might_sleep();
3851
3852 WARN_CONSOLE_UNLOCKED();
3853
3854 ignore_poke = 0;
3855 if (!console_blanked)
3856 return;
3857 if (!vc_cons_allocated(fg_console)) {
3858 /* impossible */
Joe Perchese620e542014-11-09 22:46:35 -08003859 pr_warn("unblank_screen: tty %d not allocated ??\n",
3860 fg_console + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861 return;
3862 }
3863 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003864 /* Try to unblank in oops case too */
3865 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 return; /* but leave console_blanked != 0 */
3867
3868 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003869 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 blank_state = blank_normal_wait;
3871 }
3872
3873 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003874 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 /* Low-level driver cannot restore -> do it ourselves */
3876 update_screen(vc);
3877 if (console_blank_hook)
3878 console_blank_hook(0);
3879 set_palette(vc);
3880 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003881 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882}
3883EXPORT_SYMBOL(do_unblank_screen);
3884
3885/*
3886 * This is called by the outside world to cause a forced unblank, mostly for
3887 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3888 * call it with 1 as an argument and so force a mode restore... that may kill
3889 * X or at least garbage the screen but would also make the Oops visible...
3890 */
3891void unblank_screen(void)
3892{
3893 do_unblank_screen(0);
3894}
3895
3896/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003897 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003899 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 */
3901static void blank_screen_t(unsigned long dummy)
3902{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003903 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003904 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003905 return;
3906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 blank_timer_expired = 1;
3908 schedule_work(&console_work);
3909}
3910
3911void poke_blanked_console(void)
3912{
3913 WARN_CONSOLE_UNLOCKED();
3914
3915 /* Add this so we quickly catch whoever might call us in a non
3916 * safe context. Nowadays, unblank_screen() isn't to be called in
3917 * atomic contexts and is allowed to schedule (with the special case
3918 * of oops_in_progress, but that isn't of any concern for this
3919 * function. --BenH.
3920 */
3921 might_sleep();
3922
3923 /* This isn't perfectly race free, but a race here would be mostly harmless,
3924 * at worse, we'll do a spurrious blank and it's unlikely
3925 */
3926 del_timer(&console_timer);
3927 blank_timer_expired = 0;
3928
3929 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3930 return;
3931 if (console_blanked)
3932 unblank_screen();
3933 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003934 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 blank_state = blank_normal_wait;
3936 }
3937}
3938
3939/*
3940 * Palettes
3941 */
3942
3943static void set_palette(struct vc_data *vc)
3944{
3945 WARN_CONSOLE_UNLOCKED();
3946
3947 if (vc->vc_mode != KD_GRAPHICS)
3948 vc->vc_sw->con_set_palette(vc, color_table);
3949}
3950
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951/*
3952 * Load palette into the DAC registers. arg points to a colour
3953 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3954 */
3955
3956int con_set_cmap(unsigned char __user *arg)
3957{
Michael Gehring871bdea2012-03-21 01:26:45 +01003958 int i, j, k;
3959 unsigned char colormap[3*16];
3960
3961 if (copy_from_user(colormap, arg, sizeof(colormap)))
3962 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963
Torben Hohnac751ef2011-01-25 15:07:35 -08003964 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01003965 for (i = k = 0; i < 16; i++) {
3966 default_red[i] = colormap[k++];
3967 default_grn[i] = colormap[k++];
3968 default_blu[i] = colormap[k++];
3969 }
3970 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3971 if (!vc_cons_allocated(i))
3972 continue;
3973 for (j = k = 0; j < 16; j++) {
3974 vc_cons[i].d->vc_palette[k++] = default_red[j];
3975 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3976 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3977 }
3978 set_palette(vc_cons[i].d);
3979 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003980 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981
Michael Gehring871bdea2012-03-21 01:26:45 +01003982 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983}
3984
3985int con_get_cmap(unsigned char __user *arg)
3986{
Michael Gehring871bdea2012-03-21 01:26:45 +01003987 int i, k;
3988 unsigned char colormap[3*16];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Torben Hohnac751ef2011-01-25 15:07:35 -08003990 console_lock();
Michael Gehring871bdea2012-03-21 01:26:45 +01003991 for (i = k = 0; i < 16; i++) {
3992 colormap[k++] = default_red[i];
3993 colormap[k++] = default_grn[i];
3994 colormap[k++] = default_blu[i];
3995 }
Torben Hohnac751ef2011-01-25 15:07:35 -08003996 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997
Michael Gehring871bdea2012-03-21 01:26:45 +01003998 if (copy_to_user(arg, colormap, sizeof(colormap)))
3999 return -EFAULT;
4000
4001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
4004void reset_palette(struct vc_data *vc)
4005{
4006 int j, k;
4007 for (j=k=0; j<16; j++) {
4008 vc->vc_palette[k++] = default_red[j];
4009 vc->vc_palette[k++] = default_grn[j];
4010 vc->vc_palette[k++] = default_blu[j];
4011 }
4012 set_palette(vc);
4013}
4014
4015/*
4016 * Font switching
4017 *
4018 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4019 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4020 * depending on width) reserved for each character which is kinda wasty, but
4021 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004022 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 * format (maybe we should add a `fontoffset' field to the `display'
4024 * structure so we won't have to convert the fontdata all the time.
4025 * /Jes
4026 */
4027
4028#define max_font_size 65536
4029
4030static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4031{
4032 struct console_font font;
4033 int rc = -EINVAL;
4034 int c;
4035
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 if (op->data) {
4037 font.data = kmalloc(max_font_size, GFP_KERNEL);
4038 if (!font.data)
4039 return -ENOMEM;
4040 } else
4041 font.data = NULL;
4042
Torben Hohnac751ef2011-01-25 15:07:35 -08004043 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004044 if (vc->vc_mode != KD_TEXT)
4045 rc = -EINVAL;
4046 else if (vc->vc_sw->con_font_get)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 rc = vc->vc_sw->con_font_get(vc, &font);
4048 else
4049 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004050 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
4052 if (rc)
4053 goto out;
4054
4055 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07004056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 if (op->data && font.charcount > op->charcount)
4058 rc = -ENOSPC;
4059 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4060 if (font.width > op->width || font.height > op->height)
4061 rc = -ENOSPC;
4062 } else {
4063 if (font.width != 8)
4064 rc = -EIO;
4065 else if ((op->height && font.height > op->height) ||
4066 font.height > 32)
4067 rc = -ENOSPC;
4068 }
4069 if (rc)
4070 goto out;
4071
4072 op->height = font.height;
4073 op->width = font.width;
4074 op->charcount = font.charcount;
4075
4076 if (op->data && copy_to_user(op->data, font.data, c))
4077 rc = -EFAULT;
4078
4079out:
4080 kfree(font.data);
4081 return rc;
4082}
4083
4084static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4085{
4086 struct console_font font;
4087 int rc = -EINVAL;
4088 int size;
4089
4090 if (vc->vc_mode != KD_TEXT)
4091 return -EINVAL;
4092 if (!op->data)
4093 return -EINVAL;
4094 if (op->charcount > 512)
4095 return -EINVAL;
4096 if (!op->height) { /* Need to guess font height [compat] */
4097 int h, i;
4098 u8 __user *charmap = op->data;
4099 u8 tmp;
4100
4101 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4102 so that we can get rid of this soon */
4103 if (!(op->flags & KD_FONT_FLAG_OLD))
4104 return -EINVAL;
4105 for (h = 32; h > 0; h--)
4106 for (i = 0; i < op->charcount; i++) {
4107 if (get_user(tmp, &charmap[32*i+h-1]))
4108 return -EFAULT;
4109 if (tmp)
4110 goto nonzero;
4111 }
4112 return -EINVAL;
4113 nonzero:
4114 op->height = h;
4115 }
4116 if (op->width <= 0 || op->width > 32 || op->height > 32)
4117 return -EINVAL;
4118 size = (op->width+7)/8 * 32 * op->charcount;
4119 if (size > max_font_size)
4120 return -ENOSPC;
4121 font.charcount = op->charcount;
4122 font.height = op->height;
4123 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004124 font.data = memdup_user(op->data, size);
4125 if (IS_ERR(font.data))
4126 return PTR_ERR(font.data);
Torben Hohnac751ef2011-01-25 15:07:35 -08004127 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004128 if (vc->vc_mode != KD_TEXT)
4129 rc = -EINVAL;
4130 else if (vc->vc_sw->con_font_set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4132 else
4133 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004134 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 kfree(font.data);
4136 return rc;
4137}
4138
4139static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4140{
4141 struct console_font font = {.width = op->width, .height = op->height};
4142 char name[MAX_FONT_NAME];
4143 char *s = name;
4144 int rc;
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146
4147 if (!op->data)
4148 s = NULL;
4149 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4150 return -EFAULT;
4151 else
4152 name[MAX_FONT_NAME - 1] = 0;
4153
Torben Hohnac751ef2011-01-25 15:07:35 -08004154 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004155 if (vc->vc_mode != KD_TEXT) {
4156 console_unlock();
4157 return -EINVAL;
4158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 if (vc->vc_sw->con_font_default)
4160 rc = vc->vc_sw->con_font_default(vc, &font, s);
4161 else
4162 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004163 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 if (!rc) {
4165 op->width = font.width;
4166 op->height = font.height;
4167 }
4168 return rc;
4169}
4170
4171static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4172{
4173 int con = op->height;
4174 int rc;
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Torben Hohnac751ef2011-01-25 15:07:35 -08004177 console_lock();
Alan Coxedab5582012-03-02 14:59:08 +00004178 if (vc->vc_mode != KD_TEXT)
4179 rc = -EINVAL;
4180 else if (!vc->vc_sw->con_font_copy)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 rc = -ENOSYS;
4182 else if (con < 0 || !vc_cons_allocated(con))
4183 rc = -ENOTTY;
4184 else if (con == vc->vc_num) /* nothing to do */
4185 rc = 0;
4186 else
4187 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004188 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 return rc;
4190}
4191
4192int con_font_op(struct vc_data *vc, struct console_font_op *op)
4193{
4194 switch (op->op) {
4195 case KD_FONT_OP_SET:
4196 return con_font_set(vc, op);
4197 case KD_FONT_OP_GET:
4198 return con_font_get(vc, op);
4199 case KD_FONT_OP_SET_DEFAULT:
4200 return con_font_default(vc, op);
4201 case KD_FONT_OP_COPY:
4202 return con_font_copy(vc, op);
4203 }
4204 return -ENOSYS;
4205}
4206
4207/*
4208 * Interface exported to selection and vcs.
4209 */
4210
4211/* used by selection */
4212u16 screen_glyph(struct vc_data *vc, int offset)
4213{
4214 u16 w = scr_readw(screenpos(vc, offset, 1));
4215 u16 c = w & 0xff;
4216
4217 if (w & vc->vc_hi_font_mask)
4218 c |= 0x100;
4219 return c;
4220}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004221EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
4223/* used by vcs - note the word offset */
4224unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4225{
4226 return screenpos(vc, 2 * w_offset, viewed);
4227}
4228
4229void getconsxy(struct vc_data *vc, unsigned char *p)
4230{
4231 p[0] = vc->vc_x;
4232 p[1] = vc->vc_y;
4233}
4234
4235void putconsxy(struct vc_data *vc, unsigned char *p)
4236{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004237 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 gotoxy(vc, p[0], p[1]);
4239 set_cursor(vc);
4240}
4241
4242u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4243{
4244 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4245 return softcursor_original;
4246 return scr_readw(org);
4247}
4248
4249void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4250{
4251 scr_writew(val, org);
4252 if ((unsigned long)org == vc->vc_pos) {
4253 softcursor_original = -1;
4254 add_softcursor(vc);
4255 }
4256}
4257
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004258void vcs_scr_updated(struct vc_data *vc)
4259{
4260 notify_update(vc);
4261}
4262
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263/*
4264 * Visible symbols for modules
4265 */
4266
4267EXPORT_SYMBOL(color_table);
4268EXPORT_SYMBOL(default_red);
4269EXPORT_SYMBOL(default_grn);
4270EXPORT_SYMBOL(default_blu);
4271EXPORT_SYMBOL(update_region);
4272EXPORT_SYMBOL(redraw_screen);
4273EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274EXPORT_SYMBOL(fg_console);
4275EXPORT_SYMBOL(console_blank_hook);
4276EXPORT_SYMBOL(console_blanked);
4277EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004278EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279#ifndef VT_SINGLE_DRIVER
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280EXPORT_SYMBOL(give_up_console);
4281#endif