blob: 34bfb056d7a6c86a6cef686e194dbbb58a3b4cb0 [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 }
708 if (update && vc->vc_mode != KD_GRAPHICS)
709 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
710 }
711 set_cursor(vc);
712 if (is_switch) {
713 set_leds();
714 compute_shiftstate();
Samuel Thibault8182ec42008-03-04 14:28:36 -0800715 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717}
718
719/*
720 * Allocation, freeing and resizing of VTs.
721 */
722
723int vc_cons_allocated(unsigned int i)
724{
725 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
726}
727
728static void visual_init(struct vc_data *vc, int num, int init)
729{
730 /* ++Geert: vc->vc_sw->con_init determines console size */
731 if (vc->vc_sw)
732 module_put(vc->vc_sw->owner);
733 vc->vc_sw = conswitchp;
734#ifndef VT_SINGLE_DRIVER
735 if (con_driver_map[num])
736 vc->vc_sw = con_driver_map[num];
737#endif
738 __module_get(vc->vc_sw->owner);
739 vc->vc_num = num;
740 vc->vc_display_fg = &master_display_fg;
741 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
742 vc->vc_uni_pagedir = 0;
743 vc->vc_hi_font_mask = 0;
744 vc->vc_complement_mask = 0;
745 vc->vc_can_do_color = 0;
746 vc->vc_sw->con_init(vc, init);
747 if (!vc->vc_complement_mask)
748 vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
749 vc->vc_s_complement_mask = vc->vc_complement_mask;
750 vc->vc_size_row = vc->vc_cols << 1;
751 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
752}
753
754int vc_allocate(unsigned int currcons) /* return 0 on success */
755{
756 WARN_CONSOLE_UNLOCKED();
757
758 if (currcons >= MAX_NR_CONSOLES)
759 return -ENXIO;
760 if (!vc_cons[currcons].d) {
761 struct vc_data *vc;
Samuel Thibaultb293d752007-10-18 23:39:17 -0700762 struct vt_notifier_param param;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 /* prevent users from taking too much memory */
765 if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
766 return -EPERM;
767
768 /* due to the granularity of kmalloc, we waste some memory here */
769 /* the alloc is done in two steps, to optimize the common situation
770 of a 25x80 console (structsize=216, screenbuf_size=4000) */
771 /* although the numbers above are not valid since long ago, the
772 point is still up-to-date and the comment still has its value
773 even if only as a historical artifact. --mj, July 1998 */
Samuel Thibaultb293d752007-10-18 23:39:17 -0700774 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!vc)
776 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 vc_cons[currcons].d = vc;
Bernhard Wallec2c88f12007-03-16 13:38:30 -0800778 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 visual_init(vc, currcons, 1);
780 if (!*vc->vc_uni_pagedir_loc)
781 con_set_default_unimap(vc);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100782 vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!vc->vc_screenbuf) {
784 kfree(vc);
785 vc_cons[currcons].d = NULL;
786 return -ENOMEM;
787 }
Matthew Garrettf6c06b62009-11-13 15:14:11 -0500788
789 /* If no drivers have overridden us and the user didn't pass a
790 boot option, default to displaying the cursor */
791 if (global_cursor_default == -1)
792 global_cursor_default = 1;
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
Kay Sievers4995f8e2009-03-09 14:18:52 +0100795 vcs_make_sysfs(currcons);
Samuel Thibaultb293d752007-10-18 23:39:17 -0700796 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798 return 0;
799}
800
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700801static inline int resize_screen(struct vc_data *vc, int width, int height,
802 int user)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
804 /* Resizes the resolution of the display adapater */
805 int err = 0;
806
807 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700808 err = vc->vc_sw->con_resize(vc, width, height, user);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return err;
811}
812
813/*
814 * Change # of rows and columns (0 means unchanged/the size of fg_console)
815 * [this is to be used together with some user program
816 * like resize that changes the hardware videomode]
817 */
818#define VC_RESIZE_MAXCOL (32767)
819#define VC_RESIZE_MAXROW (32767)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100820
821/**
822 * vc_do_resize - resizing method for the tty
823 * @tty: tty being resized
824 * @real_tty: real tty (different to tty if a pty/tty pair)
825 * @vc: virtual console private data
826 * @cols: columns
827 * @lines: lines
828 *
829 * Resize a virtual console, clipping according to the actual constraints.
830 * If the caller passes a tty structure then update the termios winsize
Daniel Mack3ad2f3fb2010-02-03 08:01:28 +0800831 * information and perform any necessary signal handling.
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100832 *
833 * Caller must hold the console semaphore. Takes the termios mutex and
834 * ctrl_lock of the tty IFF a tty is passed.
835 */
836
Alan Coxfc6f6232009-01-02 13:43:17 +0000837static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
838 unsigned int cols, unsigned int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
qiaochong9e0ba742010-08-09 17:21:27 -0700841 unsigned long end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned int old_cols, old_rows, old_row_size, old_screen_size;
843 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
qiaochong9e0ba742010-08-09 17:21:27 -0700844 unsigned int user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 unsigned short *newscreen;
846
847 WARN_CONSOLE_UNLOCKED();
848
849 if (!vc)
850 return -ENXIO;
851
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700852 user = vc->vc_resize_user;
853 vc->vc_resize_user = 0;
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
856 return -EINVAL;
857
858 new_cols = (cols ? cols : vc->vc_cols);
859 new_rows = (lines ? lines : vc->vc_rows);
860 new_row_size = new_cols << 1;
861 new_screen_size = new_row_size * new_rows;
862
863 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
864 return 0;
865
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800866 newscreen = kmalloc(new_screen_size, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 if (!newscreen)
868 return -ENOMEM;
869
870 old_rows = vc->vc_rows;
871 old_cols = vc->vc_cols;
872 old_row_size = vc->vc_size_row;
873 old_screen_size = vc->vc_screenbuf_size;
874
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700875 err = resize_screen(vc, new_cols, new_rows, user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (err) {
877 kfree(newscreen);
878 return err;
879 }
880
881 vc->vc_rows = new_rows;
882 vc->vc_cols = new_cols;
883 vc->vc_size_row = new_row_size;
884 vc->vc_screenbuf_size = new_screen_size;
885
886 rlth = min(old_row_size, new_row_size);
887 rrem = new_row_size - rlth;
888 old_origin = vc->vc_origin;
889 new_origin = (long) newscreen;
890 new_scr_end = new_origin + new_screen_size;
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700891
892 if (vc->vc_y > new_rows) {
893 if (old_rows - vc->vc_y < new_rows) {
894 /*
895 * Cursor near the bottom, copy contents from the
896 * bottom of buffer
897 */
898 old_origin += (old_rows - new_rows) * old_row_size;
899 end = vc->vc_scr_end;
900 } else {
901 /*
902 * Cursor is in no man's land, copy 1/2 screenful
903 * from the top and bottom of cursor position
904 */
905 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800906 end = old_origin + (old_row_size * new_rows);
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700907 }
908 } else
909 /*
910 * Cursor near the top, copy contents from the top of buffer
911 */
Antonino A. Daplas065d9ca2005-09-15 21:34:33 +0800912 end = (old_rows > new_rows) ? old_origin +
913 (old_row_size * new_rows) :
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700914 vc->vc_scr_end;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
916 update_attr(vc);
917
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700918 while (old_origin < end) {
919 scr_memcpyw((unsigned short *) new_origin,
920 (unsigned short *) old_origin, rlth);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (rrem)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700922 scr_memsetw((void *)(new_origin + rlth),
923 vc->vc_video_erase_char, rrem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 old_origin += old_row_size;
925 new_origin += new_row_size;
926 }
927 if (new_scr_end > new_origin)
Antonino A. Daplas3b41dc12005-09-09 13:04:39 -0700928 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
929 new_scr_end - new_origin);
Johannes Weiner5c9228f2009-07-16 16:06:09 +0100930 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 vc->vc_screenbuf = newscreen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 vc->vc_screenbuf_size = new_screen_size;
933 set_origin(vc);
934
935 /* do part of a reset_terminal() */
936 vc->vc_top = 0;
937 vc->vc_bottom = vc->vc_rows;
938 gotoxy(vc, vc->vc_x, vc->vc_y);
939 save_cur(vc);
940
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100941 if (tty) {
942 /* Rewrite the requested winsize data with the actual
943 resulting sizes */
944 struct winsize ws;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 memset(&ws, 0, sizeof(ws));
946 ws.ws_row = vc->vc_rows;
947 ws.ws_col = vc->vc_cols;
948 ws.ws_ypixel = vc->vc_scan_lines;
Alan Coxfc6f6232009-01-02 13:43:17 +0000949 tty_do_resize(tty, &ws);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 }
951
952 if (CON_IS_VISIBLE(vc))
953 update_screen(vc);
Alan Cox8b92e872009-09-19 13:13:24 -0700954 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return err;
956}
957
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100958/**
959 * vc_resize - resize a VT
960 * @vc: virtual console
961 * @cols: columns
962 * @rows: rows
963 *
964 * Resize a virtual console as seen from the console end of things. We
965 * use the common vc_do_resize methods to update the structures. The
966 * caller must hold the console sem to protect console internals and
967 * vc->vc_tty
968 */
969
970int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
Alan Coxca9bda02006-09-29 02:00:03 -0700971{
Alan Coxfc6f6232009-01-02 13:43:17 +0000972 return vc_do_resize(vc->vc_tty, vc, cols, rows);
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100973}
974
975/**
976 * vt_resize - resize a VT
977 * @tty: tty to resize
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100978 * @ws: winsize attributes
979 *
980 * Resize a virtual terminal. This is called by the tty layer as we
981 * register our own handler for resizing. The mutual helper does all
982 * the actual work.
983 *
984 * Takes the console sem and the called methods then take the tty
985 * termios_mutex and the tty ctrl_lock in that order.
986 */
Roel Kluinda2bdf92009-01-07 18:09:15 -0800987static int vt_resize(struct tty_struct *tty, struct winsize *ws)
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100988{
989 struct vc_data *vc = tty->driver_data;
990 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Alan Coxca9bda02006-09-29 02:00:03 -0700992 acquire_console_sem();
Alan Coxfc6f6232009-01-02 13:43:17 +0000993 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
Alan Coxca9bda02006-09-29 02:00:03 -0700994 release_console_sem();
Alan Cox8c9a9dd2008-08-15 10:39:38 +0100995 return ret;
Alan Coxca9bda02006-09-29 02:00:03 -0700996}
997
998void vc_deallocate(unsigned int currcons)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999{
1000 WARN_CONSOLE_UNLOCKED();
1001
1002 if (vc_cons_allocated(currcons)) {
1003 struct vc_data *vc = vc_cons[currcons].d;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001004 struct vt_notifier_param param = { .vc = vc };
Kay Sievers4995f8e2009-03-09 14:18:52 +01001005
Samuel Thibaultb293d752007-10-18 23:39:17 -07001006 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
Kay Sievers4995f8e2009-03-09 14:18:52 +01001007 vcs_remove_sysfs(currcons);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 vc->vc_sw->con_deinit(vc);
Eric W. Biedermanbde0d2c2006-10-02 02:17:14 -07001009 put_pid(vc->vt_pid);
Antonino A. Daplasd459ec02006-07-03 00:24:20 -07001010 module_put(vc->vc_sw->owner);
Johannes Weiner5c9228f2009-07-16 16:06:09 +01001011 kfree(vc->vc_screenbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 if (currcons >= MIN_NR_CONSOLES)
1013 kfree(vc);
1014 vc_cons[currcons].d = NULL;
1015 }
1016}
1017
1018/*
1019 * VT102 emulator
1020 */
1021
1022#define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1023#define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1024#define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
1025
1026#define decarm VC_REPEAT
1027#define decckm VC_CKMODE
1028#define kbdapplic VC_APPLIC
1029#define lnm VC_CRLF
1030
1031/*
1032 * this is what the terminal answers to a ESC-Z or csi0c query.
1033 */
1034#define VT100ID "\033[?1;2c"
1035#define VT102ID "\033[?6c"
1036
1037unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1038 8,12,10,14, 9,13,11,15 };
1039
1040/* the default colour table, for VGA+ colour systems */
1041int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
1042 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
1043int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
1044 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
1045int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
1046 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
1047
Jan Engelhardt1c2bbe62007-05-08 00:38:03 -07001048module_param_array(default_red, int, NULL, S_IRUGO | S_IWUSR);
1049module_param_array(default_grn, int, NULL, S_IRUGO | S_IWUSR);
1050module_param_array(default_blu, int, NULL, S_IRUGO | S_IWUSR);
1051
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052/*
1053 * gotoxy() must verify all boundaries, because the arguments
1054 * might also be negative. If the given position is out of
1055 * bounds, the cursor is placed at the nearest margin.
1056 */
1057static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1058{
1059 int min_y, max_y;
1060
1061 if (new_x < 0)
1062 vc->vc_x = 0;
1063 else {
1064 if (new_x >= vc->vc_cols)
1065 vc->vc_x = vc->vc_cols - 1;
1066 else
1067 vc->vc_x = new_x;
1068 }
1069
1070 if (vc->vc_decom) {
1071 min_y = vc->vc_top;
1072 max_y = vc->vc_bottom;
1073 } else {
1074 min_y = 0;
1075 max_y = vc->vc_rows;
1076 }
1077 if (new_y < min_y)
1078 vc->vc_y = min_y;
1079 else if (new_y >= max_y)
1080 vc->vc_y = max_y - 1;
1081 else
1082 vc->vc_y = new_y;
1083 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1084 vc->vc_need_wrap = 0;
1085}
1086
1087/* for absolute user moves, when decom is set */
1088static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1089{
1090 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1091}
1092
1093void scrollback(struct vc_data *vc, int lines)
1094{
1095 if (!lines)
1096 lines = vc->vc_rows / 2;
1097 scrolldelta(-lines);
1098}
1099
1100void scrollfront(struct vc_data *vc, int lines)
1101{
1102 if (!lines)
1103 lines = vc->vc_rows / 2;
1104 scrolldelta(lines);
1105}
1106
1107static void lf(struct vc_data *vc)
1108{
1109 /* don't scroll if above bottom of scrolling region, or
1110 * if below scrolling region
1111 */
1112 if (vc->vc_y + 1 == vc->vc_bottom)
1113 scrup(vc, vc->vc_top, vc->vc_bottom, 1);
1114 else if (vc->vc_y < vc->vc_rows - 1) {
1115 vc->vc_y++;
1116 vc->vc_pos += vc->vc_size_row;
1117 }
1118 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001119 notify_write(vc, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120}
1121
1122static void ri(struct vc_data *vc)
1123{
1124 /* don't scroll if below top of scrolling region, or
1125 * if above scrolling region
1126 */
1127 if (vc->vc_y == vc->vc_top)
1128 scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
1129 else if (vc->vc_y > 0) {
1130 vc->vc_y--;
1131 vc->vc_pos -= vc->vc_size_row;
1132 }
1133 vc->vc_need_wrap = 0;
1134}
1135
1136static inline void cr(struct vc_data *vc)
1137{
1138 vc->vc_pos -= vc->vc_x << 1;
1139 vc->vc_need_wrap = vc->vc_x = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001140 notify_write(vc, '\r');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
1143static inline void bs(struct vc_data *vc)
1144{
1145 if (vc->vc_x) {
1146 vc->vc_pos -= 2;
1147 vc->vc_x--;
1148 vc->vc_need_wrap = 0;
Samuel Thibaultb293d752007-10-18 23:39:17 -07001149 notify_write(vc, '\b');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 }
1151}
1152
1153static inline void del(struct vc_data *vc)
1154{
1155 /* ignored */
1156}
1157
1158static void csi_J(struct vc_data *vc, int vpar)
1159{
1160 unsigned int count;
1161 unsigned short * start;
1162
1163 switch (vpar) {
1164 case 0: /* erase from cursor to end of display */
1165 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1166 start = (unsigned short *)vc->vc_pos;
1167 if (DO_UPDATE(vc)) {
1168 /* do in two stages */
1169 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1170 vc->vc_cols - vc->vc_x);
1171 vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
1172 vc->vc_rows - vc->vc_y - 1,
1173 vc->vc_cols);
1174 }
1175 break;
1176 case 1: /* erase from start to cursor */
1177 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1178 start = (unsigned short *)vc->vc_origin;
1179 if (DO_UPDATE(vc)) {
1180 /* do in two stages */
1181 vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
1182 vc->vc_cols);
1183 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1184 vc->vc_x + 1);
1185 }
1186 break;
1187 case 2: /* erase whole display */
1188 count = vc->vc_cols * vc->vc_rows;
1189 start = (unsigned short *)vc->vc_origin;
1190 if (DO_UPDATE(vc))
1191 vc->vc_sw->con_clear(vc, 0, 0,
1192 vc->vc_rows,
1193 vc->vc_cols);
1194 break;
1195 default:
1196 return;
1197 }
1198 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1199 vc->vc_need_wrap = 0;
1200}
1201
1202static void csi_K(struct vc_data *vc, int vpar)
1203{
1204 unsigned int count;
1205 unsigned short * start;
1206
1207 switch (vpar) {
1208 case 0: /* erase from cursor to end of line */
1209 count = vc->vc_cols - vc->vc_x;
1210 start = (unsigned short *)vc->vc_pos;
1211 if (DO_UPDATE(vc))
1212 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
1213 vc->vc_cols - vc->vc_x);
1214 break;
1215 case 1: /* erase from start of line to cursor */
1216 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1217 count = vc->vc_x + 1;
1218 if (DO_UPDATE(vc))
1219 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1220 vc->vc_x + 1);
1221 break;
1222 case 2: /* erase whole line */
1223 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1224 count = vc->vc_cols;
1225 if (DO_UPDATE(vc))
1226 vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
1227 vc->vc_cols);
1228 break;
1229 default:
1230 return;
1231 }
1232 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1233 vc->vc_need_wrap = 0;
1234}
1235
1236static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1237{ /* not vt100? */
1238 int count;
1239
1240 if (!vpar)
1241 vpar++;
1242 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1243
1244 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1245 if (DO_UPDATE(vc))
1246 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1247 vc->vc_need_wrap = 0;
1248}
1249
1250static void default_attr(struct vc_data *vc)
1251{
1252 vc->vc_intensity = 1;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001253 vc->vc_italic = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 vc->vc_underline = 0;
1255 vc->vc_reverse = 0;
1256 vc->vc_blink = 0;
1257 vc->vc_color = vc->vc_def_color;
1258}
1259
1260/* console_sem is held */
1261static void csi_m(struct vc_data *vc)
1262{
1263 int i;
1264
1265 for (i = 0; i <= vc->vc_npar; i++)
1266 switch (vc->vc_par[i]) {
1267 case 0: /* all attributes off */
1268 default_attr(vc);
1269 break;
1270 case 1:
1271 vc->vc_intensity = 2;
1272 break;
1273 case 2:
1274 vc->vc_intensity = 0;
1275 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001276 case 3:
1277 vc->vc_italic = 1;
1278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case 4:
1280 vc->vc_underline = 1;
1281 break;
1282 case 5:
1283 vc->vc_blink = 1;
1284 break;
1285 case 7:
1286 vc->vc_reverse = 1;
1287 break;
1288 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1289 * Select primary font, don't display
1290 * control chars if defined, don't set
1291 * bit 8 on output.
1292 */
1293 vc->vc_translate = set_translate(vc->vc_charset == 0
1294 ? vc->vc_G0_charset
1295 : vc->vc_G1_charset, vc);
1296 vc->vc_disp_ctrl = 0;
1297 vc->vc_toggle_meta = 0;
1298 break;
1299 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1300 * Select first alternate font, lets
1301 * chars < 32 be displayed as ROM chars.
1302 */
1303 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1304 vc->vc_disp_ctrl = 1;
1305 vc->vc_toggle_meta = 0;
1306 break;
1307 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1308 * Select second alternate font, toggle
1309 * high bit before displaying as ROM char.
1310 */
1311 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1312 vc->vc_disp_ctrl = 1;
1313 vc->vc_toggle_meta = 1;
1314 break;
1315 case 21:
1316 case 22:
1317 vc->vc_intensity = 1;
1318 break;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001319 case 23:
1320 vc->vc_italic = 0;
1321 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 case 24:
1323 vc->vc_underline = 0;
1324 break;
1325 case 25:
1326 vc->vc_blink = 0;
1327 break;
1328 case 27:
1329 vc->vc_reverse = 0;
1330 break;
1331 case 38: /* ANSI X3.64-1979 (SCO-ish?)
1332 * Enables underscore, white foreground
1333 * with white underscore (Linux - use
1334 * default foreground).
1335 */
1336 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1337 vc->vc_underline = 1;
1338 break;
1339 case 39: /* ANSI X3.64-1979 (SCO-ish?)
1340 * Disable underline option.
1341 * Reset colour to default? It did this
1342 * before...
1343 */
1344 vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
1345 vc->vc_underline = 0;
1346 break;
1347 case 49:
1348 vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
1349 break;
1350 default:
1351 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1352 vc->vc_color = color_table[vc->vc_par[i] - 30]
1353 | (vc->vc_color & 0xf0);
1354 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1355 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1356 | (vc->vc_color & 0x0f);
1357 break;
1358 }
1359 update_attr(vc);
1360}
1361
1362static void respond_string(const char *p, struct tty_struct *tty)
1363{
1364 while (*p) {
1365 tty_insert_flip_char(tty, *p, 0);
1366 p++;
1367 }
1368 con_schedule_flip(tty);
1369}
1370
1371static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1372{
1373 char buf[40];
1374
1375 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1376 respond_string(buf, tty);
1377}
1378
1379static inline void status_report(struct tty_struct *tty)
1380{
1381 respond_string("\033[0n", tty); /* Terminal ok */
1382}
1383
1384static inline void respond_ID(struct tty_struct * tty)
1385{
1386 respond_string(VT102ID, tty);
1387}
1388
1389void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1390{
1391 char buf[8];
1392
1393 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1394 (char)('!' + mry));
1395 respond_string(buf, tty);
1396}
1397
1398/* invoked via ioctl(TIOCLINUX) and through set_selection */
1399int mouse_reporting(void)
1400{
1401 return vc_cons[fg_console].d->vc_report_mouse;
1402}
1403
1404/* console_sem is held */
1405static void set_mode(struct vc_data *vc, int on_off)
1406{
1407 int i;
1408
1409 for (i = 0; i <= vc->vc_npar; i++)
1410 if (vc->vc_ques) {
1411 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1412 case 1: /* Cursor keys send ^[Ox/^[[x */
1413 if (on_off)
1414 set_kbd(vc, decckm);
1415 else
1416 clr_kbd(vc, decckm);
1417 break;
1418 case 3: /* 80/132 mode switch unimplemented */
1419 vc->vc_deccolm = on_off;
1420#if 0
1421 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1422 /* this alone does not suffice; some user mode
1423 utility has to change the hardware regs */
1424#endif
1425 break;
1426 case 5: /* Inverted screen on/off */
1427 if (vc->vc_decscnm != on_off) {
1428 vc->vc_decscnm = on_off;
1429 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1430 update_attr(vc);
1431 }
1432 break;
1433 case 6: /* Origin relative/absolute */
1434 vc->vc_decom = on_off;
1435 gotoxay(vc, 0, 0);
1436 break;
1437 case 7: /* Autowrap on/off */
1438 vc->vc_decawm = on_off;
1439 break;
1440 case 8: /* Autorepeat on/off */
1441 if (on_off)
1442 set_kbd(vc, decarm);
1443 else
1444 clr_kbd(vc, decarm);
1445 break;
1446 case 9:
1447 vc->vc_report_mouse = on_off ? 1 : 0;
1448 break;
1449 case 25: /* Cursor on/off */
1450 vc->vc_deccm = on_off;
1451 break;
1452 case 1000:
1453 vc->vc_report_mouse = on_off ? 2 : 0;
1454 break;
1455 }
1456 } else {
1457 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1458 case 3: /* Monitor (display ctrls) */
1459 vc->vc_disp_ctrl = on_off;
1460 break;
1461 case 4: /* Insert Mode on/off */
1462 vc->vc_decim = on_off;
1463 break;
1464 case 20: /* Lf, Enter == CrLf/Lf */
1465 if (on_off)
1466 set_kbd(vc, lnm);
1467 else
1468 clr_kbd(vc, lnm);
1469 break;
1470 }
1471 }
1472}
1473
1474/* console_sem is held */
1475static void setterm_command(struct vc_data *vc)
1476{
1477 switch(vc->vc_par[0]) {
1478 case 1: /* set color for underline mode */
1479 if (vc->vc_can_do_color &&
1480 vc->vc_par[1] < 16) {
1481 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1482 if (vc->vc_underline)
1483 update_attr(vc);
1484 }
1485 break;
1486 case 2: /* set color for half intensity mode */
1487 if (vc->vc_can_do_color &&
1488 vc->vc_par[1] < 16) {
1489 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1490 if (vc->vc_intensity == 0)
1491 update_attr(vc);
1492 }
1493 break;
1494 case 8: /* store colors as defaults */
1495 vc->vc_def_color = vc->vc_attr;
1496 if (vc->vc_hi_font_mask == 0x100)
1497 vc->vc_def_color >>= 1;
1498 default_attr(vc);
1499 update_attr(vc);
1500 break;
1501 case 9: /* set blanking interval */
Daniel Mackf324edc2009-06-16 15:33:52 -07001502 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 poke_blanked_console();
1504 break;
1505 case 10: /* set bell frequency in Hz */
1506 if (vc->vc_npar >= 1)
1507 vc->vc_bell_pitch = vc->vc_par[1];
1508 else
1509 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1510 break;
1511 case 11: /* set bell duration in msec */
1512 if (vc->vc_npar >= 1)
1513 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1514 vc->vc_par[1] * HZ / 1000 : 0;
1515 else
1516 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1517 break;
1518 case 12: /* bring specified console to the front */
1519 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1520 set_console(vc->vc_par[1] - 1);
1521 break;
1522 case 13: /* unblank the screen */
1523 poke_blanked_console();
1524 break;
1525 case 14: /* set vesa powerdown interval */
1526 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1527 break;
1528 case 15: /* activate the previous console */
1529 set_console(last_console);
1530 break;
1531 }
1532}
1533
1534/* console_sem is held */
1535static void csi_at(struct vc_data *vc, unsigned int nr)
1536{
1537 if (nr > vc->vc_cols - vc->vc_x)
1538 nr = vc->vc_cols - vc->vc_x;
1539 else if (!nr)
1540 nr = 1;
1541 insert_char(vc, nr);
1542}
1543
1544/* console_sem is held */
1545static void csi_L(struct vc_data *vc, unsigned int nr)
1546{
1547 if (nr > vc->vc_rows - vc->vc_y)
1548 nr = vc->vc_rows - vc->vc_y;
1549 else if (!nr)
1550 nr = 1;
1551 scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
1552 vc->vc_need_wrap = 0;
1553}
1554
1555/* console_sem is held */
1556static void csi_P(struct vc_data *vc, unsigned int nr)
1557{
1558 if (nr > vc->vc_cols - vc->vc_x)
1559 nr = vc->vc_cols - vc->vc_x;
1560 else if (!nr)
1561 nr = 1;
1562 delete_char(vc, nr);
1563}
1564
1565/* console_sem is held */
1566static void csi_M(struct vc_data *vc, unsigned int nr)
1567{
1568 if (nr > vc->vc_rows - vc->vc_y)
1569 nr = vc->vc_rows - vc->vc_y;
1570 else if (!nr)
1571 nr=1;
1572 scrup(vc, vc->vc_y, vc->vc_bottom, nr);
1573 vc->vc_need_wrap = 0;
1574}
1575
1576/* console_sem is held (except via vc_init->reset_terminal */
1577static void save_cur(struct vc_data *vc)
1578{
1579 vc->vc_saved_x = vc->vc_x;
1580 vc->vc_saved_y = vc->vc_y;
1581 vc->vc_s_intensity = vc->vc_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001582 vc->vc_s_italic = vc->vc_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 vc->vc_s_underline = vc->vc_underline;
1584 vc->vc_s_blink = vc->vc_blink;
1585 vc->vc_s_reverse = vc->vc_reverse;
1586 vc->vc_s_charset = vc->vc_charset;
1587 vc->vc_s_color = vc->vc_color;
1588 vc->vc_saved_G0 = vc->vc_G0_charset;
1589 vc->vc_saved_G1 = vc->vc_G1_charset;
1590}
1591
1592/* console_sem is held */
1593static void restore_cur(struct vc_data *vc)
1594{
1595 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1596 vc->vc_intensity = vc->vc_s_intensity;
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07001597 vc->vc_italic = vc->vc_s_italic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 vc->vc_underline = vc->vc_s_underline;
1599 vc->vc_blink = vc->vc_s_blink;
1600 vc->vc_reverse = vc->vc_s_reverse;
1601 vc->vc_charset = vc->vc_s_charset;
1602 vc->vc_color = vc->vc_s_color;
1603 vc->vc_G0_charset = vc->vc_saved_G0;
1604 vc->vc_G1_charset = vc->vc_saved_G1;
1605 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1606 update_attr(vc);
1607 vc->vc_need_wrap = 0;
1608}
1609
1610enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
1611 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1612 ESpalette };
1613
1614/* console_sem is held (except via vc_init()) */
1615static void reset_terminal(struct vc_data *vc, int do_clear)
1616{
1617 vc->vc_top = 0;
1618 vc->vc_bottom = vc->vc_rows;
1619 vc->vc_state = ESnormal;
1620 vc->vc_ques = 0;
1621 vc->vc_translate = set_translate(LAT1_MAP, vc);
1622 vc->vc_G0_charset = LAT1_MAP;
1623 vc->vc_G1_charset = GRAF_MAP;
1624 vc->vc_charset = 0;
1625 vc->vc_need_wrap = 0;
1626 vc->vc_report_mouse = 0;
Antonino A. Daplas042f10e2007-05-08 00:38:09 -07001627 vc->vc_utf = default_utf8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 vc->vc_utf_count = 0;
1629
1630 vc->vc_disp_ctrl = 0;
1631 vc->vc_toggle_meta = 0;
1632
1633 vc->vc_decscnm = 0;
1634 vc->vc_decom = 0;
1635 vc->vc_decawm = 1;
Matthew Garrettf6c06b62009-11-13 15:14:11 -05001636 vc->vc_deccm = global_cursor_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 vc->vc_decim = 0;
1638
1639 set_kbd(vc, decarm);
1640 clr_kbd(vc, decckm);
1641 clr_kbd(vc, kbdapplic);
1642 clr_kbd(vc, lnm);
1643 kbd_table[vc->vc_num].lockstate = 0;
1644 kbd_table[vc->vc_num].slockstate = 0;
1645 kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
1646 kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
1647 /* do not do set_leds here because this causes an endless tasklet loop
1648 when the keyboard hasn't been initialized yet */
1649
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001650 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 vc->vc_complement_mask = vc->vc_s_complement_mask;
1652
1653 default_attr(vc);
1654 update_attr(vc);
1655
1656 vc->vc_tab_stop[0] = 0x01010100;
1657 vc->vc_tab_stop[1] =
1658 vc->vc_tab_stop[2] =
1659 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001660 vc->vc_tab_stop[4] =
1661 vc->vc_tab_stop[5] =
1662 vc->vc_tab_stop[6] =
1663 vc->vc_tab_stop[7] = 0x01010101;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
1665 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1666 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1667
1668 gotoxy(vc, 0, 0);
1669 save_cur(vc);
1670 if (do_clear)
1671 csi_J(vc, 2);
1672}
1673
1674/* console_sem is held */
1675static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1676{
1677 /*
1678 * Control characters can be used in the _middle_
1679 * of an escape sequence.
1680 */
1681 switch (c) {
1682 case 0:
1683 return;
1684 case 7:
1685 if (vc->vc_bell_duration)
1686 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1687 return;
1688 case 8:
1689 bs(vc);
1690 return;
1691 case 9:
1692 vc->vc_pos -= (vc->vc_x << 1);
1693 while (vc->vc_x < vc->vc_cols - 1) {
1694 vc->vc_x++;
1695 if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
1696 break;
1697 }
1698 vc->vc_pos += (vc->vc_x << 1);
Samuel Thibaultb293d752007-10-18 23:39:17 -07001699 notify_write(vc, '\t');
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 return;
1701 case 10: case 11: case 12:
1702 lf(vc);
1703 if (!is_kbd(vc, lnm))
1704 return;
1705 case 13:
1706 cr(vc);
1707 return;
1708 case 14:
1709 vc->vc_charset = 1;
1710 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1711 vc->vc_disp_ctrl = 1;
1712 return;
1713 case 15:
1714 vc->vc_charset = 0;
1715 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1716 vc->vc_disp_ctrl = 0;
1717 return;
1718 case 24: case 26:
1719 vc->vc_state = ESnormal;
1720 return;
1721 case 27:
1722 vc->vc_state = ESesc;
1723 return;
1724 case 127:
1725 del(vc);
1726 return;
1727 case 128+27:
1728 vc->vc_state = ESsquare;
1729 return;
1730 }
1731 switch(vc->vc_state) {
1732 case ESesc:
1733 vc->vc_state = ESnormal;
1734 switch (c) {
1735 case '[':
1736 vc->vc_state = ESsquare;
1737 return;
1738 case ']':
1739 vc->vc_state = ESnonstd;
1740 return;
1741 case '%':
1742 vc->vc_state = ESpercent;
1743 return;
1744 case 'E':
1745 cr(vc);
1746 lf(vc);
1747 return;
1748 case 'M':
1749 ri(vc);
1750 return;
1751 case 'D':
1752 lf(vc);
1753 return;
1754 case 'H':
1755 vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
1756 return;
1757 case 'Z':
1758 respond_ID(tty);
1759 return;
1760 case '7':
1761 save_cur(vc);
1762 return;
1763 case '8':
1764 restore_cur(vc);
1765 return;
1766 case '(':
1767 vc->vc_state = ESsetG0;
1768 return;
1769 case ')':
1770 vc->vc_state = ESsetG1;
1771 return;
1772 case '#':
1773 vc->vc_state = EShash;
1774 return;
1775 case 'c':
1776 reset_terminal(vc, 1);
1777 return;
1778 case '>': /* Numeric keypad */
1779 clr_kbd(vc, kbdapplic);
1780 return;
1781 case '=': /* Appl. keypad */
1782 set_kbd(vc, kbdapplic);
1783 return;
1784 }
1785 return;
1786 case ESnonstd:
1787 if (c=='P') { /* palette escape sequence */
1788 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1789 vc->vc_par[vc->vc_npar] = 0;
1790 vc->vc_npar = 0;
1791 vc->vc_state = ESpalette;
1792 return;
1793 } else if (c=='R') { /* reset palette */
1794 reset_palette(vc);
1795 vc->vc_state = ESnormal;
1796 } else
1797 vc->vc_state = ESnormal;
1798 return;
1799 case ESpalette:
Andy Shevchenko74c807c2010-06-15 17:24:16 +03001800 if (isxdigit(c)) {
1801 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 if (vc->vc_npar == 7) {
1803 int i = vc->vc_par[0] * 3, j = 1;
1804 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1805 vc->vc_palette[i++] += vc->vc_par[j++];
1806 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1807 vc->vc_palette[i++] += vc->vc_par[j++];
1808 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1809 vc->vc_palette[i] += vc->vc_par[j];
1810 set_palette(vc);
1811 vc->vc_state = ESnormal;
1812 }
1813 } else
1814 vc->vc_state = ESnormal;
1815 return;
1816 case ESsquare:
1817 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1818 vc->vc_par[vc->vc_npar] = 0;
1819 vc->vc_npar = 0;
1820 vc->vc_state = ESgetpars;
1821 if (c == '[') { /* Function key */
1822 vc->vc_state=ESfunckey;
1823 return;
1824 }
1825 vc->vc_ques = (c == '?');
1826 if (vc->vc_ques)
1827 return;
1828 case ESgetpars:
1829 if (c == ';' && vc->vc_npar < NPAR - 1) {
1830 vc->vc_npar++;
1831 return;
1832 } else if (c>='0' && c<='9') {
1833 vc->vc_par[vc->vc_npar] *= 10;
1834 vc->vc_par[vc->vc_npar] += c - '0';
1835 return;
1836 } else
1837 vc->vc_state = ESgotpars;
1838 case ESgotpars:
1839 vc->vc_state = ESnormal;
1840 switch(c) {
1841 case 'h':
1842 set_mode(vc, 1);
1843 return;
1844 case 'l':
1845 set_mode(vc, 0);
1846 return;
1847 case 'c':
1848 if (vc->vc_ques) {
1849 if (vc->vc_par[0])
1850 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1851 else
Clemens Ladisch9ea9a882009-12-15 16:45:39 -08001852 vc->vc_cursor_type = cur_default;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return;
1854 }
1855 break;
1856 case 'm':
1857 if (vc->vc_ques) {
1858 clear_selection();
1859 if (vc->vc_par[0])
1860 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1861 else
1862 vc->vc_complement_mask = vc->vc_s_complement_mask;
1863 return;
1864 }
1865 break;
1866 case 'n':
1867 if (!vc->vc_ques) {
1868 if (vc->vc_par[0] == 5)
1869 status_report(tty);
1870 else if (vc->vc_par[0] == 6)
1871 cursor_report(vc, tty);
1872 }
1873 return;
1874 }
1875 if (vc->vc_ques) {
1876 vc->vc_ques = 0;
1877 return;
1878 }
1879 switch(c) {
1880 case 'G': case '`':
1881 if (vc->vc_par[0])
1882 vc->vc_par[0]--;
1883 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1884 return;
1885 case 'A':
1886 if (!vc->vc_par[0])
1887 vc->vc_par[0]++;
1888 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1889 return;
1890 case 'B': case 'e':
1891 if (!vc->vc_par[0])
1892 vc->vc_par[0]++;
1893 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1894 return;
1895 case 'C': case 'a':
1896 if (!vc->vc_par[0])
1897 vc->vc_par[0]++;
1898 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
1899 return;
1900 case 'D':
1901 if (!vc->vc_par[0])
1902 vc->vc_par[0]++;
1903 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
1904 return;
1905 case 'E':
1906 if (!vc->vc_par[0])
1907 vc->vc_par[0]++;
1908 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
1909 return;
1910 case 'F':
1911 if (!vc->vc_par[0])
1912 vc->vc_par[0]++;
1913 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
1914 return;
1915 case 'd':
1916 if (vc->vc_par[0])
1917 vc->vc_par[0]--;
1918 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
1919 return;
1920 case 'H': case 'f':
1921 if (vc->vc_par[0])
1922 vc->vc_par[0]--;
1923 if (vc->vc_par[1])
1924 vc->vc_par[1]--;
1925 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
1926 return;
1927 case 'J':
1928 csi_J(vc, vc->vc_par[0]);
1929 return;
1930 case 'K':
1931 csi_K(vc, vc->vc_par[0]);
1932 return;
1933 case 'L':
1934 csi_L(vc, vc->vc_par[0]);
1935 return;
1936 case 'M':
1937 csi_M(vc, vc->vc_par[0]);
1938 return;
1939 case 'P':
1940 csi_P(vc, vc->vc_par[0]);
1941 return;
1942 case 'c':
1943 if (!vc->vc_par[0])
1944 respond_ID(tty);
1945 return;
1946 case 'g':
1947 if (!vc->vc_par[0])
1948 vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
1949 else if (vc->vc_par[0] == 3) {
1950 vc->vc_tab_stop[0] =
1951 vc->vc_tab_stop[1] =
1952 vc->vc_tab_stop[2] =
1953 vc->vc_tab_stop[3] =
Wolfgang Kroworscha5647382008-11-06 12:53:16 -08001954 vc->vc_tab_stop[4] =
1955 vc->vc_tab_stop[5] =
1956 vc->vc_tab_stop[6] =
1957 vc->vc_tab_stop[7] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 return;
1960 case 'm':
1961 csi_m(vc);
1962 return;
1963 case 'q': /* DECLL - but only 3 leds */
1964 /* map 0,1,2,3 to 0,1,2,4 */
1965 if (vc->vc_par[0] < 4)
1966 setledstate(kbd_table + vc->vc_num,
1967 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
1968 return;
1969 case 'r':
1970 if (!vc->vc_par[0])
1971 vc->vc_par[0]++;
1972 if (!vc->vc_par[1])
1973 vc->vc_par[1] = vc->vc_rows;
1974 /* Minimum allowed region is 2 lines */
1975 if (vc->vc_par[0] < vc->vc_par[1] &&
1976 vc->vc_par[1] <= vc->vc_rows) {
1977 vc->vc_top = vc->vc_par[0] - 1;
1978 vc->vc_bottom = vc->vc_par[1];
1979 gotoxay(vc, 0, 0);
1980 }
1981 return;
1982 case 's':
1983 save_cur(vc);
1984 return;
1985 case 'u':
1986 restore_cur(vc);
1987 return;
1988 case 'X':
1989 csi_X(vc, vc->vc_par[0]);
1990 return;
1991 case '@':
1992 csi_at(vc, vc->vc_par[0]);
1993 return;
1994 case ']': /* setterm functions */
1995 setterm_command(vc);
1996 return;
1997 }
1998 return;
1999 case ESpercent:
2000 vc->vc_state = ESnormal;
2001 switch (c) {
2002 case '@': /* defined in ISO 2022 */
2003 vc->vc_utf = 0;
2004 return;
2005 case 'G': /* prelim official escape code */
2006 case '8': /* retained for compatibility */
2007 vc->vc_utf = 1;
2008 return;
2009 }
2010 return;
2011 case ESfunckey:
2012 vc->vc_state = ESnormal;
2013 return;
2014 case EShash:
2015 vc->vc_state = ESnormal;
2016 if (c == '8') {
2017 /* DEC screen alignment test. kludge :-) */
2018 vc->vc_video_erase_char =
2019 (vc->vc_video_erase_char & 0xff00) | 'E';
2020 csi_J(vc, 2);
2021 vc->vc_video_erase_char =
2022 (vc->vc_video_erase_char & 0xff00) | ' ';
2023 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2024 }
2025 return;
2026 case ESsetG0:
2027 if (c == '0')
2028 vc->vc_G0_charset = GRAF_MAP;
2029 else if (c == 'B')
2030 vc->vc_G0_charset = LAT1_MAP;
2031 else if (c == 'U')
2032 vc->vc_G0_charset = IBMPC_MAP;
2033 else if (c == 'K')
2034 vc->vc_G0_charset = USER_MAP;
2035 if (vc->vc_charset == 0)
2036 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2037 vc->vc_state = ESnormal;
2038 return;
2039 case ESsetG1:
2040 if (c == '0')
2041 vc->vc_G1_charset = GRAF_MAP;
2042 else if (c == 'B')
2043 vc->vc_G1_charset = LAT1_MAP;
2044 else if (c == 'U')
2045 vc->vc_G1_charset = IBMPC_MAP;
2046 else if (c == 'K')
2047 vc->vc_G1_charset = USER_MAP;
2048 if (vc->vc_charset == 1)
2049 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2050 vc->vc_state = ESnormal;
2051 return;
2052 default:
2053 vc->vc_state = ESnormal;
2054 }
2055}
2056
2057/* This is a temporary buffer used to prepare a tty console write
2058 * so that we can easily avoid touching user space while holding the
2059 * console spinlock. It is allocated in con_init and is shared by
2060 * this code and the vc_screen read/write tty calls.
2061 *
2062 * We have to allocate this statically in the kernel data section
2063 * since console_init (and thus con_init) are called before any
2064 * kernel memory allocation is available.
2065 */
2066char con_buf[CON_BUF_SIZE];
Matthias Kaehlckec831c332007-05-08 00:39:49 -07002067DEFINE_MUTEX(con_buf_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002069/* is_double_width() is based on the wcwidth() implementation by
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002070 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002071 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2072 */
2073struct interval {
2074 uint32_t first;
2075 uint32_t last;
2076};
2077
2078static int bisearch(uint32_t ucs, const struct interval *table, int max)
2079{
2080 int min = 0;
2081 int mid;
2082
2083 if (ucs < table[0].first || ucs > table[max].last)
2084 return 0;
2085 while (max >= min) {
2086 mid = (min + max) / 2;
2087 if (ucs > table[mid].last)
2088 min = mid + 1;
2089 else if (ucs < table[mid].first)
2090 max = mid - 1;
2091 else
2092 return 1;
2093 }
2094 return 0;
2095}
2096
2097static int is_double_width(uint32_t ucs)
2098{
2099 static const struct interval double_width[] = {
2100 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2101 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002102 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2103 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002104 };
Jiri Slaby0f115412007-07-17 04:05:21 -07002105 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002106}
2107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108/* acquires console_sem */
2109static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2110{
2111#ifdef VT_BUF_VRAM_ONLY
2112#define FLUSH do { } while(0);
2113#else
2114#define FLUSH if (draw_x >= 0) { \
2115 vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
2116 draw_x = -1; \
2117 }
2118#endif
2119
2120 int c, tc, ok, n = 0, draw_x = -1;
2121 unsigned int currcons;
2122 unsigned long draw_from = 0, draw_to = 0;
2123 struct vc_data *vc;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002124 unsigned char vc_attr;
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002125 struct vt_notifier_param param;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002126 uint8_t rescan;
2127 uint8_t inverse;
2128 uint8_t width;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 u16 himask, charmask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 if (in_interrupt())
2132 return count;
2133
2134 might_sleep();
2135
2136 acquire_console_sem();
2137 vc = tty->driver_data;
2138 if (vc == NULL) {
2139 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2140 release_console_sem();
2141 return 0;
2142 }
2143
2144 currcons = vc->vc_num;
2145 if (!vc_cons_allocated(currcons)) {
2146 /* could this happen? */
Marcin Slusarz9074d962009-08-09 21:54:03 +02002147 printk_once("con_write: tty %d not allocated\n", currcons+1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 release_console_sem();
2149 return 0;
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 himask = vc->vc_hi_font_mask;
2153 charmask = himask ? 0x1ff : 0xff;
2154
2155 /* undraw cursor first */
2156 if (IS_FG(vc))
2157 hide_cursor(vc);
2158
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002159 param.vc = vc;
2160
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 while (!tty->stopped && count) {
2162 int orig = *buf;
2163 c = orig;
2164 buf++;
2165 n++;
2166 count--;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002167 rescan = 0;
2168 inverse = 0;
2169 width = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170
2171 /* Do no translation at all in control states */
2172 if (vc->vc_state != ESnormal) {
2173 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002174 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002175 /* Combine UTF-8 into Unicode in vc_utf_char.
2176 * vc_utf_count is the number of continuation bytes still
2177 * expected to arrive.
2178 * vc_npar is the number of continuation bytes arrived so
2179 * far
2180 */
Adam Tlalkad4328b42006-09-29 01:59:53 -07002181rescan_last_byte:
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002182 if ((c & 0xc0) == 0x80) {
2183 /* Continuation byte received */
2184 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
Adam Tlalkad4328b42006-09-29 01:59:53 -07002185 if (vc->vc_utf_count) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002186 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2187 vc->vc_npar++;
2188 if (--vc->vc_utf_count) {
2189 /* Still need some bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 continue;
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002191 }
2192 /* Got a whole character */
2193 c = vc->vc_utf_char;
2194 /* Reject overlong sequences */
2195 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2196 c > utf8_length_changes[vc->vc_npar])
2197 c = 0xfffd;
2198 } else {
2199 /* Unexpected continuation byte */
2200 vc->vc_utf_count = 0;
2201 c = 0xfffd;
2202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002204 /* Single ASCII byte or first byte of a sequence received */
2205 if (vc->vc_utf_count) {
2206 /* Continuation byte expected */
2207 rescan = 1;
2208 vc->vc_utf_count = 0;
2209 c = 0xfffd;
2210 } else if (c > 0x7f) {
2211 /* First byte of a multibyte sequence received */
2212 vc->vc_npar = 0;
2213 if ((c & 0xe0) == 0xc0) {
2214 vc->vc_utf_count = 1;
2215 vc->vc_utf_char = (c & 0x1f);
2216 } else if ((c & 0xf0) == 0xe0) {
2217 vc->vc_utf_count = 2;
2218 vc->vc_utf_char = (c & 0x0f);
2219 } else if ((c & 0xf8) == 0xf0) {
2220 vc->vc_utf_count = 3;
2221 vc->vc_utf_char = (c & 0x07);
2222 } else if ((c & 0xfc) == 0xf8) {
2223 vc->vc_utf_count = 4;
2224 vc->vc_utf_char = (c & 0x03);
2225 } else if ((c & 0xfe) == 0xfc) {
2226 vc->vc_utf_count = 5;
2227 vc->vc_utf_char = (c & 0x01);
2228 } else {
2229 /* 254 and 255 are invalid */
2230 c = 0xfffd;
2231 }
2232 if (vc->vc_utf_count) {
2233 /* Still need some bytes */
2234 continue;
2235 }
2236 }
2237 /* Nothing to do if an ASCII byte was received */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 }
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002239 /* End of UTF-8 decoding. */
2240 /* c is the received character, or U+FFFD for invalid sequences. */
2241 /* Replace invalid Unicode code points with U+FFFD too */
2242 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2243 c = 0xfffd;
2244 tc = c;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002245 } else { /* no utf or alternate charset mode */
David Woodhousea29ccf6f82008-06-03 14:59:40 +01002246 tc = vc_translate(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Karl Dahlke0341a4d2008-04-28 02:14:25 -07002249 param.c = tc;
2250 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2251 &param) == NOTIFY_STOP)
2252 continue;
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 /* If the original code was a control character we
2255 * only allow a glyph to be displayed if the code is
2256 * not normally used (such as for cursor movement) or
2257 * if the disp_ctrl mode has been explicitly enabled.
2258 * Certain characters (as given by the CTRL_ALWAYS
2259 * bitmap) are always displayed as control characters,
2260 * as the console would be pretty useless without
2261 * them; to display an arbitrary font position use the
2262 * direct-to-font zone in UTF-8 mode.
2263 */
2264 ok = tc && (c >= 32 ||
Adam Tlalkad4328b42006-09-29 01:59:53 -07002265 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2266 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 && (c != 127 || vc->vc_disp_ctrl)
2268 && (c != 128+27);
2269
2270 if (vc->vc_state == ESnormal && ok) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002271 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2272 if (is_double_width(c))
2273 width = 2;
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 /* Now try to find out how to display it */
2276 tc = conv_uni_to_pc(vc, tc);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002277 if (tc & ~charmask) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002278 if (tc == -1 || tc == -2) {
2279 continue; /* nothing to display */
2280 }
2281 /* Glyph not found */
Samuel Thibaultc0b79882009-04-18 22:17:17 +02002282 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002283 /* In legacy mode use the glyph we get by a 1:1 mapping.
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002284 This would make absolutely no sense with Unicode in mind,
2285 but do this for ASCII characters since a font may lack
2286 Unicode mapping info and we don't want to end up with
2287 having question marks only. */
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002288 tc = c;
2289 } else {
2290 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2291 tc = conv_uni_to_pc(vc, 0xfffd);
2292 if (tc < 0) {
2293 inverse = 1;
2294 tc = conv_uni_to_pc(vc, '?');
2295 if (tc < 0) tc = '?';
2296 }
2297 }
Adam Tlalkad4328b42006-09-29 01:59:53 -07002298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002300 if (!inverse) {
2301 vc_attr = vc->vc_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 } else {
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002303 /* invert vc_attr */
2304 if (!vc->vc_can_do_color) {
2305 vc_attr = (vc->vc_attr) ^ 0x08;
2306 } else if (vc->vc_hi_font_mask == 0x100) {
2307 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2308 } else {
2309 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
Adam Tlalkad4328b42006-09-29 01:59:53 -07002310 }
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002311 FLUSH
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002312 }
2313
2314 while (1) {
2315 if (vc->vc_need_wrap || vc->vc_decim)
2316 FLUSH
2317 if (vc->vc_need_wrap) {
2318 cr(vc);
2319 lf(vc);
2320 }
2321 if (vc->vc_decim)
2322 insert_char(vc, 1);
2323 scr_writew(himask ?
2324 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2325 (vc_attr << 8) + tc,
2326 (u16 *) vc->vc_pos);
2327 if (DO_UPDATE(vc) && draw_x < 0) {
2328 draw_x = vc->vc_x;
2329 draw_from = vc->vc_pos;
2330 }
2331 if (vc->vc_x == vc->vc_cols - 1) {
2332 vc->vc_need_wrap = vc->vc_decawm;
2333 draw_to = vc->vc_pos + 2;
2334 } else {
2335 vc->vc_x++;
2336 draw_to = (vc->vc_pos += 2);
2337 }
2338
2339 if (!--width) break;
2340
2341 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2342 if (tc < 0) tc = ' ';
2343 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002344 notify_write(vc, c);
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002345
Egmont Koblinger1ed8a2b2007-06-23 17:16:27 -07002346 if (inverse) {
2347 FLUSH
2348 }
2349
Egmont Koblinger2f1a2cc2007-05-08 00:30:37 -07002350 if (rescan) {
2351 rescan = 0;
2352 inverse = 0;
2353 width = 1;
Adam Tlalkad4328b42006-09-29 01:59:53 -07002354 c = orig;
2355 goto rescan_last_byte;
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 continue;
2358 }
2359 FLUSH
2360 do_con_trol(tty, vc, orig);
2361 }
2362 FLUSH
2363 console_conditional_schedule();
2364 release_console_sem();
Samuel Thibaultb293d752007-10-18 23:39:17 -07002365 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 return n;
2367#undef FLUSH
2368}
2369
2370/*
2371 * This is the console switching callback.
2372 *
2373 * Doing console switching in a process context allows
2374 * us to do the switches asynchronously (needed when we want
2375 * to switch due to a keyboard interrupt). Synchronization
2376 * with other console code and prevention of re-entrancy is
2377 * ensured with console_sem.
2378 */
David Howells65f27f32006-11-22 14:55:48 +00002379static void console_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380{
2381 acquire_console_sem();
2382
2383 if (want_console >= 0) {
2384 if (want_console != fg_console &&
2385 vc_cons_allocated(want_console)) {
2386 hide_cursor(vc_cons[fg_console].d);
2387 change_console(vc_cons[want_console].d);
2388 /* we only changed when the console had already
2389 been allocated - a new console is not created
2390 in an interrupt routine */
2391 }
2392 want_console = -1;
2393 }
2394 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2395 do_poke_blanked_console = 0;
2396 poke_blanked_console();
2397 }
2398 if (scrollback_delta) {
2399 struct vc_data *vc = vc_cons[fg_console].d;
2400 clear_selection();
2401 if (vc->vc_mode == KD_TEXT)
2402 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2403 scrollback_delta = 0;
2404 }
2405 if (blank_timer_expired) {
2406 do_blank_screen(0);
2407 blank_timer_expired = 0;
2408 }
Samuel Thibaultb293d752007-10-18 23:39:17 -07002409 notify_update(vc_cons[fg_console].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
2411 release_console_sem();
2412}
2413
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002414int set_console(int nr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002416 struct vc_data *vc = vc_cons[fg_console].d;
2417
2418 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2419 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2420
2421 /*
2422 * Console switch will fail in console_callback() or
2423 * change_console() so there is no point scheduling
2424 * the callback
2425 *
2426 * Existing set_console() users don't check the return
2427 * value so this shouldn't break anything
2428 */
2429 return -EINVAL;
2430 }
2431
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 want_console = nr;
2433 schedule_console_callback();
Andrew Johnsonb257bc02007-03-16 13:38:24 -08002434
2435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438struct tty_driver *console_driver;
2439
2440#ifdef CONFIG_VT_CONSOLE
2441
Bernhard Walle5ada9182009-12-14 18:00:43 -08002442/**
2443 * vt_kmsg_redirect() - Sets/gets the kernel message console
2444 * @new: The new virtual terminal number or -1 if the console should stay
2445 * unchanged
2446 *
2447 * By default, the kernel messages are always printed on the current virtual
2448 * console. However, the user may modify that default with the
2449 * TIOCL_SETKMSGREDIRECT ioctl call.
2450 *
2451 * This function sets the kernel message console to be @new. It returns the old
2452 * virtual console number. The virtual terminal number 0 (both as parameter and
2453 * return value) means no redirection (i.e. always printed on the currently
2454 * active console).
2455 *
2456 * The parameter -1 means that only the current console is returned, but the
2457 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2458 * case to make the code more understandable.
2459 *
2460 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2461 * the parameter and always returns 0.
2462 */
2463int vt_kmsg_redirect(int new)
2464{
2465 static int kmsg_con;
2466
2467 if (new != -1)
2468 return xchg(&kmsg_con, new);
2469 else
2470 return kmsg_con;
2471}
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473/*
2474 * Console on virtual terminal
2475 *
2476 * The console must be locked when we get here.
2477 */
2478
2479static void vt_console_print(struct console *co, const char *b, unsigned count)
2480{
2481 struct vc_data *vc = vc_cons[fg_console].d;
2482 unsigned char c;
Nick Pigginb0940002008-02-06 01:37:04 -08002483 static DEFINE_SPINLOCK(printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 const ushort *start;
2485 ushort cnt = 0;
2486 ushort myx;
Bernhard Walle5ada9182009-12-14 18:00:43 -08002487 int kmsg_console;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
2489 /* console busy or not yet initialized */
Nick Pigginb0940002008-02-06 01:37:04 -08002490 if (!printable)
2491 return;
2492 if (!spin_trylock(&printing_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 return;
2494
Bernhard Walle5ada9182009-12-14 18:00:43 -08002495 kmsg_console = vt_get_kmsg_redirect();
2496 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2497 vc = vc_cons[kmsg_console - 1].d;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498
2499 /* read `x' only after setting currcons properly (otherwise
2500 the `x' macro will read the x of the foreground console). */
2501 myx = vc->vc_x;
2502
2503 if (!vc_cons_allocated(fg_console)) {
2504 /* impossible */
2505 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2506 goto quit;
2507 }
2508
2509 if (vc->vc_mode != KD_TEXT)
2510 goto quit;
2511
2512 /* undraw cursor first */
2513 if (IS_FG(vc))
2514 hide_cursor(vc);
2515
2516 start = (ushort *)vc->vc_pos;
2517
2518 /* Contrived structure to try to emulate original need_wrap behaviour
2519 * Problems caused when we have need_wrap set on '\n' character */
2520 while (count--) {
2521 c = *b++;
2522 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2523 if (cnt > 0) {
2524 if (CON_IS_VISIBLE(vc))
2525 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2526 vc->vc_x += cnt;
2527 if (vc->vc_need_wrap)
2528 vc->vc_x--;
2529 cnt = 0;
2530 }
2531 if (c == 8) { /* backspace */
2532 bs(vc);
2533 start = (ushort *)vc->vc_pos;
2534 myx = vc->vc_x;
2535 continue;
2536 }
2537 if (c != 13)
2538 lf(vc);
2539 cr(vc);
2540 start = (ushort *)vc->vc_pos;
2541 myx = vc->vc_x;
2542 if (c == 10 || c == 13)
2543 continue;
2544 }
2545 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002546 notify_write(vc, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 cnt++;
2548 if (myx == vc->vc_cols - 1) {
2549 vc->vc_need_wrap = 1;
2550 continue;
2551 }
2552 vc->vc_pos += 2;
2553 myx++;
2554 }
2555 if (cnt > 0) {
2556 if (CON_IS_VISIBLE(vc))
2557 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2558 vc->vc_x += cnt;
2559 if (vc->vc_x == vc->vc_cols) {
2560 vc->vc_x--;
2561 vc->vc_need_wrap = 1;
2562 }
2563 }
2564 set_cursor(vc);
Samuel Thibaultb293d752007-10-18 23:39:17 -07002565 notify_update(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567quit:
Nick Pigginb0940002008-02-06 01:37:04 -08002568 spin_unlock(&printing_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569}
2570
2571static struct tty_driver *vt_console_device(struct console *c, int *index)
2572{
2573 *index = c->index ? c->index-1 : fg_console;
2574 return console_driver;
2575}
2576
2577static struct console vt_console_driver = {
2578 .name = "tty",
2579 .write = vt_console_print,
2580 .device = vt_console_device,
2581 .unblank = unblank_screen,
2582 .flags = CON_PRINTBUFFER,
2583 .index = -1,
2584};
2585#endif
2586
2587/*
2588 * Handling of Linux-specific VC ioctls
2589 */
2590
2591/*
2592 * Generally a bit racy with respect to console_sem().
2593 *
2594 * There are some functions which don't need it.
2595 *
2596 * There are some functions which can sleep for arbitrary periods
2597 * (paste_selection) but we don't need the lock there anyway.
2598 *
2599 * set_selection has locking, and definitely needs it
2600 */
2601
2602int tioclinux(struct tty_struct *tty, unsigned long arg)
2603{
2604 char type, data;
2605 char __user *p = (char __user *)arg;
2606 int lines;
2607 int ret;
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2610 return -EPERM;
2611 if (get_user(type, p))
2612 return -EFAULT;
2613 ret = 0;
Alan Cox04f378b2008-04-30 00:53:29 -07002614
2615 lock_kernel();
2616
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 switch (type)
2618 {
2619 case TIOCL_SETSEL:
2620 acquire_console_sem();
2621 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2622 release_console_sem();
2623 break;
2624 case TIOCL_PASTESEL:
2625 ret = paste_selection(tty);
2626 break;
2627 case TIOCL_UNBLANKSCREEN:
Stephane Doyon2d237c62005-09-06 15:17:31 -07002628 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 unblank_screen();
Stephane Doyon2d237c62005-09-06 15:17:31 -07002630 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 break;
2632 case TIOCL_SELLOADLUT:
2633 ret = sel_loadlut(p);
2634 break;
2635 case TIOCL_GETSHIFTSTATE:
Alan Cox04f378b2008-04-30 00:53:29 -07002636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 /*
2638 * Make it possible to react to Shift+Mousebutton.
2639 * Note that 'shift_state' is an undocumented
2640 * kernel-internal variable; programs not closely
2641 * related to the kernel should not use this.
2642 */
2643 data = shift_state;
2644 ret = __put_user(data, p);
2645 break;
2646 case TIOCL_GETMOUSEREPORTING:
2647 data = mouse_reporting();
2648 ret = __put_user(data, p);
2649 break;
2650 case TIOCL_SETVESABLANK:
Yoichi Yuasa403aac92006-12-06 20:38:38 -08002651 ret = set_vesa_blanking(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 break;
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002653 case TIOCL_GETKMSGREDIRECT:
Bernhard Walle5ada9182009-12-14 18:00:43 -08002654 data = vt_get_kmsg_redirect();
Rafael J. Wysocki0ca07732006-03-31 02:30:58 -08002655 ret = __put_user(data, p);
2656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 case TIOCL_SETKMSGREDIRECT:
2658 if (!capable(CAP_SYS_ADMIN)) {
2659 ret = -EPERM;
2660 } else {
2661 if (get_user(data, p+1))
2662 ret = -EFAULT;
2663 else
Bernhard Walle5ada9182009-12-14 18:00:43 -08002664 vt_kmsg_redirect(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 break;
2667 case TIOCL_GETFGCONSOLE:
2668 ret = fg_console;
2669 break;
2670 case TIOCL_SCROLLCONSOLE:
2671 if (get_user(lines, (s32 __user *)(p+4))) {
2672 ret = -EFAULT;
2673 } else {
2674 scrollfront(vc_cons[fg_console].d, lines);
2675 ret = 0;
2676 }
2677 break;
2678 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
Stephane Doyon2d237c62005-09-06 15:17:31 -07002679 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 ignore_poke = 1;
2681 do_blank_screen(0);
Stephane Doyon2d237c62005-09-06 15:17:31 -07002682 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 break;
2684 case TIOCL_BLANKEDSCREEN:
2685 ret = console_blanked;
2686 break;
2687 default:
2688 ret = -EINVAL;
2689 break;
2690 }
Alan Cox04f378b2008-04-30 00:53:29 -07002691 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 return ret;
2693}
2694
2695/*
2696 * /dev/ttyN handling
2697 */
2698
2699static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2700{
2701 int retval;
2702
2703 retval = do_con_write(tty, buf, count);
2704 con_flush_chars(tty);
2705
2706 return retval;
2707}
2708
Alan Cox5d19f542008-04-30 00:54:08 -07002709static int con_put_char(struct tty_struct *tty, unsigned char ch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 if (in_interrupt())
Alan Cox5d19f542008-04-30 00:54:08 -07002712 return 0; /* n_r3964 calls put_char() from interrupt context */
2713 return do_con_write(tty, &ch, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
2716static int con_write_room(struct tty_struct *tty)
2717{
2718 if (tty->stopped)
2719 return 0;
Joe Petersona88a69c2009-01-02 13:40:53 +00002720 return 32768; /* No limit, really; we're not buffering */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721}
2722
2723static int con_chars_in_buffer(struct tty_struct *tty)
2724{
2725 return 0; /* we're not buffering */
2726}
2727
2728/*
2729 * con_throttle and con_unthrottle are only used for
2730 * paste_selection(), which has to stuff in a large number of
2731 * characters...
2732 */
2733static void con_throttle(struct tty_struct *tty)
2734{
2735}
2736
2737static void con_unthrottle(struct tty_struct *tty)
2738{
2739 struct vc_data *vc = tty->driver_data;
2740
2741 wake_up_interruptible(&vc->paste_wait);
2742}
2743
2744/*
2745 * Turn the Scroll-Lock LED on when the tty is stopped
2746 */
2747static void con_stop(struct tty_struct *tty)
2748{
2749 int console_num;
2750 if (!tty)
2751 return;
2752 console_num = tty->index;
2753 if (!vc_cons_allocated(console_num))
2754 return;
2755 set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2756 set_leds();
2757}
2758
2759/*
2760 * Turn the Scroll-Lock LED off when the console is started
2761 */
2762static void con_start(struct tty_struct *tty)
2763{
2764 int console_num;
2765 if (!tty)
2766 return;
2767 console_num = tty->index;
2768 if (!vc_cons_allocated(console_num))
2769 return;
2770 clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
2771 set_leds();
2772}
2773
2774static void con_flush_chars(struct tty_struct *tty)
2775{
2776 struct vc_data *vc;
2777
2778 if (in_interrupt()) /* from flush_to_ldisc */
2779 return;
2780
2781 /* if we race with con_close(), vt may be null */
2782 acquire_console_sem();
2783 vc = tty->driver_data;
2784 if (vc)
2785 set_cursor(vc);
2786 release_console_sem();
2787}
2788
2789/*
2790 * Allocate the console screen memory.
2791 */
2792static int con_open(struct tty_struct *tty, struct file *filp)
2793{
2794 unsigned int currcons = tty->index;
2795 int ret = 0;
2796
2797 acquire_console_sem();
Paul Mackerrasf7866482005-08-28 09:40:01 +10002798 if (tty->driver_data == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 ret = vc_allocate(currcons);
2800 if (ret == 0) {
2801 struct vc_data *vc = vc_cons[currcons].d;
Alan Coxfeebed62008-10-13 10:41:30 +01002802
2803 /* Still being freed */
2804 if (vc->vc_tty) {
2805 release_console_sem();
2806 return -ERESTARTSYS;
2807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 tty->driver_data = vc;
2809 vc->vc_tty = tty;
2810
2811 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2812 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2813 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2814 }
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002815 if (vc->vc_utf)
2816 tty->termios->c_iflag |= IUTF8;
2817 else
2818 tty->termios->c_iflag &= ~IUTF8;
Jiri Slabye0426e62008-07-23 21:29:58 -07002819 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return ret;
2821 }
2822 }
2823 release_console_sem();
2824 return ret;
2825}
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827static void con_close(struct tty_struct *tty, struct file *filp)
2828{
Alan Coxfeebed62008-10-13 10:41:30 +01002829 /* Nothing to do - we defer to shutdown */
2830}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Alan Coxfeebed62008-10-13 10:41:30 +01002832static void con_shutdown(struct tty_struct *tty)
2833{
2834 struct vc_data *vc = tty->driver_data;
2835 BUG_ON(vc == NULL);
2836 acquire_console_sem();
2837 vc->vc_tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 release_console_sem();
Alan Coxfeebed62008-10-13 10:41:30 +01002839 tty_shutdown(tty);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840}
2841
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002842static int default_italic_color = 2; // green (ASCII)
2843static int default_underline_color = 3; // cyan (ASCII)
2844module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2845module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2846
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847static void vc_init(struct vc_data *vc, unsigned int rows,
2848 unsigned int cols, int do_clear)
2849{
2850 int j, k ;
2851
2852 vc->vc_cols = cols;
2853 vc->vc_rows = rows;
2854 vc->vc_size_row = cols << 1;
2855 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2856
2857 set_origin(vc);
2858 vc->vc_pos = vc->vc_origin;
2859 reset_vc(vc);
2860 for (j=k=0; j<16; j++) {
2861 vc->vc_palette[k++] = default_red[j] ;
2862 vc->vc_palette[k++] = default_grn[j] ;
2863 vc->vc_palette[k++] = default_blu[j] ;
2864 }
2865 vc->vc_def_color = 0x07; /* white */
Jan Engelhardtfa6ce9a2007-05-08 00:38:04 -07002866 vc->vc_ulcolor = default_underline_color;
2867 vc->vc_itcolor = default_italic_color;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 vc->vc_halfcolor = 0x08; /* grey */
2869 init_waitqueue_head(&vc->paste_wait);
2870 reset_terminal(vc, do_clear);
2871}
2872
2873/*
2874 * This routine initializes console interrupts, and does nothing
2875 * else. If you want the screen to clear, call tty_write with
2876 * the appropriate escape-sequence.
2877 */
2878
2879static int __init con_init(void)
2880{
2881 const char *display_desc = NULL;
2882 struct vc_data *vc;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002883 unsigned int currcons = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 acquire_console_sem();
2886
2887 if (conswitchp)
2888 display_desc = conswitchp->con_startup();
2889 if (!display_desc) {
2890 fg_console = 0;
2891 release_console_sem();
2892 return 0;
2893 }
2894
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07002895 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
2896 struct con_driver *con_driver = &registered_con_driver[i];
2897
2898 if (con_driver->con == NULL) {
2899 con_driver->con = conswitchp;
2900 con_driver->desc = display_desc;
2901 con_driver->flag = CON_DRIVER_FLAG_INIT;
2902 con_driver->first = 0;
2903 con_driver->last = MAX_NR_CONSOLES - 1;
2904 break;
2905 }
2906 }
2907
2908 for (i = 0; i < MAX_NR_CONSOLES; i++)
2909 con_driver_map[i] = conswitchp;
2910
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (blankinterval) {
2912 blank_state = blank_normal_wait;
Daniel Mackf324edc2009-06-16 15:33:52 -07002913 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 }
2915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
Pekka Enberga5f4f522009-06-10 23:53:37 +03002917 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
Eric W. Biederman7f1f86a2007-02-13 14:38:58 -07002918 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 visual_init(vc, currcons, 1);
Pekka Enberga5f4f522009-06-10 23:53:37 +03002920 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 vc_init(vc, vc->vc_rows, vc->vc_cols,
2922 currcons || !vc->vc_sw->con_save_screen);
2923 }
2924 currcons = fg_console = 0;
2925 master_display_fg = vc = vc_cons[currcons].d;
2926 set_origin(vc);
2927 save_screen(vc);
2928 gotoxy(vc, vc->vc_x, vc->vc_y);
2929 csi_J(vc, 0);
2930 update_screen(vc);
2931 printk("Console: %s %s %dx%d",
2932 vc->vc_can_do_color ? "colour" : "mono",
2933 display_desc, vc->vc_cols, vc->vc_rows);
2934 printable = 1;
2935 printk("\n");
2936
2937 release_console_sem();
2938
2939#ifdef CONFIG_VT_CONSOLE
2940 register_console(&vt_console_driver);
2941#endif
2942 return 0;
2943}
2944console_initcall(con_init);
2945
Jeff Dikeb68e31d2006-10-02 02:17:18 -07002946static const struct tty_operations con_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 .open = con_open,
2948 .close = con_close,
2949 .write = con_write,
2950 .write_room = con_write_room,
2951 .put_char = con_put_char,
2952 .flush_chars = con_flush_chars,
2953 .chars_in_buffer = con_chars_in_buffer,
2954 .ioctl = vt_ioctl,
Arnd Bergmanne9216652009-08-06 15:09:28 +02002955#ifdef CONFIG_COMPAT
2956 .compat_ioctl = vt_compat_ioctl,
2957#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 .stop = con_stop,
2959 .start = con_start,
2960 .throttle = con_throttle,
2961 .unthrottle = con_unthrottle,
Alan Cox8c9a9dd2008-08-15 10:39:38 +01002962 .resize = vt_resize,
Alan Coxfeebed62008-10-13 10:41:30 +01002963 .shutdown = con_shutdown
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964};
2965
Alan Coxd81ed102008-10-13 10:41:42 +01002966static struct cdev vc0_cdev;
2967
2968int __init vty_init(const struct file_operations *console_fops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969{
Alan Coxd81ed102008-10-13 10:41:42 +01002970 cdev_init(&vc0_cdev, console_fops);
2971 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
2972 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
2973 panic("Couldn't register /dev/tty0 driver\n");
2974 device_create(tty_class, NULL, MKDEV(TTY_MAJOR, 0), NULL, "tty0");
2975
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 vcs_init();
2977
2978 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
2979 if (!console_driver)
2980 panic("Couldn't allocate console driver\n");
2981 console_driver->owner = THIS_MODULE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 console_driver->name = "tty";
2983 console_driver->name_base = 1;
2984 console_driver->major = TTY_MAJOR;
2985 console_driver->minor_start = 1;
2986 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
2987 console_driver->init_termios = tty_std_termios;
Samuel Thibaultc1236d32008-05-06 20:42:37 -07002988 if (default_utf8)
2989 console_driver->init_termios.c_iflag |= IUTF8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
2991 tty_set_operations(console_driver, &con_ops);
2992 if (tty_register_driver(console_driver))
2993 panic("Couldn't register console driver\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 kbd_init();
2995 console_map_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996#ifdef CONFIG_MDA_CONSOLE
2997 mda_console_init();
2998#endif
2999 return 0;
3000}
3001
3002#ifndef VT_SINGLE_DRIVER
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003003
3004static struct class *vtconsole_class;
3005
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003006static int bind_con_driver(const struct consw *csw, int first, int last,
3007 int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003009 struct module *owner = csw->owner;
3010 const char *desc = NULL;
3011 struct con_driver *con_driver;
3012 int i, j = -1, k = -1, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (!try_module_get(owner))
3015 return -ENODEV;
3016
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003017 acquire_console_sem();
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003018
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003019 /* check if driver is registered */
3020 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3021 con_driver = &registered_con_driver[i];
3022
3023 if (con_driver->con == csw) {
3024 desc = con_driver->desc;
3025 retval = 0;
3026 break;
3027 }
3028 }
3029
3030 if (retval)
3031 goto err;
3032
3033 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3034 csw->con_startup();
3035 con_driver->flag |= CON_DRIVER_FLAG_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 if (deflt) {
3039 if (conswitchp)
3040 module_put(conswitchp->owner);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 __module_get(owner);
3043 conswitchp = csw;
3044 }
3045
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003046 first = max(first, con_driver->first);
3047 last = min(last, con_driver->last);
3048
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 for (i = first; i <= last; i++) {
3050 int old_was_color;
3051 struct vc_data *vc = vc_cons[i].d;
3052
3053 if (con_driver_map[i])
3054 module_put(con_driver_map[i]->owner);
3055 __module_get(owner);
3056 con_driver_map[i] = csw;
3057
3058 if (!vc || !vc->vc_sw)
3059 continue;
3060
3061 j = i;
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003062
David Hollister4ee1acc2006-06-26 00:26:41 -07003063 if (CON_IS_VISIBLE(vc)) {
3064 k = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 save_screen(vc);
David Hollister4ee1acc2006-06-26 00:26:41 -07003066 }
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 old_was_color = vc->vc_can_do_color;
3069 vc->vc_sw->con_deinit(vc);
qiaochong02f07772010-08-09 17:21:23 -07003070 if (!vc->vc_origin)
3071 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 visual_init(vc, i, 0);
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003073 set_origin(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 update_attr(vc);
3075
3076 /* If the console changed between mono <-> color, then
3077 * the attributes in the screenbuf will be wrong. The
3078 * following resets all attributes to something sane.
3079 */
3080 if (old_was_color != vc->vc_can_do_color)
3081 clear_buffer_attributes(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 }
David Hollister4ee1acc2006-06-26 00:26:41 -07003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 printk("Console: switching ");
3085 if (!deflt)
3086 printk("consoles %d-%d ", first+1, last+1);
David Hollister4ee1acc2006-06-26 00:26:41 -07003087 if (j >= 0) {
3088 struct vc_data *vc = vc_cons[j].d;
3089
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 printk("to %s %s %dx%d\n",
David Hollister4ee1acc2006-06-26 00:26:41 -07003091 vc->vc_can_do_color ? "colour" : "mono",
3092 desc, vc->vc_cols, vc->vc_rows);
3093
3094 if (k >= 0) {
3095 vc = vc_cons[k].d;
3096 update_screen(vc);
3097 }
3098 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 printk("to %s\n", desc);
3100
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003101 retval = 0;
3102err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 module_put(owner);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003105 return retval;
3106};
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003108#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3109static int con_is_graphics(const struct consw *csw, int first, int last)
3110{
3111 int i, retval = 0;
3112
3113 for (i = first; i <= last; i++) {
3114 struct vc_data *vc = vc_cons[i].d;
3115
3116 if (vc && vc->vc_mode == KD_GRAPHICS) {
3117 retval = 1;
3118 break;
3119 }
3120 }
3121
3122 return retval;
3123}
3124
Jesse Barnesb7269dd2007-07-17 04:05:34 -07003125/**
3126 * unbind_con_driver - unbind a console driver
3127 * @csw: pointer to console driver to unregister
3128 * @first: first in range of consoles that @csw should be unbound from
3129 * @last: last in range of consoles that @csw should be unbound from
3130 * @deflt: should next bound console driver be default after @csw is unbound?
3131 *
3132 * To unbind a driver from all possible consoles, pass 0 as @first and
3133 * %MAX_NR_CONSOLES as @last.
3134 *
3135 * @deflt controls whether the console that ends up replacing @csw should be
3136 * the default console.
3137 *
3138 * RETURNS:
3139 * -ENODEV if @csw isn't a registered console driver or can't be unregistered
3140 * or 0 on success.
3141 */
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003142int unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143{
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003144 struct module *owner = csw->owner;
3145 const struct consw *defcsw = NULL;
3146 struct con_driver *con_driver = NULL, *con_back = NULL;
3147 int i, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003149 if (!try_module_get(owner))
3150 return -ENODEV;
3151
3152 acquire_console_sem();
3153
3154 /* check if driver is registered and if it is unbindable */
3155 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3156 con_driver = &registered_con_driver[i];
3157
3158 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003159 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003160 retval = 0;
3161 break;
3162 }
3163 }
3164
3165 if (retval) {
3166 release_console_sem();
3167 goto err;
3168 }
3169
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003170 retval = -ENODEV;
3171
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003172 /* check if backup driver exists */
3173 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3174 con_back = &registered_con_driver[i];
3175
3176 if (con_back->con &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003177 !(con_back->flag & CON_DRIVER_FLAG_MODULE)) {
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003178 defcsw = con_back->con;
3179 retval = 0;
3180 break;
3181 }
3182 }
3183
3184 if (retval) {
3185 release_console_sem();
3186 goto err;
3187 }
3188
3189 if (!con_is_bound(csw)) {
3190 release_console_sem();
3191 goto err;
3192 }
3193
3194 first = max(first, con_driver->first);
3195 last = min(last, con_driver->last);
3196
3197 for (i = first; i <= last; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (con_driver_map[i] == csw) {
3199 module_put(csw->owner);
3200 con_driver_map[i] = NULL;
3201 }
Antonino A. Daplas1c8ce272006-06-26 00:27:03 -07003202 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003203
3204 if (!con_is_bound(defcsw)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003205 const struct consw *defconsw = conswitchp;
3206
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003207 defcsw->con_startup();
3208 con_back->flag |= CON_DRIVER_FLAG_INIT;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003209 /*
3210 * vgacon may change the default driver to point
3211 * to dummycon, we restore it here...
3212 */
3213 conswitchp = defconsw;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003214 }
3215
3216 if (!con_is_bound(csw))
3217 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3218
3219 release_console_sem();
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003220 /* ignore return value, binding should not fail */
3221 bind_con_driver(defcsw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003222err:
3223 module_put(owner);
3224 return retval;
3225
3226}
Antonino A. Daplas623e71b2007-07-17 04:05:28 -07003227EXPORT_SYMBOL(unbind_con_driver);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003228
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003229static int vt_bind(struct con_driver *con)
3230{
3231 const struct consw *defcsw = NULL, *csw = NULL;
3232 int i, more = 1, first = -1, last = -1, deflt = 0;
3233
3234 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3235 con_is_graphics(con->con, con->first, con->last))
3236 goto err;
3237
3238 csw = con->con;
3239
3240 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3241 struct con_driver *con = &registered_con_driver[i];
3242
3243 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3244 defcsw = con->con;
3245 break;
3246 }
3247 }
3248
3249 if (!defcsw)
3250 goto err;
3251
3252 while (more) {
3253 more = 0;
3254
3255 for (i = con->first; i <= con->last; i++) {
3256 if (con_driver_map[i] == defcsw) {
3257 if (first == -1)
3258 first = i;
3259 last = i;
3260 more = 1;
3261 } else if (first != -1)
3262 break;
3263 }
3264
3265 if (first == 0 && last == MAX_NR_CONSOLES -1)
3266 deflt = 1;
3267
3268 if (first != -1)
3269 bind_con_driver(csw, first, last, deflt);
3270
3271 first = -1;
3272 last = -1;
3273 deflt = 0;
3274 }
3275
3276err:
3277 return 0;
3278}
3279
3280static int vt_unbind(struct con_driver *con)
3281{
3282 const struct consw *csw = NULL;
3283 int i, more = 1, first = -1, last = -1, deflt = 0;
3284
3285 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE) ||
3286 con_is_graphics(con->con, con->first, con->last))
3287 goto err;
3288
3289 csw = con->con;
3290
3291 while (more) {
3292 more = 0;
3293
3294 for (i = con->first; i <= con->last; i++) {
3295 if (con_driver_map[i] == csw) {
3296 if (first == -1)
3297 first = i;
3298 last = i;
3299 more = 1;
3300 } else if (first != -1)
3301 break;
3302 }
3303
3304 if (first == 0 && last == MAX_NR_CONSOLES -1)
3305 deflt = 1;
3306
3307 if (first != -1)
3308 unbind_con_driver(csw, first, last, deflt);
3309
3310 first = -1;
3311 last = -1;
3312 deflt = 0;
3313 }
3314
3315err:
3316 return 0;
3317}
Antonino A. Daplas13ae6642006-06-26 00:27:12 -07003318#else
3319static inline int vt_bind(struct con_driver *con)
3320{
3321 return 0;
3322}
3323static inline int vt_unbind(struct con_driver *con)
3324{
3325 return 0;
3326}
3327#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003328
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003329static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003330 const char *buf, size_t count)
3331{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003332 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003333 int bind = simple_strtoul(buf, NULL, 0);
3334
3335 if (bind)
3336 vt_bind(con);
3337 else
3338 vt_unbind(con);
3339
3340 return count;
3341}
3342
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003343static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3344 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003345{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003346 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003347 int bind = con_is_bound(con->con);
3348
3349 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3350}
3351
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003352static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3353 char *buf)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003354{
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003355 struct con_driver *con = dev_get_drvdata(dev);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003356
3357 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3358 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3359 con->desc);
3360
3361}
3362
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003363static struct device_attribute device_attrs[] = {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003364 __ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind),
3365 __ATTR(name, S_IRUGO, show_name, NULL),
3366};
3367
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003368static int vtconsole_init_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003369{
3370 int i;
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003371 int error = 0;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003372
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003373 con->flag |= CON_DRIVER_FLAG_ATTR;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003374 dev_set_drvdata(con->dev, con);
3375 for (i = 0; i < ARRAY_SIZE(device_attrs); i++) {
3376 error = device_create_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003377 if (error)
3378 break;
3379 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003380
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003381 if (error) {
3382 while (--i >= 0)
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003383 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003384 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3385 }
3386
3387 return error;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003388}
3389
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003390static void vtconsole_deinit_device(struct con_driver *con)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003391{
3392 int i;
3393
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003394 if (con->flag & CON_DRIVER_FLAG_ATTR) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003395 for (i = 0; i < ARRAY_SIZE(device_attrs); i++)
3396 device_remove_file(con->dev, &device_attrs[i]);
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003397 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3398 }
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003399}
3400
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003401/**
3402 * con_is_bound - checks if driver is bound to the console
3403 * @csw: console driver
3404 *
3405 * RETURNS: zero if unbound, nonzero if bound
3406 *
3407 * Drivers can call this and if zero, they should release
3408 * all resources allocated on con_startup()
3409 */
3410int con_is_bound(const struct consw *csw)
3411{
3412 int i, bound = 0;
3413
3414 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3415 if (con_driver_map[i] == csw) {
3416 bound = 1;
3417 break;
3418 }
3419 }
3420
3421 return bound;
3422}
3423EXPORT_SYMBOL(con_is_bound);
3424
3425/**
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003426 * con_debug_enter - prepare the console for the kernel debugger
3427 * @sw: console driver
3428 *
3429 * Called when the console is taken over by the kernel debugger, this
3430 * function needs to save the current console state, then put the console
3431 * into a state suitable for the kernel debugger.
3432 *
3433 * RETURNS:
3434 * Zero on success, nonzero if a failure occurred when trying to prepare
3435 * the console for the debugger.
3436 */
3437int con_debug_enter(struct vc_data *vc)
3438{
3439 int ret = 0;
3440
3441 saved_fg_console = fg_console;
3442 saved_last_console = last_console;
3443 saved_want_console = want_console;
3444 saved_vc_mode = vc->vc_mode;
3445 vc->vc_mode = KD_TEXT;
3446 console_blanked = 0;
3447 if (vc->vc_sw->con_debug_enter)
3448 ret = vc->vc_sw->con_debug_enter(vc);
Jason Wessel81d44502010-08-05 09:22:30 -05003449#ifdef CONFIG_KGDB_KDB
3450 /* Set the initial LINES variable if it is not already set */
3451 if (vc->vc_rows < 999) {
3452 int linecount;
3453 char lns[4];
3454 const char *setargs[3] = {
3455 "set",
3456 "LINES",
3457 lns,
3458 };
3459 if (kdbgetintenv(setargs[0], &linecount)) {
3460 snprintf(lns, 4, "%i", vc->vc_rows);
3461 kdb_set(2, setargs);
3462 }
3463 }
3464#endif /* CONFIG_KGDB_KDB */
Jesse Barnesb45cfba2010-08-05 09:22:30 -05003465 return ret;
3466}
3467EXPORT_SYMBOL_GPL(con_debug_enter);
3468
3469/**
3470 * con_debug_leave - restore console state
3471 * @sw: console driver
3472 *
3473 * Restore the console state to what it was before the kernel debugger
3474 * was invoked.
3475 *
3476 * RETURNS:
3477 * Zero on success, nonzero if a failure occurred when trying to restore
3478 * the console.
3479 */
3480int con_debug_leave(void)
3481{
3482 struct vc_data *vc;
3483 int ret = 0;
3484
3485 fg_console = saved_fg_console;
3486 last_console = saved_last_console;
3487 want_console = saved_want_console;
3488 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3489
3490 vc = vc_cons[fg_console].d;
3491 if (vc->vc_sw->con_debug_leave)
3492 ret = vc->vc_sw->con_debug_leave(vc);
3493 return ret;
3494}
3495EXPORT_SYMBOL_GPL(con_debug_leave);
3496
3497/**
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003498 * register_con_driver - register console driver to console layer
3499 * @csw: console driver
3500 * @first: the first console to take over, minimum value is 0
3501 * @last: the last console to take over, maximum value is MAX_NR_CONSOLES -1
3502 *
3503 * DESCRIPTION: This function registers a console driver which can later
3504 * bind to a range of consoles specified by @first and @last. It will
3505 * also initialize the console driver by calling con_startup().
3506 */
3507int register_con_driver(const struct consw *csw, int first, int last)
3508{
3509 struct module *owner = csw->owner;
3510 struct con_driver *con_driver;
3511 const char *desc;
3512 int i, retval = 0;
3513
3514 if (!try_module_get(owner))
3515 return -ENODEV;
3516
3517 acquire_console_sem();
3518
3519 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3520 con_driver = &registered_con_driver[i];
3521
3522 /* already registered */
3523 if (con_driver->con == csw)
3524 retval = -EINVAL;
3525 }
3526
3527 if (retval)
3528 goto err;
3529
3530 desc = csw->con_startup();
3531
3532 if (!desc)
3533 goto err;
3534
3535 retval = -EINVAL;
3536
3537 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3538 con_driver = &registered_con_driver[i];
3539
3540 if (con_driver->con == NULL) {
3541 con_driver->con = csw;
3542 con_driver->desc = desc;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003543 con_driver->node = i;
3544 con_driver->flag = CON_DRIVER_FLAG_MODULE |
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003545 CON_DRIVER_FLAG_INIT;
3546 con_driver->first = first;
3547 con_driver->last = last;
3548 retval = 0;
3549 break;
3550 }
3551 }
3552
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003553 if (retval)
3554 goto err;
3555
Alan Coxd81ed102008-10-13 10:41:42 +01003556 con_driver->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003557 MKDEV(0, con_driver->node),
3558 NULL, "vtcon%i",
3559 con_driver->node);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003560
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003561 if (IS_ERR(con_driver->dev)) {
3562 printk(KERN_WARNING "Unable to create device for %s; "
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003563 "errno = %ld\n", con_driver->desc,
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003564 PTR_ERR(con_driver->dev));
3565 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003566 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003567 vtconsole_init_device(con_driver);
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003568 }
Antonino A. Daplas928e9642006-10-03 01:14:49 -07003569
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003570err:
3571 release_console_sem();
3572 module_put(owner);
3573 return retval;
3574}
3575EXPORT_SYMBOL(register_con_driver);
3576
3577/**
3578 * unregister_con_driver - unregister console driver from console layer
3579 * @csw: console driver
3580 *
3581 * DESCRIPTION: All drivers that registers to the console layer must
3582 * call this function upon exit, or if the console driver is in a state
3583 * where it won't be able to handle console services, such as the
3584 * framebuffer console without loaded framebuffer drivers.
3585 *
3586 * The driver must unbind first prior to unregistration.
3587 */
3588int unregister_con_driver(const struct consw *csw)
3589{
3590 int i, retval = -ENODEV;
3591
3592 acquire_console_sem();
3593
3594 /* cannot unregister a bound driver */
3595 if (con_is_bound(csw))
3596 goto err;
3597
3598 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3599 struct con_driver *con_driver = &registered_con_driver[i];
3600
3601 if (con_driver->con == csw &&
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003602 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003603 vtconsole_deinit_device(con_driver);
3604 device_destroy(vtconsole_class,
3605 MKDEV(0, con_driver->node));
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003606 con_driver->con = NULL;
3607 con_driver->desc = NULL;
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003608 con_driver->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003609 con_driver->node = 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003610 con_driver->flag = 0;
3611 con_driver->first = 0;
3612 con_driver->last = 0;
3613 retval = 0;
3614 break;
3615 }
3616 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003617err:
3618 release_console_sem();
3619 return retval;
3620}
3621EXPORT_SYMBOL(unregister_con_driver);
3622
3623/*
3624 * If we support more console drivers, this function is used
3625 * when a driver wants to take over some existing consoles
3626 * and become default driver for newly opened ones.
3627 *
3628 * take_over_console is basically a register followed by unbind
3629 */
3630int take_over_console(const struct consw *csw, int first, int last, int deflt)
3631{
3632 int err;
3633
3634 err = register_con_driver(csw, first, last);
3635
3636 if (!err)
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003637 bind_con_driver(csw, first, last, deflt);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003638
3639 return err;
3640}
3641
3642/*
3643 * give_up_console is a wrapper to unregister_con_driver. It will only
3644 * work if driver is fully unbound.
3645 */
3646void give_up_console(const struct consw *csw)
3647{
3648 unregister_con_driver(csw);
3649}
3650
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003651static int __init vtconsole_class_init(void)
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003652{
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003653 int i;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003654
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003655 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3656 if (IS_ERR(vtconsole_class)) {
3657 printk(KERN_WARNING "Unable to create vt console class; "
3658 "errno = %ld\n", PTR_ERR(vtconsole_class));
3659 vtconsole_class = NULL;
3660 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003661
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003662 /* Add system drivers to sysfs */
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003663 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3664 struct con_driver *con = &registered_con_driver[i];
3665
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003666 if (con->con && !con->dev) {
Alan Coxd81ed102008-10-13 10:41:42 +01003667 con->dev = device_create(vtconsole_class, NULL,
Greg Kroah-Hartman47aa5792008-05-21 12:52:33 -07003668 MKDEV(0, con->node),
3669 NULL, "vtcon%i",
3670 con->node);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003671
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003672 if (IS_ERR(con->dev)) {
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003673 printk(KERN_WARNING "Unable to create "
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003674 "device for %s; errno = %ld\n",
3675 con->desc, PTR_ERR(con->dev));
3676 con->dev = NULL;
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003677 } else {
Greg Kroah-Hartman805952a2006-08-07 22:19:37 -07003678 vtconsole_init_device(con);
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003679 }
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003680 }
3681 }
3682
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003683 return 0;
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003684}
Antonino A. Daplas6db40632006-06-26 00:27:12 -07003685postcore_initcall(vtconsole_class_init);
3686
3687#endif
Antonino A. Daplas3e795de2006-06-26 00:27:08 -07003688
3689/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 * Screen blanking
3691 */
3692
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003693static int set_vesa_blanking(char __user *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003694{
Yoichi Yuasa403aac92006-12-06 20:38:38 -08003695 unsigned int mode;
3696
3697 if (get_user(mode, p + 1))
3698 return -EFAULT;
3699
3700 vesa_blank_mode = (mode < 4) ? mode : 0;
3701 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704void do_blank_screen(int entering_gfx)
3705{
3706 struct vc_data *vc = vc_cons[fg_console].d;
3707 int i;
3708
3709 WARN_CONSOLE_UNLOCKED();
3710
3711 if (console_blanked) {
3712 if (blank_state == blank_vesa_wait) {
3713 blank_state = blank_off;
Ville Syrjalad060a322006-01-09 20:53:49 -08003714 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 }
3716 return;
3717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718
3719 /* entering graphics mode? */
3720 if (entering_gfx) {
3721 hide_cursor(vc);
3722 save_screen(vc);
3723 vc->vc_sw->con_blank(vc, -1, 1);
3724 console_blanked = fg_console + 1;
izumib6e8f002007-07-17 04:05:49 -07003725 blank_state = blank_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 set_origin(vc);
3727 return;
3728 }
3729
izumib6e8f002007-07-17 04:05:49 -07003730 if (blank_state != blank_normal_wait)
3731 return;
3732 blank_state = blank_off;
3733
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 /* don't blank graphics */
3735 if (vc->vc_mode != KD_TEXT) {
3736 console_blanked = fg_console + 1;
3737 return;
3738 }
3739
3740 hide_cursor(vc);
3741 del_timer_sync(&console_timer);
3742 blank_timer_expired = 0;
3743
3744 save_screen(vc);
3745 /* In case we need to reset origin, blanking hook returns 1 */
Ville Syrjalad060a322006-01-09 20:53:49 -08003746 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 console_blanked = fg_console + 1;
3748 if (i)
3749 set_origin(vc);
3750
3751 if (console_blank_hook && console_blank_hook(1))
3752 return;
3753
Ville Syrjalad060a322006-01-09 20:53:49 -08003754 if (vesa_off_interval && vesa_blank_mode) {
Nishanth Aravamudan030baba2005-07-15 03:56:25 -07003755 blank_state = blank_vesa_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756 mod_timer(&console_timer, jiffies + vesa_off_interval);
3757 }
Alan Cox8b92e872009-09-19 13:13:24 -07003758 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759}
3760EXPORT_SYMBOL(do_blank_screen);
3761
3762/*
3763 * Called by timer as well as from vt_console_driver
3764 */
3765void do_unblank_screen(int leaving_gfx)
3766{
3767 struct vc_data *vc;
3768
3769 /* This should now always be called from a "sane" (read: can schedule)
3770 * context for the sake of the low level drivers, except in the special
3771 * case of oops_in_progress
3772 */
3773 if (!oops_in_progress)
3774 might_sleep();
3775
3776 WARN_CONSOLE_UNLOCKED();
3777
3778 ignore_poke = 0;
3779 if (!console_blanked)
3780 return;
3781 if (!vc_cons_allocated(fg_console)) {
3782 /* impossible */
3783 printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
3784 return;
3785 }
3786 vc = vc_cons[fg_console].d;
3787 if (vc->vc_mode != KD_TEXT)
3788 return; /* but leave console_blanked != 0 */
3789
3790 if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003791 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792 blank_state = blank_normal_wait;
3793 }
3794
3795 console_blanked = 0;
3796 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
3797 /* Low-level driver cannot restore -> do it ourselves */
3798 update_screen(vc);
3799 if (console_blank_hook)
3800 console_blank_hook(0);
3801 set_palette(vc);
3802 set_cursor(vc);
Alan Cox8b92e872009-09-19 13:13:24 -07003803 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805EXPORT_SYMBOL(do_unblank_screen);
3806
3807/*
3808 * This is called by the outside world to cause a forced unblank, mostly for
3809 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3810 * call it with 1 as an argument and so force a mode restore... that may kill
3811 * X or at least garbage the screen but would also make the Oops visible...
3812 */
3813void unblank_screen(void)
3814{
3815 do_unblank_screen(0);
3816}
3817
3818/*
Ingo Molnar70522e12006-03-23 03:00:31 -08003819 * We defer the timer blanking to work queue so it can take the console mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 * (console operations can still happen at irq time, but only from printk which
Ingo Molnar70522e12006-03-23 03:00:31 -08003821 * has the console mutex. Not perfect yet, but better than no locking
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 */
3823static void blank_screen_t(unsigned long dummy)
3824{
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003825 if (unlikely(!keventd_up())) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003826 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Jan Beulichcc63b1e2005-06-17 13:20:58 -07003827 return;
3828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 blank_timer_expired = 1;
3830 schedule_work(&console_work);
3831}
3832
3833void poke_blanked_console(void)
3834{
3835 WARN_CONSOLE_UNLOCKED();
3836
3837 /* Add this so we quickly catch whoever might call us in a non
3838 * safe context. Nowadays, unblank_screen() isn't to be called in
3839 * atomic contexts and is allowed to schedule (with the special case
3840 * of oops_in_progress, but that isn't of any concern for this
3841 * function. --BenH.
3842 */
3843 might_sleep();
3844
3845 /* This isn't perfectly race free, but a race here would be mostly harmless,
3846 * at worse, we'll do a spurrious blank and it's unlikely
3847 */
3848 del_timer(&console_timer);
3849 blank_timer_expired = 0;
3850
3851 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3852 return;
3853 if (console_blanked)
3854 unblank_screen();
3855 else if (blankinterval) {
Daniel Mackf324edc2009-06-16 15:33:52 -07003856 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857 blank_state = blank_normal_wait;
3858 }
3859}
3860
3861/*
3862 * Palettes
3863 */
3864
3865static void set_palette(struct vc_data *vc)
3866{
3867 WARN_CONSOLE_UNLOCKED();
3868
3869 if (vc->vc_mode != KD_GRAPHICS)
3870 vc->vc_sw->con_set_palette(vc, color_table);
3871}
3872
3873static int set_get_cmap(unsigned char __user *arg, int set)
3874{
3875 int i, j, k;
3876
3877 WARN_CONSOLE_UNLOCKED();
3878
3879 for (i = 0; i < 16; i++)
3880 if (set) {
3881 get_user(default_red[i], arg++);
3882 get_user(default_grn[i], arg++);
3883 get_user(default_blu[i], arg++);
3884 } else {
3885 put_user(default_red[i], arg++);
3886 put_user(default_grn[i], arg++);
3887 put_user(default_blu[i], arg++);
3888 }
3889 if (set) {
3890 for (i = 0; i < MAX_NR_CONSOLES; i++)
3891 if (vc_cons_allocated(i)) {
3892 for (j = k = 0; j < 16; j++) {
3893 vc_cons[i].d->vc_palette[k++] = default_red[j];
3894 vc_cons[i].d->vc_palette[k++] = default_grn[j];
3895 vc_cons[i].d->vc_palette[k++] = default_blu[j];
3896 }
3897 set_palette(vc_cons[i].d);
3898 }
3899 }
3900 return 0;
3901}
3902
3903/*
3904 * Load palette into the DAC registers. arg points to a colour
3905 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
3906 */
3907
3908int con_set_cmap(unsigned char __user *arg)
3909{
3910 int rc;
3911
3912 acquire_console_sem();
3913 rc = set_get_cmap (arg,1);
3914 release_console_sem();
3915
3916 return rc;
3917}
3918
3919int con_get_cmap(unsigned char __user *arg)
3920{
3921 int rc;
3922
3923 acquire_console_sem();
3924 rc = set_get_cmap (arg,0);
3925 release_console_sem();
3926
3927 return rc;
3928}
3929
3930void reset_palette(struct vc_data *vc)
3931{
3932 int j, k;
3933 for (j=k=0; j<16; j++) {
3934 vc->vc_palette[k++] = default_red[j];
3935 vc->vc_palette[k++] = default_grn[j];
3936 vc->vc_palette[k++] = default_blu[j];
3937 }
3938 set_palette(vc);
3939}
3940
3941/*
3942 * Font switching
3943 *
3944 * Currently we only support fonts up to 32 pixels wide, at a maximum height
3945 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
3946 * depending on width) reserved for each character which is kinda wasty, but
3947 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
3948 * is upto the actual low-level console-driver convert data into its favorite
3949 * format (maybe we should add a `fontoffset' field to the `display'
3950 * structure so we won't have to convert the fontdata all the time.
3951 * /Jes
3952 */
3953
3954#define max_font_size 65536
3955
3956static int con_font_get(struct vc_data *vc, struct console_font_op *op)
3957{
3958 struct console_font font;
3959 int rc = -EINVAL;
3960 int c;
3961
3962 if (vc->vc_mode != KD_TEXT)
3963 return -EINVAL;
3964
3965 if (op->data) {
3966 font.data = kmalloc(max_font_size, GFP_KERNEL);
3967 if (!font.data)
3968 return -ENOMEM;
3969 } else
3970 font.data = NULL;
3971
3972 acquire_console_sem();
3973 if (vc->vc_sw->con_font_get)
3974 rc = vc->vc_sw->con_font_get(vc, &font);
3975 else
3976 rc = -ENOSYS;
3977 release_console_sem();
3978
3979 if (rc)
3980 goto out;
3981
3982 c = (font.width+7)/8 * 32 * font.charcount;
Alan Cox04f378b2008-04-30 00:53:29 -07003983
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 if (op->data && font.charcount > op->charcount)
3985 rc = -ENOSPC;
3986 if (!(op->flags & KD_FONT_FLAG_OLD)) {
3987 if (font.width > op->width || font.height > op->height)
3988 rc = -ENOSPC;
3989 } else {
3990 if (font.width != 8)
3991 rc = -EIO;
3992 else if ((op->height && font.height > op->height) ||
3993 font.height > 32)
3994 rc = -ENOSPC;
3995 }
3996 if (rc)
3997 goto out;
3998
3999 op->height = font.height;
4000 op->width = font.width;
4001 op->charcount = font.charcount;
4002
4003 if (op->data && copy_to_user(op->data, font.data, c))
4004 rc = -EFAULT;
4005
4006out:
4007 kfree(font.data);
4008 return rc;
4009}
4010
4011static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4012{
4013 struct console_font font;
4014 int rc = -EINVAL;
4015 int size;
4016
4017 if (vc->vc_mode != KD_TEXT)
4018 return -EINVAL;
4019 if (!op->data)
4020 return -EINVAL;
4021 if (op->charcount > 512)
4022 return -EINVAL;
4023 if (!op->height) { /* Need to guess font height [compat] */
4024 int h, i;
4025 u8 __user *charmap = op->data;
4026 u8 tmp;
4027
4028 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4029 so that we can get rid of this soon */
4030 if (!(op->flags & KD_FONT_FLAG_OLD))
4031 return -EINVAL;
4032 for (h = 32; h > 0; h--)
4033 for (i = 0; i < op->charcount; i++) {
4034 if (get_user(tmp, &charmap[32*i+h-1]))
4035 return -EFAULT;
4036 if (tmp)
4037 goto nonzero;
4038 }
4039 return -EINVAL;
4040 nonzero:
4041 op->height = h;
4042 }
4043 if (op->width <= 0 || op->width > 32 || op->height > 32)
4044 return -EINVAL;
4045 size = (op->width+7)/8 * 32 * op->charcount;
4046 if (size > max_font_size)
4047 return -ENOSPC;
4048 font.charcount = op->charcount;
4049 font.height = op->height;
4050 font.width = op->width;
Julia Lawall9b71ca22010-05-26 14:42:11 -07004051 font.data = memdup_user(op->data, size);
4052 if (IS_ERR(font.data))
4053 return PTR_ERR(font.data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 acquire_console_sem();
4055 if (vc->vc_sw->con_font_set)
4056 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4057 else
4058 rc = -ENOSYS;
4059 release_console_sem();
4060 kfree(font.data);
4061 return rc;
4062}
4063
4064static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4065{
4066 struct console_font font = {.width = op->width, .height = op->height};
4067 char name[MAX_FONT_NAME];
4068 char *s = name;
4069 int rc;
4070
4071 if (vc->vc_mode != KD_TEXT)
4072 return -EINVAL;
4073
4074 if (!op->data)
4075 s = NULL;
4076 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4077 return -EFAULT;
4078 else
4079 name[MAX_FONT_NAME - 1] = 0;
4080
4081 acquire_console_sem();
4082 if (vc->vc_sw->con_font_default)
4083 rc = vc->vc_sw->con_font_default(vc, &font, s);
4084 else
4085 rc = -ENOSYS;
4086 release_console_sem();
4087 if (!rc) {
4088 op->width = font.width;
4089 op->height = font.height;
4090 }
4091 return rc;
4092}
4093
4094static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4095{
4096 int con = op->height;
4097 int rc;
4098
4099 if (vc->vc_mode != KD_TEXT)
4100 return -EINVAL;
4101
4102 acquire_console_sem();
4103 if (!vc->vc_sw->con_font_copy)
4104 rc = -ENOSYS;
4105 else if (con < 0 || !vc_cons_allocated(con))
4106 rc = -ENOTTY;
4107 else if (con == vc->vc_num) /* nothing to do */
4108 rc = 0;
4109 else
4110 rc = vc->vc_sw->con_font_copy(vc, con);
4111 release_console_sem();
4112 return rc;
4113}
4114
4115int con_font_op(struct vc_data *vc, struct console_font_op *op)
4116{
4117 switch (op->op) {
4118 case KD_FONT_OP_SET:
4119 return con_font_set(vc, op);
4120 case KD_FONT_OP_GET:
4121 return con_font_get(vc, op);
4122 case KD_FONT_OP_SET_DEFAULT:
4123 return con_font_default(vc, op);
4124 case KD_FONT_OP_COPY:
4125 return con_font_copy(vc, op);
4126 }
4127 return -ENOSYS;
4128}
4129
4130/*
4131 * Interface exported to selection and vcs.
4132 */
4133
4134/* used by selection */
4135u16 screen_glyph(struct vc_data *vc, int offset)
4136{
4137 u16 w = scr_readw(screenpos(vc, offset, 1));
4138 u16 c = w & 0xff;
4139
4140 if (w & vc->vc_hi_font_mask)
4141 c |= 0x100;
4142 return c;
4143}
Samuel Thibaultf7511d52008-04-30 00:54:51 -07004144EXPORT_SYMBOL_GPL(screen_glyph);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145
4146/* used by vcs - note the word offset */
4147unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4148{
4149 return screenpos(vc, 2 * w_offset, viewed);
4150}
4151
4152void getconsxy(struct vc_data *vc, unsigned char *p)
4153{
4154 p[0] = vc->vc_x;
4155 p[1] = vc->vc_y;
4156}
4157
4158void putconsxy(struct vc_data *vc, unsigned char *p)
4159{
Antonino A. Daplas9477e262006-02-01 03:06:52 -08004160 hide_cursor(vc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 gotoxy(vc, p[0], p[1]);
4162 set_cursor(vc);
4163}
4164
4165u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4166{
4167 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4168 return softcursor_original;
4169 return scr_readw(org);
4170}
4171
4172void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4173{
4174 scr_writew(val, org);
4175 if ((unsigned long)org == vc->vc_pos) {
4176 softcursor_original = -1;
4177 add_softcursor(vc);
4178 }
4179}
4180
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181/*
4182 * Visible symbols for modules
4183 */
4184
4185EXPORT_SYMBOL(color_table);
4186EXPORT_SYMBOL(default_red);
4187EXPORT_SYMBOL(default_grn);
4188EXPORT_SYMBOL(default_blu);
4189EXPORT_SYMBOL(update_region);
4190EXPORT_SYMBOL(redraw_screen);
4191EXPORT_SYMBOL(vc_resize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192EXPORT_SYMBOL(fg_console);
4193EXPORT_SYMBOL(console_blank_hook);
4194EXPORT_SYMBOL(console_blanked);
4195EXPORT_SYMBOL(vc_cons);
Matthew Garrettf6c06b62009-11-13 15:14:11 -05004196EXPORT_SYMBOL(global_cursor_default);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197#ifndef VT_SINGLE_DRIVER
4198EXPORT_SYMBOL(take_over_console);
4199EXPORT_SYMBOL(give_up_console);
4200#endif