blob: f287dca0d9d9d03b1f96b8e67424f5346510d1dd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vt.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Hopefully this will be a rather complete VT102 implementation.
9 *
10 * Beeping thanks to John T Kohl.
11 *
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
14 *
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
17 *
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
20 *
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
23 *
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
26 *
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
29 *
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
32 *
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
34 *
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <geert@linux-m68k.org>, Jan 1997.
37 *
38 * Original m68k console driver modifications by
39 *
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
42 *
43 * The abstract console driver provides a generic interface for a text
44 * console. It supports VGA text mode, frame buffer based graphical consoles
45 * and special graphics processors that are only accessible through some
46 * registers (e.g. a TMS340x0 GSP).
47 *
48 * The interface to the hardware is specified using a special structure
49 * (struct consw) which contains function pointers to console operations
50 * (see <linux/console.h> for more information).
51 *
52 * Support for changeable cursor shape
53 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
54 *
55 * Ported to i386 and con_scrolldelta fixed
56 * by Emmanuel Marty <core@ggi-project.org>, April 1998
57 *
58 * Resurrected character buffers in videoram plus lots of other trickery
59 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
60 *
61 * Removed old-style timers, introduced console_timer, made timer
Francois Camie1f8e872008-10-15 22:01:59 -070062 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * Removed console_lock, enabled interrupts across all console operations
65 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070066 *
67 * Fixed UTF-8 mode so alternate charset modes always work according
68 * to control sequences interpreted in do_con_trol function
69 * preserving backward VT100 semigraphics compatibility,
70 * malformed UTF sequences represented as sequences of replacement glyphs,
71 * original codes or '?' as a last resort if replacement glyph is undefined
72 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
75#include <linux/module.h>
76#include <linux/types.h>
77#include <linux/sched.h>
78#include <linux/tty.h>
79#include <linux/tty_flip.h>
80#include <linux/kernel.h>
81#include <linux/string.h>
82#include <linux/errno.h>
83#include <linux/kd.h>
84#include <linux/slab.h>
85#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070089#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/vt_kern.h>
91#include <linux/selection.h>
92#include <linux/tiocl.h>
93#include <linux/kbd_kern.h>
94#include <linux/consolemap.h>
95#include <linux/timer.h>
96#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070097#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/pm.h>
99#include <linux/font.h>
100#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700101#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100102#include <linux/device.h>
103#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104#include <asm/system.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100105#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500106#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300107#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700109#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700111#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700112#define CON_DRIVER_FLAG_INIT 2
113#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700114
115struct con_driver {
116 const struct consw *con;
117 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700118 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700119 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700120 int first;
121 int last;
122 int flag;
123};
124
125static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126const struct consw *conswitchp;
127
128/* A bitmap for codes <32. A bit of 1 indicates that the code
129 * corresponding to that bit number invokes some special action
130 * (such as cursor movement) and should not be displayed as a
131 * glyph unless the disp_ctrl mode is explicitly enabled.
132 */
133#define CTRL_ACTION 0x0d00ff81
134#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
135
136/*
137 * Here is the default bell parameters: 750HZ, 1/8th of a second
138 */
139#define DEFAULT_BELL_PITCH 750
140#define DEFAULT_BELL_DURATION (HZ/8)
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142struct vc vc_cons [MAX_NR_CONSOLES];
143
144#ifndef VT_SINGLE_DRIVER
145static const struct consw *con_driver_map[MAX_NR_CONSOLES];
146#endif
147
148static int con_open(struct tty_struct *, struct file *);
149static void vc_init(struct vc_data *vc, unsigned int rows,
150 unsigned int cols, int do_clear);
151static void gotoxy(struct vc_data *vc, int new_x, int new_y);
152static void save_cur(struct vc_data *vc);
153static void reset_terminal(struct vc_data *vc, int do_clear);
154static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800155static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156static void set_cursor(struct vc_data *vc);
157static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000158static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159static void blank_screen_t(unsigned long dummy);
160static void set_palette(struct vc_data *vc);
161
162static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700163int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700164module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500165int global_cursor_default = -1;
166module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800168static int cur_default = CUR_DEFAULT;
169module_param(cur_default, int, S_IRUGO | S_IWUSR);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171/*
172 * ignore_poke: don't unblank the screen when things are typed. This is
173 * mainly for the privacy of braille terminal users.
174 */
175static int ignore_poke;
176
177int do_poke_blanked_console;
178int console_blanked;
179
180static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181static int vesa_off_interval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700182static int blankinterval = 10*60;
183core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
David Howells65f27f32006-11-22 14:55:48 +0000185static DECLARE_WORK(console_work, console_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187/*
188 * fg_console is the current virtual console,
189 * last_console is the last used one,
190 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500191 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193int fg_console;
194int last_console;
195int want_console = -1;
Jason Wesselfed891c2010-08-16 15:58:30 -0500196static int saved_fg_console;
197static int saved_last_console;
198static int saved_want_console;
199static int saved_vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -0500200static int saved_console_blanked;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * For each existing display, we have a pointer to console currently visible
204 * on that display, allowing consoles other than fg_console to be refreshed
205 * appropriately. Unless the low-level driver supplies its own display_fg
206 * variable, we use this one for the "master display".
207 */
208static struct vc_data *master_display_fg;
209
210/*
211 * Unfortunately, we need to delay tty echo when we're currently writing to the
212 * console since the code is (and always was) not re-entrant, so we schedule
213 * all flip requests to process context with schedule-task() and run it from
214 * console_callback().
215 */
216
217/*
218 * For the same reason, we defer scrollback to the console callback.
219 */
220static int scrollback_delta;
221
222/*
223 * Hook so that the power management routines can (un)blank
224 * the console on our behalf.
225 */
226int (*console_blank_hook)(int);
227
Jiri Slaby40565f12007-02-12 00:52:31 -0800228static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int blank_state;
230static int blank_timer_expired;
231enum {
232 blank_off = 0,
233 blank_normal_wait,
234 blank_vesa_wait,
235};
236
237/*
Kay Sieversfbc92a32010-12-01 18:51:05 +0100238 * /sys/class/tty/tty0/
239 *
240 * the attribute 'active' contains the name of the current vc
241 * console and it supports poll() to detect vc switches
242 */
243static struct device *tty0dev;
244
245/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700246 * Notifier list for console events.
247 */
248static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
249
250int register_vt_notifier(struct notifier_block *nb)
251{
252 return atomic_notifier_chain_register(&vt_notifier_list, nb);
253}
254EXPORT_SYMBOL_GPL(register_vt_notifier);
255
256int unregister_vt_notifier(struct notifier_block *nb)
257{
258 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
259}
260EXPORT_SYMBOL_GPL(unregister_vt_notifier);
261
262static void notify_write(struct vc_data *vc, unsigned int unicode)
263{
264 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
265 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
266}
267
268static void notify_update(struct vc_data *vc)
269{
270 struct vt_notifier_param param = { .vc = vc };
271 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
272}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700273/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 * Low-Level Functions
275 */
276
277#define IS_FG(vc) ((vc)->vc_num == fg_console)
278
279#ifdef VT_BUF_VRAM_ONLY
280#define DO_UPDATE(vc) 0
281#else
Stefano Stabellinif700d6e2008-07-23 21:29:59 -0700282#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283#endif
284
285static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
286{
287 unsigned short *p;
288
289 if (!viewed)
290 p = (unsigned short *)(vc->vc_origin + offset);
291 else if (!vc->vc_sw->con_screen_pos)
292 p = (unsigned short *)(vc->vc_visible_origin + offset);
293 else
294 p = vc->vc_sw->con_screen_pos(vc, offset);
295 return p;
296}
297
Alan Coxe33ac1c2010-06-01 22:52:54 +0200298/* Called from the keyboard irq path.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299static inline void scrolldelta(int lines)
300{
Alan Coxe33ac1c2010-06-01 22:52:54 +0200301 /* FIXME */
302 /* scrolldelta needs some kind of consistency lock, but the BKL was
303 and still is not protecting versus the scheduled back end */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 scrollback_delta += lines;
305 schedule_console_callback();
306}
307
308void schedule_console_callback(void)
309{
310 schedule_work(&console_work);
311}
312
313static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
314{
315 unsigned short *d, *s;
316
317 if (t+nr >= b)
318 nr = b - t - 1;
319 if (b > vc->vc_rows || t >= b || nr < 1)
320 return;
321 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
322 return;
323 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
324 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
325 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds386f40c2010-06-06 20:44:04 -0700326 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 vc->vc_size_row * nr);
328}
329
330static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
331{
332 unsigned short *s;
333 unsigned int step;
334
335 if (t+nr >= b)
336 nr = b - t - 1;
337 if (b > vc->vc_rows || t >= b || nr < 1)
338 return;
339 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
340 return;
341 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
342 step = vc->vc_cols * nr;
343 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds93f78da2008-10-14 12:12:02 -0700344 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
347static void do_update_region(struct vc_data *vc, unsigned long start, int count)
348{
349#ifndef VT_BUF_VRAM_ONLY
350 unsigned int xx, yy, offset;
351 u16 *p;
352
353 p = (u16 *) start;
354 if (!vc->vc_sw->con_getxy) {
355 offset = (start - vc->vc_origin) / 2;
356 xx = offset % vc->vc_cols;
357 yy = offset / vc->vc_cols;
358 } else {
359 int nxx, nyy;
360 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
361 xx = nxx; yy = nyy;
362 }
363 for(;;) {
364 u16 attrib = scr_readw(p) & 0xff00;
365 int startx = xx;
366 u16 *q = p;
367 while (xx < vc->vc_cols && count) {
368 if (attrib != (scr_readw(p) & 0xff00)) {
369 if (p > q)
370 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
371 startx = xx;
372 q = p;
373 attrib = scr_readw(p) & 0xff00;
374 }
375 p++;
376 xx++;
377 count--;
378 }
379 if (p > q)
380 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
381 if (!count)
382 break;
383 xx = 0;
384 yy++;
385 if (vc->vc_sw->con_getxy) {
386 p = (u16 *)start;
387 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
388 }
389 }
390#endif
391}
392
393void update_region(struct vc_data *vc, unsigned long start, int count)
394{
395 WARN_CONSOLE_UNLOCKED();
396
397 if (DO_UPDATE(vc)) {
398 hide_cursor(vc);
399 do_update_region(vc, start, count);
400 set_cursor(vc);
401 }
402}
403
404/* Structure of attributes is hardware-dependent */
405
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700406static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
407 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
409 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700410 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
411 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413#ifndef VT_BUF_VRAM_ONLY
414/*
415 * ++roman: I completely changed the attribute format for monochrome
416 * mode (!can_do_color). The formerly used MDA (monochrome display
417 * adapter) format didn't allow the combination of certain effects.
418 * Now the attribute is just a bit vector:
419 * Bit 0..1: intensity (0..2)
420 * Bit 2 : underline
421 * Bit 3 : reverse
422 * Bit 7 : blink
423 */
424 {
Jan 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);
504}
505
506/* used by selection: complement pointer position */
507void complement_pos(struct vc_data *vc, int offset)
508{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700509 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 static unsigned short old;
511 static unsigned short oldx, oldy;
512
513 WARN_CONSOLE_UNLOCKED();
514
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700515 if (old_offset != -1 && old_offset >= 0 &&
516 old_offset < vc->vc_screenbuf_size) {
517 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 if (DO_UPDATE(vc))
519 vc->vc_sw->con_putc(vc, old, oldy, oldx);
520 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700521
522 old_offset = offset;
523
524 if (offset != -1 && offset >= 0 &&
525 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700527 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 p = screenpos(vc, offset, 1);
529 old = scr_readw(p);
530 new = old ^ vc->vc_complement_mask;
531 scr_writew(new, p);
532 if (DO_UPDATE(vc)) {
533 oldx = (offset >> 1) % vc->vc_cols;
534 oldy = (offset >> 1) / vc->vc_cols;
535 vc->vc_sw->con_putc(vc, new, oldy, oldx);
536 }
537 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static void insert_char(struct vc_data *vc, unsigned int nr)
542{
543 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
544
545 p = q + vc->vc_cols - nr - vc->vc_x;
546 while (--p >= q)
547 scr_writew(scr_readw(p), p + nr);
548 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
549 vc->vc_need_wrap = 0;
550 if (DO_UPDATE(vc)) {
551 unsigned short oldattr = vc->vc_attr;
552 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
553 vc->vc_cols - vc->vc_x - nr);
554 vc->vc_attr = vc->vc_video_erase_char >> 8;
555 while (nr--)
556 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
557 vc->vc_attr = oldattr;
558 }
559}
560
561static void delete_char(struct vc_data *vc, unsigned int nr)
562{
563 unsigned int i = vc->vc_x;
564 unsigned short *p = (unsigned short *)vc->vc_pos;
565
566 while (++i <= vc->vc_cols - nr) {
567 scr_writew(scr_readw(p+nr), p);
568 p++;
569 }
570 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
571 vc->vc_need_wrap = 0;
572 if (DO_UPDATE(vc)) {
573 unsigned short oldattr = vc->vc_attr;
574 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
575 vc->vc_cols - vc->vc_x - nr);
576 vc->vc_attr = vc->vc_video_erase_char >> 8;
577 while (nr--)
578 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
579 vc->vc_cols - 1 - nr);
580 vc->vc_attr = oldattr;
581 }
582}
583
584static int softcursor_original;
585
586static void add_softcursor(struct vc_data *vc)
587{
588 int i = scr_readw((u16 *) vc->vc_pos);
589 u32 type = vc->vc_cursor_type;
590
591 if (! (type & 0x10)) return;
592 if (softcursor_original != -1) return;
593 softcursor_original = i;
594 i |= ((type >> 8) & 0xff00 );
595 i ^= ((type) & 0xff00 );
596 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
597 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
598 scr_writew(i, (u16 *) vc->vc_pos);
599 if (DO_UPDATE(vc))
600 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
601}
602
603static void hide_softcursor(struct vc_data *vc)
604{
605 if (softcursor_original != -1) {
606 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
607 if (DO_UPDATE(vc))
608 vc->vc_sw->con_putc(vc, softcursor_original,
609 vc->vc_y, vc->vc_x);
610 softcursor_original = -1;
611 }
612}
613
614static void hide_cursor(struct vc_data *vc)
615{
616 if (vc == sel_cons)
617 clear_selection();
618 vc->vc_sw->con_cursor(vc, CM_ERASE);
619 hide_softcursor(vc);
620}
621
622static void set_cursor(struct vc_data *vc)
623{
624 if (!IS_FG(vc) || console_blanked ||
625 vc->vc_mode == KD_GRAPHICS)
626 return;
627 if (vc->vc_deccm) {
628 if (vc == sel_cons)
629 clear_selection();
630 add_softcursor(vc);
631 if ((vc->vc_cursor_type & 0x0f) != 1)
632 vc->vc_sw->con_cursor(vc, CM_DRAW);
633 } else
634 hide_cursor(vc);
635}
636
637static void set_origin(struct vc_data *vc)
638{
639 WARN_CONSOLE_UNLOCKED();
640
641 if (!CON_IS_VISIBLE(vc) ||
642 !vc->vc_sw->con_set_origin ||
643 !vc->vc_sw->con_set_origin(vc))
644 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
645 vc->vc_visible_origin = vc->vc_origin;
646 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
647 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
648}
649
650static inline void save_screen(struct vc_data *vc)
651{
652 WARN_CONSOLE_UNLOCKED();
653
654 if (vc->vc_sw->con_save_screen)
655 vc->vc_sw->con_save_screen(vc);
656}
657
658/*
659 * Redrawing of screen
660 */
661
662static void clear_buffer_attributes(struct vc_data *vc)
663{
664 unsigned short *p = (unsigned short *)vc->vc_origin;
665 int count = vc->vc_screenbuf_size / 2;
666 int mask = vc->vc_hi_font_mask | 0xff;
667
668 for (; count > 0; count--, p++) {
669 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
670 }
671}
672
673void redraw_screen(struct vc_data *vc, int is_switch)
674{
675 int redraw = 0;
676
677 WARN_CONSOLE_UNLOCKED();
678
679 if (!vc) {
680 /* strange ... */
681 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
682 return;
683 }
684
685 if (is_switch) {
686 struct vc_data *old_vc = vc_cons[fg_console].d;
687 if (old_vc == vc)
688 return;
689 if (!CON_IS_VISIBLE(vc))
690 redraw = 1;
691 *vc->vc_display_fg = vc;
692 fg_console = vc->vc_num;
693 hide_cursor(old_vc);
694 if (!CON_IS_VISIBLE(old_vc)) {
695 save_screen(old_vc);
696 set_origin(old_vc);
697 }
Kay Sieversfbc92a32010-12-01 18:51:05 +0100698 if (tty0dev)
699 sysfs_notify(&tty0dev->kobj, NULL, "active");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 hide_cursor(vc);
702 redraw = 1;
703 }
704
705 if (redraw) {
706 int update;
707 int old_was_color = vc->vc_can_do_color;
708
709 set_origin(vc);
710 update = vc->vc_sw->con_switch(vc);
711 set_palette(vc);
712 /*
713 * If console changed from mono<->color, the best we can do
714 * is to clear the buffer attributes. As it currently stands,
715 * rebuilding new attributes from the old buffer is not doable
716 * without overly complex code.
717 */
718 if (old_was_color != vc->vc_can_do_color) {
719 update_attr(vc);
720 clear_buffer_attributes(vc);
721 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700722
723 /* Forcibly update if we're panicing */
724 if ((update && vc->vc_mode != KD_GRAPHICS) ||
725 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
727 }
728 set_cursor(vc);
729 if (is_switch) {
730 set_leds();
731 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800732 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
734}
735
736/*
737 * Allocation, freeing and resizing of VTs.
738 */
739
740int vc_cons_allocated(unsigned int i)
741{
742 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
743}
744
745static void visual_init(struct vc_data *vc, int num, int init)
746{
747 /* ++Geert: vc->vc_sw->con_init determines console size */
748 if (vc->vc_sw)
749 module_put(vc->vc_sw->owner);
750 vc->vc_sw = conswitchp;
751#ifndef VT_SINGLE_DRIVER
752 if (con_driver_map[num])
753 vc->vc_sw = con_driver_map[num];
754#endif
755 __module_get(vc->vc_sw->owner);
756 vc->vc_num = num;
757 vc->vc_display_fg = &master_display_fg;
758 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
759 vc->vc_uni_pagedir = 0;
760 vc->vc_hi_font_mask = 0;
761 vc->vc_complement_mask = 0;
762 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700763 vc->vc_panic_force_write = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 vc->vc_sw->con_init(vc, init);
765 if (!vc->vc_complement_mask)
766 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
767 vc->vc_s_complement_mask = vc->vc_complement_mask;
768 vc->vc_size_row = vc->vc_cols << 1;
769 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
770}
771
772int vc_allocate(unsigned int currcons) /* return 0 on success */
773{
774 WARN_CONSOLE_UNLOCKED();
775
776 if (currcons >= MAX_NR_CONSOLES)
777 return -ENXIO;
778 if (!vc_cons[currcons].d) {
779 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700780 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 /* prevent users from taking too much memory */
783 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
784 return -EPERM;
785
786 /* due to the granularity of kmalloc, we waste some memory here */
787 /* the alloc is done in two steps, to optimize the common situation
788 of a 25x80 console (structsize=216, screenbuf_size=4000) */
789 /* although the numbers above are not valid since long ago, the
790 point is still up-to-date and the comment still has its value
791 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700792 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (!vc)
794 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 vc_cons[currcons].d = vc;
Alan Coxff917ba2010-06-01 22:52:55 +0200796 tty_port_init(&vc->port);
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800797 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 visual_init(vc, currcons, 1);
799 if (!*vc->vc_uni_pagedir_loc)
800 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100801 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (!vc->vc_screenbuf) {
803 kfree(vc);
804 vc_cons[currcons].d = NULL;
805 return -ENOMEM;
806 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500807
808 /* If no drivers have overridden us and the user didn't pass a
809 boot option, default to displaying the cursor */
810 if (global_cursor_default == -1)
811 global_cursor_default = 1;
812
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100814 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700815 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
817 return 0;
818}
819
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700820static inline int resize_screen(struct vc_data *vc, int width, int height,
821 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 /* Resizes the resolution of the display adapater */
824 int err = 0;
825
826 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700827 err = vc->vc_sw->con_resize(vc, width, height, user);
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 return err;
830}
831
832/*
833 * Change # of rows and columns (0 means unchanged/the size of fg_console)
834 * [this is to be used together with some user program
835 * like resize that changes the hardware videomode]
836 */
837#define VC_RESIZE_MAXCOL (32767)
838#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100839
840/**
841 * vc_do_resize - resizing method for the tty
842 * @tty: tty being resized
843 * @real_tty: real tty (different to tty if a pty/tty pair)
844 * @vc: virtual console private data
845 * @cols: columns
846 * @lines: lines
847 *
848 * Resize a virtual console, clipping according to the actual constraints.
849 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800850 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100851 *
852 * Caller must hold the console semaphore. Takes the termios mutex and
853 * ctrl_lock of the tty IFF a tty is passed.
854 */
855
Alan Coxfc6f6232009-01-02 13:43:17 +0000856static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
857 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700860 unsigned long end;
Jiri Slabyf2ee4ae2011-03-23 09:49:57 +0100861 unsigned int old_rows, old_row_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700863 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 unsigned short *newscreen;
865
866 WARN_CONSOLE_UNLOCKED();
867
868 if (!vc)
869 return -ENXIO;
870
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700871 user = vc->vc_resize_user;
872 vc->vc_resize_user = 0;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
875 return -EINVAL;
876
877 new_cols = (cols ? cols : vc->vc_cols);
878 new_rows = (lines ? lines : vc->vc_rows);
879 new_row_size = new_cols << 1;
880 new_screen_size = new_row_size * new_rows;
881
882 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
883 return 0;
884
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800885 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!newscreen)
887 return -ENOMEM;
888
889 old_rows = vc->vc_rows;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 old_row_size = vc->vc_size_row;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700892 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (err) {
894 kfree(newscreen);
895 return err;
896 }
897
898 vc->vc_rows = new_rows;
899 vc->vc_cols = new_cols;
900 vc->vc_size_row = new_row_size;
901 vc->vc_screenbuf_size = new_screen_size;
902
903 rlth = min(old_row_size, new_row_size);
904 rrem = new_row_size - rlth;
905 old_origin = vc->vc_origin;
906 new_origin = (long) newscreen;
907 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700908
909 if (vc->vc_y > new_rows) {
910 if (old_rows - vc->vc_y < new_rows) {
911 /*
912 * Cursor near the bottom, copy contents from the
913 * bottom of buffer
914 */
915 old_origin += (old_rows - new_rows) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700916 } else {
917 /*
918 * Cursor is in no man's land, copy 1/2 screenful
919 * from the top and bottom of cursor position
920 */
921 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 }
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +0200923 }
924
925 end = old_origin + old_row_size * min(old_rows, new_rows);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 update_attr(vc);
928
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700929 while (old_origin < end) {
930 scr_memcpyw((unsigned short *) new_origin,
931 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700933 scr_memsetw((void *)(new_origin + rlth),
934 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 old_origin += old_row_size;
936 new_origin += new_row_size;
937 }
938 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700939 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
940 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100941 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 vc->vc_screenbuf_size = new_screen_size;
944 set_origin(vc);
945
946 /* do part of a reset_terminal() */
947 vc->vc_top = 0;
948 vc->vc_bottom = vc->vc_rows;
949 gotoxy(vc, vc->vc_x, vc->vc_y);
950 save_cur(vc);
951
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100952 if (tty) {
953 /* Rewrite the requested winsize data with the actual
954 resulting sizes */
955 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 memset(&ws, 0, sizeof(ws));
957 ws.ws_row = vc->vc_rows;
958 ws.ws_col = vc->vc_cols;
959 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000960 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
963 if (CON_IS_VISIBLE(vc))
964 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700965 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return err;
967}
968
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100969/**
970 * vc_resize - resize a VT
971 * @vc: virtual console
972 * @cols: columns
973 * @rows: rows
974 *
975 * Resize a virtual console as seen from the console end of things. We
976 * use the common vc_do_resize methods to update the structures. The
977 * caller must hold the console sem to protect console internals and
Alan Cox8ce73262010-06-01 22:52:56 +0200978 * vc->port.tty
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100979 */
980
981int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700982{
Alan Cox8ce73262010-06-01 22:52:56 +0200983 return vc_do_resize(vc->port.tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100984}
985
986/**
987 * vt_resize - resize a VT
988 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100989 * @ws: winsize attributes
990 *
991 * Resize a virtual terminal. This is called by the tty layer as we
992 * register our own handler for resizing. The mutual helper does all
993 * the actual work.
994 *
995 * Takes the console sem and the called methods then take the tty
996 * termios_mutex and the tty ctrl_lock in that order.
997 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800998static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100999{
1000 struct vc_data *vc = tty->driver_data;
1001 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
Torben Hohnac751ef2011-01-25 15:07:35 -08001003 console_lock();
Alan Coxfc6f6232009-01-02 13:43:17 +00001004 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Torben Hohnac751ef2011-01-25 15:07:35 -08001005 console_unlock();
Alan Cox8c9a9dd2008-08-15 10:39:38 +01001006 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001007}
1008
1009void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 WARN_CONSOLE_UNLOCKED();
1012
1013 if (vc_cons_allocated(currcons)) {
1014 struct vc_data *vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001015 struct vt_notifier_param param = { .vc = vc };
Kay Sievers4995f8e2009-03-09 14:18:52 +01001016
Samuel Thibaultb293d752007-10-18 23:39:17 -07001017 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001018 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001020 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001021 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001022 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (currcons >= MIN_NR_CONSOLES)
1024 kfree(vc);
1025 vc_cons[currcons].d = NULL;
1026 }
1027}
1028
1029/*
1030 * VT102 emulator
1031 */
1032
1033#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1034#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1035#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1036
1037#define decarm VC_REPEAT
1038#define decckm VC_CKMODE
1039#define kbdapplic VC_APPLIC
1040#define lnm VC_CRLF
1041
1042/*
1043 * this is what the terminal answers to a ESC-Z or csi0c query.
1044 */
1045#define VT100ID "\033[?1;2c"
1046#define VT102ID "\033[?6c"
1047
1048unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1049 8,12,10,14, 9,13,11,15 };
1050
1051/* the default colour table, for VGA+ colour systems */
1052int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1053 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1054int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1055 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1056int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1057 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1058
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001059module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1060module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1061module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063/*
1064 * gotoxy() must verify all boundaries, because the arguments
1065 * might also be negative. If the given position is out of
1066 * bounds, the cursor is placed at the nearest margin.
1067 */
1068static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1069{
1070 int min_y, max_y;
1071
1072 if (new_x < 0)
1073 vc->vc_x = 0;
1074 else {
1075 if (new_x >= vc->vc_cols)
1076 vc->vc_x = vc->vc_cols - 1;
1077 else
1078 vc->vc_x = new_x;
1079 }
1080
1081 if (vc->vc_decom) {
1082 min_y = vc->vc_top;
1083 max_y = vc->vc_bottom;
1084 } else {
1085 min_y = 0;
1086 max_y = vc->vc_rows;
1087 }
1088 if (new_y < min_y)
1089 vc->vc_y = min_y;
1090 else if (new_y >= max_y)
1091 vc->vc_y = max_y - 1;
1092 else
1093 vc->vc_y = new_y;
1094 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1095 vc->vc_need_wrap = 0;
1096}
1097
1098/* for absolute user moves, when decom is set */
1099static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1100{
1101 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1102}
1103
1104void scrollback(struct vc_data *vc, int lines)
1105{
1106 if (!lines)
1107 lines = vc->vc_rows / 2;
1108 scrolldelta(-lines);
1109}
1110
1111void scrollfront(struct vc_data *vc, int lines)
1112{
1113 if (!lines)
1114 lines = vc->vc_rows / 2;
1115 scrolldelta(lines);
1116}
1117
1118static void lf(struct vc_data *vc)
1119{
1120 /* don't scroll if above bottom of scrolling region, or
1121 * if below scrolling region
1122 */
1123 if (vc->vc_y + 1 == vc->vc_bottom)
1124 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1125 else if (vc->vc_y < vc->vc_rows - 1) {
1126 vc->vc_y++;
1127 vc->vc_pos += vc->vc_size_row;
1128 }
1129 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001130 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
1133static void ri(struct vc_data *vc)
1134{
1135 /* don't scroll if below top of scrolling region, or
1136 * if above scrolling region
1137 */
1138 if (vc->vc_y == vc->vc_top)
1139 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1140 else if (vc->vc_y > 0) {
1141 vc->vc_y--;
1142 vc->vc_pos -= vc->vc_size_row;
1143 }
1144 vc->vc_need_wrap = 0;
1145}
1146
1147static inline void cr(struct vc_data *vc)
1148{
1149 vc->vc_pos -= vc->vc_x << 1;
1150 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001151 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
1154static inline void bs(struct vc_data *vc)
1155{
1156 if (vc->vc_x) {
1157 vc->vc_pos -= 2;
1158 vc->vc_x--;
1159 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001160 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 }
1162}
1163
1164static inline void del(struct vc_data *vc)
1165{
1166 /* ignored */
1167}
1168
1169static void csi_J(struct vc_data *vc, int vpar)
1170{
1171 unsigned int count;
1172 unsigned short * start;
1173
1174 switch (vpar) {
1175 case 0: /* erase from cursor to end of display */
1176 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1177 start = (unsigned short *)vc->vc_pos;
1178 if (DO_UPDATE(vc)) {
1179 /* do in two stages */
1180 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1181 vc->vc_cols - vc->vc_x);
1182 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1183 vc->vc_rows - vc->vc_y - 1,
1184 vc->vc_cols);
1185 }
1186 break;
1187 case 1: /* erase from start to cursor */
1188 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1189 start = (unsigned short *)vc->vc_origin;
1190 if (DO_UPDATE(vc)) {
1191 /* do in two stages */
1192 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1193 vc->vc_cols);
1194 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1195 vc->vc_x + 1);
1196 }
1197 break;
1198 case 2: /* erase whole display */
1199 count = vc->vc_cols * vc->vc_rows;
1200 start = (unsigned short *)vc->vc_origin;
1201 if (DO_UPDATE(vc))
1202 vc->vc_sw->con_clear(vc, 0, 0,
1203 vc->vc_rows,
1204 vc->vc_cols);
1205 break;
1206 default:
1207 return;
1208 }
1209 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1210 vc->vc_need_wrap = 0;
1211}
1212
1213static void csi_K(struct vc_data *vc, int vpar)
1214{
1215 unsigned int count;
1216 unsigned short * start;
1217
1218 switch (vpar) {
1219 case 0: /* erase from cursor to end of line */
1220 count = vc->vc_cols - vc->vc_x;
1221 start = (unsigned short *)vc->vc_pos;
1222 if (DO_UPDATE(vc))
1223 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1224 vc->vc_cols - vc->vc_x);
1225 break;
1226 case 1: /* erase from start of line to cursor */
1227 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1228 count = vc->vc_x + 1;
1229 if (DO_UPDATE(vc))
1230 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1231 vc->vc_x + 1);
1232 break;
1233 case 2: /* erase whole line */
1234 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1235 count = vc->vc_cols;
1236 if (DO_UPDATE(vc))
1237 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1238 vc->vc_cols);
1239 break;
1240 default:
1241 return;
1242 }
1243 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1244 vc->vc_need_wrap = 0;
1245}
1246
1247static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1248{ /* not vt100? */
1249 int count;
1250
1251 if (!vpar)
1252 vpar++;
1253 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1254
1255 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1256 if (DO_UPDATE(vc))
1257 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1258 vc->vc_need_wrap = 0;
1259}
1260
1261static void default_attr(struct vc_data *vc)
1262{
1263 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001264 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 vc->vc_underline = 0;
1266 vc->vc_reverse = 0;
1267 vc->vc_blink = 0;
1268 vc->vc_color = vc->vc_def_color;
1269}
1270
Torben Hohnac751ef2011-01-25 15:07:35 -08001271/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272static void csi_m(struct vc_data *vc)
1273{
1274 int i;
1275
1276 for (i = 0; i <= vc->vc_npar; i++)
1277 switch (vc->vc_par[i]) {
1278 case 0: /* all attributes off */
1279 default_attr(vc);
1280 break;
1281 case 1:
1282 vc->vc_intensity = 2;
1283 break;
1284 case 2:
1285 vc->vc_intensity = 0;
1286 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001287 case 3:
1288 vc->vc_italic = 1;
1289 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 case 4:
1291 vc->vc_underline = 1;
1292 break;
1293 case 5:
1294 vc->vc_blink = 1;
1295 break;
1296 case 7:
1297 vc->vc_reverse = 1;
1298 break;
1299 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1300 * Select primary font, don't display
1301 * control chars if defined, don't set
1302 * bit 8 on output.
1303 */
1304 vc->vc_translate = set_translate(vc->vc_charset == 0
1305 ? vc->vc_G0_charset
1306 : vc->vc_G1_charset, vc);
1307 vc->vc_disp_ctrl = 0;
1308 vc->vc_toggle_meta = 0;
1309 break;
1310 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1311 * Select first alternate font, lets
1312 * chars < 32 be displayed as ROM chars.
1313 */
1314 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1315 vc->vc_disp_ctrl = 1;
1316 vc->vc_toggle_meta = 0;
1317 break;
1318 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1319 * Select second alternate font, toggle
1320 * high bit before displaying as ROM char.
1321 */
1322 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1323 vc->vc_disp_ctrl = 1;
1324 vc->vc_toggle_meta = 1;
1325 break;
1326 case 21:
1327 case 22:
1328 vc->vc_intensity = 1;
1329 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001330 case 23:
1331 vc->vc_italic = 0;
1332 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 case 24:
1334 vc->vc_underline = 0;
1335 break;
1336 case 25:
1337 vc->vc_blink = 0;
1338 break;
1339 case 27:
1340 vc->vc_reverse = 0;
1341 break;
1342 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1343 * Enables underscore, white foreground
1344 * with white underscore (Linux - use
1345 * default foreground).
1346 */
1347 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1348 vc->vc_underline = 1;
1349 break;
1350 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1351 * Disable underline option.
1352 * Reset colour to default? It did this
1353 * before...
1354 */
1355 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1356 vc->vc_underline = 0;
1357 break;
1358 case 49:
1359 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1360 break;
1361 default:
1362 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1363 vc->vc_color = color_table[vc->vc_par[i] - 30]
1364 | (vc->vc_color & 0xf0);
1365 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1366 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1367 | (vc->vc_color & 0x0f);
1368 break;
1369 }
1370 update_attr(vc);
1371}
1372
1373static void respond_string(const char *p, struct tty_struct *tty)
1374{
1375 while (*p) {
1376 tty_insert_flip_char(tty, *p, 0);
1377 p++;
1378 }
1379 con_schedule_flip(tty);
1380}
1381
1382static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1383{
1384 char buf[40];
1385
1386 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1387 respond_string(buf, tty);
1388}
1389
1390static inline void status_report(struct tty_struct *tty)
1391{
1392 respond_string("\033[0n", tty); /* Terminal ok */
1393}
1394
1395static inline void respond_ID(struct tty_struct * tty)
1396{
1397 respond_string(VT102ID, tty);
1398}
1399
1400void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1401{
1402 char buf[8];
1403
1404 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1405 (char)('!' + mry));
1406 respond_string(buf, tty);
1407}
1408
1409/* invoked via ioctl(TIOCLINUX) and through set_selection */
1410int mouse_reporting(void)
1411{
1412 return vc_cons[fg_console].d->vc_report_mouse;
1413}
1414
Torben Hohnac751ef2011-01-25 15:07:35 -08001415/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416static void set_mode(struct vc_data *vc, int on_off)
1417{
1418 int i;
1419
1420 for (i = 0; i <= vc->vc_npar; i++)
1421 if (vc->vc_ques) {
1422 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1423 case 1: /* Cursor keys send ^[Ox/^[[x */
1424 if (on_off)
1425 set_kbd(vc, decckm);
1426 else
1427 clr_kbd(vc, decckm);
1428 break;
1429 case 3: /* 80/132 mode switch unimplemented */
1430 vc->vc_deccolm = on_off;
1431#if 0
1432 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1433 /* this alone does not suffice; some user mode
1434 utility has to change the hardware regs */
1435#endif
1436 break;
1437 case 5: /* Inverted screen on/off */
1438 if (vc->vc_decscnm != on_off) {
1439 vc->vc_decscnm = on_off;
1440 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1441 update_attr(vc);
1442 }
1443 break;
1444 case 6: /* Origin relative/absolute */
1445 vc->vc_decom = on_off;
1446 gotoxay(vc, 0, 0);
1447 break;
1448 case 7: /* Autowrap on/off */
1449 vc->vc_decawm = on_off;
1450 break;
1451 case 8: /* Autorepeat on/off */
1452 if (on_off)
1453 set_kbd(vc, decarm);
1454 else
1455 clr_kbd(vc, decarm);
1456 break;
1457 case 9:
1458 vc->vc_report_mouse = on_off ? 1 : 0;
1459 break;
1460 case 25: /* Cursor on/off */
1461 vc->vc_deccm = on_off;
1462 break;
1463 case 1000:
1464 vc->vc_report_mouse = on_off ? 2 : 0;
1465 break;
1466 }
1467 } else {
1468 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1469 case 3: /* Monitor (display ctrls) */
1470 vc->vc_disp_ctrl = on_off;
1471 break;
1472 case 4: /* Insert Mode on/off */
1473 vc->vc_decim = on_off;
1474 break;
1475 case 20: /* Lf, Enter == CrLf/Lf */
1476 if (on_off)
1477 set_kbd(vc, lnm);
1478 else
1479 clr_kbd(vc, lnm);
1480 break;
1481 }
1482 }
1483}
1484
Torben Hohnac751ef2011-01-25 15:07:35 -08001485/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486static void setterm_command(struct vc_data *vc)
1487{
1488 switch(vc->vc_par[0]) {
1489 case 1: /* set color for underline mode */
1490 if (vc->vc_can_do_color &&
1491 vc->vc_par[1] < 16) {
1492 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1493 if (vc->vc_underline)
1494 update_attr(vc);
1495 }
1496 break;
1497 case 2: /* set color for half intensity mode */
1498 if (vc->vc_can_do_color &&
1499 vc->vc_par[1] < 16) {
1500 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1501 if (vc->vc_intensity == 0)
1502 update_attr(vc);
1503 }
1504 break;
1505 case 8: /* store colors as defaults */
1506 vc->vc_def_color = vc->vc_attr;
1507 if (vc->vc_hi_font_mask == 0x100)
1508 vc->vc_def_color >>= 1;
1509 default_attr(vc);
1510 update_attr(vc);
1511 break;
1512 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001513 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 poke_blanked_console();
1515 break;
1516 case 10: /* set bell frequency in Hz */
1517 if (vc->vc_npar >= 1)
1518 vc->vc_bell_pitch = vc->vc_par[1];
1519 else
1520 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1521 break;
1522 case 11: /* set bell duration in msec */
1523 if (vc->vc_npar >= 1)
1524 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1525 vc->vc_par[1] * HZ / 1000 : 0;
1526 else
1527 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1528 break;
1529 case 12: /* bring specified console to the front */
1530 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1531 set_console(vc->vc_par[1] - 1);
1532 break;
1533 case 13: /* unblank the screen */
1534 poke_blanked_console();
1535 break;
1536 case 14: /* set vesa powerdown interval */
1537 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1538 break;
1539 case 15: /* activate the previous console */
1540 set_console(last_console);
1541 break;
1542 }
1543}
1544
Torben Hohnac751ef2011-01-25 15:07:35 -08001545/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546static void csi_at(struct vc_data *vc, unsigned int nr)
1547{
1548 if (nr > vc->vc_cols - vc->vc_x)
1549 nr = vc->vc_cols - vc->vc_x;
1550 else if (!nr)
1551 nr = 1;
1552 insert_char(vc, nr);
1553}
1554
Torben Hohnac751ef2011-01-25 15:07:35 -08001555/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556static void csi_L(struct vc_data *vc, unsigned int nr)
1557{
1558 if (nr > vc->vc_rows - vc->vc_y)
1559 nr = vc->vc_rows - vc->vc_y;
1560 else if (!nr)
1561 nr = 1;
1562 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1563 vc->vc_need_wrap = 0;
1564}
1565
Torben Hohnac751ef2011-01-25 15:07:35 -08001566/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567static void csi_P(struct vc_data *vc, unsigned int nr)
1568{
1569 if (nr > vc->vc_cols - vc->vc_x)
1570 nr = vc->vc_cols - vc->vc_x;
1571 else if (!nr)
1572 nr = 1;
1573 delete_char(vc, nr);
1574}
1575
Torben Hohnac751ef2011-01-25 15:07:35 -08001576/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577static void csi_M(struct vc_data *vc, unsigned int nr)
1578{
1579 if (nr > vc->vc_rows - vc->vc_y)
1580 nr = vc->vc_rows - vc->vc_y;
1581 else if (!nr)
1582 nr=1;
1583 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1584 vc->vc_need_wrap = 0;
1585}
1586
Torben Hohnac751ef2011-01-25 15:07:35 -08001587/* console_lock is held (except via vc_init->reset_terminal */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588static void save_cur(struct vc_data *vc)
1589{
1590 vc->vc_saved_x = vc->vc_x;
1591 vc->vc_saved_y = vc->vc_y;
1592 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001593 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 vc->vc_s_underline = vc->vc_underline;
1595 vc->vc_s_blink = vc->vc_blink;
1596 vc->vc_s_reverse = vc->vc_reverse;
1597 vc->vc_s_charset = vc->vc_charset;
1598 vc->vc_s_color = vc->vc_color;
1599 vc->vc_saved_G0 = vc->vc_G0_charset;
1600 vc->vc_saved_G1 = vc->vc_G1_charset;
1601}
1602
Torben Hohnac751ef2011-01-25 15:07:35 -08001603/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604static void restore_cur(struct vc_data *vc)
1605{
1606 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1607 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001608 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 vc->vc_underline = vc->vc_s_underline;
1610 vc->vc_blink = vc->vc_s_blink;
1611 vc->vc_reverse = vc->vc_s_reverse;
1612 vc->vc_charset = vc->vc_s_charset;
1613 vc->vc_color = vc->vc_s_color;
1614 vc->vc_G0_charset = vc->vc_saved_G0;
1615 vc->vc_G1_charset = vc->vc_saved_G1;
1616 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1617 update_attr(vc);
1618 vc->vc_need_wrap = 0;
1619}
1620
1621enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1622 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1623 ESpalette };
1624
Torben Hohnac751ef2011-01-25 15:07:35 -08001625/* console_lock is held (except via vc_init()) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626static void reset_terminal(struct vc_data *vc, int do_clear)
1627{
1628 vc->vc_top = 0;
1629 vc->vc_bottom = vc->vc_rows;
1630 vc->vc_state = ESnormal;
1631 vc->vc_ques = 0;
1632 vc->vc_translate = set_translate(LAT1_MAP, vc);
1633 vc->vc_G0_charset = LAT1_MAP;
1634 vc->vc_G1_charset = GRAF_MAP;
1635 vc->vc_charset = 0;
1636 vc->vc_need_wrap = 0;
1637 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001638 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 vc->vc_utf_count = 0;
1640
1641 vc->vc_disp_ctrl = 0;
1642 vc->vc_toggle_meta = 0;
1643
1644 vc->vc_decscnm = 0;
1645 vc->vc_decom = 0;
1646 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001647 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 vc->vc_decim = 0;
1649
1650 set_kbd(vc, decarm);
1651 clr_kbd(vc, decckm);
1652 clr_kbd(vc, kbdapplic);
1653 clr_kbd(vc, lnm);
1654 kbd_table[vc->vc_num].lockstate = 0;
1655 kbd_table[vc->vc_num].slockstate = 0;
1656 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1657 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1658 /* do not do set_leds here because this causes an endless tasklet loop
1659 when the keyboard hasn't been initialized yet */
1660
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001661 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 vc->vc_complement_mask = vc->vc_s_complement_mask;
1663
1664 default_attr(vc);
1665 update_attr(vc);
1666
1667 vc->vc_tab_stop[0] = 0x01010100;
1668 vc->vc_tab_stop[1] =
1669 vc->vc_tab_stop[2] =
1670 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001671 vc->vc_tab_stop[4] =
1672 vc->vc_tab_stop[5] =
1673 vc->vc_tab_stop[6] =
1674 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675
1676 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1677 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1678
1679 gotoxy(vc, 0, 0);
1680 save_cur(vc);
1681 if (do_clear)
1682 csi_J(vc, 2);
1683}
1684
Torben Hohnac751ef2011-01-25 15:07:35 -08001685/* console_lock is held */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1687{
1688 /*
1689 * Control characters can be used in the _middle_
1690 * of an escape sequence.
1691 */
1692 switch (c) {
1693 case 0:
1694 return;
1695 case 7:
1696 if (vc->vc_bell_duration)
1697 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1698 return;
1699 case 8:
1700 bs(vc);
1701 return;
1702 case 9:
1703 vc->vc_pos -= (vc->vc_x << 1);
1704 while (vc->vc_x < vc->vc_cols - 1) {
1705 vc->vc_x++;
1706 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1707 break;
1708 }
1709 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001710 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 return;
1712 case 10: case 11: case 12:
1713 lf(vc);
1714 if (!is_kbd(vc, lnm))
1715 return;
1716 case 13:
1717 cr(vc);
1718 return;
1719 case 14:
1720 vc->vc_charset = 1;
1721 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1722 vc->vc_disp_ctrl = 1;
1723 return;
1724 case 15:
1725 vc->vc_charset = 0;
1726 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1727 vc->vc_disp_ctrl = 0;
1728 return;
1729 case 24: case 26:
1730 vc->vc_state = ESnormal;
1731 return;
1732 case 27:
1733 vc->vc_state = ESesc;
1734 return;
1735 case 127:
1736 del(vc);
1737 return;
1738 case 128+27:
1739 vc->vc_state = ESsquare;
1740 return;
1741 }
1742 switch(vc->vc_state) {
1743 case ESesc:
1744 vc->vc_state = ESnormal;
1745 switch (c) {
1746 case '[':
1747 vc->vc_state = ESsquare;
1748 return;
1749 case ']':
1750 vc->vc_state = ESnonstd;
1751 return;
1752 case '%':
1753 vc->vc_state = ESpercent;
1754 return;
1755 case 'E':
1756 cr(vc);
1757 lf(vc);
1758 return;
1759 case 'M':
1760 ri(vc);
1761 return;
1762 case 'D':
1763 lf(vc);
1764 return;
1765 case 'H':
1766 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1767 return;
1768 case 'Z':
1769 respond_ID(tty);
1770 return;
1771 case '7':
1772 save_cur(vc);
1773 return;
1774 case '8':
1775 restore_cur(vc);
1776 return;
1777 case '(':
1778 vc->vc_state = ESsetG0;
1779 return;
1780 case ')':
1781 vc->vc_state = ESsetG1;
1782 return;
1783 case '#':
1784 vc->vc_state = EShash;
1785 return;
1786 case 'c':
1787 reset_terminal(vc, 1);
1788 return;
1789 case '>': /* Numeric keypad */
1790 clr_kbd(vc, kbdapplic);
1791 return;
1792 case '=': /* Appl. keypad */
1793 set_kbd(vc, kbdapplic);
1794 return;
1795 }
1796 return;
1797 case ESnonstd:
1798 if (c=='P') { /* palette escape sequence */
1799 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1800 vc->vc_par[vc->vc_npar] = 0;
1801 vc->vc_npar = 0;
1802 vc->vc_state = ESpalette;
1803 return;
1804 } else if (c=='R') { /* reset palette */
1805 reset_palette(vc);
1806 vc->vc_state = ESnormal;
1807 } else
1808 vc->vc_state = ESnormal;
1809 return;
1810 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001811 if (isxdigit(c)) {
1812 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 if (vc->vc_npar == 7) {
1814 int i = vc->vc_par[0] * 3, j = 1;
1815 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1816 vc->vc_palette[i++] += vc->vc_par[j++];
1817 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1818 vc->vc_palette[i++] += vc->vc_par[j++];
1819 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1820 vc->vc_palette[i] += vc->vc_par[j];
1821 set_palette(vc);
1822 vc->vc_state = ESnormal;
1823 }
1824 } else
1825 vc->vc_state = ESnormal;
1826 return;
1827 case ESsquare:
1828 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1829 vc->vc_par[vc->vc_npar] = 0;
1830 vc->vc_npar = 0;
1831 vc->vc_state = ESgetpars;
1832 if (c == '[') { /* Function key */
1833 vc->vc_state=ESfunckey;
1834 return;
1835 }
1836 vc->vc_ques = (c == '?');
1837 if (vc->vc_ques)
1838 return;
1839 case ESgetpars:
1840 if (c == ';' && vc->vc_npar < NPAR - 1) {
1841 vc->vc_npar++;
1842 return;
1843 } else if (c>='0' && c<='9') {
1844 vc->vc_par[vc->vc_npar] *= 10;
1845 vc->vc_par[vc->vc_npar] += c - '0';
1846 return;
1847 } else
1848 vc->vc_state = ESgotpars;
1849 case ESgotpars:
1850 vc->vc_state = ESnormal;
1851 switch(c) {
1852 case 'h':
1853 set_mode(vc, 1);
1854 return;
1855 case 'l':
1856 set_mode(vc, 0);
1857 return;
1858 case 'c':
1859 if (vc->vc_ques) {
1860 if (vc->vc_par[0])
1861 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1862 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001863 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return;
1865 }
1866 break;
1867 case 'm':
1868 if (vc->vc_ques) {
1869 clear_selection();
1870 if (vc->vc_par[0])
1871 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1872 else
1873 vc->vc_complement_mask = vc->vc_s_complement_mask;
1874 return;
1875 }
1876 break;
1877 case 'n':
1878 if (!vc->vc_ques) {
1879 if (vc->vc_par[0] == 5)
1880 status_report(tty);
1881 else if (vc->vc_par[0] == 6)
1882 cursor_report(vc, tty);
1883 }
1884 return;
1885 }
1886 if (vc->vc_ques) {
1887 vc->vc_ques = 0;
1888 return;
1889 }
1890 switch(c) {
1891 case 'G': case '`':
1892 if (vc->vc_par[0])
1893 vc->vc_par[0]--;
1894 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1895 return;
1896 case 'A':
1897 if (!vc->vc_par[0])
1898 vc->vc_par[0]++;
1899 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1900 return;
1901 case 'B': case 'e':
1902 if (!vc->vc_par[0])
1903 vc->vc_par[0]++;
1904 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1905 return;
1906 case 'C': case 'a':
1907 if (!vc->vc_par[0])
1908 vc->vc_par[0]++;
1909 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1910 return;
1911 case 'D':
1912 if (!vc->vc_par[0])
1913 vc->vc_par[0]++;
1914 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1915 return;
1916 case 'E':
1917 if (!vc->vc_par[0])
1918 vc->vc_par[0]++;
1919 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1920 return;
1921 case 'F':
1922 if (!vc->vc_par[0])
1923 vc->vc_par[0]++;
1924 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1925 return;
1926 case 'd':
1927 if (vc->vc_par[0])
1928 vc->vc_par[0]--;
1929 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1930 return;
1931 case 'H': case 'f':
1932 if (vc->vc_par[0])
1933 vc->vc_par[0]--;
1934 if (vc->vc_par[1])
1935 vc->vc_par[1]--;
1936 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1937 return;
1938 case 'J':
1939 csi_J(vc, vc->vc_par[0]);
1940 return;
1941 case 'K':
1942 csi_K(vc, vc->vc_par[0]);
1943 return;
1944 case 'L':
1945 csi_L(vc, vc->vc_par[0]);
1946 return;
1947 case 'M':
1948 csi_M(vc, vc->vc_par[0]);
1949 return;
1950 case 'P':
1951 csi_P(vc, vc->vc_par[0]);
1952 return;
1953 case 'c':
1954 if (!vc->vc_par[0])
1955 respond_ID(tty);
1956 return;
1957 case 'g':
1958 if (!vc->vc_par[0])
1959 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1960 else if (vc->vc_par[0] == 3) {
1961 vc->vc_tab_stop[0] =
1962 vc->vc_tab_stop[1] =
1963 vc->vc_tab_stop[2] =
1964 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001965 vc->vc_tab_stop[4] =
1966 vc->vc_tab_stop[5] =
1967 vc->vc_tab_stop[6] =
1968 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 }
1970 return;
1971 case 'm':
1972 csi_m(vc);
1973 return;
1974 case 'q': /* DECLL - but only 3 leds */
1975 /* map 0,1,2,3 to 0,1,2,4 */
1976 if (vc->vc_par[0] < 4)
1977 setledstate(kbd_table + vc->vc_num,
1978 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1979 return;
1980 case 'r':
1981 if (!vc->vc_par[0])
1982 vc->vc_par[0]++;
1983 if (!vc->vc_par[1])
1984 vc->vc_par[1] = vc->vc_rows;
1985 /* Minimum allowed region is 2 lines */
1986 if (vc->vc_par[0] < vc->vc_par[1] &&
1987 vc->vc_par[1] <= vc->vc_rows) {
1988 vc->vc_top = vc->vc_par[0] - 1;
1989 vc->vc_bottom = vc->vc_par[1];
1990 gotoxay(vc, 0, 0);
1991 }
1992 return;
1993 case 's':
1994 save_cur(vc);
1995 return;
1996 case 'u':
1997 restore_cur(vc);
1998 return;
1999 case 'X':
2000 csi_X(vc, vc->vc_par[0]);
2001 return;
2002 case '@':
2003 csi_at(vc, vc->vc_par[0]);
2004 return;
2005 case ']': /* setterm functions */
2006 setterm_command(vc);
2007 return;
2008 }
2009 return;
2010 case ESpercent:
2011 vc->vc_state = ESnormal;
2012 switch (c) {
2013 case '@': /* defined in ISO 2022 */
2014 vc->vc_utf = 0;
2015 return;
2016 case 'G': /* prelim official escape code */
2017 case '8': /* retained for compatibility */
2018 vc->vc_utf = 1;
2019 return;
2020 }
2021 return;
2022 case ESfunckey:
2023 vc->vc_state = ESnormal;
2024 return;
2025 case EShash:
2026 vc->vc_state = ESnormal;
2027 if (c == '8') {
2028 /* DEC screen alignment test. kludge :-) */
2029 vc->vc_video_erase_char =
2030 (vc->vc_video_erase_char & 0xff00) | 'E';
2031 csi_J(vc, 2);
2032 vc->vc_video_erase_char =
2033 (vc->vc_video_erase_char & 0xff00) | ' ';
2034 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2035 }
2036 return;
2037 case ESsetG0:
2038 if (c == '0')
2039 vc->vc_G0_charset = GRAF_MAP;
2040 else if (c == 'B')
2041 vc->vc_G0_charset = LAT1_MAP;
2042 else if (c == 'U')
2043 vc->vc_G0_charset = IBMPC_MAP;
2044 else if (c == 'K')
2045 vc->vc_G0_charset = USER_MAP;
2046 if (vc->vc_charset == 0)
2047 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2048 vc->vc_state = ESnormal;
2049 return;
2050 case ESsetG1:
2051 if (c == '0')
2052 vc->vc_G1_charset = GRAF_MAP;
2053 else if (c == 'B')
2054 vc->vc_G1_charset = LAT1_MAP;
2055 else if (c == 'U')
2056 vc->vc_G1_charset = IBMPC_MAP;
2057 else if (c == 'K')
2058 vc->vc_G1_charset = USER_MAP;
2059 if (vc->vc_charset == 1)
2060 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2061 vc->vc_state = ESnormal;
2062 return;
2063 default:
2064 vc->vc_state = ESnormal;
2065 }
2066}
2067
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002068/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002069 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002070 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2071 */
2072struct interval {
2073 uint32_t first;
2074 uint32_t last;
2075};
2076
2077static int bisearch(uint32_t ucs, const struct interval *table, int max)
2078{
2079 int min = 0;
2080 int mid;
2081
2082 if (ucs < table[0].first || ucs > table[max].last)
2083 return 0;
2084 while (max >= min) {
2085 mid = (min + max) / 2;
2086 if (ucs > table[mid].last)
2087 min = mid + 1;
2088 else if (ucs < table[mid].first)
2089 max = mid - 1;
2090 else
2091 return 1;
2092 }
2093 return 0;
2094}
2095
2096static int is_double_width(uint32_t ucs)
2097{
2098 static const struct interval double_width[] = {
2099 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2100 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002101 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2102 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002103 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002104 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002105}
2106
Torben Hohnac751ef2011-01-25 15:07:35 -08002107/* acquires console_lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2109{
2110#ifdef VT_BUF_VRAM_ONLY
2111#define FLUSH do { } while(0);
2112#else
2113#define FLUSH if (draw_x >= 0) { \
2114 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2115 draw_x = -1; \
2116 }
2117#endif
2118
2119 int c, tc, ok, n = 0, draw_x = -1;
2120 unsigned int currcons;
2121 unsigned long draw_from = 0, draw_to = 0;
2122 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002123 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002124 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002125 uint8_t rescan;
2126 uint8_t inverse;
2127 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129
2130 if (in_interrupt())
2131 return count;
2132
2133 might_sleep();
2134
Torben Hohnac751ef2011-01-25 15:07:35 -08002135 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 vc = tty->driver_data;
2137 if (vc == NULL) {
2138 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
Torben Hohnac751ef2011-01-25 15:07:35 -08002139 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return 0;
2141 }
2142
2143 currcons = vc->vc_num;
2144 if (!vc_cons_allocated(currcons)) {
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002145 /* could this happen? */
2146 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2147 console_unlock();
2148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 himask = vc->vc_hi_font_mask;
2152 charmask = himask ? 0x1ff : 0xff;
2153
2154 /* undraw cursor first */
2155 if (IS_FG(vc))
2156 hide_cursor(vc);
2157
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002158 param.vc = vc;
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 while (!tty->stopped && count) {
2161 int orig = *buf;
2162 c = orig;
2163 buf++;
2164 n++;
2165 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002166 rescan = 0;
2167 inverse = 0;
2168 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
2170 /* Do no translation at all in control states */
2171 if (vc->vc_state != ESnormal) {
2172 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002173 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002174 /* Combine UTF-8 into Unicode in vc_utf_char.
2175 * vc_utf_count is the number of continuation bytes still
2176 * expected to arrive.
2177 * vc_npar is the number of continuation bytes arrived so
2178 * far
2179 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002180rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002181 if ((c & 0xc0) == 0x80) {
2182 /* Continuation byte received */
2183 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002184 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002185 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2186 vc->vc_npar++;
2187 if (--vc->vc_utf_count) {
2188 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002190 }
2191 /* Got a whole character */
2192 c = vc->vc_utf_char;
2193 /* Reject overlong sequences */
2194 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2195 c > utf8_length_changes[vc->vc_npar])
2196 c = 0xfffd;
2197 } else {
2198 /* Unexpected continuation byte */
2199 vc->vc_utf_count = 0;
2200 c = 0xfffd;
2201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002203 /* Single ASCII byte or first byte of a sequence received */
2204 if (vc->vc_utf_count) {
2205 /* Continuation byte expected */
2206 rescan = 1;
2207 vc->vc_utf_count = 0;
2208 c = 0xfffd;
2209 } else if (c > 0x7f) {
2210 /* First byte of a multibyte sequence received */
2211 vc->vc_npar = 0;
2212 if ((c & 0xe0) == 0xc0) {
2213 vc->vc_utf_count = 1;
2214 vc->vc_utf_char = (c & 0x1f);
2215 } else if ((c & 0xf0) == 0xe0) {
2216 vc->vc_utf_count = 2;
2217 vc->vc_utf_char = (c & 0x0f);
2218 } else if ((c & 0xf8) == 0xf0) {
2219 vc->vc_utf_count = 3;
2220 vc->vc_utf_char = (c & 0x07);
2221 } else if ((c & 0xfc) == 0xf8) {
2222 vc->vc_utf_count = 4;
2223 vc->vc_utf_char = (c & 0x03);
2224 } else if ((c & 0xfe) == 0xfc) {
2225 vc->vc_utf_count = 5;
2226 vc->vc_utf_char = (c & 0x01);
2227 } else {
2228 /* 254 and 255 are invalid */
2229 c = 0xfffd;
2230 }
2231 if (vc->vc_utf_count) {
2232 /* Still need some bytes */
2233 continue;
2234 }
2235 }
2236 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002238 /* End of UTF-8 decoding. */
2239 /* c is the received character, or U+FFFD for invalid sequences. */
2240 /* Replace invalid Unicode code points with U+FFFD too */
2241 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2242 c = 0xfffd;
2243 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002244 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002245 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 }
2247
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002248 param.c = tc;
2249 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2250 &param) == NOTIFY_STOP)
2251 continue;
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /* If the original code was a control character we
2254 * only allow a glyph to be displayed if the code is
2255 * not normally used (such as for cursor movement) or
2256 * if the disp_ctrl mode has been explicitly enabled.
2257 * Certain characters (as given by the CTRL_ALWAYS
2258 * bitmap) are always displayed as control characters,
2259 * as the console would be pretty useless without
2260 * them; to display an arbitrary font position use the
2261 * direct-to-font zone in UTF-8 mode.
2262 */
2263 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002264 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2265 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 && (c != 127 || vc->vc_disp_ctrl)
2267 && (c != 128+27);
2268
2269 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002270 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2271 if (is_double_width(c))
2272 width = 2;
2273 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /* Now try to find out how to display it */
2275 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002276 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002277 if (tc == -1 || tc == -2) {
2278 continue; /* nothing to display */
2279 }
2280 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002281 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002282 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002283 This would make absolutely no sense with Unicode in mind,
2284 but do this for ASCII characters since a font may lack
2285 Unicode mapping info and we don't want to end up with
2286 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002287 tc = c;
2288 } else {
2289 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2290 tc = conv_uni_to_pc(vc, 0xfffd);
2291 if (tc < 0) {
2292 inverse = 1;
2293 tc = conv_uni_to_pc(vc, '?');
2294 if (tc < 0) tc = '?';
2295 }
2296 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002299 if (!inverse) {
2300 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002302 /* invert vc_attr */
2303 if (!vc->vc_can_do_color) {
2304 vc_attr = (vc->vc_attr) ^ 0x08;
2305 } else if (vc->vc_hi_font_mask == 0x100) {
2306 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2307 } else {
2308 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002309 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002310 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002311 }
2312
2313 while (1) {
2314 if (vc->vc_need_wrap || vc->vc_decim)
2315 FLUSH
2316 if (vc->vc_need_wrap) {
2317 cr(vc);
2318 lf(vc);
2319 }
2320 if (vc->vc_decim)
2321 insert_char(vc, 1);
2322 scr_writew(himask ?
2323 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2324 (vc_attr << 8) + tc,
2325 (u16 *) vc->vc_pos);
2326 if (DO_UPDATE(vc) && draw_x < 0) {
2327 draw_x = vc->vc_x;
2328 draw_from = vc->vc_pos;
2329 }
2330 if (vc->vc_x == vc->vc_cols - 1) {
2331 vc->vc_need_wrap = vc->vc_decawm;
2332 draw_to = vc->vc_pos + 2;
2333 } else {
2334 vc->vc_x++;
2335 draw_to = (vc->vc_pos += 2);
2336 }
2337
2338 if (!--width) break;
2339
2340 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2341 if (tc < 0) tc = ' ';
2342 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002343 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002344
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002345 if (inverse) {
2346 FLUSH
2347 }
2348
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002349 if (rescan) {
2350 rescan = 0;
2351 inverse = 0;
2352 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002353 c = orig;
2354 goto rescan_last_byte;
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 continue;
2357 }
2358 FLUSH
2359 do_con_trol(tty, vc, orig);
2360 }
2361 FLUSH
2362 console_conditional_schedule();
Torben Hohnac751ef2011-01-25 15:07:35 -08002363 console_unlock();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002364 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 return n;
2366#undef FLUSH
2367}
2368
2369/*
2370 * This is the console switching callback.
2371 *
2372 * Doing console switching in a process context allows
2373 * us to do the switches asynchronously (needed when we want
2374 * to switch due to a keyboard interrupt). Synchronization
2375 * with other console code and prevention of re-entrancy is
Torben Hohnac751ef2011-01-25 15:07:35 -08002376 * ensured with console_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 */
David Howells65f27f32006-11-22 14:55:48 +00002378static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Torben Hohnac751ef2011-01-25 15:07:35 -08002380 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
2382 if (want_console >= 0) {
2383 if (want_console != fg_console &&
2384 vc_cons_allocated(want_console)) {
2385 hide_cursor(vc_cons[fg_console].d);
2386 change_console(vc_cons[want_console].d);
2387 /* we only changed when the console had already
2388 been allocated - a new console is not created
2389 in an interrupt routine */
2390 }
2391 want_console = -1;
2392 }
2393 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2394 do_poke_blanked_console = 0;
2395 poke_blanked_console();
2396 }
2397 if (scrollback_delta) {
2398 struct vc_data *vc = vc_cons[fg_console].d;
2399 clear_selection();
2400 if (vc->vc_mode == KD_TEXT)
2401 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2402 scrollback_delta = 0;
2403 }
2404 if (blank_timer_expired) {
2405 do_blank_screen(0);
2406 blank_timer_expired = 0;
2407 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002408 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409
Torben Hohnac751ef2011-01-25 15:07:35 -08002410 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002413int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002415 struct vc_data *vc = vc_cons[fg_console].d;
2416
2417 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2418 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2419
2420 /*
2421 * Console switch will fail in console_callback() or
2422 * change_console() so there is no point scheduling
2423 * the callback
2424 *
2425 * Existing set_console() users don't check the return
2426 * value so this shouldn't break anything
2427 */
2428 return -EINVAL;
2429 }
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 want_console = nr;
2432 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002433
2434 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435}
2436
2437struct tty_driver *console_driver;
2438
2439#ifdef CONFIG_VT_CONSOLE
2440
Bernhard Walle5ada9182009-12-14 18:00:43 -08002441/**
2442 * vt_kmsg_redirect() - Sets/gets the kernel message console
2443 * @new: The new virtual terminal number or -1 if the console should stay
2444 * unchanged
2445 *
2446 * By default, the kernel messages are always printed on the current virtual
2447 * console. However, the user may modify that default with the
2448 * TIOCL_SETKMSGREDIRECT ioctl call.
2449 *
2450 * This function sets the kernel message console to be @new. It returns the old
2451 * virtual console number. The virtual terminal number 0 (both as parameter and
2452 * return value) means no redirection (i.e. always printed on the currently
2453 * active console).
2454 *
2455 * The parameter -1 means that only the current console is returned, but the
2456 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2457 * case to make the code more understandable.
2458 *
2459 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2460 * the parameter and always returns 0.
2461 */
2462int vt_kmsg_redirect(int new)
2463{
2464 static int kmsg_con;
2465
2466 if (new != -1)
2467 return xchg(&kmsg_con, new);
2468 else
2469 return kmsg_con;
2470}
2471
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472/*
2473 * Console on virtual terminal
2474 *
2475 * The console must be locked when we get here.
2476 */
2477
2478static void vt_console_print(struct console *co, const char *b, unsigned count)
2479{
2480 struct vc_data *vc = vc_cons[fg_console].d;
2481 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002482 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 const ushort *start;
2484 ushort cnt = 0;
2485 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002486 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002489 if (!printable)
2490 return;
2491 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return;
2493
Bernhard Walle5ada9182009-12-14 18:00:43 -08002494 kmsg_console = vt_get_kmsg_redirect();
2495 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2496 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
2498 /* read `x' only after setting currcons properly (otherwise
2499 the `x' macro will read the x of the foreground console). */
2500 myx = vc->vc_x;
2501
2502 if (!vc_cons_allocated(fg_console)) {
2503 /* impossible */
2504 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2505 goto quit;
2506 }
2507
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002508 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 goto quit;
2510
2511 /* undraw cursor first */
2512 if (IS_FG(vc))
2513 hide_cursor(vc);
2514
2515 start = (ushort *)vc->vc_pos;
2516
2517 /* Contrived structure to try to emulate original need_wrap behaviour
2518 * Problems caused when we have need_wrap set on '\n' character */
2519 while (count--) {
2520 c = *b++;
2521 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2522 if (cnt > 0) {
2523 if (CON_IS_VISIBLE(vc))
2524 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2525 vc->vc_x += cnt;
2526 if (vc->vc_need_wrap)
2527 vc->vc_x--;
2528 cnt = 0;
2529 }
2530 if (c == 8) { /* backspace */
2531 bs(vc);
2532 start = (ushort *)vc->vc_pos;
2533 myx = vc->vc_x;
2534 continue;
2535 }
2536 if (c != 13)
2537 lf(vc);
2538 cr(vc);
2539 start = (ushort *)vc->vc_pos;
2540 myx = vc->vc_x;
2541 if (c == 10 || c == 13)
2542 continue;
2543 }
2544 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002545 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 cnt++;
2547 if (myx == vc->vc_cols - 1) {
2548 vc->vc_need_wrap = 1;
2549 continue;
2550 }
2551 vc->vc_pos += 2;
2552 myx++;
2553 }
2554 if (cnt > 0) {
2555 if (CON_IS_VISIBLE(vc))
2556 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2557 vc->vc_x += cnt;
2558 if (vc->vc_x == vc->vc_cols) {
2559 vc->vc_x--;
2560 vc->vc_need_wrap = 1;
2561 }
2562 }
2563 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002564 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002567 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568}
2569
2570static struct tty_driver *vt_console_device(struct console *c, int *index)
2571{
2572 *index = c->index ? c->index-1 : fg_console;
2573 return console_driver;
2574}
2575
2576static struct console vt_console_driver = {
2577 .name = "tty",
2578 .write = vt_console_print,
2579 .device = vt_console_device,
2580 .unblank = unblank_screen,
2581 .flags = CON_PRINTBUFFER,
2582 .index = -1,
2583};
2584#endif
2585
2586/*
2587 * Handling of Linux-specific VC ioctls
2588 */
2589
2590/*
Torben Hohnac751ef2011-01-25 15:07:35 -08002591 * Generally a bit racy with respect to console_lock();.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 *
2593 * There are some functions which don't need it.
2594 *
2595 * There are some functions which can sleep for arbitrary periods
2596 * (paste_selection) but we don't need the lock there anyway.
2597 *
2598 * set_selection has locking, and definitely needs it
2599 */
2600
2601int tioclinux(struct tty_struct *tty, unsigned long arg)
2602{
2603 char type, data;
2604 char __user *p = (char __user *)arg;
2605 int lines;
2606 int ret;
2607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2609 return -EPERM;
2610 if (get_user(type, p))
2611 return -EFAULT;
2612 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002613
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 switch (type)
2615 {
2616 case TIOCL_SETSEL:
Torben Hohnac751ef2011-01-25 15:07:35 -08002617 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
Torben Hohnac751ef2011-01-25 15:07:35 -08002619 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 break;
2621 case TIOCL_PASTESEL:
2622 ret = paste_selection(tty);
2623 break;
2624 case TIOCL_UNBLANKSCREEN:
Torben Hohnac751ef2011-01-25 15:07:35 -08002625 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 unblank_screen();
Torben Hohnac751ef2011-01-25 15:07:35 -08002627 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 break;
2629 case TIOCL_SELLOADLUT:
2630 ret = sel_loadlut(p);
2631 break;
2632 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002633
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 /*
2635 * Make it possible to react to Shift+Mousebutton.
2636 * Note that 'shift_state' is an undocumented
2637 * kernel-internal variable; programs not closely
2638 * related to the kernel should not use this.
2639 */
2640 data = shift_state;
2641 ret = __put_user(data, p);
2642 break;
2643 case TIOCL_GETMOUSEREPORTING:
2644 data = mouse_reporting();
2645 ret = __put_user(data, p);
2646 break;
2647 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002648 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002650 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002651 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002652 ret = __put_user(data, p);
2653 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 case TIOCL_SETKMSGREDIRECT:
2655 if (!capable(CAP_SYS_ADMIN)) {
2656 ret = -EPERM;
2657 } else {
2658 if (get_user(data, p+1))
2659 ret = -EFAULT;
2660 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002661 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663 break;
2664 case TIOCL_GETFGCONSOLE:
2665 ret = fg_console;
2666 break;
2667 case TIOCL_SCROLLCONSOLE:
2668 if (get_user(lines, (s32 __user *)(p+4))) {
2669 ret = -EFAULT;
2670 } else {
2671 scrollfront(vc_cons[fg_console].d, lines);
2672 ret = 0;
2673 }
2674 break;
2675 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Torben Hohnac751ef2011-01-25 15:07:35 -08002676 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 ignore_poke = 1;
2678 do_blank_screen(0);
Torben Hohnac751ef2011-01-25 15:07:35 -08002679 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 break;
2681 case TIOCL_BLANKEDSCREEN:
2682 ret = console_blanked;
2683 break;
2684 default:
2685 ret = -EINVAL;
2686 break;
2687 }
2688 return ret;
2689}
2690
2691/*
2692 * /dev/ttyN handling
2693 */
2694
2695static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2696{
2697 int retval;
2698
2699 retval = do_con_write(tty, buf, count);
2700 con_flush_chars(tty);
2701
2702 return retval;
2703}
2704
Alan Cox5d19f542008-04-30 00:54:08 -07002705static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706{
2707 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002708 return 0; /* n_r3964 calls put_char() from interrupt context */
2709 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712static int con_write_room(struct tty_struct *tty)
2713{
2714 if (tty->stopped)
2715 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002716 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717}
2718
2719static int con_chars_in_buffer(struct tty_struct *tty)
2720{
2721 return 0; /* we're not buffering */
2722}
2723
2724/*
2725 * con_throttle and con_unthrottle are only used for
2726 * paste_selection(), which has to stuff in a large number of
2727 * characters...
2728 */
2729static void con_throttle(struct tty_struct *tty)
2730{
2731}
2732
2733static void con_unthrottle(struct tty_struct *tty)
2734{
2735 struct vc_data *vc = tty->driver_data;
2736
2737 wake_up_interruptible(&vc->paste_wait);
2738}
2739
2740/*
2741 * Turn the Scroll-Lock LED on when the tty is stopped
2742 */
2743static void con_stop(struct tty_struct *tty)
2744{
2745 int console_num;
2746 if (!tty)
2747 return;
2748 console_num = tty->index;
2749 if (!vc_cons_allocated(console_num))
2750 return;
2751 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2752 set_leds();
2753}
2754
2755/*
2756 * Turn the Scroll-Lock LED off when the console is started
2757 */
2758static void con_start(struct tty_struct *tty)
2759{
2760 int console_num;
2761 if (!tty)
2762 return;
2763 console_num = tty->index;
2764 if (!vc_cons_allocated(console_num))
2765 return;
2766 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2767 set_leds();
2768}
2769
2770static void con_flush_chars(struct tty_struct *tty)
2771{
2772 struct vc_data *vc;
2773
2774 if (in_interrupt()) /* from flush_to_ldisc */
2775 return;
2776
2777 /* if we race with con_close(), vt may be null */
Torben Hohnac751ef2011-01-25 15:07:35 -08002778 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 vc = tty->driver_data;
2780 if (vc)
2781 set_cursor(vc);
Torben Hohnac751ef2011-01-25 15:07:35 -08002782 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
2785/*
2786 * Allocate the console screen memory.
2787 */
2788static int con_open(struct tty_struct *tty, struct file *filp)
2789{
2790 unsigned int currcons = tty->index;
2791 int ret = 0;
2792
Torben Hohnac751ef2011-01-25 15:07:35 -08002793 console_lock();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002794 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 ret = vc_allocate(currcons);
2796 if (ret == 0) {
2797 struct vc_data *vc = vc_cons[currcons].d;
Alan Coxfeebed62008-10-13 10:41:30 +01002798
2799 /* Still being freed */
Alan Cox8ce73262010-06-01 22:52:56 +02002800 if (vc->port.tty) {
Torben Hohnac751ef2011-01-25 15:07:35 -08002801 console_unlock();
Alan Coxfeebed62008-10-13 10:41:30 +01002802 return -ERESTARTSYS;
2803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 tty->driver_data = vc;
Alan Cox8ce73262010-06-01 22:52:56 +02002805 vc->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806
2807 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2808 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2809 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2810 }
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002811 if (vc->vc_utf)
2812 tty->termios->c_iflag |= IUTF8;
2813 else
2814 tty->termios->c_iflag &= ~IUTF8;
Torben Hohnac751ef2011-01-25 15:07:35 -08002815 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return ret;
2817 }
2818 }
Torben Hohnac751ef2011-01-25 15:07:35 -08002819 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return ret;
2821}
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823static void con_close(struct tty_struct *tty, struct file *filp)
2824{
Alan Coxfeebed62008-10-13 10:41:30 +01002825 /* Nothing to do - we defer to shutdown */
2826}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
Alan Coxfeebed62008-10-13 10:41:30 +01002828static void con_shutdown(struct tty_struct *tty)
2829{
2830 struct vc_data *vc = tty->driver_data;
2831 BUG_ON(vc == NULL);
Torben Hohnac751ef2011-01-25 15:07:35 -08002832 console_lock();
Alan Cox8ce73262010-06-01 22:52:56 +02002833 vc->port.tty = NULL;
Torben Hohnac751ef2011-01-25 15:07:35 -08002834 console_unlock();
Alan Coxfeebed62008-10-13 10:41:30 +01002835 tty_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002838static int default_italic_color = 2; // green (ASCII)
2839static int default_underline_color = 3; // cyan (ASCII)
2840module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2841module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843static void vc_init(struct vc_data *vc, unsigned int rows,
2844 unsigned int cols, int do_clear)
2845{
2846 int j, k ;
2847
2848 vc->vc_cols = cols;
2849 vc->vc_rows = rows;
2850 vc->vc_size_row = cols << 1;
2851 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2852
2853 set_origin(vc);
2854 vc->vc_pos = vc->vc_origin;
2855 reset_vc(vc);
2856 for (j=k=0; j<16; j++) {
2857 vc->vc_palette[k++] = default_red[j] ;
2858 vc->vc_palette[k++] = default_grn[j] ;
2859 vc->vc_palette[k++] = default_blu[j] ;
2860 }
2861 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002862 vc->vc_ulcolor = default_underline_color;
2863 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 vc->vc_halfcolor = 0x08; /* grey */
2865 init_waitqueue_head(&vc->paste_wait);
2866 reset_terminal(vc, do_clear);
2867}
2868
2869/*
2870 * This routine initializes console interrupts, and does nothing
2871 * else. If you want the screen to clear, call tty_write with
2872 * the appropriate escape-sequence.
2873 */
2874
2875static int __init con_init(void)
2876{
2877 const char *display_desc = NULL;
2878 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002879 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Torben Hohnac751ef2011-01-25 15:07:35 -08002881 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
2883 if (conswitchp)
2884 display_desc = conswitchp->con_startup();
2885 if (!display_desc) {
2886 fg_console = 0;
Torben Hohnac751ef2011-01-25 15:07:35 -08002887 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return 0;
2889 }
2890
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002891 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2892 struct con_driver *con_driver = &registered_con_driver[i];
2893
2894 if (con_driver->con == NULL) {
2895 con_driver->con = conswitchp;
2896 con_driver->desc = display_desc;
2897 con_driver->flag = CON_DRIVER_FLAG_INIT;
2898 con_driver->first = 0;
2899 con_driver->last = MAX_NR_CONSOLES - 1;
2900 break;
2901 }
2902 }
2903
2904 for (i = 0; i < MAX_NR_CONSOLES; i++)
2905 con_driver_map[i] = conswitchp;
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (blankinterval) {
2908 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002909 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002913 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002914 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Alan Coxff917ba2010-06-01 22:52:55 +02002915 tty_port_init(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002917 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 vc_init(vc, vc->vc_rows, vc->vc_cols,
2919 currcons || !vc->vc_sw->con_save_screen);
2920 }
2921 currcons = fg_console = 0;
2922 master_display_fg = vc = vc_cons[currcons].d;
2923 set_origin(vc);
2924 save_screen(vc);
2925 gotoxy(vc, vc->vc_x, vc->vc_y);
2926 csi_J(vc, 0);
2927 update_screen(vc);
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08002928 pr_info("Console: %s %s %dx%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 vc->vc_can_do_color ? "colour" : "mono",
2930 display_desc, vc->vc_cols, vc->vc_rows);
2931 printable = 1;
2932 printk("\n");
2933
Torben Hohnac751ef2011-01-25 15:07:35 -08002934 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936#ifdef CONFIG_VT_CONSOLE
2937 register_console(&vt_console_driver);
2938#endif
2939 return 0;
2940}
2941console_initcall(con_init);
2942
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002943static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 .open = con_open,
2945 .close = con_close,
2946 .write = con_write,
2947 .write_room = con_write_room,
2948 .put_char = con_put_char,
2949 .flush_chars = con_flush_chars,
2950 .chars_in_buffer = con_chars_in_buffer,
2951 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02002952#ifdef CONFIG_COMPAT
2953 .compat_ioctl = vt_compat_ioctl,
2954#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 .stop = con_stop,
2956 .start = con_start,
2957 .throttle = con_throttle,
2958 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002959 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01002960 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961};
2962
Alan Coxd81ed102008-10-13 10:41:42 +01002963static struct cdev vc0_cdev;
2964
Kay Sieversfbc92a32010-12-01 18:51:05 +01002965static ssize_t show_tty_active(struct device *dev,
2966 struct device_attribute *attr, char *buf)
2967{
2968 return sprintf(buf, "tty%d\n", fg_console + 1);
2969}
2970static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
2971
Alan Coxd81ed102008-10-13 10:41:42 +01002972int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Alan Coxd81ed102008-10-13 10:41:42 +01002974 cdev_init(&vc0_cdev, console_fops);
2975 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2976 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2977 panic("Couldn't register /dev/tty0 driver\n");
Kay Sieversfbc92a32010-12-01 18:51:05 +01002978 tty0dev = device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2979 if (IS_ERR(tty0dev))
2980 tty0dev = NULL;
2981 else
Kay Sieversa2a6a822011-01-09 16:39:14 +01002982 WARN_ON(device_create_file(tty0dev, &dev_attr_active) < 0);
Alan Coxd81ed102008-10-13 10:41:42 +01002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 vcs_init();
2985
2986 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2987 if (!console_driver)
2988 panic("Couldn't allocate console driver\n");
2989 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 console_driver->name = "tty";
2991 console_driver->name_base = 1;
2992 console_driver->major = TTY_MAJOR;
2993 console_driver->minor_start = 1;
2994 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2995 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002996 if (default_utf8)
2997 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2999 tty_set_operations(console_driver, &con_ops);
3000 if (tty_register_driver(console_driver))
3001 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 kbd_init();
3003 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004#ifdef CONFIG_MDA_CONSOLE
3005 mda_console_init();
3006#endif
3007 return 0;
3008}
3009
3010#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003011
3012static struct class *vtconsole_class;
3013
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003014static int bind_con_driver(const struct consw *csw, int first, int last,
3015 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003017 struct module *owner = csw->owner;
3018 const char *desc = NULL;
3019 struct con_driver *con_driver;
3020 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (!try_module_get(owner))
3023 return -ENODEV;
3024
Torben Hohnac751ef2011-01-25 15:07:35 -08003025 console_lock();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003026
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003027 /* check if driver is registered */
3028 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3029 con_driver = &registered_con_driver[i];
3030
3031 if (con_driver->con == csw) {
3032 desc = con_driver->desc;
3033 retval = 0;
3034 break;
3035 }
3036 }
3037
3038 if (retval)
3039 goto err;
3040
3041 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3042 csw->con_startup();
3043 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 if (deflt) {
3047 if (conswitchp)
3048 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003049
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 __module_get(owner);
3051 conswitchp = csw;
3052 }
3053
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003054 first = max(first, con_driver->first);
3055 last = min(last, con_driver->last);
3056
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 for (i = first; i <= last; i++) {
3058 int old_was_color;
3059 struct vc_data *vc = vc_cons[i].d;
3060
3061 if (con_driver_map[i])
3062 module_put(con_driver_map[i]->owner);
3063 __module_get(owner);
3064 con_driver_map[i] = csw;
3065
3066 if (!vc || !vc->vc_sw)
3067 continue;
3068
3069 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003070
David Hollister4ee1acc2006-06-26 00:26:41 -07003071 if (CON_IS_VISIBLE(vc)) {
3072 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003074 }
3075
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 old_was_color = vc->vc_can_do_color;
3077 vc->vc_sw->con_deinit(vc);
Francisco Jerez9fc2b2d2010-08-22 17:37:24 +02003078 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003080 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 update_attr(vc);
3082
3083 /* If the console changed between mono <-> color, then
3084 * the attributes in the screenbuf will be wrong. The
3085 * following resets all attributes to something sane.
3086 */
3087 if (old_was_color != vc->vc_can_do_color)
3088 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003090
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003091 pr_info("Console: switching ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 if (!deflt)
3093 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003094 if (j >= 0) {
3095 struct vc_data *vc = vc_cons[j].d;
3096
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003098 vc->vc_can_do_color ? "colour" : "mono",
3099 desc, vc->vc_cols, vc->vc_rows);
3100
3101 if (k >= 0) {
3102 vc = vc_cons[k].d;
3103 update_screen(vc);
3104 }
3105 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 printk("to %s\n", desc);
3107
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003108 retval = 0;
3109err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003110 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003112 return retval;
3113};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003115#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3116static int con_is_graphics(const struct consw *csw, int first, int last)
3117{
3118 int i, retval = 0;
3119
3120 for (i = first; i <= last; i++) {
3121 struct vc_data *vc = vc_cons[i].d;
3122
3123 if (vc && vc->vc_mode == KD_GRAPHICS) {
3124 retval = 1;
3125 break;
3126 }
3127 }
3128
3129 return retval;
3130}
3131
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003132/**
3133 * unbind_con_driver - unbind a console driver
3134 * @csw: pointer to console driver to unregister
3135 * @first: first in range of consoles that @csw should be unbound from
3136 * @last: last in range of consoles that @csw should be unbound from
3137 * @deflt: should next bound console driver be default after @csw is unbound?
3138 *
3139 * To unbind a driver from all possible consoles, pass 0 as @first and
3140 * %MAX_NR_CONSOLES as @last.
3141 *
3142 * @deflt controls whether the console that ends up replacing @csw should be
3143 * the default console.
3144 *
3145 * RETURNS:
3146 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3147 * or 0 on success.
3148 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003149int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003151 struct module *owner = csw->owner;
3152 const struct consw *defcsw = NULL;
3153 struct con_driver *con_driver = NULL, *con_back = NULL;
3154 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003156 if (!try_module_get(owner))
3157 return -ENODEV;
3158
Torben Hohnac751ef2011-01-25 15:07:35 -08003159 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003160
3161 /* check if driver is registered and if it is unbindable */
3162 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3163 con_driver = &registered_con_driver[i];
3164
3165 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003166 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003167 retval = 0;
3168 break;
3169 }
3170 }
3171
3172 if (retval) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003173 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003174 goto err;
3175 }
3176
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003177 retval = -ENODEV;
3178
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003179 /* check if backup driver exists */
3180 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3181 con_back = &registered_con_driver[i];
3182
3183 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003184 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003185 defcsw = con_back->con;
3186 retval = 0;
3187 break;
3188 }
3189 }
3190
3191 if (retval) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003192 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003193 goto err;
3194 }
3195
3196 if (!con_is_bound(csw)) {
Torben Hohnac751ef2011-01-25 15:07:35 -08003197 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003198 goto err;
3199 }
3200
3201 first = max(first, con_driver->first);
3202 last = min(last, con_driver->last);
3203
3204 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205 if (con_driver_map[i] == csw) {
3206 module_put(csw->owner);
3207 con_driver_map[i] = NULL;
3208 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003209 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003210
3211 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003212 const struct consw *defconsw = conswitchp;
3213
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003214 defcsw->con_startup();
3215 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003216 /*
3217 * vgacon may change the default driver to point
3218 * to dummycon, we restore it here...
3219 */
3220 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003221 }
3222
3223 if (!con_is_bound(csw))
3224 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3225
Torben Hohnac751ef2011-01-25 15:07:35 -08003226 console_unlock();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003227 /* ignore return value, binding should not fail */
3228 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003229err:
3230 module_put(owner);
3231 return retval;
3232
3233}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003234EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003235
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003236static int vt_bind(struct con_driver *con)
3237{
3238 const struct consw *defcsw = NULL, *csw = NULL;
3239 int i, more = 1, first = -1, last = -1, deflt = 0;
3240
3241 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3242 con_is_graphics(con->con, con->first, con->last))
3243 goto err;
3244
3245 csw = con->con;
3246
3247 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3248 struct con_driver *con = &registered_con_driver[i];
3249
3250 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3251 defcsw = con->con;
3252 break;
3253 }
3254 }
3255
3256 if (!defcsw)
3257 goto err;
3258
3259 while (more) {
3260 more = 0;
3261
3262 for (i = con->first; i <= con->last; i++) {
3263 if (con_driver_map[i] == defcsw) {
3264 if (first == -1)
3265 first = i;
3266 last = i;
3267 more = 1;
3268 } else if (first != -1)
3269 break;
3270 }
3271
3272 if (first == 0 && last == MAX_NR_CONSOLES -1)
3273 deflt = 1;
3274
3275 if (first != -1)
3276 bind_con_driver(csw, first, last, deflt);
3277
3278 first = -1;
3279 last = -1;
3280 deflt = 0;
3281 }
3282
3283err:
3284 return 0;
3285}
3286
3287static int vt_unbind(struct con_driver *con)
3288{
3289 const struct consw *csw = NULL;
3290 int i, more = 1, first = -1, last = -1, deflt = 0;
3291
3292 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3293 con_is_graphics(con->con, con->first, con->last))
3294 goto err;
3295
3296 csw = con->con;
3297
3298 while (more) {
3299 more = 0;
3300
3301 for (i = con->first; i <= con->last; i++) {
3302 if (con_driver_map[i] == csw) {
3303 if (first == -1)
3304 first = i;
3305 last = i;
3306 more = 1;
3307 } else if (first != -1)
3308 break;
3309 }
3310
3311 if (first == 0 && last == MAX_NR_CONSOLES -1)
3312 deflt = 1;
3313
3314 if (first != -1)
3315 unbind_con_driver(csw, first, last, deflt);
3316
3317 first = -1;
3318 last = -1;
3319 deflt = 0;
3320 }
3321
3322err:
3323 return 0;
3324}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003325#else
3326static inline int vt_bind(struct con_driver *con)
3327{
3328 return 0;
3329}
3330static inline int vt_unbind(struct con_driver *con)
3331{
3332 return 0;
3333}
3334#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003335
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003336static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003337 const char *buf, size_t count)
3338{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003339 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003340 int bind = simple_strtoul(buf, NULL, 0);
3341
3342 if (bind)
3343 vt_bind(con);
3344 else
3345 vt_unbind(con);
3346
3347 return count;
3348}
3349
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003350static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3351 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003352{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003353 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003354 int bind = con_is_bound(con->con);
3355
3356 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3357}
3358
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003359static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3360 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003361{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003362 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003363
3364 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3365 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3366 con->desc);
3367
3368}
3369
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003370static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003371 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3372 __ATTR(name, S_IRUGO, show_name, NULL),
3373};
3374
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003375static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003376{
3377 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003378 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003379
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003380 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003381 dev_set_drvdata(con->dev, con);
3382 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3383 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003384 if (error)
3385 break;
3386 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003387
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003388 if (error) {
3389 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003390 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003391 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3392 }
3393
3394 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003395}
3396
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003397static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003398{
3399 int i;
3400
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003401 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003402 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3403 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003404 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3405 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003406}
3407
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003408/**
3409 * con_is_bound - checks if driver is bound to the console
3410 * @csw: console driver
3411 *
3412 * RETURNS: zero if unbound, nonzero if bound
3413 *
3414 * Drivers can call this and if zero, they should release
3415 * all resources allocated on con_startup()
3416 */
3417int con_is_bound(const struct consw *csw)
3418{
3419 int i, bound = 0;
3420
3421 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3422 if (con_driver_map[i] == csw) {
3423 bound = 1;
3424 break;
3425 }
3426 }
3427
3428 return bound;
3429}
3430EXPORT_SYMBOL(con_is_bound);
3431
3432/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003433 * con_debug_enter - prepare the console for the kernel debugger
3434 * @sw: console driver
3435 *
3436 * Called when the console is taken over by the kernel debugger, this
3437 * function needs to save the current console state, then put the console
3438 * into a state suitable for the kernel debugger.
3439 *
3440 * RETURNS:
3441 * Zero on success, nonzero if a failure occurred when trying to prepare
3442 * the console for the debugger.
3443 */
3444int con_debug_enter(struct vc_data *vc)
3445{
3446 int ret = 0;
3447
3448 saved_fg_console = fg_console;
3449 saved_last_console = last_console;
3450 saved_want_console = want_console;
3451 saved_vc_mode = vc->vc_mode;
Jason Wesselbeed5332010-08-16 15:58:31 -05003452 saved_console_blanked = console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003453 vc->vc_mode = KD_TEXT;
3454 console_blanked = 0;
3455 if (vc->vc_sw->con_debug_enter)
3456 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003457#ifdef CONFIG_KGDB_KDB
3458 /* Set the initial LINES variable if it is not already set */
3459 if (vc->vc_rows < 999) {
3460 int linecount;
3461 char lns[4];
3462 const char *setargs[3] = {
3463 "set",
3464 "LINES",
3465 lns,
3466 };
3467 if (kdbgetintenv(setargs[0], &linecount)) {
3468 snprintf(lns, 4, "%i", vc->vc_rows);
3469 kdb_set(2, setargs);
3470 }
3471 }
3472#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003473 return ret;
3474}
3475EXPORT_SYMBOL_GPL(con_debug_enter);
3476
3477/**
3478 * con_debug_leave - restore console state
3479 * @sw: console driver
3480 *
3481 * Restore the console state to what it was before the kernel debugger
3482 * was invoked.
3483 *
3484 * RETURNS:
3485 * Zero on success, nonzero if a failure occurred when trying to restore
3486 * the console.
3487 */
3488int con_debug_leave(void)
3489{
3490 struct vc_data *vc;
3491 int ret = 0;
3492
3493 fg_console = saved_fg_console;
3494 last_console = saved_last_console;
3495 want_console = saved_want_console;
Jason Wesselbeed5332010-08-16 15:58:31 -05003496 console_blanked = saved_console_blanked;
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003497 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3498
3499 vc = vc_cons[fg_console].d;
3500 if (vc->vc_sw->con_debug_leave)
3501 ret = vc->vc_sw->con_debug_leave(vc);
3502 return ret;
3503}
3504EXPORT_SYMBOL_GPL(con_debug_leave);
3505
3506/**
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003507 * register_con_driver - register console driver to console layer
3508 * @csw: console driver
3509 * @first: the first console to take over, minimum value is 0
3510 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3511 *
3512 * DESCRIPTION: This function registers a console driver which can later
3513 * bind to a range of consoles specified by @first and @last. It will
3514 * also initialize the console driver by calling con_startup().
3515 */
3516int register_con_driver(const struct consw *csw, int first, int last)
3517{
3518 struct module *owner = csw->owner;
3519 struct con_driver *con_driver;
3520 const char *desc;
3521 int i, retval = 0;
3522
3523 if (!try_module_get(owner))
3524 return -ENODEV;
3525
Torben Hohnac751ef2011-01-25 15:07:35 -08003526 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003527
3528 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3529 con_driver = &registered_con_driver[i];
3530
3531 /* already registered */
3532 if (con_driver->con == csw)
Dave Airliec55c63c2011-01-07 09:57:41 +10003533 retval = -EBUSY;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003534 }
3535
3536 if (retval)
3537 goto err;
3538
3539 desc = csw->con_startup();
3540
3541 if (!desc)
3542 goto err;
3543
3544 retval = -EINVAL;
3545
3546 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3547 con_driver = &registered_con_driver[i];
3548
3549 if (con_driver->con == NULL) {
3550 con_driver->con = csw;
3551 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003552 con_driver->node = i;
3553 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003554 CON_DRIVER_FLAG_INIT;
3555 con_driver->first = first;
3556 con_driver->last = last;
3557 retval = 0;
3558 break;
3559 }
3560 }
3561
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003562 if (retval)
3563 goto err;
3564
Alan Coxd81ed102008-10-13 10:41:42 +01003565 con_driver->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003566 MKDEV(0, con_driver->node),
3567 NULL, "vtcon%i",
3568 con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003569
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003570 if (IS_ERR(con_driver->dev)) {
3571 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003572 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003573 PTR_ERR(con_driver->dev));
3574 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003575 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003576 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003577 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003578
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003579err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003580 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003581 module_put(owner);
3582 return retval;
3583}
3584EXPORT_SYMBOL(register_con_driver);
3585
3586/**
3587 * unregister_con_driver - unregister console driver from console layer
3588 * @csw: console driver
3589 *
3590 * DESCRIPTION: All drivers that registers to the console layer must
3591 * call this function upon exit, or if the console driver is in a state
3592 * where it won't be able to handle console services, such as the
3593 * framebuffer console without loaded framebuffer drivers.
3594 *
3595 * The driver must unbind first prior to unregistration.
3596 */
3597int unregister_con_driver(const struct consw *csw)
3598{
3599 int i, retval = -ENODEV;
3600
Torben Hohnac751ef2011-01-25 15:07:35 -08003601 console_lock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003602
3603 /* cannot unregister a bound driver */
3604 if (con_is_bound(csw))
3605 goto err;
3606
3607 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3608 struct con_driver *con_driver = &registered_con_driver[i];
3609
3610 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003611 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003612 vtconsole_deinit_device(con_driver);
3613 device_destroy(vtconsole_class,
3614 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003615 con_driver->con = NULL;
3616 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003617 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003618 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003619 con_driver->flag = 0;
3620 con_driver->first = 0;
3621 con_driver->last = 0;
3622 retval = 0;
3623 break;
3624 }
3625 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003626err:
Torben Hohnac751ef2011-01-25 15:07:35 -08003627 console_unlock();
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003628 return retval;
3629}
3630EXPORT_SYMBOL(unregister_con_driver);
3631
3632/*
3633 * If we support more console drivers, this function is used
3634 * when a driver wants to take over some existing consoles
3635 * and become default driver for newly opened ones.
3636 *
3637 * take_over_console is basically a register followed by unbind
3638 */
3639int take_over_console(const struct consw *csw, int first, int last, int deflt)
3640{
3641 int err;
3642
3643 err = register_con_driver(csw, first, last);
Dave Airliec55c63c2011-01-07 09:57:41 +10003644 /* if we get an busy error we still want to bind the console driver
3645 * and return success, as we may have unbound the console driver
3646  * but not unregistered it.
3647 */
3648 if (err == -EBUSY)
3649 err = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003650 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003651 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003652
3653 return err;
3654}
3655
3656/*
3657 * give_up_console is a wrapper to unregister_con_driver. It will only
3658 * work if driver is fully unbound.
3659 */
3660void give_up_console(const struct consw *csw)
3661{
3662 unregister_con_driver(csw);
3663}
3664
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003665static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003666{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003667 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003668
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003669 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3670 if (IS_ERR(vtconsole_class)) {
3671 printk(KERN_WARNING "Unable to create vt console class; "
3672 "errno = %ld\n", PTR_ERR(vtconsole_class));
3673 vtconsole_class = NULL;
3674 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003675
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003676 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003677 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3678 struct con_driver *con = &registered_con_driver[i];
3679
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003680 if (con->con && !con->dev) {
Alan Coxd81ed102008-10-13 10:41:42 +01003681 con->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003682 MKDEV(0, con->node),
3683 NULL, "vtcon%i",
3684 con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003685
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003686 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003687 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003688 "device for %s; errno = %ld\n",
3689 con->desc, PTR_ERR(con->dev));
3690 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003691 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003692 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003693 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003694 }
3695 }
3696
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003697 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003698}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003699postcore_initcall(vtconsole_class_init);
3700
3701#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003702
3703/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 * Screen blanking
3705 */
3706
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003707static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003709 unsigned int mode;
3710
3711 if (get_user(mode, p + 1))
3712 return -EFAULT;
3713
3714 vesa_blank_mode = (mode < 4) ? mode : 0;
3715 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716}
3717
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718void do_blank_screen(int entering_gfx)
3719{
3720 struct vc_data *vc = vc_cons[fg_console].d;
3721 int i;
3722
3723 WARN_CONSOLE_UNLOCKED();
3724
3725 if (console_blanked) {
3726 if (blank_state == blank_vesa_wait) {
3727 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003728 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 }
3730 return;
3731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732
3733 /* entering graphics mode? */
3734 if (entering_gfx) {
3735 hide_cursor(vc);
3736 save_screen(vc);
3737 vc->vc_sw->con_blank(vc, -1, 1);
3738 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003739 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 set_origin(vc);
3741 return;
3742 }
3743
izumib6e8f002007-07-17 04:05:49 -07003744 if (blank_state != blank_normal_wait)
3745 return;
3746 blank_state = blank_off;
3747
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 /* don't blank graphics */
3749 if (vc->vc_mode != KD_TEXT) {
3750 console_blanked = fg_console + 1;
3751 return;
3752 }
3753
3754 hide_cursor(vc);
3755 del_timer_sync(&console_timer);
3756 blank_timer_expired = 0;
3757
3758 save_screen(vc);
3759 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003760 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 console_blanked = fg_console + 1;
3762 if (i)
3763 set_origin(vc);
3764
3765 if (console_blank_hook && console_blank_hook(1))
3766 return;
3767
Ville Syrjalad060a322006-01-09 20:53:49 -08003768 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003769 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 mod_timer(&console_timer, jiffies + vesa_off_interval);
3771 }
Alan Cox8b92e872009-09-19 13:13:24 -07003772 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773}
3774EXPORT_SYMBOL(do_blank_screen);
3775
3776/*
3777 * Called by timer as well as from vt_console_driver
3778 */
3779void do_unblank_screen(int leaving_gfx)
3780{
3781 struct vc_data *vc;
3782
3783 /* This should now always be called from a "sane" (read: can schedule)
3784 * context for the sake of the low level drivers, except in the special
3785 * case of oops_in_progress
3786 */
3787 if (!oops_in_progress)
3788 might_sleep();
3789
3790 WARN_CONSOLE_UNLOCKED();
3791
3792 ignore_poke = 0;
3793 if (!console_blanked)
3794 return;
3795 if (!vc_cons_allocated(fg_console)) {
3796 /* impossible */
Mandeep Singh Baines1ffdda92011-02-06 09:31:53 -08003797 pr_warning("unblank_screen: tty %d not allocated ??\n",
3798 fg_console+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 return;
3800 }
3801 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003802 /* Try to unblank in oops case too */
3803 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 return; /* but leave console_blanked != 0 */
3805
3806 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003807 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 blank_state = blank_normal_wait;
3809 }
3810
3811 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003812 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 /* Low-level driver cannot restore -> do it ourselves */
3814 update_screen(vc);
3815 if (console_blank_hook)
3816 console_blank_hook(0);
3817 set_palette(vc);
3818 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003819 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820}
3821EXPORT_SYMBOL(do_unblank_screen);
3822
3823/*
3824 * This is called by the outside world to cause a forced unblank, mostly for
3825 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3826 * call it with 1 as an argument and so force a mode restore... that may kill
3827 * X or at least garbage the screen but would also make the Oops visible...
3828 */
3829void unblank_screen(void)
3830{
3831 do_unblank_screen(0);
3832}
3833
3834/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003835 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003837 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 */
3839static void blank_screen_t(unsigned long dummy)
3840{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003841 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003842 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003843 return;
3844 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 blank_timer_expired = 1;
3846 schedule_work(&console_work);
3847}
3848
3849void poke_blanked_console(void)
3850{
3851 WARN_CONSOLE_UNLOCKED();
3852
3853 /* Add this so we quickly catch whoever might call us in a non
3854 * safe context. Nowadays, unblank_screen() isn't to be called in
3855 * atomic contexts and is allowed to schedule (with the special case
3856 * of oops_in_progress, but that isn't of any concern for this
3857 * function. --BenH.
3858 */
3859 might_sleep();
3860
3861 /* This isn't perfectly race free, but a race here would be mostly harmless,
3862 * at worse, we'll do a spurrious blank and it's unlikely
3863 */
3864 del_timer(&console_timer);
3865 blank_timer_expired = 0;
3866
3867 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3868 return;
3869 if (console_blanked)
3870 unblank_screen();
3871 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003872 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 blank_state = blank_normal_wait;
3874 }
3875}
3876
3877/*
3878 * Palettes
3879 */
3880
3881static void set_palette(struct vc_data *vc)
3882{
3883 WARN_CONSOLE_UNLOCKED();
3884
3885 if (vc->vc_mode != KD_GRAPHICS)
3886 vc->vc_sw->con_set_palette(vc, color_table);
3887}
3888
3889static int set_get_cmap(unsigned char __user *arg, int set)
3890{
3891 int i, j, k;
3892
3893 WARN_CONSOLE_UNLOCKED();
3894
3895 for (i = 0; i < 16; i++)
3896 if (set) {
3897 get_user(default_red[i], arg++);
3898 get_user(default_grn[i], arg++);
3899 get_user(default_blu[i], arg++);
3900 } else {
3901 put_user(default_red[i], arg++);
3902 put_user(default_grn[i], arg++);
3903 put_user(default_blu[i], arg++);
3904 }
3905 if (set) {
3906 for (i = 0; i < MAX_NR_CONSOLES; i++)
3907 if (vc_cons_allocated(i)) {
3908 for (j = k = 0; j < 16; j++) {
3909 vc_cons[i].d->vc_palette[k++] = default_red[j];
3910 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3911 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3912 }
3913 set_palette(vc_cons[i].d);
3914 }
3915 }
3916 return 0;
3917}
3918
3919/*
3920 * Load palette into the DAC registers. arg points to a colour
3921 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3922 */
3923
3924int con_set_cmap(unsigned char __user *arg)
3925{
3926 int rc;
3927
Torben Hohnac751ef2011-01-25 15:07:35 -08003928 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 rc = set_get_cmap (arg,1);
Torben Hohnac751ef2011-01-25 15:07:35 -08003930 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
3932 return rc;
3933}
3934
3935int con_get_cmap(unsigned char __user *arg)
3936{
3937 int rc;
3938
Torben Hohnac751ef2011-01-25 15:07:35 -08003939 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 rc = set_get_cmap (arg,0);
Torben Hohnac751ef2011-01-25 15:07:35 -08003941 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 return rc;
3944}
3945
3946void reset_palette(struct vc_data *vc)
3947{
3948 int j, k;
3949 for (j=k=0; j<16; j++) {
3950 vc->vc_palette[k++] = default_red[j];
3951 vc->vc_palette[k++] = default_grn[j];
3952 vc->vc_palette[k++] = default_blu[j];
3953 }
3954 set_palette(vc);
3955}
3956
3957/*
3958 * Font switching
3959 *
3960 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3961 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3962 * depending on width) reserved for each character which is kinda wasty, but
3963 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003964 * is up to the actual low-level console-driver convert data into its favorite
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 * format (maybe we should add a `fontoffset' field to the `display'
3966 * structure so we won't have to convert the fontdata all the time.
3967 * /Jes
3968 */
3969
3970#define max_font_size 65536
3971
3972static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3973{
3974 struct console_font font;
3975 int rc = -EINVAL;
3976 int c;
3977
3978 if (vc->vc_mode != KD_TEXT)
3979 return -EINVAL;
3980
3981 if (op->data) {
3982 font.data = kmalloc(max_font_size, GFP_KERNEL);
3983 if (!font.data)
3984 return -ENOMEM;
3985 } else
3986 font.data = NULL;
3987
Torben Hohnac751ef2011-01-25 15:07:35 -08003988 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (vc->vc_sw->con_font_get)
3990 rc = vc->vc_sw->con_font_get(vc, &font);
3991 else
3992 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08003993 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994
3995 if (rc)
3996 goto out;
3997
3998 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07003999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 if (op->data && font.charcount > op->charcount)
4001 rc = -ENOSPC;
4002 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4003 if (font.width > op->width || font.height > op->height)
4004 rc = -ENOSPC;
4005 } else {
4006 if (font.width != 8)
4007 rc = -EIO;
4008 else if ((op->height && font.height > op->height) ||
4009 font.height > 32)
4010 rc = -ENOSPC;
4011 }
4012 if (rc)
4013 goto out;
4014
4015 op->height = font.height;
4016 op->width = font.width;
4017 op->charcount = font.charcount;
4018
4019 if (op->data && copy_to_user(op->data, font.data, c))
4020 rc = -EFAULT;
4021
4022out:
4023 kfree(font.data);
4024 return rc;
4025}
4026
4027static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4028{
4029 struct console_font font;
4030 int rc = -EINVAL;
4031 int size;
4032
4033 if (vc->vc_mode != KD_TEXT)
4034 return -EINVAL;
4035 if (!op->data)
4036 return -EINVAL;
4037 if (op->charcount > 512)
4038 return -EINVAL;
4039 if (!op->height) { /* Need to guess font height [compat] */
4040 int h, i;
4041 u8 __user *charmap = op->data;
4042 u8 tmp;
4043
4044 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4045 so that we can get rid of this soon */
4046 if (!(op->flags & KD_FONT_FLAG_OLD))
4047 return -EINVAL;
4048 for (h = 32; h > 0; h--)
4049 for (i = 0; i < op->charcount; i++) {
4050 if (get_user(tmp, &charmap[32*i+h-1]))
4051 return -EFAULT;
4052 if (tmp)
4053 goto nonzero;
4054 }
4055 return -EINVAL;
4056 nonzero:
4057 op->height = h;
4058 }
4059 if (op->width <= 0 || op->width > 32 || op->height > 32)
4060 return -EINVAL;
4061 size = (op->width+7)/8 * 32 * op->charcount;
4062 if (size > max_font_size)
4063 return -ENOSPC;
4064 font.charcount = op->charcount;
4065 font.height = op->height;
4066 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004067 font.data = memdup_user(op->data, size);
4068 if (IS_ERR(font.data))
4069 return PTR_ERR(font.data);
Torben Hohnac751ef2011-01-25 15:07:35 -08004070 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (vc->vc_sw->con_font_set)
4072 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4073 else
4074 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004075 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 kfree(font.data);
4077 return rc;
4078}
4079
4080static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4081{
4082 struct console_font font = {.width = op->width, .height = op->height};
4083 char name[MAX_FONT_NAME];
4084 char *s = name;
4085 int rc;
4086
4087 if (vc->vc_mode != KD_TEXT)
4088 return -EINVAL;
4089
4090 if (!op->data)
4091 s = NULL;
4092 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4093 return -EFAULT;
4094 else
4095 name[MAX_FONT_NAME - 1] = 0;
4096
Torben Hohnac751ef2011-01-25 15:07:35 -08004097 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (vc->vc_sw->con_font_default)
4099 rc = vc->vc_sw->con_font_default(vc, &font, s);
4100 else
4101 rc = -ENOSYS;
Torben Hohnac751ef2011-01-25 15:07:35 -08004102 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 if (!rc) {
4104 op->width = font.width;
4105 op->height = font.height;
4106 }
4107 return rc;
4108}
4109
4110static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4111{
4112 int con = op->height;
4113 int rc;
4114
4115 if (vc->vc_mode != KD_TEXT)
4116 return -EINVAL;
4117
Torben Hohnac751ef2011-01-25 15:07:35 -08004118 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 if (!vc->vc_sw->con_font_copy)
4120 rc = -ENOSYS;
4121 else if (con < 0 || !vc_cons_allocated(con))
4122 rc = -ENOTTY;
4123 else if (con == vc->vc_num) /* nothing to do */
4124 rc = 0;
4125 else
4126 rc = vc->vc_sw->con_font_copy(vc, con);
Torben Hohnac751ef2011-01-25 15:07:35 -08004127 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 return rc;
4129}
4130
4131int con_font_op(struct vc_data *vc, struct console_font_op *op)
4132{
4133 switch (op->op) {
4134 case KD_FONT_OP_SET:
4135 return con_font_set(vc, op);
4136 case KD_FONT_OP_GET:
4137 return con_font_get(vc, op);
4138 case KD_FONT_OP_SET_DEFAULT:
4139 return con_font_default(vc, op);
4140 case KD_FONT_OP_COPY:
4141 return con_font_copy(vc, op);
4142 }
4143 return -ENOSYS;
4144}
4145
4146/*
4147 * Interface exported to selection and vcs.
4148 */
4149
4150/* used by selection */
4151u16 screen_glyph(struct vc_data *vc, int offset)
4152{
4153 u16 w = scr_readw(screenpos(vc, offset, 1));
4154 u16 c = w & 0xff;
4155
4156 if (w & vc->vc_hi_font_mask)
4157 c |= 0x100;
4158 return c;
4159}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004160EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
4162/* used by vcs - note the word offset */
4163unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4164{
4165 return screenpos(vc, 2 * w_offset, viewed);
4166}
4167
4168void getconsxy(struct vc_data *vc, unsigned char *p)
4169{
4170 p[0] = vc->vc_x;
4171 p[1] = vc->vc_y;
4172}
4173
4174void putconsxy(struct vc_data *vc, unsigned char *p)
4175{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004176 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177 gotoxy(vc, p[0], p[1]);
4178 set_cursor(vc);
4179}
4180
4181u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4182{
4183 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4184 return softcursor_original;
4185 return scr_readw(org);
4186}
4187
4188void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4189{
4190 scr_writew(val, org);
4191 if ((unsigned long)org == vc->vc_pos) {
4192 softcursor_original = -1;
4193 add_softcursor(vc);
4194 }
4195}
4196
Nicolas Pitre432c9ed2010-10-01 00:10:44 -04004197void vcs_scr_updated(struct vc_data *vc)
4198{
4199 notify_update(vc);
4200}
4201
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202/*
4203 * Visible symbols for modules
4204 */
4205
4206EXPORT_SYMBOL(color_table);
4207EXPORT_SYMBOL(default_red);
4208EXPORT_SYMBOL(default_grn);
4209EXPORT_SYMBOL(default_blu);
4210EXPORT_SYMBOL(update_region);
4211EXPORT_SYMBOL(redraw_screen);
4212EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213EXPORT_SYMBOL(fg_console);
4214EXPORT_SYMBOL(console_blank_hook);
4215EXPORT_SYMBOL(console_blanked);
4216EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004217EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218#ifndef VT_SINGLE_DRIVER
4219EXPORT_SYMBOL(take_over_console);
4220EXPORT_SYMBOL(give_up_console);
4221#endif