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