/drivers/char/vt.c

https://bitbucket.org/evzijst/gittest · C · 3242 lines · 2506 code · 325 blank · 411 comment · 564 complexity · ddebc660296223b98d318d177e610f5f MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. * linux/drivers/char/vt.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /*
  7. * Hopefully this will be a rather complete VT102 implementation.
  8. *
  9. * Beeping thanks to John T Kohl.
  10. *
  11. * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  12. * Chars, and VT100 enhancements by Peter MacDonald.
  13. *
  14. * Copy and paste function by Andrew Haylett,
  15. * some enhancements by Alessandro Rubini.
  16. *
  17. * Code to check for different video-cards mostly by Galen Hunt,
  18. * <g-hunt@ee.utah.edu>
  19. *
  20. * Rudimentary ISO 10646/Unicode/UTF-8 character set support by
  21. * Markus Kuhn, <mskuhn@immd4.informatik.uni-erlangen.de>.
  22. *
  23. * Dynamic allocation of consoles, aeb@cwi.nl, May 1994
  24. * Resizing of consoles, aeb, 940926
  25. *
  26. * Code for xterm like mouse click reporting by Peter Orbaek 20-Jul-94
  27. * <poe@daimi.aau.dk>
  28. *
  29. * User-defined bell sound, new setterm control sequences and printk
  30. * redirection by Martin Mares <mj@k332.feld.cvut.cz> 19-Nov-95
  31. *
  32. * APM screenblank bug fixed Takashi Manabe <manabe@roy.dsl.tutics.tut.jp>
  33. *
  34. * Merge with the abstract console driver by Geert Uytterhoeven
  35. * <geert@linux-m68k.org>, Jan 1997.
  36. *
  37. * Original m68k console driver modifications by
  38. *
  39. * - Arno Griffioen <arno@usn.nl>
  40. * - David Carter <carter@cs.bris.ac.uk>
  41. *
  42. * The abstract console driver provides a generic interface for a text
  43. * console. It supports VGA text mode, frame buffer based graphical consoles
  44. * and special graphics processors that are only accessible through some
  45. * registers (e.g. a TMS340x0 GSP).
  46. *
  47. * The interface to the hardware is specified using a special structure
  48. * (struct consw) which contains function pointers to console operations
  49. * (see <linux/console.h> for more information).
  50. *
  51. * Support for changeable cursor shape
  52. * by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>, August 1997
  53. *
  54. * Ported to i386 and con_scrolldelta fixed
  55. * by Emmanuel Marty <core@ggi-project.org>, April 1998
  56. *
  57. * Resurrected character buffers in videoram plus lots of other trickery
  58. * by Martin Mares <mj@atrey.karlin.mff.cuni.cz>, July 1998
  59. *
  60. * Removed old-style timers, introduced console_timer, made timer
  61. * deletion SMP-safe. 17Jun00, Andrew Morton <andrewm@uow.edu.au>
  62. *
  63. * Removed console_lock, enabled interrupts across all console operations
  64. * 13 March 2001, Andrew Morton
  65. */
  66. #include <linux/module.h>
  67. #include <linux/types.h>
  68. #include <linux/sched.h>
  69. #include <linux/tty.h>
  70. #include <linux/tty_flip.h>
  71. #include <linux/kernel.h>
  72. #include <linux/string.h>
  73. #include <linux/errno.h>
  74. #include <linux/kd.h>
  75. #include <linux/slab.h>
  76. #include <linux/major.h>
  77. #include <linux/mm.h>
  78. #include <linux/console.h>
  79. #include <linux/init.h>
  80. #include <linux/devfs_fs_kernel.h>
  81. #include <linux/vt_kern.h>
  82. #include <linux/selection.h>
  83. #include <linux/tiocl.h>
  84. #include <linux/kbd_kern.h>
  85. #include <linux/consolemap.h>
  86. #include <linux/timer.h>
  87. #include <linux/interrupt.h>
  88. #include <linux/config.h>
  89. #include <linux/workqueue.h>
  90. #include <linux/bootmem.h>
  91. #include <linux/pm.h>
  92. #include <linux/font.h>
  93. #include <linux/bitops.h>
  94. #include <asm/io.h>
  95. #include <asm/system.h>
  96. #include <asm/uaccess.h>
  97. const struct consw *conswitchp;
  98. /* A bitmap for codes <32. A bit of 1 indicates that the code
  99. * corresponding to that bit number invokes some special action
  100. * (such as cursor movement) and should not be displayed as a
  101. * glyph unless the disp_ctrl mode is explicitly enabled.
  102. */
  103. #define CTRL_ACTION 0x0d00ff81
  104. #define CTRL_ALWAYS 0x0800f501 /* Cannot be overridden by disp_ctrl */
  105. /*
  106. * Here is the default bell parameters: 750HZ, 1/8th of a second
  107. */
  108. #define DEFAULT_BELL_PITCH 750
  109. #define DEFAULT_BELL_DURATION (HZ/8)
  110. extern void vcs_make_devfs(struct tty_struct *tty);
  111. extern void vcs_remove_devfs(struct tty_struct *tty);
  112. extern void console_map_init(void);
  113. #ifdef CONFIG_PROM_CONSOLE
  114. extern void prom_con_init(void);
  115. #endif
  116. #ifdef CONFIG_MDA_CONSOLE
  117. extern int mda_console_init(void);
  118. #endif
  119. struct vc vc_cons [MAX_NR_CONSOLES];
  120. #ifndef VT_SINGLE_DRIVER
  121. static const struct consw *con_driver_map[MAX_NR_CONSOLES];
  122. #endif
  123. static int con_open(struct tty_struct *, struct file *);
  124. static void vc_init(struct vc_data *vc, unsigned int rows,
  125. unsigned int cols, int do_clear);
  126. static void gotoxy(struct vc_data *vc, int new_x, int new_y);
  127. static void save_cur(struct vc_data *vc);
  128. static void reset_terminal(struct vc_data *vc, int do_clear);
  129. static void con_flush_chars(struct tty_struct *tty);
  130. static void set_vesa_blanking(char __user *p);
  131. static void set_cursor(struct vc_data *vc);
  132. static void hide_cursor(struct vc_data *vc);
  133. static void console_callback(void *ignored);
  134. static void blank_screen_t(unsigned long dummy);
  135. static void set_palette(struct vc_data *vc);
  136. static int printable; /* Is console ready for printing? */
  137. /*
  138. * ignore_poke: don't unblank the screen when things are typed. This is
  139. * mainly for the privacy of braille terminal users.
  140. */
  141. static int ignore_poke;
  142. int do_poke_blanked_console;
  143. int console_blanked;
  144. static int vesa_blank_mode; /* 0:none 1:suspendV 2:suspendH 3:powerdown */
  145. static int blankinterval = 10*60*HZ;
  146. static int vesa_off_interval;
  147. static DECLARE_WORK(console_work, console_callback, NULL);
  148. /*
  149. * fg_console is the current virtual console,
  150. * last_console is the last used one,
  151. * want_console is the console we want to switch to,
  152. * kmsg_redirect is the console for kernel messages,
  153. */
  154. int fg_console;
  155. int last_console;
  156. int want_console = -1;
  157. int kmsg_redirect;
  158. /*
  159. * For each existing display, we have a pointer to console currently visible
  160. * on that display, allowing consoles other than fg_console to be refreshed
  161. * appropriately. Unless the low-level driver supplies its own display_fg
  162. * variable, we use this one for the "master display".
  163. */
  164. static struct vc_data *master_display_fg;
  165. /*
  166. * Unfortunately, we need to delay tty echo when we're currently writing to the
  167. * console since the code is (and always was) not re-entrant, so we schedule
  168. * all flip requests to process context with schedule-task() and run it from
  169. * console_callback().
  170. */
  171. /*
  172. * For the same reason, we defer scrollback to the console callback.
  173. */
  174. static int scrollback_delta;
  175. /*
  176. * Hook so that the power management routines can (un)blank
  177. * the console on our behalf.
  178. */
  179. int (*console_blank_hook)(int);
  180. static struct timer_list console_timer;
  181. static int blank_state;
  182. static int blank_timer_expired;
  183. enum {
  184. blank_off = 0,
  185. blank_normal_wait,
  186. blank_vesa_wait,
  187. };
  188. /*
  189. * Low-Level Functions
  190. */
  191. #define IS_FG(vc) ((vc)->vc_num == fg_console)
  192. #ifdef VT_BUF_VRAM_ONLY
  193. #define DO_UPDATE(vc) 0
  194. #else
  195. #define DO_UPDATE(vc) CON_IS_VISIBLE(vc)
  196. #endif
  197. static inline unsigned short *screenpos(struct vc_data *vc, int offset, int viewed)
  198. {
  199. unsigned short *p;
  200. if (!viewed)
  201. p = (unsigned short *)(vc->vc_origin + offset);
  202. else if (!vc->vc_sw->con_screen_pos)
  203. p = (unsigned short *)(vc->vc_visible_origin + offset);
  204. else
  205. p = vc->vc_sw->con_screen_pos(vc, offset);
  206. return p;
  207. }
  208. static inline void scrolldelta(int lines)
  209. {
  210. scrollback_delta += lines;
  211. schedule_console_callback();
  212. }
  213. void schedule_console_callback(void)
  214. {
  215. schedule_work(&console_work);
  216. }
  217. static void scrup(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  218. {
  219. unsigned short *d, *s;
  220. if (t+nr >= b)
  221. nr = b - t - 1;
  222. if (b > vc->vc_rows || t >= b || nr < 1)
  223. return;
  224. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_UP, nr))
  225. return;
  226. d = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  227. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * (t + nr));
  228. scr_memmovew(d, s, (b - t - nr) * vc->vc_size_row);
  229. scr_memsetw(d + (b - t - nr) * vc->vc_cols, vc->vc_video_erase_char,
  230. vc->vc_size_row * nr);
  231. }
  232. static void scrdown(struct vc_data *vc, unsigned int t, unsigned int b, int nr)
  233. {
  234. unsigned short *s;
  235. unsigned int step;
  236. if (t+nr >= b)
  237. nr = b - t - 1;
  238. if (b > vc->vc_rows || t >= b || nr < 1)
  239. return;
  240. if (CON_IS_VISIBLE(vc) && vc->vc_sw->con_scroll(vc, t, b, SM_DOWN, nr))
  241. return;
  242. s = (unsigned short *)(vc->vc_origin + vc->vc_size_row * t);
  243. step = vc->vc_cols * nr;
  244. scr_memmovew(s + step, s, (b - t - nr) * vc->vc_size_row);
  245. scr_memsetw(s, vc->vc_video_erase_char, 2 * step);
  246. }
  247. static void do_update_region(struct vc_data *vc, unsigned long start, int count)
  248. {
  249. #ifndef VT_BUF_VRAM_ONLY
  250. unsigned int xx, yy, offset;
  251. u16 *p;
  252. p = (u16 *) start;
  253. if (!vc->vc_sw->con_getxy) {
  254. offset = (start - vc->vc_origin) / 2;
  255. xx = offset % vc->vc_cols;
  256. yy = offset / vc->vc_cols;
  257. } else {
  258. int nxx, nyy;
  259. start = vc->vc_sw->con_getxy(vc, start, &nxx, &nyy);
  260. xx = nxx; yy = nyy;
  261. }
  262. for(;;) {
  263. u16 attrib = scr_readw(p) & 0xff00;
  264. int startx = xx;
  265. u16 *q = p;
  266. while (xx < vc->vc_cols && count) {
  267. if (attrib != (scr_readw(p) & 0xff00)) {
  268. if (p > q)
  269. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  270. startx = xx;
  271. q = p;
  272. attrib = scr_readw(p) & 0xff00;
  273. }
  274. p++;
  275. xx++;
  276. count--;
  277. }
  278. if (p > q)
  279. vc->vc_sw->con_putcs(vc, q, p-q, yy, startx);
  280. if (!count)
  281. break;
  282. xx = 0;
  283. yy++;
  284. if (vc->vc_sw->con_getxy) {
  285. p = (u16 *)start;
  286. start = vc->vc_sw->con_getxy(vc, start, NULL, NULL);
  287. }
  288. }
  289. #endif
  290. }
  291. void update_region(struct vc_data *vc, unsigned long start, int count)
  292. {
  293. WARN_CONSOLE_UNLOCKED();
  294. if (DO_UPDATE(vc)) {
  295. hide_cursor(vc);
  296. do_update_region(vc, start, count);
  297. set_cursor(vc);
  298. }
  299. }
  300. /* Structure of attributes is hardware-dependent */
  301. static u8 build_attr(struct vc_data *vc, u8 _color, u8 _intensity, u8 _blink, u8 _underline, u8 _reverse)
  302. {
  303. if (vc->vc_sw->con_build_attr)
  304. return vc->vc_sw->con_build_attr(vc, _color, _intensity, _blink, _underline, _reverse);
  305. #ifndef VT_BUF_VRAM_ONLY
  306. /*
  307. * ++roman: I completely changed the attribute format for monochrome
  308. * mode (!can_do_color). The formerly used MDA (monochrome display
  309. * adapter) format didn't allow the combination of certain effects.
  310. * Now the attribute is just a bit vector:
  311. * Bit 0..1: intensity (0..2)
  312. * Bit 2 : underline
  313. * Bit 3 : reverse
  314. * Bit 7 : blink
  315. */
  316. {
  317. u8 a = vc->vc_color;
  318. if (!vc->vc_can_do_color)
  319. return _intensity |
  320. (_underline ? 4 : 0) |
  321. (_reverse ? 8 : 0) |
  322. (_blink ? 0x80 : 0);
  323. if (_underline)
  324. a = (a & 0xf0) | vc->vc_ulcolor;
  325. else if (_intensity == 0)
  326. a = (a & 0xf0) | vc->vc_ulcolor;
  327. if (_reverse)
  328. a = ((a) & 0x88) | ((((a) >> 4) | ((a) << 4)) & 0x77);
  329. if (_blink)
  330. a ^= 0x80;
  331. if (_intensity == 2)
  332. a ^= 0x08;
  333. if (vc->vc_hi_font_mask == 0x100)
  334. a <<= 1;
  335. return a;
  336. }
  337. #else
  338. return 0;
  339. #endif
  340. }
  341. static void update_attr(struct vc_data *vc)
  342. {
  343. vc->vc_attr = build_attr(vc, vc->vc_color, vc->vc_intensity, vc->vc_blink, vc->vc_underline, vc->vc_reverse ^ vc->vc_decscnm);
  344. vc->vc_video_erase_char = (build_attr(vc, vc->vc_color, 1, vc->vc_blink, 0, vc->vc_decscnm) << 8) | ' ';
  345. }
  346. /* Note: inverting the screen twice should revert to the original state */
  347. void invert_screen(struct vc_data *vc, int offset, int count, int viewed)
  348. {
  349. unsigned short *p;
  350. WARN_CONSOLE_UNLOCKED();
  351. count /= 2;
  352. p = screenpos(vc, offset, viewed);
  353. if (vc->vc_sw->con_invert_region)
  354. vc->vc_sw->con_invert_region(vc, p, count);
  355. #ifndef VT_BUF_VRAM_ONLY
  356. else {
  357. u16 *q = p;
  358. int cnt = count;
  359. u16 a;
  360. if (!vc->vc_can_do_color) {
  361. while (cnt--) {
  362. a = scr_readw(q);
  363. a ^= 0x0800;
  364. scr_writew(a, q);
  365. q++;
  366. }
  367. } else if (vc->vc_hi_font_mask == 0x100) {
  368. while (cnt--) {
  369. a = scr_readw(q);
  370. a = ((a) & 0x11ff) | (((a) & 0xe000) >> 4) | (((a) & 0x0e00) << 4);
  371. scr_writew(a, q);
  372. q++;
  373. }
  374. } else {
  375. while (cnt--) {
  376. a = scr_readw(q);
  377. a = ((a) & 0x88ff) | (((a) & 0x7000) >> 4) | (((a) & 0x0700) << 4);
  378. scr_writew(a, q);
  379. q++;
  380. }
  381. }
  382. }
  383. #endif
  384. if (DO_UPDATE(vc))
  385. do_update_region(vc, (unsigned long) p, count);
  386. }
  387. /* used by selection: complement pointer position */
  388. void complement_pos(struct vc_data *vc, int offset)
  389. {
  390. static unsigned short *p;
  391. static unsigned short old;
  392. static unsigned short oldx, oldy;
  393. WARN_CONSOLE_UNLOCKED();
  394. if (p) {
  395. scr_writew(old, p);
  396. if (DO_UPDATE(vc))
  397. vc->vc_sw->con_putc(vc, old, oldy, oldx);
  398. }
  399. if (offset == -1)
  400. p = NULL;
  401. else {
  402. unsigned short new;
  403. p = screenpos(vc, offset, 1);
  404. old = scr_readw(p);
  405. new = old ^ vc->vc_complement_mask;
  406. scr_writew(new, p);
  407. if (DO_UPDATE(vc)) {
  408. oldx = (offset >> 1) % vc->vc_cols;
  409. oldy = (offset >> 1) / vc->vc_cols;
  410. vc->vc_sw->con_putc(vc, new, oldy, oldx);
  411. }
  412. }
  413. }
  414. static void insert_char(struct vc_data *vc, unsigned int nr)
  415. {
  416. unsigned short *p, *q = (unsigned short *)vc->vc_pos;
  417. p = q + vc->vc_cols - nr - vc->vc_x;
  418. while (--p >= q)
  419. scr_writew(scr_readw(p), p + nr);
  420. scr_memsetw(q, vc->vc_video_erase_char, nr * 2);
  421. vc->vc_need_wrap = 0;
  422. if (DO_UPDATE(vc)) {
  423. unsigned short oldattr = vc->vc_attr;
  424. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x, vc->vc_y, vc->vc_x + nr, 1,
  425. vc->vc_cols - vc->vc_x - nr);
  426. vc->vc_attr = vc->vc_video_erase_char >> 8;
  427. while (nr--)
  428. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y, vc->vc_x + nr);
  429. vc->vc_attr = oldattr;
  430. }
  431. }
  432. static void delete_char(struct vc_data *vc, unsigned int nr)
  433. {
  434. unsigned int i = vc->vc_x;
  435. unsigned short *p = (unsigned short *)vc->vc_pos;
  436. while (++i <= vc->vc_cols - nr) {
  437. scr_writew(scr_readw(p+nr), p);
  438. p++;
  439. }
  440. scr_memsetw(p, vc->vc_video_erase_char, nr * 2);
  441. vc->vc_need_wrap = 0;
  442. if (DO_UPDATE(vc)) {
  443. unsigned short oldattr = vc->vc_attr;
  444. vc->vc_sw->con_bmove(vc, vc->vc_y, vc->vc_x + nr, vc->vc_y, vc->vc_x, 1,
  445. vc->vc_cols - vc->vc_x - nr);
  446. vc->vc_attr = vc->vc_video_erase_char >> 8;
  447. while (nr--)
  448. vc->vc_sw->con_putc(vc, vc->vc_video_erase_char, vc->vc_y,
  449. vc->vc_cols - 1 - nr);
  450. vc->vc_attr = oldattr;
  451. }
  452. }
  453. static int softcursor_original;
  454. static void add_softcursor(struct vc_data *vc)
  455. {
  456. int i = scr_readw((u16 *) vc->vc_pos);
  457. u32 type = vc->vc_cursor_type;
  458. if (! (type & 0x10)) return;
  459. if (softcursor_original != -1) return;
  460. softcursor_original = i;
  461. i |= ((type >> 8) & 0xff00 );
  462. i ^= ((type) & 0xff00 );
  463. if ((type & 0x20) && ((softcursor_original & 0x7000) == (i & 0x7000))) i ^= 0x7000;
  464. if ((type & 0x40) && ((i & 0x700) == ((i & 0x7000) >> 4))) i ^= 0x0700;
  465. scr_writew(i, (u16 *) vc->vc_pos);
  466. if (DO_UPDATE(vc))
  467. vc->vc_sw->con_putc(vc, i, vc->vc_y, vc->vc_x);
  468. }
  469. static void hide_softcursor(struct vc_data *vc)
  470. {
  471. if (softcursor_original != -1) {
  472. scr_writew(softcursor_original, (u16 *)vc->vc_pos);
  473. if (DO_UPDATE(vc))
  474. vc->vc_sw->con_putc(vc, softcursor_original,
  475. vc->vc_y, vc->vc_x);
  476. softcursor_original = -1;
  477. }
  478. }
  479. static void hide_cursor(struct vc_data *vc)
  480. {
  481. if (vc == sel_cons)
  482. clear_selection();
  483. vc->vc_sw->con_cursor(vc, CM_ERASE);
  484. hide_softcursor(vc);
  485. }
  486. static void set_cursor(struct vc_data *vc)
  487. {
  488. if (!IS_FG(vc) || console_blanked ||
  489. vc->vc_mode == KD_GRAPHICS)
  490. return;
  491. if (vc->vc_deccm) {
  492. if (vc == sel_cons)
  493. clear_selection();
  494. add_softcursor(vc);
  495. if ((vc->vc_cursor_type & 0x0f) != 1)
  496. vc->vc_sw->con_cursor(vc, CM_DRAW);
  497. } else
  498. hide_cursor(vc);
  499. }
  500. static void set_origin(struct vc_data *vc)
  501. {
  502. WARN_CONSOLE_UNLOCKED();
  503. if (!CON_IS_VISIBLE(vc) ||
  504. !vc->vc_sw->con_set_origin ||
  505. !vc->vc_sw->con_set_origin(vc))
  506. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  507. vc->vc_visible_origin = vc->vc_origin;
  508. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  509. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  510. }
  511. static inline void save_screen(struct vc_data *vc)
  512. {
  513. WARN_CONSOLE_UNLOCKED();
  514. if (vc->vc_sw->con_save_screen)
  515. vc->vc_sw->con_save_screen(vc);
  516. }
  517. /*
  518. * Redrawing of screen
  519. */
  520. static void clear_buffer_attributes(struct vc_data *vc)
  521. {
  522. unsigned short *p = (unsigned short *)vc->vc_origin;
  523. int count = vc->vc_screenbuf_size / 2;
  524. int mask = vc->vc_hi_font_mask | 0xff;
  525. for (; count > 0; count--, p++) {
  526. scr_writew((scr_readw(p)&mask) | (vc->vc_video_erase_char & ~mask), p);
  527. }
  528. }
  529. void redraw_screen(struct vc_data *vc, int is_switch)
  530. {
  531. int redraw = 0;
  532. WARN_CONSOLE_UNLOCKED();
  533. if (!vc) {
  534. /* strange ... */
  535. /* printk("redraw_screen: tty %d not allocated ??\n", new_console+1); */
  536. return;
  537. }
  538. if (is_switch) {
  539. struct vc_data *old_vc = vc_cons[fg_console].d;
  540. if (old_vc == vc)
  541. return;
  542. if (!CON_IS_VISIBLE(vc))
  543. redraw = 1;
  544. *vc->vc_display_fg = vc;
  545. fg_console = vc->vc_num;
  546. hide_cursor(old_vc);
  547. if (!CON_IS_VISIBLE(old_vc)) {
  548. save_screen(old_vc);
  549. set_origin(old_vc);
  550. }
  551. } else {
  552. hide_cursor(vc);
  553. redraw = 1;
  554. }
  555. if (redraw) {
  556. int update;
  557. int old_was_color = vc->vc_can_do_color;
  558. set_origin(vc);
  559. update = vc->vc_sw->con_switch(vc);
  560. set_palette(vc);
  561. /*
  562. * If console changed from mono<->color, the best we can do
  563. * is to clear the buffer attributes. As it currently stands,
  564. * rebuilding new attributes from the old buffer is not doable
  565. * without overly complex code.
  566. */
  567. if (old_was_color != vc->vc_can_do_color) {
  568. update_attr(vc);
  569. clear_buffer_attributes(vc);
  570. }
  571. if (update && vc->vc_mode != KD_GRAPHICS)
  572. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  573. }
  574. set_cursor(vc);
  575. if (is_switch) {
  576. set_leds();
  577. compute_shiftstate();
  578. }
  579. }
  580. /*
  581. * Allocation, freeing and resizing of VTs.
  582. */
  583. int vc_cons_allocated(unsigned int i)
  584. {
  585. return (i < MAX_NR_CONSOLES && vc_cons[i].d);
  586. }
  587. static void visual_init(struct vc_data *vc, int num, int init)
  588. {
  589. /* ++Geert: vc->vc_sw->con_init determines console size */
  590. if (vc->vc_sw)
  591. module_put(vc->vc_sw->owner);
  592. vc->vc_sw = conswitchp;
  593. #ifndef VT_SINGLE_DRIVER
  594. if (con_driver_map[num])
  595. vc->vc_sw = con_driver_map[num];
  596. #endif
  597. __module_get(vc->vc_sw->owner);
  598. vc->vc_num = num;
  599. vc->vc_display_fg = &master_display_fg;
  600. vc->vc_uni_pagedir_loc = &vc->vc_uni_pagedir;
  601. vc->vc_uni_pagedir = 0;
  602. vc->vc_hi_font_mask = 0;
  603. vc->vc_complement_mask = 0;
  604. vc->vc_can_do_color = 0;
  605. vc->vc_sw->con_init(vc, init);
  606. if (!vc->vc_complement_mask)
  607. vc->vc_complement_mask = vc->vc_can_do_color ? 0x7700 : 0x0800;
  608. vc->vc_s_complement_mask = vc->vc_complement_mask;
  609. vc->vc_size_row = vc->vc_cols << 1;
  610. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  611. }
  612. int vc_allocate(unsigned int currcons) /* return 0 on success */
  613. {
  614. WARN_CONSOLE_UNLOCKED();
  615. if (currcons >= MAX_NR_CONSOLES)
  616. return -ENXIO;
  617. if (!vc_cons[currcons].d) {
  618. struct vc_data *vc;
  619. /* prevent users from taking too much memory */
  620. if (currcons >= MAX_NR_USER_CONSOLES && !capable(CAP_SYS_RESOURCE))
  621. return -EPERM;
  622. /* due to the granularity of kmalloc, we waste some memory here */
  623. /* the alloc is done in two steps, to optimize the common situation
  624. of a 25x80 console (structsize=216, screenbuf_size=4000) */
  625. /* although the numbers above are not valid since long ago, the
  626. point is still up-to-date and the comment still has its value
  627. even if only as a historical artifact. --mj, July 1998 */
  628. vc = kmalloc(sizeof(struct vc_data), GFP_KERNEL);
  629. if (!vc)
  630. return -ENOMEM;
  631. memset(vc, 0, sizeof(*vc));
  632. vc_cons[currcons].d = vc;
  633. visual_init(vc, currcons, 1);
  634. if (!*vc->vc_uni_pagedir_loc)
  635. con_set_default_unimap(vc);
  636. vc->vc_screenbuf = kmalloc(vc->vc_screenbuf_size, GFP_KERNEL);
  637. if (!vc->vc_screenbuf) {
  638. kfree(vc);
  639. vc_cons[currcons].d = NULL;
  640. return -ENOMEM;
  641. }
  642. vc->vc_kmalloced = 1;
  643. vc_init(vc, vc->vc_rows, vc->vc_cols, 1);
  644. }
  645. return 0;
  646. }
  647. static inline int resize_screen(struct vc_data *vc, int width, int height)
  648. {
  649. /* Resizes the resolution of the display adapater */
  650. int err = 0;
  651. if (vc->vc_mode != KD_GRAPHICS && vc->vc_sw->con_resize)
  652. err = vc->vc_sw->con_resize(vc, width, height);
  653. return err;
  654. }
  655. /*
  656. * Change # of rows and columns (0 means unchanged/the size of fg_console)
  657. * [this is to be used together with some user program
  658. * like resize that changes the hardware videomode]
  659. */
  660. #define VC_RESIZE_MAXCOL (32767)
  661. #define VC_RESIZE_MAXROW (32767)
  662. int vc_resize(struct vc_data *vc, unsigned int cols, unsigned int lines)
  663. {
  664. unsigned long old_origin, new_origin, new_scr_end, rlth, rrem, err = 0;
  665. unsigned int old_cols, old_rows, old_row_size, old_screen_size;
  666. unsigned int new_cols, new_rows, new_row_size, new_screen_size;
  667. unsigned short *newscreen;
  668. WARN_CONSOLE_UNLOCKED();
  669. if (!vc)
  670. return -ENXIO;
  671. if (cols > VC_RESIZE_MAXCOL || lines > VC_RESIZE_MAXROW)
  672. return -EINVAL;
  673. new_cols = (cols ? cols : vc->vc_cols);
  674. new_rows = (lines ? lines : vc->vc_rows);
  675. new_row_size = new_cols << 1;
  676. new_screen_size = new_row_size * new_rows;
  677. if (new_cols == vc->vc_cols && new_rows == vc->vc_rows)
  678. return 0;
  679. newscreen = (unsigned short *) kmalloc(new_screen_size, GFP_USER);
  680. if (!newscreen)
  681. return -ENOMEM;
  682. old_rows = vc->vc_rows;
  683. old_cols = vc->vc_cols;
  684. old_row_size = vc->vc_size_row;
  685. old_screen_size = vc->vc_screenbuf_size;
  686. err = resize_screen(vc, new_cols, new_rows);
  687. if (err) {
  688. kfree(newscreen);
  689. return err;
  690. }
  691. vc->vc_rows = new_rows;
  692. vc->vc_cols = new_cols;
  693. vc->vc_size_row = new_row_size;
  694. vc->vc_screenbuf_size = new_screen_size;
  695. rlth = min(old_row_size, new_row_size);
  696. rrem = new_row_size - rlth;
  697. old_origin = vc->vc_origin;
  698. new_origin = (long) newscreen;
  699. new_scr_end = new_origin + new_screen_size;
  700. if (new_rows < old_rows)
  701. old_origin += (old_rows - new_rows) * old_row_size;
  702. update_attr(vc);
  703. while (old_origin < vc->vc_scr_end) {
  704. scr_memcpyw((unsigned short *) new_origin, (unsigned short *) old_origin, rlth);
  705. if (rrem)
  706. scr_memsetw((void *)(new_origin + rlth), vc->vc_video_erase_char, rrem);
  707. old_origin += old_row_size;
  708. new_origin += new_row_size;
  709. }
  710. if (new_scr_end > new_origin)
  711. scr_memsetw((void *)new_origin, vc->vc_video_erase_char, new_scr_end - new_origin);
  712. if (vc->vc_kmalloced)
  713. kfree(vc->vc_screenbuf);
  714. vc->vc_screenbuf = newscreen;
  715. vc->vc_kmalloced = 1;
  716. vc->vc_screenbuf_size = new_screen_size;
  717. set_origin(vc);
  718. /* do part of a reset_terminal() */
  719. vc->vc_top = 0;
  720. vc->vc_bottom = vc->vc_rows;
  721. gotoxy(vc, vc->vc_x, vc->vc_y);
  722. save_cur(vc);
  723. if (vc->vc_tty) {
  724. struct winsize ws, *cws = &vc->vc_tty->winsize;
  725. memset(&ws, 0, sizeof(ws));
  726. ws.ws_row = vc->vc_rows;
  727. ws.ws_col = vc->vc_cols;
  728. ws.ws_ypixel = vc->vc_scan_lines;
  729. if ((ws.ws_row != cws->ws_row || ws.ws_col != cws->ws_col) &&
  730. vc->vc_tty->pgrp > 0)
  731. kill_pg(vc->vc_tty->pgrp, SIGWINCH, 1);
  732. *cws = ws;
  733. }
  734. if (CON_IS_VISIBLE(vc))
  735. update_screen(vc);
  736. return err;
  737. }
  738. void vc_disallocate(unsigned int currcons)
  739. {
  740. WARN_CONSOLE_UNLOCKED();
  741. if (vc_cons_allocated(currcons)) {
  742. struct vc_data *vc = vc_cons[currcons].d;
  743. vc->vc_sw->con_deinit(vc);
  744. if (vc->vc_kmalloced)
  745. kfree(vc->vc_screenbuf);
  746. if (currcons >= MIN_NR_CONSOLES)
  747. kfree(vc);
  748. vc_cons[currcons].d = NULL;
  749. }
  750. }
  751. /*
  752. * VT102 emulator
  753. */
  754. #define set_kbd(vc, x) set_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  755. #define clr_kbd(vc, x) clr_vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  756. #define is_kbd(vc, x) vc_kbd_mode(kbd_table + (vc)->vc_num, (x))
  757. #define decarm VC_REPEAT
  758. #define decckm VC_CKMODE
  759. #define kbdapplic VC_APPLIC
  760. #define lnm VC_CRLF
  761. /*
  762. * this is what the terminal answers to a ESC-Z or csi0c query.
  763. */
  764. #define VT100ID "\033[?1;2c"
  765. #define VT102ID "\033[?6c"
  766. unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
  767. 8,12,10,14, 9,13,11,15 };
  768. /* the default colour table, for VGA+ colour systems */
  769. int default_red[] = {0x00,0xaa,0x00,0xaa,0x00,0xaa,0x00,0xaa,
  770. 0x55,0xff,0x55,0xff,0x55,0xff,0x55,0xff};
  771. int default_grn[] = {0x00,0x00,0xaa,0x55,0x00,0x00,0xaa,0xaa,
  772. 0x55,0x55,0xff,0xff,0x55,0x55,0xff,0xff};
  773. int default_blu[] = {0x00,0x00,0x00,0x00,0xaa,0xaa,0xaa,0xaa,
  774. 0x55,0x55,0x55,0x55,0xff,0xff,0xff,0xff};
  775. /*
  776. * gotoxy() must verify all boundaries, because the arguments
  777. * might also be negative. If the given position is out of
  778. * bounds, the cursor is placed at the nearest margin.
  779. */
  780. static void gotoxy(struct vc_data *vc, int new_x, int new_y)
  781. {
  782. int min_y, max_y;
  783. if (new_x < 0)
  784. vc->vc_x = 0;
  785. else {
  786. if (new_x >= vc->vc_cols)
  787. vc->vc_x = vc->vc_cols - 1;
  788. else
  789. vc->vc_x = new_x;
  790. }
  791. if (vc->vc_decom) {
  792. min_y = vc->vc_top;
  793. max_y = vc->vc_bottom;
  794. } else {
  795. min_y = 0;
  796. max_y = vc->vc_rows;
  797. }
  798. if (new_y < min_y)
  799. vc->vc_y = min_y;
  800. else if (new_y >= max_y)
  801. vc->vc_y = max_y - 1;
  802. else
  803. vc->vc_y = new_y;
  804. vc->vc_pos = vc->vc_origin + vc->vc_y * vc->vc_size_row + (vc->vc_x<<1);
  805. vc->vc_need_wrap = 0;
  806. }
  807. /* for absolute user moves, when decom is set */
  808. static void gotoxay(struct vc_data *vc, int new_x, int new_y)
  809. {
  810. gotoxy(vc, new_x, vc->vc_decom ? (vc->vc_top + new_y) : new_y);
  811. }
  812. void scrollback(struct vc_data *vc, int lines)
  813. {
  814. if (!lines)
  815. lines = vc->vc_rows / 2;
  816. scrolldelta(-lines);
  817. }
  818. void scrollfront(struct vc_data *vc, int lines)
  819. {
  820. if (!lines)
  821. lines = vc->vc_rows / 2;
  822. scrolldelta(lines);
  823. }
  824. static void lf(struct vc_data *vc)
  825. {
  826. /* don't scroll if above bottom of scrolling region, or
  827. * if below scrolling region
  828. */
  829. if (vc->vc_y + 1 == vc->vc_bottom)
  830. scrup(vc, vc->vc_top, vc->vc_bottom, 1);
  831. else if (vc->vc_y < vc->vc_rows - 1) {
  832. vc->vc_y++;
  833. vc->vc_pos += vc->vc_size_row;
  834. }
  835. vc->vc_need_wrap = 0;
  836. }
  837. static void ri(struct vc_data *vc)
  838. {
  839. /* don't scroll if below top of scrolling region, or
  840. * if above scrolling region
  841. */
  842. if (vc->vc_y == vc->vc_top)
  843. scrdown(vc, vc->vc_top, vc->vc_bottom, 1);
  844. else if (vc->vc_y > 0) {
  845. vc->vc_y--;
  846. vc->vc_pos -= vc->vc_size_row;
  847. }
  848. vc->vc_need_wrap = 0;
  849. }
  850. static inline void cr(struct vc_data *vc)
  851. {
  852. vc->vc_pos -= vc->vc_x << 1;
  853. vc->vc_need_wrap = vc->vc_x = 0;
  854. }
  855. static inline void bs(struct vc_data *vc)
  856. {
  857. if (vc->vc_x) {
  858. vc->vc_pos -= 2;
  859. vc->vc_x--;
  860. vc->vc_need_wrap = 0;
  861. }
  862. }
  863. static inline void del(struct vc_data *vc)
  864. {
  865. /* ignored */
  866. }
  867. static void csi_J(struct vc_data *vc, int vpar)
  868. {
  869. unsigned int count;
  870. unsigned short * start;
  871. switch (vpar) {
  872. case 0: /* erase from cursor to end of display */
  873. count = (vc->vc_scr_end - vc->vc_pos) >> 1;
  874. start = (unsigned short *)vc->vc_pos;
  875. if (DO_UPDATE(vc)) {
  876. /* do in two stages */
  877. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  878. vc->vc_cols - vc->vc_x);
  879. vc->vc_sw->con_clear(vc, vc->vc_y + 1, 0,
  880. vc->vc_rows - vc->vc_y - 1,
  881. vc->vc_cols);
  882. }
  883. break;
  884. case 1: /* erase from start to cursor */
  885. count = ((vc->vc_pos - vc->vc_origin) >> 1) + 1;
  886. start = (unsigned short *)vc->vc_origin;
  887. if (DO_UPDATE(vc)) {
  888. /* do in two stages */
  889. vc->vc_sw->con_clear(vc, 0, 0, vc->vc_y,
  890. vc->vc_cols);
  891. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  892. vc->vc_x + 1);
  893. }
  894. break;
  895. case 2: /* erase whole display */
  896. count = vc->vc_cols * vc->vc_rows;
  897. start = (unsigned short *)vc->vc_origin;
  898. if (DO_UPDATE(vc))
  899. vc->vc_sw->con_clear(vc, 0, 0,
  900. vc->vc_rows,
  901. vc->vc_cols);
  902. break;
  903. default:
  904. return;
  905. }
  906. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  907. vc->vc_need_wrap = 0;
  908. }
  909. static void csi_K(struct vc_data *vc, int vpar)
  910. {
  911. unsigned int count;
  912. unsigned short * start;
  913. switch (vpar) {
  914. case 0: /* erase from cursor to end of line */
  915. count = vc->vc_cols - vc->vc_x;
  916. start = (unsigned short *)vc->vc_pos;
  917. if (DO_UPDATE(vc))
  918. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1,
  919. vc->vc_cols - vc->vc_x);
  920. break;
  921. case 1: /* erase from start of line to cursor */
  922. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  923. count = vc->vc_x + 1;
  924. if (DO_UPDATE(vc))
  925. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  926. vc->vc_x + 1);
  927. break;
  928. case 2: /* erase whole line */
  929. start = (unsigned short *)(vc->vc_pos - (vc->vc_x << 1));
  930. count = vc->vc_cols;
  931. if (DO_UPDATE(vc))
  932. vc->vc_sw->con_clear(vc, vc->vc_y, 0, 1,
  933. vc->vc_cols);
  934. break;
  935. default:
  936. return;
  937. }
  938. scr_memsetw(start, vc->vc_video_erase_char, 2 * count);
  939. vc->vc_need_wrap = 0;
  940. }
  941. static void csi_X(struct vc_data *vc, int vpar) /* erase the following vpar positions */
  942. { /* not vt100? */
  943. int count;
  944. if (!vpar)
  945. vpar++;
  946. count = (vpar > vc->vc_cols - vc->vc_x) ? (vc->vc_cols - vc->vc_x) : vpar;
  947. scr_memsetw((unsigned short *)vc->vc_pos, vc->vc_video_erase_char, 2 * count);
  948. if (DO_UPDATE(vc))
  949. vc->vc_sw->con_clear(vc, vc->vc_y, vc->vc_x, 1, count);
  950. vc->vc_need_wrap = 0;
  951. }
  952. static void default_attr(struct vc_data *vc)
  953. {
  954. vc->vc_intensity = 1;
  955. vc->vc_underline = 0;
  956. vc->vc_reverse = 0;
  957. vc->vc_blink = 0;
  958. vc->vc_color = vc->vc_def_color;
  959. }
  960. /* console_sem is held */
  961. static void csi_m(struct vc_data *vc)
  962. {
  963. int i;
  964. for (i = 0; i <= vc->vc_npar; i++)
  965. switch (vc->vc_par[i]) {
  966. case 0: /* all attributes off */
  967. default_attr(vc);
  968. break;
  969. case 1:
  970. vc->vc_intensity = 2;
  971. break;
  972. case 2:
  973. vc->vc_intensity = 0;
  974. break;
  975. case 4:
  976. vc->vc_underline = 1;
  977. break;
  978. case 5:
  979. vc->vc_blink = 1;
  980. break;
  981. case 7:
  982. vc->vc_reverse = 1;
  983. break;
  984. case 10: /* ANSI X3.64-1979 (SCO-ish?)
  985. * Select primary font, don't display
  986. * control chars if defined, don't set
  987. * bit 8 on output.
  988. */
  989. vc->vc_translate = set_translate(vc->vc_charset == 0
  990. ? vc->vc_G0_charset
  991. : vc->vc_G1_charset, vc);
  992. vc->vc_disp_ctrl = 0;
  993. vc->vc_toggle_meta = 0;
  994. break;
  995. case 11: /* ANSI X3.64-1979 (SCO-ish?)
  996. * Select first alternate font, lets
  997. * chars < 32 be displayed as ROM chars.
  998. */
  999. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1000. vc->vc_disp_ctrl = 1;
  1001. vc->vc_toggle_meta = 0;
  1002. break;
  1003. case 12: /* ANSI X3.64-1979 (SCO-ish?)
  1004. * Select second alternate font, toggle
  1005. * high bit before displaying as ROM char.
  1006. */
  1007. vc->vc_translate = set_translate(IBMPC_MAP, vc);
  1008. vc->vc_disp_ctrl = 1;
  1009. vc->vc_toggle_meta = 1;
  1010. break;
  1011. case 21:
  1012. case 22:
  1013. vc->vc_intensity = 1;
  1014. break;
  1015. case 24:
  1016. vc->vc_underline = 0;
  1017. break;
  1018. case 25:
  1019. vc->vc_blink = 0;
  1020. break;
  1021. case 27:
  1022. vc->vc_reverse = 0;
  1023. break;
  1024. case 38: /* ANSI X3.64-1979 (SCO-ish?)
  1025. * Enables underscore, white foreground
  1026. * with white underscore (Linux - use
  1027. * default foreground).
  1028. */
  1029. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1030. vc->vc_underline = 1;
  1031. break;
  1032. case 39: /* ANSI X3.64-1979 (SCO-ish?)
  1033. * Disable underline option.
  1034. * Reset colour to default? It did this
  1035. * before...
  1036. */
  1037. vc->vc_color = (vc->vc_def_color & 0x0f) | (vc->vc_color & 0xf0);
  1038. vc->vc_underline = 0;
  1039. break;
  1040. case 49:
  1041. vc->vc_color = (vc->vc_def_color & 0xf0) | (vc->vc_color & 0x0f);
  1042. break;
  1043. default:
  1044. if (vc->vc_par[i] >= 30 && vc->vc_par[i] <= 37)
  1045. vc->vc_color = color_table[vc->vc_par[i] - 30]
  1046. | (vc->vc_color & 0xf0);
  1047. else if (vc->vc_par[i] >= 40 && vc->vc_par[i] <= 47)
  1048. vc->vc_color = (color_table[vc->vc_par[i] - 40] << 4)
  1049. | (vc->vc_color & 0x0f);
  1050. break;
  1051. }
  1052. update_attr(vc);
  1053. }
  1054. static void respond_string(const char *p, struct tty_struct *tty)
  1055. {
  1056. while (*p) {
  1057. tty_insert_flip_char(tty, *p, 0);
  1058. p++;
  1059. }
  1060. con_schedule_flip(tty);
  1061. }
  1062. static void cursor_report(struct vc_data *vc, struct tty_struct *tty)
  1063. {
  1064. char buf[40];
  1065. sprintf(buf, "\033[%d;%dR", vc->vc_y + (vc->vc_decom ? vc->vc_top + 1 : 1), vc->vc_x + 1);
  1066. respond_string(buf, tty);
  1067. }
  1068. static inline void status_report(struct tty_struct *tty)
  1069. {
  1070. respond_string("\033[0n", tty); /* Terminal ok */
  1071. }
  1072. static inline void respond_ID(struct tty_struct * tty)
  1073. {
  1074. respond_string(VT102ID, tty);
  1075. }
  1076. void mouse_report(struct tty_struct *tty, int butt, int mrx, int mry)
  1077. {
  1078. char buf[8];
  1079. sprintf(buf, "\033[M%c%c%c", (char)(' ' + butt), (char)('!' + mrx),
  1080. (char)('!' + mry));
  1081. respond_string(buf, tty);
  1082. }
  1083. /* invoked via ioctl(TIOCLINUX) and through set_selection */
  1084. int mouse_reporting(void)
  1085. {
  1086. return vc_cons[fg_console].d->vc_report_mouse;
  1087. }
  1088. /* console_sem is held */
  1089. static void set_mode(struct vc_data *vc, int on_off)
  1090. {
  1091. int i;
  1092. for (i = 0; i <= vc->vc_npar; i++)
  1093. if (vc->vc_ques) {
  1094. switch(vc->vc_par[i]) { /* DEC private modes set/reset */
  1095. case 1: /* Cursor keys send ^[Ox/^[[x */
  1096. if (on_off)
  1097. set_kbd(vc, decckm);
  1098. else
  1099. clr_kbd(vc, decckm);
  1100. break;
  1101. case 3: /* 80/132 mode switch unimplemented */
  1102. vc->vc_deccolm = on_off;
  1103. #if 0
  1104. vc_resize(deccolm ? 132 : 80, vc->vc_rows);
  1105. /* this alone does not suffice; some user mode
  1106. utility has to change the hardware regs */
  1107. #endif
  1108. break;
  1109. case 5: /* Inverted screen on/off */
  1110. if (vc->vc_decscnm != on_off) {
  1111. vc->vc_decscnm = on_off;
  1112. invert_screen(vc, 0, vc->vc_screenbuf_size, 0);
  1113. update_attr(vc);
  1114. }
  1115. break;
  1116. case 6: /* Origin relative/absolute */
  1117. vc->vc_decom = on_off;
  1118. gotoxay(vc, 0, 0);
  1119. break;
  1120. case 7: /* Autowrap on/off */
  1121. vc->vc_decawm = on_off;
  1122. break;
  1123. case 8: /* Autorepeat on/off */
  1124. if (on_off)
  1125. set_kbd(vc, decarm);
  1126. else
  1127. clr_kbd(vc, decarm);
  1128. break;
  1129. case 9:
  1130. vc->vc_report_mouse = on_off ? 1 : 0;
  1131. break;
  1132. case 25: /* Cursor on/off */
  1133. vc->vc_deccm = on_off;
  1134. break;
  1135. case 1000:
  1136. vc->vc_report_mouse = on_off ? 2 : 0;
  1137. break;
  1138. }
  1139. } else {
  1140. switch(vc->vc_par[i]) { /* ANSI modes set/reset */
  1141. case 3: /* Monitor (display ctrls) */
  1142. vc->vc_disp_ctrl = on_off;
  1143. break;
  1144. case 4: /* Insert Mode on/off */
  1145. vc->vc_decim = on_off;
  1146. break;
  1147. case 20: /* Lf, Enter == CrLf/Lf */
  1148. if (on_off)
  1149. set_kbd(vc, lnm);
  1150. else
  1151. clr_kbd(vc, lnm);
  1152. break;
  1153. }
  1154. }
  1155. }
  1156. /* console_sem is held */
  1157. static void setterm_command(struct vc_data *vc)
  1158. {
  1159. switch(vc->vc_par[0]) {
  1160. case 1: /* set color for underline mode */
  1161. if (vc->vc_can_do_color &&
  1162. vc->vc_par[1] < 16) {
  1163. vc->vc_ulcolor = color_table[vc->vc_par[1]];
  1164. if (vc->vc_underline)
  1165. update_attr(vc);
  1166. }
  1167. break;
  1168. case 2: /* set color for half intensity mode */
  1169. if (vc->vc_can_do_color &&
  1170. vc->vc_par[1] < 16) {
  1171. vc->vc_halfcolor = color_table[vc->vc_par[1]];
  1172. if (vc->vc_intensity == 0)
  1173. update_attr(vc);
  1174. }
  1175. break;
  1176. case 8: /* store colors as defaults */
  1177. vc->vc_def_color = vc->vc_attr;
  1178. if (vc->vc_hi_font_mask == 0x100)
  1179. vc->vc_def_color >>= 1;
  1180. default_attr(vc);
  1181. update_attr(vc);
  1182. break;
  1183. case 9: /* set blanking interval */
  1184. blankinterval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1185. poke_blanked_console();
  1186. break;
  1187. case 10: /* set bell frequency in Hz */
  1188. if (vc->vc_npar >= 1)
  1189. vc->vc_bell_pitch = vc->vc_par[1];
  1190. else
  1191. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1192. break;
  1193. case 11: /* set bell duration in msec */
  1194. if (vc->vc_npar >= 1)
  1195. vc->vc_bell_duration = (vc->vc_par[1] < 2000) ?
  1196. vc->vc_par[1] * HZ / 1000 : 0;
  1197. else
  1198. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1199. break;
  1200. case 12: /* bring specified console to the front */
  1201. if (vc->vc_par[1] >= 1 && vc_cons_allocated(vc->vc_par[1] - 1))
  1202. set_console(vc->vc_par[1] - 1);
  1203. break;
  1204. case 13: /* unblank the screen */
  1205. poke_blanked_console();
  1206. break;
  1207. case 14: /* set vesa powerdown interval */
  1208. vesa_off_interval = ((vc->vc_par[1] < 60) ? vc->vc_par[1] : 60) * 60 * HZ;
  1209. break;
  1210. case 15: /* activate the previous console */
  1211. set_console(last_console);
  1212. break;
  1213. }
  1214. }
  1215. /* console_sem is held */
  1216. static void csi_at(struct vc_data *vc, unsigned int nr)
  1217. {
  1218. if (nr > vc->vc_cols - vc->vc_x)
  1219. nr = vc->vc_cols - vc->vc_x;
  1220. else if (!nr)
  1221. nr = 1;
  1222. insert_char(vc, nr);
  1223. }
  1224. /* console_sem is held */
  1225. static void csi_L(struct vc_data *vc, unsigned int nr)
  1226. {
  1227. if (nr > vc->vc_rows - vc->vc_y)
  1228. nr = vc->vc_rows - vc->vc_y;
  1229. else if (!nr)
  1230. nr = 1;
  1231. scrdown(vc, vc->vc_y, vc->vc_bottom, nr);
  1232. vc->vc_need_wrap = 0;
  1233. }
  1234. /* console_sem is held */
  1235. static void csi_P(struct vc_data *vc, unsigned int nr)
  1236. {
  1237. if (nr > vc->vc_cols - vc->vc_x)
  1238. nr = vc->vc_cols - vc->vc_x;
  1239. else if (!nr)
  1240. nr = 1;
  1241. delete_char(vc, nr);
  1242. }
  1243. /* console_sem is held */
  1244. static void csi_M(struct vc_data *vc, unsigned int nr)
  1245. {
  1246. if (nr > vc->vc_rows - vc->vc_y)
  1247. nr = vc->vc_rows - vc->vc_y;
  1248. else if (!nr)
  1249. nr=1;
  1250. scrup(vc, vc->vc_y, vc->vc_bottom, nr);
  1251. vc->vc_need_wrap = 0;
  1252. }
  1253. /* console_sem is held (except via vc_init->reset_terminal */
  1254. static void save_cur(struct vc_data *vc)
  1255. {
  1256. vc->vc_saved_x = vc->vc_x;
  1257. vc->vc_saved_y = vc->vc_y;
  1258. vc->vc_s_intensity = vc->vc_intensity;
  1259. vc->vc_s_underline = vc->vc_underline;
  1260. vc->vc_s_blink = vc->vc_blink;
  1261. vc->vc_s_reverse = vc->vc_reverse;
  1262. vc->vc_s_charset = vc->vc_charset;
  1263. vc->vc_s_color = vc->vc_color;
  1264. vc->vc_saved_G0 = vc->vc_G0_charset;
  1265. vc->vc_saved_G1 = vc->vc_G1_charset;
  1266. }
  1267. /* console_sem is held */
  1268. static void restore_cur(struct vc_data *vc)
  1269. {
  1270. gotoxy(vc, vc->vc_saved_x, vc->vc_saved_y);
  1271. vc->vc_intensity = vc->vc_s_intensity;
  1272. vc->vc_underline = vc->vc_s_underline;
  1273. vc->vc_blink = vc->vc_s_blink;
  1274. vc->vc_reverse = vc->vc_s_reverse;
  1275. vc->vc_charset = vc->vc_s_charset;
  1276. vc->vc_color = vc->vc_s_color;
  1277. vc->vc_G0_charset = vc->vc_saved_G0;
  1278. vc->vc_G1_charset = vc->vc_saved_G1;
  1279. vc->vc_translate = set_translate(vc->vc_charset ? vc->vc_G1_charset : vc->vc_G0_charset, vc);
  1280. update_attr(vc);
  1281. vc->vc_need_wrap = 0;
  1282. }
  1283. enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey,
  1284. EShash, ESsetG0, ESsetG1, ESpercent, ESignore, ESnonstd,
  1285. ESpalette };
  1286. /* console_sem is held (except via vc_init()) */
  1287. static void reset_terminal(struct vc_data *vc, int do_clear)
  1288. {
  1289. vc->vc_top = 0;
  1290. vc->vc_bottom = vc->vc_rows;
  1291. vc->vc_state = ESnormal;
  1292. vc->vc_ques = 0;
  1293. vc->vc_translate = set_translate(LAT1_MAP, vc);
  1294. vc->vc_G0_charset = LAT1_MAP;
  1295. vc->vc_G1_charset = GRAF_MAP;
  1296. vc->vc_charset = 0;
  1297. vc->vc_need_wrap = 0;
  1298. vc->vc_report_mouse = 0;
  1299. vc->vc_utf = 0;
  1300. vc->vc_utf_count = 0;
  1301. vc->vc_disp_ctrl = 0;
  1302. vc->vc_toggle_meta = 0;
  1303. vc->vc_decscnm = 0;
  1304. vc->vc_decom = 0;
  1305. vc->vc_decawm = 1;
  1306. vc->vc_deccm = 1;
  1307. vc->vc_decim = 0;
  1308. set_kbd(vc, decarm);
  1309. clr_kbd(vc, decckm);
  1310. clr_kbd(vc, kbdapplic);
  1311. clr_kbd(vc, lnm);
  1312. kbd_table[vc->vc_num].lockstate = 0;
  1313. kbd_table[vc->vc_num].slockstate = 0;
  1314. kbd_table[vc->vc_num].ledmode = LED_SHOW_FLAGS;
  1315. kbd_table[vc->vc_num].ledflagstate = kbd_table[vc->vc_num].default_ledflagstate;
  1316. /* do not do set_leds here because this causes an endless tasklet loop
  1317. when the keyboard hasn't been initialized yet */
  1318. vc->vc_cursor_type = CUR_DEFAULT;
  1319. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1320. default_attr(vc);
  1321. update_attr(vc);
  1322. vc->vc_tab_stop[0] = 0x01010100;
  1323. vc->vc_tab_stop[1] =
  1324. vc->vc_tab_stop[2] =
  1325. vc->vc_tab_stop[3] =
  1326. vc->vc_tab_stop[4] = 0x01010101;
  1327. vc->vc_bell_pitch = DEFAULT_BELL_PITCH;
  1328. vc->vc_bell_duration = DEFAULT_BELL_DURATION;
  1329. gotoxy(vc, 0, 0);
  1330. save_cur(vc);
  1331. if (do_clear)
  1332. csi_J(vc, 2);
  1333. }
  1334. /* console_sem is held */
  1335. static void do_con_trol(struct tty_struct *tty, struct vc_data *vc, int c)
  1336. {
  1337. /*
  1338. * Control characters can be used in the _middle_
  1339. * of an escape sequence.
  1340. */
  1341. switch (c) {
  1342. case 0:
  1343. return;
  1344. case 7:
  1345. if (vc->vc_bell_duration)
  1346. kd_mksound(vc->vc_bell_pitch, vc->vc_bell_duration);
  1347. return;
  1348. case 8:
  1349. bs(vc);
  1350. return;
  1351. case 9:
  1352. vc->vc_pos -= (vc->vc_x << 1);
  1353. while (vc->vc_x < vc->vc_cols - 1) {
  1354. vc->vc_x++;
  1355. if (vc->vc_tab_stop[vc->vc_x >> 5] & (1 << (vc->vc_x & 31)))
  1356. break;
  1357. }
  1358. vc->vc_pos += (vc->vc_x << 1);
  1359. return;
  1360. case 10: case 11: case 12:
  1361. lf(vc);
  1362. if (!is_kbd(vc, lnm))
  1363. return;
  1364. case 13:
  1365. cr(vc);
  1366. return;
  1367. case 14:
  1368. vc->vc_charset = 1;
  1369. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1370. vc->vc_disp_ctrl = 1;
  1371. return;
  1372. case 15:
  1373. vc->vc_charset = 0;
  1374. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1375. vc->vc_disp_ctrl = 0;
  1376. return;
  1377. case 24: case 26:
  1378. vc->vc_state = ESnormal;
  1379. return;
  1380. case 27:
  1381. vc->vc_state = ESesc;
  1382. return;
  1383. case 127:
  1384. del(vc);
  1385. return;
  1386. case 128+27:
  1387. vc->vc_state = ESsquare;
  1388. return;
  1389. }
  1390. switch(vc->vc_state) {
  1391. case ESesc:
  1392. vc->vc_state = ESnormal;
  1393. switch (c) {
  1394. case '[':
  1395. vc->vc_state = ESsquare;
  1396. return;
  1397. case ']':
  1398. vc->vc_state = ESnonstd;
  1399. return;
  1400. case '%':
  1401. vc->vc_state = ESpercent;
  1402. return;
  1403. case 'E':
  1404. cr(vc);
  1405. lf(vc);
  1406. return;
  1407. case 'M':
  1408. ri(vc);
  1409. return;
  1410. case 'D':
  1411. lf(vc);
  1412. return;
  1413. case 'H':
  1414. vc->vc_tab_stop[vc->vc_x >> 5] |= (1 << (vc->vc_x & 31));
  1415. return;
  1416. case 'Z':
  1417. respond_ID(tty);
  1418. return;
  1419. case '7':
  1420. save_cur(vc);
  1421. return;
  1422. case '8':
  1423. restore_cur(vc);
  1424. return;
  1425. case '(':
  1426. vc->vc_state = ESsetG0;
  1427. return;
  1428. case ')':
  1429. vc->vc_state = ESsetG1;
  1430. return;
  1431. case '#':
  1432. vc->vc_state = EShash;
  1433. return;
  1434. case 'c':
  1435. reset_terminal(vc, 1);
  1436. return;
  1437. case '>': /* Numeric keypad */
  1438. clr_kbd(vc, kbdapplic);
  1439. return;
  1440. case '=': /* Appl. keypad */
  1441. set_kbd(vc, kbdapplic);
  1442. return;
  1443. }
  1444. return;
  1445. case ESnonstd:
  1446. if (c=='P') { /* palette escape sequence */
  1447. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1448. vc->vc_par[vc->vc_npar] = 0;
  1449. vc->vc_npar = 0;
  1450. vc->vc_state = ESpalette;
  1451. return;
  1452. } else if (c=='R') { /* reset palette */
  1453. reset_palette(vc);
  1454. vc->vc_state = ESnormal;
  1455. } else
  1456. vc->vc_state = ESnormal;
  1457. return;
  1458. case ESpalette:
  1459. if ( (c>='0'&&c<='9') || (c>='A'&&c<='F') || (c>='a'&&c<='f') ) {
  1460. vc->vc_par[vc->vc_npar++] = (c > '9' ? (c & 0xDF) - 'A' + 10 : c - '0');
  1461. if (vc->vc_npar == 7) {
  1462. int i = vc->vc_par[0] * 3, j = 1;
  1463. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1464. vc->vc_palette[i++] += vc->vc_par[j++];
  1465. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1466. vc->vc_palette[i++] += vc->vc_par[j++];
  1467. vc->vc_palette[i] = 16 * vc->vc_par[j++];
  1468. vc->vc_palette[i] += vc->vc_par[j];
  1469. set_palette(vc);
  1470. vc->vc_state = ESnormal;
  1471. }
  1472. } else
  1473. vc->vc_state = ESnormal;
  1474. return;
  1475. case ESsquare:
  1476. for (vc->vc_npar = 0; vc->vc_npar < NPAR; vc->vc_npar++)
  1477. vc->vc_par[vc->vc_npar] = 0;
  1478. vc->vc_npar = 0;
  1479. vc->vc_state = ESgetpars;
  1480. if (c == '[') { /* Function key */
  1481. vc->vc_state=ESfunckey;
  1482. return;
  1483. }
  1484. vc->vc_ques = (c == '?');
  1485. if (vc->vc_ques)
  1486. return;
  1487. case ESgetpars:
  1488. if (c == ';' && vc->vc_npar < NPAR - 1) {
  1489. vc->vc_npar++;
  1490. return;
  1491. } else if (c>='0' && c<='9') {
  1492. vc->vc_par[vc->vc_npar] *= 10;
  1493. vc->vc_par[vc->vc_npar] += c - '0';
  1494. return;
  1495. } else
  1496. vc->vc_state = ESgotpars;
  1497. case ESgotpars:
  1498. vc->vc_state = ESnormal;
  1499. switch(c) {
  1500. case 'h':
  1501. set_mode(vc, 1);
  1502. return;
  1503. case 'l':
  1504. set_mode(vc, 0);
  1505. return;
  1506. case 'c':
  1507. if (vc->vc_ques) {
  1508. if (vc->vc_par[0])
  1509. vc->vc_cursor_type = vc->vc_par[0] | (vc->vc_par[1] << 8) | (vc->vc_par[2] << 16);
  1510. else
  1511. vc->vc_cursor_type = CUR_DEFAULT;
  1512. return;
  1513. }
  1514. break;
  1515. case 'm':
  1516. if (vc->vc_ques) {
  1517. clear_selection();
  1518. if (vc->vc_par[0])
  1519. vc->vc_complement_mask = vc->vc_par[0] << 8 | vc->vc_par[1];
  1520. else
  1521. vc->vc_complement_mask = vc->vc_s_complement_mask;
  1522. return;
  1523. }
  1524. break;
  1525. case 'n':
  1526. if (!vc->vc_ques) {
  1527. if (vc->vc_par[0] == 5)
  1528. status_report(tty);
  1529. else if (vc->vc_par[0] == 6)
  1530. cursor_report(vc, tty);
  1531. }
  1532. return;
  1533. }
  1534. if (vc->vc_ques) {
  1535. vc->vc_ques = 0;
  1536. return;
  1537. }
  1538. switch(c) {
  1539. case 'G': case '`':
  1540. if (vc->vc_par[0])
  1541. vc->vc_par[0]--;
  1542. gotoxy(vc, vc->vc_par[0], vc->vc_y);
  1543. return;
  1544. case 'A':
  1545. if (!vc->vc_par[0])
  1546. vc->vc_par[0]++;
  1547. gotoxy(vc, vc->vc_x, vc->vc_y - vc->vc_par[0]);
  1548. return;
  1549. case 'B': case 'e':
  1550. if (!vc->vc_par[0])
  1551. vc->vc_par[0]++;
  1552. gotoxy(vc, vc->vc_x, vc->vc_y + vc->vc_par[0]);
  1553. return;
  1554. case 'C': case 'a':
  1555. if (!vc->vc_par[0])
  1556. vc->vc_par[0]++;
  1557. gotoxy(vc, vc->vc_x + vc->vc_par[0], vc->vc_y);
  1558. return;
  1559. case 'D':
  1560. if (!vc->vc_par[0])
  1561. vc->vc_par[0]++;
  1562. gotoxy(vc, vc->vc_x - vc->vc_par[0], vc->vc_y);
  1563. return;
  1564. case 'E':
  1565. if (!vc->vc_par[0])
  1566. vc->vc_par[0]++;
  1567. gotoxy(vc, 0, vc->vc_y + vc->vc_par[0]);
  1568. return;
  1569. case 'F':
  1570. if (!vc->vc_par[0])
  1571. vc->vc_par[0]++;
  1572. gotoxy(vc, 0, vc->vc_y - vc->vc_par[0]);
  1573. return;
  1574. case 'd':
  1575. if (vc->vc_par[0])
  1576. vc->vc_par[0]--;
  1577. gotoxay(vc, vc->vc_x ,vc->vc_par[0]);
  1578. return;
  1579. case 'H': case 'f':
  1580. if (vc->vc_par[0])
  1581. vc->vc_par[0]--;
  1582. if (vc->vc_par[1])
  1583. vc->vc_par[1]--;
  1584. gotoxay(vc, vc->vc_par[1], vc->vc_par[0]);
  1585. return;
  1586. case 'J':
  1587. csi_J(vc, vc->vc_par[0]);
  1588. return;
  1589. case 'K':
  1590. csi_K(vc, vc->vc_par[0]);
  1591. return;
  1592. case 'L':
  1593. csi_L(vc, vc->vc_par[0]);
  1594. return;
  1595. case 'M':
  1596. csi_M(vc, vc->vc_par[0]);
  1597. return;
  1598. case 'P':
  1599. csi_P(vc, vc->vc_par[0]);
  1600. return;
  1601. case 'c':
  1602. if (!vc->vc_par[0])
  1603. respond_ID(tty);
  1604. return;
  1605. case 'g':
  1606. if (!vc->vc_par[0])
  1607. vc->vc_tab_stop[vc->vc_x >> 5] &= ~(1 << (vc->vc_x & 31));
  1608. else if (vc->vc_par[0] == 3) {
  1609. vc->vc_tab_stop[0] =
  1610. vc->vc_tab_stop[1] =
  1611. vc->vc_tab_stop[2] =
  1612. vc->vc_tab_stop[3] =
  1613. vc->vc_tab_stop[4] = 0;
  1614. }
  1615. return;
  1616. case 'm':
  1617. csi_m(vc);
  1618. return;
  1619. case 'q': /* DECLL - but only 3 leds */
  1620. /* map 0,1,2,3 to 0,1,2,4 */
  1621. if (vc->vc_par[0] < 4)
  1622. setledstate(kbd_table + vc->vc_num,
  1623. (vc->vc_par[0] < 3) ? vc->vc_par[0] : 4);
  1624. return;
  1625. case 'r':
  1626. if (!vc->vc_par[0])
  1627. vc->vc_par[0]++;
  1628. if (!vc->vc_par[1])
  1629. vc->vc_par[1] = vc->vc_rows;
  1630. /* Minimum allowed region is 2 lines */
  1631. if (vc->vc_par[0] < vc->vc_par[1] &&
  1632. vc->vc_par[1] <= vc->vc_rows) {
  1633. vc->vc_top = vc->vc_par[0] - 1;
  1634. vc->vc_bottom = vc->vc_par[1];
  1635. gotoxay(vc, 0, 0);
  1636. }
  1637. return;
  1638. case 's':
  1639. save_cur(vc);
  1640. return;
  1641. case 'u':
  1642. restore_cur(vc);
  1643. return;
  1644. case 'X':
  1645. csi_X(vc, vc->vc_par[0]);
  1646. return;
  1647. case '@':
  1648. csi_at(vc, vc->vc_par[0]);
  1649. return;
  1650. case ']': /* setterm functions */
  1651. setterm_command(vc);
  1652. return;
  1653. }
  1654. return;
  1655. case ESpercent:
  1656. vc->vc_state = ESnormal;
  1657. switch (c) {
  1658. case '@': /* defined in ISO 2022 */
  1659. vc->vc_utf = 0;
  1660. return;
  1661. case 'G': /* prelim official escape code */
  1662. case '8': /* retained for compatibility */
  1663. vc->vc_utf = 1;
  1664. return;
  1665. }
  1666. return;
  1667. case ESfunckey:
  1668. vc->vc_state = ESnormal;
  1669. return;
  1670. case EShash:
  1671. vc->vc_state = ESnormal;
  1672. if (c == '8') {
  1673. /* DEC screen alignment test. kludge :-) */
  1674. vc->vc_video_erase_char =
  1675. (vc->vc_video_erase_char & 0xff00) | 'E';
  1676. csi_J(vc, 2);
  1677. vc->vc_video_erase_char =
  1678. (vc->vc_video_erase_char & 0xff00) | ' ';
  1679. do_update_region(vc, vc->vc_origin, vc->vc_screenbuf_size / 2);
  1680. }
  1681. return;
  1682. case ESsetG0:
  1683. if (c == '0')
  1684. vc->vc_G0_charset = GRAF_MAP;
  1685. else if (c == 'B')
  1686. vc->vc_G0_charset = LAT1_MAP;
  1687. else if (c == 'U')
  1688. vc->vc_G0_charset = IBMPC_MAP;
  1689. else if (c == 'K')
  1690. vc->vc_G0_charset = USER_MAP;
  1691. if (vc->vc_charset == 0)
  1692. vc->vc_translate = set_translate(vc->vc_G0_charset, vc);
  1693. vc->vc_state = ESnormal;
  1694. return;
  1695. case ESsetG1:
  1696. if (c == '0')
  1697. vc->vc_G1_charset = GRAF_MAP;
  1698. else if (c == 'B')
  1699. vc->vc_G1_charset = LAT1_MAP;
  1700. else if (c == 'U')
  1701. vc->vc_G1_charset = IBMPC_MAP;
  1702. else if (c == 'K')
  1703. vc->vc_G1_charset = USER_MAP;
  1704. if (vc->vc_charset == 1)
  1705. vc->vc_translate = set_translate(vc->vc_G1_charset, vc);
  1706. vc->vc_state = ESnormal;
  1707. return;
  1708. default:
  1709. vc->vc_state = ESnormal;
  1710. }
  1711. }
  1712. /* This is a temporary buffer used to prepare a tty console write
  1713. * so that we can easily avoid touching user space while holding the
  1714. * console spinlock. It is allocated in con_init and is shared by
  1715. * this code and the vc_screen read/write tty calls.
  1716. *
  1717. * We have to allocate this statically in the kernel data section
  1718. * since console_init (and thus con_init) are called before any
  1719. * kernel memory allocation is available.
  1720. */
  1721. char con_buf[CON_BUF_SIZE];
  1722. DECLARE_MUTEX(con_buf_sem);
  1723. /* acquires console_sem */
  1724. static int do_con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  1725. {
  1726. #ifdef VT_BUF_VRAM_ONLY
  1727. #define FLUSH do { } while(0);
  1728. #else
  1729. #define FLUSH if (draw_x >= 0) { \
  1730. vc->vc_sw->con_putcs(vc, (u16 *)draw_from, (u16 *)draw_to - (u16 *)draw_from, vc->vc_y, draw_x); \
  1731. draw_x = -1; \
  1732. }
  1733. #endif
  1734. int c, tc, ok, n = 0, draw_x = -1;
  1735. unsigned int currcons;
  1736. unsigned long draw_from = 0, draw_to = 0;
  1737. struct vc_data *vc;
  1738. u16 himask, charmask;
  1739. const unsigned char *orig_buf = NULL;
  1740. int orig_count;
  1741. if (in_interrupt())
  1742. return count;
  1743. might_sleep();
  1744. acquire_console_sem();
  1745. vc = tty->driver_data;
  1746. if (vc == NULL) {
  1747. printk(KERN_ERR "vt: argh, driver_data is NULL !\n");
  1748. release_console_sem();
  1749. return 0;
  1750. }
  1751. currcons = vc->vc_num;
  1752. if (!vc_cons_allocated(currcons)) {
  1753. /* could this happen? */
  1754. static int error = 0;
  1755. if (!error) {
  1756. error = 1;
  1757. printk("con_write: tty %d not allocated\n", currcons+1);
  1758. }
  1759. release_console_sem();
  1760. return 0;
  1761. }
  1762. release_console_sem();
  1763. orig_buf = buf;
  1764. orig_count = count;
  1765. /* At this point 'buf' is guaranteed to be a kernel buffer
  1766. * and therefore no access to userspace (and therefore sleeping)
  1767. * will be needed. The con_buf_sem serializes all tty based
  1768. * console rendering and vcs write/read operations. We hold
  1769. * the console spinlock during the entire write.
  1770. */
  1771. acquire_console_sem();
  1772. vc = tty->driver_data;
  1773. if (vc == NULL) {
  1774. printk(KERN_ERR "vt: argh, driver_data _became_ NULL !\n");
  1775. release_console_sem();
  1776. goto out;
  1777. }
  1778. himask = vc->vc_hi_font_mask;
  1779. charmask = himask ? 0x1ff : 0xff;
  1780. /* undraw cursor first */
  1781. if (IS_FG(vc))
  1782. hide_cursor(vc);
  1783. while (!tty->stopped && count) {
  1784. int orig = *buf;
  1785. c = orig;
  1786. buf++;
  1787. n++;
  1788. count--;
  1789. /* Do no translation at all in control states */
  1790. if (vc->vc_state != ESnormal) {
  1791. tc = c;
  1792. } else if (vc->vc_utf) {
  1793. /* Combine UTF-8 into Unicode */
  1794. /* Incomplete characters silently ignored */
  1795. if(c > 0x7f) {
  1796. if (vc->vc_utf_count > 0 && (c & 0xc0) == 0x80) {
  1797. vc->vc_utf_char = (vc->vc_utf_char << 6) | (c & 0x3f);
  1798. vc->vc_utf_count--;
  1799. if (vc->vc_utf_count == 0)
  1800. tc = c = vc->vc_utf_char;
  1801. else continue;
  1802. } else {
  1803. if ((c & 0xe0) == 0xc0) {
  1804. vc->vc_utf_count = 1;
  1805. vc->vc_utf_char = (c & 0x1f);
  1806. } else if ((c & 0xf0) == 0xe0) {
  1807. vc->vc_utf_count = 2;
  1808. vc->v