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