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