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