PageRenderTime 70ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/char/vt.c

https://bitbucket.org/evzijst/gittest
C | 3242 lines | 2506 code | 325 blank | 411 comment | 557 complexity | ddebc660296223b98d318d177e610f5f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0
  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->vc_utf_char = (c & 0x0f);
  1809. } else if ((c & 0xf8) == 0xf0) {
  1810. vc->vc_utf_count = 3;
  1811. vc->vc_utf_char = (c & 0x07);
  1812. } else if ((c & 0xfc) == 0xf8) {
  1813. vc->vc_utf_count = 4;
  1814. vc->vc_utf_char = (c & 0x03);
  1815. } else if ((c & 0xfe) == 0xfc) {
  1816. vc->vc_utf_count = 5;
  1817. vc->vc_utf_char = (c & 0x01);
  1818. } else
  1819. vc->vc_utf_count = 0;
  1820. continue;
  1821. }
  1822. } else {
  1823. tc = c;
  1824. vc->vc_utf_count = 0;
  1825. }
  1826. } else { /* no utf */
  1827. tc = vc->vc_translate[vc->vc_toggle_meta ? (c | 0x80) : c];
  1828. }
  1829. /* If the original code was a control character we
  1830. * only allow a glyph to be displayed if the code is
  1831. * not normally used (such as for cursor movement) or
  1832. * if the disp_ctrl mode has been explicitly enabled.
  1833. * Certain characters (as given by the CTRL_ALWAYS
  1834. * bitmap) are always displayed as control characters,
  1835. * as the console would be pretty useless without
  1836. * them; to display an arbitrary font position use the
  1837. * direct-to-font zone in UTF-8 mode.
  1838. */
  1839. ok = tc && (c >= 32 ||
  1840. (!vc->vc_utf && !(((vc->vc_disp_ctrl ? CTRL_ALWAYS
  1841. : CTRL_ACTION) >> c) & 1)))
  1842. && (c != 127 || vc->vc_disp_ctrl)
  1843. && (c != 128+27);
  1844. if (vc->vc_state == ESnormal && ok) {
  1845. /* Now try to find out how to display it */
  1846. tc = conv_uni_to_pc(vc, tc);
  1847. if ( tc == -4 ) {
  1848. /* If we got -4 (not found) then see if we have
  1849. defined a replacement character (U+FFFD) */
  1850. tc = conv_uni_to_pc(vc, 0xfffd);
  1851. /* One reason for the -4 can be that we just
  1852. did a clear_unimap();
  1853. try at least to show something. */
  1854. if (tc == -4)
  1855. tc = c;
  1856. } else if ( tc == -3 ) {
  1857. /* Bad hash table -- hope for the best */
  1858. tc = c;
  1859. }
  1860. if (tc & ~charmask)
  1861. continue; /* Conversion failed */
  1862. if (vc->vc_need_wrap || vc->vc_decim)
  1863. FLUSH
  1864. if (vc->vc_need_wrap) {
  1865. cr(vc);
  1866. lf(vc);
  1867. }
  1868. if (vc->vc_decim)
  1869. insert_char(vc, 1);
  1870. scr_writew(himask ?
  1871. ((vc->vc_attr << 8) & ~himask) + ((tc & 0x100) ? himask : 0) + (tc & 0xff) :
  1872. (vc->vc_attr << 8) + tc,
  1873. (u16 *) vc->vc_pos);
  1874. if (DO_UPDATE(vc) && draw_x < 0) {
  1875. draw_x = vc->vc_x;
  1876. draw_from = vc->vc_pos;
  1877. }
  1878. if (vc->vc_x == vc->vc_cols - 1) {
  1879. vc->vc_need_wrap = vc->vc_decawm;
  1880. draw_to = vc->vc_pos + 2;
  1881. } else {
  1882. vc->vc_x++;
  1883. draw_to = (vc->vc_pos += 2);
  1884. }
  1885. continue;
  1886. }
  1887. FLUSH
  1888. do_con_trol(tty, vc, orig);
  1889. }
  1890. FLUSH
  1891. console_conditional_schedule();
  1892. release_console_sem();
  1893. out:
  1894. return n;
  1895. #undef FLUSH
  1896. }
  1897. /*
  1898. * This is the console switching callback.
  1899. *
  1900. * Doing console switching in a process context allows
  1901. * us to do the switches asynchronously (needed when we want
  1902. * to switch due to a keyboard interrupt). Synchronization
  1903. * with other console code and prevention of re-entrancy is
  1904. * ensured with console_sem.
  1905. */
  1906. static void console_callback(void *ignored)
  1907. {
  1908. acquire_console_sem();
  1909. if (want_console >= 0) {
  1910. if (want_console != fg_console &&
  1911. vc_cons_allocated(want_console)) {
  1912. hide_cursor(vc_cons[fg_console].d);
  1913. change_console(vc_cons[want_console].d);
  1914. /* we only changed when the console had already
  1915. been allocated - a new console is not created
  1916. in an interrupt routine */
  1917. }
  1918. want_console = -1;
  1919. }
  1920. if (do_poke_blanked_console) { /* do not unblank for a LED change */
  1921. do_poke_blanked_console = 0;
  1922. poke_blanked_console();
  1923. }
  1924. if (scrollback_delta) {
  1925. struct vc_data *vc = vc_cons[fg_console].d;
  1926. clear_selection();
  1927. if (vc->vc_mode == KD_TEXT)
  1928. vc->vc_sw->con_scrolldelta(vc, scrollback_delta);
  1929. scrollback_delta = 0;
  1930. }
  1931. if (blank_timer_expired) {
  1932. do_blank_screen(0);
  1933. blank_timer_expired = 0;
  1934. }
  1935. release_console_sem();
  1936. }
  1937. void set_console(int nr)
  1938. {
  1939. want_console = nr;
  1940. schedule_console_callback();
  1941. }
  1942. struct tty_driver *console_driver;
  1943. #ifdef CONFIG_VT_CONSOLE
  1944. /*
  1945. * Console on virtual terminal
  1946. *
  1947. * The console must be locked when we get here.
  1948. */
  1949. static void vt_console_print(struct console *co, const char *b, unsigned count)
  1950. {
  1951. struct vc_data *vc = vc_cons[fg_console].d;
  1952. unsigned char c;
  1953. static unsigned long printing;
  1954. const ushort *start;
  1955. ushort cnt = 0;
  1956. ushort myx;
  1957. /* console busy or not yet initialized */
  1958. if (!printable || test_and_set_bit(0, &printing))
  1959. return;
  1960. if (kmsg_redirect && vc_cons_allocated(kmsg_redirect - 1))
  1961. vc = vc_cons[kmsg_redirect - 1].d;
  1962. /* read `x' only after setting currcons properly (otherwise
  1963. the `x' macro will read the x of the foreground console). */
  1964. myx = vc->vc_x;
  1965. if (!vc_cons_allocated(fg_console)) {
  1966. /* impossible */
  1967. /* printk("vt_console_print: tty %d not allocated ??\n", currcons+1); */
  1968. goto quit;
  1969. }
  1970. if (vc->vc_mode != KD_TEXT)
  1971. goto quit;
  1972. /* undraw cursor first */
  1973. if (IS_FG(vc))
  1974. hide_cursor(vc);
  1975. start = (ushort *)vc->vc_pos;
  1976. /* Contrived structure to try to emulate original need_wrap behaviour
  1977. * Problems caused when we have need_wrap set on '\n' character */
  1978. while (count--) {
  1979. c = *b++;
  1980. if (c == 10 || c == 13 || c == 8 || vc->vc_need_wrap) {
  1981. if (cnt > 0) {
  1982. if (CON_IS_VISIBLE(vc))
  1983. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  1984. vc->vc_x += cnt;
  1985. if (vc->vc_need_wrap)
  1986. vc->vc_x--;
  1987. cnt = 0;
  1988. }
  1989. if (c == 8) { /* backspace */
  1990. bs(vc);
  1991. start = (ushort *)vc->vc_pos;
  1992. myx = vc->vc_x;
  1993. continue;
  1994. }
  1995. if (c != 13)
  1996. lf(vc);
  1997. cr(vc);
  1998. start = (ushort *)vc->vc_pos;
  1999. myx = vc->vc_x;
  2000. if (c == 10 || c == 13)
  2001. continue;
  2002. }
  2003. scr_writew((vc->vc_attr << 8) + c, (unsigned short *)vc->vc_pos);
  2004. cnt++;
  2005. if (myx == vc->vc_cols - 1) {
  2006. vc->vc_need_wrap = 1;
  2007. continue;
  2008. }
  2009. vc->vc_pos += 2;
  2010. myx++;
  2011. }
  2012. if (cnt > 0) {
  2013. if (CON_IS_VISIBLE(vc))
  2014. vc->vc_sw->con_putcs(vc, start, cnt, vc->vc_y, vc->vc_x);
  2015. vc->vc_x += cnt;
  2016. if (vc->vc_x == vc->vc_cols) {
  2017. vc->vc_x--;
  2018. vc->vc_need_wrap = 1;
  2019. }
  2020. }
  2021. set_cursor(vc);
  2022. quit:
  2023. clear_bit(0, &printing);
  2024. }
  2025. static struct tty_driver *vt_console_device(struct console *c, int *index)
  2026. {
  2027. *index = c->index ? c->index-1 : fg_console;
  2028. return console_driver;
  2029. }
  2030. static struct console vt_console_driver = {
  2031. .name = "tty",
  2032. .write = vt_console_print,
  2033. .device = vt_console_device,
  2034. .unblank = unblank_screen,
  2035. .flags = CON_PRINTBUFFER,
  2036. .index = -1,
  2037. };
  2038. #endif
  2039. /*
  2040. * Handling of Linux-specific VC ioctls
  2041. */
  2042. /*
  2043. * Generally a bit racy with respect to console_sem().
  2044. *
  2045. * There are some functions which don't need it.
  2046. *
  2047. * There are some functions which can sleep for arbitrary periods
  2048. * (paste_selection) but we don't need the lock there anyway.
  2049. *
  2050. * set_selection has locking, and definitely needs it
  2051. */
  2052. int tioclinux(struct tty_struct *tty, unsigned long arg)
  2053. {
  2054. char type, data;
  2055. char __user *p = (char __user *)arg;
  2056. int lines;
  2057. int ret;
  2058. if (tty->driver->type != TTY_DRIVER_TYPE_CONSOLE)
  2059. return -EINVAL;
  2060. if (current->signal->tty != tty && !capable(CAP_SYS_ADMIN))
  2061. return -EPERM;
  2062. if (get_user(type, p))
  2063. return -EFAULT;
  2064. ret = 0;
  2065. switch (type)
  2066. {
  2067. case TIOCL_SETSEL:
  2068. acquire_console_sem();
  2069. ret = set_selection((struct tiocl_selection __user *)(p+1), tty);
  2070. release_console_sem();
  2071. break;
  2072. case TIOCL_PASTESEL:
  2073. ret = paste_selection(tty);
  2074. break;
  2075. case TIOCL_UNBLANKSCREEN:
  2076. unblank_screen();
  2077. break;
  2078. case TIOCL_SELLOADLUT:
  2079. ret = sel_loadlut(p);
  2080. break;
  2081. case TIOCL_GETSHIFTSTATE:
  2082. /*
  2083. * Make it possible to react to Shift+Mousebutton.
  2084. * Note that 'shift_state' is an undocumented
  2085. * kernel-internal variable; programs not closely
  2086. * related to the kernel should not use this.
  2087. */
  2088. data = shift_state;
  2089. ret = __put_user(data, p);
  2090. break;
  2091. case TIOCL_GETMOUSEREPORTING:
  2092. data = mouse_reporting();
  2093. ret = __put_user(data, p);
  2094. break;
  2095. case TIOCL_SETVESABLANK:
  2096. set_vesa_blanking(p);
  2097. break;
  2098. case TIOCL_SETKMSGREDIRECT:
  2099. if (!capable(CAP_SYS_ADMIN)) {
  2100. ret = -EPERM;
  2101. } else {
  2102. if (get_user(data, p+1))
  2103. ret = -EFAULT;
  2104. else
  2105. kmsg_redirect = data;
  2106. }
  2107. break;
  2108. case TIOCL_GETFGCONSOLE:
  2109. ret = fg_console;
  2110. break;
  2111. case TIOCL_SCROLLCONSOLE:
  2112. if (get_user(lines, (s32 __user *)(p+4))) {
  2113. ret = -EFAULT;
  2114. } else {
  2115. scrollfront(vc_cons[fg_console].d, lines);
  2116. ret = 0;
  2117. }
  2118. break;
  2119. case TIOCL_BLANKSCREEN: /* until explicitly unblanked, not only poked */
  2120. ignore_poke = 1;
  2121. do_blank_screen(0);
  2122. break;
  2123. case TIOCL_BLANKEDSCREEN:
  2124. ret = console_blanked;
  2125. break;
  2126. default:
  2127. ret = -EINVAL;
  2128. break;
  2129. }
  2130. return ret;
  2131. }
  2132. /*
  2133. * /dev/ttyN handling
  2134. */
  2135. static int con_write(struct tty_struct *tty, const unsigned char *buf, int count)
  2136. {
  2137. int retval;
  2138. retval = do_con_write(tty, buf, count);
  2139. con_flush_chars(tty);
  2140. return retval;
  2141. }
  2142. static void con_put_char(struct tty_struct *tty, unsigned char ch)
  2143. {
  2144. if (in_interrupt())
  2145. return; /* n_r3964 calls put_char() from interrupt context */
  2146. do_con_write(tty, &ch, 1);
  2147. }
  2148. static int con_write_room(struct tty_struct *tty)
  2149. {
  2150. if (tty->stopped)
  2151. return 0;
  2152. return 4096; /* No limit, really; we're not buffering */
  2153. }
  2154. static int con_chars_in_buffer(struct tty_struct *tty)
  2155. {
  2156. return 0; /* we're not buffering */
  2157. }
  2158. /*
  2159. * con_throttle and con_unthrottle are only used for
  2160. * paste_selection(), which has to stuff in a large number of
  2161. * characters...
  2162. */
  2163. static void con_throttle(struct tty_struct *tty)
  2164. {
  2165. }
  2166. static void con_unthrottle(struct tty_struct *tty)
  2167. {
  2168. struct vc_data *vc = tty->driver_data;
  2169. wake_up_interruptible(&vc->paste_wait);
  2170. }
  2171. /*
  2172. * Turn the Scroll-Lock LED on when the tty is stopped
  2173. */
  2174. static void con_stop(struct tty_struct *tty)
  2175. {
  2176. int console_num;
  2177. if (!tty)
  2178. return;
  2179. console_num = tty->index;
  2180. if (!vc_cons_allocated(console_num))
  2181. return;
  2182. set_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2183. set_leds();
  2184. }
  2185. /*
  2186. * Turn the Scroll-Lock LED off when the console is started
  2187. */
  2188. static void con_start(struct tty_struct *tty)
  2189. {
  2190. int console_num;
  2191. if (!tty)
  2192. return;
  2193. console_num = tty->index;
  2194. if (!vc_cons_allocated(console_num))
  2195. return;
  2196. clr_vc_kbd_led(kbd_table + console_num, VC_SCROLLOCK);
  2197. set_leds();
  2198. }
  2199. static void con_flush_chars(struct tty_struct *tty)
  2200. {
  2201. struct vc_data *vc;
  2202. if (in_interrupt()) /* from flush_to_ldisc */
  2203. return;
  2204. /* if we race with con_close(), vt may be null */
  2205. acquire_console_sem();
  2206. vc = tty->driver_data;
  2207. if (vc)
  2208. set_cursor(vc);
  2209. release_console_sem();
  2210. }
  2211. /*
  2212. * Allocate the console screen memory.
  2213. */
  2214. static int con_open(struct tty_struct *tty, struct file *filp)
  2215. {
  2216. unsigned int currcons = tty->index;
  2217. int ret = 0;
  2218. acquire_console_sem();
  2219. if (tty->count == 1) {
  2220. ret = vc_allocate(currcons);
  2221. if (ret == 0) {
  2222. struct vc_data *vc = vc_cons[currcons].d;
  2223. tty->driver_data = vc;
  2224. vc->vc_tty = tty;
  2225. if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
  2226. tty->winsize.ws_row = vc_cons[currcons].d->vc_rows;
  2227. tty->winsize.ws_col = vc_cons[currcons].d->vc_cols;
  2228. }
  2229. release_console_sem();
  2230. vcs_make_devfs(tty);
  2231. return ret;
  2232. }
  2233. }
  2234. release_console_sem();
  2235. return ret;
  2236. }
  2237. /*
  2238. * We take tty_sem in here to prevent another thread from coming in via init_dev
  2239. * and taking a ref against the tty while we're in the process of forgetting
  2240. * about it and cleaning things up.
  2241. *
  2242. * This is because vcs_remove_devfs() can sleep and will drop the BKL.
  2243. */
  2244. static void con_close(struct tty_struct *tty, struct file *filp)
  2245. {
  2246. down(&tty_sem);
  2247. acquire_console_sem();
  2248. if (tty && tty->count == 1) {
  2249. struct vc_data *vc = tty->driver_data;
  2250. if (vc)
  2251. vc->vc_tty = NULL;
  2252. tty->driver_data = NULL;
  2253. release_console_sem();
  2254. vcs_remove_devfs(tty);
  2255. up(&tty_sem);
  2256. /*
  2257. * tty_sem is released, but we still hold BKL, so there is
  2258. * still exclusion against init_dev()
  2259. */
  2260. return;
  2261. }
  2262. release_console_sem();
  2263. up(&tty_sem);
  2264. }
  2265. static void vc_init(struct vc_data *vc, unsigned int rows,
  2266. unsigned int cols, int do_clear)
  2267. {
  2268. int j, k ;
  2269. vc->vc_cols = cols;
  2270. vc->vc_rows = rows;
  2271. vc->vc_size_row = cols << 1;
  2272. vc->vc_screenbuf_size = vc->vc_rows * vc->vc_size_row;
  2273. set_origin(vc);
  2274. vc->vc_pos = vc->vc_origin;
  2275. reset_vc(vc);
  2276. for (j=k=0; j<16; j++) {
  2277. vc->vc_palette[k++] = default_red[j] ;
  2278. vc->vc_palette[k++] = default_grn[j] ;
  2279. vc->vc_palette[k++] = default_blu[j] ;
  2280. }
  2281. vc->vc_def_color = 0x07; /* white */
  2282. vc->vc_ulcolor = 0x0f; /* bold white */
  2283. vc->vc_halfcolor = 0x08; /* grey */
  2284. init_waitqueue_head(&vc->paste_wait);
  2285. reset_terminal(vc, do_clear);
  2286. }
  2287. /*
  2288. * This routine initializes console interrupts, and does nothing
  2289. * else. If you want the screen to clear, call tty_write with
  2290. * the appropriate escape-sequence.
  2291. */
  2292. static int __init con_init(void)
  2293. {
  2294. const char *display_desc = NULL;
  2295. struct vc_data *vc;
  2296. unsigned int currcons = 0;
  2297. acquire_console_sem();
  2298. if (conswitchp)
  2299. display_desc = conswitchp->con_startup();
  2300. if (!display_desc) {
  2301. fg_console = 0;
  2302. release_console_sem();
  2303. return 0;
  2304. }
  2305. init_timer(&console_timer);
  2306. console_timer.function = blank_screen_t;
  2307. if (blankinterval) {
  2308. blank_state = blank_normal_wait;
  2309. mod_timer(&console_timer, jiffies + blankinterval);
  2310. }
  2311. /*
  2312. * kmalloc is not running yet - we use the bootmem allocator.
  2313. */
  2314. for (currcons = 0; currcons < MIN_NR_CONSOLES; currcons++) {
  2315. vc_cons[currcons].d = vc = alloc_bootmem(sizeof(struct vc_data));
  2316. visual_init(vc, currcons, 1);
  2317. vc->vc_screenbuf = (unsigned short *)alloc_bootmem(vc->vc_screenbuf_size);
  2318. vc->vc_kmalloced = 0;
  2319. vc_init(vc, vc->vc_rows, vc->vc_cols,
  2320. currcons || !vc->vc_sw->con_save_screen);
  2321. }
  2322. currcons = fg_console = 0;
  2323. master_display_fg = vc = vc_cons[currcons].d;
  2324. set_origin(vc);
  2325. save_screen(vc);
  2326. gotoxy(vc, vc->vc_x, vc->vc_y);
  2327. csi_J(vc, 0);
  2328. update_screen(vc);
  2329. printk("Console: %s %s %dx%d",
  2330. vc->vc_can_do_color ? "colour" : "mono",
  2331. display_desc, vc->vc_cols, vc->vc_rows);
  2332. printable = 1;
  2333. printk("\n");
  2334. release_console_sem();
  2335. #ifdef CONFIG_VT_CONSOLE
  2336. register_console(&vt_console_driver);
  2337. #endif
  2338. return 0;
  2339. }
  2340. console_initcall(con_init);
  2341. static struct tty_operations con_ops = {
  2342. .open = con_open,
  2343. .close = con_close,
  2344. .write = con_write,
  2345. .write_room = con_write_room,
  2346. .put_char = con_put_char,
  2347. .flush_chars = con_flush_chars,
  2348. .chars_in_buffer = con_chars_in_buffer,
  2349. .ioctl = vt_ioctl,
  2350. .stop = con_stop,
  2351. .start = con_start,
  2352. .throttle = con_throttle,
  2353. .unthrottle = con_unthrottle,
  2354. };
  2355. int __init vty_init(void)
  2356. {
  2357. vcs_init();
  2358. console_driver = alloc_tty_driver(MAX_NR_CONSOLES);
  2359. if (!console_driver)
  2360. panic("Couldn't allocate console driver\n");
  2361. console_driver->owner = THIS_MODULE;
  2362. console_driver->devfs_name = "vc/";
  2363. console_driver->name = "tty";
  2364. console_driver->name_base = 1;
  2365. console_driver->major = TTY_MAJOR;
  2366. console_driver->minor_start = 1;
  2367. console_driver->type = TTY_DRIVER_TYPE_CONSOLE;
  2368. console_driver->init_termios = tty_std_termios;
  2369. console_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
  2370. tty_set_operations(console_driver, &con_ops);
  2371. if (tty_register_driver(console_driver))
  2372. panic("Couldn't register console driver\n");
  2373. kbd_init();
  2374. console_map_init();
  2375. #ifdef CONFIG_PROM_CONSOLE
  2376. prom_con_init();
  2377. #endif
  2378. #ifdef CONFIG_MDA_CONSOLE
  2379. mda_console_init();
  2380. #endif
  2381. return 0;
  2382. }
  2383. #ifndef VT_SINGLE_DRIVER
  2384. /*
  2385. * If we support more console drivers, this function is used
  2386. * when a driver wants to take over some existing consoles
  2387. * and become default driver for newly opened ones.
  2388. */
  2389. int take_over_console(const struct consw *csw, int first, int last, int deflt)
  2390. {
  2391. int i, j = -1;
  2392. const char *desc;
  2393. struct module *owner;
  2394. owner = csw->owner;
  2395. if (!try_module_get(owner))
  2396. return -ENODEV;
  2397. acquire_console_sem();
  2398. desc = csw->con_startup();
  2399. if (!desc) {
  2400. release_console_sem();
  2401. module_put(owner);
  2402. return -ENODEV;
  2403. }
  2404. if (deflt) {
  2405. if (conswitchp)
  2406. module_put(conswitchp->owner);
  2407. __module_get(owner);
  2408. conswitchp = csw;
  2409. }
  2410. for (i = first; i <= last; i++) {
  2411. int old_was_color;
  2412. struct vc_data *vc = vc_cons[i].d;
  2413. if (con_driver_map[i])
  2414. module_put(con_driver_map[i]->owner);
  2415. __module_get(owner);
  2416. con_driver_map[i] = csw;
  2417. if (!vc || !vc->vc_sw)
  2418. continue;
  2419. j = i;
  2420. if (CON_IS_VISIBLE(vc))
  2421. save_screen(vc);
  2422. old_was_color = vc->vc_can_do_color;
  2423. vc->vc_sw->con_deinit(vc);
  2424. vc->vc_origin = (unsigned long)vc->vc_screenbuf;
  2425. vc->vc_visible_origin = vc->vc_origin;
  2426. vc->vc_scr_end = vc->vc_origin + vc->vc_screenbuf_size;
  2427. vc->vc_pos = vc->vc_origin + vc->vc_size_row * vc->vc_y + 2 * vc->vc_x;
  2428. visual_init(vc, i, 0);
  2429. update_attr(vc);
  2430. /* If the console changed between mono <-> color, then
  2431. * the attributes in the screenbuf will be wrong. The
  2432. * following resets all attributes to something sane.
  2433. */
  2434. if (old_was_color != vc->vc_can_do_color)
  2435. clear_buffer_attributes(vc);
  2436. if (CON_IS_VISIBLE(vc))
  2437. update_screen(vc);
  2438. }
  2439. printk("Console: switching ");
  2440. if (!deflt)
  2441. printk("consoles %d-%d ", first+1, last+1);
  2442. if (j >= 0)
  2443. printk("to %s %s %dx%d\n",
  2444. vc_cons[j].d->vc_can_do_color ? "colour" : "mono",
  2445. desc, vc_cons[j].d->vc_cols, vc_cons[j].d->vc_rows);
  2446. else
  2447. printk("to %s\n", desc);
  2448. release_console_sem();
  2449. module_put(owner);
  2450. return 0;
  2451. }
  2452. void give_up_console(const struct consw *csw)
  2453. {
  2454. int i;
  2455. for(i = 0; i < MAX_NR_CONSOLES; i++)
  2456. if (con_driver_map[i] == csw) {
  2457. module_put(csw->owner);
  2458. con_driver_map[i] = NULL;
  2459. }
  2460. }
  2461. #endif
  2462. /*
  2463. * Screen blanking
  2464. */
  2465. static void set_vesa_blanking(char __user *p)
  2466. {
  2467. unsigned int mode;
  2468. get_user(mode, p + 1);
  2469. vesa_blank_mode = (mode < 4) ? mode : 0;
  2470. }
  2471. /*
  2472. * This is called by a timer handler
  2473. */
  2474. static void vesa_powerdown(void)
  2475. {
  2476. struct vc_data *c = vc_cons[fg_console].d;
  2477. /*
  2478. * Power down if currently suspended (1 or 2),
  2479. * suspend if currently blanked (0),
  2480. * else do nothing (i.e. already powered down (3)).
  2481. * Called only if powerdown features are allowed.
  2482. */
  2483. switch (vesa_blank_mode) {
  2484. case VESA_NO_BLANKING:
  2485. c->vc_sw->con_blank(c, VESA_VSYNC_SUSPEND+1, 0);
  2486. break;
  2487. case VESA_VSYNC_SUSPEND:
  2488. case VESA_HSYNC_SUSPEND:
  2489. c->vc_sw->con_blank(c, VESA_POWERDOWN+1, 0);
  2490. break;
  2491. }
  2492. }
  2493. void do_blank_screen(int entering_gfx)
  2494. {
  2495. struct vc_data *vc = vc_cons[fg_console].d;
  2496. int i;
  2497. WARN_CONSOLE_UNLOCKED();
  2498. if (console_blanked) {
  2499. if (blank_state == blank_vesa_wait) {
  2500. blank_state = blank_off;
  2501. vesa_powerdown();
  2502. }
  2503. return;
  2504. }
  2505. if (blank_state != blank_normal_wait)
  2506. return;
  2507. blank_state = blank_off;
  2508. /* entering graphics mode? */
  2509. if (entering_gfx) {
  2510. hide_cursor(vc);
  2511. save_screen(vc);
  2512. vc->vc_sw->con_blank(vc, -1, 1);
  2513. console_blanked = fg_console + 1;
  2514. set_origin(vc);
  2515. return;
  2516. }
  2517. /* don't blank graphics */
  2518. if (vc->vc_mode != KD_TEXT) {
  2519. console_blanked = fg_console + 1;
  2520. return;
  2521. }
  2522. hide_cursor(vc);
  2523. del_timer_sync(&console_timer);
  2524. blank_timer_expired = 0;
  2525. save_screen(vc);
  2526. /* In case we need to reset origin, blanking hook returns 1 */
  2527. i = vc->vc_sw->con_blank(vc, 1, 0);
  2528. console_blanked = fg_console + 1;
  2529. if (i)
  2530. set_origin(vc);
  2531. if (console_blank_hook && console_blank_hook(1))
  2532. return;
  2533. if (vesa_off_interval) {
  2534. blank_state = blank_vesa_wait,
  2535. mod_timer(&console_timer, jiffies + vesa_off_interval);
  2536. }
  2537. if (vesa_blank_mode)
  2538. vc->vc_sw->con_blank(vc, vesa_blank_mode + 1, 0);
  2539. }
  2540. EXPORT_SYMBOL(do_blank_screen);
  2541. /*
  2542. * Called by timer as well as from vt_console_driver
  2543. */
  2544. void do_unblank_screen(int leaving_gfx)
  2545. {
  2546. struct vc_data *vc;
  2547. /* This should now always be called from a "sane" (read: can schedule)
  2548. * context for the sake of the low level drivers, except in the special
  2549. * case of oops_in_progress
  2550. */
  2551. if (!oops_in_progress)
  2552. might_sleep();
  2553. WARN_CONSOLE_UNLOCKED();
  2554. ignore_poke = 0;
  2555. if (!console_blanked)
  2556. return;
  2557. if (!vc_cons_allocated(fg_console)) {
  2558. /* impossible */
  2559. printk("unblank_screen: tty %d not allocated ??\n", fg_console+1);
  2560. return;
  2561. }
  2562. vc = vc_cons[fg_console].d;
  2563. if (vc->vc_mode != KD_TEXT)
  2564. return; /* but leave console_blanked != 0 */
  2565. if (blankinterval) {
  2566. mod_timer(&console_timer, jiffies + blankinterval);
  2567. blank_state = blank_normal_wait;
  2568. }
  2569. console_blanked = 0;
  2570. if (vc->vc_sw->con_blank(vc, 0, leaving_gfx))
  2571. /* Low-level driver cannot restore -> do it ourselves */
  2572. update_screen(vc);
  2573. if (console_blank_hook)
  2574. console_blank_hook(0);
  2575. set_palette(vc);
  2576. set_cursor(vc);
  2577. }
  2578. EXPORT_SYMBOL(do_unblank_screen);
  2579. /*
  2580. * This is called by the outside world to cause a forced unblank, mostly for
  2581. * oopses. Currently, I just call do_unblank_screen(0), but we could eventually
  2582. * call it with 1 as an argument and so force a mode restore... that may kill
  2583. * X or at least garbage the screen but would also make the Oops visible...
  2584. */
  2585. void unblank_screen(void)
  2586. {
  2587. do_unblank_screen(0);
  2588. }
  2589. /*
  2590. * We defer the timer blanking to work queue so it can take the console semaphore
  2591. * (console operations can still happen at irq time, but only from printk which
  2592. * has the console semaphore. Not perfect yet, but better than no locking
  2593. */
  2594. static void blank_screen_t(unsigned long dummy)
  2595. {
  2596. blank_timer_expired = 1;
  2597. schedule_work(&console_work);
  2598. }
  2599. void poke_blanked_console(void)
  2600. {
  2601. WARN_CONSOLE_UNLOCKED();
  2602. /* Add this so we quickly catch whoever might call us in a non
  2603. * safe context. Nowadays, unblank_screen() isn't to be called in
  2604. * atomic contexts and is allowed to schedule (with the special case
  2605. * of oops_in_progress, but that isn't of any concern for this
  2606. * function. --BenH.
  2607. */
  2608. might_sleep();
  2609. /* This isn't perfectly race free, but a race here would be mostly harmless,
  2610. * at worse, we'll do a spurrious blank and it's unlikely
  2611. */
  2612. del_timer(&console_timer);
  2613. blank_timer_expired = 0;
  2614. if (ignore_poke || !vc_cons[fg_console].d || vc_cons[fg_console].d->vc_mode == KD_GRAPHICS)
  2615. return;
  2616. if (console_blanked)
  2617. unblank_screen();
  2618. else if (blankinterval) {
  2619. mod_timer(&console_timer, jiffies + blankinterval);
  2620. blank_state = blank_normal_wait;
  2621. }
  2622. }
  2623. /*
  2624. * Palettes
  2625. */
  2626. static void set_palette(struct vc_data *vc)
  2627. {
  2628. WARN_CONSOLE_UNLOCKED();
  2629. if (vc->vc_mode != KD_GRAPHICS)
  2630. vc->vc_sw->con_set_palette(vc, color_table);
  2631. }
  2632. static int set_get_cmap(unsigned char __user *arg, int set)
  2633. {
  2634. int i, j, k;
  2635. WARN_CONSOLE_UNLOCKED();
  2636. for (i = 0; i < 16; i++)
  2637. if (set) {
  2638. get_user(default_red[i], arg++);
  2639. get_user(default_grn[i], arg++);
  2640. get_user(default_blu[i], arg++);
  2641. } else {
  2642. put_user(default_red[i], arg++);
  2643. put_user(default_grn[i], arg++);
  2644. put_user(default_blu[i], arg++);
  2645. }
  2646. if (set) {
  2647. for (i = 0; i < MAX_NR_CONSOLES; i++)
  2648. if (vc_cons_allocated(i)) {
  2649. for (j = k = 0; j < 16; j++) {
  2650. vc_cons[i].d->vc_palette[k++] = default_red[j];
  2651. vc_cons[i].d->vc_palette[k++] = default_grn[j];
  2652. vc_cons[i].d->vc_palette[k++] = default_blu[j];
  2653. }
  2654. set_palette(vc_cons[i].d);
  2655. }
  2656. }
  2657. return 0;
  2658. }
  2659. /*
  2660. * Load palette into the DAC registers. arg points to a colour
  2661. * map, 3 bytes per colour, 16 colours, range from 0 to 255.
  2662. */
  2663. int con_set_cmap(unsigned char __user *arg)
  2664. {
  2665. int rc;
  2666. acquire_console_sem();
  2667. rc = set_get_cmap (arg,1);
  2668. release_console_sem();
  2669. return rc;
  2670. }
  2671. int con_get_cmap(unsigned char __user *arg)
  2672. {
  2673. int rc;
  2674. acquire_console_sem();
  2675. rc = set_get_cmap (arg,0);
  2676. release_console_sem();
  2677. return rc;
  2678. }
  2679. void reset_palette(struct vc_data *vc)
  2680. {
  2681. int j, k;
  2682. for (j=k=0; j<16; j++) {
  2683. vc->vc_palette[k++] = default_red[j];
  2684. vc->vc_palette[k++] = default_grn[j];
  2685. vc->vc_palette[k++] = default_blu[j];
  2686. }
  2687. set_palette(vc);
  2688. }
  2689. /*
  2690. * Font switching
  2691. *
  2692. * Currently we only support fonts up to 32 pixels wide, at a maximum height
  2693. * of 32 pixels. Userspace fontdata is stored with 32 bytes (shorts/ints,
  2694. * depending on width) reserved for each character which is kinda wasty, but
  2695. * this is done in order to maintain compatibility with the EGA/VGA fonts. It
  2696. * is upto the actual low-level console-driver convert data into its favorite
  2697. * format (maybe we should add a `fontoffset' field to the `display'
  2698. * structure so we won't have to convert the fontdata all the time.
  2699. * /Jes
  2700. */
  2701. #define max_font_size 65536
  2702. static int con_font_get(struct vc_data *vc, struct console_font_op *op)
  2703. {
  2704. struct console_font font;
  2705. int rc = -EINVAL;
  2706. int c;
  2707. if (vc->vc_mode != KD_TEXT)
  2708. return -EINVAL;
  2709. if (op->data) {
  2710. font.data = kmalloc(max_font_size, GFP_KERNEL);
  2711. if (!font.data)
  2712. return -ENOMEM;
  2713. } else
  2714. font.data = NULL;
  2715. acquire_console_sem();
  2716. if (vc->vc_sw->con_font_get)
  2717. rc = vc->vc_sw->con_font_get(vc, &font);
  2718. else
  2719. rc = -ENOSYS;
  2720. release_console_sem();
  2721. if (rc)
  2722. goto out;
  2723. c = (font.width+7)/8 * 32 * font.charcount;
  2724. if (op->data && font.charcount > op->charcount)
  2725. rc = -ENOSPC;
  2726. if (!(op->flags & KD_FONT_FLAG_OLD)) {
  2727. if (font.width > op->width || font.height > op->height)
  2728. rc = -ENOSPC;
  2729. } else {
  2730. if (font.width != 8)
  2731. rc = -EIO;
  2732. else if ((op->height && font.height > op->height) ||
  2733. font.height > 32)
  2734. rc = -ENOSPC;
  2735. }
  2736. if (rc)
  2737. goto out;
  2738. op->height = font.height;
  2739. op->width = font.width;
  2740. op->charcount = font.charcount;
  2741. if (op->data && copy_to_user(op->data, font.data, c))
  2742. rc = -EFAULT;
  2743. out:
  2744. kfree(font.data);
  2745. return rc;
  2746. }
  2747. static int con_font_set(struct vc_data *vc, struct console_font_op *op)
  2748. {
  2749. struct console_font font;
  2750. int rc = -EINVAL;
  2751. int size;
  2752. if (vc->vc_mode != KD_TEXT)
  2753. return -EINVAL;
  2754. if (!op->data)
  2755. return -EINVAL;
  2756. if (op->charcount > 512)
  2757. return -EINVAL;
  2758. if (!op->height) { /* Need to guess font height [compat] */
  2759. int h, i;
  2760. u8 __user *charmap = op->data;
  2761. u8 tmp;
  2762. /* If from KDFONTOP ioctl, don't allow things which can be done in userland,
  2763. so that we can get rid of this soon */
  2764. if (!(op->flags & KD_FONT_FLAG_OLD))
  2765. return -EINVAL;
  2766. for (h = 32; h > 0; h--)
  2767. for (i = 0; i < op->charcount; i++) {
  2768. if (get_user(tmp, &charmap[32*i+h-1]))
  2769. return -EFAULT;
  2770. if (tmp)
  2771. goto nonzero;
  2772. }
  2773. return -EINVAL;
  2774. nonzero:
  2775. op->height = h;
  2776. }
  2777. if (op->width <= 0 || op->width > 32 || op->height > 32)
  2778. return -EINVAL;
  2779. size = (op->width+7)/8 * 32 * op->charcount;
  2780. if (size > max_font_size)
  2781. return -ENOSPC;
  2782. font.charcount = op->charcount;
  2783. font.height = op->height;
  2784. font.width = op->width;
  2785. font.data = kmalloc(size, GFP_KERNEL);
  2786. if (!font.data)
  2787. return -ENOMEM;
  2788. if (copy_from_user(font.data, op->data, size)) {
  2789. kfree(font.data);
  2790. return -EFAULT;
  2791. }
  2792. acquire_console_sem();
  2793. if (vc->vc_sw->con_font_set)
  2794. rc = vc->vc_sw->con_font_set(vc, &font, op->flags);
  2795. else
  2796. rc = -ENOSYS;
  2797. release_console_sem();
  2798. kfree(font.data);
  2799. return rc;
  2800. }
  2801. static int con_font_default(struct vc_data *vc, struct console_font_op *op)
  2802. {
  2803. struct console_font font = {.width = op->width, .height = op->height};
  2804. char name[MAX_FONT_NAME];
  2805. char *s = name;
  2806. int rc;
  2807. if (vc->vc_mode != KD_TEXT)
  2808. return -EINVAL;
  2809. if (!op->data)
  2810. s = NULL;
  2811. else if (strncpy_from_user(name, op->data, MAX_FONT_NAME - 1) < 0)
  2812. return -EFAULT;
  2813. else
  2814. name[MAX_FONT_NAME - 1] = 0;
  2815. acquire_console_sem();
  2816. if (vc->vc_sw->con_font_default)
  2817. rc = vc->vc_sw->con_font_default(vc, &font, s);
  2818. else
  2819. rc = -ENOSYS;
  2820. release_console_sem();
  2821. if (!rc) {
  2822. op->width = font.width;
  2823. op->height = font.height;
  2824. }
  2825. return rc;
  2826. }
  2827. static int con_font_copy(struct vc_data *vc, struct console_font_op *op)
  2828. {
  2829. int con = op->height;
  2830. int rc;
  2831. if (vc->vc_mode != KD_TEXT)
  2832. return -EINVAL;
  2833. acquire_console_sem();
  2834. if (!vc->vc_sw->con_font_copy)
  2835. rc = -ENOSYS;
  2836. else if (con < 0 || !vc_cons_allocated(con))
  2837. rc = -ENOTTY;
  2838. else if (con == vc->vc_num) /* nothing to do */
  2839. rc = 0;
  2840. else
  2841. rc = vc->vc_sw->con_font_copy(vc, con);
  2842. release_console_sem();
  2843. return rc;
  2844. }
  2845. int con_font_op(struct vc_data *vc, struct console_font_op *op)
  2846. {
  2847. switch (op->op) {
  2848. case KD_FONT_OP_SET:
  2849. return con_font_set(vc, op);
  2850. case KD_FONT_OP_GET:
  2851. return con_font_get(vc, op);
  2852. case KD_FONT_OP_SET_DEFAULT:
  2853. return con_font_default(vc, op);
  2854. case KD_FONT_OP_COPY:
  2855. return con_font_copy(vc, op);
  2856. }
  2857. return -ENOSYS;
  2858. }
  2859. /*
  2860. * Interface exported to selection and vcs.
  2861. */
  2862. /* used by selection */
  2863. u16 screen_glyph(struct vc_data *vc, int offset)
  2864. {
  2865. u16 w = scr_readw(screenpos(vc, offset, 1));
  2866. u16 c = w & 0xff;
  2867. if (w & vc->vc_hi_font_mask)
  2868. c |= 0x100;
  2869. return c;
  2870. }
  2871. /* used by vcs - note the word offset */
  2872. unsigned short *screen_pos(struct vc_data *vc, int w_offset, int viewed)
  2873. {
  2874. return screenpos(vc, 2 * w_offset, viewed);
  2875. }
  2876. void getconsxy(struct vc_data *vc, unsigned char *p)
  2877. {
  2878. p[0] = vc->vc_x;
  2879. p[1] = vc->vc_y;
  2880. }
  2881. void putconsxy(struct vc_data *vc, unsigned char *p)
  2882. {
  2883. gotoxy(vc, p[0], p[1]);
  2884. set_cursor(vc);
  2885. }
  2886. u16 vcs_scr_readw(struct vc_data *vc, const u16 *org)
  2887. {
  2888. if ((unsigned long)org == vc->vc_pos && softcursor_original != -1)
  2889. return softcursor_original;
  2890. return scr_readw(org);
  2891. }
  2892. void vcs_scr_writew(struct vc_data *vc, u16 val, u16 *org)
  2893. {
  2894. scr_writew(val, org);
  2895. if ((unsigned long)org == vc->vc_pos) {
  2896. softcursor_original = -1;
  2897. add_softcursor(vc);
  2898. }
  2899. }
  2900. /*
  2901. * Visible symbols for modules
  2902. */
  2903. EXPORT_SYMBOL(color_table);
  2904. EXPORT_SYMBOL(default_red);
  2905. EXPORT_SYMBOL(default_grn);
  2906. EXPORT_SYMBOL(default_blu);
  2907. EXPORT_SYMBOL(update_region);
  2908. EXPORT_SYMBOL(redraw_screen);
  2909. EXPORT_SYMBOL(vc_resize);
  2910. EXPORT_SYMBOL(fg_console);
  2911. EXPORT_SYMBOL(console_blank_hook);
  2912. EXPORT_SYMBOL(console_blanked);
  2913. EXPORT_SYMBOL(vc_cons);
  2914. #ifndef VT_SINGLE_DRIVER
  2915. EXPORT_SYMBOL(take_over_console);
  2916. EXPORT_SYMBOL(give_up_console);
  2917. #endif