root/drivers/char/n_tty.c

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

DEFINITIONS

This source file includes following definitions.
  1. put_tty_queue
  2. n_tty_flush_buffer
  3. n_tty_chars_in_buffer
  4. opost
  5. put_char
  6. echo_char
  7. finish_erasing
  8. eraser
  9. isig
  10. n_tty_receive_break
  11. n_tty_receive_overrun
  12. n_tty_receive_parity_error
  13. n_tty_receive_char
  14. n_tty_receive_buf
  15. n_tty_receive_room
  16. is_ignored
  17. n_tty_set_termios
  18. n_tty_close
  19. n_tty_open
  20. input_available_p
  21. copy_from_read_buf
  22. read_chan
  23. write_chan
  24. normal_select

   1 /*
   2  * n_tty.c --- implements the N_TTY line discipline.
   3  * 
   4  * This code used to be in tty_io.c, but things are getting hairy
   5  * enough that it made sense to split things off.  (The N_TTY
   6  * processing has changed so much that it's hardly recognizable,
   7  * anyway...)
   8  *
   9  * Note that the open routine for N_TTY is guaranteed never to return
  10  * an error.  This is because Linux will fall back to setting a line
  11  * to N_TTY if it can not switch to any other line discipline.  
  12  *
  13  * Written by Theodore Ts'o, Copyright 1994.
  14  * 
  15  * This file also contains code originally written by Linus Torvalds,
  16  * Copyright 1991, 1992, 1993, and by Julian Cowley, Copyright 1994.
  17  * 
  18  * This file may be redistributed under the terms of the GNU Public
  19  * License.
  20  */
  21 
  22 #include <linux/types.h>
  23 #include <linux/major.h>
  24 #include <linux/errno.h>
  25 #include <linux/signal.h>
  26 #include <linux/fcntl.h>
  27 #include <linux/sched.h>
  28 #include <linux/interrupt.h>
  29 #include <linux/tty.h>
  30 #include <linux/timer.h>
  31 #include <linux/ctype.h>
  32 #include <linux/kd.h>
  33 #include <linux/mm.h>
  34 #include <linux/string.h>
  35 #include <linux/malloc.h>
  36 
  37 #include <asm/segment.h>
  38 #include <asm/system.h>
  39 #include <asm/bitops.h>
  40 
  41 #define CONSOLE_DEV MKDEV(TTY_MAJOR,0)
  42 
  43 #ifndef MIN
  44 #define MIN(a,b)        ((a) < (b) ? (a) : (b))
  45 #endif
  46 
  47 /* number of characters left in xmit buffer before select has we have room */
  48 #define WAKEUP_CHARS 256
  49 
  50 /*
  51  * This defines the low- and high-watermarks for throttling and
  52  * unthrottling the TTY driver.  These watermarks are used for
  53  * controlling the space in the read buffer.
  54  */
  55 #define TTY_THRESHOLD_THROTTLE          (N_TTY_BUF_SIZE - 128)
  56 #define TTY_THRESHOLD_UNTHROTTLE        128
  57 
  58 static inline void put_tty_queue(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  59 {
  60         if (tty->read_cnt < N_TTY_BUF_SIZE) {
  61                 tty->read_buf[tty->read_head] = c;
  62                 tty->read_head = (tty->read_head + 1) & (N_TTY_BUF_SIZE-1);
  63                 tty->read_cnt++;
  64         }
  65 }
  66 
  67 /*
  68  * Flush the input buffer
  69  */
  70 void n_tty_flush_buffer(struct tty_struct * tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  71 {
  72         tty->read_head = tty->read_tail = tty->read_cnt = 0;
  73         tty->canon_head = tty->canon_data = tty->erasing = 0;
  74         memset(&tty->read_flags, 0, sizeof tty->read_flags);
  75         
  76         if (!tty->link)
  77                 return;
  78 
  79         if (tty->driver.unthrottle)
  80                 (tty->driver.unthrottle)(tty);
  81         if (tty->link->packet) {
  82                 tty->ctrl_status |= TIOCPKT_FLUSHREAD;
  83                 wake_up_interruptible(&tty->link->read_wait);
  84         }
  85 }
  86 
  87 /*
  88  * Return number of characters buffered to be delivered to user
  89  */
  90 int n_tty_chars_in_buffer(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
  91 {
  92         return tty->read_cnt;
  93 }
  94 
  95 /*
  96  * Perform OPOST processing.  Returns -1 when the output device is
  97  * full and the character must be retried.
  98  */
  99 static int opost(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101         int     space, spaces;
 102 
 103         space = tty->driver.write_room(tty);
 104         if (!space)
 105                 return -1;
 106 
 107         if (O_OPOST(tty)) {
 108                 switch (c) {
 109                 case '\n':
 110                         if (O_ONLRET(tty))
 111                                 tty->column = 0;
 112                         if (O_ONLCR(tty)) {
 113                                 if (space < 2)
 114                                         return -1;
 115                                 tty->driver.put_char(tty, '\r');
 116                                 tty->column = 0;
 117                         }
 118                         tty->canon_column = tty->column;
 119                         break;
 120                 case '\r':
 121                         if (O_ONOCR(tty) && tty->column == 0)
 122                                 return 0;
 123                         if (O_OCRNL(tty)) {
 124                                 c = '\n';
 125                                 if (O_ONLRET(tty))
 126                                         tty->canon_column = tty->column = 0;
 127                                 break;
 128                         }
 129                         tty->canon_column = tty->column = 0;
 130                         break;
 131                 case '\t':
 132                         spaces = 8 - (tty->column & 7);
 133                         if (O_TABDLY(tty) == XTABS) {
 134                                 if (space < spaces)
 135                                         return -1;
 136                                 tty->column += spaces;
 137                                 tty->driver.write(tty, 0, "        ", spaces);
 138                                 return 0;
 139                         }
 140                         tty->column += spaces;
 141                         break;
 142                 case '\b':
 143                         if (tty->column > 0)
 144                                 tty->column--;
 145                         break;
 146                 default:
 147                         if (O_OLCUC(tty))
 148                                 c = toupper(c);
 149                         if (!iscntrl(c))
 150                                 tty->column++;
 151                         break;
 152                 }
 153         }
 154         tty->driver.put_char(tty, c);
 155         return 0;
 156 }
 157 
 158 static inline void put_char(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 159 {
 160         tty->driver.put_char(tty, c);
 161 }
 162 
 163 /* Must be called only when L_ECHO(tty) is true. */
 164 
 165 static void echo_char(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 166 {
 167         if (L_ECHOCTL(tty) && iscntrl(c) && c != '\t') {
 168                 put_char('^', tty);
 169                 put_char(c ^ 0100, tty);
 170                 tty->column += 2;
 171         } else
 172                 opost(c, tty);
 173 }
 174 
 175 static inline void finish_erasing(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177         if (tty->erasing) {
 178                 put_char('/', tty);
 179                 tty->column += 2;
 180                 tty->erasing = 0;
 181         }
 182 }
 183 
 184 static void eraser(unsigned char c, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 185 {
 186         enum { ERASE, WERASE, KILL } kill_type;
 187         int head, seen_alnums;
 188 
 189         if (tty->read_head == tty->canon_head) {
 190                 /* opost('\a', tty); */         /* what do you think? */
 191                 return;
 192         }
 193         if (c == ERASE_CHAR(tty))
 194                 kill_type = ERASE;
 195         else if (c == WERASE_CHAR(tty))
 196                 kill_type = WERASE;
 197         else {
 198                 if (!L_ECHO(tty)) {
 199                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
 200                                           (N_TTY_BUF_SIZE - 1));
 201                         tty->read_head = tty->canon_head;
 202                         return;
 203                 }
 204                 if (!L_ECHOK(tty) || !L_ECHOKE(tty)) {
 205                         tty->read_cnt -= ((tty->read_head - tty->canon_head) &
 206                                           (N_TTY_BUF_SIZE - 1));
 207                         tty->read_head = tty->canon_head;
 208                         finish_erasing(tty);
 209                         echo_char(KILL_CHAR(tty), tty);
 210                         /* Add a newline if ECHOK is on and ECHOKE is off. */
 211                         if (L_ECHOK(tty))
 212                                 opost('\n', tty);
 213                         return;
 214                 }
 215                 kill_type = KILL;
 216         }
 217 
 218         seen_alnums = 0;
 219         while (tty->read_head != tty->canon_head) {
 220                 head = (tty->read_head - 1) & (N_TTY_BUF_SIZE-1);
 221                 c = tty->read_buf[head];
 222                 if (kill_type == WERASE) {
 223                         /* Equivalent to BSD's ALTWERASE. */
 224                         if (isalnum(c) || c == '_')
 225                                 seen_alnums++;
 226                         else if (seen_alnums)
 227                                 break;
 228                 }
 229                 tty->read_head = head;
 230                 tty->read_cnt--;
 231                 if (L_ECHO(tty)) {
 232                         if (L_ECHOPRT(tty)) {
 233                                 if (!tty->erasing) {
 234                                         put_char('\\', tty);
 235                                         tty->column++;
 236                                         tty->erasing = 1;
 237                                 }
 238                                 echo_char(c, tty);
 239                         } else if (!L_ECHOE(tty)) {
 240                                 echo_char(ERASE_CHAR(tty), tty);
 241                         } else if (c == '\t') {
 242                                 unsigned int col = tty->canon_column;
 243                                 unsigned long tail = tty->canon_head;
 244 
 245                                 /* Find the column of the last char. */
 246                                 while (tail != tty->read_head) {
 247                                         c = tty->read_buf[tail];
 248                                         if (c == '\t')
 249                                                 col = (col | 7) + 1;
 250                                         else if (iscntrl(c)) {
 251                                                 if (L_ECHOCTL(tty))
 252                                                         col += 2;
 253                                         } else
 254                                                 col++;
 255                                         tail = (tail+1) & (N_TTY_BUF_SIZE-1);
 256                                 }
 257 
 258                                 /* Now backup to that column. */
 259                                 while (tty->column > col) {
 260                                         /* Can't use opost here. */
 261                                         put_char('\b', tty);
 262                                         tty->column--;
 263                                 }
 264                         } else {
 265                                 if (iscntrl(c) && L_ECHOCTL(tty)) {
 266                                         put_char('\b', tty);
 267                                         put_char(' ', tty);
 268                                         put_char('\b', tty);
 269                                         tty->column--;
 270                                 }
 271                                 if (!iscntrl(c) || L_ECHOCTL(tty)) {
 272                                         put_char('\b', tty);
 273                                         put_char(' ', tty);
 274                                         put_char('\b', tty);
 275                                         tty->column--;
 276                                 }
 277                         }
 278                 }
 279                 if (kill_type == ERASE)
 280                         break;
 281         }
 282         if (tty->read_head == tty->canon_head)
 283                 finish_erasing(tty);
 284 }
 285 
 286 static void isig(int sig, struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 287 {
 288         if (tty->pgrp > 0)
 289                 kill_pg(tty->pgrp, sig, 1);
 290         if (!L_NOFLSH(tty)) {
 291                 n_tty_flush_buffer(tty);
 292                 if (tty->driver.flush_buffer)
 293                         tty->driver.flush_buffer(tty);
 294         }
 295 }
 296 
 297 static inline void n_tty_receive_break(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 298 {
 299         if (I_IGNBRK(tty))
 300                 return;
 301         if (I_BRKINT(tty)) {
 302                 isig(SIGINT, tty);
 303                 return;
 304         }
 305         if (I_PARMRK(tty)) {
 306                 put_tty_queue('\377', tty);
 307                 put_tty_queue('\0', tty);
 308         }
 309         put_tty_queue('\0', tty);
 310         wake_up_interruptible(&tty->read_wait);
 311 }
 312 
 313 static inline void n_tty_receive_overrun(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 314 {
 315         char buf[64];
 316 
 317         tty->num_overrun++;
 318         if (tty->overrun_time < (jiffies - HZ)) {
 319                 printk("%s: %d input overrun(s)\n", _tty_name(tty, buf),
 320                        tty->num_overrun);
 321                 tty->overrun_time = jiffies;
 322                 tty->num_overrun = 0;
 323         }
 324 }
 325 
 326 static inline void n_tty_receive_parity_error(struct tty_struct *tty,
     /* [previous][next][first][last][top][bottom][index][help] */
 327                                               unsigned char c)
 328 {
 329         if (I_IGNPAR(tty)) {
 330                 return;
 331         }
 332         if (I_PARMRK(tty)) {
 333                 put_tty_queue('\377', tty);
 334                 put_tty_queue('\0', tty);
 335                 put_tty_queue(c, tty);
 336         } else
 337                 put_tty_queue('\0', tty);
 338         wake_up_interruptible(&tty->read_wait);
 339 }
 340 
 341 static inline void n_tty_receive_char(struct tty_struct *tty, unsigned char c)
     /* [previous][next][first][last][top][bottom][index][help] */
 342 {
 343         if (tty->raw) {
 344                 put_tty_queue(c, tty);
 345                 return;
 346         }
 347         
 348         if (tty->stopped && I_IXON(tty) && I_IXANY(tty) && L_IEXTEN(tty)) {
 349                 start_tty(tty);
 350                 return;
 351         }
 352         
 353         if (I_ISTRIP(tty))
 354                 c &= 0x7f;
 355         if (I_IUCLC(tty) && L_IEXTEN(tty))
 356                 c=tolower(c);
 357 
 358         /*
 359          * If the previous character was LNEXT, or we know that this
 360          * character is not one of the characters that we'll have to
 361          * handle specially, do shortcut processing to speed things
 362          * up.
 363          */
 364         if (!test_bit(c, &tty->process_char_map) || tty->lnext) {
 365                 finish_erasing(tty);
 366                 tty->lnext = 0;
 367                 if (L_ECHO(tty)) {
 368                         if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
 369                                 put_char('\a', tty); /* beep if no space */
 370                                 return;
 371                         }
 372                         /* Record the column of first canon char. */
 373                         if (tty->canon_head == tty->read_head)
 374                                 tty->canon_column = tty->column;
 375                         echo_char(c, tty);
 376                 }
 377                 if (I_PARMRK(tty) && c == (unsigned char) '\377')
 378                         put_tty_queue(c, tty);
 379                 put_tty_queue(c, tty);
 380                 return;
 381         }
 382                 
 383         if (c == '\r') {
 384                 if (I_IGNCR(tty))
 385                         return;
 386                 if (I_ICRNL(tty))
 387                         c = '\n';
 388         } else if (c == '\n' && I_INLCR(tty))
 389                 c = '\r';
 390         if (I_IXON(tty)) {
 391                 if (c == START_CHAR(tty)) {
 392                         start_tty(tty);
 393                         return;
 394                 }
 395                 if (c == STOP_CHAR(tty)) {
 396                         stop_tty(tty);
 397                         return;
 398                 }
 399         }
 400         if (L_ISIG(tty)) {
 401                 if (c == INTR_CHAR(tty)) {
 402                         isig(SIGINT, tty);
 403                         return;
 404                 }
 405                 if (c == QUIT_CHAR(tty)) {
 406                         isig(SIGQUIT, tty);
 407                         return;
 408                 }
 409                 if (c == SUSP_CHAR(tty)) {
 410                         if (!is_orphaned_pgrp(tty->pgrp))
 411                                 isig(SIGTSTP, tty);
 412                         return;
 413                 }
 414         }
 415         if (L_ICANON(tty)) {
 416                 if (c == ERASE_CHAR(tty) || c == KILL_CHAR(tty) ||
 417                     (c == WERASE_CHAR(tty) && L_IEXTEN(tty))) {
 418                         eraser(c, tty);
 419                         return;
 420                 }
 421                 if (c == LNEXT_CHAR(tty) && L_IEXTEN(tty)) {
 422                         tty->lnext = 1;
 423                         if (L_ECHO(tty)) {
 424                                 finish_erasing(tty);
 425                                 if (L_ECHOCTL(tty)) {
 426                                         put_char('^', tty);
 427                                         put_char('\b', tty);
 428                                 }
 429                         }
 430                         return;
 431                 }
 432                 if (c == REPRINT_CHAR(tty) && L_ECHO(tty) &&
 433                     L_IEXTEN(tty)) {
 434                         unsigned long tail = tty->canon_head;
 435 
 436                         finish_erasing(tty);
 437                         echo_char(c, tty);
 438                         opost('\n', tty);
 439                         while (tail != tty->read_head) {
 440                                 echo_char(tty->read_buf[tail], tty);
 441                                 tail = (tail+1) & (N_TTY_BUF_SIZE-1);
 442                         }
 443                         return;
 444                 }
 445                 if (c == '\n') {
 446                         if (L_ECHO(tty) || L_ECHONL(tty)) {
 447                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
 448                                         put_char('\a', tty);
 449                                         return;
 450                                 }
 451                                 opost('\n', tty);
 452                         }
 453                         goto handle_newline;
 454                 }
 455                 if (c == EOF_CHAR(tty)) {
 456                         if (tty->canon_head != tty->read_head)
 457                                 set_bit(TTY_PUSH, &tty->flags);
 458                         c = __DISABLED_CHAR;
 459                         goto handle_newline;
 460                 }
 461                 if ((c == EOL_CHAR(tty)) ||
 462                     (c == EOL2_CHAR(tty) && L_IEXTEN(tty))) {
 463                         /*
 464                          * XXX are EOL_CHAR and EOL2_CHAR echoed?!?
 465                          */
 466                         if (L_ECHO(tty)) {
 467                                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
 468                                         put_char('\a', tty);
 469                                         return;
 470                                 }
 471                                 /* Record the column of first canon char. */
 472                                 if (tty->canon_head == tty->read_head)
 473                                         tty->canon_column = tty->column;
 474                                 echo_char(c, tty);
 475                         }
 476                         /*
 477                          * XXX does PARMRK doubling happen for
 478                          * EOL_CHAR and EOL2_CHAR?
 479                          */
 480                         if (I_PARMRK(tty) && c == (unsigned char) '\377')
 481                                 put_tty_queue(c, tty);
 482 
 483                 handle_newline:
 484                         set_bit(tty->read_head, &tty->read_flags);
 485                         put_tty_queue(c, tty);
 486                         tty->canon_head = tty->read_head;
 487                         tty->canon_data++;
 488                         if (tty->fasync)
 489                                 kill_fasync(tty->fasync, SIGIO);
 490                         if (tty->read_wait)
 491                                 wake_up_interruptible(&tty->read_wait);
 492                         return;
 493                 }
 494         }
 495         
 496         finish_erasing(tty);
 497         if (L_ECHO(tty)) {
 498                 if (tty->read_cnt >= N_TTY_BUF_SIZE-1) {
 499                         put_char('\a', tty); /* beep if no space */
 500                         return;
 501                 }
 502                 if (c == '\n')
 503                         opost('\n', tty);
 504                 else {
 505                         /* Record the column of first canon char. */
 506                         if (tty->canon_head == tty->read_head)
 507                                 tty->canon_column = tty->column;
 508                         echo_char(c, tty);
 509                 }
 510         }
 511 
 512         if (I_PARMRK(tty) && c == (unsigned char) '\377')
 513                 put_tty_queue(c, tty);
 514 
 515         put_tty_queue(c, tty);
 516 }       
 517 
 518 static void n_tty_receive_buf(struct tty_struct *tty, unsigned char *cp,
     /* [previous][next][first][last][top][bottom][index][help] */
 519                               char *fp, int count)
 520 {
 521         unsigned char *p;
 522         char *f, flags = 0;
 523         int     i;
 524 
 525         if (!tty->read_buf)
 526                 return;
 527 
 528         if (tty->real_raw) {
 529                 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
 530                                    N_TTY_BUF_SIZE - tty->read_head));
 531                 memcpy(tty->read_buf + tty->read_head, cp, i);
 532                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
 533                 tty->read_cnt += i;
 534                 cp += i;
 535                 count -= i;
 536 
 537                 i = MIN(count, MIN(N_TTY_BUF_SIZE - tty->read_cnt,
 538                                N_TTY_BUF_SIZE - tty->read_head));
 539                 memcpy(tty->read_buf + tty->read_head, cp, i);
 540                 tty->read_head = (tty->read_head + i) & (N_TTY_BUF_SIZE-1);
 541                 tty->read_cnt += i;
 542         } else {
 543                 for (i=count, p = cp, f = fp; i; i--, p++) {
 544                         if (f)
 545                                 flags = *f++;
 546                         switch (flags) {
 547                         case TTY_NORMAL:
 548                                 n_tty_receive_char(tty, *p);
 549                                 break;
 550                         case TTY_BREAK:
 551                                 n_tty_receive_break(tty);
 552                                 break;
 553                         case TTY_PARITY:
 554                         case TTY_FRAME:
 555                                 n_tty_receive_parity_error(tty, *p);
 556                                 break;
 557                         case TTY_OVERRUN:
 558                                 n_tty_receive_overrun(tty);
 559                                 break;
 560                         default:
 561                                 printk("%s: unknown flag %d\n", tty_name(tty),
 562                                        flags);
 563                                 break;
 564                         }
 565                 }
 566                 if (tty->driver.flush_chars)
 567                         tty->driver.flush_chars(tty);
 568         }
 569 
 570         if (!tty->icanon && (tty->read_cnt >= tty->minimum_to_wake)) {
 571                 if (tty->fasync)
 572                         kill_fasync(tty->fasync, SIGIO);
 573                 if (tty->read_wait)
 574                         wake_up_interruptible(&tty->read_wait);
 575         }
 576 
 577         if ((tty->read_cnt >= TTY_THRESHOLD_THROTTLE) &&
 578             tty->driver.throttle &&
 579             !set_bit(TTY_THROTTLED, &tty->flags))
 580                 tty->driver.throttle(tty);
 581 }
 582 
 583 static int n_tty_receive_room(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 584 {
 585         int     left = N_TTY_BUF_SIZE - tty->read_cnt - 1;
 586 
 587         /*
 588          * If we are doing input canonicalization, and there are no
 589          * pending newlines, let characters through without limit, so
 590          * that erase characters will be handled.  Other excess
 591          * characters will be beeped.
 592          */
 593         if (tty->icanon && !tty->canon_data)
 594                 return N_TTY_BUF_SIZE;
 595 
 596         if (left > 0)
 597                 return left;
 598         return 0;
 599 }
 600 
 601 int is_ignored(int sig)
     /* [previous][next][first][last][top][bottom][index][help] */
 602 {
 603         return ((current->blocked & (1<<(sig-1))) ||
 604                 (current->sigaction[sig-1].sa_handler == SIG_IGN));
 605 }
 606 
 607 static void n_tty_set_termios(struct tty_struct *tty, struct termios * old)
     /* [previous][next][first][last][top][bottom][index][help] */
 608 {
 609         if (!tty)
 610                 return;
 611         
 612         tty->icanon = (L_ICANON(tty) != 0);
 613         if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
 614             I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
 615             I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
 616             I_PARMRK(tty)) {
 617                 cli();
 618                 memset(tty->process_char_map, 0, 256/32);
 619 
 620                 if (I_IGNCR(tty) || I_ICRNL(tty))
 621                         set_bit('\r', &tty->process_char_map);
 622                 if (I_INLCR(tty))
 623                         set_bit('\n', &tty->process_char_map);
 624 
 625                 if (L_ICANON(tty)) {
 626                         set_bit(ERASE_CHAR(tty), &tty->process_char_map);
 627                         set_bit(KILL_CHAR(tty), &tty->process_char_map);
 628                         set_bit(EOF_CHAR(tty), &tty->process_char_map);
 629                         set_bit('\n', &tty->process_char_map);
 630                         set_bit(EOL_CHAR(tty), &tty->process_char_map);
 631                         if (L_IEXTEN(tty)) {
 632                                 set_bit(WERASE_CHAR(tty),
 633                                         &tty->process_char_map);
 634                                 set_bit(LNEXT_CHAR(tty),
 635                                         &tty->process_char_map);
 636                                 set_bit(EOL2_CHAR(tty),
 637                                         &tty->process_char_map);
 638                                 if (L_ECHO(tty))
 639                                         set_bit(REPRINT_CHAR(tty),
 640                                                 &tty->process_char_map);
 641                         }
 642                 }
 643                 if (I_IXON(tty)) {
 644                         set_bit(START_CHAR(tty), &tty->process_char_map);
 645                         set_bit(STOP_CHAR(tty), &tty->process_char_map);
 646                 }
 647                 if (L_ISIG(tty)) {
 648                         set_bit(INTR_CHAR(tty), &tty->process_char_map);
 649                         set_bit(QUIT_CHAR(tty), &tty->process_char_map);
 650                         set_bit(SUSP_CHAR(tty), &tty->process_char_map);
 651                 }
 652                 clear_bit(__DISABLED_CHAR, &tty->process_char_map);
 653                 sti();
 654                 tty->raw = 0;
 655                 tty->real_raw = 0;
 656         } else {
 657                 tty->raw = 1;
 658                 if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
 659                     (I_IGNPAR(tty) || !I_INPCK(tty)) &&
 660                     (tty->driver.flags & TTY_DRIVER_REAL_RAW))
 661                         tty->real_raw = 1;
 662                 else
 663                         tty->real_raw = 0;
 664         }
 665 }
 666 
 667 static void n_tty_close(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 668 {
 669         tty_wait_until_sent(tty, 0);
 670         n_tty_flush_buffer(tty);
 671         if (tty->read_buf) {
 672                 free_page((unsigned long) tty->read_buf);
 673                 tty->read_buf = 0;
 674         }
 675 }
 676 
 677 static int n_tty_open(struct tty_struct *tty)
     /* [previous][next][first][last][top][bottom][index][help] */
 678 {
 679         if (!tty)
 680                 return -EINVAL;
 681 
 682         if (!tty->read_buf) {
 683                 tty->read_buf = (unsigned char *)
 684                         get_free_page(intr_count ? GFP_ATOMIC : GFP_KERNEL);
 685                 if (!tty->read_buf)
 686                         return -ENOMEM;
 687         }
 688         memset(tty->read_buf, 0, N_TTY_BUF_SIZE);
 689         tty->read_head = tty->read_tail = tty->read_cnt = 0;
 690         memset(tty->read_flags, 0, sizeof(tty->read_flags));
 691         n_tty_set_termios(tty, 0);
 692         tty->minimum_to_wake = 1;
 693         return 0;
 694 }
 695 
 696 static inline int input_available_p(struct tty_struct *tty, int amt)
     /* [previous][next][first][last][top][bottom][index][help] */
 697 {
 698         if (L_ICANON(tty)) {
 699                 if (tty->canon_data)
 700                         return 1;
 701         } else if (tty->read_cnt >= (amt ? amt : 1))
 702                 return 1;
 703 
 704         return 0;
 705 }
 706 
 707 /*
 708  * Helper function to speed up read_chan.  It is only called when
 709  * ICANON is off; it copies characters straight from the tty queue to
 710  * user space directly.  It can be profitably called twice; once to
 711  * drain the space from the tail pointer to the (physical) end of the
 712  * buffer, and once to drain the space from the (physical) beginning of
 713  * the buffer to head pointer.
 714  */
 715 static inline void copy_from_read_buf(struct tty_struct *tty,
     /* [previous][next][first][last][top][bottom][index][help] */
 716                                       unsigned char **b,
 717                                       unsigned int *nr)
 718 
 719 {
 720         int     n;
 721 
 722         n = MIN(*nr, MIN(tty->read_cnt, N_TTY_BUF_SIZE - tty->read_tail));
 723         if (!n)
 724                 return;
 725         memcpy_tofs(*b, &tty->read_buf[tty->read_tail], n);
 726         tty->read_tail = (tty->read_tail + n) & (N_TTY_BUF_SIZE-1);
 727         tty->read_cnt -= n;
 728         *b += n;
 729         *nr -= n;
 730 }
 731 
 732 static int read_chan(struct tty_struct *tty, struct file *file,
     /* [previous][next][first][last][top][bottom][index][help] */
 733                      unsigned char *buf, unsigned int nr)
 734 {
 735         struct wait_queue wait = { current, NULL };
 736         int c;
 737         unsigned char *b = buf;
 738         int minimum, time;
 739         int retval = 0;
 740         int size;
 741 
 742 do_it_again:
 743 
 744         if (!tty->read_buf) {
 745                 printk("n_tty_read_chan: called with read_buf == NULL?!?\n");
 746                 return -EIO;
 747         }
 748 
 749         /* Job control check -- must be done at start and after
 750            every sleep (POSIX.1 7.1.1.4). */
 751         /* NOTE: not yet done after every sleep pending a thorough
 752            check of the logic of this change. -- jlc */
 753         /* don't stop on /dev/console */
 754         if (file->f_inode->i_rdev != CONSOLE_DEV &&
 755             current->tty == tty) {
 756                 if (tty->pgrp <= 0)
 757                         printk("read_chan: tty->pgrp <= 0!\n");
 758                 else if (current->pgrp != tty->pgrp) {
 759                         if (is_ignored(SIGTTIN) ||
 760                             is_orphaned_pgrp(current->pgrp))
 761                                 return -EIO;
 762                         kill_pg(current->pgrp, SIGTTIN, 1);
 763                         return -ERESTARTSYS;
 764                 }
 765         }
 766 
 767         if (L_ICANON(tty)) {
 768                 minimum = time = 0;
 769                 current->timeout = (unsigned long) -1;
 770         } else {
 771                 time = (HZ / 10) * TIME_CHAR(tty);
 772                 minimum = MIN_CHAR(tty);
 773                 if (minimum) {
 774                         current->timeout = (unsigned long) -1;
 775                         if (time)
 776                                 tty->minimum_to_wake = 1;
 777                         else if (!tty->read_wait ||
 778                                  (tty->minimum_to_wake > minimum))
 779                                 tty->minimum_to_wake = minimum;
 780                 } else {
 781                         if (time) {
 782                                 current->timeout = time + jiffies;
 783                                 time = 0;
 784                         } else
 785                                 current->timeout = 0;
 786                         tty->minimum_to_wake = minimum = 1;
 787                 }
 788         }
 789 
 790         add_wait_queue(&tty->read_wait, &wait);
 791         while (1) {
 792                 /* First test for status change. */
 793                 if (tty->packet && tty->link->ctrl_status) {
 794                         if (b != buf)
 795                                 break;
 796                         put_fs_byte(tty->link->ctrl_status, b++);
 797                         tty->link->ctrl_status = 0;
 798                         break;
 799                 }
 800                 /* This statement must be first before checking for input
 801                    so that any interrupt will set the state back to
 802                    TASK_RUNNING. */
 803                 current->state = TASK_INTERRUPTIBLE;
 804                 
 805                 if (((minimum - (b - buf)) < tty->minimum_to_wake) &&
 806                     ((minimum - (b - buf)) >= 1))
 807                         tty->minimum_to_wake = (minimum - (b - buf));
 808                 
 809                 if (!input_available_p(tty, 0)) {
 810                         if (tty->flags & (1 << TTY_SLAVE_CLOSED)) {
 811                                 retval = -EIO;
 812                                 break;
 813                         }
 814                         if (tty_hung_up_p(file))
 815                                 break;
 816                         if (!current->timeout)
 817                                 break;
 818                         if (file->f_flags & O_NONBLOCK) {
 819                                 retval = -EAGAIN;
 820                                 break;
 821                         }
 822                         if (current->signal & ~current->blocked) {
 823                                 retval = -ERESTARTSYS;
 824                                 break;
 825                         }
 826                         schedule();
 827                         continue;
 828                 }
 829                 current->state = TASK_RUNNING;
 830 
 831                 /* Deal with packet mode. */
 832                 if (tty->packet && b == buf) {
 833                         put_fs_byte(TIOCPKT_DATA, b++);
 834                         nr--;
 835                 }
 836 
 837                 if (L_ICANON(tty)) {
 838                         while (1) {
 839                                 int eol;
 840 
 841                                 disable_bh(TQUEUE_BH);
 842                                 if (!tty->read_cnt) {
 843                                         enable_bh(TQUEUE_BH);
 844                                         break;
 845                                 }
 846                                 eol = clear_bit(tty->read_tail,
 847                                                 &tty->read_flags);
 848                                 c = tty->read_buf[tty->read_tail];
 849                                 tty->read_tail = ((tty->read_tail+1) &
 850                                                   (N_TTY_BUF_SIZE-1));
 851                                 tty->read_cnt--;
 852                                 enable_bh(TQUEUE_BH);
 853                                 if (!eol) {
 854                                         put_fs_byte(c, b++);
 855                                         if (--nr)
 856                                                 continue;
 857                                         break;
 858                                 }
 859                                 if (--tty->canon_data < 0) {
 860                                         tty->canon_data = 0;
 861                                 }
 862                                 if (c != __DISABLED_CHAR) {
 863                                         put_fs_byte(c, b++);
 864                                         nr--;
 865                                 }
 866                                 break;
 867                         }
 868                 } else {
 869                         disable_bh(TQUEUE_BH);
 870                         copy_from_read_buf(tty, &b, &nr);
 871                         copy_from_read_buf(tty, &b, &nr);
 872                         enable_bh(TQUEUE_BH);
 873                 }
 874 
 875                 /* If there is enough space in the read buffer now, let the
 876                    low-level driver know. */
 877                 if (tty->driver.unthrottle &&
 878                     (tty->read_cnt <= TTY_THRESHOLD_UNTHROTTLE)
 879                     && clear_bit(TTY_THROTTLED, &tty->flags))
 880                         tty->driver.unthrottle(tty);
 881 
 882                 if (b - buf >= minimum || !nr)
 883                         break;
 884                 if (time)
 885                         current->timeout = time + jiffies;
 886         }
 887         remove_wait_queue(&tty->read_wait, &wait);
 888 
 889         if (!tty->read_wait)
 890                 tty->minimum_to_wake = minimum;
 891 
 892         current->state = TASK_RUNNING;
 893         current->timeout = 0;
 894         size = b - buf;
 895         if (size && nr)
 896                 clear_bit(TTY_PUSH, &tty->flags);
 897         if (!size && clear_bit(TTY_PUSH, &tty->flags))
 898                 goto do_it_again;
 899         if (!size && !retval)
 900                 clear_bit(TTY_PUSH, &tty->flags);
 901         return (size ? size : retval);
 902 }
 903 
 904 static int write_chan(struct tty_struct * tty, struct file * file,
     /* [previous][next][first][last][top][bottom][index][help] */
 905                       unsigned char * buf, unsigned int nr)
 906 {
 907         struct wait_queue wait = { current, NULL };
 908         int c;
 909         unsigned char *b = buf;
 910         int retval = 0;
 911 
 912         /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
 913         if (L_TOSTOP(tty) && file->f_inode->i_rdev != CONSOLE_DEV) {
 914                 retval = tty_check_change(tty);
 915                 if (retval)
 916                         return retval;
 917         }
 918 
 919         add_wait_queue(&tty->write_wait, &wait);
 920         while (1) {
 921                 current->state = TASK_INTERRUPTIBLE;
 922                 if (current->signal & ~current->blocked) {
 923                         retval = -ERESTARTSYS;
 924                         break;
 925                 }
 926                 if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
 927                         retval = -EIO;
 928                         break;
 929                 }
 930                 if (O_OPOST(tty)) {
 931                         while (nr > 0) {
 932                                 c = get_fs_byte(b);
 933                                 if (opost(c, tty) < 0)
 934                                         break;
 935                                 b++; nr--;
 936                         }
 937                         if (tty->driver.flush_chars)
 938                                 tty->driver.flush_chars(tty);
 939                 } else {
 940                         c = tty->driver.write(tty, 1, b, nr);
 941                         b += c;
 942                         nr -= c;
 943                 }
 944                 if (!nr)
 945                         break;
 946                 if (file->f_flags & O_NONBLOCK) {
 947                         retval = -EAGAIN;
 948                         break;
 949                 }
 950                 schedule();
 951         }
 952         current->state = TASK_RUNNING;
 953         remove_wait_queue(&tty->write_wait, &wait);
 954         return (b - buf) ? b - buf : retval;
 955 }
 956 
 957 static int normal_select(struct tty_struct * tty, struct inode * inode,
     /* [previous][next][first][last][top][bottom][index][help] */
 958                          struct file * file, int sel_type, select_table *wait)
 959 {
 960         switch (sel_type) {
 961                 case SEL_IN:
 962                         if (input_available_p(tty, TIME_CHAR(tty) ? 0 :
 963                                               MIN_CHAR(tty)))
 964                                 return 1;
 965                         /* fall through */
 966                 case SEL_EX:
 967                         if (tty->packet && tty->link->ctrl_status)
 968                                 return 1;
 969                         if (tty->flags & (1 << TTY_SLAVE_CLOSED))
 970                                 return 1;
 971                         if (tty_hung_up_p(file))
 972                                 return 1;
 973                         if (!tty->read_wait) {
 974                                 if (MIN_CHAR(tty) && !TIME_CHAR(tty))
 975                                         tty->minimum_to_wake = MIN_CHAR(tty);
 976                                 else
 977                                         tty->minimum_to_wake = 1;
 978                         }
 979                         select_wait(&tty->read_wait, wait);
 980                         return 0;
 981                 case SEL_OUT:
 982                         if (tty->driver.chars_in_buffer(tty) < WAKEUP_CHARS)
 983                                 return 1;
 984                         select_wait(&tty->write_wait, wait);
 985                         return 0;
 986         }
 987         return 0;
 988 }
 989 
 990 struct tty_ldisc tty_ldisc_N_TTY = {
 991         TTY_LDISC_MAGIC,        /* magic */
 992         0,                      /* num */
 993         0,                      /* flags */
 994         n_tty_open,             /* open */
 995         n_tty_close,            /* close */
 996         n_tty_flush_buffer,     /* flush_buffer */
 997         n_tty_chars_in_buffer,  /* chars_in_buffer */
 998         read_chan,              /* read */
 999         write_chan,             /* write */
1000         n_tty_ioctl,            /* ioctl */
1001         n_tty_set_termios,      /* set_termios */
1002         normal_select,          /* select */
1003         n_tty_receive_buf,      /* receive_buf */
1004         n_tty_receive_room,     /* receive_room */
1005         0                       /* write_wakeup */
1006 };
1007 

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