root/kernel/chr_drv/console.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. gotoxy
  2. set_origin
  3. scrup
  4. scrdown
  5. lf
  6. ri
  7. cr
  8. bs
  9. del
  10. csi_J
  11. csi_K
  12. update_attr
  13. default_attr
  14. csi_m
  15. hide_cursor
  16. set_cursor
  17. respond_string
  18. respond_num
  19. cursor_report
  20. status_report
  21. respond_ID
  22. invert_screen
  23. set_mode
  24. setterm_command
  25. insert_char
  26. insert_line
  27. delete_char
  28. delete_line
  29. csi_at
  30. csi_L
  31. csi_P
  32. csi_M
  33. save_cur
  34. restore_cur
  35. reset_terminal
  36. con_write
  37. do_keyboard_interrupt
  38. memsetw
  39. con_init
  40. kbdsave
  41. get_scrmem
  42. set_scrmem
  43. blank_screen
  44. unblank_screen
  45. update_screen
  46. sysbeepstop
  47. sysbeep
  48. do_screendump
  49. console_print
  50. con_open

   1 /*
   2  *  linux/kernel/console.c
   3  *
   4  *  Copyright (C) 1991, 1992  Linus Torvalds
   5  */
   6 
   7 /*
   8  *      console.c
   9  *
  10  * This module exports the console io functions:
  11  * 
  12  *      'long con_init(long)'
  13  *      'void con_open(struct tty_queue * queue, struct )'
  14  *      'void update_screen(int new_console)'
  15  *      'void blank_screen(void)'
  16  *      'void unblank_screen(void)'
  17  * 
  18  * Hopefully this will be a rather complete VT102 implementation.
  19  *
  20  * Beeping thanks to John T Kohl.
  21  * 
  22  * Virtual Consoles, Screen Blanking, Screen Dumping, Color, Graphics
  23  *   Chars, and VT100 enhancements by Peter MacDonald.
  24  */
  25 
  26 /*
  27  *  NOTE!!! We sometimes disable and enable interrupts for a short while
  28  * (to put a word in video IO), but this will work even for keyboard
  29  * interrupts. We know interrupts aren't enabled when getting a keyboard
  30  * interrupt, as we use trap-gates. Hopefully all is well.
  31  */
  32 
  33 /*
  34  * Code to check for different video-cards mostly by Galen Hunt,
  35  * <g-hunt@ee.utah.edu>
  36  */
  37 
  38 #include <linux/sched.h>
  39 #include <linux/timer.h>
  40 #include <linux/tty.h>
  41 #include <linux/config.h>
  42 #include <linux/kernel.h>
  43 #include <linux/string.h>
  44 #include <linux/errno.h>
  45 #include <linux/kd.h>
  46 #include <linux/keyboard.h>
  47 
  48 #include <asm/io.h>
  49 #include <asm/system.h>
  50 #include <asm/segment.h>
  51 
  52 #include "vt_kern.h"
  53 
  54 #define NPAR 16
  55 
  56 extern void vt_init(void);
  57 
  58 unsigned long   video_num_columns;              /* Number of text columns       */
  59 unsigned long   video_num_lines;                /* Number of test lines         */
  60 
  61 static unsigned char    video_type;             /* Type of display being used   */
  62 static unsigned long    video_mem_base;         /* Base of video memory         */
  63 static unsigned long    video_mem_term;         /* End of video memory          */
  64 static unsigned long    video_size_row;         /* Bytes per row                */
  65 static unsigned char    video_page;             /* Initial video page           */
  66 static unsigned short   video_port_reg;         /* Video register select port   */
  67 static unsigned short   video_port_val;         /* Video register value port    */
  68 static int can_do_color = 0;
  69 
  70 static struct {
  71         unsigned short  vc_video_erase_char;    /* Background erase character */
  72         unsigned char   vc_attr;                /* Current attributes */
  73         unsigned char   vc_def_color;           /* Default colors */
  74         unsigned char   vc_color;               /* Foreground & background */
  75         unsigned char   vc_s_color;             /* Saved foreground & background */
  76         unsigned char   vc_ulcolor;             /* Colour for underline mode */
  77         unsigned char   vc_halfcolor;           /* Colour for half intensity mode */
  78         unsigned long   vc_origin;              /* Used for EGA/VGA fast scroll */
  79         unsigned long   vc_scr_end;             /* Used for EGA/VGA fast scroll */
  80         unsigned long   vc_pos;
  81         unsigned long   vc_x,vc_y;
  82         unsigned long   vc_top,vc_bottom;
  83         unsigned long   vc_state;
  84         unsigned long   vc_npar,vc_par[NPAR];
  85         unsigned long   vc_video_mem_start;     /* Start of video RAM           */
  86         unsigned long   vc_video_mem_end;       /* End of video RAM (sort of)   */
  87         unsigned long   vc_saved_x;
  88         unsigned long   vc_saved_y;
  89         /* mode flags */
  90         unsigned long   vc_charset      : 1;    /* Character set G0 / G1 */
  91         unsigned long   vc_s_charset    : 1;    /* Saved character set */
  92         unsigned long   vc_decscnm      : 1;    /* Screen Mode */
  93         unsigned long   vc_decom        : 1;    /* Origin Mode */
  94         unsigned long   vc_decawm       : 1;    /* Autowrap Mode */
  95         unsigned long   vc_deccm        : 1;    /* Cursor Visible */
  96         unsigned long   vc_decim        : 1;    /* Insert Mode */
  97         /* attribute flags */
  98         unsigned long   vc_intensity    : 2;    /* 0=half-bright, 1=normal, 2=bold */
  99         unsigned long   vc_underline    : 1;
 100         unsigned long   vc_blink        : 1;
 101         unsigned long   vc_reverse      : 1;
 102         unsigned long   vc_s_intensity  : 2;    /* saved rendition */
 103         unsigned long   vc_s_underline  : 1;
 104         unsigned long   vc_s_blink      : 1;
 105         unsigned long   vc_s_reverse    : 1;
 106         /* misc */
 107         unsigned long   vc_ques         : 1;
 108         unsigned long   vc_need_wrap    : 1;
 109         unsigned long   vc_tab_stop[5];         /* Tab stops. 160 columns. */
 110         unsigned char   vc_kbdmode;
 111         char *          vc_translate;
 112         char *          vc_G0_charset;
 113         char *          vc_G1_charset;
 114         char *          vc_saved_G0;
 115         char *          vc_saved_G1;
 116         /* additional information is in vt_kern.h */
 117 } vc_cons [NR_CONSOLES];
 118 
 119 #define MEM_BUFFER_SIZE (2*80*50*8) 
 120 
 121 unsigned short *vc_scrbuf[NR_CONSOLES];
 122 static unsigned short * vc_scrmembuf;
 123 static int console_blanked = 0;
 124 
 125 #define origin          (vc_cons[currcons].vc_origin)
 126 #define scr_end         (vc_cons[currcons].vc_scr_end)
 127 #define pos             (vc_cons[currcons].vc_pos)
 128 #define top             (vc_cons[currcons].vc_top)
 129 #define bottom          (vc_cons[currcons].vc_bottom)
 130 #define x               (vc_cons[currcons].vc_x)
 131 #define y               (vc_cons[currcons].vc_y)
 132 #define state           (vc_cons[currcons].vc_state)
 133 #define npar            (vc_cons[currcons].vc_npar)
 134 #define par             (vc_cons[currcons].vc_par)
 135 #define ques            (vc_cons[currcons].vc_ques)
 136 #define attr            (vc_cons[currcons].vc_attr)
 137 #define saved_x         (vc_cons[currcons].vc_saved_x)
 138 #define saved_y         (vc_cons[currcons].vc_saved_y)
 139 #define translate       (vc_cons[currcons].vc_translate)
 140 #define G0_charset      (vc_cons[currcons].vc_G0_charset)
 141 #define G1_charset      (vc_cons[currcons].vc_G1_charset)
 142 #define saved_G0        (vc_cons[currcons].vc_saved_G0)
 143 #define saved_G1        (vc_cons[currcons].vc_saved_G1)
 144 #define video_mem_start (vc_cons[currcons].vc_video_mem_start)
 145 #define video_mem_end   (vc_cons[currcons].vc_video_mem_end)
 146 #define video_erase_char (vc_cons[currcons].vc_video_erase_char)        
 147 #define decscnm         (vc_cons[currcons].vc_decscnm)
 148 #define decom           (vc_cons[currcons].vc_decom)
 149 #define decawm          (vc_cons[currcons].vc_decawm)
 150 #define deccm           (vc_cons[currcons].vc_deccm)
 151 #define decim           (vc_cons[currcons].vc_decim)
 152 #define need_wrap       (vc_cons[currcons].vc_need_wrap)
 153 #define color           (vc_cons[currcons].vc_color)
 154 #define s_color         (vc_cons[currcons].vc_s_color)
 155 #define def_color       (vc_cons[currcons].vc_def_color)
 156 #define foreground      (color & 0x0f)
 157 #define background      (color & 0xf0)
 158 #define charset         (vc_cons[currcons].vc_charset)
 159 #define s_charset       (vc_cons[currcons].vc_s_charset)
 160 #define intensity       (vc_cons[currcons].vc_intensity)
 161 #define underline       (vc_cons[currcons].vc_underline)
 162 #define blink           (vc_cons[currcons].vc_blink)
 163 #define reverse         (vc_cons[currcons].vc_reverse)
 164 #define s_intensity     (vc_cons[currcons].vc_s_intensity)
 165 #define s_underline     (vc_cons[currcons].vc_s_underline)
 166 #define s_blink         (vc_cons[currcons].vc_s_blink)
 167 #define s_reverse       (vc_cons[currcons].vc_s_reverse)
 168 #define ulcolor         (vc_cons[currcons].vc_ulcolor)
 169 #define halfcolor       (vc_cons[currcons].vc_halfcolor)
 170 #define kbdmode         (vc_cons[currcons].vc_kbdmode)
 171 #define tab_stop        (vc_cons[currcons].vc_tab_stop)
 172 #define vcmode          (vt_cons[currcons].vc_mode)
 173 #define vtmode          (vt_cons[currcons].vt_mode)
 174 #define vtpid           (vt_cons[currcons].vt_pid)
 175 #define vtnewvt         (vt_cons[currcons].vt_newvt)
 176 
 177 #define set_kbd(x) set_vc_kbd_flag(kbd_table+currcons,x)
 178 #define clr_kbd(x) clr_vc_kbd_flag(kbd_table+currcons,x)
 179 #define is_kbd(x) vc_kbd_flag(kbd_table+currcons,x)
 180 
 181 #define decarm          VC_REPEAT
 182 #define decckm          VC_CKMODE
 183 #define kbdapplic       VC_APPLIC
 184 #define kbdraw          VC_RAW
 185 #define lnm             VC_CRLF
 186 
 187 int blankinterval = 10*60*HZ;
 188 static int screen_size = 0;
 189 
 190 static void sysbeep(void);
 191 
 192 /*
 193  * this is what the terminal answers to a ESC-Z or csi0c query.
 194  */
 195 #define VT100ID "\033[?1;2c"
 196 #define VT102ID "\033[?6c"
 197 
 198 static char * translations[] = {
 199 /* 8-bit Latin-1 mapped to the PC charater set: '\0' means non-printable */
 200         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 201         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 202         " !\"#$%&'()*+,-./0123456789:;<=>?"
 203         "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
 204         "`abcdefghijklmnopqrstuvwxyz{|}~\0"
 205         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 206         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 207         "\040\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
 208         "\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
 209         "\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
 210         "\376\245\376\376\376\376\231\376\376\376\376\376\232\376\376\341"
 211         "\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
 212         "\376\244\225\242\223\376\224\366\376\227\243\226\201\376\376\230",
 213 /* vt100 graphics */
 214         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 215         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 216         " !\"#$%&'()*+,-./0123456789:;<=>?"
 217         "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^ "
 218         "\004\261\007\007\007\007\370\361\040\007\331\277\332\300\305\007"
 219         "\007\304\007\007\303\264\301\302\263\007\007\007\007\007\234\0"
 220         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 221         "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
 222         "\040\255\233\234\376\235\174\025\376\376\246\256\252\055\376\376"
 223         "\370\361\375\376\376\346\024\371\376\376\247\257\254\253\376\250"
 224         "\376\376\376\376\216\217\222\200\376\220\376\376\376\376\376\376"
 225         "\376\245\376\376\376\376\231\376\376\376\376\376\232\376\376\341"
 226         "\205\240\203\376\204\206\221\207\212\202\210\211\215\241\214\213"
 227         "\376\244\225\242\223\376\224\366\376\227\243\226\201\376\376\230",
 228 /* IBM grapgics: minimal translations (CR, LF, LL and ESC) */
 229         "\000\001\002\003\004\005\006\007\010\011\000\013\000\000\016\017"
 230         "\020\021\022\023\024\025\026\027\030\031\032\000\034\035\036\037"
 231         "\040\041\042\043\044\045\046\047\050\051\052\053\054\055\056\057"
 232         "\060\061\062\063\064\065\066\067\070\071\072\073\074\075\076\077"
 233         "\100\101\102\103\104\105\106\107\110\111\112\113\114\115\116\117"
 234         "\120\121\122\123\124\125\126\127\130\131\132\133\134\135\136\137"
 235         "\140\141\142\143\144\145\146\147\150\151\152\153\154\155\156\157"
 236         "\160\161\162\163\164\165\166\167\170\171\172\173\174\175\176\177"
 237         "\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217"
 238         "\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237"
 239         "\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257"
 240         "\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277"
 241         "\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317"
 242         "\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337"
 243         "\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357"
 244         "\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377"
 245 };
 246 
 247 #define NORM_TRANS (translations[0])
 248 #define GRAF_TRANS (translations[1])
 249 #define NULL_TRANS (translations[2])
 250 
 251 static unsigned char color_table[] = { 0, 4, 2, 6, 1, 5, 3, 7,
 252                                        8,12,10,14, 9,13,11,15 };
 253 
 254 /*
 255  * gotoxy() must verify all boundaries, because the arguments
 256  * might also be negative. If the given position is out of
 257  * bounds, the cursor is placed at the nearest margin.
 258  */
 259 static void gotoxy(int currcons, int new_x, int new_y)
     /* [previous][next][first][last][top][bottom][index][help] */
 260 {
 261         int max_y;
 262 
 263         if (new_x < 0)
 264                 x = 0;
 265         else
 266                 if (new_x >= video_num_columns)
 267                         x = video_num_columns - 1;
 268                 else
 269                         x = new_x;
 270         if (decom) {
 271                 new_y += top;
 272                 max_y = bottom;
 273         } else
 274                 max_y = video_num_lines;
 275         if (new_y < 0)
 276                 y = 0;
 277         else
 278                 if (new_y >= max_y)
 279                         y = max_y - 1;
 280                 else
 281                         y = new_y;
 282         pos = origin + y*video_size_row + (x<<1);
 283         need_wrap = 0;
 284 }
 285 
 286 static void set_origin(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288         if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
 289                 return;
 290         if (currcons != fg_console || console_blanked || vcmode == KD_GRAPHICS)
 291                 return;
 292         cli();
 293         outb_p(12, video_port_reg);
 294         outb_p(0xff&((origin-video_mem_base)>>9), video_port_val);
 295         outb_p(13, video_port_reg);
 296         outb_p(0xff&((origin-video_mem_base)>>1), video_port_val);
 297         sti();
 298 }
 299 
 300 static void scrup(int currcons, unsigned int t, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 301 {
 302         int hardscroll = 1;
 303 
 304         if (b > video_num_lines || t >= b)
 305                 return;
 306         if (video_type != VIDEO_TYPE_EGAC && video_type != VIDEO_TYPE_EGAM)
 307                 hardscroll = 0;
 308         else if (t || b != video_num_lines)
 309                 hardscroll = 0;
 310         if (hardscroll) {
 311                 origin += video_size_row;
 312                 pos += video_size_row;
 313                 scr_end += video_size_row;
 314                 if (scr_end > video_mem_end) {
 315                         __asm__("cld\n\t"
 316                                 "rep\n\t"
 317                                 "movsl\n\t"
 318                                 "movl _video_num_columns,%1\n\t"
 319                                 "rep\n\t"
 320                                 "stosw"
 321                                 ::"a" (video_erase_char),
 322                                 "c" ((video_num_lines-1)*video_num_columns>>1),
 323                                 "D" (video_mem_start),
 324                                 "S" (origin)
 325                                 :"cx","di","si");
 326                         scr_end -= origin-video_mem_start;
 327                         pos -= origin-video_mem_start;
 328                         origin = video_mem_start;
 329                 } else {
 330                         __asm__("cld\n\t"
 331                                 "rep\n\t"
 332                                 "stosw"
 333                                 ::"a" (video_erase_char),
 334                                 "c" (video_num_columns),
 335                                 "D" (scr_end-video_size_row)
 336                                 :"cx","di");
 337                 }
 338                 set_origin(currcons);
 339         } else {
 340                 __asm__("cld\n\t"
 341                         "rep\n\t"
 342                         "movsl\n\t"
 343                         "movl _video_num_columns,%%ecx\n\t"
 344                         "rep\n\t"
 345                         "stosw"
 346                         ::"a" (video_erase_char),
 347                         "c" ((b-t-1)*video_num_columns>>1),
 348                         "D" (origin+video_size_row*t),
 349                         "S" (origin+video_size_row*(t+1))
 350                         :"cx","di","si");
 351         }
 352 }
 353 
 354 static void scrdown(int currcons, unsigned int t, unsigned int b)
     /* [previous][next][first][last][top][bottom][index][help] */
 355 {
 356         if (b > video_num_lines || t >= b)
 357                 return;
 358         __asm__("std\n\t"
 359                 "rep\n\t"
 360                 "movsl\n\t"
 361                 "addl $2,%%edi\n\t"     /* %edi has been decremented by 4 */
 362                 "movl _video_num_columns,%%ecx\n\t"
 363                 "rep\n\t"
 364                 "stosw\n\t"
 365                 "cld"
 366                 ::"a" (video_erase_char),
 367                 "c" ((b-t-1)*video_num_columns>>1),
 368                 "D" (origin+video_size_row*b-4),
 369                 "S" (origin+video_size_row*(b-1)-4)
 370                 :"ax","cx","di","si");
 371 }
 372 
 373 static void lf(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 374 {
 375         if (y+1<bottom) {
 376                 y++;
 377                 pos += video_size_row;
 378                 return;
 379         } else 
 380                 scrup(currcons,top,bottom);
 381         need_wrap = 0;
 382 }
 383 
 384 static void ri(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 385 {
 386         if (y>top) {
 387                 y--;
 388                 pos -= video_size_row;
 389                 return;
 390         } else
 391                 scrdown(currcons,top,bottom);
 392         need_wrap = 0;
 393 }
 394 
 395 static inline void cr(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 396 {
 397         pos -= x<<1;
 398         need_wrap = x = 0;
 399 }
 400 
 401 static inline void bs(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 402 {
 403         if (x) {
 404                 pos -= 2;
 405                 x--;
 406                 need_wrap = 0;
 407         }
 408 }
 409 
 410 static inline void del(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 411 {
 412         if (x) {
 413                 pos -= 2;
 414                 x--;
 415                 *(unsigned short *)pos = video_erase_char;
 416                 need_wrap = 0;
 417         }
 418 }
 419 
 420 static void csi_J(int currcons, int vpar)
     /* [previous][next][first][last][top][bottom][index][help] */
 421 {
 422         unsigned long count;
 423         unsigned long start;
 424 
 425         switch (vpar) {
 426                 case 0: /* erase from cursor to end of display */
 427                         count = (scr_end-pos)>>1;
 428                         start = pos;
 429                         break;
 430                 case 1: /* erase from start to cursor */
 431                         count = ((pos-origin)>>1)+1;
 432                         start = origin;
 433                         break;
 434                 case 2: /* erase whole display */
 435                         count = video_num_columns * video_num_lines;
 436                         start = origin;
 437                         break;
 438                 default:
 439                         return;
 440         }
 441         __asm__("cld\n\t"
 442                 "rep\n\t"
 443                 "stosw\n\t"
 444                 ::"c" (count),
 445                 "D" (start),"a" (video_erase_char)
 446                 :"cx","di");
 447         need_wrap = 0;
 448 }
 449 
 450 static void csi_K(int currcons, int vpar)
     /* [previous][next][first][last][top][bottom][index][help] */
 451 {
 452         long count;
 453         long start;
 454 
 455         switch (vpar) {
 456                 case 0: /* erase from cursor to end of line */
 457                         count = video_num_columns-x;
 458                         start = pos;
 459                         break;
 460                 case 1: /* erase from start of line to cursor */
 461                         start = pos - (x<<1);
 462                         count = x+1;
 463                         break;
 464                 case 2: /* erase whole line */
 465                         start = pos - (x<<1);
 466                         count = video_num_columns;
 467                         break;
 468                 default:
 469                         return;
 470         }
 471         __asm__("cld\n\t"
 472                 "rep\n\t"
 473                 "stosw\n\t"
 474                 ::"c" (count),
 475                 "D" (start),"a" (video_erase_char)
 476                 :"cx","di");
 477         need_wrap = 0;
 478 }
 479 
 480 /*
 481  *  I hope this works. The monochrome part is untested.
 482  */
 483 static void update_attr(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 484 {
 485         attr = color;
 486         if (can_do_color) {
 487                 if (underline)
 488                         attr = (attr & 0xf0) | ulcolor;
 489                 else if (intensity == 0)
 490                         attr = (attr & 0xf0) | halfcolor;
 491         }
 492         if (reverse ^ decscnm)
 493                 attr = (attr & 0x88) | (((attr >> 4) | (attr << 4)) & 0x77);
 494         if (blink)
 495                 attr ^= 0x80;
 496         if (intensity == 2)
 497                 attr ^= 0x08;
 498         if (!can_do_color) {
 499                 if (underline)
 500                         attr = (attr & 0xf8) | 0x01;
 501                 else if (intensity == 0)
 502                         attr = (attr & 0xf0) | 0x08;
 503         }
 504         if (decscnm)
 505                 video_erase_char = ((color & 0x88) | (((color >> 4) |
 506                         (color << 4)) & 0x77) << 8) | ' ';
 507         else
 508                 video_erase_char = (color << 8) | ' ';
 509 }
 510 
 511 static void default_attr(int currcons) {
     /* [previous][next][first][last][top][bottom][index][help] */
 512         intensity = 1;
 513         underline = 0;
 514         reverse = 0;
 515         blink = 0;
 516         color = def_color;
 517 }
 518 
 519 static void csi_m(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 520 {
 521         int i;
 522 
 523         for (i=0;i<=npar;i++)
 524                 switch (par[i]) {
 525                         case 0: /* all attributes off */
 526                                 default_attr(currcons);
 527                                 break;
 528                         case 1:
 529                                 intensity = 2;
 530                                 break;
 531                         case 2:
 532                                 intensity = 0;
 533                                 break;
 534                         case 4:
 535                                 underline = 1;
 536                                 break;
 537                         case 5:
 538                                 blink = 1;
 539                                 break;
 540                         case 7:
 541                                 reverse = 1;
 542                                 break;
 543                         case 21:
 544                         case 22:
 545                                 intensity = 1;
 546                                 break;
 547                         case 24:
 548                                 underline = 0;
 549                                 break;
 550                         case 25:
 551                                 blink = 0;
 552                                 break;
 553                         case 27:
 554                                 reverse = 0;
 555                                 break;
 556                         case 39:
 557                                 color = (def_color & 0x0f) | background;
 558                                 break;
 559                         case 49:
 560                                 color = (def_color & 0xf0) | foreground;
 561                                 break;
 562                         default:
 563                                 if (par[i] >= 30 && par[i] <= 37)
 564                                         color = color_table[par[i]-30]
 565                                                 | background; 
 566                                 else if (par[i] >= 40 && par[i] <= 47)
 567                                         color = (color_table[par[i]-40]<<4)
 568                                                 | foreground;
 569                                 break;
 570                 }
 571         update_attr(currcons);
 572 }
 573 
 574 static inline void hide_cursor(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 575 {
 576         outb_p(14, video_port_reg);
 577         outb_p(0xff&((scr_end-video_mem_base)>>9), video_port_val);
 578         outb_p(15, video_port_reg);
 579         outb_p(0xff&((scr_end-video_mem_base)>>1), video_port_val);
 580 }
 581 
 582 static inline void set_cursor(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 583 {
 584         if (currcons != fg_console || console_blanked)
 585                 return;
 586         cli();
 587         if (deccm) {
 588                 outb_p(14, video_port_reg);
 589                 outb_p(0xff&((pos-video_mem_base)>>9), video_port_val);
 590                 outb_p(15, video_port_reg);
 591                 outb_p(0xff&((pos-video_mem_base)>>1), video_port_val);
 592         } else
 593                 hide_cursor(currcons);
 594         sti();
 595 }
 596 
 597 static void respond_string(char * p, int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 598 {
 599         while (*p) {
 600                 put_tty_queue(*p, &tty->read_q);
 601                 p++;
 602         }
 603         TTY_READ_FLUSH(tty);
 604 }
 605 
 606 static void respond_num(unsigned int n, int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 607 {
 608         char buff[3];
 609         int i = 0;
 610 
 611         do {
 612                 buff[i++] = (n%10)+'0';
 613                 n /= 10;
 614         } while(n && i < 3);    /* We'll take no chances */
 615         while (i--) {
 616                 put_tty_queue(buff[i], &tty->read_q);
 617         }
 618         /* caller must flush */
 619 }
 620 
 621 static void cursor_report(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 622 {
 623         put_tty_queue('\033', &tty->read_q);
 624         put_tty_queue('[', &tty->read_q);
 625         respond_num(y + (decom ? top+1 : 1), currcons, tty);
 626         put_tty_queue(';', &tty->read_q);
 627         respond_num(x+1, currcons, tty);
 628         put_tty_queue('R', &tty->read_q);
 629         TTY_READ_FLUSH(tty);
 630 }
 631 
 632 static inline void status_report(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 633 {
 634         respond_string("\033[0n", currcons, tty);       /* Terminal ok */
 635 }
 636 
 637 static inline void respond_ID(int currcons, struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 638 {
 639         respond_string(VT102ID, currcons, tty);
 640 }
 641 
 642 static void invert_screen(int currcons) {
     /* [previous][next][first][last][top][bottom][index][help] */
 643         unsigned char *p;
 644 
 645         if (can_do_color)
 646                 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
 647                         *p = (*p & 0x88) | (((*p >> 4) | (*p << 4)) & 0x77);
 648         else
 649                 for (p = (unsigned char *)origin+1; p < (unsigned char *)scr_end; p+=2)
 650                         *p ^= *p & 0x07 == 1 ? 0x70 : 0x77;
 651 }
 652 
 653 static void set_mode(int currcons, int on_off)
     /* [previous][next][first][last][top][bottom][index][help] */
 654 {
 655         int i;
 656 
 657         for (i=0; i<=npar; i++)
 658                 if (ques) switch(par[i]) {      /* DEC private modes set/reset */
 659                         case 1:                 /* Cursor keys send ^[Ox/^[[x */
 660                                 if (on_off)
 661                                         set_kbd(decckm);
 662                                 else
 663                                         clr_kbd(decckm);
 664                                 break;
 665                         case 3: /* 80/132 mode switch unimplemented */
 666                                 csi_J(currcons,2);
 667                                 gotoxy(currcons,0,0);
 668                                 break;
 669                         case 5:                 /* Inverted screen on/off */
 670                                 if (decscnm != on_off) {
 671                                         decscnm = on_off;
 672                                         invert_screen(currcons);
 673                                         update_attr(currcons);
 674                                 }
 675                                 break;
 676                         case 6:                 /* Origin relative/absolute */
 677                                 decom = on_off;
 678                                 gotoxy(currcons,0,0);
 679                                 break;
 680                         case 7:                 /* Autowrap on/off */
 681                                 decawm = on_off;
 682                                 break;
 683                         case 8:                 /* Autorepeat on/off */
 684                                 if (on_off)
 685                                         set_kbd(decarm);
 686                                 else
 687                                         clr_kbd(decarm);
 688                                 break;
 689                         case 25:                /* Cursor on/off */
 690                                 deccm = on_off;
 691                                 set_cursor(currcons);
 692                                 break;
 693                 } else switch(par[i]) {         /* ANSI modes set/reset */
 694                         case 4:                 /* Insert Mode on/off */
 695                                 decim = on_off;
 696                                 break;
 697                         case 20:                /* Lf, Enter == CrLf/Lf */
 698                                 if (on_off)
 699                                         set_kbd(lnm);
 700                                 else
 701                                         clr_kbd(lnm);
 702                                 break;
 703                 }
 704 }
 705 
 706 static void setterm_command(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 707 {
 708         switch(par[0]) {
 709                 case 1: /* set color for underline mode */
 710                         if (can_do_color && par[1] < 16) {
 711                                 ulcolor = color_table[par[1]];
 712                                 if (underline)
 713                                         update_attr(currcons);
 714                         }
 715                         break;
 716                 case 2: /* set color for half intensity mode */
 717                         if (can_do_color && par[1] < 16) {
 718                                 halfcolor = color_table[par[1]];
 719                                 if (intensity == 0)
 720                                         update_attr(currcons);
 721                         }
 722                         break;
 723                 case 8: /* store colors as defaults */
 724                         def_color = attr;
 725                         default_attr(currcons);
 726                         update_attr(currcons);
 727                         break;
 728                 case 9: /* set blanking interval */
 729                         blankinterval = ((par[1] < 60) ? par[1] : 60) * 60 * HZ;
 730                         break;
 731         }
 732 }
 733 
 734 static void insert_char(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 735 {
 736         unsigned int i = x;
 737         unsigned short tmp, old = video_erase_char;
 738         unsigned short * p = (unsigned short *) pos;
 739 
 740         while (i++ < video_num_columns) {
 741                 tmp = *p;
 742                 *p = old;
 743                 old = tmp;
 744                 p++;
 745         }
 746         need_wrap = 0;
 747 }
 748 
 749 static void insert_line(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 750 {
 751         scrdown(currcons,y,bottom);
 752         need_wrap = 0;
 753 }
 754 
 755 static void delete_char(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 756 {
 757         unsigned int i = x;
 758         unsigned short * p = (unsigned short *) pos;
 759 
 760         while (++i < video_num_columns) {
 761                 *p = *(p+1);
 762                 p++;
 763         }
 764         *p = video_erase_char;
 765         need_wrap = 0;
 766 }
 767 
 768 static void delete_line(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 769 {
 770         scrup(currcons,y,bottom);
 771         need_wrap = 0;
 772 }
 773 
 774 static void csi_at(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 775 {
 776         if (nr > video_num_columns)
 777                 nr = video_num_columns;
 778         else if (!nr)
 779                 nr = 1;
 780         while (nr--)
 781                 insert_char(currcons);
 782 }
 783 
 784 static void csi_L(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 785 {
 786         if (nr > video_num_lines)
 787                 nr = video_num_lines;
 788         else if (!nr)
 789                 nr = 1;
 790         while (nr--)
 791                 insert_line(currcons);
 792 }
 793 
 794 static void csi_P(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 795 {
 796         if (nr > video_num_columns)
 797                 nr = video_num_columns;
 798         else if (!nr)
 799                 nr = 1;
 800         while (nr--)
 801                 delete_char(currcons);
 802 }
 803 
 804 static void csi_M(int currcons, unsigned int nr)
     /* [previous][next][first][last][top][bottom][index][help] */
 805 {
 806         if (nr > video_num_lines)
 807                 nr = video_num_lines;
 808         else if (!nr)
 809                 nr=1;
 810         while (nr--)
 811                 delete_line(currcons);
 812 }
 813 
 814 static void save_cur(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 815 {
 816         saved_x         = x;
 817         saved_y         = y;
 818         s_intensity     = intensity;
 819         s_underline     = underline;
 820         s_blink         = blink;
 821         s_reverse       = reverse;
 822         s_charset       = charset;
 823         s_color         = color;
 824         saved_G0        = G0_charset;
 825         saved_G1        = G1_charset;
 826 }
 827 
 828 static void restore_cur(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
 829 {
 830         gotoxy(currcons,saved_x,saved_y);
 831         intensity       = s_intensity;
 832         underline       = s_underline;
 833         blink           = s_blink;
 834         reverse         = s_reverse;
 835         charset         = s_charset;
 836         color           = s_color;
 837         G0_charset      = saved_G0;
 838         G1_charset      = saved_G1;
 839         translate       = charset ? G1_charset : G0_charset;
 840         update_attr(currcons);
 841         need_wrap = 0;
 842 }
 843 
 844 enum { ESnormal, ESesc, ESsquare, ESgetpars, ESgotpars, ESfunckey, 
 845         EShash, ESsetG0, ESsetG1, ESignore };
 846 
 847 static void reset_terminal(int currcons, int do_clear)
     /* [previous][next][first][last][top][bottom][index][help] */
 848 {
 849         top             = 0;
 850         bottom          = video_num_lines;
 851         state           = ESnormal;
 852         ques            = 0;
 853         translate       = NORM_TRANS;
 854         G0_charset      = NORM_TRANS;
 855         G1_charset      = GRAF_TRANS;
 856         charset         = 0;
 857         need_wrap       = 0;
 858 
 859         decscnm         = 0;
 860         decom           = 0;
 861         decawm          = 1;
 862         deccm           = 1;
 863         decim           = 0;
 864 
 865         set_kbd(decarm);
 866         clr_kbd(decckm);
 867         clr_kbd(kbdapplic);
 868         clr_kbd(lnm);
 869         kbd_table[currcons].flags =
 870                 (kbd_table[currcons].flags & ~LED_MASK) |
 871                 (kbd_table[currcons].default_flags & LED_MASK);
 872         kbdmode         = 0;
 873         set_leds();
 874 
 875         default_attr(currcons);
 876         update_attr(currcons);
 877 
 878         tab_stop[0]     = 0x01010100;
 879         tab_stop[1]     =
 880         tab_stop[2]     =
 881         tab_stop[3]     =
 882         tab_stop[4]     = 0x01010101;
 883 
 884         if (do_clear) {
 885                 gotoxy(currcons,0,0);
 886                 csi_J(currcons,2);
 887                 save_cur(currcons);
 888         }
 889 }
 890 
 891 void con_write(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 892 {
 893         int c;
 894         unsigned int currcons;
 895 
 896         wake_up_interruptible(&tty->write_q.proc_list);
 897         currcons = tty->line - 1;
 898         if (currcons >= NR_CONSOLES) {
 899                 printk("con_write: illegal tty (%d)\n", currcons);
 900                 return;
 901         }
 902         while (!tty->stopped && (c = get_tty_queue(&tty->write_q)) >= 0) {
 903                 if (state == ESnormal && translate[c]) {
 904                         if (need_wrap) {
 905                                 cr(currcons);
 906                                 lf(currcons);
 907                         }
 908                         if (decim)
 909                                 insert_char(currcons);
 910                         c = translate[c];
 911                         *(char *) pos = c;
 912                         *(char *) (pos+1) = attr;
 913                         if (x == video_num_columns - 1)
 914                                 need_wrap = decawm;
 915                         else {
 916                                 x++;
 917                                 pos+=2;
 918                         }
 919                         continue;
 920                 }
 921 
 922                 /*
 923                  *  Control characters can be used in the _middle_
 924                  *  of an escape sequence.
 925                  */
 926                 switch (c) {
 927                         case 7:
 928                                 sysbeep();
 929                                 continue;
 930                         case 8:
 931                                 bs(currcons);
 932                                 continue;
 933                         case 9:
 934                                 pos -= (x << 1);
 935                                 while (x < video_num_columns - 1) {
 936                                         x++;
 937                                         if (tab_stop[x >> 5] & (1 << (x & 31)))
 938                                                 break;
 939                                 }
 940                                 pos += (x << 1);
 941                                 continue;
 942                         case 10: case 11: case 12:
 943                                 lf(currcons);
 944                                 if (!is_kbd(lnm))
 945                                         continue;
 946                         case 13:
 947                                 cr(currcons);
 948                                 continue;
 949                         case 14:
 950                                 charset = 1;
 951                                 translate = G1_charset;
 952                                 continue;
 953                         case 15:
 954                                 charset = 0;
 955                                 translate = G0_charset;
 956                                 continue;
 957                         case 24: case 26:
 958                                 state = ESnormal;
 959                                 continue;
 960                         case 27:
 961                                 state = ESesc;
 962                                 continue;
 963                         case 127:
 964                                 del(currcons);
 965                                 continue;
 966                         case 128+27:
 967                                 state = ESsquare;
 968                                 continue;
 969                 }
 970                 switch(state) {
 971                         case ESesc:
 972                                 state = ESnormal;
 973                                 switch (c) {
 974                                   case '[':
 975                                         state = ESsquare;
 976                                         continue;
 977                                   case 'E':
 978                                         cr(currcons);
 979                                         lf(currcons);
 980                                         continue;
 981                                   case 'M':
 982                                         ri(currcons);
 983                                         continue;
 984                                   case 'D':
 985                                         lf(currcons);
 986                                         continue;
 987                                   case 'H':
 988                                         tab_stop[x >> 5] |= (1 << (x & 31));
 989                                         continue;
 990                                   case 'Z':
 991                                         respond_ID(currcons,tty);
 992                                         continue;
 993                                   case '7':
 994                                         save_cur(currcons);
 995                                         continue;
 996                                   case '8':
 997                                         restore_cur(currcons);
 998                                         continue;
 999                                   case '(':
1000                                         state = ESsetG0;
1001                                         continue;
1002                                   case ')':
1003                                         state = ESsetG1;
1004                                         continue;
1005                                   case '#':
1006                                         state = EShash;
1007                                         continue;
1008                                   case 'c':
1009                                         reset_terminal(currcons,1);
1010                                         continue;
1011                                   case '>':  /* Numeric keypad */
1012                                         clr_kbd(kbdapplic);
1013                                         continue;
1014                                   case '=':  /* Appl. keypad */
1015                                         set_kbd(kbdapplic);
1016                                         continue;
1017                                 }       
1018                                 continue;
1019                         case ESsquare:
1020                                 for(npar = 0 ; npar < NPAR ; npar++)
1021                                         par[npar] = 0;
1022                                 npar = 0;
1023                                 state = ESgetpars;
1024                                 if (c == '[') { /* Function key */
1025                                         state=ESfunckey;
1026                                         continue;
1027                                 }
1028                                 ques = (c=='?');
1029                                 if (ques)
1030                                         continue;
1031                         case ESgetpars:
1032                                 if (c==';' && npar<NPAR-1) {
1033                                         npar++;
1034                                         continue;
1035                                 } else if (c>='0' && c<='9') {
1036                                         par[npar] *= 10;
1037                                         par[npar] += c-'0';
1038                                         continue;
1039                                 } else state=ESgotpars;
1040                         case ESgotpars:
1041                                 state = ESnormal;
1042                                 switch(c) {
1043                                         case 'h':
1044                                                 set_mode(currcons,1);
1045                                                 continue;
1046                                         case 'l':
1047                                                 set_mode(currcons,0);
1048                                                 continue;
1049                                         case 'n':
1050                                                 if (!ques)
1051                                                         if (par[0] == 5)
1052                                                                 status_report(currcons,tty);
1053                                                         else if (par[0] == 6)
1054                                                                 cursor_report(currcons,tty);
1055                                                 continue;
1056                                 }
1057                                 if (ques) {
1058                                         ques = 0;
1059                                         continue;
1060                                 }
1061                                 switch(c) {
1062                                         case 'G': case '`':
1063                                                 if (par[0]) par[0]--;
1064                                                 gotoxy(currcons,par[0],y);
1065                                                 continue;
1066                                         case 'A':
1067                                                 if (!par[0]) par[0]++;
1068                                                 gotoxy(currcons,x,y-par[0]);
1069                                                 continue;
1070                                         case 'B': case 'e':
1071                                                 if (!par[0]) par[0]++;
1072                                                 gotoxy(currcons,x,y+par[0]);
1073                                                 continue;
1074                                         case 'C': case 'a':
1075                                                 if (!par[0]) par[0]++;
1076                                                 gotoxy(currcons,x+par[0],y);
1077                                                 continue;
1078                                         case 'D':
1079                                                 if (!par[0]) par[0]++;
1080                                                 gotoxy(currcons,x-par[0],y);
1081                                                 continue;
1082                                         case 'E':
1083                                                 if (!par[0]) par[0]++;
1084                                                 gotoxy(currcons,0,y+par[0]);
1085                                                 continue;
1086                                         case 'F':
1087                                                 if (!par[0]) par[0]++;
1088                                                 gotoxy(currcons,0,y-par[0]);
1089                                                 continue;
1090                                         case 'd':
1091                                                 if (par[0]) par[0]--;
1092                                                 gotoxy(currcons,x,par[0]);
1093                                                 continue;
1094                                         case 'H': case 'f':
1095                                                 if (par[0]) par[0]--;
1096                                                 if (par[1]) par[1]--;
1097                                                 gotoxy(currcons,par[1],par[0]);
1098                                                 continue;
1099                                         case 'J':
1100                                                 csi_J(currcons,par[0]);
1101                                                 continue;
1102                                         case 'K':
1103                                                 csi_K(currcons,par[0]);
1104                                                 continue;
1105                                         case 'L':
1106                                                 csi_L(currcons,par[0]);
1107                                                 continue;
1108                                         case 'M':
1109                                                 csi_M(currcons,par[0]);
1110                                                 continue;
1111                                         case 'P':
1112                                                 csi_P(currcons,par[0]);
1113                                                 continue;
1114                                         case 'c':
1115                                                 if (!par[0])
1116                                                         respond_ID(currcons,tty);
1117                                                 continue;
1118                                         case 'g':
1119                                                 if (!par[0])
1120                                                         tab_stop[x >> 5] &= ~(1 << (x & 31));
1121                                                 else if (par[0] == 3) {
1122                                                         tab_stop[0] =
1123                                                         tab_stop[1] =
1124                                                         tab_stop[2] =
1125                                                         tab_stop[3] =
1126                                                         tab_stop[4] = 0;
1127                                                 }
1128                                                 continue;
1129                                         case 'm':
1130                                                 csi_m(currcons);
1131                                                 continue;
1132                                         case 'r':
1133                                                 if (!par[0])
1134                                                         par[0]++;
1135                                                 if (!par[1])
1136                                                         par[1] = video_num_lines;
1137                                                 /* Minimum allowed region is 2 lines */
1138                                                 if (par[0] < par[1] &&
1139                                                     par[1] <= video_num_lines) {
1140                                                         top=par[0]-1;
1141                                                         bottom=par[1];
1142                                                         gotoxy(currcons,0,0);
1143                                                 }
1144                                                 continue;
1145                                         case 's':
1146                                                 save_cur(currcons);
1147                                                 continue;
1148                                         case 'u':
1149                                                 restore_cur(currcons);
1150                                                 continue;
1151                                         case '@':
1152                                                 csi_at(currcons,par[0]);
1153                                                 continue;
1154                                         case ']': /* setterm functions */
1155                                                 setterm_command(currcons);
1156                                                 continue;
1157                                 }
1158                                 continue;
1159                         case ESfunckey:
1160                                 state = ESnormal;
1161                                 continue;
1162                         case EShash:
1163                                 state = ESnormal;
1164                                 if (c == '8') {
1165                                         /* DEC screen alignment test. kludge :-) */
1166                                         video_erase_char =
1167                                                 (video_erase_char & 0xff00) | 'E';
1168                                         csi_J(currcons, 2);
1169                                         video_erase_char =
1170                                                 (video_erase_char & 0xff00) | ' ';
1171                                 }
1172                                 continue;
1173                         case ESsetG0:
1174                                 if (c == '0')
1175                                         G0_charset = GRAF_TRANS;
1176                                 else if (c == 'B')
1177                                         G0_charset = NORM_TRANS;
1178                                 else if (c == 'U')
1179                                         G0_charset = NULL_TRANS;
1180                                 if (charset == 0)
1181                                         translate = G0_charset;
1182                                 state = ESnormal;
1183                                 continue;
1184                         case ESsetG1:
1185                                 if (c == '0')
1186                                         G1_charset = GRAF_TRANS;
1187                                 else if (c == 'B')
1188                                         G1_charset = NORM_TRANS;
1189                                 else if (c == 'U')
1190                                         G1_charset = NULL_TRANS;
1191                                 if (charset == 1)
1192                                         translate = G1_charset;
1193                                 state = ESnormal;
1194                                 continue;
1195                         default:
1196                                 state = ESnormal;
1197                 }
1198         }
1199         if (vcmode == KD_GRAPHICS)
1200                 return;
1201         set_cursor(currcons);
1202 }
1203 
1204 void do_keyboard_interrupt(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1205 {
1206         TTY_READ_FLUSH(TTY_TABLE(0));
1207         timer_active &= ~(1<<BLANK_TIMER);
1208         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1209                 return;
1210         if (console_blanked) {
1211                 timer_table[BLANK_TIMER].expires = 0;
1212                 timer_active |= 1<<BLANK_TIMER;
1213         } else if (blankinterval) {
1214                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1215                 timer_active |= 1<<BLANK_TIMER;
1216         }
1217 }       
1218 
1219 void * memsetw(void * s,unsigned short c,int count)
     /* [previous][next][first][last][top][bottom][index][help] */
1220 {
1221 __asm__("cld\n\t"
1222         "rep\n\t"
1223         "stosw"
1224         ::"a" (c),"D" (s),"c" (count)
1225         :"cx","di");
1226 return s;
1227 }
1228 
1229 /*
1230  *  long con_init(long);
1231  *
1232  * This routine initalizes console interrupts, and does nothing
1233  * else. If you want the screen to clear, call tty_write with
1234  * the appropriate escape-sequece.
1235  *
1236  * Reads the information preserved by setup.s to determine the current display
1237  * type and sets everything accordingly.
1238  */
1239 long con_init(long kmem_start)
     /* [previous][next][first][last][top][bottom][index][help] */
1240 {
1241         char *display_desc = "????";
1242         char *display_ptr;
1243         int currcons = 0;
1244         long base;
1245         int orig_x = ORIG_X;
1246         int orig_y = ORIG_Y;
1247 
1248         vc_scrmembuf = (unsigned short *) kmem_start;
1249         video_num_columns = ORIG_VIDEO_COLS;
1250         video_size_row = video_num_columns * 2;
1251         video_num_lines = ORIG_VIDEO_LINES;
1252         video_page = ORIG_VIDEO_PAGE;
1253         screen_size = (video_num_lines * video_size_row);
1254         kmem_start += NR_CONSOLES * screen_size;
1255         timer_table[BLANK_TIMER].fn = blank_screen;
1256         timer_table[BLANK_TIMER].expires = 0;
1257         if (blankinterval) {
1258                 timer_table[BLANK_TIMER].expires = jiffies+blankinterval;
1259                 timer_active |= 1<<BLANK_TIMER;
1260         }
1261         
1262         if (ORIG_VIDEO_MODE == 7)       /* Is this a monochrome display? */
1263         {
1264                 video_mem_base = 0xb0000;
1265                 video_port_reg = 0x3b4;
1266                 video_port_val = 0x3b5;
1267                 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1268                 {
1269                         video_type = VIDEO_TYPE_EGAM;
1270                         video_mem_term = 0xb8000;
1271                         display_desc = "EGAm";
1272                 }
1273                 else
1274                 {
1275                         video_type = VIDEO_TYPE_MDA;
1276                         video_mem_term = 0xb2000;
1277                         display_desc = "*MDA";
1278                 }
1279         }
1280         else                            /* If not, it is color. */
1281         {
1282                 can_do_color = 1;
1283                 video_mem_base = 0xb8000;
1284                 video_port_reg  = 0x3d4;
1285                 video_port_val  = 0x3d5;
1286                 if ((ORIG_VIDEO_EGA_BX & 0xff) != 0x10)
1287                 {
1288                         video_type = VIDEO_TYPE_EGAC;
1289                         video_mem_term = 0xc0000;
1290                         display_desc = "EGAc";
1291                 }
1292                 else
1293                 {
1294                         video_type = VIDEO_TYPE_CGA;
1295                         video_mem_term = 0xba000;
1296                         display_desc = "*CGA";
1297                 }
1298         }
1299         
1300         /* Let the user know what kind of display driver we are using */
1301         
1302         display_ptr = ((char *)video_mem_base) + video_size_row - 8;
1303         while (*display_desc)
1304         {
1305                 *display_ptr++ = *display_desc++;
1306                 display_ptr++;
1307         }
1308 
1309         /* Initialize the variables used for scrolling (mostly EGA/VGA) */
1310 
1311         base = (long)vc_scrmembuf;
1312         for (currcons = 0; currcons<NR_CONSOLES; currcons++) {
1313                 pos = origin = video_mem_start = base;
1314                 scr_end = video_mem_end = (base += screen_size);
1315                 vc_scrbuf[currcons] = (unsigned short *) origin;
1316                 vcmode          = KD_TEXT;
1317                 vtmode.mode     = VT_AUTO;
1318                 vtmode.waitv    = 0;
1319                 vtmode.relsig   = 0;
1320                 vtmode.acqsig   = 0;
1321                 vtmode.frsig    = 0;
1322                 vtpid           = -1;
1323                 vtnewvt         = -1;
1324                 clr_kbd(kbdraw);
1325                 def_color       = 0x07;   /* white */
1326                 ulcolor         = 0x0f;   /* bold white */
1327                 halfcolor       = 0x08;   /* grey */
1328                 reset_terminal(currcons, currcons);
1329         }
1330         currcons = fg_console = 0;
1331 
1332         video_mem_start = video_mem_base;
1333         video_mem_end = video_mem_term;
1334         origin = video_mem_start;
1335         scr_end = video_mem_start + video_num_lines * video_size_row;
1336         gotoxy(currcons,0,0);
1337         save_cur(currcons);
1338         gotoxy(currcons,orig_x,orig_y);
1339         update_screen(fg_console);
1340         return kmem_start;
1341 }
1342 
1343 /*
1344  * kbdsave doesn't need to do anything: it's all handled automatically
1345  * with the new data structures..
1346  */
1347 void kbdsave(int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
1348 {
1349 }
1350 
1351 static void get_scrmem(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
1352 {
1353         memcpy((void *)vc_scrbuf[fg_console],(void *)origin, screen_size);
1354         video_mem_start = (unsigned long)vc_scrbuf[fg_console];
1355         origin  = video_mem_start;
1356         scr_end = video_mem_end = video_mem_start+screen_size;
1357         pos = origin + y*video_size_row + (x<<1);
1358 }
1359 
1360 static void set_scrmem(int currcons)
     /* [previous][next][first][last][top][bottom][index][help] */
1361 {
1362         video_mem_start = video_mem_base;
1363         video_mem_end = video_mem_term;
1364         origin  = video_mem_start;
1365         scr_end = video_mem_start + screen_size;
1366         pos = origin + y*video_size_row + (x<<1);
1367         memcpy((void *)video_mem_base, (void *)vc_scrbuf[fg_console], screen_size);
1368 }
1369 
1370 void blank_screen(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1371 {
1372         if (console_blanked)
1373                 return;
1374         timer_table[BLANK_TIMER].fn = unblank_screen;
1375         get_scrmem(fg_console);
1376         hide_cursor(fg_console);
1377         console_blanked = 1;
1378         memsetw((void *)video_mem_base, 0x0020, video_mem_term-video_mem_base );
1379 }
1380 
1381 void unblank_screen(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1382 {
1383         if (!console_blanked)
1384                 return;
1385         timer_table[BLANK_TIMER].fn = blank_screen;
1386         if (blankinterval) {
1387                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1388                 timer_active |= 1<<BLANK_TIMER;
1389         }
1390         console_blanked = 0;
1391         set_scrmem(fg_console);
1392         set_origin(fg_console);
1393         set_cursor(fg_console);
1394 }
1395 
1396 void update_screen(int new_console)
     /* [previous][next][first][last][top][bottom][index][help] */
1397 {
1398         static int lock = 0;
1399 
1400         if (new_console == fg_console || lock)
1401                 return;
1402         lock = 1;
1403         kbdsave(new_console);
1404         get_scrmem(fg_console); 
1405         fg_console = new_console;
1406         set_scrmem(fg_console); 
1407         set_origin(fg_console);
1408         set_cursor(new_console);
1409         set_leds();
1410         lock = 0;
1411 }
1412 
1413 /* from bsd-net-2: */
1414 
1415 static void sysbeepstop(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1416 {
1417         /* disable counter 2 */
1418         outb(inb_p(0x61)&0xFC, 0x61);
1419 }
1420 
1421 static void sysbeep(void)
     /* [previous][next][first][last][top][bottom][index][help] */
1422 {
1423         /* enable counter 2 */
1424         outb_p(inb_p(0x61)|3, 0x61);
1425         /* set command for counter 2, 2 byte write */
1426         outb_p(0xB6, 0x43);
1427         /* send 0x637 for 750 HZ */
1428         outb_p(0x37, 0x42);
1429         outb(0x06, 0x42);
1430         /* 1/8 second */
1431         timer_table[BEEP_TIMER].expires = jiffies + HZ/8;
1432         timer_table[BEEP_TIMER].fn = sysbeepstop;
1433         timer_active |= 1<<BEEP_TIMER;
1434 }
1435 
1436 int do_screendump(int arg)
     /* [previous][next][first][last][top][bottom][index][help] */
1437 {
1438         char *sptr, *buf = (char *)arg;
1439         int currcons, l;
1440 
1441         verify_area(buf,2+video_num_columns*video_num_lines);
1442         currcons = get_fs_byte(buf+1);
1443         if ((currcons<0) || (currcons>=NR_CONSOLES))
1444                 return -EIO;
1445         put_fs_byte((char)(video_num_lines),buf++);     
1446         put_fs_byte((char)(video_num_columns),buf++);
1447         currcons = (currcons ? currcons-1 : fg_console);
1448         sptr = (char *) origin;
1449         for (l=video_num_lines*video_num_columns; l>0 ; l--, sptr++)
1450                 put_fs_byte(*sptr++,buf++);     
1451         return(0);
1452 }
1453 
1454 void console_print(const char * b)
     /* [previous][next][first][last][top][bottom][index][help] */
1455 {
1456         int currcons = fg_console;
1457         char c;
1458 
1459         if (currcons<0 || currcons>=NR_CONSOLES)
1460                 currcons = 0;
1461         while ((c = *(b++)) != 0) {
1462                 if (c == 10 || c == 13 || need_wrap) {
1463                         if (c != 13)
1464                                 lf(currcons);
1465                         cr(currcons);
1466                         if (c == 10 || c == 13)
1467                                 continue;
1468                 }
1469                 *(char *) pos = c;
1470                 *(char *) (pos+1) = attr;
1471                 if (x == video_num_columns - 1) {
1472                         need_wrap = 1;
1473                         continue;
1474                 }
1475                 x++;
1476                 pos+=2;
1477         }
1478         set_cursor(currcons);
1479         if (vt_cons[fg_console].vc_mode == KD_GRAPHICS)
1480                 return;
1481         timer_active &= ~(1<<BLANK_TIMER);
1482         if (console_blanked) {
1483                 timer_table[BLANK_TIMER].expires = 0;
1484                 timer_active |= 1<<BLANK_TIMER;
1485         } else if (blankinterval) {
1486                 timer_table[BLANK_TIMER].expires = jiffies + blankinterval;
1487                 timer_active |= 1<<BLANK_TIMER;
1488         }
1489 }
1490 
1491 /*
1492  * All we do is set the write and ioctl subroutines; later on maybe we'll
1493  * dynamically allocate the console screen memory.
1494  */
1495 int con_open(struct tty_struct *tty, struct file * filp)
     /* [previous][next][first][last][top][bottom][index][help] */
1496 {
1497         tty->write = con_write;
1498         tty->ioctl = vt_ioctl;
1499         if (tty->line > NR_CONSOLES)
1500                 return -ENODEV;
1501         return 0;
1502 }

/* [previous][next][first][last][top][bottom][index][help] */