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