blob: 82f64ac211911624df4cba181535d216b9529738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vt.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/*
8 * Hopefully this will be a rather complete VT102 implementation.
9 *
10 * Beeping thanks to John T Kohl.
11 *
12 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
13 * Chars, and VT100 enhancements by Peter MacDonald.
14 *
15 * Copy and paste function by Andrew Haylett,
16 * some enhancements by Alessandro Rubini.
17 *
18 * Code to check for different video-cards mostly by Galen Hunt,
19 * <g-hunt@ee.utah.edu>
20 *
21 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
22 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
23 *
24 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
25 * Resizing of consoles, aeb, 940926
26 *
27 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
28 * <poe@daimi.aau.dk>
29 *
30 * User-defined bell sound, new setterm control sequences and printk
31 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
32 *
33 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
34 *
35 * Merge with the abstract console driver by Geert Uytterhoeven
36 * <geert@linux-m68k.org>, Jan 1997.
37 *
38 * Original m68k console driver modifications by
39 *
40 * - Arno Griffioen <arno@usn.nl>
41 * - David Carter <carter@cs.bris.ac.uk>
42 *
43 * The abstract console driver provides a generic interface for a text
44 * console. It supports VGA text mode, frame buffer based graphical consoles
45 * and special graphics processors that are only accessible through some
46 * registers (e.g. a TMS340x0 GSP).
47 *
48 * The interface to the hardware is specified using a special structure
49 * (struct consw) which contains function pointers to console operations
50 * (see <linux/console.h> for more information).
51 *
52 * Support for changeable cursor shape
53 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
54 *
55 * Ported to i386 and con_scrolldelta fixed
56 * by Emmanuel Marty <core@ggi-project.org>, April 1998
57 *
58 * Resurrected character buffers in videoram plus lots of other trickery
59 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
60 *
61 * Removed old-style timers, introduced console_timer, made timer
Francois Camie1f8e872008-10-15 22:01:59 -070062 * deletion SMP-safe. 17Jun00, Andrew Morton
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * Removed console_lock, enabled interrupts across all console operations
65 * 13 March 2001, Andrew Morton
Adam Tlalkad4328b42006-09-29 01:59:53 -070066 *
67 * Fixed UTF-8 mode so alternate charset modes always work according
68 * to control sequences interpreted in do_con_trol function
69 * preserving backward VT100 semigraphics compatibility,
70 * malformed UTF sequences represented as sequences of replacement glyphs,
71 * original codes or '?' as a last resort if replacement glyph is undefined
72 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74
75#include <linux/module.h>
76#include <linux/types.h>
77#include <linux/sched.h>
78#include <linux/tty.h>
79#include <linux/tty_flip.h>
80#include <linux/kernel.h>
81#include <linux/string.h>
82#include <linux/errno.h>
83#include <linux/kd.h>
84#include <linux/slab.h>
85#include <linux/major.h>
86#include <linux/mm.h>
87#include <linux/console.h>
88#include <linux/init.h>
Matthias Kaehlckec831c332007-05-08 00:39:49 -070089#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070090#include <linux/vt_kern.h>
91#include <linux/selection.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040092#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/tiocl.h>
94#include <linux/kbd_kern.h>
95#include <linux/consolemap.h>
96#include <linux/timer.h>
97#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/pm.h>
100#include <linux/font.h>
101#include <linux/bitops.h>
Samuel Thibaultb293d752007-10-18 23:39:17 -0700102#include <linux/notifier.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100103#include <linux/device.h>
104#include <linux/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#include <asm/system.h>
Alan Coxd81ed102008-10-13 10:41:42 +0100106#include <linux/uaccess.h>
Jason Wessel81d44502010-08-05 09:22:30 -0500107#include <linux/kdb.h>
Andy Shevchenko74c807c2010-06-15 17:24:16 +0300108#include <linux/ctype.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700110#define MAX_NR_CON_DRIVER 16
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700112#define CON_DRIVER_FLAG_MODULE 1
Antonino A. Daplas928e9642006-10-03 01:14:49 -0700113#define CON_DRIVER_FLAG_INIT 2
114#define CON_DRIVER_FLAG_ATTR 4
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700115
116struct con_driver {
117 const struct consw *con;
118 const char *desc;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -0700119 struct device *dev;
Antonino A. Daplas6db40632006-06-26 00:27:12 -0700120 int node;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -0700121 int first;
122 int last;
123 int flag;
124};
125
126static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127const struct consw *conswitchp;
128
129/* A bitmap for codes <32. A bit of 1 indicates that the code
130 * corresponding to that bit number invokes some special action
131 * (such as cursor movement) and should not be displayed as a
132 * glyph unless the disp_ctrl mode is explicitly enabled.
133 */
134#define CTRL_ACTION 0x0d00ff81
135#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
136
137/*
138 * Here is the default bell parameters: 750HZ, 1/8th of a second
139 */
140#define DEFAULT_BELL_PITCH 750
141#define DEFAULT_BELL_DURATION (HZ/8)
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143struct vc vc_cons [MAX_NR_CONSOLES];
144
145#ifndef VT_SINGLE_DRIVER
146static const struct consw *con_driver_map[MAX_NR_CONSOLES];
147#endif
148
149static int con_open(struct tty_struct *, struct file *);
150static void vc_init(struct vc_data *vc, unsigned int rows,
151 unsigned int cols, int do_clear);
152static void gotoxy(struct vc_data *vc, int new_x, int new_y);
153static void save_cur(struct vc_data *vc);
154static void reset_terminal(struct vc_data *vc, int do_clear);
155static void con_flush_chars(struct tty_struct *tty);
Yoichi Yuasa403aac92006-12-06 20:38:38 -0800156static int set_vesa_blanking(char __user *p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static void set_cursor(struct vc_data *vc);
158static void hide_cursor(struct vc_data *vc);
David Howells65f27f32006-11-22 14:55:48 +0000159static void console_callback(struct work_struct *ignored);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static void blank_screen_t(unsigned long dummy);
161static void set_palette(struct vc_data *vc);
162
163static int printable; /* Is console ready for printing? */
Jan Engelhardt77bf2ba2007-10-18 03:04:34 -0700164int default_utf8 = true;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -0700165module_param(default_utf8, int, S_IRUGO | S_IWUSR);
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500166int global_cursor_default = -1;
167module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Clemens Ladisch9ea9a882009-12-15 16:45:39 -0800169static int cur_default = CUR_DEFAULT;
170module_param(cur_default, int, S_IRUGO | S_IWUSR);
171
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172/*
173 * ignore_poke: don't unblank the screen when things are typed. This is
174 * mainly for the privacy of braille terminal users.
175 */
176static int ignore_poke;
177
178int do_poke_blanked_console;
179int console_blanked;
180
181static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static int vesa_off_interval;
Daniel Mackf324edc2009-06-16 15:33:52 -0700183static int blankinterval = 10*60;
184core_param(consoleblank, blankinterval, int, 0444);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
David Howells65f27f32006-11-22 14:55:48 +0000186static DECLARE_WORK(console_work, console_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/*
189 * fg_console is the current virtual console,
190 * last_console is the last used one,
191 * want_console is the console we want to switch to,
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500192 * saved_* variants are for save/restore around kernel debugger enter/leave
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
194int fg_console;
195int last_console;
196int want_console = -1;
Jesse Barnesb45cfba2010-08-05 09:22:30 -0500197int saved_fg_console;
198int saved_last_console;
199int saved_want_console;
200int saved_vc_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202/*
203 * For each existing display, we have a pointer to console currently visible
204 * on that display, allowing consoles other than fg_console to be refreshed
205 * appropriately. Unless the low-level driver supplies its own display_fg
206 * variable, we use this one for the "master display".
207 */
208static struct vc_data *master_display_fg;
209
210/*
211 * Unfortunately, we need to delay tty echo when we're currently writing to the
212 * console since the code is (and always was) not re-entrant, so we schedule
213 * all flip requests to process context with schedule-task() and run it from
214 * console_callback().
215 */
216
217/*
218 * For the same reason, we defer scrollback to the console callback.
219 */
220static int scrollback_delta;
221
222/*
223 * Hook so that the power management routines can (un)blank
224 * the console on our behalf.
225 */
226int (*console_blank_hook)(int);
227
Jiri Slaby40565f12007-02-12 00:52:31 -0800228static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int blank_state;
230static int blank_timer_expired;
231enum {
232 blank_off = 0,
233 blank_normal_wait,
234 blank_vesa_wait,
235};
236
237/*
Samuel Thibaultb293d752007-10-18 23:39:17 -0700238 * Notifier list for console events.
239 */
240static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
241
242int register_vt_notifier(struct notifier_block *nb)
243{
244 return atomic_notifier_chain_register(&vt_notifier_list, nb);
245}
246EXPORT_SYMBOL_GPL(register_vt_notifier);
247
248int unregister_vt_notifier(struct notifier_block *nb)
249{
250 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
251}
252EXPORT_SYMBOL_GPL(unregister_vt_notifier);
253
254static void notify_write(struct vc_data *vc, unsigned int unicode)
255{
256 struct vt_notifier_param param = { .vc = vc, unicode = unicode };
257 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
258}
259
260static void notify_update(struct vc_data *vc)
261{
262 struct vt_notifier_param param = { .vc = vc };
263 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
264}
Samuel Thibaultb293d752007-10-18 23:39:17 -0700265/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 * Low-Level Functions
267 */
268
269#define IS_FG(vc) ((vc)->vc_num == fg_console)
270
271#ifdef VT_BUF_VRAM_ONLY
272#define DO_UPDATE(vc) 0
273#else
Stefano Stabellinif700d6e2008-07-23 21:29:59 -0700274#define DO_UPDATE(vc) (CON_IS_VISIBLE(vc) && !console_blanked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275#endif
276
277static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
278{
279 unsigned short *p;
280
281 if (!viewed)
282 p = (unsigned short *)(vc->vc_origin + offset);
283 else if (!vc->vc_sw->con_screen_pos)
284 p = (unsigned short *)(vc->vc_visible_origin + offset);
285 else
286 p = vc->vc_sw->con_screen_pos(vc, offset);
287 return p;
288}
289
290static inline void scrolldelta(int lines)
291{
292 scrollback_delta += lines;
293 schedule_console_callback();
294}
295
296void schedule_console_callback(void)
297{
298 schedule_work(&console_work);
299}
300
301static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
302{
303 unsigned short *d, *s;
304
305 if (t+nr >= b)
306 nr = b - t - 1;
307 if (b > vc->vc_rows || t >= b || nr < 1)
308 return;
309 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
310 return;
311 d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
312 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
313 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds386f40c2010-06-06 20:44:04 -0700314 scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 vc->vc_size_row * nr);
316}
317
318static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
319{
320 unsigned short *s;
321 unsigned int step;
322
323 if (t+nr >= b)
324 nr = b - t - 1;
325 if (b > vc->vc_rows || t >= b || nr < 1)
326 return;
327 if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
328 return;
329 s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
330 step = vc->vc_cols * nr;
331 scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
Linus Torvalds93f78da2008-10-14 12:12:02 -0700332 scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335static void do_update_region(struct vc_data *vc, unsigned long start, int count)
336{
337#ifndef VT_BUF_VRAM_ONLY
338 unsigned int xx, yy, offset;
339 u16 *p;
340
341 p = (u16 *) start;
342 if (!vc->vc_sw->con_getxy) {
343 offset = (start - vc->vc_origin) / 2;
344 xx = offset % vc->vc_cols;
345 yy = offset / vc->vc_cols;
346 } else {
347 int nxx, nyy;
348 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
349 xx = nxx; yy = nyy;
350 }
351 for(;;) {
352 u16 attrib = scr_readw(p) & 0xff00;
353 int startx = xx;
354 u16 *q = p;
355 while (xx < vc->vc_cols && count) {
356 if (attrib != (scr_readw(p) & 0xff00)) {
357 if (p > q)
358 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
359 startx = xx;
360 q = p;
361 attrib = scr_readw(p) & 0xff00;
362 }
363 p++;
364 xx++;
365 count--;
366 }
367 if (p > q)
368 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
369 if (!count)
370 break;
371 xx = 0;
372 yy++;
373 if (vc->vc_sw->con_getxy) {
374 p = (u16 *)start;
375 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
376 }
377 }
378#endif
379}
380
381void update_region(struct vc_data *vc, unsigned long start, int count)
382{
383 WARN_CONSOLE_UNLOCKED();
384
385 if (DO_UPDATE(vc)) {
386 hide_cursor(vc);
387 do_update_region(vc, start, count);
388 set_cursor(vc);
389 }
390}
391
392/* Structure of attributes is hardware-dependent */
393
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700394static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
395 u8 _underline, u8 _reverse, u8 _italic)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
397 if (vc->vc_sw->con_build_attr)
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700398 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
399 _blink, _underline, _reverse, _italic);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401#ifndef VT_BUF_VRAM_ONLY
402/*
403 * ++roman: I completely changed the attribute format for monochrome
404 * mode (!can_do_color). The formerly used MDA (monochrome display
405 * adapter) format didn't allow the combination of certain effects.
406 * Now the attribute is just a bit vector:
407 * Bit 0..1: intensity (0..2)
408 * Bit 2 : underline
409 * Bit 3 : reverse
410 * Bit 7 : blink
411 */
412 {
Jan Engelhardtc9e587ab2008-04-29 00:59:46 -0700413 u8 a = _color;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (!vc->vc_can_do_color)
415 return _intensity |
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700416 (_italic ? 2 : 0) |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 (_underline ? 4 : 0) |
418 (_reverse ? 8 : 0) |
419 (_blink ? 0x80 : 0);
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700420 if (_italic)
421 a = (a & 0xF0) | vc->vc_itcolor;
422 else if (_underline)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 a = (a & 0xf0) | vc->vc_ulcolor;
424 else if (_intensity == 0)
425 a = (a & 0xf0) | vc->vc_ulcolor;
426 if (_reverse)
427 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
428 if (_blink)
429 a ^= 0x80;
430 if (_intensity == 2)
431 a ^= 0x08;
432 if (vc->vc_hi_font_mask == 0x100)
433 a <<= 1;
434 return a;
435 }
436#else
437 return 0;
438#endif
439}
440
441static void update_attr(struct vc_data *vc)
442{
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -0700443 vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity,
444 vc->vc_blink, vc->vc_underline,
445 vc->vc_reverse ^ vc->vc_decscnm, vc->vc_italic);
446 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 -0700447}
448
449/* Note: inverting the screen twice should revert to the original state */
450void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
451{
452 unsigned short *p;
453
454 WARN_CONSOLE_UNLOCKED();
455
456 count /= 2;
457 p = screenpos(vc, offset, viewed);
458 if (vc->vc_sw->con_invert_region)
459 vc->vc_sw->con_invert_region(vc, p, count);
460#ifndef VT_BUF_VRAM_ONLY
461 else {
462 u16 *q = p;
463 int cnt = count;
464 u16 a;
465
466 if (!vc->vc_can_do_color) {
467 while (cnt--) {
468 a = scr_readw(q);
469 a ^= 0x0800;
470 scr_writew(a, q);
471 q++;
472 }
473 } else if (vc->vc_hi_font_mask == 0x100) {
474 while (cnt--) {
475 a = scr_readw(q);
476 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
477 scr_writew(a, q);
478 q++;
479 }
480 } else {
481 while (cnt--) {
482 a = scr_readw(q);
483 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
484 scr_writew(a, q);
485 q++;
486 }
487 }
488 }
489#endif
490 if (DO_UPDATE(vc))
491 do_update_region(vc, (unsigned long) p, count);
492}
493
494/* used by selection: complement pointer position */
495void complement_pos(struct vc_data *vc, int offset)
496{
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700497 static int old_offset = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 static unsigned short old;
499 static unsigned short oldx, oldy;
500
501 WARN_CONSOLE_UNLOCKED();
502
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700503 if (old_offset != -1 && old_offset >= 0 &&
504 old_offset < vc->vc_screenbuf_size) {
505 scr_writew(old, screenpos(vc, old_offset, 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 if (DO_UPDATE(vc))
507 vc->vc_sw->con_putc(vc, old, oldy, oldx);
508 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700509
510 old_offset = offset;
511
512 if (offset != -1 && offset >= 0 &&
513 offset < vc->vc_screenbuf_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 unsigned short new;
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700515 unsigned short *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 p = screenpos(vc, offset, 1);
517 old = scr_readw(p);
518 new = old ^ vc->vc_complement_mask;
519 scr_writew(new, p);
520 if (DO_UPDATE(vc)) {
521 oldx = (offset >> 1) % vc->vc_cols;
522 oldy = (offset >> 1) / vc->vc_cols;
523 vc->vc_sw->con_putc(vc, new, oldy, oldx);
524 }
525 }
Antonino A. Daplas414edcd2005-09-06 15:17:52 -0700526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529static void insert_char(struct vc_data *vc, unsigned int nr)
530{
531 unsigned short *p, *q = (unsigned short *)vc->vc_pos;
532
533 p = q + vc->vc_cols - nr - vc->vc_x;
534 while (--p >= q)
535 scr_writew(scr_readw(p), p + nr);
536 scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
537 vc->vc_need_wrap = 0;
538 if (DO_UPDATE(vc)) {
539 unsigned short oldattr = vc->vc_attr;
540 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
541 vc->vc_cols - vc->vc_x - nr);
542 vc->vc_attr = vc->vc_video_erase_char >> 8;
543 while (nr--)
544 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
545 vc->vc_attr = oldattr;
546 }
547}
548
549static void delete_char(struct vc_data *vc, unsigned int nr)
550{
551 unsigned int i = vc->vc_x;
552 unsigned short *p = (unsigned short *)vc->vc_pos;
553
554 while (++i <= vc->vc_cols - nr) {
555 scr_writew(scr_readw(p+nr), p);
556 p++;
557 }
558 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
559 vc->vc_need_wrap = 0;
560 if (DO_UPDATE(vc)) {
561 unsigned short oldattr = vc->vc_attr;
562 vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
563 vc->vc_cols - vc->vc_x - nr);
564 vc->vc_attr = vc->vc_video_erase_char >> 8;
565 while (nr--)
566 vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
567 vc->vc_cols - 1 - nr);
568 vc->vc_attr = oldattr;
569 }
570}
571
572static int softcursor_original;
573
574static void add_softcursor(struct vc_data *vc)
575{
576 int i = scr_readw((u16 *) vc->vc_pos);
577 u32 type = vc->vc_cursor_type;
578
579 if (! (type & 0x10)) return;
580 if (softcursor_original != -1) return;
581 softcursor_original = i;
582 i |= ((type >> 8) & 0xff00 );
583 i ^= ((type) & 0xff00 );
584 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
585 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
586 scr_writew(i, (u16 *) vc->vc_pos);
587 if (DO_UPDATE(vc))
588 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
589}
590
591static void hide_softcursor(struct vc_data *vc)
592{
593 if (softcursor_original != -1) {
594 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
595 if (DO_UPDATE(vc))
596 vc->vc_sw->con_putc(vc, softcursor_original,
597 vc->vc_y, vc->vc_x);
598 softcursor_original = -1;
599 }
600}
601
602static void hide_cursor(struct vc_data *vc)
603{
604 if (vc == sel_cons)
605 clear_selection();
606 vc->vc_sw->con_cursor(vc, CM_ERASE);
607 hide_softcursor(vc);
608}
609
610static void set_cursor(struct vc_data *vc)
611{
612 if (!IS_FG(vc) || console_blanked ||
613 vc->vc_mode == KD_GRAPHICS)
614 return;
615 if (vc->vc_deccm) {
616 if (vc == sel_cons)
617 clear_selection();
618 add_softcursor(vc);
619 if ((vc->vc_cursor_type & 0x0f) != 1)
620 vc->vc_sw->con_cursor(vc, CM_DRAW);
621 } else
622 hide_cursor(vc);
623}
624
625static void set_origin(struct vc_data *vc)
626{
627 WARN_CONSOLE_UNLOCKED();
628
629 if (!CON_IS_VISIBLE(vc) ||
630 !vc->vc_sw->con_set_origin ||
631 !vc->vc_sw->con_set_origin(vc))
632 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
633 vc->vc_visible_origin = vc->vc_origin;
634 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
635 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
636}
637
638static inline void save_screen(struct vc_data *vc)
639{
640 WARN_CONSOLE_UNLOCKED();
641
642 if (vc->vc_sw->con_save_screen)
643 vc->vc_sw->con_save_screen(vc);
644}
645
646/*
647 * Redrawing of screen
648 */
649
650static void clear_buffer_attributes(struct vc_data *vc)
651{
652 unsigned short *p = (unsigned short *)vc->vc_origin;
653 int count = vc->vc_screenbuf_size / 2;
654 int mask = vc->vc_hi_font_mask | 0xff;
655
656 for (; count > 0; count--, p++) {
657 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
658 }
659}
660
661void redraw_screen(struct vc_data *vc, int is_switch)
662{
663 int redraw = 0;
664
665 WARN_CONSOLE_UNLOCKED();
666
667 if (!vc) {
668 /* strange ... */
669 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
670 return;
671 }
672
673 if (is_switch) {
674 struct vc_data *old_vc = vc_cons[fg_console].d;
675 if (old_vc == vc)
676 return;
677 if (!CON_IS_VISIBLE(vc))
678 redraw = 1;
679 *vc->vc_display_fg = vc;
680 fg_console = vc->vc_num;
681 hide_cursor(old_vc);
682 if (!CON_IS_VISIBLE(old_vc)) {
683 save_screen(old_vc);
684 set_origin(old_vc);
685 }
686 } else {
687 hide_cursor(vc);
688 redraw = 1;
689 }
690
691 if (redraw) {
692 int update;
693 int old_was_color = vc->vc_can_do_color;
694
695 set_origin(vc);
696 update = vc->vc_sw->con_switch(vc);
697 set_palette(vc);
698 /*
699 * If console changed from mono<->color, the best we can do
700 * is to clear the buffer attributes. As it currently stands,
701 * rebuilding new attributes from the old buffer is not doable
702 * without overly complex code.
703 */
704 if (old_was_color != vc->vc_can_do_color) {
705 update_attr(vc);
706 clear_buffer_attributes(vc);
707 }
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700708
709 /* Forcibly update if we're panicing */
710 if ((update && vc->vc_mode != KD_GRAPHICS) ||
711 vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
713 }
714 set_cursor(vc);
715 if (is_switch) {
716 set_leds();
717 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800718 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720}
721
722/*
723 * Allocation, freeing and resizing of VTs.
724 */
725
726int vc_cons_allocated(unsigned int i)
727{
728 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
729}
730
731static void visual_init(struct vc_data *vc, int num, int init)
732{
733 /* ++Geert: vc->vc_sw->con_init determines console size */
734 if (vc->vc_sw)
735 module_put(vc->vc_sw->owner);
736 vc->vc_sw = conswitchp;
737#ifndef VT_SINGLE_DRIVER
738 if (con_driver_map[num])
739 vc->vc_sw = con_driver_map[num];
740#endif
741 __module_get(vc->vc_sw->owner);
742 vc->vc_num = num;
743 vc->vc_display_fg = &master_display_fg;
744 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
745 vc->vc_uni_pagedir = 0;
746 vc->vc_hi_font_mask = 0;
747 vc->vc_complement_mask = 0;
748 vc->vc_can_do_color = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -0700749 vc->vc_panic_force_write = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 vc->vc_sw->con_init(vc, init);
751 if (!vc->vc_complement_mask)
752 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
753 vc->vc_s_complement_mask = vc->vc_complement_mask;
754 vc->vc_size_row = vc->vc_cols << 1;
755 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
756}
757
758int vc_allocate(unsigned int currcons) /* return 0 on success */
759{
760 WARN_CONSOLE_UNLOCKED();
761
762 if (currcons >= MAX_NR_CONSOLES)
763 return -ENXIO;
764 if (!vc_cons[currcons].d) {
765 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700766 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* prevent users from taking too much memory */
769 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
770 return -EPERM;
771
772 /* due to the granularity of kmalloc, we waste some memory here */
773 /* the alloc is done in two steps, to optimize the common situation
774 of a 25x80 console (structsize=216, screenbuf_size=4000) */
775 /* although the numbers above are not valid since long ago, the
776 point is still up-to-date and the comment still has its value
777 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700778 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 if (!vc)
780 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 vc_cons[currcons].d = vc;
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800782 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 visual_init(vc, currcons, 1);
784 if (!*vc->vc_uni_pagedir_loc)
785 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100786 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!vc->vc_screenbuf) {
788 kfree(vc);
789 vc_cons[currcons].d = NULL;
790 return -ENOMEM;
791 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500792
793 /* If no drivers have overridden us and the user didn't pass a
794 boot option, default to displaying the cursor */
795 if (global_cursor_default == -1)
796 global_cursor_default = 1;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100799 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700800 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 return 0;
803}
804
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700805static inline int resize_screen(struct vc_data *vc, int width, int height,
806 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 /* Resizes the resolution of the display adapater */
809 int err = 0;
810
811 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700812 err = vc->vc_sw->con_resize(vc, width, height, user);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return err;
815}
816
817/*
818 * Change # of rows and columns (0 means unchanged/the size of fg_console)
819 * [this is to be used together with some user program
820 * like resize that changes the hardware videomode]
821 */
822#define VC_RESIZE_MAXCOL (32767)
823#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100824
825/**
826 * vc_do_resize - resizing method for the tty
827 * @tty: tty being resized
828 * @real_tty: real tty (different to tty if a pty/tty pair)
829 * @vc: virtual console private data
830 * @cols: columns
831 * @lines: lines
832 *
833 * Resize a virtual console, clipping according to the actual constraints.
834 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3f2010-02-03 08:01:28 +0800835 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100836 *
837 * Caller must hold the console semaphore. Takes the termios mutex and
838 * ctrl_lock of the tty IFF a tty is passed.
839 */
840
Alan Coxfc6f6232009-01-02 13:43:17 +0000841static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
842 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700845 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
847 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700848 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 unsigned short *newscreen;
850
851 WARN_CONSOLE_UNLOCKED();
852
853 if (!vc)
854 return -ENXIO;
855
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700856 user = vc->vc_resize_user;
857 vc->vc_resize_user = 0;
858
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
860 return -EINVAL;
861
862 new_cols = (cols ? cols : vc->vc_cols);
863 new_rows = (lines ? lines : vc->vc_rows);
864 new_row_size = new_cols << 1;
865 new_screen_size = new_row_size * new_rows;
866
867 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
868 return 0;
869
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800870 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (!newscreen)
872 return -ENOMEM;
873
874 old_rows = vc->vc_rows;
875 old_cols = vc->vc_cols;
876 old_row_size = vc->vc_size_row;
877 old_screen_size = vc->vc_screenbuf_size;
878
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700879 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (err) {
881 kfree(newscreen);
882 return err;
883 }
884
885 vc->vc_rows = new_rows;
886 vc->vc_cols = new_cols;
887 vc->vc_size_row = new_row_size;
888 vc->vc_screenbuf_size = new_screen_size;
889
890 rlth = min(old_row_size, new_row_size);
891 rrem = new_row_size - rlth;
892 old_origin = vc->vc_origin;
893 new_origin = (long) newscreen;
894 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700895
896 if (vc->vc_y > new_rows) {
897 if (old_rows - vc->vc_y < new_rows) {
898 /*
899 * Cursor near the bottom, copy contents from the
900 * bottom of buffer
901 */
902 old_origin += (old_rows - new_rows) * old_row_size;
903 end = vc->vc_scr_end;
904 } else {
905 /*
906 * Cursor is in no man's land, copy 1/2 screenful
907 * from the top and bottom of cursor position
908 */
909 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800910 end = old_origin + (old_row_size * new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700911 }
912 } else
913 /*
914 * Cursor near the top, copy contents from the top of buffer
915 */
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800916 end = (old_rows > new_rows) ? old_origin +
917 (old_row_size * new_rows) :
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700918 vc->vc_scr_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 update_attr(vc);
921
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 while (old_origin < end) {
923 scr_memcpyw((unsigned short *) new_origin,
924 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700926 scr_memsetw((void *)(new_origin + rlth),
927 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 old_origin += old_row_size;
929 new_origin += new_row_size;
930 }
931 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700932 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
933 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100934 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 vc->vc_screenbuf_size = new_screen_size;
937 set_origin(vc);
938
939 /* do part of a reset_terminal() */
940 vc->vc_top = 0;
941 vc->vc_bottom = vc->vc_rows;
942 gotoxy(vc, vc->vc_x, vc->vc_y);
943 save_cur(vc);
944
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100945 if (tty) {
946 /* Rewrite the requested winsize data with the actual
947 resulting sizes */
948 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 memset(&ws, 0, sizeof(ws));
950 ws.ws_row = vc->vc_rows;
951 ws.ws_col = vc->vc_cols;
952 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000953 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 }
955
956 if (CON_IS_VISIBLE(vc))
957 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700958 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 return err;
960}
961
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100962/**
963 * vc_resize - resize a VT
964 * @vc: virtual console
965 * @cols: columns
966 * @rows: rows
967 *
968 * Resize a virtual console as seen from the console end of things. We
969 * use the common vc_do_resize methods to update the structures. The
970 * caller must hold the console sem to protect console internals and
971 * vc->vc_tty
972 */
973
974int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700975{
Alan Coxfc6f6232009-01-02 13:43:17 +0000976 return vc_do_resize(vc->vc_tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100977}
978
979/**
980 * vt_resize - resize a VT
981 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100982 * @ws: winsize attributes
983 *
984 * Resize a virtual terminal. This is called by the tty layer as we
985 * register our own handler for resizing. The mutual helper does all
986 * the actual work.
987 *
988 * Takes the console sem and the called methods then take the tty
989 * termios_mutex and the tty ctrl_lock in that order.
990 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800991static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100992{
993 struct vc_data *vc = tty->driver_data;
994 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
Alan Coxca9bda02006-09-29 02:00:03 -0700996 acquire_console_sem();
Alan Coxfc6f6232009-01-02 13:43:17 +0000997 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Alan Coxca9bda02006-09-29 02:00:03 -0700998 release_console_sem();
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100999 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -07001000}
1001
1002void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
1004 WARN_CONSOLE_UNLOCKED();
1005
1006 if (vc_cons_allocated(currcons)) {
1007 struct vc_data *vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001008 struct vt_notifier_param param = { .vc = vc };
Kay Sievers4995f8e2009-03-09 14:18:52 +01001009
Samuel Thibaultb293d752007-10-18 23:39:17 -07001010 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001011 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001013 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001014 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001015 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 if (currcons >= MIN_NR_CONSOLES)
1017 kfree(vc);
1018 vc_cons[currcons].d = NULL;
1019 }
1020}
1021
1022/*
1023 * VT102 emulator
1024 */
1025
1026#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1027#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1028#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1029
1030#define decarm VC_REPEAT
1031#define decckm VC_CKMODE
1032#define kbdapplic VC_APPLIC
1033#define lnm VC_CRLF
1034
1035/*
1036 * this is what the terminal answers to a ESC-Z or csi0c query.
1037 */
1038#define VT100ID "\033[?1;2c"
1039#define VT102ID "\033[?6c"
1040
1041unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1042 8,12,10,14, 9,13,11,15 };
1043
1044/* the default colour table, for VGA+ colour systems */
1045int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1046 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1047int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1048 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1049int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1050 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1051
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001052module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1053module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1054module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056/*
1057 * gotoxy() must verify all boundaries, because the arguments
1058 * might also be negative. If the given position is out of
1059 * bounds, the cursor is placed at the nearest margin.
1060 */
1061static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1062{
1063 int min_y, max_y;
1064
1065 if (new_x < 0)
1066 vc->vc_x = 0;
1067 else {
1068 if (new_x >= vc->vc_cols)
1069 vc->vc_x = vc->vc_cols - 1;
1070 else
1071 vc->vc_x = new_x;
1072 }
1073
1074 if (vc->vc_decom) {
1075 min_y = vc->vc_top;
1076 max_y = vc->vc_bottom;
1077 } else {
1078 min_y = 0;
1079 max_y = vc->vc_rows;
1080 }
1081 if (new_y < min_y)
1082 vc->vc_y = min_y;
1083 else if (new_y >= max_y)
1084 vc->vc_y = max_y - 1;
1085 else
1086 vc->vc_y = new_y;
1087 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1088 vc->vc_need_wrap = 0;
1089}
1090
1091/* for absolute user moves, when decom is set */
1092static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1093{
1094 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1095}
1096
1097void scrollback(struct vc_data *vc, int lines)
1098{
1099 if (!lines)
1100 lines = vc->vc_rows / 2;
1101 scrolldelta(-lines);
1102}
1103
1104void scrollfront(struct vc_data *vc, int lines)
1105{
1106 if (!lines)
1107 lines = vc->vc_rows / 2;
1108 scrolldelta(lines);
1109}
1110
1111static void lf(struct vc_data *vc)
1112{
1113 /* don't scroll if above bottom of scrolling region, or
1114 * if below scrolling region
1115 */
1116 if (vc->vc_y + 1 == vc->vc_bottom)
1117 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1118 else if (vc->vc_y < vc->vc_rows - 1) {
1119 vc->vc_y++;
1120 vc->vc_pos += vc->vc_size_row;
1121 }
1122 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001123 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124}
1125
1126static void ri(struct vc_data *vc)
1127{
1128 /* don't scroll if below top of scrolling region, or
1129 * if above scrolling region
1130 */
1131 if (vc->vc_y == vc->vc_top)
1132 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1133 else if (vc->vc_y > 0) {
1134 vc->vc_y--;
1135 vc->vc_pos -= vc->vc_size_row;
1136 }
1137 vc->vc_need_wrap = 0;
1138}
1139
1140static inline void cr(struct vc_data *vc)
1141{
1142 vc->vc_pos -= vc->vc_x << 1;
1143 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001144 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
1147static inline void bs(struct vc_data *vc)
1148{
1149 if (vc->vc_x) {
1150 vc->vc_pos -= 2;
1151 vc->vc_x--;
1152 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001153 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155}
1156
1157static inline void del(struct vc_data *vc)
1158{
1159 /* ignored */
1160}
1161
1162static void csi_J(struct vc_data *vc, int vpar)
1163{
1164 unsigned int count;
1165 unsigned short * start;
1166
1167 switch (vpar) {
1168 case 0: /* erase from cursor to end of display */
1169 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1170 start = (unsigned short *)vc->vc_pos;
1171 if (DO_UPDATE(vc)) {
1172 /* do in two stages */
1173 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1174 vc->vc_cols - vc->vc_x);
1175 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1176 vc->vc_rows - vc->vc_y - 1,
1177 vc->vc_cols);
1178 }
1179 break;
1180 case 1: /* erase from start to cursor */
1181 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1182 start = (unsigned short *)vc->vc_origin;
1183 if (DO_UPDATE(vc)) {
1184 /* do in two stages */
1185 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1186 vc->vc_cols);
1187 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1188 vc->vc_x + 1);
1189 }
1190 break;
1191 case 2: /* erase whole display */
1192 count = vc->vc_cols * vc->vc_rows;
1193 start = (unsigned short *)vc->vc_origin;
1194 if (DO_UPDATE(vc))
1195 vc->vc_sw->con_clear(vc, 0, 0,
1196 vc->vc_rows,
1197 vc->vc_cols);
1198 break;
1199 default:
1200 return;
1201 }
1202 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1203 vc->vc_need_wrap = 0;
1204}
1205
1206static void csi_K(struct vc_data *vc, int vpar)
1207{
1208 unsigned int count;
1209 unsigned short * start;
1210
1211 switch (vpar) {
1212 case 0: /* erase from cursor to end of line */
1213 count = vc->vc_cols - vc->vc_x;
1214 start = (unsigned short *)vc->vc_pos;
1215 if (DO_UPDATE(vc))
1216 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1217 vc->vc_cols - vc->vc_x);
1218 break;
1219 case 1: /* erase from start of line to cursor */
1220 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1221 count = vc->vc_x + 1;
1222 if (DO_UPDATE(vc))
1223 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1224 vc->vc_x + 1);
1225 break;
1226 case 2: /* erase whole line */
1227 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1228 count = vc->vc_cols;
1229 if (DO_UPDATE(vc))
1230 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1231 vc->vc_cols);
1232 break;
1233 default:
1234 return;
1235 }
1236 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1237 vc->vc_need_wrap = 0;
1238}
1239
1240static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1241{ /* not vt100? */
1242 int count;
1243
1244 if (!vpar)
1245 vpar++;
1246 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1247
1248 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1249 if (DO_UPDATE(vc))
1250 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1251 vc->vc_need_wrap = 0;
1252}
1253
1254static void default_attr(struct vc_data *vc)
1255{
1256 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001257 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 vc->vc_underline = 0;
1259 vc->vc_reverse = 0;
1260 vc->vc_blink = 0;
1261 vc->vc_color = vc->vc_def_color;
1262}
1263
1264/* console_sem is held */
1265static void csi_m(struct vc_data *vc)
1266{
1267 int i;
1268
1269 for (i = 0; i <= vc->vc_npar; i++)
1270 switch (vc->vc_par[i]) {
1271 case 0: /* all attributes off */
1272 default_attr(vc);
1273 break;
1274 case 1:
1275 vc->vc_intensity = 2;
1276 break;
1277 case 2:
1278 vc->vc_intensity = 0;
1279 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001280 case 3:
1281 vc->vc_italic = 1;
1282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case 4:
1284 vc->vc_underline = 1;
1285 break;
1286 case 5:
1287 vc->vc_blink = 1;
1288 break;
1289 case 7:
1290 vc->vc_reverse = 1;
1291 break;
1292 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1293 * Select primary font, don't display
1294 * control chars if defined, don't set
1295 * bit 8 on output.
1296 */
1297 vc->vc_translate = set_translate(vc->vc_charset == 0
1298 ? vc->vc_G0_charset
1299 : vc->vc_G1_charset, vc);
1300 vc->vc_disp_ctrl = 0;
1301 vc->vc_toggle_meta = 0;
1302 break;
1303 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1304 * Select first alternate font, lets
1305 * chars < 32 be displayed as ROM chars.
1306 */
1307 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1308 vc->vc_disp_ctrl = 1;
1309 vc->vc_toggle_meta = 0;
1310 break;
1311 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1312 * Select second alternate font, toggle
1313 * high bit before displaying as ROM char.
1314 */
1315 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1316 vc->vc_disp_ctrl = 1;
1317 vc->vc_toggle_meta = 1;
1318 break;
1319 case 21:
1320 case 22:
1321 vc->vc_intensity = 1;
1322 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001323 case 23:
1324 vc->vc_italic = 0;
1325 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 case 24:
1327 vc->vc_underline = 0;
1328 break;
1329 case 25:
1330 vc->vc_blink = 0;
1331 break;
1332 case 27:
1333 vc->vc_reverse = 0;
1334 break;
1335 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1336 * Enables underscore, white foreground
1337 * with white underscore (Linux - use
1338 * default foreground).
1339 */
1340 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1341 vc->vc_underline = 1;
1342 break;
1343 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1344 * Disable underline option.
1345 * Reset colour to default? It did this
1346 * before...
1347 */
1348 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1349 vc->vc_underline = 0;
1350 break;
1351 case 49:
1352 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1353 break;
1354 default:
1355 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1356 vc->vc_color = color_table[vc->vc_par[i] - 30]
1357 | (vc->vc_color & 0xf0);
1358 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1359 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1360 | (vc->vc_color & 0x0f);
1361 break;
1362 }
1363 update_attr(vc);
1364}
1365
1366static void respond_string(const char *p, struct tty_struct *tty)
1367{
1368 while (*p) {
1369 tty_insert_flip_char(tty, *p, 0);
1370 p++;
1371 }
1372 con_schedule_flip(tty);
1373}
1374
1375static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1376{
1377 char buf[40];
1378
1379 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1380 respond_string(buf, tty);
1381}
1382
1383static inline void status_report(struct tty_struct *tty)
1384{
1385 respond_string("\033[0n", tty); /* Terminal ok */
1386}
1387
1388static inline void respond_ID(struct tty_struct * tty)
1389{
1390 respond_string(VT102ID, tty);
1391}
1392
1393void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1394{
1395 char buf[8];
1396
1397 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1398 (char)('!' + mry));
1399 respond_string(buf, tty);
1400}
1401
1402/* invoked via ioctl(TIOCLINUX) and through set_selection */
1403int mouse_reporting(void)
1404{
1405 return vc_cons[fg_console].d->vc_report_mouse;
1406}
1407
1408/* console_sem is held */
1409static void set_mode(struct vc_data *vc, int on_off)
1410{
1411 int i;
1412
1413 for (i = 0; i <= vc->vc_npar; i++)
1414 if (vc->vc_ques) {
1415 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1416 case 1: /* Cursor keys send ^[Ox/^[[x */
1417 if (on_off)
1418 set_kbd(vc, decckm);
1419 else
1420 clr_kbd(vc, decckm);
1421 break;
1422 case 3: /* 80/132 mode switch unimplemented */
1423 vc->vc_deccolm = on_off;
1424#if 0
1425 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1426 /* this alone does not suffice; some user mode
1427 utility has to change the hardware regs */
1428#endif
1429 break;
1430 case 5: /* Inverted screen on/off */
1431 if (vc->vc_decscnm != on_off) {
1432 vc->vc_decscnm = on_off;
1433 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1434 update_attr(vc);
1435 }
1436 break;
1437 case 6: /* Origin relative/absolute */
1438 vc->vc_decom = on_off;
1439 gotoxay(vc, 0, 0);
1440 break;
1441 case 7: /* Autowrap on/off */
1442 vc->vc_decawm = on_off;
1443 break;
1444 case 8: /* Autorepeat on/off */
1445 if (on_off)
1446 set_kbd(vc, decarm);
1447 else
1448 clr_kbd(vc, decarm);
1449 break;
1450 case 9:
1451 vc->vc_report_mouse = on_off ? 1 : 0;
1452 break;
1453 case 25: /* Cursor on/off */
1454 vc->vc_deccm = on_off;
1455 break;
1456 case 1000:
1457 vc->vc_report_mouse = on_off ? 2 : 0;
1458 break;
1459 }
1460 } else {
1461 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1462 case 3: /* Monitor (display ctrls) */
1463 vc->vc_disp_ctrl = on_off;
1464 break;
1465 case 4: /* Insert Mode on/off */
1466 vc->vc_decim = on_off;
1467 break;
1468 case 20: /* Lf, Enter == CrLf/Lf */
1469 if (on_off)
1470 set_kbd(vc, lnm);
1471 else
1472 clr_kbd(vc, lnm);
1473 break;
1474 }
1475 }
1476}
1477
1478/* console_sem is held */
1479static void setterm_command(struct vc_data *vc)
1480{
1481 switch(vc->vc_par[0]) {
1482 case 1: /* set color for underline mode */
1483 if (vc->vc_can_do_color &&
1484 vc->vc_par[1] < 16) {
1485 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1486 if (vc->vc_underline)
1487 update_attr(vc);
1488 }
1489 break;
1490 case 2: /* set color for half intensity mode */
1491 if (vc->vc_can_do_color &&
1492 vc->vc_par[1] < 16) {
1493 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1494 if (vc->vc_intensity == 0)
1495 update_attr(vc);
1496 }
1497 break;
1498 case 8: /* store colors as defaults */
1499 vc->vc_def_color = vc->vc_attr;
1500 if (vc->vc_hi_font_mask == 0x100)
1501 vc->vc_def_color >>= 1;
1502 default_attr(vc);
1503 update_attr(vc);
1504 break;
1505 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001506 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 poke_blanked_console();
1508 break;
1509 case 10: /* set bell frequency in Hz */
1510 if (vc->vc_npar >= 1)
1511 vc->vc_bell_pitch = vc->vc_par[1];
1512 else
1513 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1514 break;
1515 case 11: /* set bell duration in msec */
1516 if (vc->vc_npar >= 1)
1517 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1518 vc->vc_par[1] * HZ / 1000 : 0;
1519 else
1520 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1521 break;
1522 case 12: /* bring specified console to the front */
1523 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1524 set_console(vc->vc_par[1] - 1);
1525 break;
1526 case 13: /* unblank the screen */
1527 poke_blanked_console();
1528 break;
1529 case 14: /* set vesa powerdown interval */
1530 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1531 break;
1532 case 15: /* activate the previous console */
1533 set_console(last_console);
1534 break;
1535 }
1536}
1537
1538/* console_sem is held */
1539static void csi_at(struct vc_data *vc, unsigned int nr)
1540{
1541 if (nr > vc->vc_cols - vc->vc_x)
1542 nr = vc->vc_cols - vc->vc_x;
1543 else if (!nr)
1544 nr = 1;
1545 insert_char(vc, nr);
1546}
1547
1548/* console_sem is held */
1549static void csi_L(struct vc_data *vc, unsigned int nr)
1550{
1551 if (nr > vc->vc_rows - vc->vc_y)
1552 nr = vc->vc_rows - vc->vc_y;
1553 else if (!nr)
1554 nr = 1;
1555 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1556 vc->vc_need_wrap = 0;
1557}
1558
1559/* console_sem is held */
1560static void csi_P(struct vc_data *vc, unsigned int nr)
1561{
1562 if (nr > vc->vc_cols - vc->vc_x)
1563 nr = vc->vc_cols - vc->vc_x;
1564 else if (!nr)
1565 nr = 1;
1566 delete_char(vc, nr);
1567}
1568
1569/* console_sem is held */
1570static void csi_M(struct vc_data *vc, unsigned int nr)
1571{
1572 if (nr > vc->vc_rows - vc->vc_y)
1573 nr = vc->vc_rows - vc->vc_y;
1574 else if (!nr)
1575 nr=1;
1576 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1577 vc->vc_need_wrap = 0;
1578}
1579
1580/* console_sem is held (except via vc_init->reset_terminal */
1581static void save_cur(struct vc_data *vc)
1582{
1583 vc->vc_saved_x = vc->vc_x;
1584 vc->vc_saved_y = vc->vc_y;
1585 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001586 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 vc->vc_s_underline = vc->vc_underline;
1588 vc->vc_s_blink = vc->vc_blink;
1589 vc->vc_s_reverse = vc->vc_reverse;
1590 vc->vc_s_charset = vc->vc_charset;
1591 vc->vc_s_color = vc->vc_color;
1592 vc->vc_saved_G0 = vc->vc_G0_charset;
1593 vc->vc_saved_G1 = vc->vc_G1_charset;
1594}
1595
1596/* console_sem is held */
1597static void restore_cur(struct vc_data *vc)
1598{
1599 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1600 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001601 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 vc->vc_underline = vc->vc_s_underline;
1603 vc->vc_blink = vc->vc_s_blink;
1604 vc->vc_reverse = vc->vc_s_reverse;
1605 vc->vc_charset = vc->vc_s_charset;
1606 vc->vc_color = vc->vc_s_color;
1607 vc->vc_G0_charset = vc->vc_saved_G0;
1608 vc->vc_G1_charset = vc->vc_saved_G1;
1609 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1610 update_attr(vc);
1611 vc->vc_need_wrap = 0;
1612}
1613
1614enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1615 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1616 ESpalette };
1617
1618/* console_sem is held (except via vc_init()) */
1619static void reset_terminal(struct vc_data *vc, int do_clear)
1620{
1621 vc->vc_top = 0;
1622 vc->vc_bottom = vc->vc_rows;
1623 vc->vc_state = ESnormal;
1624 vc->vc_ques = 0;
1625 vc->vc_translate = set_translate(LAT1_MAP, vc);
1626 vc->vc_G0_charset = LAT1_MAP;
1627 vc->vc_G1_charset = GRAF_MAP;
1628 vc->vc_charset = 0;
1629 vc->vc_need_wrap = 0;
1630 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001631 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 vc->vc_utf_count = 0;
1633
1634 vc->vc_disp_ctrl = 0;
1635 vc->vc_toggle_meta = 0;
1636
1637 vc->vc_decscnm = 0;
1638 vc->vc_decom = 0;
1639 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001640 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 vc->vc_decim = 0;
1642
1643 set_kbd(vc, decarm);
1644 clr_kbd(vc, decckm);
1645 clr_kbd(vc, kbdapplic);
1646 clr_kbd(vc, lnm);
1647 kbd_table[vc->vc_num].lockstate = 0;
1648 kbd_table[vc->vc_num].slockstate = 0;
1649 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1650 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1651 /* do not do set_leds here because this causes an endless tasklet loop
1652 when the keyboard hasn't been initialized yet */
1653
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001654 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 vc->vc_complement_mask = vc->vc_s_complement_mask;
1656
1657 default_attr(vc);
1658 update_attr(vc);
1659
1660 vc->vc_tab_stop[0] = 0x01010100;
1661 vc->vc_tab_stop[1] =
1662 vc->vc_tab_stop[2] =
1663 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001664 vc->vc_tab_stop[4] =
1665 vc->vc_tab_stop[5] =
1666 vc->vc_tab_stop[6] =
1667 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1670 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1671
1672 gotoxy(vc, 0, 0);
1673 save_cur(vc);
1674 if (do_clear)
1675 csi_J(vc, 2);
1676}
1677
1678/* console_sem is held */
1679static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1680{
1681 /*
1682 * Control characters can be used in the _middle_
1683 * of an escape sequence.
1684 */
1685 switch (c) {
1686 case 0:
1687 return;
1688 case 7:
1689 if (vc->vc_bell_duration)
1690 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1691 return;
1692 case 8:
1693 bs(vc);
1694 return;
1695 case 9:
1696 vc->vc_pos -= (vc->vc_x << 1);
1697 while (vc->vc_x < vc->vc_cols - 1) {
1698 vc->vc_x++;
1699 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1700 break;
1701 }
1702 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001703 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return;
1705 case 10: case 11: case 12:
1706 lf(vc);
1707 if (!is_kbd(vc, lnm))
1708 return;
1709 case 13:
1710 cr(vc);
1711 return;
1712 case 14:
1713 vc->vc_charset = 1;
1714 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1715 vc->vc_disp_ctrl = 1;
1716 return;
1717 case 15:
1718 vc->vc_charset = 0;
1719 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1720 vc->vc_disp_ctrl = 0;
1721 return;
1722 case 24: case 26:
1723 vc->vc_state = ESnormal;
1724 return;
1725 case 27:
1726 vc->vc_state = ESesc;
1727 return;
1728 case 127:
1729 del(vc);
1730 return;
1731 case 128+27:
1732 vc->vc_state = ESsquare;
1733 return;
1734 }
1735 switch(vc->vc_state) {
1736 case ESesc:
1737 vc->vc_state = ESnormal;
1738 switch (c) {
1739 case '[':
1740 vc->vc_state = ESsquare;
1741 return;
1742 case ']':
1743 vc->vc_state = ESnonstd;
1744 return;
1745 case '%':
1746 vc->vc_state = ESpercent;
1747 return;
1748 case 'E':
1749 cr(vc);
1750 lf(vc);
1751 return;
1752 case 'M':
1753 ri(vc);
1754 return;
1755 case 'D':
1756 lf(vc);
1757 return;
1758 case 'H':
1759 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1760 return;
1761 case 'Z':
1762 respond_ID(tty);
1763 return;
1764 case '7':
1765 save_cur(vc);
1766 return;
1767 case '8':
1768 restore_cur(vc);
1769 return;
1770 case '(':
1771 vc->vc_state = ESsetG0;
1772 return;
1773 case ')':
1774 vc->vc_state = ESsetG1;
1775 return;
1776 case '#':
1777 vc->vc_state = EShash;
1778 return;
1779 case 'c':
1780 reset_terminal(vc, 1);
1781 return;
1782 case '>': /* Numeric keypad */
1783 clr_kbd(vc, kbdapplic);
1784 return;
1785 case '=': /* Appl. keypad */
1786 set_kbd(vc, kbdapplic);
1787 return;
1788 }
1789 return;
1790 case ESnonstd:
1791 if (c=='P') { /* palette escape sequence */
1792 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1793 vc->vc_par[vc->vc_npar] = 0;
1794 vc->vc_npar = 0;
1795 vc->vc_state = ESpalette;
1796 return;
1797 } else if (c=='R') { /* reset palette */
1798 reset_palette(vc);
1799 vc->vc_state = ESnormal;
1800 } else
1801 vc->vc_state = ESnormal;
1802 return;
1803 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001804 if (isxdigit(c)) {
1805 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 if (vc->vc_npar == 7) {
1807 int i = vc->vc_par[0] * 3, j = 1;
1808 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1809 vc->vc_palette[i++] += vc->vc_par[j++];
1810 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1811 vc->vc_palette[i++] += vc->vc_par[j++];
1812 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1813 vc->vc_palette[i] += vc->vc_par[j];
1814 set_palette(vc);
1815 vc->vc_state = ESnormal;
1816 }
1817 } else
1818 vc->vc_state = ESnormal;
1819 return;
1820 case ESsquare:
1821 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1822 vc->vc_par[vc->vc_npar] = 0;
1823 vc->vc_npar = 0;
1824 vc->vc_state = ESgetpars;
1825 if (c == '[') { /* Function key */
1826 vc->vc_state=ESfunckey;
1827 return;
1828 }
1829 vc->vc_ques = (c == '?');
1830 if (vc->vc_ques)
1831 return;
1832 case ESgetpars:
1833 if (c == ';' && vc->vc_npar < NPAR - 1) {
1834 vc->vc_npar++;
1835 return;
1836 } else if (c>='0' && c<='9') {
1837 vc->vc_par[vc->vc_npar] *= 10;
1838 vc->vc_par[vc->vc_npar] += c - '0';
1839 return;
1840 } else
1841 vc->vc_state = ESgotpars;
1842 case ESgotpars:
1843 vc->vc_state = ESnormal;
1844 switch(c) {
1845 case 'h':
1846 set_mode(vc, 1);
1847 return;
1848 case 'l':
1849 set_mode(vc, 0);
1850 return;
1851 case 'c':
1852 if (vc->vc_ques) {
1853 if (vc->vc_par[0])
1854 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1855 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001856 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 return;
1858 }
1859 break;
1860 case 'm':
1861 if (vc->vc_ques) {
1862 clear_selection();
1863 if (vc->vc_par[0])
1864 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1865 else
1866 vc->vc_complement_mask = vc->vc_s_complement_mask;
1867 return;
1868 }
1869 break;
1870 case 'n':
1871 if (!vc->vc_ques) {
1872 if (vc->vc_par[0] == 5)
1873 status_report(tty);
1874 else if (vc->vc_par[0] == 6)
1875 cursor_report(vc, tty);
1876 }
1877 return;
1878 }
1879 if (vc->vc_ques) {
1880 vc->vc_ques = 0;
1881 return;
1882 }
1883 switch(c) {
1884 case 'G': case '`':
1885 if (vc->vc_par[0])
1886 vc->vc_par[0]--;
1887 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1888 return;
1889 case 'A':
1890 if (!vc->vc_par[0])
1891 vc->vc_par[0]++;
1892 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1893 return;
1894 case 'B': case 'e':
1895 if (!vc->vc_par[0])
1896 vc->vc_par[0]++;
1897 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1898 return;
1899 case 'C': case 'a':
1900 if (!vc->vc_par[0])
1901 vc->vc_par[0]++;
1902 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1903 return;
1904 case 'D':
1905 if (!vc->vc_par[0])
1906 vc->vc_par[0]++;
1907 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1908 return;
1909 case 'E':
1910 if (!vc->vc_par[0])
1911 vc->vc_par[0]++;
1912 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1913 return;
1914 case 'F':
1915 if (!vc->vc_par[0])
1916 vc->vc_par[0]++;
1917 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1918 return;
1919 case 'd':
1920 if (vc->vc_par[0])
1921 vc->vc_par[0]--;
1922 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1923 return;
1924 case 'H': case 'f':
1925 if (vc->vc_par[0])
1926 vc->vc_par[0]--;
1927 if (vc->vc_par[1])
1928 vc->vc_par[1]--;
1929 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1930 return;
1931 case 'J':
1932 csi_J(vc, vc->vc_par[0]);
1933 return;
1934 case 'K':
1935 csi_K(vc, vc->vc_par[0]);
1936 return;
1937 case 'L':
1938 csi_L(vc, vc->vc_par[0]);
1939 return;
1940 case 'M':
1941 csi_M(vc, vc->vc_par[0]);
1942 return;
1943 case 'P':
1944 csi_P(vc, vc->vc_par[0]);
1945 return;
1946 case 'c':
1947 if (!vc->vc_par[0])
1948 respond_ID(tty);
1949 return;
1950 case 'g':
1951 if (!vc->vc_par[0])
1952 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1953 else if (vc->vc_par[0] == 3) {
1954 vc->vc_tab_stop[0] =
1955 vc->vc_tab_stop[1] =
1956 vc->vc_tab_stop[2] =
1957 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001958 vc->vc_tab_stop[4] =
1959 vc->vc_tab_stop[5] =
1960 vc->vc_tab_stop[6] =
1961 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
1963 return;
1964 case 'm':
1965 csi_m(vc);
1966 return;
1967 case 'q': /* DECLL - but only 3 leds */
1968 /* map 0,1,2,3 to 0,1,2,4 */
1969 if (vc->vc_par[0] < 4)
1970 setledstate(kbd_table + vc->vc_num,
1971 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1972 return;
1973 case 'r':
1974 if (!vc->vc_par[0])
1975 vc->vc_par[0]++;
1976 if (!vc->vc_par[1])
1977 vc->vc_par[1] = vc->vc_rows;
1978 /* Minimum allowed region is 2 lines */
1979 if (vc->vc_par[0] < vc->vc_par[1] &&
1980 vc->vc_par[1] <= vc->vc_rows) {
1981 vc->vc_top = vc->vc_par[0] - 1;
1982 vc->vc_bottom = vc->vc_par[1];
1983 gotoxay(vc, 0, 0);
1984 }
1985 return;
1986 case 's':
1987 save_cur(vc);
1988 return;
1989 case 'u':
1990 restore_cur(vc);
1991 return;
1992 case 'X':
1993 csi_X(vc, vc->vc_par[0]);
1994 return;
1995 case '@':
1996 csi_at(vc, vc->vc_par[0]);
1997 return;
1998 case ']': /* setterm functions */
1999 setterm_command(vc);
2000 return;
2001 }
2002 return;
2003 case ESpercent:
2004 vc->vc_state = ESnormal;
2005 switch (c) {
2006 case '@': /* defined in ISO 2022 */
2007 vc->vc_utf = 0;
2008 return;
2009 case 'G': /* prelim official escape code */
2010 case '8': /* retained for compatibility */
2011 vc->vc_utf = 1;
2012 return;
2013 }
2014 return;
2015 case ESfunckey:
2016 vc->vc_state = ESnormal;
2017 return;
2018 case EShash:
2019 vc->vc_state = ESnormal;
2020 if (c == '8') {
2021 /* DEC screen alignment test. kludge :-) */
2022 vc->vc_video_erase_char =
2023 (vc->vc_video_erase_char & 0xff00) | 'E';
2024 csi_J(vc, 2);
2025 vc->vc_video_erase_char =
2026 (vc->vc_video_erase_char & 0xff00) | ' ';
2027 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2028 }
2029 return;
2030 case ESsetG0:
2031 if (c == '0')
2032 vc->vc_G0_charset = GRAF_MAP;
2033 else if (c == 'B')
2034 vc->vc_G0_charset = LAT1_MAP;
2035 else if (c == 'U')
2036 vc->vc_G0_charset = IBMPC_MAP;
2037 else if (c == 'K')
2038 vc->vc_G0_charset = USER_MAP;
2039 if (vc->vc_charset == 0)
2040 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2041 vc->vc_state = ESnormal;
2042 return;
2043 case ESsetG1:
2044 if (c == '0')
2045 vc->vc_G1_charset = GRAF_MAP;
2046 else if (c == 'B')
2047 vc->vc_G1_charset = LAT1_MAP;
2048 else if (c == 'U')
2049 vc->vc_G1_charset = IBMPC_MAP;
2050 else if (c == 'K')
2051 vc->vc_G1_charset = USER_MAP;
2052 if (vc->vc_charset == 1)
2053 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2054 vc->vc_state = ESnormal;
2055 return;
2056 default:
2057 vc->vc_state = ESnormal;
2058 }
2059}
2060
2061/* This is a temporary buffer used to prepare a tty console write
2062 * so that we can easily avoid touching user space while holding the
2063 * console spinlock. It is allocated in con_init and is shared by
2064 * this code and the vc_screen read/write tty calls.
2065 *
2066 * We have to allocate this statically in the kernel data section
2067 * since console_init (and thus con_init) are called before any
2068 * kernel memory allocation is available.
2069 */
2070char con_buf[CON_BUF_SIZE];
Matthias Kaehlckec831c332007-05-08 00:39:49 -07002071DEFINE_MUTEX(con_buf_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002073/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002074 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002075 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2076 */
2077struct interval {
2078 uint32_t first;
2079 uint32_t last;
2080};
2081
2082static int bisearch(uint32_t ucs, const struct interval *table, int max)
2083{
2084 int min = 0;
2085 int mid;
2086
2087 if (ucs < table[0].first || ucs > table[max].last)
2088 return 0;
2089 while (max >= min) {
2090 mid = (min + max) / 2;
2091 if (ucs > table[mid].last)
2092 min = mid + 1;
2093 else if (ucs < table[mid].first)
2094 max = mid - 1;
2095 else
2096 return 1;
2097 }
2098 return 0;
2099}
2100
2101static int is_double_width(uint32_t ucs)
2102{
2103 static const struct interval double_width[] = {
2104 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2105 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002106 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2107 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002108 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002109 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002110}
2111
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112/* acquires console_sem */
2113static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2114{
2115#ifdef VT_BUF_VRAM_ONLY
2116#define FLUSH do { } while(0);
2117#else
2118#define FLUSH if (draw_x >= 0) { \
2119 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2120 draw_x = -1; \
2121 }
2122#endif
2123
2124 int c, tc, ok, n = 0, draw_x = -1;
2125 unsigned int currcons;
2126 unsigned long draw_from = 0, draw_to = 0;
2127 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002128 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002129 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002130 uint8_t rescan;
2131 uint8_t inverse;
2132 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
2135 if (in_interrupt())
2136 return count;
2137
2138 might_sleep();
2139
2140 acquire_console_sem();
2141 vc = tty->driver_data;
2142 if (vc == NULL) {
2143 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2144 release_console_sem();
2145 return 0;
2146 }
2147
2148 currcons = vc->vc_num;
2149 if (!vc_cons_allocated(currcons)) {
2150 /* could this happen? */
Marcin Slusarz9074d962009-08-09 21:54:03 +02002151 printk_once("con_write: tty %d not allocated\n", currcons+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 release_console_sem();
2153 return 0;
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 himask = vc->vc_hi_font_mask;
2157 charmask = himask ? 0x1ff : 0xff;
2158
2159 /* undraw cursor first */
2160 if (IS_FG(vc))
2161 hide_cursor(vc);
2162
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002163 param.vc = vc;
2164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 while (!tty->stopped && count) {
2166 int orig = *buf;
2167 c = orig;
2168 buf++;
2169 n++;
2170 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002171 rescan = 0;
2172 inverse = 0;
2173 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
2175 /* Do no translation at all in control states */
2176 if (vc->vc_state != ESnormal) {
2177 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002178 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002179 /* Combine UTF-8 into Unicode in vc_utf_char.
2180 * vc_utf_count is the number of continuation bytes still
2181 * expected to arrive.
2182 * vc_npar is the number of continuation bytes arrived so
2183 * far
2184 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002185rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002186 if ((c & 0xc0) == 0x80) {
2187 /* Continuation byte received */
2188 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002189 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002190 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2191 vc->vc_npar++;
2192 if (--vc->vc_utf_count) {
2193 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002195 }
2196 /* Got a whole character */
2197 c = vc->vc_utf_char;
2198 /* Reject overlong sequences */
2199 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2200 c > utf8_length_changes[vc->vc_npar])
2201 c = 0xfffd;
2202 } else {
2203 /* Unexpected continuation byte */
2204 vc->vc_utf_count = 0;
2205 c = 0xfffd;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002208 /* Single ASCII byte or first byte of a sequence received */
2209 if (vc->vc_utf_count) {
2210 /* Continuation byte expected */
2211 rescan = 1;
2212 vc->vc_utf_count = 0;
2213 c = 0xfffd;
2214 } else if (c > 0x7f) {
2215 /* First byte of a multibyte sequence received */
2216 vc->vc_npar = 0;
2217 if ((c & 0xe0) == 0xc0) {
2218 vc->vc_utf_count = 1;
2219 vc->vc_utf_char = (c & 0x1f);
2220 } else if ((c & 0xf0) == 0xe0) {
2221 vc->vc_utf_count = 2;
2222 vc->vc_utf_char = (c & 0x0f);
2223 } else if ((c & 0xf8) == 0xf0) {
2224 vc->vc_utf_count = 3;
2225 vc->vc_utf_char = (c & 0x07);
2226 } else if ((c & 0xfc) == 0xf8) {
2227 vc->vc_utf_count = 4;
2228 vc->vc_utf_char = (c & 0x03);
2229 } else if ((c & 0xfe) == 0xfc) {
2230 vc->vc_utf_count = 5;
2231 vc->vc_utf_char = (c & 0x01);
2232 } else {
2233 /* 254 and 255 are invalid */
2234 c = 0xfffd;
2235 }
2236 if (vc->vc_utf_count) {
2237 /* Still need some bytes */
2238 continue;
2239 }
2240 }
2241 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002243 /* End of UTF-8 decoding. */
2244 /* c is the received character, or U+FFFD for invalid sequences. */
2245 /* Replace invalid Unicode code points with U+FFFD too */
2246 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2247 c = 0xfffd;
2248 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002249 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002250 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002253 param.c = tc;
2254 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2255 &param) == NOTIFY_STOP)
2256 continue;
2257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /* If the original code was a control character we
2259 * only allow a glyph to be displayed if the code is
2260 * not normally used (such as for cursor movement) or
2261 * if the disp_ctrl mode has been explicitly enabled.
2262 * Certain characters (as given by the CTRL_ALWAYS
2263 * bitmap) are always displayed as control characters,
2264 * as the console would be pretty useless without
2265 * them; to display an arbitrary font position use the
2266 * direct-to-font zone in UTF-8 mode.
2267 */
2268 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002269 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2270 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 && (c != 127 || vc->vc_disp_ctrl)
2272 && (c != 128+27);
2273
2274 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002275 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2276 if (is_double_width(c))
2277 width = 2;
2278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 /* Now try to find out how to display it */
2280 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002281 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002282 if (tc == -1 || tc == -2) {
2283 continue; /* nothing to display */
2284 }
2285 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002286 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002287 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002288 This would make absolutely no sense with Unicode in mind,
2289 but do this for ASCII characters since a font may lack
2290 Unicode mapping info and we don't want to end up with
2291 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002292 tc = c;
2293 } else {
2294 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2295 tc = conv_uni_to_pc(vc, 0xfffd);
2296 if (tc < 0) {
2297 inverse = 1;
2298 tc = conv_uni_to_pc(vc, '?');
2299 if (tc < 0) tc = '?';
2300 }
2301 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002304 if (!inverse) {
2305 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002307 /* invert vc_attr */
2308 if (!vc->vc_can_do_color) {
2309 vc_attr = (vc->vc_attr) ^ 0x08;
2310 } else if (vc->vc_hi_font_mask == 0x100) {
2311 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2312 } else {
2313 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002314 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002315 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002316 }
2317
2318 while (1) {
2319 if (vc->vc_need_wrap || vc->vc_decim)
2320 FLUSH
2321 if (vc->vc_need_wrap) {
2322 cr(vc);
2323 lf(vc);
2324 }
2325 if (vc->vc_decim)
2326 insert_char(vc, 1);
2327 scr_writew(himask ?
2328 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2329 (vc_attr << 8) + tc,
2330 (u16 *) vc->vc_pos);
2331 if (DO_UPDATE(vc) && draw_x < 0) {
2332 draw_x = vc->vc_x;
2333 draw_from = vc->vc_pos;
2334 }
2335 if (vc->vc_x == vc->vc_cols - 1) {
2336 vc->vc_need_wrap = vc->vc_decawm;
2337 draw_to = vc->vc_pos + 2;
2338 } else {
2339 vc->vc_x++;
2340 draw_to = (vc->vc_pos += 2);
2341 }
2342
2343 if (!--width) break;
2344
2345 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2346 if (tc < 0) tc = ' ';
2347 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002348 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002349
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002350 if (inverse) {
2351 FLUSH
2352 }
2353
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002354 if (rescan) {
2355 rescan = 0;
2356 inverse = 0;
2357 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002358 c = orig;
2359 goto rescan_last_byte;
2360 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 continue;
2362 }
2363 FLUSH
2364 do_con_trol(tty, vc, orig);
2365 }
2366 FLUSH
2367 console_conditional_schedule();
2368 release_console_sem();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002369 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 return n;
2371#undef FLUSH
2372}
2373
2374/*
2375 * This is the console switching callback.
2376 *
2377 * Doing console switching in a process context allows
2378 * us to do the switches asynchronously (needed when we want
2379 * to switch due to a keyboard interrupt). Synchronization
2380 * with other console code and prevention of re-entrancy is
2381 * ensured with console_sem.
2382 */
David Howells65f27f32006-11-22 14:55:48 +00002383static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
2385 acquire_console_sem();
2386
2387 if (want_console >= 0) {
2388 if (want_console != fg_console &&
2389 vc_cons_allocated(want_console)) {
2390 hide_cursor(vc_cons[fg_console].d);
2391 change_console(vc_cons[want_console].d);
2392 /* we only changed when the console had already
2393 been allocated - a new console is not created
2394 in an interrupt routine */
2395 }
2396 want_console = -1;
2397 }
2398 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2399 do_poke_blanked_console = 0;
2400 poke_blanked_console();
2401 }
2402 if (scrollback_delta) {
2403 struct vc_data *vc = vc_cons[fg_console].d;
2404 clear_selection();
2405 if (vc->vc_mode == KD_TEXT)
2406 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2407 scrollback_delta = 0;
2408 }
2409 if (blank_timer_expired) {
2410 do_blank_screen(0);
2411 blank_timer_expired = 0;
2412 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002413 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
2415 release_console_sem();
2416}
2417
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002418int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002420 struct vc_data *vc = vc_cons[fg_console].d;
2421
2422 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2423 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2424
2425 /*
2426 * Console switch will fail in console_callback() or
2427 * change_console() so there is no point scheduling
2428 * the callback
2429 *
2430 * Existing set_console() users don't check the return
2431 * value so this shouldn't break anything
2432 */
2433 return -EINVAL;
2434 }
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 want_console = nr;
2437 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002438
2439 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440}
2441
2442struct tty_driver *console_driver;
2443
2444#ifdef CONFIG_VT_CONSOLE
2445
Bernhard Walle5ada9182009-12-14 18:00:43 -08002446/**
2447 * vt_kmsg_redirect() - Sets/gets the kernel message console
2448 * @new: The new virtual terminal number or -1 if the console should stay
2449 * unchanged
2450 *
2451 * By default, the kernel messages are always printed on the current virtual
2452 * console. However, the user may modify that default with the
2453 * TIOCL_SETKMSGREDIRECT ioctl call.
2454 *
2455 * This function sets the kernel message console to be @new. It returns the old
2456 * virtual console number. The virtual terminal number 0 (both as parameter and
2457 * return value) means no redirection (i.e. always printed on the currently
2458 * active console).
2459 *
2460 * The parameter -1 means that only the current console is returned, but the
2461 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2462 * case to make the code more understandable.
2463 *
2464 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2465 * the parameter and always returns 0.
2466 */
2467int vt_kmsg_redirect(int new)
2468{
2469 static int kmsg_con;
2470
2471 if (new != -1)
2472 return xchg(&kmsg_con, new);
2473 else
2474 return kmsg_con;
2475}
2476
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477/*
2478 * Console on virtual terminal
2479 *
2480 * The console must be locked when we get here.
2481 */
2482
2483static void vt_console_print(struct console *co, const char *b, unsigned count)
2484{
2485 struct vc_data *vc = vc_cons[fg_console].d;
2486 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002487 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 const ushort *start;
2489 ushort cnt = 0;
2490 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002491 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
2493 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002494 if (!printable)
2495 return;
2496 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 return;
2498
Bernhard Walle5ada9182009-12-14 18:00:43 -08002499 kmsg_console = vt_get_kmsg_redirect();
2500 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2501 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 /* read `x' only after setting currcons properly (otherwise
2504 the `x' macro will read the x of the foreground console). */
2505 myx = vc->vc_x;
2506
2507 if (!vc_cons_allocated(fg_console)) {
2508 /* impossible */
2509 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2510 goto quit;
2511 }
2512
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07002513 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 goto quit;
2515
2516 /* undraw cursor first */
2517 if (IS_FG(vc))
2518 hide_cursor(vc);
2519
2520 start = (ushort *)vc->vc_pos;
2521
2522 /* Contrived structure to try to emulate original need_wrap behaviour
2523 * Problems caused when we have need_wrap set on '\n' character */
2524 while (count--) {
2525 c = *b++;
2526 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2527 if (cnt > 0) {
2528 if (CON_IS_VISIBLE(vc))
2529 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2530 vc->vc_x += cnt;
2531 if (vc->vc_need_wrap)
2532 vc->vc_x--;
2533 cnt = 0;
2534 }
2535 if (c == 8) { /* backspace */
2536 bs(vc);
2537 start = (ushort *)vc->vc_pos;
2538 myx = vc->vc_x;
2539 continue;
2540 }
2541 if (c != 13)
2542 lf(vc);
2543 cr(vc);
2544 start = (ushort *)vc->vc_pos;
2545 myx = vc->vc_x;
2546 if (c == 10 || c == 13)
2547 continue;
2548 }
2549 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002550 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 cnt++;
2552 if (myx == vc->vc_cols - 1) {
2553 vc->vc_need_wrap = 1;
2554 continue;
2555 }
2556 vc->vc_pos += 2;
2557 myx++;
2558 }
2559 if (cnt > 0) {
2560 if (CON_IS_VISIBLE(vc))
2561 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2562 vc->vc_x += cnt;
2563 if (vc->vc_x == vc->vc_cols) {
2564 vc->vc_x--;
2565 vc->vc_need_wrap = 1;
2566 }
2567 }
2568 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002569 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002572 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
2575static struct tty_driver *vt_console_device(struct console *c, int *index)
2576{
2577 *index = c->index ? c->index-1 : fg_console;
2578 return console_driver;
2579}
2580
2581static struct console vt_console_driver = {
2582 .name = "tty",
2583 .write = vt_console_print,
2584 .device = vt_console_device,
2585 .unblank = unblank_screen,
2586 .flags = CON_PRINTBUFFER,
2587 .index = -1,
2588};
2589#endif
2590
2591/*
2592 * Handling of Linux-specific VC ioctls
2593 */
2594
2595/*
2596 * Generally a bit racy with respect to console_sem().
2597 *
2598 * There are some functions which don't need it.
2599 *
2600 * There are some functions which can sleep for arbitrary periods
2601 * (paste_selection) but we don't need the lock there anyway.
2602 *
2603 * set_selection has locking, and definitely needs it
2604 */
2605
2606int tioclinux(struct tty_struct *tty, unsigned long arg)
2607{
2608 char type, data;
2609 char __user *p = (char __user *)arg;
2610 int lines;
2611 int ret;
2612
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2614 return -EPERM;
2615 if (get_user(type, p))
2616 return -EFAULT;
2617 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002618
2619 lock_kernel();
2620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 switch (type)
2622 {
2623 case TIOCL_SETSEL:
2624 acquire_console_sem();
2625 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2626 release_console_sem();
2627 break;
2628 case TIOCL_PASTESEL:
2629 ret = paste_selection(tty);
2630 break;
2631 case TIOCL_UNBLANKSCREEN:
Stephane Doyon2d237c62005-09-06 15:17:31 -07002632 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 unblank_screen();
Stephane Doyon2d237c62005-09-06 15:17:31 -07002634 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 break;
2636 case TIOCL_SELLOADLUT:
2637 ret = sel_loadlut(p);
2638 break;
2639 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 /*
2642 * Make it possible to react to Shift+Mousebutton.
2643 * Note that 'shift_state' is an undocumented
2644 * kernel-internal variable; programs not closely
2645 * related to the kernel should not use this.
2646 */
2647 data = shift_state;
2648 ret = __put_user(data, p);
2649 break;
2650 case TIOCL_GETMOUSEREPORTING:
2651 data = mouse_reporting();
2652 ret = __put_user(data, p);
2653 break;
2654 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002655 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002657 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002658 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002659 ret = __put_user(data, p);
2660 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 case TIOCL_SETKMSGREDIRECT:
2662 if (!capable(CAP_SYS_ADMIN)) {
2663 ret = -EPERM;
2664 } else {
2665 if (get_user(data, p+1))
2666 ret = -EFAULT;
2667 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002668 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670 break;
2671 case TIOCL_GETFGCONSOLE:
2672 ret = fg_console;
2673 break;
2674 case TIOCL_SCROLLCONSOLE:
2675 if (get_user(lines, (s32 __user *)(p+4))) {
2676 ret = -EFAULT;
2677 } else {
2678 scrollfront(vc_cons[fg_console].d, lines);
2679 ret = 0;
2680 }
2681 break;
2682 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Stephane Doyon2d237c62005-09-06 15:17:31 -07002683 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 ignore_poke = 1;
2685 do_blank_screen(0);
Stephane Doyon2d237c62005-09-06 15:17:31 -07002686 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 break;
2688 case TIOCL_BLANKEDSCREEN:
2689 ret = console_blanked;
2690 break;
2691 default:
2692 ret = -EINVAL;
2693 break;
2694 }
Alan Cox04f378b2008-04-30 00:53:29 -07002695 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 return ret;
2697}
2698
2699/*
2700 * /dev/ttyN handling
2701 */
2702
2703static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2704{
2705 int retval;
2706
2707 retval = do_con_write(tty, buf, count);
2708 con_flush_chars(tty);
2709
2710 return retval;
2711}
2712
Alan Cox5d19f542008-04-30 00:54:08 -07002713static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002716 return 0; /* n_r3964 calls put_char() from interrupt context */
2717 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720static int con_write_room(struct tty_struct *tty)
2721{
2722 if (tty->stopped)
2723 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002724 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727static int con_chars_in_buffer(struct tty_struct *tty)
2728{
2729 return 0; /* we're not buffering */
2730}
2731
2732/*
2733 * con_throttle and con_unthrottle are only used for
2734 * paste_selection(), which has to stuff in a large number of
2735 * characters...
2736 */
2737static void con_throttle(struct tty_struct *tty)
2738{
2739}
2740
2741static void con_unthrottle(struct tty_struct *tty)
2742{
2743 struct vc_data *vc = tty->driver_data;
2744
2745 wake_up_interruptible(&vc->paste_wait);
2746}
2747
2748/*
2749 * Turn the Scroll-Lock LED on when the tty is stopped
2750 */
2751static void con_stop(struct tty_struct *tty)
2752{
2753 int console_num;
2754 if (!tty)
2755 return;
2756 console_num = tty->index;
2757 if (!vc_cons_allocated(console_num))
2758 return;
2759 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2760 set_leds();
2761}
2762
2763/*
2764 * Turn the Scroll-Lock LED off when the console is started
2765 */
2766static void con_start(struct tty_struct *tty)
2767{
2768 int console_num;
2769 if (!tty)
2770 return;
2771 console_num = tty->index;
2772 if (!vc_cons_allocated(console_num))
2773 return;
2774 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2775 set_leds();
2776}
2777
2778static void con_flush_chars(struct tty_struct *tty)
2779{
2780 struct vc_data *vc;
2781
2782 if (in_interrupt()) /* from flush_to_ldisc */
2783 return;
2784
2785 /* if we race with con_close(), vt may be null */
2786 acquire_console_sem();
2787 vc = tty->driver_data;
2788 if (vc)
2789 set_cursor(vc);
2790 release_console_sem();
2791}
2792
2793/*
2794 * Allocate the console screen memory.
2795 */
2796static int con_open(struct tty_struct *tty, struct file *filp)
2797{
2798 unsigned int currcons = tty->index;
2799 int ret = 0;
2800
2801 acquire_console_sem();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002802 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 ret = vc_allocate(currcons);
2804 if (ret == 0) {
2805 struct vc_data *vc = vc_cons[currcons].d;
Alan Coxfeebed62008-10-13 10:41:30 +01002806
2807 /* Still being freed */
2808 if (vc->vc_tty) {
2809 release_console_sem();
2810 return -ERESTARTSYS;
2811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 tty->driver_data = vc;
2813 vc->vc_tty = tty;
2814
2815 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2816 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2817 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2818 }
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002819 if (vc->vc_utf)
2820 tty->termios->c_iflag |= IUTF8;
2821 else
2822 tty->termios->c_iflag &= ~IUTF8;
Jiri Slabye0426e62008-07-23 21:29:58 -07002823 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 return ret;
2825 }
2826 }
2827 release_console_sem();
2828 return ret;
2829}
2830
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831static void con_close(struct tty_struct *tty, struct file *filp)
2832{
Alan Coxfeebed62008-10-13 10:41:30 +01002833 /* Nothing to do - we defer to shutdown */
2834}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
Alan Coxfeebed62008-10-13 10:41:30 +01002836static void con_shutdown(struct tty_struct *tty)
2837{
2838 struct vc_data *vc = tty->driver_data;
2839 BUG_ON(vc == NULL);
2840 acquire_console_sem();
2841 vc->vc_tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 release_console_sem();
Alan Coxfeebed62008-10-13 10:41:30 +01002843 tty_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844}
2845
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002846static int default_italic_color = 2; // green (ASCII)
2847static int default_underline_color = 3; // cyan (ASCII)
2848module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2849module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851static void vc_init(struct vc_data *vc, unsigned int rows,
2852 unsigned int cols, int do_clear)
2853{
2854 int j, k ;
2855
2856 vc->vc_cols = cols;
2857 vc->vc_rows = rows;
2858 vc->vc_size_row = cols << 1;
2859 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2860
2861 set_origin(vc);
2862 vc->vc_pos = vc->vc_origin;
2863 reset_vc(vc);
2864 for (j=k=0; j<16; j++) {
2865 vc->vc_palette[k++] = default_red[j] ;
2866 vc->vc_palette[k++] = default_grn[j] ;
2867 vc->vc_palette[k++] = default_blu[j] ;
2868 }
2869 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002870 vc->vc_ulcolor = default_underline_color;
2871 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 vc->vc_halfcolor = 0x08; /* grey */
2873 init_waitqueue_head(&vc->paste_wait);
2874 reset_terminal(vc, do_clear);
2875}
2876
2877/*
2878 * This routine initializes console interrupts, and does nothing
2879 * else. If you want the screen to clear, call tty_write with
2880 * the appropriate escape-sequence.
2881 */
2882
2883static int __init con_init(void)
2884{
2885 const char *display_desc = NULL;
2886 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002887 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 acquire_console_sem();
2890
2891 if (conswitchp)
2892 display_desc = conswitchp->con_startup();
2893 if (!display_desc) {
2894 fg_console = 0;
2895 release_console_sem();
2896 return 0;
2897 }
2898
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002899 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2900 struct con_driver *con_driver = &registered_con_driver[i];
2901
2902 if (con_driver->con == NULL) {
2903 con_driver->con = conswitchp;
2904 con_driver->desc = display_desc;
2905 con_driver->flag = CON_DRIVER_FLAG_INIT;
2906 con_driver->first = 0;
2907 con_driver->last = MAX_NR_CONSOLES - 1;
2908 break;
2909 }
2910 }
2911
2912 for (i = 0; i < MAX_NR_CONSOLES; i++)
2913 con_driver_map[i] = conswitchp;
2914
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 if (blankinterval) {
2916 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002917 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002921 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002922 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002924 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 vc_init(vc, vc->vc_rows, vc->vc_cols,
2926 currcons || !vc->vc_sw->con_save_screen);
2927 }
2928 currcons = fg_console = 0;
2929 master_display_fg = vc = vc_cons[currcons].d;
2930 set_origin(vc);
2931 save_screen(vc);
2932 gotoxy(vc, vc->vc_x, vc->vc_y);
2933 csi_J(vc, 0);
2934 update_screen(vc);
2935 printk("Console: %s %s %dx%d",
2936 vc->vc_can_do_color ? "colour" : "mono",
2937 display_desc, vc->vc_cols, vc->vc_rows);
2938 printable = 1;
2939 printk("\n");
2940
2941 release_console_sem();
2942
2943#ifdef CONFIG_VT_CONSOLE
2944 register_console(&vt_console_driver);
2945#endif
2946 return 0;
2947}
2948console_initcall(con_init);
2949
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002950static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 .open = con_open,
2952 .close = con_close,
2953 .write = con_write,
2954 .write_room = con_write_room,
2955 .put_char = con_put_char,
2956 .flush_chars = con_flush_chars,
2957 .chars_in_buffer = con_chars_in_buffer,
2958 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02002959#ifdef CONFIG_COMPAT
2960 .compat_ioctl = vt_compat_ioctl,
2961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 .stop = con_stop,
2963 .start = con_start,
2964 .throttle = con_throttle,
2965 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002966 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01002967 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968};
2969
Alan Coxd81ed102008-10-13 10:41:42 +01002970static struct cdev vc0_cdev;
2971
2972int __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");
2978 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 vcs_init();
2981
2982 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2983 if (!console_driver)
2984 panic("Couldn't allocate console driver\n");
2985 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 console_driver->name = "tty";
2987 console_driver->name_base = 1;
2988 console_driver->major = TTY_MAJOR;
2989 console_driver->minor_start = 1;
2990 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2991 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002992 if (default_utf8)
2993 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2995 tty_set_operations(console_driver, &con_ops);
2996 if (tty_register_driver(console_driver))
2997 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 kbd_init();
2999 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000#ifdef CONFIG_MDA_CONSOLE
3001 mda_console_init();
3002#endif
3003 return 0;
3004}
3005
3006#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003007
3008static struct class *vtconsole_class;
3009
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003010static int bind_con_driver(const struct consw *csw, int first, int last,
3011 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003013 struct module *owner = csw->owner;
3014 const char *desc = NULL;
3015 struct con_driver *con_driver;
3016 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (!try_module_get(owner))
3019 return -ENODEV;
3020
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003021 acquire_console_sem();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003022
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003023 /* check if driver is registered */
3024 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3025 con_driver = &registered_con_driver[i];
3026
3027 if (con_driver->con == csw) {
3028 desc = con_driver->desc;
3029 retval = 0;
3030 break;
3031 }
3032 }
3033
3034 if (retval)
3035 goto err;
3036
3037 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3038 csw->con_startup();
3039 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 if (deflt) {
3043 if (conswitchp)
3044 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 __module_get(owner);
3047 conswitchp = csw;
3048 }
3049
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003050 first = max(first, con_driver->first);
3051 last = min(last, con_driver->last);
3052
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 for (i = first; i <= last; i++) {
3054 int old_was_color;
3055 struct vc_data *vc = vc_cons[i].d;
3056
3057 if (con_driver_map[i])
3058 module_put(con_driver_map[i]->owner);
3059 __module_get(owner);
3060 con_driver_map[i] = csw;
3061
3062 if (!vc || !vc->vc_sw)
3063 continue;
3064
3065 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003066
David Hollister4ee1acc2006-06-26 00:26:41 -07003067 if (CON_IS_VISIBLE(vc)) {
3068 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003070 }
3071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 old_was_color = vc->vc_can_do_color;
3073 vc->vc_sw->con_deinit(vc);
qiaochong02f07772010-08-09 17:21:23 -07003074 if (!vc->vc_origin)
3075 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003077 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 update_attr(vc);
3079
3080 /* If the console changed between mono <-> color, then
3081 * the attributes in the screenbuf will be wrong. The
3082 * following resets all attributes to something sane.
3083 */
3084 if (old_was_color != vc->vc_can_do_color)
3085 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 printk("Console: switching ");
3089 if (!deflt)
3090 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003091 if (j >= 0) {
3092 struct vc_data *vc = vc_cons[j].d;
3093
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003095 vc->vc_can_do_color ? "colour" : "mono",
3096 desc, vc->vc_cols, vc->vc_rows);
3097
3098 if (k >= 0) {
3099 vc = vc_cons[k].d;
3100 update_screen(vc);
3101 }
3102 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 printk("to %s\n", desc);
3104
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003105 retval = 0;
3106err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003109 return retval;
3110};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003112#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3113static int con_is_graphics(const struct consw *csw, int first, int last)
3114{
3115 int i, retval = 0;
3116
3117 for (i = first; i <= last; i++) {
3118 struct vc_data *vc = vc_cons[i].d;
3119
3120 if (vc && vc->vc_mode == KD_GRAPHICS) {
3121 retval = 1;
3122 break;
3123 }
3124 }
3125
3126 return retval;
3127}
3128
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003129/**
3130 * unbind_con_driver - unbind a console driver
3131 * @csw: pointer to console driver to unregister
3132 * @first: first in range of consoles that @csw should be unbound from
3133 * @last: last in range of consoles that @csw should be unbound from
3134 * @deflt: should next bound console driver be default after @csw is unbound?
3135 *
3136 * To unbind a driver from all possible consoles, pass 0 as @first and
3137 * %MAX_NR_CONSOLES as @last.
3138 *
3139 * @deflt controls whether the console that ends up replacing @csw should be
3140 * the default console.
3141 *
3142 * RETURNS:
3143 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3144 * or 0 on success.
3145 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003146int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003148 struct module *owner = csw->owner;
3149 const struct consw *defcsw = NULL;
3150 struct con_driver *con_driver = NULL, *con_back = NULL;
3151 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003153 if (!try_module_get(owner))
3154 return -ENODEV;
3155
3156 acquire_console_sem();
3157
3158 /* check if driver is registered and if it is unbindable */
3159 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3160 con_driver = &registered_con_driver[i];
3161
3162 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003163 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003164 retval = 0;
3165 break;
3166 }
3167 }
3168
3169 if (retval) {
3170 release_console_sem();
3171 goto err;
3172 }
3173
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003174 retval = -ENODEV;
3175
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003176 /* check if backup driver exists */
3177 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3178 con_back = &registered_con_driver[i];
3179
3180 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003181 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003182 defcsw = con_back->con;
3183 retval = 0;
3184 break;
3185 }
3186 }
3187
3188 if (retval) {
3189 release_console_sem();
3190 goto err;
3191 }
3192
3193 if (!con_is_bound(csw)) {
3194 release_console_sem();
3195 goto err;
3196 }
3197
3198 first = max(first, con_driver->first);
3199 last = min(last, con_driver->last);
3200
3201 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 if (con_driver_map[i] == csw) {
3203 module_put(csw->owner);
3204 con_driver_map[i] = NULL;
3205 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003206 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003207
3208 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003209 const struct consw *defconsw = conswitchp;
3210
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003211 defcsw->con_startup();
3212 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003213 /*
3214 * vgacon may change the default driver to point
3215 * to dummycon, we restore it here...
3216 */
3217 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003218 }
3219
3220 if (!con_is_bound(csw))
3221 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3222
3223 release_console_sem();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003224 /* ignore return value, binding should not fail */
3225 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003226err:
3227 module_put(owner);
3228 return retval;
3229
3230}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003231EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003232
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003233static int vt_bind(struct con_driver *con)
3234{
3235 const struct consw *defcsw = NULL, *csw = NULL;
3236 int i, more = 1, first = -1, last = -1, deflt = 0;
3237
3238 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3239 con_is_graphics(con->con, con->first, con->last))
3240 goto err;
3241
3242 csw = con->con;
3243
3244 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3245 struct con_driver *con = &registered_con_driver[i];
3246
3247 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3248 defcsw = con->con;
3249 break;
3250 }
3251 }
3252
3253 if (!defcsw)
3254 goto err;
3255
3256 while (more) {
3257 more = 0;
3258
3259 for (i = con->first; i <= con->last; i++) {
3260 if (con_driver_map[i] == defcsw) {
3261 if (first == -1)
3262 first = i;
3263 last = i;
3264 more = 1;
3265 } else if (first != -1)
3266 break;
3267 }
3268
3269 if (first == 0 && last == MAX_NR_CONSOLES -1)
3270 deflt = 1;
3271
3272 if (first != -1)
3273 bind_con_driver(csw, first, last, deflt);
3274
3275 first = -1;
3276 last = -1;
3277 deflt = 0;
3278 }
3279
3280err:
3281 return 0;
3282}
3283
3284static int vt_unbind(struct con_driver *con)
3285{
3286 const struct consw *csw = NULL;
3287 int i, more = 1, first = -1, last = -1, deflt = 0;
3288
3289 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3290 con_is_graphics(con->con, con->first, con->last))
3291 goto err;
3292
3293 csw = con->con;
3294
3295 while (more) {
3296 more = 0;
3297
3298 for (i = con->first; i <= con->last; i++) {
3299 if (con_driver_map[i] == csw) {
3300 if (first == -1)
3301 first = i;
3302 last = i;
3303 more = 1;
3304 } else if (first != -1)
3305 break;
3306 }
3307
3308 if (first == 0 && last == MAX_NR_CONSOLES -1)
3309 deflt = 1;
3310
3311 if (first != -1)
3312 unbind_con_driver(csw, first, last, deflt);
3313
3314 first = -1;
3315 last = -1;
3316 deflt = 0;
3317 }
3318
3319err:
3320 return 0;
3321}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003322#else
3323static inline int vt_bind(struct con_driver *con)
3324{
3325 return 0;
3326}
3327static inline int vt_unbind(struct con_driver *con)
3328{
3329 return 0;
3330}
3331#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003332
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003333static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003334 const char *buf, size_t count)
3335{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003336 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003337 int bind = simple_strtoul(buf, NULL, 0);
3338
3339 if (bind)
3340 vt_bind(con);
3341 else
3342 vt_unbind(con);
3343
3344 return count;
3345}
3346
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003347static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3348 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003349{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003350 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003351 int bind = con_is_bound(con->con);
3352
3353 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3354}
3355
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003356static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3357 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003358{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003359 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003360
3361 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3362 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3363 con->desc);
3364
3365}
3366
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003367static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003368 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3369 __ATTR(name, S_IRUGO, show_name, NULL),
3370};
3371
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003372static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003373{
3374 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003375 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003376
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003377 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003378 dev_set_drvdata(con->dev, con);
3379 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3380 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003381 if (error)
3382 break;
3383 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003384
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003385 if (error) {
3386 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003387 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003388 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3389 }
3390
3391 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003392}
3393
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003394static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003395{
3396 int i;
3397
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003398 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003399 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3400 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003401 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3402 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003403}
3404
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003405/**
3406 * con_is_bound - checks if driver is bound to the console
3407 * @csw: console driver
3408 *
3409 * RETURNS: zero if unbound, nonzero if bound
3410 *
3411 * Drivers can call this and if zero, they should release
3412 * all resources allocated on con_startup()
3413 */
3414int con_is_bound(const struct consw *csw)
3415{
3416 int i, bound = 0;
3417
3418 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3419 if (con_driver_map[i] == csw) {
3420 bound = 1;
3421 break;
3422 }
3423 }
3424
3425 return bound;
3426}
3427EXPORT_SYMBOL(con_is_bound);
3428
3429/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003430 * con_debug_enter - prepare the console for the kernel debugger
3431 * @sw: console driver
3432 *
3433 * Called when the console is taken over by the kernel debugger, this
3434 * function needs to save the current console state, then put the console
3435 * into a state suitable for the kernel debugger.
3436 *
3437 * RETURNS:
3438 * Zero on success, nonzero if a failure occurred when trying to prepare
3439 * the console for the debugger.
3440 */
3441int con_debug_enter(struct vc_data *vc)
3442{
3443 int ret = 0;
3444
3445 saved_fg_console = fg_console;
3446 saved_last_console = last_console;
3447 saved_want_console = want_console;
3448 saved_vc_mode = vc->vc_mode;
3449 vc->vc_mode = KD_TEXT;
3450 console_blanked = 0;
3451 if (vc->vc_sw->con_debug_enter)
3452 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003453#ifdef CONFIG_KGDB_KDB
3454 /* Set the initial LINES variable if it is not already set */
3455 if (vc->vc_rows < 999) {
3456 int linecount;
3457 char lns[4];
3458 const char *setargs[3] = {
3459 "set",
3460 "LINES",
3461 lns,
3462 };
3463 if (kdbgetintenv(setargs[0], &linecount)) {
3464 snprintf(lns, 4, "%i", vc->vc_rows);
3465 kdb_set(2, setargs);
3466 }
3467 }
3468#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003469 return ret;
3470}
3471EXPORT_SYMBOL_GPL(con_debug_enter);
3472
3473/**
3474 * con_debug_leave - restore console state
3475 * @sw: console driver
3476 *
3477 * Restore the console state to what it was before the kernel debugger
3478 * was invoked.
3479 *
3480 * RETURNS:
3481 * Zero on success, nonzero if a failure occurred when trying to restore
3482 * the console.
3483 */
3484int con_debug_leave(void)
3485{
3486 struct vc_data *vc;
3487 int ret = 0;
3488
3489 fg_console = saved_fg_console;
3490 last_console = saved_last_console;
3491 want_console = saved_want_console;
3492 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3493
3494 vc = vc_cons[fg_console].d;
3495 if (vc->vc_sw->con_debug_leave)
3496 ret = vc->vc_sw->con_debug_leave(vc);
3497 return ret;
3498}
3499EXPORT_SYMBOL_GPL(con_debug_leave);
3500
3501/**
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003502 * register_con_driver - register console driver to console layer
3503 * @csw: console driver
3504 * @first: the first console to take over, minimum value is 0
3505 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3506 *
3507 * DESCRIPTION: This function registers a console driver which can later
3508 * bind to a range of consoles specified by @first and @last. It will
3509 * also initialize the console driver by calling con_startup().
3510 */
3511int register_con_driver(const struct consw *csw, int first, int last)
3512{
3513 struct module *owner = csw->owner;
3514 struct con_driver *con_driver;
3515 const char *desc;
3516 int i, retval = 0;
3517
3518 if (!try_module_get(owner))
3519 return -ENODEV;
3520
3521 acquire_console_sem();
3522
3523 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3524 con_driver = &registered_con_driver[i];
3525
3526 /* already registered */
3527 if (con_driver->con == csw)
3528 retval = -EINVAL;
3529 }
3530
3531 if (retval)
3532 goto err;
3533
3534 desc = csw->con_startup();
3535
3536 if (!desc)
3537 goto err;
3538
3539 retval = -EINVAL;
3540
3541 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3542 con_driver = &registered_con_driver[i];
3543
3544 if (con_driver->con == NULL) {
3545 con_driver->con = csw;
3546 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003547 con_driver->node = i;
3548 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003549 CON_DRIVER_FLAG_INIT;
3550 con_driver->first = first;
3551 con_driver->last = last;
3552 retval = 0;
3553 break;
3554 }
3555 }
3556
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003557 if (retval)
3558 goto err;
3559
Alan Coxd81ed102008-10-13 10:41:42 +01003560 con_driver->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003561 MKDEV(0, con_driver->node),
3562 NULL, "vtcon%i",
3563 con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003564
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003565 if (IS_ERR(con_driver->dev)) {
3566 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003567 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003568 PTR_ERR(con_driver->dev));
3569 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003570 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003571 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003572 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003573
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003574err:
3575 release_console_sem();
3576 module_put(owner);
3577 return retval;
3578}
3579EXPORT_SYMBOL(register_con_driver);
3580
3581/**
3582 * unregister_con_driver - unregister console driver from console layer
3583 * @csw: console driver
3584 *
3585 * DESCRIPTION: All drivers that registers to the console layer must
3586 * call this function upon exit, or if the console driver is in a state
3587 * where it won't be able to handle console services, such as the
3588 * framebuffer console without loaded framebuffer drivers.
3589 *
3590 * The driver must unbind first prior to unregistration.
3591 */
3592int unregister_con_driver(const struct consw *csw)
3593{
3594 int i, retval = -ENODEV;
3595
3596 acquire_console_sem();
3597
3598 /* cannot unregister a bound driver */
3599 if (con_is_bound(csw))
3600 goto err;
3601
3602 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3603 struct con_driver *con_driver = &registered_con_driver[i];
3604
3605 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003606 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003607 vtconsole_deinit_device(con_driver);
3608 device_destroy(vtconsole_class,
3609 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003610 con_driver->con = NULL;
3611 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003612 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003613 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003614 con_driver->flag = 0;
3615 con_driver->first = 0;
3616 con_driver->last = 0;
3617 retval = 0;
3618 break;
3619 }
3620 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003621err:
3622 release_console_sem();
3623 return retval;
3624}
3625EXPORT_SYMBOL(unregister_con_driver);
3626
3627/*
3628 * If we support more console drivers, this function is used
3629 * when a driver wants to take over some existing consoles
3630 * and become default driver for newly opened ones.
3631 *
3632 * take_over_console is basically a register followed by unbind
3633 */
3634int take_over_console(const struct consw *csw, int first, int last, int deflt)
3635{
3636 int err;
3637
3638 err = register_con_driver(csw, first, last);
3639
3640 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003641 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003642
3643 return err;
3644}
3645
3646/*
3647 * give_up_console is a wrapper to unregister_con_driver. It will only
3648 * work if driver is fully unbound.
3649 */
3650void give_up_console(const struct consw *csw)
3651{
3652 unregister_con_driver(csw);
3653}
3654
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003655static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003656{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003657 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003658
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003659 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3660 if (IS_ERR(vtconsole_class)) {
3661 printk(KERN_WARNING "Unable to create vt console class; "
3662 "errno = %ld\n", PTR_ERR(vtconsole_class));
3663 vtconsole_class = NULL;
3664 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003665
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003666 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003667 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3668 struct con_driver *con = &registered_con_driver[i];
3669
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003670 if (con->con && !con->dev) {
Alan Coxd81ed102008-10-13 10:41:42 +01003671 con->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003672 MKDEV(0, con->node),
3673 NULL, "vtcon%i",
3674 con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003675
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003676 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003677 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003678 "device for %s; errno = %ld\n",
3679 con->desc, PTR_ERR(con->dev));
3680 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003681 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003682 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003683 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003684 }
3685 }
3686
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003687 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003688}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003689postcore_initcall(vtconsole_class_init);
3690
3691#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003692
3693/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694 * Screen blanking
3695 */
3696
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003697static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003699 unsigned int mode;
3700
3701 if (get_user(mode, p + 1))
3702 return -EFAULT;
3703
3704 vesa_blank_mode = (mode < 4) ? mode : 0;
3705 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708void do_blank_screen(int entering_gfx)
3709{
3710 struct vc_data *vc = vc_cons[fg_console].d;
3711 int i;
3712
3713 WARN_CONSOLE_UNLOCKED();
3714
3715 if (console_blanked) {
3716 if (blank_state == blank_vesa_wait) {
3717 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003718 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 }
3720 return;
3721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
3723 /* entering graphics mode? */
3724 if (entering_gfx) {
3725 hide_cursor(vc);
3726 save_screen(vc);
3727 vc->vc_sw->con_blank(vc, -1, 1);
3728 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003729 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 set_origin(vc);
3731 return;
3732 }
3733
izumib6e8f002007-07-17 04:05:49 -07003734 if (blank_state != blank_normal_wait)
3735 return;
3736 blank_state = blank_off;
3737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 /* don't blank graphics */
3739 if (vc->vc_mode != KD_TEXT) {
3740 console_blanked = fg_console + 1;
3741 return;
3742 }
3743
3744 hide_cursor(vc);
3745 del_timer_sync(&console_timer);
3746 blank_timer_expired = 0;
3747
3748 save_screen(vc);
3749 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003750 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 console_blanked = fg_console + 1;
3752 if (i)
3753 set_origin(vc);
3754
3755 if (console_blank_hook && console_blank_hook(1))
3756 return;
3757
Ville Syrjalad060a322006-01-09 20:53:49 -08003758 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003759 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760 mod_timer(&console_timer, jiffies + vesa_off_interval);
3761 }
Alan Cox8b92e872009-09-19 13:13:24 -07003762 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764EXPORT_SYMBOL(do_blank_screen);
3765
3766/*
3767 * Called by timer as well as from vt_console_driver
3768 */
3769void do_unblank_screen(int leaving_gfx)
3770{
3771 struct vc_data *vc;
3772
3773 /* This should now always be called from a "sane" (read: can schedule)
3774 * context for the sake of the low level drivers, except in the special
3775 * case of oops_in_progress
3776 */
3777 if (!oops_in_progress)
3778 might_sleep();
3779
3780 WARN_CONSOLE_UNLOCKED();
3781
3782 ignore_poke = 0;
3783 if (!console_blanked)
3784 return;
3785 if (!vc_cons_allocated(fg_console)) {
3786 /* impossible */
3787 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3788 return;
3789 }
3790 vc = vc_cons[fg_console].d;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003791 /* Try to unblank in oops case too */
3792 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 return; /* but leave console_blanked != 0 */
3794
3795 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003796 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 blank_state = blank_normal_wait;
3798 }
3799
3800 console_blanked = 0;
Jesse Barnes8fd4bd22010-06-23 12:56:12 -07003801 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 /* Low-level driver cannot restore -> do it ourselves */
3803 update_screen(vc);
3804 if (console_blank_hook)
3805 console_blank_hook(0);
3806 set_palette(vc);
3807 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003808 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810EXPORT_SYMBOL(do_unblank_screen);
3811
3812/*
3813 * This is called by the outside world to cause a forced unblank, mostly for
3814 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3815 * call it with 1 as an argument and so force a mode restore... that may kill
3816 * X or at least garbage the screen but would also make the Oops visible...
3817 */
3818void unblank_screen(void)
3819{
3820 do_unblank_screen(0);
3821}
3822
3823/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003824 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003826 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 */
3828static void blank_screen_t(unsigned long dummy)
3829{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003830 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003831 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003832 return;
3833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 blank_timer_expired = 1;
3835 schedule_work(&console_work);
3836}
3837
3838void poke_blanked_console(void)
3839{
3840 WARN_CONSOLE_UNLOCKED();
3841
3842 /* Add this so we quickly catch whoever might call us in a non
3843 * safe context. Nowadays, unblank_screen() isn't to be called in
3844 * atomic contexts and is allowed to schedule (with the special case
3845 * of oops_in_progress, but that isn't of any concern for this
3846 * function. --BenH.
3847 */
3848 might_sleep();
3849
3850 /* This isn't perfectly race free, but a race here would be mostly harmless,
3851 * at worse, we'll do a spurrious blank and it's unlikely
3852 */
3853 del_timer(&console_timer);
3854 blank_timer_expired = 0;
3855
3856 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3857 return;
3858 if (console_blanked)
3859 unblank_screen();
3860 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003861 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 blank_state = blank_normal_wait;
3863 }
3864}
3865
3866/*
3867 * Palettes
3868 */
3869
3870static void set_palette(struct vc_data *vc)
3871{
3872 WARN_CONSOLE_UNLOCKED();
3873
3874 if (vc->vc_mode != KD_GRAPHICS)
3875 vc->vc_sw->con_set_palette(vc, color_table);
3876}
3877
3878static int set_get_cmap(unsigned char __user *arg, int set)
3879{
3880 int i, j, k;
3881
3882 WARN_CONSOLE_UNLOCKED();
3883
3884 for (i = 0; i < 16; i++)
3885 if (set) {
3886 get_user(default_red[i], arg++);
3887 get_user(default_grn[i], arg++);
3888 get_user(default_blu[i], arg++);
3889 } else {
3890 put_user(default_red[i], arg++);
3891 put_user(default_grn[i], arg++);
3892 put_user(default_blu[i], arg++);
3893 }
3894 if (set) {
3895 for (i = 0; i < MAX_NR_CONSOLES; i++)
3896 if (vc_cons_allocated(i)) {
3897 for (j = k = 0; j < 16; j++) {
3898 vc_cons[i].d->vc_palette[k++] = default_red[j];
3899 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3900 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3901 }
3902 set_palette(vc_cons[i].d);
3903 }
3904 }
3905 return 0;
3906}
3907
3908/*
3909 * Load palette into the DAC registers. arg points to a colour
3910 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3911 */
3912
3913int con_set_cmap(unsigned char __user *arg)
3914{
3915 int rc;
3916
3917 acquire_console_sem();
3918 rc = set_get_cmap (arg,1);
3919 release_console_sem();
3920
3921 return rc;
3922}
3923
3924int con_get_cmap(unsigned char __user *arg)
3925{
3926 int rc;
3927
3928 acquire_console_sem();
3929 rc = set_get_cmap (arg,0);
3930 release_console_sem();
3931
3932 return rc;
3933}
3934
3935void reset_palette(struct vc_data *vc)
3936{
3937 int j, k;
3938 for (j=k=0; j<16; j++) {
3939 vc->vc_palette[k++] = default_red[j];
3940 vc->vc_palette[k++] = default_grn[j];
3941 vc->vc_palette[k++] = default_blu[j];
3942 }
3943 set_palette(vc);
3944}
3945
3946/*
3947 * Font switching
3948 *
3949 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3950 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3951 * depending on width) reserved for each character which is kinda wasty, but
3952 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3953 * is upto the actual low-level console-driver convert data into its favorite
3954 * format (maybe we should add a `fontoffset' field to the `display'
3955 * structure so we won't have to convert the fontdata all the time.
3956 * /Jes
3957 */
3958
3959#define max_font_size 65536
3960
3961static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3962{
3963 struct console_font font;
3964 int rc = -EINVAL;
3965 int c;
3966
3967 if (vc->vc_mode != KD_TEXT)
3968 return -EINVAL;
3969
3970 if (op->data) {
3971 font.data = kmalloc(max_font_size, GFP_KERNEL);
3972 if (!font.data)
3973 return -ENOMEM;
3974 } else
3975 font.data = NULL;
3976
3977 acquire_console_sem();
3978 if (vc->vc_sw->con_font_get)
3979 rc = vc->vc_sw->con_font_get(vc, &font);
3980 else
3981 rc = -ENOSYS;
3982 release_console_sem();
3983
3984 if (rc)
3985 goto out;
3986
3987 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07003988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 if (op->data && font.charcount > op->charcount)
3990 rc = -ENOSPC;
3991 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3992 if (font.width > op->width || font.height > op->height)
3993 rc = -ENOSPC;
3994 } else {
3995 if (font.width != 8)
3996 rc = -EIO;
3997 else if ((op->height && font.height > op->height) ||
3998 font.height > 32)
3999 rc = -ENOSPC;
4000 }
4001 if (rc)
4002 goto out;
4003
4004 op->height = font.height;
4005 op->width = font.width;
4006 op->charcount = font.charcount;
4007
4008 if (op->data && copy_to_user(op->data, font.data, c))
4009 rc = -EFAULT;
4010
4011out:
4012 kfree(font.data);
4013 return rc;
4014}
4015
4016static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4017{
4018 struct console_font font;
4019 int rc = -EINVAL;
4020 int size;
4021
4022 if (vc->vc_mode != KD_TEXT)
4023 return -EINVAL;
4024 if (!op->data)
4025 return -EINVAL;
4026 if (op->charcount > 512)
4027 return -EINVAL;
4028 if (!op->height) { /* Need to guess font height [compat] */
4029 int h, i;
4030 u8 __user *charmap = op->data;
4031 u8 tmp;
4032
4033 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4034 so that we can get rid of this soon */
4035 if (!(op->flags & KD_FONT_FLAG_OLD))
4036 return -EINVAL;
4037 for (h = 32; h > 0; h--)
4038 for (i = 0; i < op->charcount; i++) {
4039 if (get_user(tmp, &charmap[32*i+h-1]))
4040 return -EFAULT;
4041 if (tmp)
4042 goto nonzero;
4043 }
4044 return -EINVAL;
4045 nonzero:
4046 op->height = h;
4047 }
4048 if (op->width <= 0 || op->width > 32 || op->height > 32)
4049 return -EINVAL;
4050 size = (op->width+7)/8 * 32 * op->charcount;
4051 if (size > max_font_size)
4052 return -ENOSPC;
4053 font.charcount = op->charcount;
4054 font.height = op->height;
4055 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004056 font.data = memdup_user(op->data, size);
4057 if (IS_ERR(font.data))
4058 return PTR_ERR(font.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 acquire_console_sem();
4060 if (vc->vc_sw->con_font_set)
4061 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4062 else
4063 rc = -ENOSYS;
4064 release_console_sem();
4065 kfree(font.data);
4066 return rc;
4067}
4068
4069static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4070{
4071 struct console_font font = {.width = op->width, .height = op->height};
4072 char name[MAX_FONT_NAME];
4073 char *s = name;
4074 int rc;
4075
4076 if (vc->vc_mode != KD_TEXT)
4077 return -EINVAL;
4078
4079 if (!op->data)
4080 s = NULL;
4081 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4082 return -EFAULT;
4083 else
4084 name[MAX_FONT_NAME - 1] = 0;
4085
4086 acquire_console_sem();
4087 if (vc->vc_sw->con_font_default)
4088 rc = vc->vc_sw->con_font_default(vc, &font, s);
4089 else
4090 rc = -ENOSYS;
4091 release_console_sem();
4092 if (!rc) {
4093 op->width = font.width;
4094 op->height = font.height;
4095 }
4096 return rc;
4097}
4098
4099static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4100{
4101 int con = op->height;
4102 int rc;
4103
4104 if (vc->vc_mode != KD_TEXT)
4105 return -EINVAL;
4106
4107 acquire_console_sem();
4108 if (!vc->vc_sw->con_font_copy)
4109 rc = -ENOSYS;
4110 else if (con < 0 || !vc_cons_allocated(con))
4111 rc = -ENOTTY;
4112 else if (con == vc->vc_num) /* nothing to do */
4113 rc = 0;
4114 else
4115 rc = vc->vc_sw->con_font_copy(vc, con);
4116 release_console_sem();
4117 return rc;
4118}
4119
4120int con_font_op(struct vc_data *vc, struct console_font_op *op)
4121{
4122 switch (op->op) {
4123 case KD_FONT_OP_SET:
4124 return con_font_set(vc, op);
4125 case KD_FONT_OP_GET:
4126 return con_font_get(vc, op);
4127 case KD_FONT_OP_SET_DEFAULT:
4128 return con_font_default(vc, op);
4129 case KD_FONT_OP_COPY:
4130 return con_font_copy(vc, op);
4131 }
4132 return -ENOSYS;
4133}
4134
4135/*
4136 * Interface exported to selection and vcs.
4137 */
4138
4139/* used by selection */
4140u16 screen_glyph(struct vc_data *vc, int offset)
4141{
4142 u16 w = scr_readw(screenpos(vc, offset, 1));
4143 u16 c = w & 0xff;
4144
4145 if (w & vc->vc_hi_font_mask)
4146 c |= 0x100;
4147 return c;
4148}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004149EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
4151/* used by vcs - note the word offset */
4152unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4153{
4154 return screenpos(vc, 2 * w_offset, viewed);
4155}
4156
4157void getconsxy(struct vc_data *vc, unsigned char *p)
4158{
4159 p[0] = vc->vc_x;
4160 p[1] = vc->vc_y;
4161}
4162
4163void putconsxy(struct vc_data *vc, unsigned char *p)
4164{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004165 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 gotoxy(vc, p[0], p[1]);
4167 set_cursor(vc);
4168}
4169
4170u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4171{
4172 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4173 return softcursor_original;
4174 return scr_readw(org);
4175}
4176
4177void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4178{
4179 scr_writew(val, org);
4180 if ((unsigned long)org == vc->vc_pos) {
4181 softcursor_original = -1;
4182 add_softcursor(vc);
4183 }
4184}
4185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186/*
4187 * Visible symbols for modules
4188 */
4189
4190EXPORT_SYMBOL(color_table);
4191EXPORT_SYMBOL(default_red);
4192EXPORT_SYMBOL(default_grn);
4193EXPORT_SYMBOL(default_blu);
4194EXPORT_SYMBOL(update_region);
4195EXPORT_SYMBOL(redraw_screen);
4196EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197EXPORT_SYMBOL(fg_console);
4198EXPORT_SYMBOL(console_blank_hook);
4199EXPORT_SYMBOL(console_blanked);
4200EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004201EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202#ifndef VT_SINGLE_DRIVER
4203EXPORT_SYMBOL(take_over_console);
4204EXPORT_SYMBOL(give_up_console);
4205#endif