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