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