blob: 781d95113742fcc359af6c94cb5e4c6eb00be00a [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 1991, 1992 Linus Torvalds
3 */
4
5/*
6 * Hopefully this will be a rather complete VT102 implementation.
7 *
8 * Beeping thanks to John T Kohl.
9 *
10 * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
11 * Chars, and VT100 enhancements by Peter MacDonald.
12 *
13 * Copy and paste function by Andrew Haylett,
14 * some enhancements by Alessandro Rubini.
15 *
16 * Code to check for different video-cards mostly by Galen Hunt,
17 * <g-hunt@ee.utah.edu>
18 *
19 * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
20 * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
21 *
22 * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
23 * Resizing of consoles, aeb, 940926
24 *
25 * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
26 * <poe@daimi.aau.dk>
27 *
28 * User-defined bell sound, new setterm control sequences and printk
29 * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
30 *
31 * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
32 *
33 * Merge with the abstract console driver by Geert Uytterhoeven
34 * <geert@linux-m68k.org>, Jan 1997.
35 *
36 * Original m68k console driver modifications by
37 *
38 * - Arno Griffioen <arno@usn.nl>
39 * - David Carter <carter@cs.bris.ac.uk>
40 *
41 * The abstract console driver provides a generic interface for a text
42 * console. It supports VGA text mode, frame buffer based graphical consoles
43 * and special graphics processors that are only accessible through some
44 * registers (e.g. a TMS340x0 GSP).
45 *
46 * The interface to the hardware is specified using a special structure
47 * (struct consw) which contains function pointers to console operations
48 * (see <linux/console.h> for more information).
49 *
50 * Support for changeable cursor shape
51 * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
52 *
53 * Ported to i386 and con_scrolldelta fixed
54 * by Emmanuel Marty <core@ggi-project.org>, April 1998
55 *
56 * Resurrected character buffers in videoram plus lots of other trickery
57 * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
58 *
59 * Removed old-style timers, introduced console_timer, made timer
60 * deletion SMP-safe. 17Jun00, Andrew Morton
61 *
62 * Removed console_lock, enabled interrupts across all console operations
63 * 13 March 2001, Andrew Morton
64 *
65 * Fixed UTF-8 mode so alternate charset modes always work according
66 * to control sequences interpreted in do_con_trol function
67 * preserving backward VT100 semigraphics compatibility,
68 * malformed UTF sequences represented as sequences of replacement glyphs,
69 * original codes or '?' as a last resort if replacement glyph is undefined
70 * by Adam Tla/lka <atlka@pg.gda.pl>, Aug 2006
71 */
72
73#include <linux/module.h>
74#include <linux/types.h>
75#include <linux/sched/signal.h>
76#include <linux/tty.h>
77#include <linux/tty_flip.h>
78#include <linux/kernel.h>
79#include <linux/string.h>
80#include <linux/errno.h>
81#include <linux/kd.h>
82#include <linux/slab.h>
83#include <linux/major.h>
84#include <linux/mm.h>
85#include <linux/console.h>
86#include <linux/init.h>
87#include <linux/mutex.h>
88#include <linux/vt_kern.h>
89#include <linux/selection.h>
90#include <linux/tiocl.h>
91#include <linux/kbd_kern.h>
92#include <linux/consolemap.h>
93#include <linux/timer.h>
94#include <linux/interrupt.h>
95#include <linux/workqueue.h>
96#include <linux/pm.h>
97#include <linux/font.h>
98#include <linux/bitops.h>
99#include <linux/notifier.h>
100#include <linux/device.h>
101#include <linux/io.h>
102#include <linux/uaccess.h>
103#include <linux/kdb.h>
104#include <linux/ctype.h>
105
106#define MAX_NR_CON_DRIVER 16
107
108#define CON_DRIVER_FLAG_MODULE 1
109#define CON_DRIVER_FLAG_INIT 2
110#define CON_DRIVER_FLAG_ATTR 4
111#define CON_DRIVER_FLAG_ZOMBIE 8
112
113struct con_driver {
114 const struct consw *con;
115 const char *desc;
116 struct device *dev;
117 int node;
118 int first;
119 int last;
120 int flag;
121};
122
123static struct con_driver registered_con_driver[MAX_NR_CON_DRIVER];
124const struct consw *conswitchp;
125
126/* A bitmap for codes <32. A bit of 1 indicates that the code
127 * corresponding to that bit number invokes some special action
128 * (such as cursor movement) and should not be displayed as a
129 * glyph unless the disp_ctrl mode is explicitly enabled.
130 */
131#define CTRL_ACTION 0x0d00ff81
132#define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
133
134/*
135 * Here is the default bell parameters: 750HZ, 1/8th of a second
136 */
137#define DEFAULT_BELL_PITCH 750
138#define DEFAULT_BELL_DURATION (HZ/8)
139#define DEFAULT_CURSOR_BLINK_MS 200
140
141struct vc vc_cons [MAX_NR_CONSOLES];
142
143#ifndef VT_SINGLE_DRIVER
144static const struct consw *con_driver_map[MAX_NR_CONSOLES];
145#endif
146
147static int con_open(struct tty_struct *, struct file *);
148static void vc_init(struct vc_data *vc, unsigned int rows,
149 unsigned int cols, int do_clear);
150static void gotoxy(struct vc_data *vc, int new_x, int new_y);
151static void save_cur(struct vc_data *vc);
152static void reset_terminal(struct vc_data *vc, int do_clear);
153static void con_flush_chars(struct tty_struct *tty);
154static int set_vesa_blanking(char __user *p);
155static void set_cursor(struct vc_data *vc);
156static void hide_cursor(struct vc_data *vc);
157static void console_callback(struct work_struct *ignored);
158static void con_driver_unregister_callback(struct work_struct *ignored);
159static void blank_screen_t(unsigned long dummy);
160static void set_palette(struct vc_data *vc);
161
162#define vt_get_kmsg_redirect() vt_kmsg_redirect(-1)
163
164static int printable; /* Is console ready for printing? */
165int default_utf8 = true;
166module_param(default_utf8, int, S_IRUGO | S_IWUSR);
167int global_cursor_default = -1;
168module_param(global_cursor_default, int, S_IRUGO | S_IWUSR);
169
170static int cur_default = CUR_DEFAULT;
171module_param(cur_default, int, S_IRUGO | S_IWUSR);
172
173/*
174 * ignore_poke: don't unblank the screen when things are typed. This is
175 * mainly for the privacy of braille terminal users.
176 */
177static int ignore_poke;
178
179int do_poke_blanked_console;
180int console_blanked;
181
182static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
183static int vesa_off_interval;
184static int blankinterval;
185core_param(consoleblank, blankinterval, int, 0444);
186
187static DECLARE_WORK(console_work, console_callback);
188static DECLARE_WORK(con_driver_unregister_work, con_driver_unregister_callback);
189
190/*
191 * fg_console is the current virtual console,
192 * last_console is the last used one,
193 * want_console is the console we want to switch to,
194 * saved_* variants are for save/restore around kernel debugger enter/leave
195 */
196int fg_console;
197int last_console;
198int want_console = -1;
199static int saved_fg_console;
200static int saved_last_console;
201static int saved_want_console;
202static int saved_vc_mode;
203static int saved_console_blanked;
204
205/*
206 * For each existing display, we have a pointer to console currently visible
207 * on that display, allowing consoles other than fg_console to be refreshed
208 * appropriately. Unless the low-level driver supplies its own display_fg
209 * variable, we use this one for the "master display".
210 */
211static struct vc_data *master_display_fg;
212
213/*
214 * Unfortunately, we need to delay tty echo when we're currently writing to the
215 * console since the code is (and always was) not re-entrant, so we schedule
216 * all flip requests to process context with schedule-task() and run it from
217 * console_callback().
218 */
219
220/*
221 * For the same reason, we defer scrollback to the console callback.
222 */
223static int scrollback_delta;
224
225/*
226 * Hook so that the power management routines can (un)blank
227 * the console on our behalf.
228 */
229int (*console_blank_hook)(int);
230
231static DEFINE_TIMER(console_timer, blank_screen_t, 0, 0);
232static int blank_state;
233static int blank_timer_expired;
234enum {
235 blank_off = 0,
236 blank_normal_wait,
237 blank_vesa_wait,
238};
239
240/*
241 * /sys/class/tty/tty0/
242 *
243 * the attribute 'active' contains the name of the current vc
244 * console and it supports poll() to detect vc switches
245 */
246static struct device *tty0dev;
247
248/*
249 * Notifier list for console events.
250 */
251static ATOMIC_NOTIFIER_HEAD(vt_notifier_list);
252
253int register_vt_notifier(struct notifier_block *nb)
254{
255 return atomic_notifier_chain_register(&vt_notifier_list, nb);
256}
257EXPORT_SYMBOL_GPL(register_vt_notifier);
258
259int unregister_vt_notifier(struct notifier_block *nb)
260{
261 return atomic_notifier_chain_unregister(&vt_notifier_list, nb);
262}
263EXPORT_SYMBOL_GPL(unregister_vt_notifier);
264
265static void notify_write(struct vc_data *vc, unsigned int unicode)
266{
267 struct vt_notifier_param param = { .vc = vc, .c = unicode };
268 atomic_notifier_call_chain(&vt_notifier_list, VT_WRITE, &param);
269}
270
271static void notify_update(struct vc_data *vc)
272{
273 struct vt_notifier_param param = { .vc = vc };
274 atomic_notifier_call_chain(&vt_notifier_list, VT_UPDATE, &param);
275}
276/*
277 * Low-Level Functions
278 */
279
280static inline bool con_is_fg(const struct vc_data *vc)
281{
282 return vc->vc_num == fg_console;
283}
284
285static inline bool con_should_update(const struct vc_data *vc)
286{
287 return con_is_visible(vc) && !console_blanked;
288}
289
290static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
291{
292 unsigned short *p;
293
294 if (!viewed)
295 p = (unsigned short *)(vc->vc_origin + offset);
296 else if (!vc->vc_sw->con_screen_pos)
297 p = (unsigned short *)(vc->vc_visible_origin + offset);
298 else
299 p = vc->vc_sw->con_screen_pos(vc, offset);
300 return p;
301}
302
303/* Called from the keyboard irq path.. */
304static inline void scrolldelta(int lines)
305{
306 /* FIXME */
307 /* scrolldelta needs some kind of consistency lock, but the BKL was
308 and still is not protecting versus the scheduled back end */
309 scrollback_delta += lines;
310 schedule_console_callback();
311}
312
313void schedule_console_callback(void)
314{
315 schedule_work(&console_work);
316}
317
318static void con_scroll(struct vc_data *vc, unsigned int t, unsigned int b,
319 enum con_scroll dir, unsigned int nr)
320{
321 u16 *clear, *d, *s;
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, dir, nr))
328 return;
329
330 s = clear = (u16 *)(vc->vc_origin + vc->vc_size_row * t);
331 d = (u16 *)(vc->vc_origin + vc->vc_size_row * (t + nr));
332
333 if (dir == SM_UP) {
334 clear = s + (b - t - nr) * vc->vc_cols;
335 swap(s, d);
336 }
337 scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
338 scr_memsetw(clear, vc->vc_video_erase_char, vc->vc_size_row * nr);
339}
340
341static void do_update_region(struct vc_data *vc, unsigned long start, int count)
342{
343 unsigned int xx, yy, offset;
344 u16 *p;
345
346 p = (u16 *) start;
347 if (!vc->vc_sw->con_getxy) {
348 offset = (start - vc->vc_origin) / 2;
349 xx = offset % vc->vc_cols;
350 yy = offset / vc->vc_cols;
351 } else {
352 int nxx, nyy;
353 start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
354 xx = nxx; yy = nyy;
355 }
356 for(;;) {
357 u16 attrib = scr_readw(p) & 0xff00;
358 int startx = xx;
359 u16 *q = p;
360 while (xx < vc->vc_cols && count) {
361 if (attrib != (scr_readw(p) & 0xff00)) {
362 if (p > q)
363 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
364 startx = xx;
365 q = p;
366 attrib = scr_readw(p) & 0xff00;
367 }
368 p++;
369 xx++;
370 count--;
371 }
372 if (p > q)
373 vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
374 if (!count)
375 break;
376 xx = 0;
377 yy++;
378 if (vc->vc_sw->con_getxy) {
379 p = (u16 *)start;
380 start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
381 }
382 }
383}
384
385void update_region(struct vc_data *vc, unsigned long start, int count)
386{
387 WARN_CONSOLE_UNLOCKED();
388
389 if (con_should_update(vc)) {
390 hide_cursor(vc);
391 do_update_region(vc, start, count);
392 set_cursor(vc);
393 }
394}
395
396/* Structure of attributes is hardware-dependent */
397
398static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink,
399 u8 _underline, u8 _reverse, u8 _italic)
400{
401 if (vc->vc_sw->con_build_attr)
402 return vc->vc_sw->con_build_attr(vc, _color, _intensity,
403 _blink, _underline, _reverse, _italic);
404
405/*
406 * ++roman: I completely changed the attribute format for monochrome
407 * mode (!can_do_color). The formerly used MDA (monochrome display
408 * adapter) format didn't allow the combination of certain effects.
409 * Now the attribute is just a bit vector:
410 * Bit 0..1: intensity (0..2)
411 * Bit 2 : underline
412 * Bit 3 : reverse
413 * Bit 7 : blink
414 */
415 {
416 u8 a = _color;
417 if (!vc->vc_can_do_color)
418 return _intensity |
419 (_italic ? 2 : 0) |
420 (_underline ? 4 : 0) |
421 (_reverse ? 8 : 0) |
422 (_blink ? 0x80 : 0);
423 if (_italic)
424 a = (a & 0xF0) | vc->vc_itcolor;
425 else if (_underline)
426 a = (a & 0xf0) | vc->vc_ulcolor;
427 else if (_intensity == 0)
428 a = (a & 0xf0) | vc->vc_halfcolor;
429 if (_reverse)
430 a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
431 if (_blink)
432 a ^= 0x80;
433 if (_intensity == 2)
434 a ^= 0x08;
435 if (vc->vc_hi_font_mask == 0x100)
436 a <<= 1;
437 return a;
438 }
439}
440
441static void update_attr(struct vc_data *vc)
442{
443 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) | ' ';
447}
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 } else {
461 u16 *q = p;
462 int cnt = count;
463 u16 a;
464
465 if (!vc->vc_can_do_color) {
466 while (cnt--) {
467 a = scr_readw(q);
468 a ^= 0x0800;
469 scr_writew(a, q);
470 q++;
471 }
472 } else if (vc->vc_hi_font_mask == 0x100) {
473 while (cnt--) {
474 a = scr_readw(q);
475 a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
476 scr_writew(a, q);
477 q++;
478 }
479 } else {
480 while (cnt--) {
481 a = scr_readw(q);
482 a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
483 scr_writew(a, q);
484 q++;
485 }
486 }
487 }
488
489 if (con_should_update(vc))
490 do_update_region(vc, (unsigned long) p, count);
491 notify_update(vc);
492}
493
494/* used by selection: complement pointer position */
495void complement_pos(struct vc_data *vc, int offset)
496{
497 static int old_offset = -1;
498 static unsigned short old;
499 static unsigned short oldx, oldy;
500
501 WARN_CONSOLE_UNLOCKED();
502
503 if (old_offset != -1 && old_offset >= 0 &&
504 old_offset < vc->vc_screenbuf_size) {
505 scr_writew(old, screenpos(vc, old_offset, 1));
506 if (con_should_update(vc))
507 vc->vc_sw->con_putc(vc, old, oldy, oldx);
508 notify_update(vc);
509 }
510
511 old_offset = offset;
512
513 if (offset != -1 && offset >= 0 &&
514 offset < vc->vc_screenbuf_size) {
515 unsigned short new;
516 unsigned short *p;
517 p = screenpos(vc, offset, 1);
518 old = scr_readw(p);
519 new = old ^ vc->vc_complement_mask;
520 scr_writew(new, p);
521 if (con_should_update(vc)) {
522 oldx = (offset >> 1) % vc->vc_cols;
523 oldy = (offset >> 1) / vc->vc_cols;
524 vc->vc_sw->con_putc(vc, new, oldy, oldx);
525 }
526 notify_update(vc);
527 }
528}
529
530static void insert_char(struct vc_data *vc, unsigned int nr)
531{
532 unsigned short *p = (unsigned short *) vc->vc_pos;
533
534 scr_memmovew(p + nr, p, (vc->vc_cols - vc->vc_x - nr) * 2);
535 scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
536 vc->vc_need_wrap = 0;
537 if (con_should_update(vc))
538 do_update_region(vc, (unsigned long) p,
539 vc->vc_cols - vc->vc_x);
540}
541
542static void delete_char(struct vc_data *vc, unsigned int nr)
543{
544 unsigned short *p = (unsigned short *) vc->vc_pos;
545
546 scr_memcpyw(p, p + nr, (vc->vc_cols - vc->vc_x - nr) * 2);
547 scr_memsetw(p + vc->vc_cols - vc->vc_x - nr, vc->vc_video_erase_char,
548 nr * 2);
549 vc->vc_need_wrap = 0;
550 if (con_should_update(vc))
551 do_update_region(vc, (unsigned long) p,
552 vc->vc_cols - vc->vc_x);
553}
554
555static int softcursor_original = -1;
556
557static void add_softcursor(struct vc_data *vc)
558{
559 int i = scr_readw((u16 *) vc->vc_pos);
560 u32 type = vc->vc_cursor_type;
561
562 if (! (type & 0x10)) return;
563 if (softcursor_original != -1) return;
564 softcursor_original = i;
565 i |= ((type >> 8) & 0xff00 );
566 i ^= ((type) & 0xff00 );
567 if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
568 if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
569 scr_writew(i, (u16 *) vc->vc_pos);
570 if (con_should_update(vc))
571 vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
572}
573
574static void hide_softcursor(struct vc_data *vc)
575{
576 if (softcursor_original != -1) {
577 scr_writew(softcursor_original, (u16 *)vc->vc_pos);
578 if (con_should_update(vc))
579 vc->vc_sw->con_putc(vc, softcursor_original,
580 vc->vc_y, vc->vc_x);
581 softcursor_original = -1;
582 }
583}
584
585static void hide_cursor(struct vc_data *vc)
586{
587 if (vc_is_sel(vc))
588 clear_selection();
589
590 vc->vc_sw->con_cursor(vc, CM_ERASE);
591 hide_softcursor(vc);
592}
593
594static void set_cursor(struct vc_data *vc)
595{
596 if (!con_is_fg(vc) || console_blanked || vc->vc_mode == KD_GRAPHICS)
597 return;
598 if (vc->vc_deccm) {
599 if (vc_is_sel(vc))
600 clear_selection();
601 add_softcursor(vc);
602 if ((vc->vc_cursor_type & 0x0f) != 1)
603 vc->vc_sw->con_cursor(vc, CM_DRAW);
604 } else
605 hide_cursor(vc);
606}
607
608static void set_origin(struct vc_data *vc)
609{
610 WARN_CONSOLE_UNLOCKED();
611
612 if (!con_is_visible(vc) ||
613 !vc->vc_sw->con_set_origin ||
614 !vc->vc_sw->con_set_origin(vc))
615 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
616 vc->vc_visible_origin = vc->vc_origin;
617 vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
618 vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
619}
620
621static void save_screen(struct vc_data *vc)
622{
623 WARN_CONSOLE_UNLOCKED();
624
625 if (vc->vc_sw->con_save_screen)
626 vc->vc_sw->con_save_screen(vc);
627}
628
629static void flush_scrollback(struct vc_data *vc)
630{
631 WARN_CONSOLE_UNLOCKED();
632
633 if (vc->vc_sw->con_flush_scrollback)
634 vc->vc_sw->con_flush_scrollback(vc);
635}
636
637/*
638 * Redrawing of screen
639 */
640
641void clear_buffer_attributes(struct vc_data *vc)
642{
643 unsigned short *p = (unsigned short *)vc->vc_origin;
644 int count = vc->vc_screenbuf_size / 2;
645 int mask = vc->vc_hi_font_mask | 0xff;
646
647 for (; count > 0; count--, p++) {
648 scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
649 }
650}
651
652void redraw_screen(struct vc_data *vc, int is_switch)
653{
654 int redraw = 0;
655
656 WARN_CONSOLE_UNLOCKED();
657
658 if (!vc) {
659 /* strange ... */
660 /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
661 return;
662 }
663
664 if (is_switch) {
665 struct vc_data *old_vc = vc_cons[fg_console].d;
666 if (old_vc == vc)
667 return;
668 if (!con_is_visible(vc))
669 redraw = 1;
670 *vc->vc_display_fg = vc;
671 fg_console = vc->vc_num;
672 hide_cursor(old_vc);
673 if (!con_is_visible(old_vc)) {
674 save_screen(old_vc);
675 set_origin(old_vc);
676 }
677 if (tty0dev)
678 sysfs_notify(&tty0dev->kobj, NULL, "active");
679 } else {
680 hide_cursor(vc);
681 redraw = 1;
682 }
683
684 if (redraw) {
685 int update;
686 int old_was_color = vc->vc_can_do_color;
687
688 set_origin(vc);
689 update = vc->vc_sw->con_switch(vc);
690 set_palette(vc);
691 /*
692 * If console changed from mono<->color, the best we can do
693 * is to clear the buffer attributes. As it currently stands,
694 * rebuilding new attributes from the old buffer is not doable
695 * without overly complex code.
696 */
697 if (old_was_color != vc->vc_can_do_color) {
698 update_attr(vc);
699 clear_buffer_attributes(vc);
700 }
701
702 /* Forcibly update if we're panicing */
703 if ((update && vc->vc_mode != KD_GRAPHICS) ||
704 vt_force_oops_output(vc))
705 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
706 }
707 set_cursor(vc);
708 if (is_switch) {
709 set_leds();
710 compute_shiftstate();
711 notify_update(vc);
712 }
713}
714
715/*
716 * Allocation, freeing and resizing of VTs.
717 */
718
719int vc_cons_allocated(unsigned int i)
720{
721 return (i < MAX_NR_CONSOLES && vc_cons[i].d);
722}
723
724static void visual_init(struct vc_data *vc, int num, int init)
725{
726 /* ++Geert: vc->vc_sw->con_init determines console size */
727 if (vc->vc_sw)
728 module_put(vc->vc_sw->owner);
729 vc->vc_sw = conswitchp;
730#ifndef VT_SINGLE_DRIVER
731 if (con_driver_map[num])
732 vc->vc_sw = con_driver_map[num];
733#endif
734 __module_get(vc->vc_sw->owner);
735 vc->vc_num = num;
736 vc->vc_display_fg = &master_display_fg;
737 if (vc->vc_uni_pagedir_loc)
738 con_free_unimap(vc);
739 vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
740 vc->vc_uni_pagedir = NULL;
741 vc->vc_hi_font_mask = 0;
742 vc->vc_complement_mask = 0;
743 vc->vc_can_do_color = 0;
744 vc->vc_panic_force_write = false;
745 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
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
754static void vc_port_destruct(struct tty_port *port)
755{
756 struct vc_data *vc = container_of(port, struct vc_data, port);
757
758 kfree(vc);
759}
760
761static const struct tty_port_operations vc_port_ops = {
762 .destruct = vc_port_destruct,
763};
764
765/*
766 * Change # of rows and columns (0 means unchanged/the size of fg_console)
767 * [this is to be used together with some user program
768 * like resize that changes the hardware videomode]
769 */
770#define VC_MAXCOL (32767)
771#define VC_MAXROW (32767)
772
773int vc_allocate(unsigned int currcons) /* return 0 on success */
774{
775 struct vt_notifier_param param;
776 struct vc_data *vc;
777 int err;
778
779 WARN_CONSOLE_UNLOCKED();
780
781 if (currcons >= MAX_NR_CONSOLES)
782 return -ENXIO;
783
784 if (vc_cons[currcons].d)
785 return 0;
786
787 /* due to the granularity of kmalloc, we waste some memory here */
788 /* the alloc is done in two steps, to optimize the common situation
789 of a 25x80 console (structsize=216, screenbuf_size=4000) */
790 /* although the numbers above are not valid since long ago, the
791 point is still up-to-date and the comment still has its value
792 even if only as a historical artifact. --mj, July 1998 */
793 param.vc = vc = kzalloc(sizeof(struct vc_data), GFP_KERNEL);
794 if (!vc)
795 return -ENOMEM;
796
797 vc_cons[currcons].d = vc;
798 tty_port_init(&vc->port);
799 vc->port.ops = &vc_port_ops;
800 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
801
802 visual_init(vc, currcons, 1);
803
804 if (!*vc->vc_uni_pagedir_loc)
805 con_set_default_unimap(vc);
806
807 err = -EINVAL;
808 if (vc->vc_cols > VC_MAXCOL || vc->vc_rows > VC_MAXROW ||
809 vc->vc_screenbuf_size > KMALLOC_MAX_SIZE || !vc->vc_screenbuf_size)
810 goto err_free;
811 err = -ENOMEM;
812 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_KERNEL);
813 if (!vc->vc_screenbuf)
814 goto err_free;
815
816 /* If no drivers have overridden us and the user didn't pass a
817 boot option, default to displaying the cursor */
818 if (global_cursor_default == -1)
819 global_cursor_default = 1;
820
821 vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
822 vcs_make_sysfs(currcons);
823 atomic_notifier_call_chain(&vt_notifier_list, VT_ALLOCATE, &param);
824
825 return 0;
826err_free:
827 kfree(vc);
828 vc_cons[currcons].d = NULL;
829 return err;
830}
831
832static inline int resize_screen(struct vc_data *vc, int width, int height,
833 int user)
834{
835 /* Resizes the resolution of the display adapater */
836 int err = 0;
837
838 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
839 err = vc->vc_sw->con_resize(vc, width, height, user);
840
841 return err;
842}
843
844/**
845 * vc_do_resize - resizing method for the tty
846 * @tty: tty being resized
847 * @real_tty: real tty (different to tty if a pty/tty pair)
848 * @vc: virtual console private data
849 * @cols: columns
850 * @lines: lines
851 *
852 * Resize a virtual console, clipping according to the actual constraints.
853 * If the caller passes a tty structure then update the termios winsize
854 * information and perform any necessary signal handling.
855 *
856 * Caller must hold the console semaphore. Takes the termios rwsem and
857 * ctrl_lock of the tty IFF a tty is passed.
858 */
859
860static int vc_do_resize(struct tty_struct *tty, struct vc_data *vc,
861 unsigned int cols, unsigned int lines)
862{
863 unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
864 unsigned long end;
865 unsigned int old_rows, old_row_size;
866 unsigned int new_cols, new_rows, new_row_size, new_screen_size;
867 unsigned int user;
868 unsigned short *oldscreen, *newscreen;
869
870 WARN_CONSOLE_UNLOCKED();
871
872 if (!vc)
873 return -ENXIO;
874
875 user = vc->vc_resize_user;
876 vc->vc_resize_user = 0;
877
878 if (cols > VC_MAXCOL || lines > VC_MAXROW)
879 return -EINVAL;
880
881 new_cols = (cols ? cols : vc->vc_cols);
882 new_rows = (lines ? lines : vc->vc_rows);
883 new_row_size = new_cols << 1;
884 new_screen_size = new_row_size * new_rows;
885
886 if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
887 return 0;
888
889 if (new_screen_size > KMALLOC_MAX_SIZE || !new_screen_size)
890 return -EINVAL;
891 newscreen = kzalloc(new_screen_size, GFP_USER);
892 if (!newscreen)
893 return -ENOMEM;
894
895 if (vc_is_sel(vc))
896 clear_selection();
897
898 old_rows = vc->vc_rows;
899 old_row_size = vc->vc_size_row;
900
901 err = resize_screen(vc, new_cols, new_rows, user);
902 if (err) {
903 kfree(newscreen);
904 return err;
905 }
906
907 vc->vc_rows = new_rows;
908 vc->vc_cols = new_cols;
909 vc->vc_size_row = new_row_size;
910 vc->vc_screenbuf_size = new_screen_size;
911
912 rlth = min(old_row_size, new_row_size);
913 rrem = new_row_size - rlth;
914 old_origin = vc->vc_origin;
915 new_origin = (long) newscreen;
916 new_scr_end = new_origin + new_screen_size;
917
918 if (vc->vc_y > new_rows) {
919 if (old_rows - vc->vc_y < new_rows) {
920 /*
921 * Cursor near the bottom, copy contents from the
922 * bottom of buffer
923 */
924 old_origin += (old_rows - new_rows) * old_row_size;
925 } else {
926 /*
927 * Cursor is in no man's land, copy 1/2 screenful
928 * from the top and bottom of cursor position
929 */
930 old_origin += (vc->vc_y - new_rows/2) * old_row_size;
931 }
932 }
933
934 end = old_origin + old_row_size * min(old_rows, new_rows);
935
936 update_attr(vc);
937
938 while (old_origin < end) {
939 scr_memcpyw((unsigned short *) new_origin,
940 (unsigned short *) old_origin, rlth);
941 if (rrem)
942 scr_memsetw((void *)(new_origin + rlth),
943 vc->vc_video_erase_char, rrem);
944 old_origin += old_row_size;
945 new_origin += new_row_size;
946 }
947 if (new_scr_end > new_origin)
948 scr_memsetw((void *)new_origin, vc->vc_video_erase_char,
949 new_scr_end - new_origin);
950 oldscreen = vc->vc_screenbuf;
951 vc->vc_screenbuf = newscreen;
952 vc->vc_screenbuf_size = new_screen_size;
953 set_origin(vc);
954 kfree(oldscreen);
955
956 /* do part of a reset_terminal() */
957 vc->vc_top = 0;
958 vc->vc_bottom = vc->vc_rows;
959 gotoxy(vc, vc->vc_x, vc->vc_y);
960 save_cur(vc);
961
962 if (tty) {
963 /* Rewrite the requested winsize data with the actual
964 resulting sizes */
965 struct winsize ws;
966 memset(&ws, 0, sizeof(ws));
967 ws.ws_row = vc->vc_rows;
968 ws.ws_col = vc->vc_cols;
969 ws.ws_ypixel = vc->vc_scan_lines;
970 tty_do_resize(tty, &ws);
971 }
972
973 if (con_is_visible(vc))
974 update_screen(vc);
975 vt_event_post(VT_EVENT_RESIZE, vc->vc_num, vc->vc_num);
976 notify_update(vc);
977 return err;
978}
979
980/**
981 * vc_resize - resize a VT
982 * @vc: virtual console
983 * @cols: columns
984 * @rows: rows
985 *
986 * Resize a virtual console as seen from the console end of things. We
987 * use the common vc_do_resize methods to update the structures. The
988 * caller must hold the console sem to protect console internals and
989 * vc->port.tty
990 */
991
992int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int rows)
993{
994 return vc_do_resize(vc->port.tty, vc, cols, rows);
995}
996
997/**
998 * vt_resize - resize a VT
999 * @tty: tty to resize
1000 * @ws: winsize attributes
1001 *
1002 * Resize a virtual terminal. This is called by the tty layer as we
1003 * register our own handler for resizing. The mutual helper does all
1004 * the actual work.
1005 *
1006 * Takes the console sem and the called methods then take the tty
1007 * termios_rwsem and the tty ctrl_lock in that order.
1008 */
1009static int vt_resize(struct tty_struct *tty, struct winsize *ws)
1010{
1011 struct vc_data *vc = tty->driver_data;
1012 int ret;
1013
1014 console_lock();
1015 ret = vc_do_resize(tty, vc, ws->ws_col, ws->ws_row);
1016 console_unlock();
1017 return ret;
1018}
1019
1020struct vc_data *vc_deallocate(unsigned int currcons)
1021{
1022 struct vc_data *vc = NULL;
1023
1024 WARN_CONSOLE_UNLOCKED();
1025
1026 if (vc_cons_allocated(currcons)) {
1027 struct vt_notifier_param param;
1028
1029 param.vc = vc = vc_cons[currcons].d;
1030 atomic_notifier_call_chain(&vt_notifier_list, VT_DEALLOCATE, &param);
1031 vcs_remove_sysfs(currcons);
1032 vc->vc_sw->con_deinit(vc);
1033 put_pid(vc->vt_pid);
1034 module_put(vc->vc_sw->owner);
1035 kfree(vc->vc_screenbuf);
1036 vc_cons[currcons].d = NULL;
1037 }
1038 return vc;
1039}
1040
1041/*
1042 * VT102 emulator
1043 */
1044
1045#define set_kbd(vc, x) vt_set_kbd_mode_bit((vc)->vc_num, (x))
1046#define clr_kbd(vc, x) vt_clr_kbd_mode_bit((vc)->vc_num, (x))
1047#define is_kbd(vc, x) vt_get_kbd_mode_bit((vc)->vc_num, (x))
1048
1049#define decarm VC_REPEAT
1050#define decckm VC_CKMODE
1051#define kbdapplic VC_APPLIC
1052#define lnm VC_CRLF
1053
1054/*
1055 * this is what the terminal answers to a ESC-Z or csi0c query.
1056 */
1057#define VT100ID "\033[?1;2c"
1058#define VT102ID "\033[?6c"
1059
1060const unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
1061 8,12,10,14, 9,13,11,15 };
1062
1063/* the default colour table, for VGA+ colour systems */
1064unsigned char default_red[] = {
1065 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa, 0x00, 0xaa,
1066 0x55, 0xff, 0x55, 0xff, 0x55, 0xff, 0x55, 0xff
1067};
1068module_param_array(default_red, byte, NULL, S_IRUGO | S_IWUSR);
1069
1070unsigned char default_grn[] = {
1071 0x00, 0x00, 0xaa, 0x55, 0x00, 0x00, 0xaa, 0xaa,
1072 0x55, 0x55, 0xff, 0xff, 0x55, 0x55, 0xff, 0xff
1073};
1074module_param_array(default_grn, byte, NULL, S_IRUGO | S_IWUSR);
1075
1076unsigned char default_blu[] = {
1077 0x00, 0x00, 0x00, 0x00, 0xaa, 0xaa, 0xaa, 0xaa,
1078 0x55, 0x55, 0x55, 0x55, 0xff, 0xff, 0xff, 0xff
1079};
1080module_param_array(default_blu, byte, NULL, S_IRUGO | S_IWUSR);
1081
1082/*
1083 * gotoxy() must verify all boundaries, because the arguments
1084 * might also be negative. If the given position is out of
1085 * bounds, the cursor is placed at the nearest margin.
1086 */
1087static void gotoxy(struct vc_data *vc, int new_x, int new_y)
1088{
1089 int min_y, max_y;
1090
1091 if (new_x < 0)
1092 vc->vc_x = 0;
1093 else {
1094 if (new_x >= vc->vc_cols)
1095 vc->vc_x = vc->vc_cols - 1;
1096 else
1097 vc->vc_x = new_x;
1098 }
1099
1100 if (vc->vc_decom) {
1101 min_y = vc->vc_top;
1102 max_y = vc->vc_bottom;
1103 } else {
1104 min_y = 0;
1105 max_y = vc->vc_rows;
1106 }
1107 if (new_y < min_y)
1108 vc->vc_y = min_y;
1109 else if (new_y >= max_y)
1110 vc->vc_y = max_y - 1;
1111 else
1112 vc->vc_y = new_y;
1113 vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
1114 vc->vc_need_wrap = 0;
1115}
1116
1117/* for absolute user moves, when decom is set */
1118static void gotoxay(struct vc_data *vc, int new_x, int new_y)
1119{
1120 gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
1121}
1122
1123void scrollback(struct vc_data *vc)
1124{
1125 scrolldelta(-(vc->vc_rows / 2));
1126}
1127
1128void scrollfront(struct vc_data *vc, int lines)
1129{
1130 if (!lines)
1131 lines = vc->vc_rows / 2;
1132 scrolldelta(lines);
1133}
1134
1135static void lf(struct vc_data *vc)
1136{
1137 /* don't scroll if above bottom of scrolling region, or
1138 * if below scrolling region
1139 */
1140 if (vc->vc_y + 1 == vc->vc_bottom)
1141 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_UP, 1);
1142 else if (vc->vc_y < vc->vc_rows - 1) {
1143 vc->vc_y++;
1144 vc->vc_pos += vc->vc_size_row;
1145 }
1146 vc->vc_need_wrap = 0;
1147 notify_write(vc, '\n');
1148}
1149
1150static void ri(struct vc_data *vc)
1151{
1152 /* don't scroll if below top of scrolling region, or
1153 * if above scrolling region
1154 */
1155 if (vc->vc_y == vc->vc_top)
1156 con_scroll(vc, vc->vc_top, vc->vc_bottom, SM_DOWN, 1);
1157 else if (vc->vc_y > 0) {
1158 vc->vc_y--;
1159 vc->vc_pos -= vc->vc_size_row;
1160 }
1161 vc->vc_need_wrap = 0;
1162}
1163
1164static inline void cr(struct vc_data *vc)
1165{
1166 vc->vc_pos -= vc->vc_x << 1;
1167 vc->vc_need_wrap = vc->vc_x = 0;
1168 notify_write(vc, '\r');
1169}
1170
1171static inline void bs(struct vc_data *vc)
1172{
1173 if (vc->vc_x) {
1174 vc->vc_pos -= 2;
1175 vc->vc_x--;
1176 vc->vc_need_wrap = 0;
1177 notify_write(vc, '\b');
1178 }
1179}
1180
1181static inline void del(struct vc_data *vc)
1182{
1183 /* ignored */
1184}
1185
1186static void csi_J(struct vc_data *vc, int vpar)
1187{
1188 unsigned int count;
1189 unsigned short * start;
1190
1191 switch (vpar) {
1192 case 0: /* erase from cursor to end of display */
1193 count = (vc->vc_scr_end - vc->vc_pos) >> 1;
1194 start = (unsigned short *)vc->vc_pos;
1195 break;
1196 case 1: /* erase from start to cursor */
1197 count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
1198 start = (unsigned short *)vc->vc_origin;
1199 break;
1200 case 3: /* erase scroll-back buffer (and whole display) */
1201 scr_memsetw(vc->vc_screenbuf, vc->vc_video_erase_char,
1202 vc->vc_screenbuf_size);
1203 flush_scrollback(vc);
1204 set_origin(vc);
1205 if (con_is_visible(vc))
1206 update_screen(vc);
1207 /* fall through */
1208 case 2: /* erase whole display */
1209 count = vc->vc_cols * vc->vc_rows;
1210 start = (unsigned short *)vc->vc_origin;
1211 break;
1212 default:
1213 return;
1214 }
1215 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1216 if (con_should_update(vc))
1217 do_update_region(vc, (unsigned long) start, count);
1218 vc->vc_need_wrap = 0;
1219}
1220
1221static void csi_K(struct vc_data *vc, int vpar)
1222{
1223 unsigned int count;
1224 unsigned short * start;
1225
1226 switch (vpar) {
1227 case 0: /* erase from cursor to end of line */
1228 count = vc->vc_cols - vc->vc_x;
1229 start = (unsigned short *)vc->vc_pos;
1230 break;
1231 case 1: /* erase from start of line to cursor */
1232 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1233 count = vc->vc_x + 1;
1234 break;
1235 case 2: /* erase whole line */
1236 start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
1237 count = vc->vc_cols;
1238 break;
1239 default:
1240 return;
1241 }
1242 scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
1243 vc->vc_need_wrap = 0;
1244 if (con_should_update(vc))
1245 do_update_region(vc, (unsigned long) start, count);
1246}
1247
1248static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
1249{ /* not vt100? */
1250 int count;
1251
1252 if (!vpar)
1253 vpar++;
1254 count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
1255
1256 scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
1257 if (con_should_update(vc))
1258 vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
1259 vc->vc_need_wrap = 0;
1260}
1261
1262static void default_attr(struct vc_data *vc)
1263{
1264 vc->vc_intensity = 1;
1265 vc->vc_italic = 0;
1266 vc->vc_underline = 0;
1267 vc->vc_reverse = 0;
1268 vc->vc_blink = 0;
1269 vc->vc_color = vc->vc_def_color;
1270}
1271
1272struct rgb { u8 r; u8 g; u8 b; };
1273
1274static void rgb_from_256(int i, struct rgb *c)
1275{
1276 if (i < 8) { /* Standard colours. */
1277 c->r = i&1 ? 0xaa : 0x00;
1278 c->g = i&2 ? 0xaa : 0x00;
1279 c->b = i&4 ? 0xaa : 0x00;
1280 } else if (i < 16) {
1281 c->r = i&1 ? 0xff : 0x55;
1282 c->g = i&2 ? 0xff : 0x55;
1283 c->b = i&4 ? 0xff : 0x55;
1284 } else if (i < 232) { /* 6x6x6 colour cube. */
1285 c->r = (i - 16) / 36 * 85 / 2;
1286 c->g = (i - 16) / 6 % 6 * 85 / 2;
1287 c->b = (i - 16) % 6 * 85 / 2;
1288 } else /* Grayscale ramp. */
1289 c->r = c->g = c->b = i * 10 - 2312;
1290}
1291
1292static void rgb_foreground(struct vc_data *vc, const struct rgb *c)
1293{
1294 u8 hue = 0, max = max3(c->r, c->g, c->b);
1295
1296 if (c->r > max / 2)
1297 hue |= 4;
1298 if (c->g > max / 2)
1299 hue |= 2;
1300 if (c->b > max / 2)
1301 hue |= 1;
1302
1303 if (hue == 7 && max <= 0x55) {
1304 hue = 0;
1305 vc->vc_intensity = 2;
1306 } else if (max > 0xaa)
1307 vc->vc_intensity = 2;
1308 else
1309 vc->vc_intensity = 1;
1310
1311 vc->vc_color = (vc->vc_color & 0xf0) | hue;
1312}
1313
1314static void rgb_background(struct vc_data *vc, const struct rgb *c)
1315{
1316 /* For backgrounds, err on the dark side. */
1317 vc->vc_color = (vc->vc_color & 0x0f)
1318 | (c->r&0x80) >> 1 | (c->g&0x80) >> 2 | (c->b&0x80) >> 3;
1319}
1320
1321/*
1322 * ITU T.416 Higher colour modes. They break the usual properties of SGR codes
1323 * and thus need to be detected and ignored by hand. Strictly speaking, that
1324 * standard also wants : rather than ; as separators, contrary to ECMA-48, but
1325 * no one produces such codes and almost no one accepts them.
1326 *
1327 * Subcommands 3 (CMY) and 4 (CMYK) are so insane there's no point in
1328 * supporting them.
1329 */
1330static int vc_t416_color(struct vc_data *vc, int i,
1331 void(*set_color)(struct vc_data *vc, const struct rgb *c))
1332{
1333 struct rgb c;
1334
1335 i++;
1336 if (i > vc->vc_npar)
1337 return i;
1338
1339 if (vc->vc_par[i] == 5 && i + 1 <= vc->vc_npar) {
1340 /* 256 colours */
1341 i++;
1342 rgb_from_256(vc->vc_par[i], &c);
1343 } else if (vc->vc_par[i] == 2 && i + 3 <= vc->vc_npar) {
1344 /* 24 bit */
1345 c.r = vc->vc_par[i + 1];
1346 c.g = vc->vc_par[i + 2];
1347 c.b = vc->vc_par[i + 3];
1348 i += 3;
1349 } else
1350 return i;
1351
1352 set_color(vc, &c);
1353
1354 return i;
1355}
1356
1357/* console_lock is held */
1358static void csi_m(struct vc_data *vc)
1359{
1360 int i;
1361
1362 for (i = 0; i <= vc->vc_npar; i++)
1363 switch (vc->vc_par[i]) {
1364 case 0: /* all attributes off */
1365 default_attr(vc);
1366 break;
1367 case 1:
1368 vc->vc_intensity = 2;
1369 break;
1370 case 2:
1371 vc->vc_intensity = 0;
1372 break;
1373 case 3:
1374 vc->vc_italic = 1;
1375 break;
1376 case 21:
1377 /*
1378 * No console drivers support double underline, so
1379 * convert it to a single underline.
1380 */
1381 case 4:
1382 vc->vc_underline = 1;
1383 break;
1384 case 5:
1385 vc->vc_blink = 1;
1386 break;
1387 case 7:
1388 vc->vc_reverse = 1;
1389 break;
1390 case 10: /* ANSI X3.64-1979 (SCO-ish?)
1391 * Select primary font, don't display control chars if
1392 * defined, don't set bit 8 on output.
1393 */
1394 vc->vc_translate = set_translate(vc->vc_charset == 0
1395 ? vc->vc_G0_charset
1396 : vc->vc_G1_charset, vc);
1397 vc->vc_disp_ctrl = 0;
1398 vc->vc_toggle_meta = 0;
1399 break;
1400 case 11: /* ANSI X3.64-1979 (SCO-ish?)
1401 * Select first alternate font, lets chars < 32 be
1402 * displayed as ROM chars.
1403 */
1404 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1405 vc->vc_disp_ctrl = 1;
1406 vc->vc_toggle_meta = 0;
1407 break;
1408 case 12: /* ANSI X3.64-1979 (SCO-ish?)
1409 * Select second alternate font, toggle high bit
1410 * before displaying as ROM char.
1411 */
1412 vc->vc_translate = set_translate(IBMPC_MAP, vc);
1413 vc->vc_disp_ctrl = 1;
1414 vc->vc_toggle_meta = 1;
1415 break;
1416 case 22:
1417 vc->vc_intensity = 1;
1418 break;
1419 case 23:
1420 vc->vc_italic = 0;
1421 break;
1422 case 24:
1423 vc->vc_underline = 0;
1424 break;
1425 case 25:
1426 vc->vc_blink = 0;
1427 break;
1428 case 27:
1429 vc->vc_reverse = 0;
1430 break;
1431 case 38:
1432 i = vc_t416_color(vc, i, rgb_foreground);
1433 break;
1434 case 48:
1435 i = vc_t416_color(vc, i, rgb_background);
1436 break;
1437 case 39:
1438 vc->vc_color = (vc->vc_def_color & 0x0f) |
1439 (vc->vc_color & 0xf0);
1440 break;
1441 case 49:
1442 vc->vc_color = (vc->vc_def_color & 0xf0) |
1443 (vc->vc_color & 0x0f);
1444 break;
1445 default:
1446 if (vc->vc_par[i] >= 90 && vc->vc_par[i] <= 107) {
1447 if (vc->vc_par[i] < 100)
1448 vc->vc_intensity = 2;
1449 vc->vc_par[i] -= 60;
1450 }
1451 if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
1452 vc->vc_color = color_table[vc->vc_par[i] - 30]
1453 | (vc->vc_color & 0xf0);
1454 else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
1455 vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
1456 | (vc->vc_color & 0x0f);
1457 break;
1458 }
1459 update_attr(vc);
1460}
1461
1462static void respond_string(const char *p, struct tty_port *port)
1463{
1464 while (*p) {
1465 tty_insert_flip_char(port, *p, 0);
1466 p++;
1467 }
1468 tty_schedule_flip(port);
1469}
1470
1471static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
1472{
1473 char buf[40];
1474
1475 sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
1476 respond_string(buf, tty->port);
1477}
1478
1479static inline void status_report(struct tty_struct *tty)
1480{
1481 respond_string("\033[0n", tty->port); /* Terminal ok */
1482}
1483
1484static inline void respond_ID(struct tty_struct *tty)
1485{
1486 respond_string(VT102ID, tty->port);
1487}
1488
1489void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
1490{
1491 char buf[8];
1492
1493 sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
1494 (char)('!' + mry));
1495 respond_string(buf, tty->port);
1496}
1497
1498/* invoked via ioctl(TIOCLINUX) and through set_selection */
1499int mouse_reporting(void)
1500{
1501 return vc_cons[fg_console].d->vc_report_mouse;
1502}
1503
1504/* console_lock is held */
1505static void set_mode(struct vc_data *vc, int on_off)
1506{
1507 int i;
1508
1509 for (i = 0; i <= vc->vc_npar; i++)
1510 if (vc->vc_ques) {
1511 switch(vc->vc_par[i]) { /* DEC private modes set/reset */
1512 case 1: /* Cursor keys send ^[Ox/^[[x */
1513 if (on_off)
1514 set_kbd(vc, decckm);
1515 else
1516 clr_kbd(vc, decckm);
1517 break;
1518 case 3: /* 80/132 mode switch unimplemented */
1519#if 0
1520 vc_resize(deccolm ? 132 : 80, vc->vc_rows);
1521 /* this alone does not suffice; some user mode
1522 utility has to change the hardware regs */
1523#endif
1524 break;
1525 case 5: /* Inverted screen on/off */
1526 if (vc->vc_decscnm != on_off) {
1527 vc->vc_decscnm = on_off;
1528 invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
1529 update_attr(vc);
1530 }
1531 break;
1532 case 6: /* Origin relative/absolute */
1533 vc->vc_decom = on_off;
1534 gotoxay(vc, 0, 0);
1535 break;
1536 case 7: /* Autowrap on/off */
1537 vc->vc_decawm = on_off;
1538 break;
1539 case 8: /* Autorepeat on/off */
1540 if (on_off)
1541 set_kbd(vc, decarm);
1542 else
1543 clr_kbd(vc, decarm);
1544 break;
1545 case 9:
1546 vc->vc_report_mouse = on_off ? 1 : 0;
1547 break;
1548 case 25: /* Cursor on/off */
1549 vc->vc_deccm = on_off;
1550 break;
1551 case 1000:
1552 vc->vc_report_mouse = on_off ? 2 : 0;
1553 break;
1554 }
1555 } else {
1556 switch(vc->vc_par[i]) { /* ANSI modes set/reset */
1557 case 3: /* Monitor (display ctrls) */
1558 vc->vc_disp_ctrl = on_off;
1559 break;
1560 case 4: /* Insert Mode on/off */
1561 vc->vc_decim = on_off;
1562 break;
1563 case 20: /* Lf, Enter == CrLf/Lf */
1564 if (on_off)
1565 set_kbd(vc, lnm);
1566 else
1567 clr_kbd(vc, lnm);
1568 break;
1569 }
1570 }
1571}
1572
1573/* console_lock is held */
1574static void setterm_command(struct vc_data *vc)
1575{
1576 switch(vc->vc_par[0]) {
1577 case 1: /* set color for underline mode */
1578 if (vc->vc_can_do_color &&
1579 vc->vc_par[1] < 16) {
1580 vc->vc_ulcolor = color_table[vc->vc_par[1]];
1581 if (vc->vc_underline)
1582 update_attr(vc);
1583 }
1584 break;
1585 case 2: /* set color for half intensity mode */
1586 if (vc->vc_can_do_color &&
1587 vc->vc_par[1] < 16) {
1588 vc->vc_halfcolor = color_table[vc->vc_par[1]];
1589 if (vc->vc_intensity == 0)
1590 update_attr(vc);
1591 }
1592 break;
1593 case 8: /* store colors as defaults */
1594 vc->vc_def_color = vc->vc_attr;
1595 if (vc->vc_hi_font_mask == 0x100)
1596 vc->vc_def_color >>= 1;
1597 default_attr(vc);
1598 update_attr(vc);
1599 break;
1600 case 9: /* set blanking interval */
1601 blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60;
1602 poke_blanked_console();
1603 break;
1604 case 10: /* set bell frequency in Hz */
1605 if (vc->vc_npar >= 1)
1606 vc->vc_bell_pitch = vc->vc_par[1];
1607 else
1608 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1609 break;
1610 case 11: /* set bell duration in msec */
1611 if (vc->vc_npar >= 1)
1612 vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
1613 msecs_to_jiffies(vc->vc_par[1]) : 0;
1614 else
1615 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1616 break;
1617 case 12: /* bring specified console to the front */
1618 if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
1619 set_console(vc->vc_par[1] - 1);
1620 break;
1621 case 13: /* unblank the screen */
1622 poke_blanked_console();
1623 break;
1624 case 14: /* set vesa powerdown interval */
1625 vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
1626 break;
1627 case 15: /* activate the previous console */
1628 set_console(last_console);
1629 break;
1630 case 16: /* set cursor blink duration in msec */
1631 if (vc->vc_npar >= 1 && vc->vc_par[1] >= 50 &&
1632 vc->vc_par[1] <= USHRT_MAX)
1633 vc->vc_cur_blink_ms = vc->vc_par[1];
1634 else
1635 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1636 break;
1637 }
1638}
1639
1640/* console_lock is held */
1641static void csi_at(struct vc_data *vc, unsigned int nr)
1642{
1643 if (nr > vc->vc_cols - vc->vc_x)
1644 nr = vc->vc_cols - vc->vc_x;
1645 else if (!nr)
1646 nr = 1;
1647 insert_char(vc, nr);
1648}
1649
1650/* console_lock is held */
1651static void csi_L(struct vc_data *vc, unsigned int nr)
1652{
1653 if (nr > vc->vc_rows - vc->vc_y)
1654 nr = vc->vc_rows - vc->vc_y;
1655 else if (!nr)
1656 nr = 1;
1657 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_DOWN, nr);
1658 vc->vc_need_wrap = 0;
1659}
1660
1661/* console_lock is held */
1662static void csi_P(struct vc_data *vc, unsigned int nr)
1663{
1664 if (nr > vc->vc_cols - vc->vc_x)
1665 nr = vc->vc_cols - vc->vc_x;
1666 else if (!nr)
1667 nr = 1;
1668 delete_char(vc, nr);
1669}
1670
1671/* console_lock is held */
1672static void csi_M(struct vc_data *vc, unsigned int nr)
1673{
1674 if (nr > vc->vc_rows - vc->vc_y)
1675 nr = vc->vc_rows - vc->vc_y;
1676 else if (!nr)
1677 nr=1;
1678 con_scroll(vc, vc->vc_y, vc->vc_bottom, SM_UP, nr);
1679 vc->vc_need_wrap = 0;
1680}
1681
1682/* console_lock is held (except via vc_init->reset_terminal */
1683static void save_cur(struct vc_data *vc)
1684{
1685 vc->vc_saved_x = vc->vc_x;
1686 vc->vc_saved_y = vc->vc_y;
1687 vc->vc_s_intensity = vc->vc_intensity;
1688 vc->vc_s_italic = vc->vc_italic;
1689 vc->vc_s_underline = vc->vc_underline;
1690 vc->vc_s_blink = vc->vc_blink;
1691 vc->vc_s_reverse = vc->vc_reverse;
1692 vc->vc_s_charset = vc->vc_charset;
1693 vc->vc_s_color = vc->vc_color;
1694 vc->vc_saved_G0 = vc->vc_G0_charset;
1695 vc->vc_saved_G1 = vc->vc_G1_charset;
1696}
1697
1698/* console_lock is held */
1699static void restore_cur(struct vc_data *vc)
1700{
1701 gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
1702 vc->vc_intensity = vc->vc_s_intensity;
1703 vc->vc_italic = vc->vc_s_italic;
1704 vc->vc_underline = vc->vc_s_underline;
1705 vc->vc_blink = vc->vc_s_blink;
1706 vc->vc_reverse = vc->vc_s_reverse;
1707 vc->vc_charset = vc->vc_s_charset;
1708 vc->vc_color = vc->vc_s_color;
1709 vc->vc_G0_charset = vc->vc_saved_G0;
1710 vc->vc_G1_charset = vc->vc_saved_G1;
1711 vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
1712 update_attr(vc);
1713 vc->vc_need_wrap = 0;
1714}
1715
1716enum { ESnormal, ESesc, ESsquare, ESgetpars, ESfunckey,
1717 EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
1718 ESpalette, ESosc };
1719
1720/* console_lock is held (except via vc_init()) */
1721static void reset_terminal(struct vc_data *vc, int do_clear)
1722{
1723 vc->vc_top = 0;
1724 vc->vc_bottom = vc->vc_rows;
1725 vc->vc_state = ESnormal;
1726 vc->vc_ques = 0;
1727 vc->vc_translate = set_translate(LAT1_MAP, vc);
1728 vc->vc_G0_charset = LAT1_MAP;
1729 vc->vc_G1_charset = GRAF_MAP;
1730 vc->vc_charset = 0;
1731 vc->vc_need_wrap = 0;
1732 vc->vc_report_mouse = 0;
1733 vc->vc_utf = default_utf8;
1734 vc->vc_utf_count = 0;
1735
1736 vc->vc_disp_ctrl = 0;
1737 vc->vc_toggle_meta = 0;
1738
1739 vc->vc_decscnm = 0;
1740 vc->vc_decom = 0;
1741 vc->vc_decawm = 1;
1742 vc->vc_deccm = global_cursor_default;
1743 vc->vc_decim = 0;
1744
1745 vt_reset_keyboard(vc->vc_num);
1746
1747 vc->vc_cursor_type = cur_default;
1748 vc->vc_complement_mask = vc->vc_s_complement_mask;
1749
1750 default_attr(vc);
1751 update_attr(vc);
1752
1753 vc->vc_tab_stop[0] =
1754 vc->vc_tab_stop[1] =
1755 vc->vc_tab_stop[2] =
1756 vc->vc_tab_stop[3] =
1757 vc->vc_tab_stop[4] =
1758 vc->vc_tab_stop[5] =
1759 vc->vc_tab_stop[6] =
1760 vc->vc_tab_stop[7] = 0x01010101;
1761
1762 vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
1763 vc->vc_bell_duration = DEFAULT_BELL_DURATION;
1764 vc->vc_cur_blink_ms = DEFAULT_CURSOR_BLINK_MS;
1765
1766 gotoxy(vc, 0, 0);
1767 save_cur(vc);
1768 if (do_clear)
1769 csi_J(vc, 2);
1770}
1771
1772/* console_lock is held */
1773static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
1774{
1775 /*
1776 * Control characters can be used in the _middle_
1777 * of an escape sequence.
1778 */
1779 if (vc->vc_state == ESosc && c>=8 && c<=13) /* ... except for OSC */
1780 return;
1781 switch (c) {
1782 case 0:
1783 return;
1784 case 7:
1785 if (vc->vc_state == ESosc)
1786 vc->vc_state = ESnormal;
1787 else if (vc->vc_bell_duration)
1788 kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
1789 return;
1790 case 8:
1791 bs(vc);
1792 return;
1793 case 9:
1794 vc->vc_pos -= (vc->vc_x << 1);
1795 while (vc->vc_x < vc->vc_cols - 1) {
1796 vc->vc_x++;
1797 if (vc->vc_tab_stop[7 & (vc->vc_x >> 5)] & (1 << (vc->vc_x & 31)))
1798 break;
1799 }
1800 vc->vc_pos += (vc->vc_x << 1);
1801 notify_write(vc, '\t');
1802 return;
1803 case 10: case 11: case 12:
1804 lf(vc);
1805 if (!is_kbd(vc, lnm))
1806 return;
1807 case 13:
1808 cr(vc);
1809 return;
1810 case 14:
1811 vc->vc_charset = 1;
1812 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
1813 vc->vc_disp_ctrl = 1;
1814 return;
1815 case 15:
1816 vc->vc_charset = 0;
1817 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
1818 vc->vc_disp_ctrl = 0;
1819 return;
1820 case 24: case 26:
1821 vc->vc_state = ESnormal;
1822 return;
1823 case 27:
1824 vc->vc_state = ESesc;
1825 return;
1826 case 127:
1827 del(vc);
1828 return;
1829 case 128+27:
1830 vc->vc_state = ESsquare;
1831 return;
1832 }
1833 switch(vc->vc_state) {
1834 case ESesc:
1835 vc->vc_state = ESnormal;
1836 switch (c) {
1837 case '[':
1838 vc->vc_state = ESsquare;
1839 return;
1840 case ']':
1841 vc->vc_state = ESnonstd;
1842 return;
1843 case '%':
1844 vc->vc_state = ESpercent;
1845 return;
1846 case 'E':
1847 cr(vc);
1848 lf(vc);
1849 return;
1850 case 'M':
1851 ri(vc);
1852 return;
1853 case 'D':
1854 lf(vc);
1855 return;
1856 case 'H':
1857 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] |= (1 << (vc->vc_x & 31));
1858 return;
1859 case 'Z':
1860 respond_ID(tty);
1861 return;
1862 case '7':
1863 save_cur(vc);
1864 return;
1865 case '8':
1866 restore_cur(vc);
1867 return;
1868 case '(':
1869 vc->vc_state = ESsetG0;
1870 return;
1871 case ')':
1872 vc->vc_state = ESsetG1;
1873 return;
1874 case '#':
1875 vc->vc_state = EShash;
1876 return;
1877 case 'c':
1878 reset_terminal(vc, 1);
1879 return;
1880 case '>': /* Numeric keypad */
1881 clr_kbd(vc, kbdapplic);
1882 return;
1883 case '=': /* Appl. keypad */
1884 set_kbd(vc, kbdapplic);
1885 return;
1886 }
1887 return;
1888 case ESnonstd:
1889 if (c=='P') { /* palette escape sequence */
1890 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1891 vc->vc_par[vc->vc_npar] = 0;
1892 vc->vc_npar = 0;
1893 vc->vc_state = ESpalette;
1894 return;
1895 } else if (c=='R') { /* reset palette */
1896 reset_palette(vc);
1897 vc->vc_state = ESnormal;
1898 } else if (c>='0' && c<='9')
1899 vc->vc_state = ESosc;
1900 else
1901 vc->vc_state = ESnormal;
1902 return;
1903 case ESpalette:
1904 if (isxdigit(c)) {
1905 vc->vc_par[vc->vc_npar++] = hex_to_bin(c);
1906 if (vc->vc_npar == 7) {
1907 int i = vc->vc_par[0] * 3, j = 1;
1908 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1909 vc->vc_palette[i++] += vc->vc_par[j++];
1910 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1911 vc->vc_palette[i++] += vc->vc_par[j++];
1912 vc->vc_palette[i] = 16 * vc->vc_par[j++];
1913 vc->vc_palette[i] += vc->vc_par[j];
1914 set_palette(vc);
1915 vc->vc_state = ESnormal;
1916 }
1917 } else
1918 vc->vc_state = ESnormal;
1919 return;
1920 case ESsquare:
1921 for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
1922 vc->vc_par[vc->vc_npar] = 0;
1923 vc->vc_npar = 0;
1924 vc->vc_state = ESgetpars;
1925 if (c == '[') { /* Function key */
1926 vc->vc_state=ESfunckey;
1927 return;
1928 }
1929 vc->vc_ques = (c == '?');
1930 if (vc->vc_ques)
1931 return;
1932 case ESgetpars:
1933 if (c == ';' && vc->vc_npar < NPAR - 1) {
1934 vc->vc_npar++;
1935 return;
1936 } else if (c>='0' && c<='9') {
1937 vc->vc_par[vc->vc_npar] *= 10;
1938 vc->vc_par[vc->vc_npar] += c - '0';
1939 return;
1940 }
1941 vc->vc_state = ESnormal;
1942 switch(c) {
1943 case 'h':
1944 set_mode(vc, 1);
1945 return;
1946 case 'l':
1947 set_mode(vc, 0);
1948 return;
1949 case 'c':
1950 if (vc->vc_ques) {
1951 if (vc->vc_par[0])
1952 vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
1953 else
1954 vc->vc_cursor_type = cur_default;
1955 return;
1956 }
1957 break;
1958 case 'm':
1959 if (vc->vc_ques) {
1960 clear_selection();
1961 if (vc->vc_par[0])
1962 vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
1963 else
1964 vc->vc_complement_mask = vc->vc_s_complement_mask;
1965 return;
1966 }
1967 break;
1968 case 'n':
1969 if (!vc->vc_ques) {
1970 if (vc->vc_par[0] == 5)
1971 status_report(tty);
1972 else if (vc->vc_par[0] == 6)
1973 cursor_report(vc, tty);
1974 }
1975 return;
1976 }
1977 if (vc->vc_ques) {
1978 vc->vc_ques = 0;
1979 return;
1980 }
1981 switch(c) {
1982 case 'G': case '`':
1983 if (vc->vc_par[0])
1984 vc->vc_par[0]--;
1985 gotoxy(vc, vc->vc_par[0], vc->vc_y);
1986 return;
1987 case 'A':
1988 if (!vc->vc_par[0])
1989 vc->vc_par[0]++;
1990 gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
1991 return;
1992 case 'B': case 'e':
1993 if (!vc->vc_par[0])
1994 vc->vc_par[0]++;
1995 gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
1996 return;
1997 case 'C': case 'a':
1998 if (!vc->vc_par[0])
1999 vc->vc_par[0]++;
2000 gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
2001 return;
2002 case 'D':
2003 if (!vc->vc_par[0])
2004 vc->vc_par[0]++;
2005 gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
2006 return;
2007 case 'E':
2008 if (!vc->vc_par[0])
2009 vc->vc_par[0]++;
2010 gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
2011 return;
2012 case 'F':
2013 if (!vc->vc_par[0])
2014 vc->vc_par[0]++;
2015 gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
2016 return;
2017 case 'd':
2018 if (vc->vc_par[0])
2019 vc->vc_par[0]--;
2020 gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
2021 return;
2022 case 'H': case 'f':
2023 if (vc->vc_par[0])
2024 vc->vc_par[0]--;
2025 if (vc->vc_par[1])
2026 vc->vc_par[1]--;
2027 gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
2028 return;
2029 case 'J':
2030 csi_J(vc, vc->vc_par[0]);
2031 return;
2032 case 'K':
2033 csi_K(vc, vc->vc_par[0]);
2034 return;
2035 case 'L':
2036 csi_L(vc, vc->vc_par[0]);
2037 return;
2038 case 'M':
2039 csi_M(vc, vc->vc_par[0]);
2040 return;
2041 case 'P':
2042 csi_P(vc, vc->vc_par[0]);
2043 return;
2044 case 'c':
2045 if (!vc->vc_par[0])
2046 respond_ID(tty);
2047 return;
2048 case 'g':
2049 if (!vc->vc_par[0])
2050 vc->vc_tab_stop[7 & (vc->vc_x >> 5)] &= ~(1 << (vc->vc_x & 31));
2051 else if (vc->vc_par[0] == 3) {
2052 vc->vc_tab_stop[0] =
2053 vc->vc_tab_stop[1] =
2054 vc->vc_tab_stop[2] =
2055 vc->vc_tab_stop[3] =
2056 vc->vc_tab_stop[4] =
2057 vc->vc_tab_stop[5] =
2058 vc->vc_tab_stop[6] =
2059 vc->vc_tab_stop[7] = 0;
2060 }
2061 return;
2062 case 'm':
2063 csi_m(vc);
2064 return;
2065 case 'q': /* DECLL - but only 3 leds */
2066 /* map 0,1,2,3 to 0,1,2,4 */
2067 if (vc->vc_par[0] < 4)
2068 vt_set_led_state(vc->vc_num,
2069 (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
2070 return;
2071 case 'r':
2072 if (!vc->vc_par[0])
2073 vc->vc_par[0]++;
2074 if (!vc->vc_par[1])
2075 vc->vc_par[1] = vc->vc_rows;
2076 /* Minimum allowed region is 2 lines */
2077 if (vc->vc_par[0] < vc->vc_par[1] &&
2078 vc->vc_par[1] <= vc->vc_rows) {
2079 vc->vc_top = vc->vc_par[0] - 1;
2080 vc->vc_bottom = vc->vc_par[1];
2081 gotoxay(vc, 0, 0);
2082 }
2083 return;
2084 case 's':
2085 save_cur(vc);
2086 return;
2087 case 'u':
2088 restore_cur(vc);
2089 return;
2090 case 'X':
2091 csi_X(vc, vc->vc_par[0]);
2092 return;
2093 case '@':
2094 csi_at(vc, vc->vc_par[0]);
2095 return;
2096 case ']': /* setterm functions */
2097 setterm_command(vc);
2098 return;
2099 }
2100 return;
2101 case ESpercent:
2102 vc->vc_state = ESnormal;
2103 switch (c) {
2104 case '@': /* defined in ISO 2022 */
2105 vc->vc_utf = 0;
2106 return;
2107 case 'G': /* prelim official escape code */
2108 case '8': /* retained for compatibility */
2109 vc->vc_utf = 1;
2110 return;
2111 }
2112 return;
2113 case ESfunckey:
2114 vc->vc_state = ESnormal;
2115 return;
2116 case EShash:
2117 vc->vc_state = ESnormal;
2118 if (c == '8') {
2119 /* DEC screen alignment test. kludge :-) */
2120 vc->vc_video_erase_char =
2121 (vc->vc_video_erase_char & 0xff00) | 'E';
2122 csi_J(vc, 2);
2123 vc->vc_video_erase_char =
2124 (vc->vc_video_erase_char & 0xff00) | ' ';
2125 do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
2126 }
2127 return;
2128 case ESsetG0:
2129 if (c == '0')
2130 vc->vc_G0_charset = GRAF_MAP;
2131 else if (c == 'B')
2132 vc->vc_G0_charset = LAT1_MAP;
2133 else if (c == 'U')
2134 vc->vc_G0_charset = IBMPC_MAP;
2135 else if (c == 'K')
2136 vc->vc_G0_charset = USER_MAP;
2137 if (vc->vc_charset == 0)
2138 vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
2139 vc->vc_state = ESnormal;
2140 return;
2141 case ESsetG1:
2142 if (c == '0')
2143 vc->vc_G1_charset = GRAF_MAP;
2144 else if (c == 'B')
2145 vc->vc_G1_charset = LAT1_MAP;
2146 else if (c == 'U')
2147 vc->vc_G1_charset = IBMPC_MAP;
2148 else if (c == 'K')
2149 vc->vc_G1_charset = USER_MAP;
2150 if (vc->vc_charset == 1)
2151 vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
2152 vc->vc_state = ESnormal;
2153 return;
2154 case ESosc:
2155 return;
2156 default:
2157 vc->vc_state = ESnormal;
2158 }
2159}
2160
2161/* is_double_width() is based on the wcwidth() implementation by
2162 * Markus Kuhn -- 2007-05-26 (Unicode 5.0)
2163 * Latest version: http://www.cl.cam.ac.uk/~mgk25/ucs/wcwidth.c
2164 */
2165struct interval {
2166 uint32_t first;
2167 uint32_t last;
2168};
2169
2170static int bisearch(uint32_t ucs, const struct interval *table, int max)
2171{
2172 int min = 0;
2173 int mid;
2174
2175 if (ucs < table[0].first || ucs > table[max].last)
2176 return 0;
2177 while (max >= min) {
2178 mid = (min + max) / 2;
2179 if (ucs > table[mid].last)
2180 min = mid + 1;
2181 else if (ucs < table[mid].first)
2182 max = mid - 1;
2183 else
2184 return 1;
2185 }
2186 return 0;
2187}
2188
2189static int is_double_width(uint32_t ucs)
2190{
2191 static const struct interval double_width[] = {
2192 { 0x1100, 0x115F }, { 0x2329, 0x232A }, { 0x2E80, 0x303E },
2193 { 0x3040, 0xA4CF }, { 0xAC00, 0xD7A3 }, { 0xF900, 0xFAFF },
2194 { 0xFE10, 0xFE19 }, { 0xFE30, 0xFE6F }, { 0xFF00, 0xFF60 },
2195 { 0xFFE0, 0xFFE6 }, { 0x20000, 0x2FFFD }, { 0x30000, 0x3FFFD }
2196 };
2197 return bisearch(ucs, double_width, ARRAY_SIZE(double_width) - 1);
2198}
2199
2200static void con_flush(struct vc_data *vc, unsigned long draw_from,
2201 unsigned long draw_to, int *draw_x)
2202{
2203 if (*draw_x < 0)
2204 return;
2205
2206 vc->vc_sw->con_putcs(vc, (u16 *)draw_from,
2207 (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, *draw_x);
2208 *draw_x = -1;
2209}
2210
2211/* acquires console_lock */
2212static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2213{
2214 int c, tc, ok, n = 0, draw_x = -1;
2215 unsigned int currcons;
2216 unsigned long draw_from = 0, draw_to = 0;
2217 struct vc_data *vc;
2218 unsigned char vc_attr;
2219 struct vt_notifier_param param;
2220 uint8_t rescan;
2221 uint8_t inverse;
2222 uint8_t width;
2223 u16 himask, charmask;
2224
2225 if (in_interrupt())
2226 return count;
2227
2228 might_sleep();
2229
2230 console_lock();
2231 vc = tty->driver_data;
2232 if (vc == NULL) {
2233 printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
2234 console_unlock();
2235 return 0;
2236 }
2237
2238 currcons = vc->vc_num;
2239 if (!vc_cons_allocated(currcons)) {
2240 /* could this happen? */
2241 pr_warn_once("con_write: tty %d not allocated\n", currcons+1);
2242 console_unlock();
2243 return 0;
2244 }
2245
2246 himask = vc->vc_hi_font_mask;
2247 charmask = himask ? 0x1ff : 0xff;
2248
2249 /* undraw cursor first */
2250 if (con_is_fg(vc))
2251 hide_cursor(vc);
2252
2253 param.vc = vc;
2254
2255 while (!tty->stopped && count) {
2256 int orig = *buf;
2257 c = orig;
2258 buf++;
2259 n++;
2260 count--;
2261 rescan = 0;
2262 inverse = 0;
2263 width = 1;
2264
2265 /* Do no translation at all in control states */
2266 if (vc->vc_state != ESnormal) {
2267 tc = c;
2268 } else if (vc->vc_utf && !vc->vc_disp_ctrl) {
2269 /* Combine UTF-8 into Unicode in vc_utf_char.
2270 * vc_utf_count is the number of continuation bytes still
2271 * expected to arrive.
2272 * vc_npar is the number of continuation bytes arrived so
2273 * far
2274 */
2275rescan_last_byte:
2276 if ((c & 0xc0) == 0x80) {
2277 /* Continuation byte received */
2278 static const uint32_t utf8_length_changes[] = { 0x0000007f, 0x000007ff, 0x0000ffff, 0x001fffff, 0x03ffffff, 0x7fffffff };
2279 if (vc->vc_utf_count) {
2280 vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
2281 vc->vc_npar++;
2282 if (--vc->vc_utf_count) {
2283 /* Still need some bytes */
2284 continue;
2285 }
2286 /* Got a whole character */
2287 c = vc->vc_utf_char;
2288 /* Reject overlong sequences */
2289 if (c <= utf8_length_changes[vc->vc_npar - 1] ||
2290 c > utf8_length_changes[vc->vc_npar])
2291 c = 0xfffd;
2292 } else {
2293 /* Unexpected continuation byte */
2294 vc->vc_utf_count = 0;
2295 c = 0xfffd;
2296 }
2297 } else {
2298 /* Single ASCII byte or first byte of a sequence received */
2299 if (vc->vc_utf_count) {
2300 /* Continuation byte expected */
2301 rescan = 1;
2302 vc->vc_utf_count = 0;
2303 c = 0xfffd;
2304 } else if (c > 0x7f) {
2305 /* First byte of a multibyte sequence received */
2306 vc->vc_npar = 0;
2307 if ((c & 0xe0) == 0xc0) {
2308 vc->vc_utf_count = 1;
2309 vc->vc_utf_char = (c & 0x1f);
2310 } else if ((c & 0xf0) == 0xe0) {
2311 vc->vc_utf_count = 2;
2312 vc->vc_utf_char = (c & 0x0f);
2313 } else if ((c & 0xf8) == 0xf0) {
2314 vc->vc_utf_count = 3;
2315 vc->vc_utf_char = (c & 0x07);
2316 } else if ((c & 0xfc) == 0xf8) {
2317 vc->vc_utf_count = 4;
2318 vc->vc_utf_char = (c & 0x03);
2319 } else if ((c & 0xfe) == 0xfc) {
2320 vc->vc_utf_count = 5;
2321 vc->vc_utf_char = (c & 0x01);
2322 } else {
2323 /* 254 and 255 are invalid */
2324 c = 0xfffd;
2325 }
2326 if (vc->vc_utf_count) {
2327 /* Still need some bytes */
2328 continue;
2329 }
2330 }
2331 /* Nothing to do if an ASCII byte was received */
2332 }
2333 /* End of UTF-8 decoding. */
2334 /* c is the received character, or U+FFFD for invalid sequences. */
2335 /* Replace invalid Unicode code points with U+FFFD too */
2336 if ((c >= 0xd800 && c <= 0xdfff) || c == 0xfffe || c == 0xffff)
2337 c = 0xfffd;
2338 tc = c;
2339 } else { /* no utf or alternate charset mode */
2340 tc = vc_translate(vc, c);
2341 }
2342
2343 param.c = tc;
2344 if (atomic_notifier_call_chain(&vt_notifier_list, VT_PREWRITE,
2345 &param) == NOTIFY_STOP)
2346 continue;
2347
2348 /* If the original code was a control character we
2349 * only allow a glyph to be displayed if the code is
2350 * not normally used (such as for cursor movement) or
2351 * if the disp_ctrl mode has been explicitly enabled.
2352 * Certain characters (as given by the CTRL_ALWAYS
2353 * bitmap) are always displayed as control characters,
2354 * as the console would be pretty useless without
2355 * them; to display an arbitrary font position use the
2356 * direct-to-font zone in UTF-8 mode.
2357 */
2358 ok = tc && (c >= 32 ||
2359 !(vc->vc_disp_ctrl ? (CTRL_ALWAYS >> c) & 1 :
2360 vc->vc_utf || ((CTRL_ACTION >> c) & 1)))
2361 && (c != 127 || vc->vc_disp_ctrl)
2362 && (c != 128+27);
2363
2364 if (vc->vc_state == ESnormal && ok) {
2365 if (vc->vc_utf && !vc->vc_disp_ctrl) {
2366 if (is_double_width(c))
2367 width = 2;
2368 }
2369 /* Now try to find out how to display it */
2370 tc = conv_uni_to_pc(vc, tc);
2371 if (tc & ~charmask) {
2372 if (tc == -1 || tc == -2) {
2373 continue; /* nothing to display */
2374 }
2375 /* Glyph not found */
2376 if ((!(vc->vc_utf && !vc->vc_disp_ctrl) || c < 128) && !(c & ~charmask)) {
2377 /* In legacy mode use the glyph we get by a 1:1 mapping.
2378 This would make absolutely no sense with Unicode in mind,
2379 but do this for ASCII characters since a font may lack
2380 Unicode mapping info and we don't want to end up with
2381 having question marks only. */
2382 tc = c;
2383 } else {
2384 /* Display U+FFFD. If it's not found, display an inverse question mark. */
2385 tc = conv_uni_to_pc(vc, 0xfffd);
2386 if (tc < 0) {
2387 inverse = 1;
2388 tc = conv_uni_to_pc(vc, '?');
2389 if (tc < 0) tc = '?';
2390 }
2391 }
2392 }
2393
2394 if (!inverse) {
2395 vc_attr = vc->vc_attr;
2396 } else {
2397 /* invert vc_attr */
2398 if (!vc->vc_can_do_color) {
2399 vc_attr = (vc->vc_attr) ^ 0x08;
2400 } else if (vc->vc_hi_font_mask == 0x100) {
2401 vc_attr = ((vc->vc_attr) & 0x11) | (((vc->vc_attr) & 0xe0) >> 4) | (((vc->vc_attr) & 0x0e) << 4);
2402 } else {
2403 vc_attr = ((vc->vc_attr) & 0x88) | (((vc->vc_attr) & 0x70) >> 4) | (((vc->vc_attr) & 0x07) << 4);
2404 }
2405 con_flush(vc, draw_from, draw_to, &draw_x);
2406 }
2407
2408 while (1) {
2409 if (vc->vc_need_wrap || vc->vc_decim)
2410 con_flush(vc, draw_from, draw_to,
2411 &draw_x);
2412 if (vc->vc_need_wrap) {
2413 cr(vc);
2414 lf(vc);
2415 }
2416 if (vc->vc_decim)
2417 insert_char(vc, 1);
2418 scr_writew(himask ?
2419 ((vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
2420 (vc_attr << 8) + tc,
2421 (u16 *) vc->vc_pos);
2422 if (con_should_update(vc) && draw_x < 0) {
2423 draw_x = vc->vc_x;
2424 draw_from = vc->vc_pos;
2425 }
2426 if (vc->vc_x == vc->vc_cols - 1) {
2427 vc->vc_need_wrap = vc->vc_decawm;
2428 draw_to = vc->vc_pos + 2;
2429 } else {
2430 vc->vc_x++;
2431 draw_to = (vc->vc_pos += 2);
2432 }
2433
2434 if (!--width) break;
2435
2436 tc = conv_uni_to_pc(vc, ' '); /* A space is printed in the second column */
2437 if (tc < 0) tc = ' ';
2438 }
2439 notify_write(vc, c);
2440
2441 if (inverse)
2442 con_flush(vc, draw_from, draw_to, &draw_x);
2443
2444 if (rescan) {
2445 rescan = 0;
2446 inverse = 0;
2447 width = 1;
2448 c = orig;
2449 goto rescan_last_byte;
2450 }
2451 continue;
2452 }
2453 con_flush(vc, draw_from, draw_to, &draw_x);
2454 do_con_trol(tty, vc, orig);
2455 }
2456 con_flush(vc, draw_from, draw_to, &draw_x);
2457 console_conditional_schedule();
2458 notify_update(vc);
2459 console_unlock();
2460 return n;
2461}
2462
2463/*
2464 * This is the console switching callback.
2465 *
2466 * Doing console switching in a process context allows
2467 * us to do the switches asynchronously (needed when we want
2468 * to switch due to a keyboard interrupt). Synchronization
2469 * with other console code and prevention of re-entrancy is
2470 * ensured with console_lock.
2471 */
2472static void console_callback(struct work_struct *ignored)
2473{
2474 console_lock();
2475
2476 if (want_console >= 0) {
2477 if (want_console != fg_console &&
2478 vc_cons_allocated(want_console)) {
2479 hide_cursor(vc_cons[fg_console].d);
2480 change_console(vc_cons[want_console].d);
2481 /* we only changed when the console had already
2482 been allocated - a new console is not created
2483 in an interrupt routine */
2484 }
2485 want_console = -1;
2486 }
2487 if (do_poke_blanked_console) { /* do not unblank for a LED change */
2488 do_poke_blanked_console = 0;
2489 poke_blanked_console();
2490 }
2491 if (scrollback_delta) {
2492 struct vc_data *vc = vc_cons[fg_console].d;
2493 clear_selection();
2494 if (vc->vc_mode == KD_TEXT && vc->vc_sw->con_scrolldelta)
2495 vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
2496 scrollback_delta = 0;
2497 }
2498 if (blank_timer_expired) {
2499 do_blank_screen(0);
2500 blank_timer_expired = 0;
2501 }
2502 notify_update(vc_cons[fg_console].d);
2503
2504 console_unlock();
2505}
2506
2507int set_console(int nr)
2508{
2509 struct vc_data *vc = vc_cons[fg_console].d;
2510
2511 if (!vc_cons_allocated(nr) || vt_dont_switch ||
2512 (vc->vt_mode.mode == VT_AUTO && vc->vc_mode == KD_GRAPHICS)) {
2513
2514 /*
2515 * Console switch will fail in console_callback() or
2516 * change_console() so there is no point scheduling
2517 * the callback
2518 *
2519 * Existing set_console() users don't check the return
2520 * value so this shouldn't break anything
2521 */
2522 return -EINVAL;
2523 }
2524
2525 want_console = nr;
2526 schedule_console_callback();
2527
2528 return 0;
2529}
2530
2531struct tty_driver *console_driver;
2532
2533#ifdef CONFIG_VT_CONSOLE
2534
2535/**
2536 * vt_kmsg_redirect() - Sets/gets the kernel message console
2537 * @new: The new virtual terminal number or -1 if the console should stay
2538 * unchanged
2539 *
2540 * By default, the kernel messages are always printed on the current virtual
2541 * console. However, the user may modify that default with the
2542 * TIOCL_SETKMSGREDIRECT ioctl call.
2543 *
2544 * This function sets the kernel message console to be @new. It returns the old
2545 * virtual console number. The virtual terminal number 0 (both as parameter and
2546 * return value) means no redirection (i.e. always printed on the currently
2547 * active console).
2548 *
2549 * The parameter -1 means that only the current console is returned, but the
2550 * value is not modified. You may use the macro vt_get_kmsg_redirect() in that
2551 * case to make the code more understandable.
2552 *
2553 * When the kernel is compiled without CONFIG_VT_CONSOLE, this function ignores
2554 * the parameter and always returns 0.
2555 */
2556int vt_kmsg_redirect(int new)
2557{
2558 static int kmsg_con;
2559
2560 if (new != -1)
2561 return xchg(&kmsg_con, new);
2562 else
2563 return kmsg_con;
2564}
2565
2566/*
2567 * Console on virtual terminal
2568 *
2569 * The console must be locked when we get here.
2570 */
2571
2572static void vt_console_print(struct console *co, const char *b, unsigned count)
2573{
2574 struct vc_data *vc = vc_cons[fg_console].d;
2575 unsigned char c;
2576 static DEFINE_SPINLOCK(printing_lock);
2577 const ushort *start;
2578 ushort cnt = 0;
2579 ushort myx;
2580 int kmsg_console;
2581
2582 /* console busy or not yet initialized */
2583 if (!printable)
2584 return;
2585 if (!spin_trylock(&printing_lock))
2586 return;
2587
2588 kmsg_console = vt_get_kmsg_redirect();
2589 if (kmsg_console && vc_cons_allocated(kmsg_console - 1))
2590 vc = vc_cons[kmsg_console - 1].d;
2591
2592 /* read `x' only after setting currcons properly (otherwise
2593 the `x' macro will read the x of the foreground console). */
2594 myx = vc->vc_x;
2595
2596 if (!vc_cons_allocated(fg_console)) {
2597 /* impossible */
2598 /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
2599 goto quit;
2600 }
2601
2602 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
2603 goto quit;
2604
2605 /* undraw cursor first */
2606 if (con_is_fg(vc))
2607 hide_cursor(vc);
2608
2609 start = (ushort *)vc->vc_pos;
2610
2611 /* Contrived structure to try to emulate original need_wrap behaviour
2612 * Problems caused when we have need_wrap set on '\n' character */
2613 while (count--) {
2614 c = *b++;
2615 if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
2616 if (cnt > 0) {
2617 if (con_is_visible(vc))
2618 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2619 vc->vc_x += cnt;
2620 if (vc->vc_need_wrap)
2621 vc->vc_x--;
2622 cnt = 0;
2623 }
2624 if (c == 8) { /* backspace */
2625 bs(vc);
2626 start = (ushort *)vc->vc_pos;
2627 myx = vc->vc_x;
2628 continue;
2629 }
2630 if (c != 13)
2631 lf(vc);
2632 cr(vc);
2633 start = (ushort *)vc->vc_pos;
2634 myx = vc->vc_x;
2635 if (c == 10 || c == 13)
2636 continue;
2637 }
2638 scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
2639 notify_write(vc, c);
2640 cnt++;
2641 if (myx == vc->vc_cols - 1) {
2642 vc->vc_need_wrap = 1;
2643 continue;
2644 }
2645 vc->vc_pos += 2;
2646 myx++;
2647 }
2648 if (cnt > 0) {
2649 if (con_is_visible(vc))
2650 vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
2651 vc->vc_x += cnt;
2652 if (vc->vc_x == vc->vc_cols) {
2653 vc->vc_x--;
2654 vc->vc_need_wrap = 1;
2655 }
2656 }
2657 set_cursor(vc);
2658 notify_update(vc);
2659
2660quit:
2661 spin_unlock(&printing_lock);
2662}
2663
2664static struct tty_driver *vt_console_device(struct console *c, int *index)
2665{
2666 *index = c->index ? c->index-1 : fg_console;
2667 return console_driver;
2668}
2669
2670static struct console vt_console_driver = {
2671 .name = "tty",
2672 .write = vt_console_print,
2673 .device = vt_console_device,
2674 .unblank = unblank_screen,
2675 .flags = CON_PRINTBUFFER,
2676 .index = -1,
2677};
2678#endif
2679
2680/*
2681 * Handling of Linux-specific VC ioctls
2682 */
2683
2684/*
2685 * Generally a bit racy with respect to console_lock();.
2686 *
2687 * There are some functions which don't need it.
2688 *
2689 * There are some functions which can sleep for arbitrary periods
2690 * (paste_selection) but we don't need the lock there anyway.
2691 *
2692 * set_selection has locking, and definitely needs it
2693 */
2694
2695int tioclinux(struct tty_struct *tty, unsigned long arg)
2696{
2697 char type, data;
2698 char __user *p = (char __user *)arg;
2699 int lines;
2700 int ret;
2701
2702 if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
2703 return -EPERM;
2704 if (get_user(type, p))
2705 return -EFAULT;
2706 ret = 0;
2707
2708 switch (type)
2709 {
2710 case TIOCL_SETSEL:
2711 ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
2712 break;
2713 case TIOCL_PASTESEL:
2714 ret = paste_selection(tty);
2715 break;
2716 case TIOCL_UNBLANKSCREEN:
2717 console_lock();
2718 unblank_screen();
2719 console_unlock();
2720 break;
2721 case TIOCL_SELLOADLUT:
2722 console_lock();
2723 ret = sel_loadlut(p);
2724 console_unlock();
2725 break;
2726 case TIOCL_GETSHIFTSTATE:
2727
2728 /*
2729 * Make it possible to react to Shift+Mousebutton.
2730 * Note that 'shift_state' is an undocumented
2731 * kernel-internal variable; programs not closely
2732 * related to the kernel should not use this.
2733 */
2734 data = vt_get_shift_state();
2735 ret = put_user(data, p);
2736 break;
2737 case TIOCL_GETMOUSEREPORTING:
2738 console_lock(); /* May be overkill */
2739 data = mouse_reporting();
2740 console_unlock();
2741 ret = put_user(data, p);
2742 break;
2743 case TIOCL_SETVESABLANK:
2744 console_lock();
2745 ret = set_vesa_blanking(p);
2746 console_unlock();
2747 break;
2748 case TIOCL_GETKMSGREDIRECT:
2749 data = vt_get_kmsg_redirect();
2750 ret = put_user(data, p);
2751 break;
2752 case TIOCL_SETKMSGREDIRECT:
2753 if (!capable(CAP_SYS_ADMIN)) {
2754 ret = -EPERM;
2755 } else {
2756 if (get_user(data, p+1))
2757 ret = -EFAULT;
2758 else
2759 vt_kmsg_redirect(data);
2760 }
2761 break;
2762 case TIOCL_GETFGCONSOLE:
2763 /* No locking needed as this is a transiently
2764 correct return anyway if the caller hasn't
2765 disabled switching */
2766 ret = fg_console;
2767 break;
2768 case TIOCL_SCROLLCONSOLE:
2769 if (get_user(lines, (s32 __user *)(p+4))) {
2770 ret = -EFAULT;
2771 } else {
2772 /* Need the console lock here. Note that lots
2773 of other calls need fixing before the lock
2774 is actually useful ! */
2775 console_lock();
2776 scrollfront(vc_cons[fg_console].d, lines);
2777 console_unlock();
2778 ret = 0;
2779 }
2780 break;
2781 case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
2782 console_lock();
2783 ignore_poke = 1;
2784 do_blank_screen(0);
2785 console_unlock();
2786 break;
2787 case TIOCL_BLANKEDSCREEN:
2788 ret = console_blanked;
2789 break;
2790 default:
2791 ret = -EINVAL;
2792 break;
2793 }
2794 return ret;
2795}
2796
2797/*
2798 * /dev/ttyN handling
2799 */
2800
2801static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
2802{
2803 int retval;
2804
2805 retval = do_con_write(tty, buf, count);
2806 con_flush_chars(tty);
2807
2808 return retval;
2809}
2810
2811static int con_put_char(struct tty_struct *tty, unsigned char ch)
2812{
2813 if (in_interrupt())
2814 return 0; /* n_r3964 calls put_char() from interrupt context */
2815 return do_con_write(tty, &ch, 1);
2816}
2817
2818static int con_write_room(struct tty_struct *tty)
2819{
2820 if (tty->stopped)
2821 return 0;
2822 return 32768; /* No limit, really; we're not buffering */
2823}
2824
2825static int con_chars_in_buffer(struct tty_struct *tty)
2826{
2827 return 0; /* we're not buffering */
2828}
2829
2830/*
2831 * con_throttle and con_unthrottle are only used for
2832 * paste_selection(), which has to stuff in a large number of
2833 * characters...
2834 */
2835static void con_throttle(struct tty_struct *tty)
2836{
2837}
2838
2839static void con_unthrottle(struct tty_struct *tty)
2840{
2841 struct vc_data *vc = tty->driver_data;
2842
2843 wake_up_interruptible(&vc->paste_wait);
2844}
2845
2846/*
2847 * Turn the Scroll-Lock LED on when the tty is stopped
2848 */
2849static void con_stop(struct tty_struct *tty)
2850{
2851 int console_num;
2852 if (!tty)
2853 return;
2854 console_num = tty->index;
2855 if (!vc_cons_allocated(console_num))
2856 return;
2857 vt_kbd_con_stop(console_num);
2858}
2859
2860/*
2861 * Turn the Scroll-Lock LED off when the console is started
2862 */
2863static void con_start(struct tty_struct *tty)
2864{
2865 int console_num;
2866 if (!tty)
2867 return;
2868 console_num = tty->index;
2869 if (!vc_cons_allocated(console_num))
2870 return;
2871 vt_kbd_con_start(console_num);
2872}
2873
2874static void con_flush_chars(struct tty_struct *tty)
2875{
2876 struct vc_data *vc;
2877
2878 if (in_interrupt()) /* from flush_to_ldisc */
2879 return;
2880
2881 /* if we race with con_close(), vt may be null */
2882 console_lock();
2883 vc = tty->driver_data;
2884 if (vc)
2885 set_cursor(vc);
2886 console_unlock();
2887}
2888
2889/*
2890 * Allocate the console screen memory.
2891 */
2892static int con_install(struct tty_driver *driver, struct tty_struct *tty)
2893{
2894 unsigned int currcons = tty->index;
2895 struct vc_data *vc;
2896 int ret;
2897
2898 console_lock();
2899 ret = vc_allocate(currcons);
2900 if (ret)
2901 goto unlock;
2902
2903 vc = vc_cons[currcons].d;
2904
2905 /* Still being freed */
2906 if (vc->port.tty) {
2907 ret = -ERESTARTSYS;
2908 goto unlock;
2909 }
2910
2911 ret = tty_port_install(&vc->port, driver, tty);
2912 if (ret)
2913 goto unlock;
2914
2915 tty->driver_data = vc;
2916 vc->port.tty = tty;
2917 tty_port_get(&vc->port);
2918
2919 if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
2920 tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
2921 tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
2922 }
2923 if (vc->vc_utf)
2924 tty->termios.c_iflag |= IUTF8;
2925 else
2926 tty->termios.c_iflag &= ~IUTF8;
2927unlock:
2928 console_unlock();
2929 return ret;
2930}
2931
2932static int con_open(struct tty_struct *tty, struct file *filp)
2933{
2934 /* everything done in install */
2935 return 0;
2936}
2937
2938
2939static void con_close(struct tty_struct *tty, struct file *filp)
2940{
2941 /* Nothing to do - we defer to shutdown */
2942}
2943
2944static void con_shutdown(struct tty_struct *tty)
2945{
2946 struct vc_data *vc = tty->driver_data;
2947 BUG_ON(vc == NULL);
2948 console_lock();
2949 vc->port.tty = NULL;
2950 console_unlock();
2951}
2952
2953static void con_cleanup(struct tty_struct *tty)
2954{
2955 struct vc_data *vc = tty->driver_data;
2956
2957 tty_port_put(&vc->port);
2958}
2959
2960static int default_color = 7; /* white */
2961static int default_italic_color = 2; // green (ASCII)
2962static int default_underline_color = 3; // cyan (ASCII)
2963module_param_named(color, default_color, int, S_IRUGO | S_IWUSR);
2964module_param_named(italic, default_italic_color, int, S_IRUGO | S_IWUSR);
2965module_param_named(underline, default_underline_color, int, S_IRUGO | S_IWUSR);
2966
2967static void vc_init(struct vc_data *vc, unsigned int rows,
2968 unsigned int cols, int do_clear)
2969{
2970 int j, k ;
2971
2972 vc->vc_cols = cols;
2973 vc->vc_rows = rows;
2974 vc->vc_size_row = cols << 1;
2975 vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
2976
2977 set_origin(vc);
2978 vc->vc_pos = vc->vc_origin;
2979 reset_vc(vc);
2980 for (j=k=0; j<16; j++) {
2981 vc->vc_palette[k++] = default_red[j] ;
2982 vc->vc_palette[k++] = default_grn[j] ;
2983 vc->vc_palette[k++] = default_blu[j] ;
2984 }
2985 vc->vc_def_color = default_color;
2986 vc->vc_ulcolor = default_underline_color;
2987 vc->vc_itcolor = default_italic_color;
2988 vc->vc_halfcolor = 0x08; /* grey */
2989 init_waitqueue_head(&vc->paste_wait);
2990 reset_terminal(vc, do_clear);
2991}
2992
2993/*
2994 * This routine initializes console interrupts, and does nothing
2995 * else. If you want the screen to clear, call tty_write with
2996 * the appropriate escape-sequence.
2997 */
2998
2999static int __init con_init(void)
3000{
3001 const char *display_desc = NULL;
3002 struct vc_data *vc;
3003 unsigned int currcons = 0, i;
3004
3005 console_lock();
3006
3007 if (conswitchp)
3008 display_desc = conswitchp->con_startup();
3009 if (!display_desc) {
3010 fg_console = 0;
3011 console_unlock();
3012 return 0;
3013 }
3014
3015 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3016 struct con_driver *con_driver = &registered_con_driver[i];
3017
3018 if (con_driver->con == NULL) {
3019 con_driver->con = conswitchp;
3020 con_driver->desc = display_desc;
3021 con_driver->flag = CON_DRIVER_FLAG_INIT;
3022 con_driver->first = 0;
3023 con_driver->last = MAX_NR_CONSOLES - 1;
3024 break;
3025 }
3026 }
3027
3028 for (i = 0; i < MAX_NR_CONSOLES; i++)
3029 con_driver_map[i] = conswitchp;
3030
3031 if (blankinterval) {
3032 blank_state = blank_normal_wait;
3033 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3034 }
3035
3036 for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
3037 vc_cons[currcons].d = vc = kzalloc(sizeof(struct vc_data), GFP_NOWAIT);
3038 INIT_WORK(&vc_cons[currcons].SAK_work, vc_SAK);
3039 tty_port_init(&vc->port);
3040 visual_init(vc, currcons, 1);
3041 /* Assuming vc->vc_{cols,rows,screenbuf_size} are sane here. */
3042 vc->vc_screenbuf = kzalloc(vc->vc_screenbuf_size, GFP_NOWAIT);
3043 vc_init(vc, vc->vc_rows, vc->vc_cols,
3044 currcons || !vc->vc_sw->con_save_screen);
3045 }
3046 currcons = fg_console = 0;
3047 master_display_fg = vc = vc_cons[currcons].d;
3048 set_origin(vc);
3049 save_screen(vc);
3050 gotoxy(vc, vc->vc_x, vc->vc_y);
3051 csi_J(vc, 0);
3052 update_screen(vc);
3053 pr_info("Console: %s %s %dx%d\n",
3054 vc->vc_can_do_color ? "colour" : "mono",
3055 display_desc, vc->vc_cols, vc->vc_rows);
3056 printable = 1;
3057
3058 console_unlock();
3059
3060#ifdef CONFIG_VT_CONSOLE
3061 register_console(&vt_console_driver);
3062#endif
3063 return 0;
3064}
3065console_initcall(con_init);
3066
3067static const struct tty_operations con_ops = {
3068 .install = con_install,
3069 .open = con_open,
3070 .close = con_close,
3071 .write = con_write,
3072 .write_room = con_write_room,
3073 .put_char = con_put_char,
3074 .flush_chars = con_flush_chars,
3075 .chars_in_buffer = con_chars_in_buffer,
3076 .ioctl = vt_ioctl,
3077#ifdef CONFIG_COMPAT
3078 .compat_ioctl = vt_compat_ioctl,
3079#endif
3080 .stop = con_stop,
3081 .start = con_start,
3082 .throttle = con_throttle,
3083 .unthrottle = con_unthrottle,
3084 .resize = vt_resize,
3085 .shutdown = con_shutdown,
3086 .cleanup = con_cleanup,
3087};
3088
3089static struct cdev vc0_cdev;
3090
3091static ssize_t show_tty_active(struct device *dev,
3092 struct device_attribute *attr, char *buf)
3093{
3094 return sprintf(buf, "tty%d\n", fg_console + 1);
3095}
3096static DEVICE_ATTR(active, S_IRUGO, show_tty_active, NULL);
3097
3098static struct attribute *vt_dev_attrs[] = {
3099 &dev_attr_active.attr,
3100 NULL
3101};
3102
3103ATTRIBUTE_GROUPS(vt_dev);
3104
3105int __init vty_init(const struct file_operations *console_fops)
3106{
3107 cdev_init(&vc0_cdev, console_fops);
3108 if (cdev_add(&vc0_cdev, MKDEV(TTY_MAJOR, 0), 1) ||
3109 register_chrdev_region(MKDEV(TTY_MAJOR, 0), 1, "/dev/vc/0") < 0)
3110 panic("Couldn't register /dev/tty0 driver\n");
3111 tty0dev = device_create_with_groups(tty_class, NULL,
3112 MKDEV(TTY_MAJOR, 0), NULL,
3113 vt_dev_groups, "tty0");
3114 if (IS_ERR(tty0dev))
3115 tty0dev = NULL;
3116
3117 vcs_init();
3118
3119 console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
3120 if (!console_driver)
3121 panic("Couldn't allocate console driver\n");
3122
3123 console_driver->name = "tty";
3124 console_driver->name_base = 1;
3125 console_driver->major = TTY_MAJOR;
3126 console_driver->minor_start = 1;
3127 console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
3128 console_driver->init_termios = tty_std_termios;
3129 if (default_utf8)
3130 console_driver->init_termios.c_iflag |= IUTF8;
3131 console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
3132 tty_set_operations(console_driver, &con_ops);
3133 if (tty_register_driver(console_driver))
3134 panic("Couldn't register console driver\n");
3135 kbd_init();
3136 console_map_init();
3137#ifdef CONFIG_MDA_CONSOLE
3138 mda_console_init();
3139#endif
3140 return 0;
3141}
3142
3143#ifndef VT_SINGLE_DRIVER
3144
3145static struct class *vtconsole_class;
3146
3147static int do_bind_con_driver(const struct consw *csw, int first, int last,
3148 int deflt)
3149{
3150 struct module *owner = csw->owner;
3151 const char *desc = NULL;
3152 struct con_driver *con_driver;
3153 int i, j = -1, k = -1, retval = -ENODEV;
3154
3155 if (!try_module_get(owner))
3156 return -ENODEV;
3157
3158 WARN_CONSOLE_UNLOCKED();
3159
3160 /* check if driver is registered */
3161 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3162 con_driver = &registered_con_driver[i];
3163
3164 if (con_driver->con == csw) {
3165 desc = con_driver->desc;
3166 retval = 0;
3167 break;
3168 }
3169 }
3170
3171 if (retval)
3172 goto err;
3173
3174 if (!(con_driver->flag & CON_DRIVER_FLAG_INIT)) {
3175 csw->con_startup();
3176 con_driver->flag |= CON_DRIVER_FLAG_INIT;
3177 }
3178
3179 if (deflt) {
3180 if (conswitchp)
3181 module_put(conswitchp->owner);
3182
3183 __module_get(owner);
3184 conswitchp = csw;
3185 }
3186
3187 first = max(first, con_driver->first);
3188 last = min(last, con_driver->last);
3189
3190 for (i = first; i <= last; i++) {
3191 int old_was_color;
3192 struct vc_data *vc = vc_cons[i].d;
3193
3194 if (con_driver_map[i])
3195 module_put(con_driver_map[i]->owner);
3196 __module_get(owner);
3197 con_driver_map[i] = csw;
3198
3199 if (!vc || !vc->vc_sw)
3200 continue;
3201
3202 j = i;
3203
3204 if (con_is_visible(vc)) {
3205 k = i;
3206 save_screen(vc);
3207 }
3208
3209 old_was_color = vc->vc_can_do_color;
3210 vc->vc_sw->con_deinit(vc);
3211 vc->vc_origin = (unsigned long)vc->vc_screenbuf;
3212 visual_init(vc, i, 0);
3213 set_origin(vc);
3214 update_attr(vc);
3215
3216 /* If the console changed between mono <-> color, then
3217 * the attributes in the screenbuf will be wrong. The
3218 * following resets all attributes to something sane.
3219 */
3220 if (old_was_color != vc->vc_can_do_color)
3221 clear_buffer_attributes(vc);
3222 }
3223
3224 pr_info("Console: switching ");
3225 if (!deflt)
3226 printk(KERN_CONT "consoles %d-%d ", first+1, last+1);
3227 if (j >= 0) {
3228 struct vc_data *vc = vc_cons[j].d;
3229
3230 printk(KERN_CONT "to %s %s %dx%d\n",
3231 vc->vc_can_do_color ? "colour" : "mono",
3232 desc, vc->vc_cols, vc->vc_rows);
3233
3234 if (k >= 0) {
3235 vc = vc_cons[k].d;
3236 update_screen(vc);
3237 }
3238 } else
3239 printk(KERN_CONT "to %s\n", desc);
3240
3241 retval = 0;
3242err:
3243 module_put(owner);
3244 return retval;
3245};
3246
3247
3248#ifdef CONFIG_VT_HW_CONSOLE_BINDING
3249/* unlocked version of unbind_con_driver() */
3250int do_unbind_con_driver(const struct consw *csw, int first, int last, int deflt)
3251{
3252 struct module *owner = csw->owner;
3253 const struct consw *defcsw = NULL;
3254 struct con_driver *con_driver = NULL, *con_back = NULL;
3255 int i, retval = -ENODEV;
3256
3257 if (!try_module_get(owner))
3258 return -ENODEV;
3259
3260 WARN_CONSOLE_UNLOCKED();
3261
3262 /* check if driver is registered and if it is unbindable */
3263 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3264 con_driver = &registered_con_driver[i];
3265
3266 if (con_driver->con == csw &&
3267 con_driver->flag & CON_DRIVER_FLAG_MODULE) {
3268 retval = 0;
3269 break;
3270 }
3271 }
3272
3273 if (retval)
3274 goto err;
3275
3276 retval = -ENODEV;
3277
3278 /* check if backup driver exists */
3279 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3280 con_back = &registered_con_driver[i];
3281
3282 if (con_back->con && con_back->con != csw) {
3283 defcsw = con_back->con;
3284 retval = 0;
3285 break;
3286 }
3287 }
3288
3289 if (retval)
3290 goto err;
3291
3292 if (!con_is_bound(csw))
3293 goto err;
3294
3295 first = max(first, con_driver->first);
3296 last = min(last, con_driver->last);
3297
3298 for (i = first; i <= last; i++) {
3299 if (con_driver_map[i] == csw) {
3300 module_put(csw->owner);
3301 con_driver_map[i] = NULL;
3302 }
3303 }
3304
3305 if (!con_is_bound(defcsw)) {
3306 const struct consw *defconsw = conswitchp;
3307
3308 defcsw->con_startup();
3309 con_back->flag |= CON_DRIVER_FLAG_INIT;
3310 /*
3311 * vgacon may change the default driver to point
3312 * to dummycon, we restore it here...
3313 */
3314 conswitchp = defconsw;
3315 }
3316
3317 if (!con_is_bound(csw))
3318 con_driver->flag &= ~CON_DRIVER_FLAG_INIT;
3319
3320 /* ignore return value, binding should not fail */
3321 do_bind_con_driver(defcsw, first, last, deflt);
3322err:
3323 module_put(owner);
3324 return retval;
3325
3326}
3327EXPORT_SYMBOL_GPL(do_unbind_con_driver);
3328
3329static int vt_bind(struct con_driver *con)
3330{
3331 const struct consw *defcsw = NULL, *csw = NULL;
3332 int i, more = 1, first = -1, last = -1, deflt = 0;
3333
3334 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3335 goto err;
3336
3337 csw = con->con;
3338
3339 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3340 struct con_driver *con = &registered_con_driver[i];
3341
3342 if (con->con && !(con->flag & CON_DRIVER_FLAG_MODULE)) {
3343 defcsw = con->con;
3344 break;
3345 }
3346 }
3347
3348 if (!defcsw)
3349 goto err;
3350
3351 while (more) {
3352 more = 0;
3353
3354 for (i = con->first; i <= con->last; i++) {
3355 if (con_driver_map[i] == defcsw) {
3356 if (first == -1)
3357 first = i;
3358 last = i;
3359 more = 1;
3360 } else if (first != -1)
3361 break;
3362 }
3363
3364 if (first == 0 && last == MAX_NR_CONSOLES -1)
3365 deflt = 1;
3366
3367 if (first != -1)
3368 do_bind_con_driver(csw, first, last, deflt);
3369
3370 first = -1;
3371 last = -1;
3372 deflt = 0;
3373 }
3374
3375err:
3376 return 0;
3377}
3378
3379static int vt_unbind(struct con_driver *con)
3380{
3381 const struct consw *csw = NULL;
3382 int i, more = 1, first = -1, last = -1, deflt = 0;
3383 int ret;
3384
3385 if (!con->con || !(con->flag & CON_DRIVER_FLAG_MODULE))
3386 goto err;
3387
3388 csw = con->con;
3389
3390 while (more) {
3391 more = 0;
3392
3393 for (i = con->first; i <= con->last; i++) {
3394 if (con_driver_map[i] == csw) {
3395 if (first == -1)
3396 first = i;
3397 last = i;
3398 more = 1;
3399 } else if (first != -1)
3400 break;
3401 }
3402
3403 if (first == 0 && last == MAX_NR_CONSOLES -1)
3404 deflt = 1;
3405
3406 if (first != -1) {
3407 ret = do_unbind_con_driver(csw, first, last, deflt);
3408 if (ret != 0)
3409 return ret;
3410 }
3411
3412 first = -1;
3413 last = -1;
3414 deflt = 0;
3415 }
3416
3417err:
3418 return 0;
3419}
3420#else
3421static inline int vt_bind(struct con_driver *con)
3422{
3423 return 0;
3424}
3425static inline int vt_unbind(struct con_driver *con)
3426{
3427 return 0;
3428}
3429#endif /* CONFIG_VT_HW_CONSOLE_BINDING */
3430
3431static ssize_t store_bind(struct device *dev, struct device_attribute *attr,
3432 const char *buf, size_t count)
3433{
3434 struct con_driver *con = dev_get_drvdata(dev);
3435 int bind = simple_strtoul(buf, NULL, 0);
3436
3437 console_lock();
3438
3439 if (bind)
3440 vt_bind(con);
3441 else
3442 vt_unbind(con);
3443
3444 console_unlock();
3445
3446 return count;
3447}
3448
3449static ssize_t show_bind(struct device *dev, struct device_attribute *attr,
3450 char *buf)
3451{
3452 struct con_driver *con = dev_get_drvdata(dev);
3453 int bind = con_is_bound(con->con);
3454
3455 return snprintf(buf, PAGE_SIZE, "%i\n", bind);
3456}
3457
3458static ssize_t show_name(struct device *dev, struct device_attribute *attr,
3459 char *buf)
3460{
3461 struct con_driver *con = dev_get_drvdata(dev);
3462
3463 return snprintf(buf, PAGE_SIZE, "%s %s\n",
3464 (con->flag & CON_DRIVER_FLAG_MODULE) ? "(M)" : "(S)",
3465 con->desc);
3466
3467}
3468
3469static DEVICE_ATTR(bind, S_IRUGO|S_IWUSR, show_bind, store_bind);
3470static DEVICE_ATTR(name, S_IRUGO, show_name, NULL);
3471
3472static struct attribute *con_dev_attrs[] = {
3473 &dev_attr_bind.attr,
3474 &dev_attr_name.attr,
3475 NULL
3476};
3477
3478ATTRIBUTE_GROUPS(con_dev);
3479
3480static int vtconsole_init_device(struct con_driver *con)
3481{
3482 con->flag |= CON_DRIVER_FLAG_ATTR;
3483 return 0;
3484}
3485
3486static void vtconsole_deinit_device(struct con_driver *con)
3487{
3488 con->flag &= ~CON_DRIVER_FLAG_ATTR;
3489}
3490
3491/**
3492 * con_is_bound - checks if driver is bound to the console
3493 * @csw: console driver
3494 *
3495 * RETURNS: zero if unbound, nonzero if bound
3496 *
3497 * Drivers can call this and if zero, they should release
3498 * all resources allocated on con_startup()
3499 */
3500int con_is_bound(const struct consw *csw)
3501{
3502 int i, bound = 0;
3503
3504 for (i = 0; i < MAX_NR_CONSOLES; i++) {
3505 if (con_driver_map[i] == csw) {
3506 bound = 1;
3507 break;
3508 }
3509 }
3510
3511 return bound;
3512}
3513EXPORT_SYMBOL(con_is_bound);
3514
3515/**
3516 * con_debug_enter - prepare the console for the kernel debugger
3517 * @sw: console driver
3518 *
3519 * Called when the console is taken over by the kernel debugger, this
3520 * function needs to save the current console state, then put the console
3521 * into a state suitable for the kernel debugger.
3522 *
3523 * RETURNS:
3524 * Zero on success, nonzero if a failure occurred when trying to prepare
3525 * the console for the debugger.
3526 */
3527int con_debug_enter(struct vc_data *vc)
3528{
3529 int ret = 0;
3530
3531 saved_fg_console = fg_console;
3532 saved_last_console = last_console;
3533 saved_want_console = want_console;
3534 saved_vc_mode = vc->vc_mode;
3535 saved_console_blanked = console_blanked;
3536 vc->vc_mode = KD_TEXT;
3537 console_blanked = 0;
3538 if (vc->vc_sw->con_debug_enter)
3539 ret = vc->vc_sw->con_debug_enter(vc);
3540#ifdef CONFIG_KGDB_KDB
3541 /* Set the initial LINES variable if it is not already set */
3542 if (vc->vc_rows < 999) {
3543 int linecount;
3544 char lns[4];
3545 const char *setargs[3] = {
3546 "set",
3547 "LINES",
3548 lns,
3549 };
3550 if (kdbgetintenv(setargs[0], &linecount)) {
3551 snprintf(lns, 4, "%i", vc->vc_rows);
3552 kdb_set(2, setargs);
3553 }
3554 }
3555 if (vc->vc_cols < 999) {
3556 int colcount;
3557 char cols[4];
3558 const char *setargs[3] = {
3559 "set",
3560 "COLUMNS",
3561 cols,
3562 };
3563 if (kdbgetintenv(setargs[0], &colcount)) {
3564 snprintf(cols, 4, "%i", vc->vc_cols);
3565 kdb_set(2, setargs);
3566 }
3567 }
3568#endif /* CONFIG_KGDB_KDB */
3569 return ret;
3570}
3571EXPORT_SYMBOL_GPL(con_debug_enter);
3572
3573/**
3574 * con_debug_leave - restore console state
3575 * @sw: console driver
3576 *
3577 * Restore the console state to what it was before the kernel debugger
3578 * was invoked.
3579 *
3580 * RETURNS:
3581 * Zero on success, nonzero if a failure occurred when trying to restore
3582 * the console.
3583 */
3584int con_debug_leave(void)
3585{
3586 struct vc_data *vc;
3587 int ret = 0;
3588
3589 fg_console = saved_fg_console;
3590 last_console = saved_last_console;
3591 want_console = saved_want_console;
3592 console_blanked = saved_console_blanked;
3593 vc_cons[fg_console].d->vc_mode = saved_vc_mode;
3594
3595 vc = vc_cons[fg_console].d;
3596 if (vc->vc_sw->con_debug_leave)
3597 ret = vc->vc_sw->con_debug_leave(vc);
3598 return ret;
3599}
3600EXPORT_SYMBOL_GPL(con_debug_leave);
3601
3602static int do_register_con_driver(const struct consw *csw, int first, int last)
3603{
3604 struct module *owner = csw->owner;
3605 struct con_driver *con_driver;
3606 const char *desc;
3607 int i, retval;
3608
3609 WARN_CONSOLE_UNLOCKED();
3610
3611 if (!try_module_get(owner))
3612 return -ENODEV;
3613
3614 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3615 con_driver = &registered_con_driver[i];
3616
3617 /* already registered */
3618 if (con_driver->con == csw) {
3619 retval = -EBUSY;
3620 goto err;
3621 }
3622 }
3623
3624 desc = csw->con_startup();
3625 if (!desc) {
3626 retval = -ENODEV;
3627 goto err;
3628 }
3629
3630 retval = -EINVAL;
3631
3632 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3633 con_driver = &registered_con_driver[i];
3634
3635 if (con_driver->con == NULL &&
3636 !(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE)) {
3637 con_driver->con = csw;
3638 con_driver->desc = desc;
3639 con_driver->node = i;
3640 con_driver->flag = CON_DRIVER_FLAG_MODULE |
3641 CON_DRIVER_FLAG_INIT;
3642 con_driver->first = first;
3643 con_driver->last = last;
3644 retval = 0;
3645 break;
3646 }
3647 }
3648
3649 if (retval)
3650 goto err;
3651
3652 con_driver->dev =
3653 device_create_with_groups(vtconsole_class, NULL,
3654 MKDEV(0, con_driver->node),
3655 con_driver, con_dev_groups,
3656 "vtcon%i", con_driver->node);
3657 if (IS_ERR(con_driver->dev)) {
3658 printk(KERN_WARNING "Unable to create device for %s; "
3659 "errno = %ld\n", con_driver->desc,
3660 PTR_ERR(con_driver->dev));
3661 con_driver->dev = NULL;
3662 } else {
3663 vtconsole_init_device(con_driver);
3664 }
3665
3666err:
3667 module_put(owner);
3668 return retval;
3669}
3670
3671
3672/**
3673 * do_unregister_con_driver - unregister console driver from console layer
3674 * @csw: console driver
3675 *
3676 * DESCRIPTION: All drivers that registers to the console layer must
3677 * call this function upon exit, or if the console driver is in a state
3678 * where it won't be able to handle console services, such as the
3679 * framebuffer console without loaded framebuffer drivers.
3680 *
3681 * The driver must unbind first prior to unregistration.
3682 */
3683int do_unregister_con_driver(const struct consw *csw)
3684{
3685 int i;
3686
3687 /* cannot unregister a bound driver */
3688 if (con_is_bound(csw))
3689 return -EBUSY;
3690
3691 if (csw == conswitchp)
3692 return -EINVAL;
3693
3694 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3695 struct con_driver *con_driver = &registered_con_driver[i];
3696
3697 if (con_driver->con == csw) {
3698 /*
3699 * Defer the removal of the sysfs entries since that
3700 * will acquire the kernfs s_active lock and we can't
3701 * acquire this lock while holding the console lock:
3702 * the unbind sysfs entry imposes already the opposite
3703 * order. Reset con already here to prevent any later
3704 * lookup to succeed and mark this slot as zombie, so
3705 * it won't get reused until we complete the removal
3706 * in the deferred work.
3707 */
3708 con_driver->con = NULL;
3709 con_driver->flag = CON_DRIVER_FLAG_ZOMBIE;
3710 schedule_work(&con_driver_unregister_work);
3711
3712 return 0;
3713 }
3714 }
3715
3716 return -ENODEV;
3717}
3718EXPORT_SYMBOL_GPL(do_unregister_con_driver);
3719
3720static void con_driver_unregister_callback(struct work_struct *ignored)
3721{
3722 int i;
3723
3724 console_lock();
3725
3726 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3727 struct con_driver *con_driver = &registered_con_driver[i];
3728
3729 if (!(con_driver->flag & CON_DRIVER_FLAG_ZOMBIE))
3730 continue;
3731
3732 console_unlock();
3733
3734 vtconsole_deinit_device(con_driver);
3735 device_destroy(vtconsole_class, MKDEV(0, con_driver->node));
3736
3737 console_lock();
3738
3739 if (WARN_ON_ONCE(con_driver->con))
3740 con_driver->con = NULL;
3741 con_driver->desc = NULL;
3742 con_driver->dev = NULL;
3743 con_driver->node = 0;
3744 WARN_ON_ONCE(con_driver->flag != CON_DRIVER_FLAG_ZOMBIE);
3745 con_driver->flag = 0;
3746 con_driver->first = 0;
3747 con_driver->last = 0;
3748 }
3749
3750 console_unlock();
3751}
3752
3753/*
3754 * If we support more console drivers, this function is used
3755 * when a driver wants to take over some existing consoles
3756 * and become default driver for newly opened ones.
3757 *
3758 * do_take_over_console is basically a register followed by unbind
3759 */
3760int do_take_over_console(const struct consw *csw, int first, int last, int deflt)
3761{
3762 int err;
3763
3764 err = do_register_con_driver(csw, first, last);
3765 /*
3766 * If we get an busy error we still want to bind the console driver
3767 * and return success, as we may have unbound the console driver
3768 * but not unregistered it.
3769 */
3770 if (err == -EBUSY)
3771 err = 0;
3772 if (!err)
3773 do_bind_con_driver(csw, first, last, deflt);
3774
3775 return err;
3776}
3777EXPORT_SYMBOL_GPL(do_take_over_console);
3778
3779
3780/*
3781 * give_up_console is a wrapper to unregister_con_driver. It will only
3782 * work if driver is fully unbound.
3783 */
3784void give_up_console(const struct consw *csw)
3785{
3786 console_lock();
3787 do_unregister_con_driver(csw);
3788 console_unlock();
3789}
3790
3791static int __init vtconsole_class_init(void)
3792{
3793 int i;
3794
3795 vtconsole_class = class_create(THIS_MODULE, "vtconsole");
3796 if (IS_ERR(vtconsole_class)) {
3797 printk(KERN_WARNING "Unable to create vt console class; "
3798 "errno = %ld\n", PTR_ERR(vtconsole_class));
3799 vtconsole_class = NULL;
3800 }
3801
3802 /* Add system drivers to sysfs */
3803 for (i = 0; i < MAX_NR_CON_DRIVER; i++) {
3804 struct con_driver *con = &registered_con_driver[i];
3805
3806 if (con->con && !con->dev) {
3807 con->dev =
3808 device_create_with_groups(vtconsole_class, NULL,
3809 MKDEV(0, con->node),
3810 con, con_dev_groups,
3811 "vtcon%i", con->node);
3812
3813 if (IS_ERR(con->dev)) {
3814 printk(KERN_WARNING "Unable to create "
3815 "device for %s; errno = %ld\n",
3816 con->desc, PTR_ERR(con->dev));
3817 con->dev = NULL;
3818 } else {
3819 vtconsole_init_device(con);
3820 }
3821 }
3822 }
3823
3824 return 0;
3825}
3826postcore_initcall(vtconsole_class_init);
3827
3828#endif
3829
3830/*
3831 * Screen blanking
3832 */
3833
3834static int set_vesa_blanking(char __user *p)
3835{
3836 unsigned int mode;
3837
3838 if (get_user(mode, p + 1))
3839 return -EFAULT;
3840
3841 vesa_blank_mode = (mode < 4) ? mode : 0;
3842 return 0;
3843}
3844
3845void do_blank_screen(int entering_gfx)
3846{
3847 struct vc_data *vc = vc_cons[fg_console].d;
3848 int i;
3849
3850 WARN_CONSOLE_UNLOCKED();
3851
3852 if (console_blanked) {
3853 if (blank_state == blank_vesa_wait) {
3854 blank_state = blank_off;
3855 vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
3856 }
3857 return;
3858 }
3859
3860 /* entering graphics mode? */
3861 if (entering_gfx) {
3862 hide_cursor(vc);
3863 save_screen(vc);
3864 vc->vc_sw->con_blank(vc, -1, 1);
3865 console_blanked = fg_console + 1;
3866 blank_state = blank_off;
3867 set_origin(vc);
3868 return;
3869 }
3870
3871 blank_state = blank_off;
3872
3873 /* don't blank graphics */
3874 if (vc->vc_mode != KD_TEXT) {
3875 console_blanked = fg_console + 1;
3876 return;
3877 }
3878
3879 hide_cursor(vc);
3880 del_timer_sync(&console_timer);
3881 blank_timer_expired = 0;
3882
3883 save_screen(vc);
3884 /* In case we need to reset origin, blanking hook returns 1 */
3885 i = vc->vc_sw->con_blank(vc, vesa_off_interval ? 1 : (vesa_blank_mode + 1), 0);
3886 console_blanked = fg_console + 1;
3887 if (i)
3888 set_origin(vc);
3889
3890 if (console_blank_hook && console_blank_hook(1))
3891 return;
3892
3893 if (vesa_off_interval && vesa_blank_mode) {
3894 blank_state = blank_vesa_wait;
3895 mod_timer(&console_timer, jiffies + vesa_off_interval);
3896 }
3897 vt_event_post(VT_EVENT_BLANK, vc->vc_num, vc->vc_num);
3898}
3899EXPORT_SYMBOL(do_blank_screen);
3900
3901/*
3902 * Called by timer as well as from vt_console_driver
3903 */
3904void do_unblank_screen(int leaving_gfx)
3905{
3906 struct vc_data *vc;
3907
3908 /* This should now always be called from a "sane" (read: can schedule)
3909 * context for the sake of the low level drivers, except in the special
3910 * case of oops_in_progress
3911 */
3912 if (!oops_in_progress)
3913 might_sleep();
3914
3915 WARN_CONSOLE_UNLOCKED();
3916
3917 ignore_poke = 0;
3918 if (!console_blanked)
3919 return;
3920 if (!vc_cons_allocated(fg_console)) {
3921 /* impossible */
3922 pr_warn("unblank_screen: tty %d not allocated ??\n",
3923 fg_console + 1);
3924 return;
3925 }
3926 vc = vc_cons[fg_console].d;
3927 /* Try to unblank in oops case too */
3928 if (vc->vc_mode != KD_TEXT && !vt_force_oops_output(vc))
3929 return; /* but leave console_blanked != 0 */
3930
3931 if (blankinterval) {
3932 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3933 blank_state = blank_normal_wait;
3934 }
3935
3936 console_blanked = 0;
3937 if (vc->vc_sw->con_blank(vc, 0, leaving_gfx) || vt_force_oops_output(vc))
3938 /* Low-level driver cannot restore -> do it ourselves */
3939 update_screen(vc);
3940 if (console_blank_hook)
3941 console_blank_hook(0);
3942 set_palette(vc);
3943 set_cursor(vc);
3944 vt_event_post(VT_EVENT_UNBLANK, vc->vc_num, vc->vc_num);
3945}
3946EXPORT_SYMBOL(do_unblank_screen);
3947
3948/*
3949 * This is called by the outside world to cause a forced unblank, mostly for
3950 * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
3951 * call it with 1 as an argument and so force a mode restore... that may kill
3952 * X or at least garbage the screen but would also make the Oops visible...
3953 */
3954void unblank_screen(void)
3955{
3956 do_unblank_screen(0);
3957}
3958
3959/*
3960 * We defer the timer blanking to work queue so it can take the console mutex
3961 * (console operations can still happen at irq time, but only from printk which
3962 * has the console mutex. Not perfect yet, but better than no locking
3963 */
3964static void blank_screen_t(unsigned long dummy)
3965{
3966 blank_timer_expired = 1;
3967 schedule_work(&console_work);
3968}
3969
3970void poke_blanked_console(void)
3971{
3972 WARN_CONSOLE_UNLOCKED();
3973
3974 /* Add this so we quickly catch whoever might call us in a non
3975 * safe context. Nowadays, unblank_screen() isn't to be called in
3976 * atomic contexts and is allowed to schedule (with the special case
3977 * of oops_in_progress, but that isn't of any concern for this
3978 * function. --BenH.
3979 */
3980 might_sleep();
3981
3982 /* This isn't perfectly race free, but a race here would be mostly harmless,
3983 * at worse, we'll do a spurrious blank and it's unlikely
3984 */
3985 del_timer(&console_timer);
3986 blank_timer_expired = 0;
3987
3988 if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
3989 return;
3990 if (console_blanked)
3991 unblank_screen();
3992 else if (blankinterval) {
3993 mod_timer(&console_timer, jiffies + (blankinterval * HZ));
3994 blank_state = blank_normal_wait;
3995 }
3996}
3997
3998/*
3999 * Palettes
4000 */
4001
4002static void set_palette(struct vc_data *vc)
4003{
4004 WARN_CONSOLE_UNLOCKED();
4005
4006 if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_set_palette)
4007 vc->vc_sw->con_set_palette(vc, color_table);
4008}
4009
4010/*
4011 * Load palette into the DAC registers. arg points to a colour
4012 * map, 3 bytes per colour, 16 colours, range from 0 to 255.
4013 */
4014
4015int con_set_cmap(unsigned char __user *arg)
4016{
4017 int i, j, k;
4018 unsigned char colormap[3*16];
4019
4020 if (copy_from_user(colormap, arg, sizeof(colormap)))
4021 return -EFAULT;
4022
4023 console_lock();
4024 for (i = k = 0; i < 16; i++) {
4025 default_red[i] = colormap[k++];
4026 default_grn[i] = colormap[k++];
4027 default_blu[i] = colormap[k++];
4028 }
4029 for (i = 0; i < MAX_NR_CONSOLES; i++) {
4030 if (!vc_cons_allocated(i))
4031 continue;
4032 for (j = k = 0; j < 16; j++) {
4033 vc_cons[i].d->vc_palette[k++] = default_red[j];
4034 vc_cons[i].d->vc_palette[k++] = default_grn[j];
4035 vc_cons[i].d->vc_palette[k++] = default_blu[j];
4036 }
4037 set_palette(vc_cons[i].d);
4038 }
4039 console_unlock();
4040
4041 return 0;
4042}
4043
4044int con_get_cmap(unsigned char __user *arg)
4045{
4046 int i, k;
4047 unsigned char colormap[3*16];
4048
4049 console_lock();
4050 for (i = k = 0; i < 16; i++) {
4051 colormap[k++] = default_red[i];
4052 colormap[k++] = default_grn[i];
4053 colormap[k++] = default_blu[i];
4054 }
4055 console_unlock();
4056
4057 if (copy_to_user(arg, colormap, sizeof(colormap)))
4058 return -EFAULT;
4059
4060 return 0;
4061}
4062
4063void reset_palette(struct vc_data *vc)
4064{
4065 int j, k;
4066 for (j=k=0; j<16; j++) {
4067 vc->vc_palette[k++] = default_red[j];
4068 vc->vc_palette[k++] = default_grn[j];
4069 vc->vc_palette[k++] = default_blu[j];
4070 }
4071 set_palette(vc);
4072}
4073
4074/*
4075 * Font switching
4076 *
4077 * Currently we only support fonts up to 32 pixels wide, at a maximum height
4078 * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
4079 * depending on width) reserved for each character which is kinda wasty, but
4080 * this is done in order to maintain compatibility with the EGA/VGA fonts. It
4081 * is up to the actual low-level console-driver convert data into its favorite
4082 * format (maybe we should add a `fontoffset' field to the `display'
4083 * structure so we won't have to convert the fontdata all the time.
4084 * /Jes
4085 */
4086
4087#define max_font_size 65536
4088
4089static int con_font_get(struct vc_data *vc, struct console_font_op *op)
4090{
4091 struct console_font font;
4092 int rc = -EINVAL;
4093 int c;
4094
4095 if (op->data) {
4096 font.data = kmalloc(max_font_size, GFP_KERNEL);
4097 if (!font.data)
4098 return -ENOMEM;
4099 } else
4100 font.data = NULL;
4101
4102 console_lock();
4103 if (vc->vc_mode != KD_TEXT)
4104 rc = -EINVAL;
4105 else if (vc->vc_sw->con_font_get)
4106 rc = vc->vc_sw->con_font_get(vc, &font);
4107 else
4108 rc = -ENOSYS;
4109 console_unlock();
4110
4111 if (rc)
4112 goto out;
4113
4114 c = (font.width+7)/8 * 32 * font.charcount;
4115
4116 if (op->data && font.charcount > op->charcount)
4117 rc = -ENOSPC;
4118 if (!(op->flags & KD_FONT_FLAG_OLD)) {
4119 if (font.width > op->width || font.height > op->height)
4120 rc = -ENOSPC;
4121 } else {
4122 if (font.width != 8)
4123 rc = -EIO;
4124 else if ((op->height && font.height > op->height) ||
4125 font.height > 32)
4126 rc = -ENOSPC;
4127 }
4128 if (rc)
4129 goto out;
4130
4131 op->height = font.height;
4132 op->width = font.width;
4133 op->charcount = font.charcount;
4134
4135 if (op->data && copy_to_user(op->data, font.data, c))
4136 rc = -EFAULT;
4137
4138out:
4139 kfree(font.data);
4140 return rc;
4141}
4142
4143static int con_font_set(struct vc_data *vc, struct console_font_op *op)
4144{
4145 struct console_font font;
4146 int rc = -EINVAL;
4147 int size;
4148
4149 if (vc->vc_mode != KD_TEXT)
4150 return -EINVAL;
4151 if (!op->data)
4152 return -EINVAL;
4153 if (op->charcount > 512)
4154 return -EINVAL;
4155 if (!op->height) { /* Need to guess font height [compat] */
4156 int h, i;
4157 u8 __user *charmap = op->data;
4158 u8 tmp;
4159
4160 /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
4161 so that we can get rid of this soon */
4162 if (!(op->flags & KD_FONT_FLAG_OLD))
4163 return -EINVAL;
4164 for (h = 32; h > 0; h--)
4165 for (i = 0; i < op->charcount; i++) {
4166 if (get_user(tmp, &charmap[32*i+h-1]))
4167 return -EFAULT;
4168 if (tmp)
4169 goto nonzero;
4170 }
4171 return -EINVAL;
4172 nonzero:
4173 op->height = h;
4174 }
4175 if (op->width <= 0 || op->width > 32 || op->height > 32)
4176 return -EINVAL;
4177 size = (op->width+7)/8 * 32 * op->charcount;
4178 if (size > max_font_size)
4179 return -ENOSPC;
4180 font.charcount = op->charcount;
4181 font.height = op->height;
4182 font.width = op->width;
4183 font.data = memdup_user(op->data, size);
4184 if (IS_ERR(font.data))
4185 return PTR_ERR(font.data);
4186 console_lock();
4187 if (vc->vc_mode != KD_TEXT)
4188 rc = -EINVAL;
4189 else if (vc->vc_sw->con_font_set)
4190 rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
4191 else
4192 rc = -ENOSYS;
4193 console_unlock();
4194 kfree(font.data);
4195 return rc;
4196}
4197
4198static int con_font_default(struct vc_data *vc, struct console_font_op *op)
4199{
4200 struct console_font font = {.width = op->width, .height = op->height};
4201 char name[MAX_FONT_NAME];
4202 char *s = name;
4203 int rc;
4204
4205
4206 if (!op->data)
4207 s = NULL;
4208 else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
4209 return -EFAULT;
4210 else
4211 name[MAX_FONT_NAME - 1] = 0;
4212
4213 console_lock();
4214 if (vc->vc_mode != KD_TEXT) {
4215 console_unlock();
4216 return -EINVAL;
4217 }
4218 if (vc->vc_sw->con_font_default)
4219 rc = vc->vc_sw->con_font_default(vc, &font, s);
4220 else
4221 rc = -ENOSYS;
4222 console_unlock();
4223 if (!rc) {
4224 op->width = font.width;
4225 op->height = font.height;
4226 }
4227 return rc;
4228}
4229
4230static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
4231{
4232 int con = op->height;
4233 int rc;
4234
4235
4236 console_lock();
4237 if (vc->vc_mode != KD_TEXT)
4238 rc = -EINVAL;
4239 else if (!vc->vc_sw->con_font_copy)
4240 rc = -ENOSYS;
4241 else if (con < 0 || !vc_cons_allocated(con))
4242 rc = -ENOTTY;
4243 else if (con == vc->vc_num) /* nothing to do */
4244 rc = 0;
4245 else
4246 rc = vc->vc_sw->con_font_copy(vc, con);
4247 console_unlock();
4248 return rc;
4249}
4250
4251int con_font_op(struct vc_data *vc, struct console_font_op *op)
4252{
4253 switch (op->op) {
4254 case KD_FONT_OP_SET:
4255 return con_font_set(vc, op);
4256 case KD_FONT_OP_GET:
4257 return con_font_get(vc, op);
4258 case KD_FONT_OP_SET_DEFAULT:
4259 return con_font_default(vc, op);
4260 case KD_FONT_OP_COPY:
4261 return con_font_copy(vc, op);
4262 }
4263 return -ENOSYS;
4264}
4265
4266/*
4267 * Interface exported to selection and vcs.
4268 */
4269
4270/* used by selection */
4271u16 screen_glyph(struct vc_data *vc, int offset)
4272{
4273 u16 w = scr_readw(screenpos(vc, offset, 1));
4274 u16 c = w & 0xff;
4275
4276 if (w & vc->vc_hi_font_mask)
4277 c |= 0x100;
4278 return c;
4279}
4280EXPORT_SYMBOL_GPL(screen_glyph);
4281
4282/* used by vcs - note the word offset */
4283unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
4284{
4285 return screenpos(vc, 2 * w_offset, viewed);
4286}
4287EXPORT_SYMBOL_GPL(screen_pos);
4288
4289void getconsxy(struct vc_data *vc, unsigned char *p)
4290{
4291 p[0] = vc->vc_x;
4292 p[1] = vc->vc_y;
4293}
4294
4295void putconsxy(struct vc_data *vc, unsigned char *p)
4296{
4297 hide_cursor(vc);
4298 gotoxy(vc, p[0], p[1]);
4299 set_cursor(vc);
4300}
4301
4302u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
4303{
4304 if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
4305 return softcursor_original;
4306 return scr_readw(org);
4307}
4308
4309void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
4310{
4311 scr_writew(val, org);
4312 if ((unsigned long)org == vc->vc_pos) {
4313 softcursor_original = -1;
4314 add_softcursor(vc);
4315 }
4316}
4317
4318void vcs_scr_updated(struct vc_data *vc)
4319{
4320 notify_update(vc);
4321}
4322
4323void vc_scrolldelta_helper(struct vc_data *c, int lines,
4324 unsigned int rolled_over, void *base, unsigned int size)
4325{
4326 unsigned long ubase = (unsigned long)base;
4327 ptrdiff_t scr_end = (void *)c->vc_scr_end - base;
4328 ptrdiff_t vorigin = (void *)c->vc_visible_origin - base;
4329 ptrdiff_t origin = (void *)c->vc_origin - base;
4330 int margin = c->vc_size_row * 4;
4331 int from, wrap, from_off, avail;
4332
4333 /* Turn scrollback off */
4334 if (!lines) {
4335 c->vc_visible_origin = c->vc_origin;
4336 return;
4337 }
4338
4339 /* Do we have already enough to allow jumping from 0 to the end? */
4340 if (rolled_over > scr_end + margin) {
4341 from = scr_end;
4342 wrap = rolled_over + c->vc_size_row;
4343 } else {
4344 from = 0;
4345 wrap = size;
4346 }
4347
4348 from_off = (vorigin - from + wrap) % wrap + lines * c->vc_size_row;
4349 avail = (origin - from + wrap) % wrap;
4350
4351 /* Only a little piece would be left? Show all incl. the piece! */
4352 if (avail < 2 * margin)
4353 margin = 0;
4354 if (from_off < margin)
4355 from_off = 0;
4356 if (from_off > avail - margin)
4357 from_off = avail;
4358
4359 c->vc_visible_origin = ubase + (from + from_off) % wrap;
4360}
4361EXPORT_SYMBOL_GPL(vc_scrolldelta_helper);
4362
4363/*
4364 * Visible symbols for modules
4365 */
4366
4367EXPORT_SYMBOL(color_table);
4368EXPORT_SYMBOL(default_red);
4369EXPORT_SYMBOL(default_grn);
4370EXPORT_SYMBOL(default_blu);
4371EXPORT_SYMBOL(update_region);
4372EXPORT_SYMBOL(redraw_screen);
4373EXPORT_SYMBOL(vc_resize);
4374EXPORT_SYMBOL(fg_console);
4375EXPORT_SYMBOL(console_blank_hook);
4376EXPORT_SYMBOL(console_blanked);
4377EXPORT_SYMBOL(vc_cons);
4378EXPORT_SYMBOL(global_cursor_default);
4379#ifndef VT_SINGLE_DRIVER
4380EXPORT_SYMBOL(give_up_console);
4381#endif