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